1 /*
2 *
3 * BlueZ - Bluetooth protocol stack for Linux
4 *
5 * Copyright (C) 2000-2001 Qualcomm Incorporated
6 * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
7 * Copyright (C) 2002-2008 Marcel Holtmann <marcel@holtmann.org>
8 *
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 *
24 */
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29
30 #include <stdio.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include <sys/param.h>
38 #include <sys/uio.h>
39 #include <sys/poll.h>
40 #include <sys/types.h>
41 #include <sys/ioctl.h>
42 #include <sys/socket.h>
43
44 #include <bluetooth/bluetooth.h>
45 #include <bluetooth/hci.h>
46 #include <bluetooth/hci_lib.h>
47
48 #ifndef MIN
49 #define MIN(a,b) ((a)<(b)?(a):(b))
50 #endif
51
52 typedef struct {
53 char *str;
54 unsigned int val;
55 } hci_map;
56
hci_bit2str(hci_map * m,unsigned int val)57 static char *hci_bit2str(hci_map *m, unsigned int val)
58 {
59 char *str = malloc(120);
60 char *ptr = str;
61
62 if (!str)
63 return NULL;
64
65 *ptr = 0;
66 while (m->str) {
67 if ((unsigned int) m->val & val)
68 ptr += sprintf(ptr, "%s ", m->str);
69 m++;
70 }
71 return str;
72 }
73
hci_str2bit(hci_map * map,char * str,unsigned int * val)74 static int hci_str2bit(hci_map *map, char *str, unsigned int *val)
75 {
76 char *t, *ptr;
77 hci_map *m;
78 int set;
79
80 if (!str || !(str = ptr = strdup(str)))
81 return 0;
82
83 *val = set = 0;
84
85 while ((t = strsep(&ptr, ","))) {
86 for (m = map; m->str; m++) {
87 if (!strcasecmp(m->str, t)) {
88 *val |= (unsigned int) m->val;
89 set = 1;
90 }
91 }
92 }
93 free(str);
94
95 return set;
96 }
97
hci_uint2str(hci_map * m,unsigned int val)98 static char *hci_uint2str(hci_map *m, unsigned int val)
99 {
100 char *str = malloc(50);
101 char *ptr = str;
102
103 if (!str)
104 return NULL;
105
106 *ptr = 0;
107 while (m->str) {
108 if ((unsigned int) m->val == val) {
109 ptr += sprintf(ptr, "%s", m->str);
110 break;
111 }
112 m++;
113 }
114 return str;
115 }
116
hci_str2uint(hci_map * map,char * str,unsigned int * val)117 static int hci_str2uint(hci_map *map, char *str, unsigned int *val)
118 {
119 char *t, *ptr;
120 hci_map *m;
121 int set = 0;
122
123 if (!str)
124 return 0;
125
126 str = ptr = strdup(str);
127
128 while ((t = strsep(&ptr, ","))) {
129 for (m = map; m->str; m++) {
130 if (!strcasecmp(m->str,t)) {
131 *val = (unsigned int) m->val; set = 1;
132 break;
133 }
134 }
135 }
136 free(str);
137
138 return set;
139 }
140
hci_dtypetostr(int type)141 char *hci_dtypetostr(int type)
142 {
143 switch (type) {
144 case HCI_VIRTUAL:
145 return "VIRTUAL";
146 case HCI_USB:
147 return "USB";
148 case HCI_PCCARD:
149 return "PCCARD";
150 case HCI_UART:
151 return "UART";
152 case HCI_RS232:
153 return "RS232";
154 case HCI_PCI:
155 return "PCI";
156 case HCI_SDIO:
157 return "SDIO";
158 default:
159 return "UNKNOWN";
160 }
161 }
162
163 /* HCI dev flags mapping */
164 static hci_map dev_flags_map[] = {
165 { "UP", HCI_UP },
166 { "INIT", HCI_INIT },
167 { "RUNNING", HCI_RUNNING },
168 { "RAW", HCI_RAW },
169 { "PSCAN", HCI_PSCAN },
170 { "ISCAN", HCI_ISCAN },
171 { "INQUIRY", HCI_INQUIRY },
172 { "AUTH", HCI_AUTH },
173 { "ENCRYPT", HCI_ENCRYPT },
174 { "SECMGR", HCI_SECMGR },
175 { NULL }
176 };
177
hci_dflagstostr(uint32_t flags)178 char *hci_dflagstostr(uint32_t flags)
179 {
180 char *str = bt_malloc(50);
181 char *ptr = str;
182 hci_map *m = dev_flags_map;
183
184 if (!str)
185 return NULL;
186
187 *ptr = 0;
188
189 if (!hci_test_bit(HCI_UP, &flags))
190 ptr += sprintf(ptr, "DOWN ");
191
192 while (m->str) {
193 if (hci_test_bit(m->val, &flags))
194 ptr += sprintf(ptr, "%s ", m->str);
195 m++;
196 }
197 return str;
198 }
199
200 /* HCI packet type mapping */
201 static hci_map pkt_type_map[] = {
202 { "DM1", HCI_DM1 },
203 { "DM3", HCI_DM3 },
204 { "DM5", HCI_DM5 },
205 { "DH1", HCI_DH1 },
206 { "DH3", HCI_DH3 },
207 { "DH5", HCI_DH5 },
208 { "HV1", HCI_HV1 },
209 { "HV2", HCI_HV2 },
210 { "HV3", HCI_HV3 },
211 { "2-DH1", HCI_2DH1 },
212 { "2-DH3", HCI_2DH3 },
213 { "2-DH5", HCI_2DH5 },
214 { "3-DH1", HCI_3DH1 },
215 { "3-DH3", HCI_3DH3 },
216 { "3-DH5", HCI_3DH5 },
217 { NULL }
218 };
219
220 static hci_map sco_ptype_map[] = {
221 { "HV1", 0x0001 },
222 { "HV2", 0x0002 },
223 { "HV3", 0x0004 },
224 { "EV3", HCI_EV3 },
225 { "EV4", HCI_EV4 },
226 { "EV5", HCI_EV5 },
227 { "2-EV3", HCI_2EV3 },
228 { "2-EV5", HCI_2EV5 },
229 { "3-EV3", HCI_3EV3 },
230 { "3-EV5", HCI_3EV5 },
231 { NULL }
232 };
233
hci_ptypetostr(unsigned int ptype)234 char *hci_ptypetostr(unsigned int ptype)
235 {
236 return hci_bit2str(pkt_type_map, ptype);
237 }
238
hci_strtoptype(char * str,unsigned int * val)239 int hci_strtoptype(char *str, unsigned int *val)
240 {
241 return hci_str2bit(pkt_type_map, str, val);
242 }
243
hci_scoptypetostr(unsigned int ptype)244 char *hci_scoptypetostr(unsigned int ptype)
245 {
246 return hci_bit2str(sco_ptype_map, ptype);
247 }
248
hci_strtoscoptype(char * str,unsigned int * val)249 int hci_strtoscoptype(char *str, unsigned int *val)
250 {
251 return hci_str2bit(sco_ptype_map, str, val);
252 }
253
254 /* Link policy mapping */
255 static hci_map link_policy_map[] = {
256 { "NONE", 0 },
257 { "RSWITCH", HCI_LP_RSWITCH },
258 { "HOLD", HCI_LP_HOLD },
259 { "SNIFF", HCI_LP_SNIFF },
260 { "PARK", HCI_LP_PARK },
261 { NULL }
262 };
263
hci_lptostr(unsigned int lp)264 char *hci_lptostr(unsigned int lp)
265 {
266 return hci_bit2str(link_policy_map, lp);
267 }
268
hci_strtolp(char * str,unsigned int * val)269 int hci_strtolp(char *str, unsigned int *val)
270 {
271 return hci_str2bit(link_policy_map, str, val);
272 }
273
274 /* Link mode mapping */
275 static hci_map link_mode_map[] = {
276 { "NONE", 0 },
277 { "ACCEPT", HCI_LM_ACCEPT },
278 { "MASTER", HCI_LM_MASTER },
279 { "AUTH", HCI_LM_AUTH },
280 { "ENCRYPT", HCI_LM_ENCRYPT },
281 { "TRUSTED", HCI_LM_TRUSTED },
282 { "RELIABLE", HCI_LM_RELIABLE },
283 { "SECURE", HCI_LM_SECURE },
284 { NULL }
285 };
286
hci_lmtostr(unsigned int lm)287 char *hci_lmtostr(unsigned int lm)
288 {
289 char *s, *str = bt_malloc(50);
290 if (!str)
291 return NULL;
292
293 *str = 0;
294 if (!(lm & HCI_LM_MASTER))
295 strcpy(str, "SLAVE ");
296
297 s = hci_bit2str(link_mode_map, lm);
298 if (!s) {
299 bt_free(str);
300 return NULL;
301 }
302
303 strcat(str, s);
304 free(s);
305 return str;
306 }
307
hci_strtolm(char * str,unsigned int * val)308 int hci_strtolm(char *str, unsigned int *val)
309 {
310 return hci_str2bit(link_mode_map, str, val);
311 }
312
313 /* Command mapping */
314 static hci_map commands_map[] = {
315 { "Inquiry", 0 },
316 { "Inquiry Cancel", 1 },
317 { "Periodic Inquiry Mode", 2 },
318 { "Exit Periodic Inquiry Mode", 3 },
319 { "Create Connection", 4 },
320 { "Disconnect", 5 },
321 { "Add SCO Connection", 6 },
322 { "Cancel Create Connection", 7 },
323
324 { "Accept Connection Request", 8 },
325 { "Reject Connection Request", 9 },
326 { "Link Key Request Reply", 10 },
327 { "Link Key Request Negative Reply", 11 },
328 { "PIN Code Request Reply", 12 },
329 { "PIN Code Request Negative Reply", 13 },
330 { "Change Connection Packet Type", 14 },
331 { "Authentication Requested", 15 },
332
333 { "Set Connection Encryption", 16 },
334 { "Change Connection Link Key", 17 },
335 { "Master Link Key", 18 },
336 { "Remote Name Request", 19 },
337 { "Cancel Remote Name Request", 20 },
338 { "Read Remote Supported Features", 21 },
339 { "Read Remote Extended Features", 22 },
340 { "Read Remote Version Information", 23 },
341
342 { "Read Clock Offset", 24 },
343 { "Read LMP Handle", 25 },
344 { "Reserved", 26 },
345 { "Reserved", 27 },
346 { "Reserved", 28 },
347 { "Reserved", 29 },
348 { "Reserved", 30 },
349 { "Reserved", 31 },
350
351 { "Reserved", 32 },
352 { "Hold Mode", 33 },
353 { "Sniff Mode", 34 },
354 { "Exit Sniff Mode", 35 },
355 { "Park State", 36 },
356 { "Exit Park State", 37 },
357 { "QoS Setup", 38 },
358 { "Role Discovery", 39 },
359
360 { "Switch Role", 40 },
361 { "Read Link Policy Settings", 41 },
362 { "Write Link Policy Settings", 42 },
363 { "Read Default Link Policy Settings", 43 },
364 { "Write Default Link Policy Settings", 44 },
365 { "Flow Specification", 45 },
366 { "Set Event Mask", 46 },
367 { "Reset", 47 },
368
369 { "Set Event Filter", 48 },
370 { "Flush", 49 },
371 { "Read PIN Type", 50 },
372 { "Write PIN Type", 51 },
373 { "Create New Unit Key", 52 },
374 { "Read Stored Link Key", 53 },
375 { "Write Stored Link Key", 54 },
376 { "Delete Stored Link Key", 55 },
377
378 { "Write Local Name", 56 },
379 { "Read Local Name", 57 },
380 { "Read Connection Accept Timeout", 58 },
381 { "Write Connection Accept Timeout", 59 },
382 { "Read Page Timeout", 60 },
383 { "Write Page Timeout", 61 },
384 { "Read Scan Enable", 62 },
385 { "Write Scan Enable", 63 },
386
387 { "Read Page Scan Activity", 64 },
388 { "Write Page Scan Activity", 65 },
389 { "Read Inquiry Scan Activity", 66 },
390 { "Write Inquiry Scan Activity", 67 },
391 { "Read Authentication Enable", 68 },
392 { "Write Authentication Enable", 69 },
393 { "Read Encryption Mode", 70 },
394 { "Write Encryption Mode", 71 },
395
396 { "Read Class Of Device", 72 },
397 { "Write Class Of Device", 73 },
398 { "Read Voice Setting", 74 },
399 { "Write Voice Setting", 75 },
400 { "Read Automatic Flush Timeout", 76 },
401 { "Write Automatic Flush Timeout", 77 },
402 { "Read Num Broadcast Retransmissions", 78 },
403 { "Write Num Broadcast Retransmissions", 79 },
404
405 { "Read Hold Mode Activity", 80 },
406 { "Write Hold Mode Activity", 81 },
407 { "Read Transmit Power Level", 82 },
408 { "Read Synchronous Flow Control Enable", 83 },
409 { "Write Synchronous Flow Control Enable", 84 },
410 { "Set Host Controller To Host Flow Control", 85 },
411 { "Host Buffer Size", 86 },
412 { "Host Number Of Completed Packets", 87 },
413
414 { "Read Link Supervision Timeout", 88 },
415 { "Write Link Supervision Timeout", 89 },
416 { "Read Number of Supported IAC", 90 },
417 { "Read Current IAC LAP", 91 },
418 { "Write Current IAC LAP", 92 },
419 { "Read Page Scan Period Mode", 93 },
420 { "Write Page Scan Period Mode", 94 },
421 { "Read Page Scan Mode", 95 },
422
423 { "Write Page Scan Mode", 96 },
424 { "Set AFH Channel Classification", 97 },
425 { "Reserved", 98 },
426 { "Reserved", 99 },
427 { "Read Inquiry Scan Type", 100 },
428 { "Write Inquiry Scan Type", 101 },
429 { "Read Inquiry Mode", 102 },
430 { "Write Inquiry Mode", 103 },
431
432 { "Read Page Scan Type", 104 },
433 { "Write Page Scan Type", 105 },
434 { "Read AFH Channel Assessment Mode", 106 },
435 { "Write AFH Channel Assessment Mode", 107 },
436 { "Reserved", 108 },
437 { "Reserved", 109 },
438 { "Reserved", 110 },
439 { "Reserved", 111 },
440
441 { "Reserved", 112 },
442 { "Reserved", 113 },
443 { "Reserved", 114 },
444 { "Read Local Version Information", 115 },
445 { "Read Local Supported Commands", 116 },
446 { "Read Local Supported Features", 117 },
447 { "Read Local Extended Features", 118 },
448 { "Read Buffer Size", 119 },
449
450 { "Read Country Code", 120 },
451 { "Read BD ADDR", 121 },
452 { "Read Failed Contact Counter", 122 },
453 { "Reset Failed Contact Counter", 123 },
454 { "Get Link Quality", 124 },
455 { "Read RSSI", 125 },
456 { "Read AFH Channel Map", 126 },
457 { "Read BD Clock", 127 },
458
459 { "Read Loopback Mode", 128 },
460 { "Write Loopback Mode", 129 },
461 { "Enable Device Under Test Mode", 130 },
462 { "Setup Synchronous Connection", 131 },
463 { "Accept Synchronous Connection", 132 },
464 { "Reject Synchronous Connection", 133 },
465 { "Reserved", 134 },
466 { "Reserved", 135 },
467
468 { "Read Extended Inquiry Response", 136 },
469 { "Write Extended Inquiry Response", 137 },
470 { "Refresh Encryption Key", 138 },
471 { "Reserved", 139 },
472 { "Sniff Subrating", 140 },
473 { "Read Simple Pairing Mode", 141 },
474 { "Write Simple Pairing Mode", 142 },
475 { "Read Local OOB Data", 143 },
476
477 { "Read Inquiry Transmit Power Level", 144 },
478 { "Write Inquiry Transmit Power Level", 145 },
479 { "Read Default Erroneous Data Reporting", 146 },
480 { "Write Default Erroneous Data Reporting", 147 },
481 { "Reserved", 148 },
482 { "Reserved", 149 },
483 { "Reserved", 150 },
484 { "IO Capability Request Reply", 151 },
485
486 { "User Confirmation Request Reply", 152 },
487 { "User Confirmation Request Negative Reply", 153 },
488 { "User Passkey Request Reply", 154 },
489 { "User Passkey Request Negative Reply", 155 },
490 { "Remote OOB Data Request Reply", 156 },
491 { "Write Simple Pairing Debug Mode", 157 },
492 { "Enhanced Flush", 158 },
493 { "Remote OOB Data Request Negative Reply", 159 },
494
495 { "Reserved", 160 },
496 { "Reserved", 161 },
497 { "Send Keypress Notification", 162 },
498 { "IO Capabilities Response Negative Reply", 163 },
499 { "Reserved", 164 },
500 { "Reserved", 165 },
501 { "Reserved", 166 },
502 { "Reserved", 167 },
503
504 { NULL }
505 };
506
hci_cmdtostr(unsigned int cmd)507 char *hci_cmdtostr(unsigned int cmd)
508 {
509 return hci_uint2str(commands_map, cmd);
510 }
511
hci_commandstostr(uint8_t * commands,char * pref,int width)512 char *hci_commandstostr(uint8_t *commands, char *pref, int width)
513 {
514 hci_map *m;
515 char *off, *ptr, *str;
516 int size = 10;
517
518 m = commands_map;
519
520 while (m->str) {
521 if (commands[m->val / 8] & (1 << (m->val % 8)))
522 size += strlen(m->str) + (pref ? strlen(pref) : 0) + 3;
523 m++;
524 }
525
526 str = bt_malloc(size);
527 if (!str)
528 return NULL;
529
530 ptr = str; *ptr = '\0';
531
532 if (pref)
533 ptr += sprintf(ptr, "%s", pref);
534
535 off = ptr;
536
537 m = commands_map;
538
539 while (m->str) {
540 if (commands[m->val / 8] & (1 << (m->val % 8))) {
541 if (strlen(off) + strlen(m->str) > width - 3) {
542 ptr += sprintf(ptr, "\n%s", pref ? pref : "");
543 off = ptr;
544 }
545 ptr += sprintf(ptr, "'%s' ", m->str);
546 }
547 m++;
548 }
549
550 return str;
551 }
552
553 /* Version mapping */
554 static hci_map ver_map[] = {
555 { "1.0b", 0x00 },
556 { "1.1", 0x01 },
557 { "1.2", 0x02 },
558 { "2.0", 0x03 },
559 { "2.1", 0x04 },
560 { NULL }
561 };
562
hci_vertostr(unsigned int ver)563 char *hci_vertostr(unsigned int ver)
564 {
565 return hci_uint2str(ver_map, ver);
566 }
567
hci_strtover(char * str,unsigned int * ver)568 int hci_strtover(char *str, unsigned int *ver)
569 {
570 return hci_str2uint(ver_map, str, ver);
571 }
572
lmp_vertostr(unsigned int ver)573 char *lmp_vertostr(unsigned int ver)
574 {
575 return hci_uint2str(ver_map, ver);
576 }
577
lmp_strtover(char * str,unsigned int * ver)578 int lmp_strtover(char *str, unsigned int *ver)
579 {
580 return hci_str2uint(ver_map, str, ver);
581 }
582
583 /* LMP features mapping */
584 static hci_map lmp_features_map[8][9] = {
585 { /* Byte 0 */
586 { "<3-slot packets>", LMP_3SLOT }, /* Bit 0 */
587 { "<5-slot packets>", LMP_5SLOT }, /* Bit 1 */
588 { "<encryption>", LMP_ENCRYPT }, /* Bit 2 */
589 { "<slot offset>", LMP_SOFFSET }, /* Bit 3 */
590 { "<timing accuracy>", LMP_TACCURACY }, /* Bit 4 */
591 { "<role switch>", LMP_RSWITCH }, /* Bit 5 */
592 { "<hold mode>", LMP_HOLD }, /* Bit 6 */
593 { "<sniff mode>", LMP_SNIFF }, /* Bit 7 */
594 { NULL }
595 },
596 { /* Byte 1 */
597 { "<park state>", LMP_PARK }, /* Bit 0 */
598 { "<RSSI>", LMP_RSSI }, /* Bit 1 */
599 { "<channel quality>", LMP_QUALITY }, /* Bit 2 */
600 { "<SCO link>", LMP_SCO }, /* Bit 3 */
601 { "<HV2 packets>", LMP_HV2 }, /* Bit 4 */
602 { "<HV3 packets>", LMP_HV3 }, /* Bit 5 */
603 { "<u-law log>", LMP_ULAW }, /* Bit 6 */
604 { "<A-law log>", LMP_ALAW }, /* Bit 7 */
605 { NULL }
606 },
607 { /* Byte 2 */
608 { "<CVSD>", LMP_CVSD }, /* Bit 0 */
609 { "<paging scheme>", LMP_PSCHEME }, /* Bit 1 */
610 { "<power control>", LMP_PCONTROL }, /* Bit 2 */
611 { "<transparent SCO>", LMP_TRSP_SCO }, /* Bit 3 */
612 { "<broadcast encrypt>",LMP_BCAST_ENC }, /* Bit 7 */
613 { NULL }
614 },
615 { /* Byte 3 */
616 { "<no. 24>", 0x01 }, /* Bit 0 */
617 { "<EDR ACL 2 Mbps>", LMP_EDR_ACL_2M }, /* Bit 1 */
618 { "<EDR ACL 3 Mbps>", LMP_EDR_ACL_3M }, /* Bit 2 */
619 { "<enhanced iscan>", LMP_ENH_ISCAN }, /* Bit 3 */
620 { "<interlaced iscan>", LMP_ILACE_ISCAN }, /* Bit 4 */
621 { "<interlaced pscan>", LMP_ILACE_PSCAN }, /* Bit 5 */
622 { "<inquiry with RSSI>",LMP_RSSI_INQ }, /* Bit 6 */
623 { "<extended SCO>", LMP_ESCO }, /* Bit 7 */
624 { NULL }
625 },
626 { /* Byte 4 */
627 { "<EV4 packets>", LMP_EV4 }, /* Bit 0 */
628 { "<EV5 packets>", LMP_EV5 }, /* Bit 1 */
629 { "<no. 34>", 0x04 }, /* Bit 2 */
630 { "<AFH cap. slave>", LMP_AFH_CAP_SLV }, /* Bit 3 */
631 { "<AFH class. slave>", LMP_AFH_CLS_SLV }, /* Bit 4 */
632 { "<no. 37>", 0x20 }, /* Bit 5 */
633 { "<no. 38>", 0x40 }, /* Bit 6 */
634 { "<3-slot EDR ACL>", LMP_EDR_3SLOT }, /* Bit 7 */
635 { NULL }
636 },
637 { /* Byte 5 */
638 { "<5-slot EDR ACL>", LMP_EDR_5SLOT }, /* Bit 0 */
639 { "<sniff subrating>", LMP_SNIFF_SUBR }, /* Bit 1 */
640 { "<pause encryption>", LMP_PAUSE_ENC }, /* Bit 2 */
641 { "<AFH cap. master>", LMP_AFH_CAP_MST }, /* Bit 3 */
642 { "<AFH class. master>",LMP_AFH_CLS_MST }, /* Bit 4 */
643 { "<EDR eSCO 2 Mbps>", LMP_EDR_ESCO_2M }, /* Bit 5 */
644 { "<EDR eSCO 3 Mbps>", LMP_EDR_ESCO_3M }, /* Bit 6 */
645 { "<3-slot EDR eSCO>", LMP_EDR_3S_ESCO }, /* Bit 7 */
646 { NULL }
647 },
648 { /* Byte 6 */
649 { "<extended inquiry>", LMP_EXT_INQ }, /* Bit 0 */
650 { "<no. 49>", 0x02 }, /* Bit 1 */
651 { "<no. 50>", 0x04 }, /* Bit 2 */
652 { "<simple pairing>", LMP_SIMPLE_PAIR }, /* Bit 3 */
653 { "<encapsulated PDU>", LMP_ENCAPS_PDU }, /* Bit 4 */
654 { "<err. data report>", LMP_ERR_DAT_REP }, /* Bit 5 */
655 { "<non-flush flag>", LMP_NFLUSH_PKTS }, /* Bit 6 */
656 { "<no. 55>", 0x80 }, /* Bit 7 */
657 { NULL }
658 },
659 { /* Byte 7 */
660 { "<LSTO>", LMP_LSTO }, /* Bit 1 */
661 { "<inquiry TX power>", LMP_INQ_TX_PWR }, /* Bit 1 */
662 { "<no. 58>", 0x04 }, /* Bit 2 */
663 { "<no. 59>", 0x08 }, /* Bit 3 */
664 { "<no. 60>", 0x10 }, /* Bit 4 */
665 { "<no. 61>", 0x20 }, /* Bit 5 */
666 { "<no. 62>", 0x40 }, /* Bit 6 */
667 { "<extended features>",LMP_EXT_FEAT }, /* Bit 7 */
668 { NULL }
669 },
670 };
671
lmp_featurestostr(uint8_t * features,char * pref,int width)672 char *lmp_featurestostr(uint8_t *features, char *pref, int width)
673 {
674 char *off, *ptr, *str;
675 int i, size = 10;
676
677 for (i = 0; i < 8; i++) {
678 hci_map *m = lmp_features_map[i];
679
680 while (m->str) {
681 if (m->val & features[i])
682 size += strlen(m->str) + (pref ? strlen(pref) : 0) + 1;
683 m++;
684 }
685 }
686
687 str = bt_malloc(size);
688 if (!str)
689 return NULL;
690
691 ptr = str; *ptr = '\0';
692
693 if (pref)
694 ptr += sprintf(ptr, "%s", pref);
695
696 off = ptr;
697
698 for (i = 0; i < 8; i++) {
699 hci_map *m = lmp_features_map[i];
700
701 while (m->str) {
702 if (m->val & features[i]) {
703 if (strlen(off) + strlen(m->str) > width - 1) {
704 ptr += sprintf(ptr, "\n%s", pref ? pref : "");
705 off = ptr;
706 }
707 ptr += sprintf(ptr, "%s ", m->str);
708 }
709 m++;
710 }
711 }
712
713 return str;
714 }
715
716 /* HCI functions that do not require open device */
717
hci_for_each_dev(int flag,int (* func)(int dd,int dev_id,long arg),long arg)718 int hci_for_each_dev(int flag, int (*func)(int dd, int dev_id, long arg), long arg)
719 {
720 struct hci_dev_list_req *dl;
721 struct hci_dev_req *dr;
722 int dev_id = -1;
723 int i, sk, err = 0;
724
725 sk = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
726 if (sk < 0)
727 return -1;
728
729 dl = malloc(HCI_MAX_DEV * sizeof(*dr) + sizeof(*dl));
730 if (!dl) {
731 err = errno;
732 goto done;
733 }
734
735 memset(dl, 0, HCI_MAX_DEV * sizeof(*dr) + sizeof(*dl));
736
737 dl->dev_num = HCI_MAX_DEV;
738 dr = dl->dev_req;
739
740 if (ioctl(sk, HCIGETDEVLIST, (void *) dl) < 0) {
741 err = errno;
742 goto free;
743 }
744
745 for (i = 0; i < dl->dev_num; i++, dr++) {
746 if (hci_test_bit(flag, &dr->dev_opt))
747 if (!func || func(sk, dr->dev_id, arg)) {
748 dev_id = dr->dev_id;
749 break;
750 }
751 }
752
753 if (dev_id < 0)
754 err = ENODEV;
755
756 free:
757 free(dl);
758
759 done:
760 close(sk);
761 errno = err;
762
763 return dev_id;
764 }
765
__other_bdaddr(int dd,int dev_id,long arg)766 static int __other_bdaddr(int dd, int dev_id, long arg)
767 {
768 struct hci_dev_info di = { dev_id: dev_id };
769
770 if (ioctl(dd, HCIGETDEVINFO, (void *) &di))
771 return 0;
772
773 if (hci_test_bit(HCI_RAW, &di.flags))
774 return 0;
775
776 return bacmp((bdaddr_t *) arg, &di.bdaddr);
777 }
778
__same_bdaddr(int dd,int dev_id,long arg)779 static int __same_bdaddr(int dd, int dev_id, long arg)
780 {
781 struct hci_dev_info di = { dev_id: dev_id };
782
783 if (ioctl(dd, HCIGETDEVINFO, (void *) &di))
784 return 0;
785
786 return !bacmp((bdaddr_t *) arg, &di.bdaddr);
787 }
788
hci_get_route(bdaddr_t * bdaddr)789 int hci_get_route(bdaddr_t *bdaddr)
790 {
791 return hci_for_each_dev(HCI_UP, __other_bdaddr,
792 (long) (bdaddr ? bdaddr : BDADDR_ANY));
793 }
794
hci_devid(const char * str)795 int hci_devid(const char *str)
796 {
797 bdaddr_t ba;
798 int id = -1;
799
800 if (!strncmp(str, "hci", 3) && strlen(str) >= 4) {
801 id = atoi(str + 3);
802 if (hci_devba(id, &ba) < 0)
803 return -1;
804 } else {
805 errno = ENODEV;
806 str2ba(str, &ba);
807 id = hci_for_each_dev(HCI_UP, __same_bdaddr, (long) &ba);
808 }
809
810 return id;
811 }
812
hci_devinfo(int dev_id,struct hci_dev_info * di)813 int hci_devinfo(int dev_id, struct hci_dev_info *di)
814 {
815 int dd, err, ret;
816
817 dd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
818 if (dd < 0)
819 return dd;
820
821 memset(di, 0, sizeof(struct hci_dev_info));
822
823 di->dev_id = dev_id;
824 ret = ioctl(dd, HCIGETDEVINFO, (void *) di);
825
826 err = errno;
827 close(dd);
828 errno = err;
829
830 return ret;
831 }
832
hci_devba(int dev_id,bdaddr_t * bdaddr)833 int hci_devba(int dev_id, bdaddr_t *bdaddr)
834 {
835 struct hci_dev_info di;
836
837 memset(&di, 0, sizeof(di));
838
839 if (hci_devinfo(dev_id, &di))
840 return -1;
841
842 if (!hci_test_bit(HCI_UP, &di.flags)) {
843 errno = ENETDOWN;
844 return -1;
845 }
846
847 bacpy(bdaddr, &di.bdaddr);
848
849 return 0;
850 }
851
hci_inquiry(int dev_id,int len,int nrsp,const uint8_t * lap,inquiry_info ** ii,long flags)852 int hci_inquiry(int dev_id, int len, int nrsp, const uint8_t *lap, inquiry_info **ii, long flags)
853 {
854 struct hci_inquiry_req *ir;
855 uint8_t num_rsp = nrsp;
856 void *buf;
857 int dd, size, err, ret = -1;
858
859 if (nrsp <= 0) {
860 num_rsp = 0;
861 nrsp = 255;
862 }
863
864 if (dev_id < 0) {
865 dev_id = hci_get_route(NULL);
866 if (dev_id < 0) {
867 errno = ENODEV;
868 return -1;
869 }
870 }
871
872 dd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
873 if (dd < 0)
874 return dd;
875
876 buf = malloc(sizeof(*ir) + (sizeof(inquiry_info) * (nrsp)));
877 if (!buf)
878 goto done;
879
880 ir = buf;
881 ir->dev_id = dev_id;
882 ir->num_rsp = num_rsp;
883 ir->length = len;
884 ir->flags = flags;
885
886 if (lap) {
887 memcpy(ir->lap, lap, 3);
888 } else {
889 ir->lap[0] = 0x33;
890 ir->lap[1] = 0x8b;
891 ir->lap[2] = 0x9e;
892 }
893
894 ret = ioctl(dd, HCIINQUIRY, (unsigned long) buf);
895 if (ret < 0)
896 goto free;
897
898 size = sizeof(inquiry_info) * ir->num_rsp;
899
900 if (!*ii)
901 *ii = malloc(size);
902
903 if (*ii) {
904 memcpy((void *) *ii, buf + sizeof(*ir), size);
905 ret = ir->num_rsp;
906 } else
907 ret = -1;
908
909 free:
910 free(buf);
911
912 done:
913 err = errno;
914 close(dd);
915 errno = err;
916
917 return ret;
918 }
919
920 /* Open HCI device.
921 * Returns device descriptor (dd). */
hci_open_dev(int dev_id)922 int hci_open_dev(int dev_id)
923 {
924 struct sockaddr_hci a;
925 int dd, err;
926
927 /* Create HCI socket */
928 dd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
929 if (dd < 0)
930 return dd;
931
932 /* Bind socket to the HCI device */
933 memset(&a, 0, sizeof(a));
934 a.hci_family = AF_BLUETOOTH;
935 a.hci_dev = dev_id;
936 if (bind(dd, (struct sockaddr *) &a, sizeof(a)) < 0)
937 goto failed;
938
939 return dd;
940
941 failed:
942 err = errno;
943 close(dd);
944 errno = err;
945
946 return -1;
947 }
948
hci_close_dev(int dd)949 int hci_close_dev(int dd)
950 {
951 return close(dd);
952 }
953
954 /* HCI functions that require open device
955 * dd - Device descriptor returned by hci_open_dev. */
956
hci_send_cmd(int dd,uint16_t ogf,uint16_t ocf,uint8_t plen,void * param)957 int hci_send_cmd(int dd, uint16_t ogf, uint16_t ocf, uint8_t plen, void *param)
958 {
959 uint8_t type = HCI_COMMAND_PKT;
960 hci_command_hdr hc;
961 struct iovec iv[3];
962 int ivn;
963
964 hc.opcode = htobs(cmd_opcode_pack(ogf, ocf));
965 hc.plen= plen;
966
967 iv[0].iov_base = &type;
968 iv[0].iov_len = 1;
969 iv[1].iov_base = &hc;
970 iv[1].iov_len = HCI_COMMAND_HDR_SIZE;
971 ivn = 2;
972
973 if (plen) {
974 iv[2].iov_base = param;
975 iv[2].iov_len = plen;
976 ivn = 3;
977 }
978
979 while (writev(dd, iv, ivn) < 0) {
980 if (errno == EAGAIN || errno == EINTR)
981 continue;
982 return -1;
983 }
984 return 0;
985 }
986
hci_send_req(int dd,struct hci_request * r,int to)987 int hci_send_req(int dd, struct hci_request *r, int to)
988 {
989 unsigned char buf[HCI_MAX_EVENT_SIZE], *ptr;
990 uint16_t opcode = htobs(cmd_opcode_pack(r->ogf, r->ocf));
991 struct hci_filter nf, of;
992 socklen_t len;
993 hci_event_hdr *hdr;
994 int err, try;
995
996 len = sizeof(of);
997 if (getsockopt(dd, SOL_HCI, HCI_FILTER, &of, &len) < 0)
998 return -1;
999
1000 hci_filter_clear(&nf);
1001 hci_filter_set_ptype(HCI_EVENT_PKT, &nf);
1002 hci_filter_set_event(EVT_CMD_STATUS, &nf);
1003 hci_filter_set_event(EVT_CMD_COMPLETE, &nf);
1004 hci_filter_set_event(r->event, &nf);
1005 hci_filter_set_opcode(opcode, &nf);
1006 if (setsockopt(dd, SOL_HCI, HCI_FILTER, &nf, sizeof(nf)) < 0)
1007 return -1;
1008
1009 if (hci_send_cmd(dd, r->ogf, r->ocf, r->clen, r->cparam) < 0)
1010 goto failed;
1011
1012 try = 10;
1013 while (try--) {
1014 evt_cmd_complete *cc;
1015 evt_cmd_status *cs;
1016 evt_remote_name_req_complete *rn;
1017 remote_name_req_cp *cp;
1018
1019 if (to) {
1020 struct pollfd p;
1021 int n;
1022
1023 p.fd = dd; p.events = POLLIN;
1024 while ((n = poll(&p, 1, to)) < 0) {
1025 if (errno == EAGAIN || errno == EINTR)
1026 continue;
1027 goto failed;
1028 }
1029
1030 if (!n) {
1031 errno = ETIMEDOUT;
1032 goto failed;
1033 }
1034
1035 to -= 10;
1036 if (to < 0) to = 0;
1037
1038 }
1039
1040 while ((len = read(dd, buf, sizeof(buf))) < 0) {
1041 if (errno == EAGAIN || errno == EINTR)
1042 continue;
1043 goto failed;
1044 }
1045
1046 hdr = (void *) (buf + 1);
1047 ptr = buf + (1 + HCI_EVENT_HDR_SIZE);
1048 len -= (1 + HCI_EVENT_HDR_SIZE);
1049
1050 switch (hdr->evt) {
1051 case EVT_CMD_STATUS:
1052 cs = (void *) ptr;
1053
1054 if (cs->opcode != opcode)
1055 continue;
1056
1057 if (r->event != EVT_CMD_STATUS) {
1058 if (cs->status) {
1059 errno = EIO;
1060 goto failed;
1061 }
1062 break;
1063 }
1064
1065 r->rlen = MIN(len, r->rlen);
1066 memcpy(r->rparam, ptr, r->rlen);
1067 goto done;
1068
1069 case EVT_CMD_COMPLETE:
1070 cc = (void *) ptr;
1071
1072 if (cc->opcode != opcode)
1073 continue;
1074
1075 ptr += EVT_CMD_COMPLETE_SIZE;
1076 len -= EVT_CMD_COMPLETE_SIZE;
1077
1078 r->rlen = MIN(len, r->rlen);
1079 memcpy(r->rparam, ptr, r->rlen);
1080 goto done;
1081
1082 case EVT_REMOTE_NAME_REQ_COMPLETE:
1083 if (hdr->evt != r->event)
1084 break;
1085
1086 rn = (void *) ptr;
1087 cp = r->cparam;
1088
1089 if (bacmp(&rn->bdaddr, &cp->bdaddr))
1090 continue;
1091
1092 r->rlen = MIN(len, r->rlen);
1093 memcpy(r->rparam, ptr, r->rlen);
1094 goto done;
1095
1096 default:
1097 if (hdr->evt != r->event)
1098 break;
1099
1100 r->rlen = MIN(len, r->rlen);
1101 memcpy(r->rparam, ptr, r->rlen);
1102 goto done;
1103 }
1104 }
1105 errno = ETIMEDOUT;
1106
1107 failed:
1108 err = errno;
1109 setsockopt(dd, SOL_HCI, HCI_FILTER, &of, sizeof(of));
1110 errno = err;
1111 return -1;
1112
1113 done:
1114 setsockopt(dd, SOL_HCI, HCI_FILTER, &of, sizeof(of));
1115 return 0;
1116 }
1117
hci_create_connection(int dd,const bdaddr_t * bdaddr,uint16_t ptype,uint16_t clkoffset,uint8_t rswitch,uint16_t * handle,int to)1118 int hci_create_connection(int dd, const bdaddr_t *bdaddr, uint16_t ptype, uint16_t clkoffset, uint8_t rswitch, uint16_t *handle, int to)
1119 {
1120 evt_conn_complete rp;
1121 create_conn_cp cp;
1122 struct hci_request rq;
1123
1124 memset(&cp, 0, sizeof(cp));
1125 bacpy(&cp.bdaddr, bdaddr);
1126 cp.pkt_type = ptype;
1127 cp.pscan_rep_mode = 0x02;
1128 cp.clock_offset = clkoffset;
1129 cp.role_switch = rswitch;
1130
1131 memset(&rq, 0, sizeof(rq));
1132 rq.ogf = OGF_LINK_CTL;
1133 rq.ocf = OCF_CREATE_CONN;
1134 rq.event = EVT_CONN_COMPLETE;
1135 rq.cparam = &cp;
1136 rq.clen = CREATE_CONN_CP_SIZE;
1137 rq.rparam = &rp;
1138 rq.rlen = EVT_CONN_COMPLETE_SIZE;
1139
1140 if (hci_send_req(dd, &rq, to) < 0)
1141 return -1;
1142
1143 if (rp.status) {
1144 errno = EIO;
1145 return -1;
1146 }
1147
1148 *handle = rp.handle;
1149 return 0;
1150 }
1151
hci_disconnect(int dd,uint16_t handle,uint8_t reason,int to)1152 int hci_disconnect(int dd, uint16_t handle, uint8_t reason, int to)
1153 {
1154 evt_disconn_complete rp;
1155 disconnect_cp cp;
1156 struct hci_request rq;
1157
1158 memset(&cp, 0, sizeof(cp));
1159 cp.handle = handle;
1160 cp.reason = reason;
1161
1162 memset(&rq, 0, sizeof(rq));
1163 rq.ogf = OGF_LINK_CTL;
1164 rq.ocf = OCF_DISCONNECT;
1165 rq.event = EVT_DISCONN_COMPLETE;
1166 rq.cparam = &cp;
1167 rq.clen = DISCONNECT_CP_SIZE;
1168 rq.rparam = &rp;
1169 rq.rlen = EVT_DISCONN_COMPLETE_SIZE;
1170
1171 if (hci_send_req(dd, &rq, to) < 0)
1172 return -1;
1173
1174 if (rp.status) {
1175 errno = EIO;
1176 return -1;
1177 }
1178 return 0;
1179 }
1180
hci_read_local_name(int dd,int len,char * name,int to)1181 int hci_read_local_name(int dd, int len, char *name, int to)
1182 {
1183 read_local_name_rp rp;
1184 struct hci_request rq;
1185
1186 memset(&rq, 0, sizeof(rq));
1187 rq.ogf = OGF_HOST_CTL;
1188 rq.ocf = OCF_READ_LOCAL_NAME;
1189 rq.rparam = &rp;
1190 rq.rlen = READ_LOCAL_NAME_RP_SIZE;
1191
1192 if (hci_send_req(dd, &rq, to) < 0)
1193 return -1;
1194
1195 if (rp.status) {
1196 errno = EIO;
1197 return -1;
1198 }
1199
1200 rp.name[247] = '\0';
1201 strncpy(name, (char *) rp.name, len);
1202 return 0;
1203 }
1204
hci_write_local_name(int dd,const char * name,int to)1205 int hci_write_local_name(int dd, const char *name, int to)
1206 {
1207 change_local_name_cp cp;
1208 struct hci_request rq;
1209
1210 memset(&cp, 0, sizeof(cp));
1211 strncpy((char *) cp.name, name, sizeof(cp.name));
1212
1213 memset(&rq, 0, sizeof(rq));
1214 rq.ogf = OGF_HOST_CTL;
1215 rq.ocf = OCF_CHANGE_LOCAL_NAME;
1216 rq.cparam = &cp;
1217 rq.clen = CHANGE_LOCAL_NAME_CP_SIZE;
1218
1219 if (hci_send_req(dd, &rq, to) < 0)
1220 return -1;
1221
1222 return 0;
1223 }
1224
hci_read_remote_name_with_clock_offset(int dd,const bdaddr_t * bdaddr,uint8_t pscan_rep_mode,uint16_t clkoffset,int len,char * name,int to)1225 int hci_read_remote_name_with_clock_offset(int dd, const bdaddr_t *bdaddr, uint8_t pscan_rep_mode, uint16_t clkoffset, int len, char *name, int to)
1226 {
1227 evt_remote_name_req_complete rn;
1228 remote_name_req_cp cp;
1229 struct hci_request rq;
1230
1231 memset(&cp, 0, sizeof(cp));
1232 bacpy(&cp.bdaddr, bdaddr);
1233 cp.pscan_rep_mode = pscan_rep_mode;
1234 cp.clock_offset = clkoffset;
1235
1236 memset(&rq, 0, sizeof(rq));
1237 rq.ogf = OGF_LINK_CTL;
1238 rq.ocf = OCF_REMOTE_NAME_REQ;
1239 rq.cparam = &cp;
1240 rq.clen = REMOTE_NAME_REQ_CP_SIZE;
1241 rq.event = EVT_REMOTE_NAME_REQ_COMPLETE;
1242 rq.rparam = &rn;
1243 rq.rlen = EVT_REMOTE_NAME_REQ_COMPLETE_SIZE;
1244
1245 if (hci_send_req(dd, &rq, to) < 0)
1246 return -1;
1247
1248 if (rn.status) {
1249 errno = EIO;
1250 return -1;
1251 }
1252
1253 rn.name[247] = '\0';
1254 strncpy(name, (char *) rn.name, len);
1255 return 0;
1256 }
1257
hci_read_remote_name(int dd,const bdaddr_t * bdaddr,int len,char * name,int to)1258 int hci_read_remote_name(int dd, const bdaddr_t *bdaddr, int len, char *name, int to)
1259 {
1260 return hci_read_remote_name_with_clock_offset(dd, bdaddr, 0x02, 0x0000, len, name, to);
1261 }
1262
hci_read_remote_name_cancel(int dd,const bdaddr_t * bdaddr,int to)1263 int hci_read_remote_name_cancel(int dd, const bdaddr_t *bdaddr, int to)
1264 {
1265 remote_name_req_cancel_cp cp;
1266 struct hci_request rq;
1267
1268 memset(&cp, 0, sizeof(cp));
1269 bacpy(&cp.bdaddr, bdaddr);
1270
1271 memset(&rq, 0, sizeof(rq));
1272 rq.ogf = OGF_LINK_CTL;
1273 rq.ocf = OCF_REMOTE_NAME_REQ_CANCEL;
1274 rq.cparam = &cp;
1275 rq.clen = REMOTE_NAME_REQ_CANCEL_CP_SIZE;
1276
1277 if (hci_send_req(dd, &rq, to) < 0)
1278 return -1;
1279
1280 return 0;
1281 }
1282
hci_read_remote_version(int dd,uint16_t handle,struct hci_version * ver,int to)1283 int hci_read_remote_version(int dd, uint16_t handle, struct hci_version *ver, int to)
1284 {
1285 evt_read_remote_version_complete rp;
1286 read_remote_version_cp cp;
1287 struct hci_request rq;
1288
1289 memset(&cp, 0, sizeof(cp));
1290 cp.handle = handle;
1291
1292 memset(&rq, 0, sizeof(rq));
1293 rq.ogf = OGF_LINK_CTL;
1294 rq.ocf = OCF_READ_REMOTE_VERSION;
1295 rq.event = EVT_READ_REMOTE_VERSION_COMPLETE;
1296 rq.cparam = &cp;
1297 rq.clen = READ_REMOTE_VERSION_CP_SIZE;
1298 rq.rparam = &rp;
1299 rq.rlen = EVT_READ_REMOTE_VERSION_COMPLETE_SIZE;
1300
1301 if (hci_send_req(dd, &rq, to) < 0)
1302 return -1;
1303
1304 if (rp.status) {
1305 errno = EIO;
1306 return -1;
1307 }
1308
1309 ver->manufacturer = btohs(rp.manufacturer);
1310 ver->lmp_ver = rp.lmp_ver;
1311 ver->lmp_subver = btohs(rp.lmp_subver);
1312 return 0;
1313 }
1314
hci_read_remote_features(int dd,uint16_t handle,uint8_t * features,int to)1315 int hci_read_remote_features(int dd, uint16_t handle, uint8_t *features, int to)
1316 {
1317 evt_read_remote_features_complete rp;
1318 read_remote_features_cp cp;
1319 struct hci_request rq;
1320
1321 memset(&cp, 0, sizeof(cp));
1322 cp.handle = handle;
1323
1324 memset(&rq, 0, sizeof(rq));
1325 rq.ogf = OGF_LINK_CTL;
1326 rq.ocf = OCF_READ_REMOTE_FEATURES;
1327 rq.event = EVT_READ_REMOTE_FEATURES_COMPLETE;
1328 rq.cparam = &cp;
1329 rq.clen = READ_REMOTE_FEATURES_CP_SIZE;
1330 rq.rparam = &rp;
1331 rq.rlen = EVT_READ_REMOTE_FEATURES_COMPLETE_SIZE;
1332
1333 if (hci_send_req(dd, &rq, to) < 0)
1334 return -1;
1335
1336 if (rp.status) {
1337 errno = EIO;
1338 return -1;
1339 }
1340
1341 memcpy(features, rp.features, 8);
1342 return 0;
1343 }
1344
hci_read_remote_ext_features(int dd,uint16_t handle,uint8_t page,uint8_t * max_page,uint8_t * features,int to)1345 int hci_read_remote_ext_features(int dd, uint16_t handle, uint8_t page, uint8_t *max_page, uint8_t *features, int to)
1346 {
1347 evt_read_remote_ext_features_complete rp;
1348 read_remote_ext_features_cp cp;
1349 struct hci_request rq;
1350
1351 memset(&cp, 0, sizeof(cp));
1352 cp.handle = handle;
1353 cp.page_num = page;
1354
1355 memset(&rq, 0, sizeof(rq));
1356 rq.ogf = OGF_LINK_CTL;
1357 rq.ocf = OCF_READ_REMOTE_EXT_FEATURES;
1358 rq.event = EVT_READ_REMOTE_EXT_FEATURES_COMPLETE;
1359 rq.cparam = &cp;
1360 rq.clen = READ_REMOTE_EXT_FEATURES_CP_SIZE;
1361 rq.rparam = &rp;
1362 rq.rlen = EVT_READ_REMOTE_EXT_FEATURES_COMPLETE_SIZE;
1363
1364 if (hci_send_req(dd, &rq, to) < 0)
1365 return -1;
1366
1367 if (rp.status) {
1368 errno = EIO;
1369 return -1;
1370 }
1371
1372 *max_page = rp.max_page_num;
1373 memcpy(features, rp.features, 8);
1374 return 0;
1375 }
1376
hci_read_clock_offset(int dd,uint16_t handle,uint16_t * clkoffset,int to)1377 int hci_read_clock_offset(int dd, uint16_t handle, uint16_t *clkoffset, int to)
1378 {
1379 evt_read_clock_offset_complete rp;
1380 read_clock_offset_cp cp;
1381 struct hci_request rq;
1382
1383 memset(&cp, 0, sizeof(cp));
1384 cp.handle = handle;
1385
1386 memset(&rq, 0, sizeof(rq));
1387 rq.ogf = OGF_LINK_CTL;
1388 rq.ocf = OCF_READ_CLOCK_OFFSET;
1389 rq.event = EVT_READ_CLOCK_OFFSET_COMPLETE;
1390 rq.cparam = &cp;
1391 rq.clen = READ_CLOCK_OFFSET_CP_SIZE;
1392 rq.rparam = &rp;
1393 rq.rlen = EVT_READ_CLOCK_OFFSET_COMPLETE_SIZE;
1394
1395 if (hci_send_req(dd, &rq, to) < 0)
1396 return -1;
1397
1398 if (rp.status) {
1399 errno = EIO;
1400 return -1;
1401 }
1402
1403 *clkoffset = rp.clock_offset;
1404 return 0;
1405 }
1406
hci_read_local_version(int dd,struct hci_version * ver,int to)1407 int hci_read_local_version(int dd, struct hci_version *ver, int to)
1408 {
1409 read_local_version_rp rp;
1410 struct hci_request rq;
1411
1412 memset(&rq, 0, sizeof(rq));
1413 rq.ogf = OGF_INFO_PARAM;
1414 rq.ocf = OCF_READ_LOCAL_VERSION;
1415 rq.rparam = &rp;
1416 rq.rlen = READ_LOCAL_VERSION_RP_SIZE;
1417
1418 if (hci_send_req(dd, &rq, to) < 0)
1419 return -1;
1420
1421 if (rp.status) {
1422 errno = EIO;
1423 return -1;
1424 }
1425
1426 ver->manufacturer = btohs(rp.manufacturer);
1427 ver->hci_ver = rp.hci_ver;
1428 ver->hci_rev = btohs(rp.hci_rev);
1429 ver->lmp_ver = rp.lmp_ver;
1430 ver->lmp_subver = btohs(rp.lmp_subver);
1431 return 0;
1432 }
1433
hci_read_local_commands(int dd,uint8_t * commands,int to)1434 int hci_read_local_commands(int dd, uint8_t *commands, int to)
1435 {
1436 read_local_commands_rp rp;
1437 struct hci_request rq;
1438
1439 memset(&rq, 0, sizeof(rq));
1440 rq.ogf = OGF_INFO_PARAM;
1441 rq.ocf = OCF_READ_LOCAL_COMMANDS;
1442 rq.rparam = &rp;
1443 rq.rlen = READ_LOCAL_COMMANDS_RP_SIZE;
1444
1445 if (hci_send_req(dd, &rq, to) < 0)
1446 return -1;
1447
1448 if (rp.status) {
1449 errno = EIO;
1450 return -1;
1451 }
1452
1453 memcpy(commands, rp.commands, 64);
1454 return 0;
1455 }
1456
hci_read_local_features(int dd,uint8_t * features,int to)1457 int hci_read_local_features(int dd, uint8_t *features, int to)
1458 {
1459 read_local_features_rp rp;
1460 struct hci_request rq;
1461
1462 memset(&rq, 0, sizeof(rq));
1463 rq.ogf = OGF_INFO_PARAM;
1464 rq.ocf = OCF_READ_LOCAL_FEATURES;
1465 rq.rparam = &rp;
1466 rq.rlen = READ_LOCAL_FEATURES_RP_SIZE;
1467
1468 if (hci_send_req(dd, &rq, to) < 0)
1469 return -1;
1470
1471 if (rp.status) {
1472 errno = EIO;
1473 return -1;
1474 }
1475
1476 memcpy(features, rp.features, 8);
1477 return 0;
1478 }
1479
hci_read_local_ext_features(int dd,uint8_t page,uint8_t * max_page,uint8_t * features,int to)1480 int hci_read_local_ext_features(int dd, uint8_t page, uint8_t *max_page, uint8_t *features, int to)
1481 {
1482 read_local_ext_features_cp cp;
1483 read_local_ext_features_rp rp;
1484 struct hci_request rq;
1485
1486 cp.page_num = page;
1487
1488 memset(&rq, 0, sizeof(rq));
1489 rq.ogf = OGF_INFO_PARAM;
1490 rq.ocf = OCF_READ_LOCAL_EXT_FEATURES;
1491 rq.cparam = &cp;
1492 rq.clen = READ_LOCAL_EXT_FEATURES_CP_SIZE;
1493 rq.rparam = &rp;
1494 rq.rlen = READ_LOCAL_EXT_FEATURES_RP_SIZE;
1495
1496 if (hci_send_req(dd, &rq, to) < 0)
1497 return -1;
1498
1499 if (rp.status) {
1500 errno = EIO;
1501 return -1;
1502 }
1503
1504 *max_page = rp.max_page_num;
1505 memcpy(features, rp.features, 8);
1506 return 0;
1507 }
1508
hci_read_bd_addr(int dd,bdaddr_t * bdaddr,int to)1509 int hci_read_bd_addr(int dd, bdaddr_t *bdaddr, int to)
1510 {
1511 read_bd_addr_rp rp;
1512 struct hci_request rq;
1513
1514 memset(&rq, 0, sizeof(rq));
1515 rq.ogf = OGF_INFO_PARAM;
1516 rq.ocf = OCF_READ_BD_ADDR;
1517 rq.rparam = &rp;
1518 rq.rlen = READ_BD_ADDR_RP_SIZE;
1519
1520 if (hci_send_req(dd, &rq, to) < 0)
1521 return -1;
1522
1523 if (rp.status) {
1524 errno = EIO;
1525 return -1;
1526 }
1527
1528 bacpy(bdaddr, &rp.bdaddr);
1529 return 0;
1530 }
1531
hci_read_class_of_dev(int dd,uint8_t * cls,int to)1532 int hci_read_class_of_dev(int dd, uint8_t *cls, int to)
1533 {
1534 read_class_of_dev_rp rp;
1535 struct hci_request rq;
1536
1537 memset(&rq, 0, sizeof(rq));
1538 rq.ogf = OGF_HOST_CTL;
1539 rq.ocf = OCF_READ_CLASS_OF_DEV;
1540 rq.rparam = &rp;
1541 rq.rlen = READ_CLASS_OF_DEV_RP_SIZE;
1542
1543 if (hci_send_req(dd, &rq, to) < 0)
1544 return -1;
1545
1546 if (rp.status) {
1547 errno = EIO;
1548 return -1;
1549 }
1550
1551 memcpy(cls, rp.dev_class, 3);
1552 return 0;
1553 }
1554
hci_write_class_of_dev(int dd,uint32_t cls,int to)1555 int hci_write_class_of_dev(int dd, uint32_t cls, int to)
1556 {
1557 write_class_of_dev_cp cp;
1558 struct hci_request rq;
1559
1560 memset(&rq, 0, sizeof(rq));
1561 cp.dev_class[0] = cls & 0xff;
1562 cp.dev_class[1] = (cls >> 8) & 0xff;
1563 cp.dev_class[2] = (cls >> 16) & 0xff;
1564 rq.ogf = OGF_HOST_CTL;
1565 rq.ocf = OCF_WRITE_CLASS_OF_DEV;
1566 rq.cparam = &cp;
1567 rq.clen = WRITE_CLASS_OF_DEV_CP_SIZE;
1568 return hci_send_req(dd, &rq, to);
1569 }
1570
hci_read_voice_setting(int dd,uint16_t * vs,int to)1571 int hci_read_voice_setting(int dd, uint16_t *vs, int to)
1572 {
1573 read_voice_setting_rp rp;
1574 struct hci_request rq;
1575
1576 memset(&rq, 0, sizeof(rq));
1577 rq.ogf = OGF_HOST_CTL;
1578 rq.ocf = OCF_READ_VOICE_SETTING;
1579 rq.rparam = &rp;
1580 rq.rlen = READ_VOICE_SETTING_RP_SIZE;
1581
1582 if (hci_send_req(dd, &rq, to) < 0)
1583 return -1;
1584
1585 if (rp.status) {
1586 errno = EIO;
1587 return -1;
1588 }
1589
1590 *vs = rp.voice_setting;
1591 return 0;
1592 }
1593
hci_write_voice_setting(int dd,uint16_t vs,int to)1594 int hci_write_voice_setting(int dd, uint16_t vs, int to)
1595 {
1596 write_voice_setting_cp cp;
1597 struct hci_request rq;
1598
1599 memset(&rq, 0, sizeof(rq));
1600 cp.voice_setting = vs;
1601 rq.ogf = OGF_HOST_CTL;
1602 rq.ocf = OCF_WRITE_VOICE_SETTING;
1603 rq.cparam = &cp;
1604 rq.clen = WRITE_VOICE_SETTING_CP_SIZE;
1605
1606 return hci_send_req(dd, &rq, to);
1607 }
1608
hci_read_current_iac_lap(int dd,uint8_t * num_iac,uint8_t * lap,int to)1609 int hci_read_current_iac_lap(int dd, uint8_t *num_iac, uint8_t *lap, int to)
1610 {
1611 read_current_iac_lap_rp rp;
1612 struct hci_request rq;
1613
1614 memset(&rq, 0, sizeof(rq));
1615 rq.ogf = OGF_HOST_CTL;
1616 rq.ocf = OCF_READ_CURRENT_IAC_LAP;
1617 rq.rparam = &rp;
1618 rq.rlen = READ_CURRENT_IAC_LAP_RP_SIZE;
1619
1620 if (hci_send_req(dd, &rq, to) < 0)
1621 return -1;
1622
1623 if (rp.status) {
1624 errno = EIO;
1625 return -1;
1626 }
1627
1628 *num_iac = rp.num_current_iac;
1629 memcpy(lap, rp.lap, rp.num_current_iac * 3);
1630 return 0;
1631 }
1632
hci_write_current_iac_lap(int dd,uint8_t num_iac,uint8_t * lap,int to)1633 int hci_write_current_iac_lap(int dd, uint8_t num_iac, uint8_t *lap, int to)
1634 {
1635 write_current_iac_lap_cp cp;
1636 struct hci_request rq;
1637
1638 memset(&cp, 0, sizeof(cp));
1639 cp.num_current_iac = num_iac;
1640 memcpy(&cp.lap, lap, num_iac * 3);
1641
1642 memset(&rq, 0, sizeof(rq));
1643 rq.ogf = OGF_HOST_CTL;
1644 rq.ocf = OCF_WRITE_CURRENT_IAC_LAP;
1645 rq.cparam = &cp;
1646 rq.clen = num_iac * 3 + 1;
1647
1648 return hci_send_req(dd, &rq, to);
1649 }
1650
hci_read_stored_link_key(int dd,bdaddr_t * bdaddr,uint8_t all,int to)1651 int hci_read_stored_link_key(int dd, bdaddr_t *bdaddr, uint8_t all, int to)
1652 {
1653 read_stored_link_key_cp cp;
1654 struct hci_request rq;
1655
1656 memset(&cp, 0, sizeof(cp));
1657 bacpy(&cp.bdaddr, bdaddr);
1658 cp.read_all = all;
1659
1660 memset(&rq, 0, sizeof(rq));
1661 rq.ogf = OGF_HOST_CTL;
1662 rq.ocf = OCF_READ_STORED_LINK_KEY;
1663 rq.cparam = &cp;
1664 rq.clen = READ_STORED_LINK_KEY_CP_SIZE;
1665
1666 return hci_send_req(dd, &rq, to);
1667 }
1668
hci_write_stored_link_key(int dd,bdaddr_t * bdaddr,uint8_t * key,int to)1669 int hci_write_stored_link_key(int dd, bdaddr_t *bdaddr, uint8_t *key, int to)
1670 {
1671 unsigned char cp[WRITE_STORED_LINK_KEY_CP_SIZE + 6 + 16];
1672 struct hci_request rq;
1673
1674 memset(&cp, 0, sizeof(cp));
1675 cp[0] = 1;
1676 bacpy((bdaddr_t *) (cp + 1), bdaddr);
1677 memcpy(cp + 7, key, 16);
1678
1679 memset(&rq, 0, sizeof(rq));
1680 rq.ogf = OGF_HOST_CTL;
1681 rq.ocf = OCF_WRITE_STORED_LINK_KEY;
1682 rq.cparam = &cp;
1683 rq.clen = WRITE_STORED_LINK_KEY_CP_SIZE + 6 + 16;
1684
1685 return hci_send_req(dd, &rq, to);
1686 }
1687
hci_delete_stored_link_key(int dd,bdaddr_t * bdaddr,uint8_t all,int to)1688 int hci_delete_stored_link_key(int dd, bdaddr_t *bdaddr, uint8_t all, int to)
1689 {
1690 delete_stored_link_key_cp cp;
1691 struct hci_request rq;
1692
1693 memset(&cp, 0, sizeof(cp));
1694 bacpy(&cp.bdaddr, bdaddr);
1695 cp.delete_all = all;
1696
1697 memset(&rq, 0, sizeof(rq));
1698 rq.ogf = OGF_HOST_CTL;
1699 rq.ocf = OCF_DELETE_STORED_LINK_KEY;
1700 rq.cparam = &cp;
1701 rq.clen = DELETE_STORED_LINK_KEY_CP_SIZE;
1702
1703 return hci_send_req(dd, &rq, to);
1704 }
1705
hci_authenticate_link(int dd,uint16_t handle,int to)1706 int hci_authenticate_link(int dd, uint16_t handle, int to)
1707 {
1708 auth_requested_cp cp;
1709 evt_auth_complete rp;
1710 struct hci_request rq;
1711
1712 cp.handle = handle;
1713
1714 rq.ogf = OGF_LINK_CTL;
1715 rq.ocf = OCF_AUTH_REQUESTED;
1716 rq.event = EVT_AUTH_COMPLETE;
1717 rq.cparam = &cp;
1718 rq.clen = AUTH_REQUESTED_CP_SIZE;
1719 rq.rparam = &rp;
1720 rq.rlen = EVT_AUTH_COMPLETE_SIZE;
1721
1722 if (hci_send_req(dd, &rq, to) < 0)
1723 return -1;
1724
1725 if (rp.status) {
1726 errno = EIO;
1727 return -1;
1728 }
1729
1730 return 0;
1731 }
1732
hci_encrypt_link(int dd,uint16_t handle,uint8_t encrypt,int to)1733 int hci_encrypt_link(int dd, uint16_t handle, uint8_t encrypt, int to)
1734 {
1735 set_conn_encrypt_cp cp;
1736 evt_encrypt_change rp;
1737 struct hci_request rq;
1738
1739 cp.handle = handle;
1740 cp.encrypt = encrypt;
1741
1742 rq.ogf = OGF_LINK_CTL;
1743 rq.ocf = OCF_SET_CONN_ENCRYPT;
1744 rq.event = EVT_ENCRYPT_CHANGE;
1745 rq.cparam = &cp;
1746 rq.clen = SET_CONN_ENCRYPT_CP_SIZE;
1747 rq.rparam = &rp;
1748 rq.rlen = EVT_ENCRYPT_CHANGE_SIZE;
1749
1750 if (hci_send_req(dd, &rq, to) < 0)
1751 return -1;
1752
1753 if (rp.status) {
1754 errno = EIO;
1755 return -1;
1756 }
1757
1758 return 0;
1759 }
1760
hci_change_link_key(int dd,uint16_t handle,int to)1761 int hci_change_link_key(int dd, uint16_t handle, int to)
1762 {
1763 change_conn_link_key_cp cp;
1764 evt_change_conn_link_key_complete rp;
1765 struct hci_request rq;
1766
1767 cp.handle = handle;
1768
1769 rq.ogf = OGF_LINK_CTL;
1770 rq.ocf = OCF_CHANGE_CONN_LINK_KEY;
1771 rq.event = EVT_CHANGE_CONN_LINK_KEY_COMPLETE;
1772 rq.cparam = &cp;
1773 rq.clen = CHANGE_CONN_LINK_KEY_CP_SIZE;
1774 rq.rparam = &rp;
1775 rq.rlen = EVT_CHANGE_CONN_LINK_KEY_COMPLETE_SIZE;
1776
1777 if (hci_send_req(dd, &rq, to) < 0)
1778 return -1;
1779
1780 if (rp.status) {
1781 errno = EIO;
1782 return -1;
1783 }
1784
1785 return 0;
1786 }
1787
hci_switch_role(int dd,bdaddr_t * bdaddr,uint8_t role,int to)1788 int hci_switch_role(int dd, bdaddr_t *bdaddr, uint8_t role, int to)
1789 {
1790 switch_role_cp cp;
1791 evt_role_change rp;
1792 struct hci_request rq;
1793
1794 bacpy(&cp.bdaddr, bdaddr);
1795 cp.role = role;
1796 rq.ogf = OGF_LINK_POLICY;
1797 rq.ocf = OCF_SWITCH_ROLE;
1798 rq.cparam = &cp;
1799 rq.clen = SWITCH_ROLE_CP_SIZE;
1800 rq.rparam = &rp;
1801 rq.rlen = EVT_ROLE_CHANGE_SIZE;
1802 rq.event = EVT_ROLE_CHANGE;
1803
1804 if (hci_send_req(dd, &rq, to) < 0)
1805 return -1;
1806
1807 if (rp.status) {
1808 errno = EIO;
1809 return -1;
1810 }
1811
1812 return 0;
1813 }
1814
hci_park_mode(int dd,uint16_t handle,uint16_t max_interval,uint16_t min_interval,int to)1815 int hci_park_mode(int dd, uint16_t handle, uint16_t max_interval, uint16_t min_interval, int to)
1816 {
1817 park_mode_cp cp;
1818 evt_mode_change rp;
1819 struct hci_request rq;
1820
1821 memset(&cp, 0, sizeof (cp));
1822 cp.handle = handle;
1823 cp.max_interval = max_interval;
1824 cp.min_interval = min_interval;
1825
1826 memset(&rq, 0, sizeof (rq));
1827 rq.ogf = OGF_LINK_POLICY;
1828 rq.ocf = OCF_PARK_MODE;
1829 rq.event = EVT_MODE_CHANGE;
1830 rq.cparam = &cp;
1831 rq.clen = PARK_MODE_CP_SIZE;
1832 rq.rparam = &rp;
1833 rq.rlen = EVT_MODE_CHANGE_SIZE;
1834
1835 if (hci_send_req(dd, &rq, to) < 0)
1836 return -1;
1837
1838 if (rp.status) {
1839 errno = EIO;
1840 return -1;
1841 }
1842
1843 return 0;
1844 }
1845
hci_exit_park_mode(int dd,uint16_t handle,int to)1846 int hci_exit_park_mode(int dd, uint16_t handle, int to)
1847 {
1848 exit_park_mode_cp cp;
1849 evt_mode_change rp;
1850 struct hci_request rq;
1851
1852 memset(&cp, 0, sizeof (cp));
1853 cp.handle = handle;
1854
1855 memset (&rq, 0, sizeof (rq));
1856 rq.ogf = OGF_LINK_POLICY;
1857 rq.ocf = OCF_EXIT_PARK_MODE;
1858 rq.event = EVT_MODE_CHANGE;
1859 rq.cparam = &cp;
1860 rq.clen = EXIT_PARK_MODE_CP_SIZE;
1861 rq.rparam = &rp;
1862 rq.rlen = EVT_MODE_CHANGE_SIZE;
1863
1864 if (hci_send_req(dd, &rq, to) < 0)
1865 return -1;
1866
1867 if (rp.status) {
1868 errno = EIO;
1869 return -1;
1870 }
1871
1872 return 0;
1873 }
1874
hci_read_inquiry_scan_type(int dd,uint8_t * type,int to)1875 int hci_read_inquiry_scan_type(int dd, uint8_t *type, int to)
1876 {
1877 read_inquiry_scan_type_rp rp;
1878 struct hci_request rq;
1879
1880 memset(&rq, 0, sizeof(rq));
1881 rq.ogf = OGF_HOST_CTL;
1882 rq.ocf = OCF_READ_INQUIRY_SCAN_TYPE;
1883 rq.rparam = &rp;
1884 rq.rlen = READ_INQUIRY_SCAN_TYPE_RP_SIZE;
1885
1886 if (hci_send_req(dd, &rq, to) < 0)
1887 return -1;
1888
1889 if (rp.status) {
1890 errno = EIO;
1891 return -1;
1892 }
1893
1894 *type = rp.type;
1895 return 0;
1896 }
1897
hci_write_inquiry_scan_type(int dd,uint8_t type,int to)1898 int hci_write_inquiry_scan_type(int dd, uint8_t type, int to)
1899 {
1900 write_inquiry_scan_type_cp cp;
1901 write_inquiry_scan_type_rp rp;
1902 struct hci_request rq;
1903
1904 memset(&cp, 0, sizeof(cp));
1905 cp.type = type;
1906
1907 memset(&rq, 0, sizeof(rq));
1908 rq.ogf = OGF_HOST_CTL;
1909 rq.ocf = OCF_WRITE_INQUIRY_SCAN_TYPE;
1910 rq.cparam = &cp;
1911 rq.clen = WRITE_INQUIRY_SCAN_TYPE_CP_SIZE;
1912 rq.rparam = &rp;
1913 rq.rlen = WRITE_INQUIRY_SCAN_TYPE_RP_SIZE;
1914
1915 if (hci_send_req(dd, &rq, to) < 0)
1916 return -1;
1917
1918 if (rp.status) {
1919 errno = EIO;
1920 return -1;
1921 }
1922
1923 return 0;
1924 }
1925
hci_read_inquiry_mode(int dd,uint8_t * mode,int to)1926 int hci_read_inquiry_mode(int dd, uint8_t *mode, int to)
1927 {
1928 read_inquiry_mode_rp rp;
1929 struct hci_request rq;
1930
1931 memset(&rq, 0, sizeof(rq));
1932 rq.ogf = OGF_HOST_CTL;
1933 rq.ocf = OCF_READ_INQUIRY_MODE;
1934 rq.rparam = &rp;
1935 rq.rlen = READ_INQUIRY_MODE_RP_SIZE;
1936
1937 if (hci_send_req(dd, &rq, to) < 0)
1938 return -1;
1939
1940 if (rp.status) {
1941 errno = EIO;
1942 return -1;
1943 }
1944
1945 *mode = rp.mode;
1946 return 0;
1947 }
1948
hci_write_inquiry_mode(int dd,uint8_t mode,int to)1949 int hci_write_inquiry_mode(int dd, uint8_t mode, int to)
1950 {
1951 write_inquiry_mode_cp cp;
1952 write_inquiry_mode_rp rp;
1953 struct hci_request rq;
1954
1955 memset(&cp, 0, sizeof(cp));
1956 cp.mode = mode;
1957
1958 memset(&rq, 0, sizeof(rq));
1959 rq.ogf = OGF_HOST_CTL;
1960 rq.ocf = OCF_WRITE_INQUIRY_MODE;
1961 rq.cparam = &cp;
1962 rq.clen = WRITE_INQUIRY_MODE_CP_SIZE;
1963 rq.rparam = &rp;
1964 rq.rlen = WRITE_INQUIRY_MODE_RP_SIZE;
1965
1966 if (hci_send_req(dd, &rq, to) < 0)
1967 return -1;
1968
1969 if (rp.status) {
1970 errno = EIO;
1971 return -1;
1972 }
1973
1974 return 0;
1975 }
1976
hci_read_afh_mode(int dd,uint8_t * mode,int to)1977 int hci_read_afh_mode(int dd, uint8_t *mode, int to)
1978 {
1979 read_afh_mode_rp rp;
1980 struct hci_request rq;
1981
1982 memset(&rq, 0, sizeof(rq));
1983 rq.ogf = OGF_HOST_CTL;
1984 rq.ocf = OCF_READ_AFH_MODE;
1985 rq.rparam = &rp;
1986 rq.rlen = READ_AFH_MODE_RP_SIZE;
1987
1988 if (hci_send_req(dd, &rq, to) < 0)
1989 return -1;
1990
1991 if (rp.status) {
1992 errno = EIO;
1993 return -1;
1994 }
1995
1996 *mode = rp.mode;
1997 return 0;
1998 }
1999
hci_write_afh_mode(int dd,uint8_t mode,int to)2000 int hci_write_afh_mode(int dd, uint8_t mode, int to)
2001 {
2002 write_afh_mode_cp cp;
2003 write_afh_mode_rp rp;
2004 struct hci_request rq;
2005
2006 memset(&cp, 0, sizeof(cp));
2007 cp.mode = mode;
2008
2009 memset(&rq, 0, sizeof(rq));
2010 rq.ogf = OGF_HOST_CTL;
2011 rq.ocf = OCF_WRITE_AFH_MODE;
2012 rq.cparam = &cp;
2013 rq.clen = WRITE_AFH_MODE_CP_SIZE;
2014 rq.rparam = &rp;
2015 rq.rlen = WRITE_AFH_MODE_RP_SIZE;
2016
2017 if (hci_send_req(dd, &rq, to) < 0)
2018 return -1;
2019
2020 if (rp.status) {
2021 errno = EIO;
2022 return -1;
2023 }
2024
2025 return 0;
2026 }
2027
hci_read_ext_inquiry_response(int dd,uint8_t * fec,uint8_t * data,int to)2028 int hci_read_ext_inquiry_response(int dd, uint8_t *fec, uint8_t *data, int to)
2029 {
2030 read_ext_inquiry_response_rp rp;
2031 struct hci_request rq;
2032
2033 memset(&rq, 0, sizeof(rq));
2034 rq.ogf = OGF_HOST_CTL;
2035 rq.ocf = OCF_READ_EXT_INQUIRY_RESPONSE;
2036 rq.rparam = &rp;
2037 rq.rlen = READ_EXT_INQUIRY_RESPONSE_RP_SIZE;
2038
2039 if (hci_send_req(dd, &rq, to) < 0)
2040 return -1;
2041
2042 if (rp.status) {
2043 errno = EIO;
2044 return -1;
2045 }
2046
2047 *fec = rp.fec;
2048 memcpy(data, rp.data, 240);
2049
2050 return 0;
2051 }
2052
hci_write_ext_inquiry_response(int dd,uint8_t fec,uint8_t * data,int to)2053 int hci_write_ext_inquiry_response(int dd, uint8_t fec, uint8_t *data, int to)
2054 {
2055 write_ext_inquiry_response_cp cp;
2056 write_ext_inquiry_response_rp rp;
2057 struct hci_request rq;
2058
2059 memset(&cp, 0, sizeof(cp));
2060 cp.fec = fec;
2061 memcpy(cp.data, data, 240);
2062
2063 memset(&rq, 0, sizeof(rq));
2064 rq.ogf = OGF_HOST_CTL;
2065 rq.ocf = OCF_WRITE_EXT_INQUIRY_RESPONSE;
2066 rq.cparam = &cp;
2067 rq.clen = WRITE_EXT_INQUIRY_RESPONSE_CP_SIZE;
2068 rq.rparam = &rp;
2069 rq.rlen = WRITE_EXT_INQUIRY_RESPONSE_RP_SIZE;
2070
2071 if (hci_send_req(dd, &rq, to) < 0)
2072 return -1;
2073
2074 if (rp.status) {
2075 errno = EIO;
2076 return -1;
2077 }
2078
2079 return 0;
2080 }
2081
hci_read_simple_pairing_mode(int dd,uint8_t * mode,int to)2082 int hci_read_simple_pairing_mode(int dd, uint8_t *mode, int to)
2083 {
2084 read_simple_pairing_mode_rp rp;
2085 struct hci_request rq;
2086
2087 memset(&rq, 0, sizeof(rq));
2088 rq.ogf = OGF_HOST_CTL;
2089 rq.ocf = OCF_READ_SIMPLE_PAIRING_MODE;
2090 rq.rparam = &rp;
2091 rq.rlen = READ_SIMPLE_PAIRING_MODE_RP_SIZE;
2092
2093 if (hci_send_req(dd, &rq, to) < 0)
2094 return -1;
2095
2096 if (rp.status) {
2097 errno = EIO;
2098 return -1;
2099 }
2100
2101 *mode = rp.mode;
2102 return 0;
2103 }
2104
hci_write_simple_pairing_mode(int dd,uint8_t mode,int to)2105 int hci_write_simple_pairing_mode(int dd, uint8_t mode, int to)
2106 {
2107 write_simple_pairing_mode_cp cp;
2108 write_simple_pairing_mode_rp rp;
2109 struct hci_request rq;
2110
2111 memset(&cp, 0, sizeof(cp));
2112 cp.mode = mode;
2113
2114 memset(&rq, 0, sizeof(rq));
2115 rq.ogf = OGF_HOST_CTL;
2116 rq.ocf = OCF_WRITE_SIMPLE_PAIRING_MODE;
2117 rq.cparam = &cp;
2118 rq.clen = WRITE_SIMPLE_PAIRING_MODE_CP_SIZE;
2119 rq.rparam = &rp;
2120 rq.rlen = WRITE_SIMPLE_PAIRING_MODE_RP_SIZE;
2121
2122 if (hci_send_req(dd, &rq, to) < 0)
2123 return -1;
2124
2125 if (rp.status) {
2126 errno = EIO;
2127 return -1;
2128 }
2129
2130 return 0;
2131 }
2132
hci_read_local_oob_data(int dd,uint8_t * hash,uint8_t * randomizer,int to)2133 int hci_read_local_oob_data(int dd, uint8_t *hash, uint8_t *randomizer, int to)
2134 {
2135 read_local_oob_data_rp rp;
2136 struct hci_request rq;
2137
2138 memset(&rq, 0, sizeof(rq));
2139 rq.ogf = OGF_HOST_CTL;
2140 rq.ocf = OCF_READ_LOCAL_OOB_DATA;
2141 rq.rparam = &rp;
2142 rq.rlen = READ_LOCAL_OOB_DATA_RP_SIZE;
2143
2144 if (hci_send_req(dd, &rq, to) < 0)
2145 return -1;
2146
2147 if (rp.status) {
2148 errno = EIO;
2149 return -1;
2150 }
2151
2152 memcpy(hash, rp.hash, 16);
2153 memcpy(randomizer, rp.randomizer, 16);
2154 return 0;
2155 }
2156
hci_read_inquiry_transmit_power_level(int dd,int8_t * level,int to)2157 int hci_read_inquiry_transmit_power_level(int dd, int8_t *level, int to)
2158 {
2159 read_inquiry_transmit_power_level_rp rp;
2160 struct hci_request rq;
2161
2162 memset(&rq, 0, sizeof(rq));
2163 rq.ogf = OGF_HOST_CTL;
2164 rq.ocf = OCF_READ_INQUIRY_TRANSMIT_POWER_LEVEL;
2165 rq.rparam = &rp;
2166 rq.rlen = READ_INQUIRY_TRANSMIT_POWER_LEVEL_RP_SIZE;
2167
2168 if (hci_send_req(dd, &rq, to) < 0)
2169 return -1;
2170
2171 if (rp.status) {
2172 errno = EIO;
2173 return -1;
2174 }
2175
2176 *level = rp.level;
2177 return 0;
2178 }
2179
hci_write_inquiry_transmit_power_level(int dd,int8_t level,int to)2180 int hci_write_inquiry_transmit_power_level(int dd, int8_t level, int to)
2181 {
2182 write_inquiry_transmit_power_level_cp cp;
2183 write_inquiry_transmit_power_level_rp rp;
2184 struct hci_request rq;
2185
2186 memset(&cp, 0, sizeof(cp));
2187 cp.level = level;
2188
2189 memset(&rq, 0, sizeof(rq));
2190 rq.ogf = OGF_HOST_CTL;
2191 rq.ocf = OCF_WRITE_INQUIRY_TRANSMIT_POWER_LEVEL;
2192 rq.cparam = &cp;
2193 rq.clen = WRITE_INQUIRY_TRANSMIT_POWER_LEVEL_CP_SIZE;
2194 rq.rparam = &rp;
2195 rq.rlen = WRITE_INQUIRY_TRANSMIT_POWER_LEVEL_RP_SIZE;
2196
2197 if (hci_send_req(dd, &rq, to) < 0)
2198 return -1;
2199
2200 if (rp.status) {
2201 errno = EIO;
2202 return -1;
2203 }
2204
2205 return 0;
2206 }
2207
hci_read_transmit_power_level(int dd,uint16_t handle,uint8_t type,int8_t * level,int to)2208 int hci_read_transmit_power_level(int dd, uint16_t handle, uint8_t type, int8_t *level, int to)
2209 {
2210 read_transmit_power_level_cp cp;
2211 read_transmit_power_level_rp rp;
2212 struct hci_request rq;
2213
2214 memset(&cp, 0, sizeof(cp));
2215 cp.handle = handle;
2216 cp.type = type;
2217
2218 memset(&rq, 0, sizeof(rq));
2219 rq.ogf = OGF_HOST_CTL;
2220 rq.ocf = OCF_READ_TRANSMIT_POWER_LEVEL;
2221 rq.cparam = &cp;
2222 rq.clen = READ_TRANSMIT_POWER_LEVEL_CP_SIZE;
2223 rq.rparam = &rp;
2224 rq.rlen = READ_TRANSMIT_POWER_LEVEL_RP_SIZE;
2225
2226 if (hci_send_req(dd, &rq, to) < 0)
2227 return -1;
2228
2229 if (rp.status) {
2230 errno = EIO;
2231 return -1;
2232 }
2233
2234 *level = rp.level;
2235 return 0;
2236 }
2237
hci_read_link_policy(int dd,uint16_t handle,uint16_t * policy,int to)2238 int hci_read_link_policy(int dd, uint16_t handle, uint16_t *policy, int to)
2239 {
2240 read_link_policy_rp rp;
2241 struct hci_request rq;
2242
2243 memset(&rq, 0, sizeof(rq));
2244 rq.ogf = OGF_LINK_POLICY;
2245 rq.ocf = OCF_READ_LINK_POLICY;
2246 rq.cparam = &handle;
2247 rq.clen = 2;
2248 rq.rparam = &rp;
2249 rq.rlen = READ_LINK_POLICY_RP_SIZE;
2250
2251 if (hci_send_req(dd, &rq, to) < 0)
2252 return -1;
2253
2254 if (rp.status) {
2255 errno = EIO;
2256 return -1;
2257 }
2258
2259 *policy = rp.policy;
2260 return 0;
2261 }
2262
hci_write_link_policy(int dd,uint16_t handle,uint16_t policy,int to)2263 int hci_write_link_policy(int dd, uint16_t handle, uint16_t policy, int to)
2264 {
2265 write_link_policy_cp cp;
2266 write_link_policy_rp rp;
2267 struct hci_request rq;
2268
2269 memset(&cp, 0, sizeof(cp));
2270 cp.handle = handle;
2271 cp.policy = policy;
2272
2273 memset(&rq, 0, sizeof(rq));
2274 rq.ogf = OGF_LINK_POLICY;
2275 rq.ocf = OCF_WRITE_LINK_POLICY;
2276 rq.cparam = &cp;
2277 rq.clen = WRITE_LINK_POLICY_CP_SIZE;
2278 rq.rparam = &rp;
2279 rq.rlen = WRITE_LINK_POLICY_RP_SIZE;
2280
2281 if (hci_send_req(dd, &rq, to) < 0)
2282 return -1;
2283
2284 if (rp.status) {
2285 errno = EIO;
2286 return -1;
2287 }
2288
2289 return 0;
2290 }
2291
hci_read_link_supervision_timeout(int dd,uint16_t handle,uint16_t * timeout,int to)2292 int hci_read_link_supervision_timeout(int dd, uint16_t handle, uint16_t *timeout, int to)
2293 {
2294 read_link_supervision_timeout_rp rp;
2295 struct hci_request rq;
2296
2297 memset(&rq, 0, sizeof(rq));
2298 rq.ogf = OGF_HOST_CTL;
2299 rq.ocf = OCF_READ_LINK_SUPERVISION_TIMEOUT;
2300 rq.cparam = &handle;
2301 rq.clen = 2;
2302 rq.rparam = &rp;
2303 rq.rlen = READ_LINK_SUPERVISION_TIMEOUT_RP_SIZE;
2304
2305 if (hci_send_req(dd, &rq, to) < 0)
2306 return -1;
2307
2308 if (rp.status) {
2309 errno = EIO;
2310 return -1;
2311 }
2312
2313 *timeout = rp.timeout;
2314 return 0;
2315 }
2316
hci_write_link_supervision_timeout(int dd,uint16_t handle,uint16_t timeout,int to)2317 int hci_write_link_supervision_timeout(int dd, uint16_t handle, uint16_t timeout, int to)
2318 {
2319 write_link_supervision_timeout_cp cp;
2320 write_link_supervision_timeout_rp rp;
2321 struct hci_request rq;
2322
2323 memset(&cp, 0, sizeof(cp));
2324 cp.handle = handle;
2325 cp.timeout = timeout;
2326
2327 memset(&rq, 0, sizeof(rq));
2328 rq.ogf = OGF_HOST_CTL;
2329 rq.ocf = OCF_WRITE_LINK_SUPERVISION_TIMEOUT;
2330 rq.cparam = &cp;
2331 rq.clen = WRITE_LINK_SUPERVISION_TIMEOUT_CP_SIZE;
2332 rq.rparam = &rp;
2333 rq.rlen = WRITE_LINK_SUPERVISION_TIMEOUT_RP_SIZE;
2334
2335 if (hci_send_req(dd, &rq, to) < 0)
2336 return -1;
2337
2338 if (rp.status) {
2339 errno = EIO;
2340 return -1;
2341 }
2342
2343 return 0;
2344 }
2345
hci_set_afh_classification(int dd,uint8_t * map,int to)2346 int hci_set_afh_classification(int dd, uint8_t *map, int to)
2347 {
2348 set_afh_classification_cp cp;
2349 set_afh_classification_rp rp;
2350 struct hci_request rq;
2351
2352 memset(&cp, 0, sizeof(cp));
2353 memcpy(cp.map, map, 10);
2354
2355 memset(&rq, 0, sizeof(rq));
2356 rq.ogf = OGF_HOST_CTL;
2357 rq.ocf = OCF_SET_AFH_CLASSIFICATION;
2358 rq.cparam = &cp;
2359 rq.clen = SET_AFH_CLASSIFICATION_CP_SIZE;
2360 rq.rparam = &rp;
2361 rq.rlen = SET_AFH_CLASSIFICATION_RP_SIZE;
2362
2363 if (hci_send_req(dd, &rq, to) < 0)
2364 return -1;
2365
2366 if (rp.status) {
2367 errno = EIO;
2368 return -1;
2369 }
2370
2371 return 0;
2372 }
2373
hci_read_link_quality(int dd,uint16_t handle,uint8_t * link_quality,int to)2374 int hci_read_link_quality(int dd, uint16_t handle, uint8_t *link_quality, int to)
2375 {
2376 read_link_quality_rp rp;
2377 struct hci_request rq;
2378
2379 memset(&rq, 0, sizeof(rq));
2380 rq.ogf = OGF_STATUS_PARAM;
2381 rq.ocf = OCF_READ_LINK_QUALITY;
2382 rq.cparam = &handle;
2383 rq.clen = 2;
2384 rq.rparam = &rp;
2385 rq.rlen = READ_LINK_QUALITY_RP_SIZE;
2386
2387 if (hci_send_req(dd, &rq, to) < 0)
2388 return -1;
2389
2390 if (rp.status) {
2391 errno = EIO;
2392 return -1;
2393 }
2394
2395 *link_quality = rp.link_quality;
2396 return 0;
2397 }
2398
hci_read_rssi(int dd,uint16_t handle,int8_t * rssi,int to)2399 int hci_read_rssi(int dd, uint16_t handle, int8_t *rssi, int to)
2400 {
2401 read_rssi_rp rp;
2402 struct hci_request rq;
2403
2404 memset(&rq, 0, sizeof(rq));
2405 rq.ogf = OGF_STATUS_PARAM;
2406 rq.ocf = OCF_READ_RSSI;
2407 rq.cparam = &handle;
2408 rq.clen = 2;
2409 rq.rparam = &rp;
2410 rq.rlen = READ_RSSI_RP_SIZE;
2411
2412 if (hci_send_req(dd, &rq, to) < 0)
2413 return -1;
2414
2415 if (rp.status) {
2416 errno = EIO;
2417 return -1;
2418 }
2419
2420 *rssi = rp.rssi;
2421 return 0;
2422 }
2423
hci_read_afh_map(int dd,uint16_t handle,uint8_t * mode,uint8_t * map,int to)2424 int hci_read_afh_map(int dd, uint16_t handle, uint8_t *mode, uint8_t *map, int to)
2425 {
2426 read_afh_map_rp rp;
2427 struct hci_request rq;
2428
2429 memset(&rq, 0, sizeof(rq));
2430 rq.ogf = OGF_STATUS_PARAM;
2431 rq.ocf = OCF_READ_AFH_MAP;
2432 rq.cparam = &handle;
2433 rq.clen = 2;
2434 rq.rparam = &rp;
2435 rq.rlen = READ_AFH_MAP_RP_SIZE;
2436
2437 if (hci_send_req(dd, &rq, to) < 0)
2438 return -1;
2439
2440 if (rp.status) {
2441 errno = EIO;
2442 return -1;
2443 }
2444
2445 *mode = rp.mode;
2446 memcpy(map, rp.map, 10);
2447 return 0;
2448 }
2449
hci_read_clock(int dd,uint16_t handle,uint8_t which,uint32_t * clock,uint16_t * accuracy,int to)2450 int hci_read_clock(int dd, uint16_t handle, uint8_t which, uint32_t *clock, uint16_t *accuracy, int to)
2451 {
2452 read_clock_cp cp;
2453 read_clock_rp rp;
2454 struct hci_request rq;
2455
2456 memset(&cp, 0, sizeof(cp));
2457 cp.handle = handle;
2458 cp.which_clock = which;
2459
2460 memset(&rq, 0, sizeof(rq));
2461 rq.ogf = OGF_STATUS_PARAM;
2462 rq.ocf = OCF_READ_CLOCK;
2463 rq.cparam = &cp;
2464 rq.clen = READ_CLOCK_CP_SIZE;
2465 rq.rparam = &rp;
2466 rq.rlen = READ_CLOCK_RP_SIZE;
2467
2468 if (hci_send_req(dd, &rq, to) < 0)
2469 return -1;
2470
2471 if (rp.status) {
2472 errno = EIO;
2473 return -1;
2474 }
2475
2476 *clock = rp.clock;
2477 *accuracy = rp.accuracy;
2478 return 0;
2479 }
2480
hci_local_name(int dd,int len,char * name,int to)2481 int hci_local_name(int dd, int len, char *name, int to)
2482 {
2483 return hci_read_local_name(dd, len, name, to);
2484 }
2485
hci_remote_name(int dd,const bdaddr_t * bdaddr,int len,char * name,int to)2486 int hci_remote_name(int dd, const bdaddr_t *bdaddr, int len, char *name, int to)
2487 {
2488 return hci_read_remote_name(dd, bdaddr, len, name, to);
2489 }
2490