]> rtime.felk.cvut.cz Git - tiny-bt.git/blob - src/hcidriver.c
24819b239fce27e74e51a45421fac51092b3e3bf
[tiny-bt.git] / src / hcidriver.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <string.h>
5 #include <errno.h>
6 #include <unistd.h>
7 #include<stdint.h>
8 #include <sys/poll.h>
9 #include <sys/ioctl.h>
10 #include "hcidriver.h"
11
12
13
14 /************************** hci command functions**************************/
15 int call_hci_inquiry_cmd(int dd, bt_address *p_addressarray,int timeout){
16         inquiry_cp cmdp,*p_cmdp=&cmdp;
17         hci_request req,*p_req=&req;
18         
19         //hci_inquiry_complete_ev com_ev,*p_com_ev=&com_ev;
20         __u16 OCF_OGF,*p_OCF_OGF=&OCF_OGF;
21         
22         p_cmdp->lap[0]=0x33;
23         p_cmdp->lap[1]=0x8b;
24         p_cmdp->lap[2]=0x9e;
25         p_cmdp->length=5;
26         p_cmdp->num_rsp=5;
27         memset(p_req,0,sizeof(req));
28         assemble_ocf_ogf(0x01,0x01,p_OCF_OGF);
29         p_req->p_OCF_OGF=p_OCF_OGF;
30         p_req->p_retcmdp=p_addressarray;
31         p_req->p_cmdp=p_cmdp;
32         p_req->retcmdp_len=INQUIRY_INFO_SIZE;
33         p_req->cmdp_len=INQUIRY_CP_SIZE;
34         p_req->event=EVT_INQUIRY_RESULT;
35
36         if(hci_send_request(dd,p_req,timeout)<0)
37                 return -1;
38 //      if(p_com_ev->status)
39 //              return -1;
40         
41         
42         return 0;
43 }
44 // fill up the input parameter address pointer by one address
45 int call_hci_read_bd_addr_cmd(int dd,bt_address *p_address, int timeout){
46         read_bd_addr_rp cmdp,*p_cmdp;   //command parameters return/comand
47         hci_request req,*p_req;
48         __u16 OCF_OGF,*p_OCF_OGF;
49         p_OCF_OGF=&OCF_OGF;
50         p_cmdp=&cmdp;
51         p_req=&req;
52         
53         
54         memset(p_req,0,sizeof(req));
55         assemble_ocf_ogf(0x09,0x04,p_OCF_OGF);
56         p_req->p_OCF_OGF=p_OCF_OGF;
57         p_req->p_retcmdp=p_cmdp;
58         p_req->retcmdp_len=READ_BD_ADDR_RP_SIZE;
59         p_req->cmdp_len=0;
60         p_req->event=EVT_CMD_COMPLETE;
61
62         if(hci_send_request(dd,p_req,timeout)<0)
63                 return -1;
64         if(p_cmdp->status)
65                 return -1;
66
67         bacpy(p_address, &cmdp.bdaddr);
68         return 0;
69 }
70
71 int call_hci_create_connection_cmd(int dd, bt_address *p_address, int timeout){
72         create_conn_cp cmdp,*p_cmdp=&cmdp;
73         hci_request req,*p_req=&req;
74         __u16 OCF_OGF,*p_OCF_OGF=&OCF_OGF;
75         
76         p_cmdp->bdaddr=*p_address;
77         p_cmdp->pkt_type=0x0010;
78         p_cmdp->pscan_rep_mode=0x01;
79         p_cmdp->pscan_mode=0x00;
80         p_cmdp->clock_offset=0xf000;
81         p_cmdp->role_switch=0x00;
82         memset(p_req,0,sizeof(req));
83         assemble_ocf_ogf(0x05,0x01,p_OCF_OGF);
84         p_req->p_OCF_OGF=p_OCF_OGF;
85         p_req->p_cmdp=p_cmdp;
86         p_req->cmdp_len=CREATE_CONN_CP_SIZE;
87         p_req->event=EVT_CONN_COMPLETE;
88
89         if(hci_send_request(dd,p_req,timeout)<0)
90                 return -1;
91         //if(p_com_ev->status)
92         //      return -1;
93                 
94         return 0;
95 }
96
97
98 /*******************************HCI main functions ********************************/
99
100 //create and bind the socket to one device, return -1 when error, device descriptor when OK
101 int hci_open_device(int dev_id){
102         struct sockaddr_hci address;
103         int dd;
104
105         if((dd=socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI))<0){
106                 perror("socket no created");
107                 return dd; //it return -1 when socket isn't created
108         }
109
110         memset(&address,0,sizeof(address));
111         address.hci_family=AF_BLUETOOTH;
112         address.hci_dev=dev_id;
113         if(bind(dd,(struct sockaddr *) &address, sizeof(address))<0){
114                 perror("Socket not binded to hci device");
115                 close(dd);
116                 return -1;
117         }
118
119         return dd;
120 }
121
122 int hci_close_dev(int dd){
123         return close(dd);
124 }
125
126 int hci_send_command(int dd, hci_request *p_req){
127         __u8 array[p_req->cmdp_len+4]; //type + OCF+OGF+plen
128         int ii;
129         array[0]=0x01;
130         memcpy(&array[1],p_req->p_OCF_OGF,2);
131         array[3]= p_req->cmdp_len;
132         if(p_req->cmdp_len > 0){
133                 memcpy(&array[4],p_req->p_cmdp,p_req->cmdp_len); // !!!!!!!!! segmentation fault
134         }
135
136         for(ii=0;ii<sizeof(array);ii++){
137                 printf(" %x",array[ii]);
138         }
139         printf("\n");
140
141         while(write(dd, &array, (p_req->cmdp_len+4))<0){
142                 perror("write interrupted");
143                 if(errno == EAGAIN || errno == EINTR)
144                         continue;
145                 return -1;
146         }
147         return 0;
148 }
149
150 int hci_send_request(int dd, hci_request *p_req,int timeout){
151         return 0;
152 }
153
154 /*void print_device_list(int ctl){
155         bt_device_req_list dlist,*p_dlist;
156         bt_device_req *p_dreq;
157         int i;
158         bt_device devi;
159
160         p_dlist=&dlist;
161         p_dlist->dev_num=HCI_MAX_DEV;
162         p_dreq=p_dlist->dev_req;
163         
164         if(ioctl(ctl, HCIGETDEVLIST, (void *) p_dlist)<0){
165                 perror("Can't get device list");
166                 exit(1);
167         }
168         
169         for(i=0;i<p_dlist->dev_num;i++){
170                 devi.dev_id=(p_dreq+i)->dev_id;
171                 if(ioctl(ctl, HCIGETDEVINFO, (void *) &devi)<0)
172                         printf("No info");
173                         continue;
174         
175         }       
176
177 }
178
179
180
181 void assemble_hci_data(void *p_con_handle,void *p_data_size,void *p_data){
182         __u16 data_size=0;
183         data_size= *(__u16 *)p_data_size;
184         __u8 array[data_size+5];  //type + handle + flags + size 
185         array[0]=0x02;
186         memcpy(&array[1],p_con_handle,2);
187         memcpy(&array[3],p_data_size,2);
188         memcpy(&array[6],p_data,data_size);
189         //call send to socket/uart
190
191 } */
192
193
194
195 /*****************************HCI support functions*****************************/
196
197
198 void assemble_ocf_ogf(__u8 ocf,__u8 ogf,__u16 *p_ocf_ogf){
199         __u16 var1;
200         __u16 result;
201         //result=ocf;
202         //result=(result<<8);
203         //var1=(ogf<<2);
204         //*p_ocf_ogf=(result|var1);
205         var1=(ogf<<10);
206         result=ocf;
207         *p_ocf_ogf=(result|var1);
208 }
209
210 void printba(bt_address *ba){
211         printf("address: %2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X: \n",ba->byte[0],ba->byte[1],ba->byte[2],ba->byte[3],ba->byte[4],ba->byte[5]);
212 }
213
214 int compare_bda(bt_address *p_first, bt_address *p_second){
215         __u8 i,k=0;
216         for(i=0;i<sizeof(bt_address);i++){
217                 if(p_first->byte[i]==p_second->byte[i])
218                         k++;
219         }
220         if(k==sizeof(bt_address)) return 1; //all bytes are similar
221         
222         return 0; //addreses are different in one byte at least  
223 }
224
225 void fill_zero(bt_address *p_addr){
226         __u8 i;
227         for(i=0;i<sizeof(bt_address);i++){
228                 p_addr->byte[i]=0x00;
229         }
230 }
231 void swap_addrbytes(bt_address *p_addr){
232         bt_address help,*p_help=&help;
233         __u8 i;
234         for(i=0;i<sizeof(bt_address);i++){
235                 p_help->byte[i]=p_addr->byte[5-i];
236         }
237         *p_addr=*p_help;
238 }
239
240 __u8 swap8(__u8 byte1){
241         __u8 i,mask=0,hvar1=0,resvar=0;
242         for(i=0;i<8;i++){
243                 mask=1<<i; // 1,2,4,8,16,32,64,128
244                 hvar1=byte1 & mask;
245                 if(hvar1>0)
246                 resvar=resvar + (128>>i);
247         }
248         return resvar;          
249 }
250
251 __u16 swap16(__u16 byte2){
252         __u8 i;
253         __u16 mask=0,hvar1=0,resvar=0;
254         for(i=0;i<16;i++){
255         mask=1<<i;
256         hvar1=byte2 & mask;
257         if(hvar1>0)
258         resvar=resvar + (32768>>i);
259         }
260         return resvar;
261 }
262
263 void fill_add(bt_address *addr,__u8 first, __u8 sec, __u8 third, __u8 forth, __u8 fifth, __u8 sixth){   
264         addr->byte[0]=first;
265         addr->byte[1]=sec;
266         addr->byte[2]=third;
267         addr->byte[3]=forth;
268         addr->byte[4]=fifth;
269         addr->byte[5]=sixth;
270 }
271