2 * C Implementation: testovaci
7 * Author: root <root@ubuntu>, (C) 2008
9 * Copyright: See COPYING file that comes with this distribution
14 #include <sys/ioctl.h>
18 #include "hcidriver.h"
21 cmd_state_request array_req[MAX_CMD_STATE_REQUEST];
28 int create_master_connection(connection_hci *p_connection){//establish connection and send a coutn of bytes every x second
29 //array of open request which are in waiting state for appropriate event or answer
30 p_connection->con_id=0;
31 p_connection->con_state=DISCONNECTED;
33 p_connection->socket_fd=hci_open_device(p_connection->master.dev_id);
34 if(l2cap_call_cmd(CREATE_CONNECTION_CMD_OP,p_connection->socket_fd,p_connection)<0){
35 printf("connection problem \n");
43 int show_all_local_dev(struct hci_dev_info *master){ // and choose a master device which will initiate the connection
45 struct hci_dev_list_req dlist,*p_dlist=&dlist;
46 struct hci_dev_req *p_dreq;
48 p_dlist->dev_num=HCI_MAX_DEV;
49 p_dreq=p_dlist->dev_req;
51 if((sock=socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI))<0){
55 if((ioctl(sock, HCIGETDEVLIST, (void *) p_dlist))<0){
58 if(p_dlist->dev_num > 0){
59 struct hci_dev_info dev_array[p_dlist->dev_num];
60 for(i=0;i<p_dlist->dev_num;i++){
61 dev_array[i].dev_id=(p_dreq+i)->dev_id;
62 if(ioctl(sock, HCIGETDEVINFO, (void *) &dev_array[i])<0){
66 int dd=hci_open_device(dev_array[i].dev_id);
67 if(l2cap_call_cmd(READ_BD_ADDR_CMD_OP,dd,&dev_array[i].bdaddr)< 0){
68 printf("Error in call hci bd addr cmd \n");
71 if(l2cap_call_cmd(READ_LOCAL_NAME_CMD_OP,dd,&dev_array[i].name)< 0){
72 printf("Error in call hci bd addr cmd \n");
75 //hci_close_dev(dev_array[i].dev_id);
77 *master = dev_array[0]; //I will take a first dev like a master
81 printf("There is no hci device \n");
88 int show_all_remote_dev(struct hci_dev_info *p_master, bt_address *p_remadrar, int *p_remd_count){
89 int dd=hci_open_device(p_master->dev_id);
91 if((*p_remd_count=l2cap_call_cmd(INQUIRY_CMD_OP, dd, p_remadrar))< 0){
92 printf("Error in call inquiry command \n");
99 int l2cap_call_cmd(__u16 ocf_ogf,int dd, void *p_param){
100 cmd_state_request new_req,*p_new_req=&new_req;
102 int remd_count,ret_val;
103 sw_opcode=swap_2_bytes(ocf_ogf);
104 p_new_req->actual_status=ESTABLISHED;
105 p_new_req->id=cmd_req_id++;
106 p_new_req->req_opcode = ocf_ogf;
110 case READ_BD_ADDR_CMD_OP:
111 p_new_req->evt_code = EVT_CMD_COMPLETE;
112 p_new_req->p_serv_rutine=NULL;
114 if(req_fcn_add(p_new_req)<0){
115 printf("Mistake with req array adding \n ");
119 if(call_hci_read_bd_addr_cmd(dd,(bt_address *)p_param,&sw_opcode)){
120 printf("Mistake with bt addr cmd \n ");
126 case READ_LOCAL_NAME_CMD_OP:
127 p_new_req->evt_code = EVT_CMD_COMPLETE;
128 p_new_req->p_serv_rutine=NULL;
130 if(req_fcn_add(p_new_req)<0){
131 printf("Mistake with req array adding \n ");
134 if(call_hci_read_local_name_cmd(dd,p_param,&sw_opcode)){
135 printf("Mistake with read local name cmd \n ");
143 p_new_req->evt_code = EVT_INQUIRY_COMPLETE;
144 p_new_req->p_serv_rutine=NULL;
146 if(req_fcn_add(p_new_req)<0){
147 printf("Mistake with req array adding \n ");
150 if((remd_count=call_hci_inquiry_cmd(dd,p_param,&sw_opcode)) < 0){
151 printf("Mistake with inquiry cmd \n ");
157 case CREATE_CONNECTION_CMD_OP:
158 p_new_req->evt_code = EVT_CONN_COMPLETE;
159 p_new_req->p_serv_rutine=NULL;
161 if(req_fcn_add(p_new_req)<0){
162 printf("Mistake with req array adding \n ");
165 if((ret_val=call_hci_create_connection_cmd(dd, &(((connection_hci *)p_param)->slave.bdaddr), &sw_opcode)) < 0){
166 printf("Mistake with inquiry cmd \n ");
169 ((connection_hci *)p_param)->handle=ret_val;
170 ((connection_hci *)p_param)->con_state=CONNECTED;
177 /* request array support functions */
178 int req_fcn_add(cmd_state_request *p_req){ //fcn add the request at the end of array or replace the oldest request
180 cmd_state_request the_oldest=array_req[0];
182 for(i=0;i<MAX_CMD_STATE_REQUEST;i++){
183 if((array_req[i].actual_status)==0 || array_req[i].actual_status==3 ){
189 if((array_req[i].id) < the_oldest.id){ //the smallest id (oldest request) will stay here
190 the_oldest = array_req[i];
197 array_req[index]=*p_req;
203 void req_fcn_remove(void){
204 array_req[global_index].actual_status=DONE;
207 /* rutines for each command */
209 void *rutine_catch_bd_addr(void *arg){
216 struct hci_dev_info master,*p_master=&master;
217 bt_address remote_bd_addr_array[HCI_MAX_DEV];
218 connection_hci connection,*p_connection=&connection;
219 memset(remote_bd_addr_array,0,(sizeof(bt_address)*HCI_MAX_DEV));
224 memset(array_req,0,(sizeof(cmd_state_request)*MAX_CMD_STATE_REQUEST));
225 if(show_all_local_dev(p_master)<0){
226 printf("error no dev \n");
229 if(show_all_remote_dev(p_master, remote_bd_addr_array, &remd_count)<0){
230 printf("error no remote dev \n");
233 printf("All remote devices:\n");
234 for(i=0;i<remd_count;i++){
235 printba(&remote_bd_addr_array[i]);
237 p_connection->master=master;
238 p_connection->slave.bdaddr=remote_bd_addr_array[0];
239 //fill_add(&prefered,0x31,0x16,0xCA,0x72,0x02,0x00);
240 //p_connection->slave.bdaddr=prefered;
241 if(create_master_connection(p_connection)<0){
242 printf("connection error");