]> rtime.felk.cvut.cz Git - tiny-bt.git/blob - testf/hcidriver.h
Version from David Plotek's bachelor thesis CD
[tiny-bt.git] / testf / 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 struct hci_inquiry_cmd {
223         __u8 lap[3];
224         __u8 inquiry_length;
225         __u8 num_responces;
226 } __attribute__ ((packed));
227
228 #define OCF_INQUIRY_CANCEL_CMD 0x0002
229
230 #define OCF_CREATE_CONNECTION_CMD 0x0005
231 struct hci_create_connection_cmd{
232         bt_address address;
233         __u16 packet_type;
234         __u8 rep_mode;
235         __u8 reserved;
236         __u16 clock_offset;
237         __u8 role_switch;
238 } __attribute__ ((packed));
239
240 #define OCF_DISCONNECT_CMD 0x0006
241 struct hci_disconnect_cmd{
242         __u8 reason;
243 } __attribute__ ((packed));
244
245 #define OCF_CREATE_CONNECTION_CANCEL_CMD 0x0008
246 struct hci_create_connection_cancel_cmd{
247         bt_address address;
248 } __attribute__ ((packed));
249
250 #define OCF_ACCEPT_CONNECTION_REQ_CMD 0x0009
251 struct hci_accept_connection_req_cmd{
252         bt_address address;
253         __u8 role;
254 } __attribute__ ((packed));
255
256 #define OCF_REJECT_CONNECTION_REQ_CMD 0x000A
257 struct hci_reject_connection_req_cmd{
258         bt_address address;
259         __u8 reason;
260 } __attribute__ ((packed));
261
262 #define OCF_REMOTE_NAME_REQ_CMD 0x0019
263 typedef struct {
264         bt_address address;
265         __u8 rep_mode;
266         __u8 reserved;
267         __u16 clock_offset;
268 } __attribute__ ((packed)) hci_remote_name_req_cmd;
269
270 /*Link policy commands*/
271 #define OCF_RESET_CMD 0x0003
272
273
274 /*Informational parameters commands */
275 #define OGF_INFO_CMD 0x04
276
277 #define OCF_READ_BD_ADDR_CMD 0x0009
278 #define READ_BD_ADDR_CMD_PARL 7
279 typedef struct{
280         
281         //return parameters
282         __u8 status;
283         bt_address address;
284 } __attribute__ ((packed)) read_bd_addr_cmd;
285
286 /*Controllers and baseband commands */
287 #define OGF_CON_AND_BSB_CMD 0x03
288
289 #define OCF_READ_LOCAL_NAME_CMD 0x0014
290
291
292
293 /* HCI events  */
294
295 #define INQUIRY_COMPLETE_EV 0x01
296 struct hci_inquiry_complete_ev{
297         __u8 status;
298 } __attribute__ ((packed));
299
300 #define INQUIRY_RESULT_EV 0x02
301 struct hci_inquiry_result_ev{
302         __u8 num_responces;
303         bt_address ba_array;
304         __u8 rep_mode_array;
305         __u8 per_mode_array;
306         __u8 reserved_array;
307         cl_device cod_array;
308         __u16 clock_offset_array;
309 } __attribute__ ((packed));
310
311 #define CONNECTION_COMPLETE_EV 0x03
312 struct hci_connection_complete_ev{
313         __u8 status;            // error code 00 status ok
314         __u16 con_handle;
315         bt_address adress;
316         __u8 link_type;
317         __u8 encrypt_mode;
318 } __attribute__ ((packed));
319
320 #define CONNECTION_REQUEST_EV 0x04
321 struct hci_connection_request_ev{
322         bt_address adress;
323         cl_device cod;   //clas of device
324         __u8 link_type;
325 } __attribute__ ((packed));
326
327 #define DISCONNECTION_COMPLETE_EV 0x05
328 struct hci_disconnection_complete_ev{
329         __u8 status;            // error code 00 status ok
330         __u16 con_handle;
331         __u8 reason;
332 } __attribute__ ((packed));
333
334 #define REMOTE_NAME_REQUEST_EV 0x07
335 typedef struct {
336         __u8 status;
337         bt_address address;
338         __u8 remote_name[247];  // array for 248 Octets
339 } __attribute__ ((packed)) hci_remote_name_request_complete_ev;
340
341 #define CMD_COMPLETE_EV 0x0E
342 #define CMD_COMPLETE_EV_SIZE 3  
343 // !!!!!!
344 typedef struct {
345         __u8 cmd_packets_number;
346         __u16 cmd_opcode;
347         __u8 return_param[31];
348 } __attribute__ ((packed)) hci_cmd_complete_ev;
349
350 #define CMD_STATUS_EV 0x0F
351 typedef struct {
352         __u8 status;
353         __u8 cmd_packets_number;
354         __u16 cmd_opcode;
355 } __attribute__ ((packed)) hci_cmd_status_ev;
356
357 #define HARDWARE_ERROR_EV 0x10
358 struct hci_hardware_error_ev{
359         __u8 hw_code;
360 } __attribute__ ((packed));
361
362 extern void call_hci_inquiry_cmd(dev_lap lap, __u8 inq_length, __u8 resp_count);
363 extern int call_hci_read_bd_addr_cmd(int dd, bt_address *p_bdaddr, int timeout);
364 extern int hci_open_device(int dev_id);
365 extern int hci_send_command(int dd, hci_request *p_req);
366 extern int hci_send_request(int dd, hci_request *p_req,int timeout);
367 extern void print_device_list(int ctl);
368 extern int hci_close_dev(int dd);
369
370
371 extern void assemble_hci_data(void *p_con_handle,void *p_data_size,void *p_data);
372 extern void assemble_ocf_ogf(__u8 ogf,__u8 ocf,__u16 *p_ocf_ogf);
373 extern __u8 swap8(__u8 byte1);
374 extern __u16 swap16(__u16 byte2);
375 //extern void bacpy(bt_address p_dest, bt_address p_source);
376 extern void fill_add(bt_address *addr,__u8 first, __u8 sec, __u8 third, __u8 forth, __u8 fifth, __u8 sixth);
377 extern void printba(bt_address *ba);
378
379 static inline void hci_set_bit(int nr, void *addr)
380 {
381         *((uint32_t *) addr + (nr >> 5)) |= (1 << (nr & 31));
382 }
383
384 static inline void hci_clear_bit(int nr, void *addr)
385 {
386         *((uint32_t *) addr + (nr >> 5)) &= ~(1 << (nr & 31));
387 }
388
389 static inline int hci_test_bit(int nr, void *addr)
390 {
391         return *((uint32_t *) addr + (nr >> 5)) & (1 << (nr & 31));
392 }
393
394 /* HCI filter tools */
395 static inline void hci_filter_clear(struct hci_filter *f)
396 {
397         memset(f, 0, sizeof(*f));
398 }
399 static inline void hci_filter_set_ptype(int t, struct hci_filter *f)
400 {
401         hci_set_bit((t == HCI_VENDOR_PKT) ? 0 : (t & HCI_FLT_TYPE_BITS), &f->type_mask);
402 }
403 static inline void hci_filter_clear_ptype(int t, struct hci_filter *f)
404 {
405         hci_clear_bit((t == HCI_VENDOR_PKT) ? 0 : (t & HCI_FLT_TYPE_BITS), &f->type_mask);
406 }
407 static inline int hci_filter_test_ptype(int t, struct hci_filter *f)
408 {
409         return hci_test_bit((t == HCI_VENDOR_PKT) ? 0 : (t & HCI_FLT_TYPE_BITS), &f->type_mask);
410 }
411 static inline void hci_filter_all_ptypes(struct hci_filter *f)
412 {
413         memset((void *) &f->type_mask, 0xff, sizeof(f->type_mask));
414 }
415 static inline void hci_filter_set_event(int e, struct hci_filter *f)
416 {
417         hci_set_bit((e & HCI_FLT_EVENT_BITS), &f->event_mask);
418 }
419 static inline void hci_filter_clear_event(int e, struct hci_filter *f)
420 {
421         hci_clear_bit((e & HCI_FLT_EVENT_BITS), &f->event_mask);
422 }
423 static inline int hci_filter_test_event(int e, struct hci_filter *f)
424 {
425         return hci_test_bit((e & HCI_FLT_EVENT_BITS), &f->event_mask);
426 }
427 static inline void hci_filter_all_events(struct hci_filter *f)
428 {
429         memset((void *) f->event_mask, 0xff, sizeof(f->event_mask));
430 }
431 static inline void hci_filter_set_opcode(int opcode, struct hci_filter *f)
432 {
433         f->opcode = opcode;
434 }
435 static inline void hci_filter_clear_opcode(struct hci_filter *f)
436 {
437         f->opcode = 0;
438 }
439 static inline int hci_filter_test_opcode(int opcode, struct hci_filter *f)
440 {
441         return (f->opcode == opcode);
442 }
443
444 static inline int bacmp(const bt_address *ba1, const bt_address *ba2)
445 {
446         return memcmp(ba1, ba2, sizeof(bt_address));
447 }
448 static inline void bacpy(bt_address *dst, const bt_address *src)
449 {
450         memcpy(dst, src, sizeof(bt_address));
451 }
452
453
454 #endif  /* __HCIDRIVER */