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