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