]> rtime.felk.cvut.cz Git - tiny-bt.git/blob - testf/t1/hcidriver.h
Version from David Plotek's bachelor thesis CD
[tiny-bt.git] / testf / t1 / hcidriver.h
1 //
2 // C++ Interface: hcidriver
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 #ifndef __HCIDRIVER
13 #define __HCIDRIVER
14
15 #include<asm/types.h>
16 #include<sys/socket.h>
17 #include<string.h>
18 #include<stdint.h>
19
20 /* HCI ioctl defines */
21 #define HCIDEVUP        _IOW('H', 201, int)
22 #define HCIDEVDOWN      _IOW('H', 202, int)
23 #define HCIDEVRESET     _IOW('H', 203, int)
24 #define HCIDEVRESTAT    _IOW('H', 204, int)
25
26 #define HCIGETDEVLIST   _IOR('H', 210, int)
27 #define HCIGETDEVINFO   _IOR('H', 211, int)
28 #define HCIGETCONNLIST  _IOR('H', 212, int)
29 #define HCIGETCONNINFO  _IOR('H', 213, int)
30
31 #define HCISETRAW       _IOW('H', 220, int)
32 #define HCISETSCAN      _IOW('H', 221, int)
33 #define HCISETAUTH      _IOW('H', 222, int)
34 #define HCISETENCRYPT   _IOW('H', 223, int)
35 #define HCISETPTYPE     _IOW('H', 224, int)
36 #define HCISETLINKPOL   _IOW('H', 225, int)
37 #define HCISETLINKMODE  _IOW('H', 226, int)
38 #define HCISETACLMTU    _IOW('H', 227, int)
39 #define HCISETSCOMTU    _IOW('H', 228, int)
40
41 #define HCISETSECMGR    _IOW('H', 230, int)
42
43 #define HCIINQUIRY      _IOR('H', 240, int)
44
45 /* HCI socket options*/
46 #define HCI_FILTER      2
47 #define SOL_HCI         0
48
49 #define HCI_FLT_TYPE_BITS       31
50 #define HCI_FLT_EVENT_BITS      63
51 #define HCI_FLT_OGF_BITS        63
52 #define HCI_FLT_OCF_BITS        127
53
54 /* HCI data types */
55 #define HCI_COMMAND_PKT         0x01
56 #define HCI_ACLDATA_PKT         0x02
57 #define HCI_EVENT_PKT           0x04
58 #define HCI_VENDOR_PKT          0xFF
59
60 /* HCI packet types */
61 #define HCI_DM1         0x0008
62 #define HCI_DM3         0x0400
63 #define HCI_DM5         0x4000
64 #define HCI_DH1         0x0010
65 #define HCI_DH3         0x0800
66 #define HCI_DH5         0x8000
67
68 #define HCI_HV1         0x0020
69 #define HCI_HV2         0x0040
70 #define HCI_HV3         0x0080
71
72 /* ACL flags */
73 #define ACL_CONT                0x01
74 #define ACL_START               0x02
75 #define ACL_ACTIVE_BCAST        0x04
76 #define ACL_PICO_BCAST          0x08
77
78 /* Connection states */
79 #define DISCONNECTED 0
80 #define CONNECTED 1
81 #define PENDING 2
82
83 /* BT protocols*/
84 #define BTPROTO_L2CAP   0
85 #define BTPROTO_HCI     1
86 #define BTPROTO_SCO     2
87 #define BTPROTO_RFCOMM  3
88 #define BTPROTO_BNEP    4
89 #define BTPROTO_CMTP    5
90 #define BTPROTO_HIDP    6
91 #define BTPROTO_AVDTP   7
92
93 #define SOL_HCI         0
94 #define SOL_L2CAP       6
95 #define SOL_SCO         17
96 #define SOL_RFCOMM      18
97
98 #define HCI_MAX_DEV     4
99 #define HCI_MAX_EVENT_SIZE      260
100
101 #define max(a, b)       ((a) > (b) ? (a) : (b))
102 #define min(a, b)       ((a) < (b) ? (a) : (b))
103
104
105 /* BD_ADDR  */
106 typedef struct{
107         __u8 byte[6];
108 } __attribute__((packed)) bt_address;
109
110 typedef struct{
111         __u8 byte[3];
112 } __attribute__((packed)) dev_lap;
113
114
115
116 /* Class of device*/
117
118 typedef struct{
119         __u8 byte[3];
120 }cl_device;
121
122 struct hci_dev_stats {
123         __u32 err_rx;
124         __u32 err_tx;
125         __u32 cmd_tx;
126         __u32 evt_rx;
127         __u32 acl_tx;
128         __u32 acl_rx;
129         __u32 sco_tx;
130         __u32 sco_rx;
131         __u32 byte_rx;
132         __u32 byte_tx;
133 };
134
135 typedef struct{
136         __u16 dev_id;
137         char  name[8];
138
139         bt_address bdaddr;
140
141         __u32 flags;
142         __u8  type;
143
144         __u8  features[8];
145
146         __u32 pkt_type;
147         __u32 link_policy;
148         __u32 link_mode;
149
150         __u16 acl_mtu;
151         __u16 acl_pkts;
152         __u16 sco_mtu;
153         __u16 sco_pkts;
154
155         struct hci_dev_stats stat;
156 }bt_device;
157
158
159 typedef struct{
160         __u16 dev_id;
161         __u32 dev_opt;
162 }bt_device_req;
163
164 typedef struct{
165         __u16 dev_num;
166         bt_device_req dev_req[HCI_MAX_DEV];
167 }bt_device_req_list;
168
169 typedef struct{
170         __u16 *p_OCF_OGF;
171         void *p_cmdp;
172         void *p_retcmdp;
173         __u16 cmdp_len;
174         __u16 retcmdp_len;
175         int event;      //for setting event filter , some hci commands have own events
176 } hci_request;
177
178 struct hci_filter{
179         __u32 type_mask;
180         __u32 event_mask[2];
181         __u16 opcode;
182 };
183
184 /* Socket address*/
185 typedef struct{
186         sa_family_t hci_family;
187         __u8 dev_id;
188 }sockaddr_hci;
189
190 typedef struct{
191         __u16 con_id;
192         __u8 con_state;
193         int socket_fd;
194 }connection_hci;
195
196
197 /*HCI packet headers*/
198
199 typedef struct {
200         uint16_t        opcode;         /* OCF & OGF */
201         uint8_t         plen;
202 } __attribute__ ((packed))      hci_command_hdr;
203 #define HCI_COMMAND_HDR_SIZE    3
204
205 typedef struct {
206         uint8_t         evt;
207         uint8_t         plen;
208 } __attribute__ ((packed))      hci_event_hdr;
209 #define HCI_EVENT_HDR_SIZE      2
210
211 typedef struct {
212         uint16_t        handle;         /* Handle & Flags(PB, BC) */
213         uint16_t        dlen;
214 } __attribute__ ((packed))      hci_acl_hdr;
215 #define HCI_ACL_HDR_SIZE        4
216
217
218
219 /*HCI Commands  */
220 /*Link control commands*/
221 #define OCF_INQUIRY_CMD 0x0001
222 #define HCI_INQUIRY_CMD_PARL 0x05
223 typedef struct {
224         __u8 lap[3];
225         __u8 inquiry_length;
226         __u8 num_responces;
227 } __attribute__ ((packed)) hci_inquiry_cmd;
228
229 #define OCF_INQUIRY_CANCEL_CMD 0x0002
230
231 #define OCF_CREATE_CONNECTION_CMD 0x0005
232 #define HCI_CREATE_CONNECTION_CMD_PARL 0x0D
233 typedef struct {
234         bt_address address;
235         __u16 packet_type;
236         __u8 rep_mode;
237         __u8 reserved;
238         __u16 clock_offset;
239         __u8 role_switch;
240 } __attribute__ ((packed)) hci_create_connection_cmd;
241
242 #define OCF_DISCONNECT_CMD 0x0006
243 struct hci_disconnect_cmd{
244         __u8 reason;
245 } __attribute__ ((packed));
246
247 #define OCF_CREATE_CONNECTION_CANCEL_CMD 0x0008
248 struct hci_create_connection_cancel_cmd{
249         bt_address address;
250 } __attribute__ ((packed));
251
252 #define OCF_ACCEPT_CONNECTION_REQ_CMD 0x0009
253 struct hci_accept_connection_req_cmd{
254         bt_address address;
255         __u8 role;
256 } __attribute__ ((packed));
257
258 #define OCF_REJECT_CONNECTION_REQ_CMD 0x000A
259 struct hci_reject_connection_req_cmd{
260         bt_address address;
261         __u8 reason;
262 } __attribute__ ((packed));
263
264 #define OCF_REMOTE_NAME_REQ_CMD 0x0019
265 typedef struct {
266         bt_address address;
267         __u8 rep_mode;
268         __u8 reserved;
269         __u16 clock_offset;
270 } __attribute__ ((packed)) hci_remote_name_req_cmd;
271
272 /*Link policy commands*/
273 #define OCF_RESET_CMD 0x0003
274
275
276 /*Informational parameters commands */
277 #define OGF_INFO_CMD 0x04
278
279 #define OCF_READ_BD_ADDR_CMD 0x0009
280 #define READ_BD_ADDR_CMD_PARL 7
281 typedef struct{
282         
283         //return parameters
284         __u8 status;
285         bt_address address;
286 } __attribute__ ((packed)) read_bd_addr_cmd;
287
288 /*Controllers and baseband commands */
289 #define OGF_CON_AND_BSB_CMD 0x03
290
291 #define OCF_READ_LOCAL_NAME_CMD 0x0014
292
293
294
295 /* HCI events  */
296
297 #define INQUIRY_COMPLETE_EV 0x01
298
299 typedef struct {
300         __u8 status;
301 } __attribute__ ((packed)) hci_inquiry_complete_ev;
302
303 #define INQUIRY_RESULT_EV 0x02
304 #define INQUIRY_RESULT_EV_PARL 0x0F  
305         //lenght for one responsing device
306 typedef struct {
307         __u8 num_responces;
308         bt_address ba_array;
309         __u8 rep_mode_array;
310         __u8 per_mode_array;
311         __u8 reserved_array;
312         cl_device cod_array;
313         __u16 clock_offset_array;
314 } __attribute__ ((packed)) hci_inquiry_result_ev;
315
316 #define INQUIRY_RESULT_RSSI_EV 0x22
317 #define INQUIRY_RESULT_RSSI_EV_PARL 0x0F  
318         //lenght for one responsing device
319 typedef struct {
320         __u8 num_responces;
321         bt_address ba_array;
322         __u8 rep_mode_array;
323         __u8 per_mode_array;
324         cl_device dev_class_array;
325         __u16 clock_offset_array;
326         __u8 RSSI_array;
327 } __attribute__ ((packed)) hci_inquiry_result_RSSI_ev;
328
329 #define CONNECTION_COMPLETE_EV 0x03
330 #define CONNECTION_COMPLETE_EV_PARL 0x0B
331 struct hci_connection_complete_ev{
332                 __u8 status;            // error code 00 status ok
333         __u16 con_handle;
334         bt_address adress;
335         __u8 link_type;
336         __u8 encrypt_mode;
337 } __attribute__ ((packed));
338
339 #define CONNECTION_REQUEST_EV 0x04
340 struct hci_connection_request_ev{
341         bt_address adress;
342         cl_device cod;   //clas of device
343         __u8 link_type;
344 } __attribute__ ((packed));
345
346 #define DISCONNECTION_COMPLETE_EV 0x05
347 struct hci_disconnection_complete_ev{
348         __u8 status;            // error code 00 status ok
349         __u16 con_handle;
350         __u8 reason;
351 } __attribute__ ((packed));
352
353 #define REMOTE_NAME_REQUEST_EV 0x07
354 typedef struct {
355         __u8 status;
356         bt_address address;
357         __u8 remote_name[247];  // array for 248 Octets
358 } __attribute__ ((packed)) hci_remote_name_request_complete_ev;
359
360 #define CMD_COMPLETE_EV 0x0E
361 #define CMD_COMPLETE_EV_SIZE 3  
362 // !!!!!!
363 typedef struct {
364         __u8 cmd_packets_number;
365         __u16 cmd_opcode;
366         __u8 return_param[31];
367 } __attribute__ ((packed)) hci_cmd_complete_ev;
368
369 #define CMD_STATUS_EV 0x0F
370 typedef struct {
371         __u8 status;
372         __u8 cmd_packets_number;
373         __u16 cmd_opcode;
374 } __attribute__ ((packed)) hci_cmd_status_ev;
375
376 #define HARDWARE_ERROR_EV 0x10
377 struct hci_hardware_error_ev{
378         __u8 hw_code;
379 } __attribute__ ((packed));
380
381 extern int call_hci_inquiry_cmd(int dd, bt_address *p_addressarray,int timeout);
382 extern int call_hci_read_bd_addr_cmd(int dd,bt_address *p_address, int timeout);
383 extern int call_hci_create_connection_cmd(int dd, bt_address *p_address, int timeout);
384 extern int hci_open_device(int dev_id);
385 extern int hci_send_command(int dd, hci_request *p_req);
386 extern int hci_send_request(int dd, hci_request *p_req,int timeout);
387 extern void print_device_list(int ctl);
388 extern int hci_close_dev(int dd);
389
390
391 extern void assemble_hci_data(void *p_con_handle,void *p_data_size,void *p_data);
392 extern void assemble_ocf_ogf(__u8 ogf,__u8 ocf,__u16 *p_ocf_ogf);
393 extern __u8 swap8(__u8 byte1);
394 extern __u16 swap16(__u16 byte2);
395 //extern void bacpy(bt_address p_dest, bt_address p_source);
396 extern void fill_add(bt_address *addr,__u8 first, __u8 sec, __u8 third, __u8 forth, __u8 fifth, __u8 sixth);
397 extern void printba(bt_address *ba);
398 extern int compare_bda(bt_address *p_first, bt_address *p_second);
399 extern void fill_zero(bt_address *p_addr);
400 extern void swap_addrbytes(bt_address *p_addr);
401
402
403 static inline void hci_set_bit(int nr, void *addr)
404 {
405         *((uint32_t *) addr + (nr >> 5)) |= (1 << (nr & 31));
406 }
407
408 static inline void hci_clear_bit(int nr, void *addr)
409 {
410         *((uint32_t *) addr + (nr >> 5)) &= ~(1 << (nr & 31));
411 }
412
413 static inline int hci_test_bit(int nr, void *addr)
414 {
415         return *((uint32_t *) addr + (nr >> 5)) & (1 << (nr & 31));
416 }
417
418 /* HCI filter tools */
419 static inline void hci_filter_clear(struct hci_filter *f)
420 {
421         memset(f, 0, sizeof(*f));
422 }
423 static inline void hci_filter_set_ptype(int t, struct hci_filter *f)
424 {
425         hci_set_bit((t == HCI_VENDOR_PKT) ? 0 : (t & HCI_FLT_TYPE_BITS), &f->type_mask);
426 }
427 static inline void hci_filter_clear_ptype(int t, struct hci_filter *f)
428 {
429         hci_clear_bit((t == HCI_VENDOR_PKT) ? 0 : (t & HCI_FLT_TYPE_BITS), &f->type_mask);
430 }
431 static inline int hci_filter_test_ptype(int t, struct hci_filter *f)
432 {
433         return hci_test_bit((t == HCI_VENDOR_PKT) ? 0 : (t & HCI_FLT_TYPE_BITS), &f->type_mask);
434 }
435 static inline void hci_filter_all_ptypes(struct hci_filter *f)
436 {
437         memset((void *) &f->type_mask, 0xff, sizeof(f->type_mask));
438 }
439 static inline void hci_filter_set_event(int e, struct hci_filter *f)
440 {
441         hci_set_bit((e & HCI_FLT_EVENT_BITS), &f->event_mask);
442 }
443 static inline void hci_filter_clear_event(int e, struct hci_filter *f)
444 {
445         hci_clear_bit((e & HCI_FLT_EVENT_BITS), &f->event_mask);
446 }
447 static inline int hci_filter_test_event(int e, struct hci_filter *f)
448 {
449         return hci_test_bit((e & HCI_FLT_EVENT_BITS), &f->event_mask);
450 }
451 static inline void hci_filter_all_events(struct hci_filter *f)
452 {
453         memset((void *) f->event_mask, 0xff, sizeof(f->event_mask));
454 }
455 static inline void hci_filter_set_opcode(int opcode, struct hci_filter *f)
456 {
457         f->opcode = opcode;
458 }
459 static inline void hci_filter_clear_opcode(struct hci_filter *f)
460 {
461         f->opcode = 0;
462 }
463 static inline int hci_filter_test_opcode(int opcode, struct hci_filter *f)
464 {
465         return (f->opcode == opcode);
466 }
467
468 static inline int bacmp(const bt_address *ba1, const bt_address *ba2)
469 {
470         return memcmp(ba1, ba2, sizeof(bt_address));
471 }
472 static inline void bacpy(bt_address *dst, const bt_address *src)
473 {
474         memcpy(dst, src, sizeof(bt_address));
475 }
476
477
478 #endif  /* __HCIDRIVER */