]> rtime.felk.cvut.cz Git - tiny-bt.git/blob - src/hil2cap.c
02a4a44ee33515186fe979f801302e239351a271
[tiny-bt.git] / src / hil2cap.c
1 /*
2 *  C Implementation: testovaci
3 *
4 * Description: 
5 *
6 *
7 * Author: root <root@ubuntu>, (C) 2008
8 *
9 * Copyright: See COPYING file that comes with this distribution
10 *
11 */
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <sys/ioctl.h>
15 #include <errno.h>
16 #include <stdint.h>
17 #include "hil2cap.h"
18 #include "hcidriver.h"
19
20
21 cmd_state_request array_req[MAX_CMD_STATE_REQUEST];
22 int cmd_req_id;
23 int global_index;
24
25
26
27
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;
32         
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");
36                 return -1;
37         }       
38         
39         
40         return 0;
41 }
42
43 int show_all_local_dev(struct hci_dev_info *master){ // and choose a master device which will initiate the connection
44         int sock,i;
45         struct hci_dev_list_req dlist,*p_dlist=&dlist;
46         struct hci_dev_req *p_dreq;
47         
48         p_dlist->dev_num=HCI_MAX_DEV;
49         p_dreq=p_dlist->dev_req;
50         
51         if((sock=socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI))<0){ 
52                 return -1;
53         }
54         
55         if((ioctl(sock, HCIGETDEVLIST, (void *) p_dlist))<0){
56                 return -1;
57         }
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){
63                                 printf("No info");
64                                 continue;
65                         }
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");
69                                 return -1;
70                         }
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");
73                                 return -1;
74                         }
75                         //hci_close_dev(dev_array[i].dev_id);
76                 }
77                 *master = dev_array[0]; //I will take a first dev like a master
78                 
79         }
80         else{
81                 printf("There is no hci device \n");
82                 return -1;
83         }
84         return 0;
85                 
86 }
87
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);
90         
91         if((*p_remd_count=l2cap_call_cmd(INQUIRY_CMD_OP, dd, p_remadrar))< 0){
92                                 printf("Error in call inquiry command \n");
93                                 return -1;
94                         }
95         return 0;
96
97 }
98
99 int l2cap_call_cmd(__u16 ocf_ogf,int dd, void *p_param){
100         cmd_state_request new_req,*p_new_req=&new_req;
101         __u16 sw_opcode;
102         int remd_count;
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;        
107         
108         switch(ocf_ogf){
109         
110         case READ_BD_ADDR_CMD_OP:
111                 p_new_req->evt_code = EVT_CMD_COMPLETE;
112                 p_new_req->p_serv_rutine=NULL;
113
114                 if(req_fcn_add(p_new_req)<0){
115                         printf("Mistake with req array adding \n ");
116                         return -1;
117                 }
118                 
119                 if(call_hci_read_bd_addr_cmd(dd,(bt_address *)p_param,&sw_opcode)){
120                         printf("Mistake with bt addr cmd \n ");
121                         return -1;
122                 }
123                 req_fcn_remove();
124                 return 0;
125                 
126         case READ_LOCAL_NAME_CMD_OP:
127                 p_new_req->evt_code = EVT_CMD_COMPLETE;
128                 p_new_req->p_serv_rutine=NULL;
129
130                 if(req_fcn_add(p_new_req)<0){
131                         printf("Mistake with req array adding \n ");
132                         return -1;
133                 }
134                 if(call_hci_read_local_name_cmd(dd,p_param,&sw_opcode)){
135                         printf("Mistake with read local name cmd \n ");
136                         return -1;
137                 }
138                 req_fcn_remove();
139                 return 0;
140         
141         case INQUIRY_CMD_OP:
142                 
143                 p_new_req->evt_code = EVT_INQUIRY_COMPLETE;
144                 p_new_req->p_serv_rutine=NULL;
145                 
146                 if(req_fcn_add(p_new_req)<0){
147                         printf("Mistake with req array adding \n ");
148                         return -1;
149                 }
150                 if((remd_count=call_hci_inquiry_cmd(dd,p_param,&sw_opcode)) < 0){
151                         printf("Mistake with inquiry cmd \n ");
152                         return -1;
153                 }
154                 req_fcn_remove();
155                 return remd_count;
156         
157         case CREATE_CONNECTION_CMD_OP:
158                 p_new_req->evt_code = EVT_CONN_COMPLETE;
159                 p_new_req->p_serv_rutine=NULL;
160                 
161                 if(req_fcn_add(p_new_req)<0){
162                         printf("Mistake with req array adding \n ");
163                         return -1;
164                 }
165                 if((((connection_hci *)p_param)->handle=call_hci_create_connection_cmd(dd, &(((connection_hci *)p_param)->slave.bdaddr), &sw_opcode)) < 0){
166                         printf("Mistake with inquiry cmd \n ");
167                         return -1;
168                 }
169                 ((connection_hci *)p_param)->con_state=CONNECTED;
170                 req_fcn_remove();
171                 return 0;
172         }
173         
174         return 0;
175 }
176 /* request array support functions */
177 int req_fcn_add(cmd_state_request *p_req){ //fcn add the request at the end of array or replace the oldest request
178         int i,sig=0,index=0;
179         cmd_state_request the_oldest=array_req[0];
180         
181         for(i=0;i<MAX_CMD_STATE_REQUEST;i++){
182                 if((array_req[i].actual_status)==0 || array_req[i].actual_status==3 ){
183                         array_req[i]=*p_req;
184                         sig=1;
185                         global_index=i;
186                         break;
187                 }
188                 if((array_req[i].id) < the_oldest.id){ //the smallest id (oldest request) will stay here
189                         the_oldest = array_req[i];
190                         index=i;
191                         
192                 }
193         }
194         if(!sig){
195                 //return -1;
196                 array_req[index]=*p_req;
197                 global_index=index;     
198         }
199         return 0;
200 }
201
202 void req_fcn_remove(void){
203         array_req[global_index].actual_status=DONE;
204 }
205
206 /* rutines for each command */
207 //todo:
208 void *rutine_catch_bd_addr(void *arg){
209
210 return (void*)0;
211 }
212
213
214 int main(void){
215         struct hci_dev_info master,*p_master=&master;
216         bt_address remote_bd_addr_array[HCI_MAX_DEV];
217         memset(remote_bd_addr_array,0,(sizeof(bt_address)*HCI_MAX_DEV));
218         connection_hci connection,*p_connection=&connection;
219         int remd_count,i;
220         cmd_req_id=0;
221         global_index=0;
222         memset(array_req,0,(sizeof(cmd_state_request)*MAX_CMD_STATE_REQUEST));
223         if(show_all_local_dev(p_master)<0){
224                 printf("error no dev \n");
225                 return -1;
226         }
227         if(show_all_remote_dev(p_master, remote_bd_addr_array, &remd_count)<0){
228                 printf("error no remote dev \n");
229                 return -1;
230         }
231         printf("All remote devices:\n");
232         for(i=0;i<remd_count;i++){
233                 printba(&remote_bd_addr_array[i]);
234         }
235         p_connection->master=master;
236         p_connection->slave.bdaddr=remote_bd_addr_array[0];
237         
238         if(create_master_connection(p_connection)<0){
239                 printf("connection error");
240                 return -1;
241         }
242
243 return 0;
244 }
245