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