Upgrade bluez5_37 :Merge the code from private
[platform/upstream/bluez.git] / tools / hcitool.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 <ctype.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <getopt.h>
38 #include <sys/param.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
41 #include <signal.h>
42
43 #include "lib/bluetooth.h"
44 #include "lib/hci.h"
45 #include "lib/hci_lib.h"
46
47 #include "src/oui.h"
48
49 #ifndef MIN
50 #define MIN(a, b) ((a) < (b) ? (a) : (b))
51 #endif
52
53 /* Unofficial value, might still change */
54 #define LE_LINK         0x80
55
56 #define FLAGS_AD_TYPE 0x01
57 #define FLAGS_LIMITED_MODE_BIT 0x01
58 #define FLAGS_GENERAL_MODE_BIT 0x02
59
60 #define EIR_FLAGS                   0x01  /* flags */
61 #define EIR_UUID16_SOME             0x02  /* 16-bit UUID, more available */
62 #define EIR_UUID16_ALL              0x03  /* 16-bit UUID, all listed */
63 #define EIR_UUID32_SOME             0x04  /* 32-bit UUID, more available */
64 #define EIR_UUID32_ALL              0x05  /* 32-bit UUID, all listed */
65 #define EIR_UUID128_SOME            0x06  /* 128-bit UUID, more available */
66 #define EIR_UUID128_ALL             0x07  /* 128-bit UUID, all listed */
67 #define EIR_NAME_SHORT              0x08  /* shortened local name */
68 #define EIR_NAME_COMPLETE           0x09  /* complete local name */
69 #define EIR_TX_POWER                0x0A  /* transmit power level */
70 #define EIR_DEVICE_ID               0x10  /* device ID */
71
72 #define for_each_opt(opt, long, short) while ((opt=getopt_long(argc, argv, short ? short:"+", long, NULL)) != -1)
73
74 static volatile int signal_received = 0;
75
76 static void usage(void);
77
78 static int str2buf(const char *str, uint8_t *buf, size_t blen)
79 {
80         int i, dlen;
81
82         if (str == NULL)
83                 return -EINVAL;
84
85         memset(buf, 0, blen);
86
87         dlen = MIN((strlen(str) / 2), blen);
88
89         for (i = 0; i < dlen; i++)
90                 sscanf(str + (i * 2), "%02hhX", &buf[i]);
91
92         return 0;
93 }
94
95 static int dev_info(int s, int dev_id, long arg)
96 {
97         struct hci_dev_info di = { .dev_id = dev_id };
98         char addr[18];
99
100         if (ioctl(s, HCIGETDEVINFO, (void *) &di))
101                 return 0;
102
103         ba2str(&di.bdaddr, addr);
104         printf("\t%s\t%s\n", di.name, addr);
105         return 0;
106 }
107
108 static void helper_arg(int min_num_arg, int max_num_arg, int *argc,
109                         char ***argv, const char *usage)
110 {
111         *argc -= optind;
112         /* too many arguments, but when "max_num_arg < min_num_arg" then no
113                  limiting (prefer "max_num_arg=-1" to gen infinity)
114         */
115         if ( (*argc > max_num_arg) && (max_num_arg >= min_num_arg ) ) {
116                 fprintf(stderr, "%s: too many arguments (maximal: %i)\n",
117                                 *argv[0], max_num_arg);
118                 printf("%s", usage);
119                 exit(1);
120         }
121
122         /* print usage */
123         if (*argc < min_num_arg) {
124                 fprintf(stderr, "%s: too few arguments (minimal: %i)\n",
125                                 *argv[0], min_num_arg);
126                 printf("%s", usage);
127                 exit(0);
128         }
129
130         *argv += optind;
131 }
132
133 static char *type2str(uint8_t type)
134 {
135         switch (type) {
136         case SCO_LINK:
137                 return "SCO";
138         case ACL_LINK:
139                 return "ACL";
140         case ESCO_LINK:
141                 return "eSCO";
142         case LE_LINK:
143                 return "LE";
144         default:
145                 return "Unknown";
146         }
147 }
148
149 static int conn_list(int s, int dev_id, long arg)
150 {
151         struct hci_conn_list_req *cl;
152         struct hci_conn_info *ci;
153         int id = arg;
154         int i;
155
156         if (id != -1 && dev_id != id)
157                 return 0;
158
159         if (!(cl = malloc(10 * sizeof(*ci) + sizeof(*cl)))) {
160                 perror("Can't allocate memory");
161                 exit(1);
162         }
163         cl->dev_id = dev_id;
164         cl->conn_num = 10;
165         ci = cl->conn_info;
166
167         if (ioctl(s, HCIGETCONNLIST, (void *) cl)) {
168                 perror("Can't get connection list");
169                 exit(1);
170         }
171
172         for (i = 0; i < cl->conn_num; i++, ci++) {
173                 char addr[18];
174                 char *str;
175                 ba2str(&ci->bdaddr, addr);
176                 str = hci_lmtostr(ci->link_mode);
177                 printf("\t%s %s %s handle %d state %d lm %s\n",
178                         ci->out ? "<" : ">", type2str(ci->type),
179                         addr, ci->handle, ci->state, str);
180                 bt_free(str);
181         }
182
183         free(cl);
184         return 0;
185 }
186
187 static int find_conn(int s, int dev_id, long arg)
188 {
189         struct hci_conn_list_req *cl;
190         struct hci_conn_info *ci;
191         int i;
192
193         if (!(cl = malloc(10 * sizeof(*ci) + sizeof(*cl)))) {
194                 perror("Can't allocate memory");
195                 exit(1);
196         }
197         cl->dev_id = dev_id;
198         cl->conn_num = 10;
199         ci = cl->conn_info;
200
201         if (ioctl(s, HCIGETCONNLIST, (void *) cl)) {
202                 perror("Can't get connection list");
203                 exit(1);
204         }
205
206         for (i = 0; i < cl->conn_num; i++, ci++)
207                 if (!bacmp((bdaddr_t *) arg, &ci->bdaddr)) {
208                         free(cl);
209                         return 1;
210                 }
211
212         free(cl);
213         return 0;
214 }
215
216 static void hex_dump(char *pref, int width, unsigned char *buf, int len)
217 {
218         register int i,n;
219
220         for (i = 0, n = 1; i < len; i++, n++) {
221                 if (n == 1)
222                         printf("%s", pref);
223                 printf("%2.2X ", buf[i]);
224                 if (n == width) {
225                         printf("\n");
226                         n = 0;
227                 }
228         }
229         if (i && n!=1)
230                 printf("\n");
231 }
232
233 static char *get_minor_device_name(int major, int minor)
234 {
235         switch (major) {
236         case 0: /* misc */
237                 return "";
238         case 1: /* computer */
239                 switch (minor) {
240                 case 0:
241                         return "Uncategorized";
242                 case 1:
243                         return "Desktop workstation";
244                 case 2:
245                         return "Server";
246                 case 3:
247                         return "Laptop";
248                 case 4:
249                         return "Handheld";
250                 case 5:
251                         return "Palm";
252                 case 6:
253                         return "Wearable";
254                 }
255                 break;
256         case 2: /* phone */
257                 switch (minor) {
258                 case 0:
259                         return "Uncategorized";
260                 case 1:
261                         return "Cellular";
262                 case 2:
263                         return "Cordless";
264                 case 3:
265                         return "Smart phone";
266                 case 4:
267                         return "Wired modem or voice gateway";
268                 case 5:
269                         return "Common ISDN Access";
270                 case 6:
271                         return "Sim Card Reader";
272                 }
273                 break;
274         case 3: /* lan access */
275                 if (minor == 0)
276                         return "Uncategorized";
277                 switch (minor / 8) {
278                 case 0:
279                         return "Fully available";
280                 case 1:
281                         return "1-17% utilized";
282                 case 2:
283                         return "17-33% utilized";
284                 case 3:
285                         return "33-50% utilized";
286                 case 4:
287                         return "50-67% utilized";
288                 case 5:
289                         return "67-83% utilized";
290                 case 6:
291                         return "83-99% utilized";
292                 case 7:
293                         return "No service available";
294                 }
295                 break;
296         case 4: /* audio/video */
297                 switch (minor) {
298                 case 0:
299                         return "Uncategorized";
300                 case 1:
301                         return "Device conforms to the Headset profile";
302                 case 2:
303                         return "Hands-free";
304                         /* 3 is reserved */
305                 case 4:
306                         return "Microphone";
307                 case 5:
308                         return "Loudspeaker";
309                 case 6:
310                         return "Headphones";
311                 case 7:
312                         return "Portable Audio";
313                 case 8:
314                         return "Car Audio";
315                 case 9:
316                         return "Set-top box";
317                 case 10:
318                         return "HiFi Audio Device";
319                 case 11:
320                         return "VCR";
321                 case 12:
322                         return "Video Camera";
323                 case 13:
324                         return "Camcorder";
325                 case 14:
326                         return "Video Monitor";
327                 case 15:
328                         return "Video Display and Loudspeaker";
329                 case 16:
330                         return "Video Conferencing";
331                         /* 17 is reserved */
332                 case 18:
333                         return "Gaming/Toy";
334                 }
335                 break;
336         case 5: /* peripheral */ {
337                 static char cls_str[48]; cls_str[0] = 0;
338
339                 switch (minor & 48) {
340                 case 16:
341                         strncpy(cls_str, "Keyboard", sizeof(cls_str));
342                         break;
343                 case 32:
344                         strncpy(cls_str, "Pointing device", sizeof(cls_str));
345                         break;
346                 case 48:
347                         strncpy(cls_str, "Combo keyboard/pointing device", sizeof(cls_str));
348                         break;
349                 }
350                 if ((minor & 15) && (strlen(cls_str) > 0))
351                         strcat(cls_str, "/");
352
353                 switch (minor & 15) {
354                 case 0:
355                         break;
356                 case 1:
357                         strncat(cls_str, "Joystick",
358                                         sizeof(cls_str) - strlen(cls_str) - 1);
359                         break;
360                 case 2:
361                         strncat(cls_str, "Gamepad",
362                                         sizeof(cls_str) - strlen(cls_str) - 1);
363                         break;
364                 case 3:
365                         strncat(cls_str, "Remote control",
366                                         sizeof(cls_str) - strlen(cls_str) - 1);
367                         break;
368                 case 4:
369                         strncat(cls_str, "Sensing device",
370                                         sizeof(cls_str) - strlen(cls_str) - 1);
371                         break;
372                 case 5:
373                         strncat(cls_str, "Digitizer tablet",
374                                         sizeof(cls_str) - strlen(cls_str) - 1);
375                         break;
376                 case 6:
377                         strncat(cls_str, "Card reader",
378                                         sizeof(cls_str) - strlen(cls_str) - 1);
379                         break;
380                 default:
381                         strncat(cls_str, "(reserved)",
382                                         sizeof(cls_str) - strlen(cls_str) - 1);
383                         break;
384                 }
385                 if (strlen(cls_str) > 0)
386                         return cls_str;
387                 break;
388         }
389         case 6: /* imaging */
390                 if (minor & 4)
391                         return "Display";
392                 if (minor & 8)
393                         return "Camera";
394                 if (minor & 16)
395                         return "Scanner";
396                 if (minor & 32)
397                         return "Printer";
398                 break;
399         case 7: /* wearable */
400                 switch (minor) {
401                 case 1:
402                         return "Wrist Watch";
403                 case 2:
404                         return "Pager";
405                 case 3:
406                         return "Jacket";
407                 case 4:
408                         return "Helmet";
409                 case 5:
410                         return "Glasses";
411                 }
412                 break;
413         case 8: /* toy */
414                 switch (minor) {
415                 case 1:
416                         return "Robot";
417                 case 2:
418                         return "Vehicle";
419                 case 3:
420                         return "Doll / Action Figure";
421                 case 4:
422                         return "Controller";
423                 case 5:
424                         return "Game";
425                 }
426                 break;
427         case 63:        /* uncategorised */
428                 return "";
429         }
430         return "Unknown (reserved) minor device class";
431 }
432
433 static char *major_classes[] = {
434         "Miscellaneous", "Computer", "Phone", "LAN Access",
435         "Audio/Video", "Peripheral", "Imaging", "Uncategorized"
436 };
437
438 /* Display local devices */
439
440 static struct option dev_options[] = {
441         { "help",       0, 0, 'h' },
442         {0, 0, 0, 0 }
443 };
444
445 static const char *dev_help =
446         "Usage:\n"
447         "\tdev\n";
448
449 static void cmd_dev(int dev_id, int argc, char **argv)
450 {
451         int opt;
452
453         for_each_opt(opt, dev_options, NULL) {
454                 switch (opt) {
455                 default:
456                         printf("%s", dev_help);
457                         return;
458                 }
459         }
460         helper_arg(0, 0, &argc, &argv, dev_help);
461
462         printf("Devices:\n");
463
464         hci_for_each_dev(HCI_UP, dev_info, 0);
465 }
466
467 /* Inquiry */
468
469 static struct option inq_options[] = {
470         { "help",       0, 0, 'h' },
471         { "length",     1, 0, 'l' },
472         { "numrsp",     1, 0, 'n' },
473         { "iac",        1, 0, 'i' },
474         { "flush",      0, 0, 'f' },
475         { 0, 0, 0, 0 }
476 };
477
478 static const char *inq_help =
479         "Usage:\n"
480         "\tinq [--length=N] maximum inquiry duration in 1.28 s units\n"
481         "\t    [--numrsp=N] specify maximum number of inquiry responses\n"
482         "\t    [--iac=lap]  specify the inquiry access code\n"
483         "\t    [--flush]    flush the inquiry cache\n";
484
485 static void cmd_inq(int dev_id, int argc, char **argv)
486 {
487         inquiry_info *info = NULL;
488         uint8_t lap[3] = { 0x33, 0x8b, 0x9e };
489         int num_rsp, length, flags;
490         char addr[18];
491         int i, l, opt;
492
493         length  = 8;    /* ~10 seconds */
494         num_rsp = 0;
495         flags   = 0;
496
497         for_each_opt(opt, inq_options, NULL) {
498                 switch (opt) {
499                 case 'l':
500                         length = atoi(optarg);
501                         break;
502
503                 case 'n':
504                         num_rsp = atoi(optarg);
505                         break;
506
507                 case 'i':
508                         l = strtoul(optarg, 0, 16);
509                         if (!strcasecmp(optarg, "giac")) {
510                                 l = 0x9e8b33;
511                         } else if (!strcasecmp(optarg, "liac")) {
512                                 l = 0x9e8b00;
513                         } if (l < 0x9e8b00 || l > 0x9e8b3f) {
514                                 printf("Invalid access code 0x%x\n", l);
515                                 exit(1);
516                         }
517                         lap[0] = (l & 0xff);
518                         lap[1] = (l >> 8) & 0xff;
519                         lap[2] = (l >> 16) & 0xff;
520                         break;
521
522                 case 'f':
523                         flags |= IREQ_CACHE_FLUSH;
524                         break;
525
526                 default:
527                         printf("%s", inq_help);
528                         return;
529                 }
530         }
531         helper_arg(0, 0, &argc, &argv, inq_help);
532
533         printf("Inquiring ...\n");
534
535         num_rsp = hci_inquiry(dev_id, length, num_rsp, lap, &info, flags);
536         if (num_rsp < 0) {
537                 perror("Inquiry failed.");
538                 exit(1);
539         }
540
541         for (i = 0; i < num_rsp; i++) {
542                 ba2str(&(info+i)->bdaddr, addr);
543                 printf("\t%s\tclock offset: 0x%4.4x\tclass: 0x%2.2x%2.2x%2.2x\n",
544                         addr, btohs((info+i)->clock_offset),
545                         (info+i)->dev_class[2],
546                         (info+i)->dev_class[1],
547                         (info+i)->dev_class[0]);
548         }
549
550         bt_free(info);
551 }
552
553 /* Device scanning */
554
555 static struct option scan_options[] = {
556         { "help",       0, 0, 'h' },
557         { "length",     1, 0, 'l' },
558         { "numrsp",     1, 0, 'n' },
559         { "iac",        1, 0, 'i' },
560         { "flush",      0, 0, 'f' },
561         { "class",      0, 0, 'C' },
562         { "info",       0, 0, 'I' },
563         { "oui",        0, 0, 'O' },
564         { "all",        0, 0, 'A' },
565         { "ext",        0, 0, 'A' },
566         { 0, 0, 0, 0 }
567 };
568
569 static const char *scan_help =
570         "Usage:\n"
571         "\tscan [--length=N] [--numrsp=N] [--iac=lap] [--flush] [--class] [--info] [--oui] [--refresh]\n";
572
573 static void cmd_scan(int dev_id, int argc, char **argv)
574 {
575         inquiry_info *info = NULL;
576         uint8_t lap[3] = { 0x33, 0x8b, 0x9e };
577         int num_rsp, length, flags;
578         uint8_t cls[3], features[8];
579         char addr[18], name[249], *comp;
580         struct hci_version version;
581         struct hci_dev_info di;
582         struct hci_conn_info_req *cr;
583         int extcls = 0, extinf = 0, extoui = 0;
584         int i, n, l, opt, dd, cc;
585
586         length  = 8;    /* ~10 seconds */
587         num_rsp = 0;
588         flags   = 0;
589
590         for_each_opt(opt, scan_options, NULL) {
591                 switch (opt) {
592                 case 'l':
593                         length = atoi(optarg);
594                         break;
595
596                 case 'n':
597                         num_rsp = atoi(optarg);
598                         break;
599
600                 case 'i':
601                         l = strtoul(optarg, 0, 16);
602                         if (!strcasecmp(optarg, "giac")) {
603                                 l = 0x9e8b33;
604                         } else if (!strcasecmp(optarg, "liac")) {
605                                 l = 0x9e8b00;
606                         } else if (l < 0x9e8b00 || l > 0x9e8b3f) {
607                                 printf("Invalid access code 0x%x\n", l);
608                                 exit(1);
609                         }
610                         lap[0] = (l & 0xff);
611                         lap[1] = (l >> 8) & 0xff;
612                         lap[2] = (l >> 16) & 0xff;
613                         break;
614
615                 case 'f':
616                         flags |= IREQ_CACHE_FLUSH;
617                         break;
618
619                 case 'C':
620                         extcls = 1;
621                         break;
622
623                 case 'I':
624                         extinf = 1;
625                         break;
626
627                 case 'O':
628                         extoui = 1;
629                         break;
630
631                 case 'A':
632                         extcls = 1;
633                         extinf = 1;
634                         extoui = 1;
635                         break;
636
637                 default:
638                         printf("%s", scan_help);
639                         return;
640                 }
641         }
642         helper_arg(0, 0, &argc, &argv, scan_help);
643
644         if (dev_id < 0) {
645                 dev_id = hci_get_route(NULL);
646                 if (dev_id < 0) {
647                         perror("Device is not available");
648                         exit(1);
649                 }
650         }
651
652         if (hci_devinfo(dev_id, &di) < 0) {
653                 perror("Can't get device info");
654                 exit(1);
655         }
656
657         printf("Scanning ...\n");
658         num_rsp = hci_inquiry(dev_id, length, num_rsp, lap, &info, flags);
659         if (num_rsp < 0) {
660                 perror("Inquiry failed");
661                 exit(1);
662         }
663
664         dd = hci_open_dev(dev_id);
665         if (dd < 0) {
666                 perror("HCI device open failed");
667                 free(info);
668                 exit(1);
669         }
670
671         if (extcls || extinf || extoui)
672                 printf("\n");
673
674         for (i = 0; i < num_rsp; i++) {
675                 uint16_t handle = 0;
676
677                 if (!extcls && !extinf && !extoui) {
678                         ba2str(&(info+i)->bdaddr, addr);
679
680                         if (hci_read_remote_name_with_clock_offset(dd,
681                                         &(info+i)->bdaddr,
682                                         (info+i)->pscan_rep_mode,
683                                         (info+i)->clock_offset | 0x8000,
684                                         sizeof(name), name, 100000) < 0)
685                                 strcpy(name, "n/a");
686
687                         for (n = 0; n < 248 && name[n]; n++) {
688                                 if ((unsigned char) name[i] < 32 || name[i] == 127)
689                                         name[i] = '.';
690                         }
691
692                         name[248] = '\0';
693
694                         printf("\t%s\t%s\n", addr, name);
695                         continue;
696                 }
697
698                 ba2str(&(info+i)->bdaddr, addr);
699                 printf("BD Address:\t%s [mode %d, clkoffset 0x%4.4x]\n", addr,
700                         (info+i)->pscan_rep_mode, btohs((info+i)->clock_offset));
701
702                 if (extoui) {
703                         comp = batocomp(&(info+i)->bdaddr);
704                         if (comp) {
705                                 char oui[9];
706                                 ba2oui(&(info+i)->bdaddr, oui);
707                                 printf("OUI company:\t%s (%s)\n", comp, oui);
708                                 free(comp);
709                         }
710                 }
711
712                 cc = 0;
713
714                 if (extinf) {
715                         cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
716                         if (cr) {
717                                 bacpy(&cr->bdaddr, &(info+i)->bdaddr);
718                                 cr->type = ACL_LINK;
719                                 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
720                                         handle = 0;
721                                         cc = 1;
722                                 } else {
723                                         handle = htobs(cr->conn_info->handle);
724                                         cc = 0;
725                                 }
726                                 free(cr);
727                         }
728
729                         if (cc) {
730                                 if (hci_create_connection(dd, &(info+i)->bdaddr,
731                                                 htobs(di.pkt_type & ACL_PTYPE_MASK),
732                                                 (info+i)->clock_offset | 0x8000,
733                                                 0x01, &handle, 25000) < 0) {
734                                         handle = 0;
735                                         cc = 0;
736                                 }
737                         }
738                 }
739
740                 if (hci_read_remote_name_with_clock_offset(dd,
741                                         &(info+i)->bdaddr,
742                                         (info+i)->pscan_rep_mode,
743                                         (info+i)->clock_offset | 0x8000,
744                                         sizeof(name), name, 100000) < 0) {
745                 } else {
746                         for (n = 0; n < 248 && name[n]; n++) {
747                                 if ((unsigned char) name[i] < 32 || name[i] == 127)
748                                         name[i] = '.';
749                         }
750
751                         name[248] = '\0';
752                 }
753
754                 if (strlen(name) > 0)
755                         printf("Device name:\t%s\n", name);
756
757                 if (extcls) {
758                         memcpy(cls, (info+i)->dev_class, 3);
759                         printf("Device class:\t");
760                         if ((cls[1] & 0x1f) > sizeof(major_classes) / sizeof(char *))
761                                 printf("Invalid");
762                         else
763                                 printf("%s, %s", major_classes[cls[1] & 0x1f],
764                                         get_minor_device_name(cls[1] & 0x1f, cls[0] >> 2));
765                         printf(" (0x%2.2x%2.2x%2.2x)\n", cls[2], cls[1], cls[0]);
766                 }
767
768                 if (extinf && handle > 0) {
769                         if (hci_read_remote_version(dd, handle, &version, 20000) == 0) {
770                                 char *ver = lmp_vertostr(version.lmp_ver);
771                                 printf("Manufacturer:\t%s (%d)\n",
772                                         bt_compidtostr(version.manufacturer),
773                                         version.manufacturer);
774                                 printf("LMP version:\t%s (0x%x) [subver 0x%x]\n",
775                                         ver ? ver : "n/a",
776                                         version.lmp_ver, version.lmp_subver);
777                                 if (ver)
778                                         bt_free(ver);
779                         }
780
781                         if (hci_read_remote_features(dd, handle, features, 20000) == 0) {
782                                 char *tmp = lmp_featurestostr(features, "\t\t", 63);
783                                 printf("LMP features:\t0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x"
784                                         " 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n",
785                                         features[0], features[1],
786                                         features[2], features[3],
787                                         features[4], features[5],
788                                         features[6], features[7]);
789                                 printf("%s\n", tmp);
790                                 bt_free(tmp);
791                         }
792
793                         if (cc) {
794                                 usleep(10000);
795                                 hci_disconnect(dd, handle, HCI_OE_USER_ENDED_CONNECTION, 10000);
796                         }
797                 }
798
799                 printf("\n");
800         }
801
802         bt_free(info);
803
804         hci_close_dev(dd);
805 }
806
807 /* Remote name */
808
809 static struct option name_options[] = {
810         { "help",       0, 0, 'h' },
811         { 0, 0, 0, 0 }
812 };
813
814 static const char *name_help =
815         "Usage:\n"
816         "\tname <bdaddr>\n";
817
818 static void cmd_name(int dev_id, int argc, char **argv)
819 {
820         bdaddr_t bdaddr;
821         char name[248];
822         int opt, dd;
823
824         for_each_opt(opt, name_options, NULL) {
825                 switch (opt) {
826                 default:
827                         printf("%s", name_help);
828                         return;
829                 }
830         }
831         helper_arg(1, 1, &argc, &argv, name_help);
832
833         str2ba(argv[0], &bdaddr);
834
835         if (dev_id < 0) {
836                 dev_id = hci_get_route(&bdaddr);
837                 if (dev_id < 0) {
838                         fprintf(stderr, "Device is not available.\n");
839                         exit(1);
840                 }
841         }
842
843         dd = hci_open_dev(dev_id);
844         if (dd < 0) {
845                 perror("HCI device open failed");
846                 exit(1);
847         }
848
849         if (hci_read_remote_name(dd, &bdaddr, sizeof(name), name, 25000) == 0)
850                 printf("%s\n", name);
851
852         hci_close_dev(dd);
853 }
854
855 /* Info about remote device */
856
857 static struct option info_options[] = {
858         { "help",       0, 0, 'h' },
859         { 0, 0, 0, 0 }
860 };
861
862 static const char *info_help =
863         "Usage:\n"
864         "\tinfo <bdaddr>\n";
865
866 static void cmd_info(int dev_id, int argc, char **argv)
867 {
868         bdaddr_t bdaddr;
869         uint16_t handle;
870         uint8_t features[8], max_page = 0;
871         char name[249], *comp, *tmp;
872         struct hci_version version;
873         struct hci_dev_info di;
874         struct hci_conn_info_req *cr;
875         int i, opt, dd, cc = 0;
876
877         for_each_opt(opt, info_options, NULL) {
878                 switch (opt) {
879                 default:
880                         printf("%s", info_help);
881                         return;
882                 }
883         }
884         helper_arg(1, 1, &argc, &argv, info_help);
885
886         str2ba(argv[0], &bdaddr);
887
888         if (dev_id < 0)
889                 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
890
891         if (dev_id < 0)
892                 dev_id = hci_get_route(&bdaddr);
893
894         if (dev_id < 0) {
895                 fprintf(stderr, "Device is not available or not connected.\n");
896                 exit(1);
897         }
898
899         if (hci_devinfo(dev_id, &di) < 0) {
900                 perror("Can't get device info");
901                 exit(1);
902         }
903
904         printf("Requesting information ...\n");
905
906         dd = hci_open_dev(dev_id);
907         if (dd < 0) {
908                 perror("HCI device open failed");
909                 exit(1);
910         }
911
912         cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
913         if (!cr) {
914                 perror("Can't get connection info");
915                 close(dd);
916                 exit(1);
917         }
918
919         bacpy(&cr->bdaddr, &bdaddr);
920         cr->type = ACL_LINK;
921         if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
922                 if (hci_create_connection(dd, &bdaddr,
923                                         htobs(di.pkt_type & ACL_PTYPE_MASK),
924                                         0, 0x01, &handle, 25000) < 0) {
925                         perror("Can't create connection");
926                         free(cr);
927                         close(dd);
928                         exit(1);
929                 }
930                 sleep(1);
931                 cc = 1;
932         } else
933                 handle = htobs(cr->conn_info->handle);
934
935         free(cr);
936
937         printf("\tBD Address:  %s\n", argv[0]);
938
939         comp = batocomp(&bdaddr);
940         if (comp) {
941                 char oui[9];
942                 ba2oui(&bdaddr, oui);
943                 printf("\tOUI Company: %s (%s)\n", comp, oui);
944                 free(comp);
945         }
946
947         if (hci_read_remote_name(dd, &bdaddr, sizeof(name), name, 25000) == 0)
948                 printf("\tDevice Name: %s\n", name);
949
950         if (hci_read_remote_version(dd, handle, &version, 20000) == 0) {
951                 char *ver = lmp_vertostr(version.lmp_ver);
952                 printf("\tLMP Version: %s (0x%x) LMP Subversion: 0x%x\n"
953                         "\tManufacturer: %s (%d)\n",
954                         ver ? ver : "n/a",
955                         version.lmp_ver,
956                         version.lmp_subver,
957                         bt_compidtostr(version.manufacturer),
958                         version.manufacturer);
959                 if (ver)
960                         bt_free(ver);
961         }
962
963         memset(features, 0, sizeof(features));
964         hci_read_remote_features(dd, handle, features, 20000);
965
966         if ((di.features[7] & LMP_EXT_FEAT) && (features[7] & LMP_EXT_FEAT))
967                 hci_read_remote_ext_features(dd, handle, 0, &max_page,
968                                                         features, 20000);
969
970         if (max_page < 1 && (features[6] & LMP_SIMPLE_PAIR))
971                 max_page = 1;
972
973         printf("\tFeatures%s: 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x "
974                                 "0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n",
975                 (max_page > 0) ? " page 0" : "",
976                 features[0], features[1], features[2], features[3],
977                 features[4], features[5], features[6], features[7]);
978
979         tmp = lmp_featurestostr(features, "\t\t", 63);
980         printf("%s\n", tmp);
981         bt_free(tmp);
982
983         for (i = 1; i <= max_page; i++) {
984                 if (hci_read_remote_ext_features(dd, handle, i, NULL,
985                                                         features, 20000) < 0)
986                         continue;
987
988                 printf("\tFeatures page %d: 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x "
989                                         "0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n", i,
990                         features[0], features[1], features[2], features[3],
991                         features[4], features[5], features[6], features[7]);
992         }
993
994         if (cc) {
995                 usleep(10000);
996                 hci_disconnect(dd, handle, HCI_OE_USER_ENDED_CONNECTION, 10000);
997         }
998
999         hci_close_dev(dd);
1000 }
1001
1002 /* Start periodic inquiry */
1003
1004 static struct option spinq_options[] = {
1005         { "help",       0, 0, 'h' },
1006         { 0, 0, 0, 0 }
1007 };
1008
1009 static const char *spinq_help =
1010         "Usage:\n"
1011         "\tspinq\n";
1012
1013 static void cmd_spinq(int dev_id, int argc, char **argv)
1014 {
1015         uint8_t lap[3] = { 0x33, 0x8b, 0x9e };
1016         struct hci_request rq;
1017         periodic_inquiry_cp cp;
1018         int opt, dd;
1019
1020         for_each_opt(opt, spinq_options, NULL) {
1021                 switch (opt) {
1022                 default:
1023                         printf("%s", spinq_help);
1024                         return;
1025                 }
1026         }
1027         helper_arg(0, 0, &argc, &argv, spinq_help);
1028
1029         if (dev_id < 0)
1030                 dev_id = hci_get_route(NULL);
1031
1032         dd = hci_open_dev(dev_id);
1033         if (dd < 0) {
1034                 perror("Device open failed");
1035                 exit(EXIT_FAILURE);
1036         }
1037
1038         memset(&cp, 0, sizeof(cp));
1039         memcpy(cp.lap, lap, 3);
1040         cp.max_period = htobs(16);
1041         cp.min_period = htobs(10);
1042         cp.length     = 8;
1043         cp.num_rsp    = 0;
1044
1045         memset(&rq, 0, sizeof(rq));
1046         rq.ogf    = OGF_LINK_CTL;
1047         rq.ocf    = OCF_PERIODIC_INQUIRY;
1048         rq.cparam = &cp;
1049         rq.clen   = PERIODIC_INQUIRY_CP_SIZE;
1050
1051         if (hci_send_req(dd, &rq, 100) < 0) {
1052                 perror("Periodic inquiry failed");
1053                 exit(EXIT_FAILURE);
1054         }
1055
1056         hci_close_dev(dd);
1057 }
1058
1059 /* Exit periodic inquiry */
1060
1061 static struct option epinq_options[] = {
1062         { "help",       0, 0, 'h' },
1063         { 0, 0, 0, 0 }
1064 };
1065
1066 static const char *epinq_help =
1067         "Usage:\n"
1068         "\tepinq\n";
1069
1070 static void cmd_epinq(int dev_id, int argc, char **argv)
1071 {
1072         int opt, dd;
1073
1074         for_each_opt(opt, epinq_options, NULL) {
1075                 switch (opt) {
1076                 default:
1077                         printf("%s", epinq_help);
1078                         return;
1079                 }
1080         }
1081         helper_arg(0, 0, &argc, &argv, epinq_help);
1082
1083         if (dev_id < 0)
1084                 dev_id = hci_get_route(NULL);
1085
1086         dd = hci_open_dev(dev_id);
1087         if (dd < 0) {
1088                 perror("Device open failed");
1089                 exit(EXIT_FAILURE);
1090         }
1091
1092         if (hci_send_cmd(dd, OGF_LINK_CTL,
1093                                 OCF_EXIT_PERIODIC_INQUIRY, 0, NULL) < 0) {
1094                 perror("Exit periodic inquiry failed");
1095                 exit(EXIT_FAILURE);
1096         }
1097
1098         hci_close_dev(dd);
1099 }
1100
1101 #ifdef __TIZEN_PATCH__
1102 /* Send arbitrary ACL data */
1103 static struct option data_options[] = {
1104         { "help",       0, 0, 'h' },
1105         { 0, 0, 0, 0 }
1106 };
1107
1108 static const char *data_help =
1109         "Usage:\n"
1110         "\tcmd <handle> <data>\n"
1111         "Example:\n"
1112         "\tcmd 0x0064 0x41 0x42 0x43 0x44\n";
1113
1114 static void cmd_data(int dev_id, int argc, char **argv)
1115 {
1116         unsigned char buf[HCI_MAX_ACL_SIZE], *ptr = buf;
1117         struct hci_filter flt;
1118         int i, opt, len, dd;
1119         uint16_t handle;
1120
1121         for_each_opt(opt, data_options, NULL) {
1122                 switch (opt) {
1123                 default:
1124                         printf("%s", data_help);
1125                         return;
1126                 }
1127         }
1128         helper_arg(2, -1, &argc, &argv, data_help);
1129
1130         if (dev_id < 0)
1131                 dev_id = hci_get_route(NULL);
1132
1133         handle = atoi(argv[0]);
1134
1135         for (i = 1, len = 0; i < argc && len < (int) sizeof(buf); i++, len++)
1136                 *ptr++ = (uint8_t) strtol(argv[i], NULL, 16);
1137
1138         dd = hci_open_dev(dev_id);
1139         if (dd < 0) {
1140                 perror("Device open failed");
1141                 exit(EXIT_FAILURE);
1142         }
1143
1144         /* Setup filter */
1145         hci_filter_clear(&flt);
1146         hci_filter_all_events(&flt);
1147         if (setsockopt(dd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
1148                 perror("HCI filter setup failed");
1149                 exit(EXIT_FAILURE);
1150         }
1151
1152         if (hci_send_data(dd, handle, len, buf) < 0) {
1153                 perror("Send failed");
1154                 exit(EXIT_FAILURE);
1155         }
1156
1157         hci_close_dev(dd);
1158 }
1159 #endif
1160
1161 /* Send arbitrary HCI commands */
1162
1163 static struct option cmd_options[] = {
1164         { "help",       0, 0, 'h' },
1165         { 0, 0, 0, 0 }
1166 };
1167
1168 static const char *cmd_help =
1169         "Usage:\n"
1170         "\tcmd <ogf> <ocf> [parameters]\n"
1171         "Example:\n"
1172         "\tcmd 0x03 0x0013 0x41 0x42 0x43 0x44\n";
1173
1174 static void cmd_cmd(int dev_id, int argc, char **argv)
1175 {
1176         unsigned char buf[HCI_MAX_EVENT_SIZE], *ptr = buf;
1177         struct hci_filter flt;
1178         hci_event_hdr *hdr;
1179         int i, opt, len, dd;
1180         uint16_t ocf;
1181         uint8_t ogf;
1182
1183         for_each_opt(opt, cmd_options, NULL) {
1184                 switch (opt) {
1185                 default:
1186                         printf("%s", cmd_help);
1187                         return;
1188                 }
1189         }
1190         helper_arg(2, -1, &argc, &argv, cmd_help);
1191
1192         if (dev_id < 0)
1193                 dev_id = hci_get_route(NULL);
1194
1195         errno = 0;
1196         ogf = strtol(argv[0], NULL, 16);
1197         ocf = strtol(argv[1], NULL, 16);
1198         if (errno == ERANGE || (ogf > 0x3f) || (ocf > 0x3ff)) {
1199                 printf("%s", cmd_help);
1200                 return;
1201         }
1202
1203         for (i = 2, len = 0; i < argc && len < (int) sizeof(buf); i++, len++)
1204                 *ptr++ = (uint8_t) strtol(argv[i], NULL, 16);
1205
1206         dd = hci_open_dev(dev_id);
1207         if (dd < 0) {
1208                 perror("Device open failed");
1209                 exit(EXIT_FAILURE);
1210         }
1211
1212         /* Setup filter */
1213         hci_filter_clear(&flt);
1214         hci_filter_set_ptype(HCI_EVENT_PKT, &flt);
1215         hci_filter_all_events(&flt);
1216         if (setsockopt(dd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
1217                 perror("HCI filter setup failed");
1218                 exit(EXIT_FAILURE);
1219         }
1220
1221         printf("< HCI Command: ogf 0x%02x, ocf 0x%04x, plen %d\n", ogf, ocf, len);
1222         hex_dump("  ", 20, buf, len); fflush(stdout);
1223
1224         if (hci_send_cmd(dd, ogf, ocf, len, buf) < 0) {
1225                 perror("Send failed");
1226                 exit(EXIT_FAILURE);
1227         }
1228
1229         len = read(dd, buf, sizeof(buf));
1230         if (len < 0) {
1231                 perror("Read failed");
1232                 exit(EXIT_FAILURE);
1233         }
1234
1235         hdr = (void *)(buf + 1);
1236         ptr = buf + (1 + HCI_EVENT_HDR_SIZE);
1237         len -= (1 + HCI_EVENT_HDR_SIZE);
1238
1239         printf("> HCI Event: 0x%02x plen %d\n", hdr->evt, hdr->plen);
1240         hex_dump("  ", 20, ptr, len); fflush(stdout);
1241
1242         hci_close_dev(dd);
1243 }
1244
1245 /* Display active connections */
1246
1247 static struct option con_options[] = {
1248         { "help",       0, 0, 'h' },
1249         { 0, 0, 0, 0 }
1250 };
1251
1252 static const char *con_help =
1253         "Usage:\n"
1254         "\tcon\n";
1255
1256 static void cmd_con(int dev_id, int argc, char **argv)
1257 {
1258         int opt;
1259
1260         for_each_opt(opt, con_options, NULL) {
1261                 switch (opt) {
1262                 default:
1263                         printf("%s", con_help);
1264                         return;
1265                 }
1266         }
1267         helper_arg(0, 0, &argc, &argv, con_help);
1268
1269         printf("Connections:\n");
1270
1271         hci_for_each_dev(HCI_UP, conn_list, dev_id);
1272 }
1273
1274 /* Create connection */
1275
1276 static struct option cc_options[] = {
1277         { "help",       0, 0, 'h' },
1278         { "role",       1, 0, 'r' },
1279         { "ptype",      1, 0, 'p' },
1280         { 0, 0, 0, 0 }
1281 };
1282
1283 static const char *cc_help =
1284         "Usage:\n"
1285         "\tcc [--role=m|s] [--ptype=pkt_types] <bdaddr>\n"
1286         "Example:\n"
1287         "\tcc --ptype=dm1,dh3,dh5 01:02:03:04:05:06\n"
1288         "\tcc --role=m 01:02:03:04:05:06\n";
1289
1290 static void cmd_cc(int dev_id, int argc, char **argv)
1291 {
1292         bdaddr_t bdaddr;
1293         uint16_t handle;
1294         uint8_t role;
1295         unsigned int ptype;
1296         int dd, opt;
1297
1298         role = 0x01;
1299         ptype = HCI_DM1 | HCI_DM3 | HCI_DM5 | HCI_DH1 | HCI_DH3 | HCI_DH5;
1300
1301         for_each_opt(opt, cc_options, NULL) {
1302                 switch (opt) {
1303                 case 'p':
1304                         hci_strtoptype(optarg, &ptype);
1305                         break;
1306
1307                 case 'r':
1308                         role = optarg[0] == 'm' ? 0 : 1;
1309                         break;
1310
1311                 default:
1312                         printf("%s", cc_help);
1313                         return;
1314                 }
1315         }
1316         helper_arg(1, 1, &argc, &argv, cc_help);
1317
1318         str2ba(argv[0], &bdaddr);
1319
1320         if (dev_id < 0) {
1321                 dev_id = hci_get_route(&bdaddr);
1322                 if (dev_id < 0) {
1323                         fprintf(stderr, "Device is not available.\n");
1324                         exit(1);
1325                 }
1326         }
1327
1328         dd = hci_open_dev(dev_id);
1329         if (dd < 0) {
1330                 perror("HCI device open failed");
1331                 exit(1);
1332         }
1333
1334         if (hci_create_connection(dd, &bdaddr, htobs(ptype),
1335                                 htobs(0x0000), role, &handle, 25000) < 0)
1336                 perror("Can't create connection");
1337
1338         hci_close_dev(dd);
1339 }
1340
1341 /* Close connection */
1342
1343 static struct option dc_options[] = {
1344         { "help",       0, 0, 'h' },
1345         { 0, 0, 0, 0 }
1346 };
1347
1348 static const char *dc_help =
1349         "Usage:\n"
1350         "\tdc <bdaddr> [reason]\n";
1351
1352 static void cmd_dc(int dev_id, int argc, char **argv)
1353 {
1354         struct hci_conn_info_req *cr;
1355         bdaddr_t bdaddr;
1356         uint8_t reason;
1357         int opt, dd;
1358
1359         for_each_opt(opt, dc_options, NULL) {
1360                 switch (opt) {
1361                 default:
1362                         printf("%s", dc_help);
1363                         return;
1364                 }
1365         }
1366         helper_arg(1, 2, &argc, &argv, dc_help);
1367
1368         str2ba(argv[0], &bdaddr);
1369         reason = (argc > 1) ? atoi(argv[1]) : HCI_OE_USER_ENDED_CONNECTION;
1370
1371         if (dev_id < 0) {
1372                 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1373                 if (dev_id < 0) {
1374                         fprintf(stderr, "Not connected.\n");
1375                         exit(1);
1376                 }
1377         }
1378
1379         dd = hci_open_dev(dev_id);
1380         if (dd < 0) {
1381                 perror("HCI device open failed");
1382                 exit(1);
1383         }
1384
1385         cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1386         if (!cr) {
1387                 perror("Can't allocate memory");
1388                 exit(1);
1389         }
1390
1391         bacpy(&cr->bdaddr, &bdaddr);
1392         cr->type = ACL_LINK;
1393         if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1394                 perror("Get connection info failed");
1395                 exit(1);
1396         }
1397
1398         if (hci_disconnect(dd, htobs(cr->conn_info->handle),
1399                                                 reason, 10000) < 0)
1400                 perror("Disconnect failed");
1401
1402         free(cr);
1403
1404         hci_close_dev(dd);
1405 }
1406
1407 /* Role switch */
1408
1409 static struct option sr_options[] = {
1410         { "help",       0, 0, 'h' },
1411         { 0, 0, 0, 0 }
1412 };
1413
1414 static const char *sr_help =
1415         "Usage:\n"
1416         "\tsr <bdaddr> <role>\n";
1417
1418 static void cmd_sr(int dev_id, int argc, char **argv)
1419 {
1420         bdaddr_t bdaddr;
1421         uint8_t role;
1422         int opt, dd;
1423
1424         for_each_opt(opt, sr_options, NULL) {
1425                 switch (opt) {
1426                 default:
1427                         printf("%s", sr_help);
1428                         return;
1429                 }
1430         }
1431         helper_arg(2, 2, &argc, &argv, sr_help);
1432
1433         str2ba(argv[0], &bdaddr);
1434         switch (argv[1][0]) {
1435         case 'm':
1436                 role = 0;
1437                 break;
1438         case 's':
1439                 role = 1;
1440                 break;
1441         default:
1442                 role = atoi(argv[1]);
1443                 break;
1444         }
1445
1446         if (dev_id < 0) {
1447                 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1448                 if (dev_id < 0) {
1449                         fprintf(stderr, "Not connected.\n");
1450                         exit(1);
1451                 }
1452         }
1453
1454         dd = hci_open_dev(dev_id);
1455         if (dd < 0) {
1456                 perror("HCI device open failed");
1457                 exit(1);
1458         }
1459
1460         if (hci_switch_role(dd, &bdaddr, role, 10000) < 0) {
1461                 perror("Switch role request failed");
1462                 exit(1);
1463         }
1464
1465         hci_close_dev(dd);
1466 }
1467
1468 /* Read RSSI */
1469
1470 static struct option rssi_options[] = {
1471         { "help",       0, 0, 'h' },
1472         { 0, 0, 0, 0 }
1473 };
1474
1475 static const char *rssi_help =
1476         "Usage:\n"
1477         "\trssi <bdaddr>\n";
1478
1479 static void cmd_rssi(int dev_id, int argc, char **argv)
1480 {
1481         struct hci_conn_info_req *cr;
1482         bdaddr_t bdaddr;
1483         int8_t rssi;
1484         int opt, dd;
1485
1486         for_each_opt(opt, rssi_options, NULL) {
1487                 switch (opt) {
1488                 default:
1489                         printf("%s", rssi_help);
1490                         return;
1491                 }
1492         }
1493         helper_arg(1, 1, &argc, &argv, rssi_help);
1494
1495         str2ba(argv[0], &bdaddr);
1496
1497         if (dev_id < 0) {
1498                 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1499                 if (dev_id < 0) {
1500                         fprintf(stderr, "Not connected.\n");
1501                         exit(1);
1502                 }
1503         }
1504
1505         dd = hci_open_dev(dev_id);
1506         if (dd < 0) {
1507                 perror("HCI device open failed");
1508                 exit(1);
1509         }
1510
1511         cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1512         if (!cr) {
1513                 perror("Can't allocate memory");
1514                 exit(1);
1515         }
1516
1517         bacpy(&cr->bdaddr, &bdaddr);
1518         cr->type = ACL_LINK;
1519         if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1520                 perror("Get connection info failed");
1521                 exit(1);
1522         }
1523
1524         if (hci_read_rssi(dd, htobs(cr->conn_info->handle), &rssi, 1000) < 0) {
1525                 perror("Read RSSI failed");
1526                 exit(1);
1527         }
1528
1529         printf("RSSI return value: %d\n", rssi);
1530
1531         free(cr);
1532
1533         hci_close_dev(dd);
1534 }
1535
1536 /* Get link quality */
1537
1538 static struct option lq_options[] = {
1539         { "help",       0, 0, 'h' },
1540         { 0, 0, 0, 0 }
1541 };
1542
1543 static const char *lq_help =
1544         "Usage:\n"
1545         "\tlq <bdaddr>\n";
1546
1547 static void cmd_lq(int dev_id, int argc, char **argv)
1548 {
1549         struct hci_conn_info_req *cr;
1550         bdaddr_t bdaddr;
1551         uint8_t lq;
1552         int opt, dd;
1553
1554         for_each_opt(opt, lq_options, NULL) {
1555                 switch (opt) {
1556                 default:
1557                         printf("%s", lq_help);
1558                         return;
1559                 }
1560         }
1561         helper_arg(1, 1, &argc, &argv, lq_help);
1562
1563         str2ba(argv[0], &bdaddr);
1564
1565         if (dev_id < 0) {
1566                 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1567                 if (dev_id < 0) {
1568                         fprintf(stderr, "Not connected.\n");
1569                         exit(1);
1570                 }
1571         }
1572
1573         dd = hci_open_dev(dev_id);
1574         if (dd < 0) {
1575                 perror("HCI device open failed");
1576                 exit(1);
1577         }
1578
1579         cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1580         if (!cr) {
1581                 perror("Can't allocate memory");
1582                 exit(1);
1583         }
1584
1585         bacpy(&cr->bdaddr, &bdaddr);
1586         cr->type = ACL_LINK;
1587         if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1588                 perror("Get connection info failed");
1589                 exit(1);
1590         }
1591
1592         if (hci_read_link_quality(dd, htobs(cr->conn_info->handle), &lq, 1000) < 0) {
1593                 perror("HCI read_link_quality request failed");
1594                 exit(1);
1595         }
1596
1597         printf("Link quality: %d\n", lq);
1598
1599         free(cr);
1600
1601         hci_close_dev(dd);
1602 }
1603
1604 /* Get transmit power level */
1605
1606 static struct option tpl_options[] = {
1607         { "help",       0, 0, 'h' },
1608         { 0, 0, 0, 0 }
1609 };
1610
1611 static const char *tpl_help =
1612         "Usage:\n"
1613         "\ttpl <bdaddr> [type]\n";
1614
1615 static void cmd_tpl(int dev_id, int argc, char **argv)
1616 {
1617         struct hci_conn_info_req *cr;
1618         bdaddr_t bdaddr;
1619         uint8_t type;
1620         int8_t level;
1621         int opt, dd;
1622
1623         for_each_opt(opt, tpl_options, NULL) {
1624                 switch (opt) {
1625                 default:
1626                         printf("%s", tpl_help);
1627                         return;
1628                 }
1629         }
1630         helper_arg(1, 2, &argc, &argv, tpl_help);
1631
1632         str2ba(argv[0], &bdaddr);
1633         type = (argc > 1) ? atoi(argv[1]) : 0;
1634
1635         if (dev_id < 0) {
1636                 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1637                 if (dev_id < 0) {
1638                         fprintf(stderr, "Not connected.\n");
1639                         exit(1);
1640                 }
1641         }
1642
1643         dd = hci_open_dev(dev_id);
1644         if (dd < 0) {
1645                 perror("HCI device open failed");
1646                 exit(1);
1647         }
1648
1649         cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1650         if (!cr) {
1651                 perror("Can't allocate memory");
1652                 exit(1);
1653         }
1654
1655         bacpy(&cr->bdaddr, &bdaddr);
1656         cr->type = ACL_LINK;
1657         if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1658                 perror("Get connection info failed");
1659                 exit(1);
1660         }
1661
1662         if (hci_read_transmit_power_level(dd, htobs(cr->conn_info->handle), type, &level, 1000) < 0) {
1663                 perror("HCI read transmit power level request failed");
1664                 exit(1);
1665         }
1666
1667         printf("%s transmit power level: %d\n",
1668                 (type == 0) ? "Current" : "Maximum", level);
1669
1670         free(cr);
1671
1672         hci_close_dev(dd);
1673 }
1674
1675 /* Get AFH channel map */
1676
1677 static struct option afh_options[] = {
1678         { "help",       0, 0, 'h' },
1679         { 0, 0, 0, 0 }
1680 };
1681
1682 static const char *afh_help =
1683         "Usage:\n"
1684         "\tafh <bdaddr>\n";
1685
1686 static void cmd_afh(int dev_id, int argc, char **argv)
1687 {
1688         struct hci_conn_info_req *cr;
1689         bdaddr_t bdaddr;
1690         uint16_t handle;
1691         uint8_t mode, map[10];
1692         int opt, dd;
1693
1694         for_each_opt(opt, afh_options, NULL) {
1695                 switch (opt) {
1696                 default:
1697                         printf("%s", afh_help);
1698                         return;
1699                 }
1700         }
1701         helper_arg(1, 1, &argc, &argv, afh_help);
1702
1703         str2ba(argv[0], &bdaddr);
1704
1705         if (dev_id < 0) {
1706                 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1707                 if (dev_id < 0) {
1708                         fprintf(stderr, "Not connected.\n");
1709                         exit(1);
1710                 }
1711         }
1712
1713         dd = hci_open_dev(dev_id);
1714         if (dd < 0) {
1715                 perror("HCI device open failed");
1716                 exit(1);
1717         }
1718
1719         cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1720         if (!cr) {
1721                 perror("Can't allocate memory");
1722                 exit(1);
1723         }
1724
1725         bacpy(&cr->bdaddr, &bdaddr);
1726         cr->type = ACL_LINK;
1727         if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1728                 perror("Get connection info failed");
1729                 exit(1);
1730         }
1731
1732         handle = htobs(cr->conn_info->handle);
1733
1734         if (hci_read_afh_map(dd, handle, &mode, map, 1000) < 0) {
1735                 perror("HCI read AFH map request failed");
1736                 exit(1);
1737         }
1738
1739         if (mode == 0x01) {
1740                 int i;
1741                 printf("AFH map: 0x");
1742                 for (i = 0; i < 10; i++)
1743                         printf("%02x", map[i]);
1744                 printf("\n");
1745         } else
1746                 printf("AFH disabled\n");
1747
1748         free(cr);
1749
1750         hci_close_dev(dd);
1751 }
1752
1753 /* Set connection packet type */
1754
1755 static struct option cpt_options[] = {
1756         { "help",       0, 0, 'h' },
1757         { 0, 0, 0, 0 }
1758 };
1759
1760 static const char *cpt_help =
1761         "Usage:\n"
1762         "\tcpt <bdaddr> <packet_types>\n";
1763
1764 static void cmd_cpt(int dev_id, int argc, char **argv)
1765 {
1766         struct hci_conn_info_req *cr;
1767         struct hci_request rq;
1768         set_conn_ptype_cp cp;
1769         evt_conn_ptype_changed rp;
1770         bdaddr_t bdaddr;
1771         unsigned int ptype;
1772         int dd, opt;
1773
1774         for_each_opt(opt, cpt_options, NULL) {
1775                 switch (opt) {
1776                 default:
1777                         printf("%s", cpt_help);
1778                         return;
1779                 }
1780         }
1781         helper_arg(2, 2, &argc, &argv, cpt_help);
1782
1783         str2ba(argv[0], &bdaddr);
1784         hci_strtoptype(argv[1], &ptype);
1785
1786         if (dev_id < 0) {
1787                 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1788                 if (dev_id < 0) {
1789                         fprintf(stderr, "Not connected.\n");
1790                         exit(1);
1791                 }
1792         }
1793
1794         dd = hci_open_dev(dev_id);
1795         if (dd < 0) {
1796                 perror("HCI device open failed");
1797                 exit(1);
1798         }
1799
1800         cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1801         if (!cr) {
1802                 perror("Can't allocate memory");
1803                 exit(1);
1804         }
1805
1806         bacpy(&cr->bdaddr, &bdaddr);
1807         cr->type = ACL_LINK;
1808         if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1809                 perror("Get connection info failed");
1810                 exit(1);
1811         }
1812
1813         cp.handle   = htobs(cr->conn_info->handle);
1814         cp.pkt_type = ptype;
1815
1816         memset(&rq, 0, sizeof(rq));
1817         rq.ogf    = OGF_LINK_CTL;
1818         rq.ocf    = OCF_SET_CONN_PTYPE;
1819         rq.cparam = &cp;
1820         rq.clen   = SET_CONN_PTYPE_CP_SIZE;
1821         rq.rparam = &rp;
1822         rq.rlen   = EVT_CONN_PTYPE_CHANGED_SIZE;
1823         rq.event  = EVT_CONN_PTYPE_CHANGED;
1824
1825         if (hci_send_req(dd, &rq, 100) < 0) {
1826                 perror("Packet type change failed");
1827                 exit(1);
1828         }
1829
1830         free(cr);
1831
1832         hci_close_dev(dd);
1833 }
1834
1835 /* Get/Set link policy settings */
1836
1837 static struct option lp_options[] = {
1838         { "help",       0, 0, 'h' },
1839         { 0, 0, 0, 0 }
1840 };
1841
1842 static const char *lp_help =
1843         "Usage:\n"
1844         "\tlp <bdaddr> [link policy]\n";
1845
1846 static void cmd_lp(int dev_id, int argc, char **argv)
1847 {
1848         struct hci_conn_info_req *cr;
1849         bdaddr_t bdaddr;
1850         uint16_t policy;
1851         int opt, dd;
1852
1853         for_each_opt(opt, lp_options, NULL) {
1854                 switch (opt) {
1855                 default:
1856                         printf("%s", lp_help);
1857                         return;
1858                 }
1859         }
1860         helper_arg(1, 2, &argc, &argv, lp_help);
1861
1862         str2ba(argv[0], &bdaddr);
1863
1864         if (dev_id < 0) {
1865                 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1866                 if (dev_id < 0) {
1867                         fprintf(stderr, "Not connected.\n");
1868                         exit(1);
1869                 }
1870         }
1871
1872         dd = hci_open_dev(dev_id);
1873         if (dd < 0) {
1874                 perror("HCI device open failed");
1875                 exit(1);
1876         }
1877
1878         cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1879         if (!cr) {
1880                 perror("Can't allocate memory");
1881                 exit(1);
1882         }
1883
1884         bacpy(&cr->bdaddr, &bdaddr);
1885         cr->type = ACL_LINK;
1886         if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1887                 perror("Get connection info failed");
1888                 exit(1);
1889         }
1890
1891         if (argc == 1) {
1892                 char *str;
1893                 if (hci_read_link_policy(dd, htobs(cr->conn_info->handle),
1894                                                         &policy, 1000) < 0) {
1895                         perror("HCI read_link_policy_settings request failed");
1896                         exit(1);
1897                 }
1898
1899                 policy = btohs(policy);
1900                 str = hci_lptostr(policy);
1901                 if (str) {
1902                         printf("Link policy settings: %s\n", str);
1903                         bt_free(str);
1904                 } else {
1905                         fprintf(stderr, "Invalig settings\n");
1906                         exit(1);
1907                 }
1908         } else {
1909                 unsigned int val;
1910                 if (hci_strtolp(argv[1], &val) < 0) {
1911                         fprintf(stderr, "Invalig arguments\n");
1912                         exit(1);
1913                 }
1914                 policy = val;
1915
1916                 if (hci_write_link_policy(dd, htobs(cr->conn_info->handle),
1917                                                 htobs(policy), 1000) < 0) {
1918                         perror("HCI write_link_policy_settings request failed");
1919                         exit(1);
1920                 }
1921         }
1922
1923         free(cr);
1924
1925         hci_close_dev(dd);
1926 }
1927
1928 /* Get/Set link supervision timeout */
1929
1930 static struct option lst_options[] = {
1931         { "help",       0, 0, 'h' },
1932         { 0, 0, 0, 0 }
1933 };
1934
1935 static const char *lst_help =
1936         "Usage:\n"
1937         "\tlst <bdaddr> [new value in slots]\n";
1938
1939 static void cmd_lst(int dev_id, int argc, char **argv)
1940 {
1941         struct hci_conn_info_req *cr;
1942         bdaddr_t bdaddr;
1943         uint16_t timeout;
1944         int opt, dd;
1945
1946         for_each_opt(opt, lst_options, NULL) {
1947                 switch (opt) {
1948                 default:
1949                         printf("%s", lst_help);
1950                         return;
1951                 }
1952         }
1953         helper_arg(1, 2, &argc, &argv, lst_help);
1954
1955         str2ba(argv[0], &bdaddr);
1956
1957         if (dev_id < 0) {
1958                 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
1959                 if (dev_id < 0) {
1960                         fprintf(stderr, "Not connected.\n");
1961                         exit(1);
1962                 }
1963         }
1964
1965         dd = hci_open_dev(dev_id);
1966         if (dd < 0) {
1967                 perror("HCI device open failed");
1968                 exit(1);
1969         }
1970
1971         cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
1972         if (!cr) {
1973                 perror("Can't allocate memory");
1974                 exit(1);
1975         }
1976
1977         bacpy(&cr->bdaddr, &bdaddr);
1978         cr->type = ACL_LINK;
1979         if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
1980                 perror("Get connection info failed");
1981                 exit(1);
1982         }
1983
1984         if (argc == 1) {
1985                 if (hci_read_link_supervision_timeout(dd, htobs(cr->conn_info->handle),
1986                                                         &timeout, 1000) < 0) {
1987                         perror("HCI read_link_supervision_timeout request failed");
1988                         exit(1);
1989                 }
1990
1991                 timeout = btohs(timeout);
1992
1993                 if (timeout)
1994                         printf("Link supervision timeout: %u slots (%.2f msec)\n",
1995                                 timeout, (float) timeout * 0.625);
1996                 else
1997                         printf("Link supervision timeout never expires\n");
1998         } else {
1999                 timeout = strtol(argv[1], NULL, 10);
2000
2001                 if (hci_write_link_supervision_timeout(dd, htobs(cr->conn_info->handle),
2002                                                         htobs(timeout), 1000) < 0) {
2003                         perror("HCI write_link_supervision_timeout request failed");
2004                         exit(1);
2005                 }
2006         }
2007
2008         free(cr);
2009
2010         hci_close_dev(dd);
2011 }
2012
2013 /* Request authentication */
2014
2015 static struct option auth_options[] = {
2016         { "help",       0, 0, 'h' },
2017         { 0, 0, 0, 0 }
2018 };
2019
2020 static const char *auth_help =
2021         "Usage:\n"
2022         "\tauth <bdaddr>\n";
2023
2024 static void cmd_auth(int dev_id, int argc, char **argv)
2025 {
2026         struct hci_conn_info_req *cr;
2027         bdaddr_t bdaddr;
2028         int opt, dd;
2029
2030         for_each_opt(opt, auth_options, NULL) {
2031                 switch (opt) {
2032                 default:
2033                         printf("%s", auth_help);
2034                         return;
2035                 }
2036         }
2037         helper_arg(1, 1, &argc, &argv, auth_help);
2038
2039         str2ba(argv[0], &bdaddr);
2040
2041         if (dev_id < 0) {
2042                 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
2043                 if (dev_id < 0) {
2044                         fprintf(stderr, "Not connected.\n");
2045                         exit(1);
2046                 }
2047         }
2048
2049         dd = hci_open_dev(dev_id);
2050         if (dd < 0) {
2051                 perror("HCI device open failed");
2052                 exit(1);
2053         }
2054
2055         cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
2056         if (!cr) {
2057                 perror("Can't allocate memory");
2058                 exit(1);
2059         }
2060
2061         bacpy(&cr->bdaddr, &bdaddr);
2062         cr->type = ACL_LINK;
2063         if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
2064                 perror("Get connection info failed");
2065                 exit(1);
2066         }
2067
2068         if (hci_authenticate_link(dd, htobs(cr->conn_info->handle), 25000) < 0) {
2069                 perror("HCI authentication request failed");
2070                 exit(1);
2071         }
2072
2073         free(cr);
2074
2075         hci_close_dev(dd);
2076 }
2077
2078 /* Activate encryption */
2079
2080 static struct option enc_options[] = {
2081         { "help",       0, 0, 'h' },
2082         { 0, 0, 0, 0 }
2083 };
2084
2085 static const char *enc_help =
2086         "Usage:\n"
2087         "\tenc <bdaddr> [encrypt enable]\n";
2088
2089 static void cmd_enc(int dev_id, int argc, char **argv)
2090 {
2091         struct hci_conn_info_req *cr;
2092         bdaddr_t bdaddr;
2093         uint8_t encrypt;
2094         int opt, dd;
2095
2096         for_each_opt(opt, enc_options, NULL) {
2097                 switch (opt) {
2098                 default:
2099                         printf("%s", enc_help);
2100                         return;
2101                 }
2102         }
2103         helper_arg(1, 2, &argc, &argv, enc_help);
2104
2105         str2ba(argv[0], &bdaddr);
2106
2107         if (dev_id < 0) {
2108                 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
2109                 if (dev_id < 0) {
2110                         fprintf(stderr, "Not connected.\n");
2111                         exit(1);
2112                 }
2113         }
2114
2115         dd = hci_open_dev(dev_id);
2116         if (dd < 0) {
2117                 perror("HCI device open failed");
2118                 exit(1);
2119         }
2120
2121         cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
2122         if (!cr) {
2123                 perror("Can't allocate memory");
2124                 exit(1);
2125         }
2126
2127         bacpy(&cr->bdaddr, &bdaddr);
2128         cr->type = ACL_LINK;
2129         if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
2130                 perror("Get connection info failed");
2131                 exit(1);
2132         }
2133
2134         encrypt = (argc > 1) ? atoi(argv[1]) : 1;
2135
2136         if (hci_encrypt_link(dd, htobs(cr->conn_info->handle), encrypt, 25000) < 0) {
2137                 perror("HCI set encryption request failed");
2138                 exit(1);
2139         }
2140
2141         free(cr);
2142
2143         hci_close_dev(dd);
2144 }
2145
2146 /* Change connection link key */
2147
2148 static struct option key_options[] = {
2149         { "help",       0, 0, 'h' },
2150         { 0, 0, 0, 0 }
2151 };
2152
2153 static const char *key_help =
2154         "Usage:\n"
2155         "\tkey <bdaddr>\n";
2156
2157 static void cmd_key(int dev_id, int argc, char **argv)
2158 {
2159         struct hci_conn_info_req *cr;
2160         bdaddr_t bdaddr;
2161         int opt, dd;
2162
2163         for_each_opt(opt, key_options, NULL) {
2164                 switch (opt) {
2165                 default:
2166                         printf("%s", key_help);
2167                         return;
2168                 }
2169         }
2170         helper_arg(1, 1, &argc, &argv, key_help);
2171
2172         str2ba(argv[0], &bdaddr);
2173
2174         if (dev_id < 0) {
2175                 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
2176                 if (dev_id < 0) {
2177                         fprintf(stderr, "Not connected.\n");
2178                         exit(1);
2179                 }
2180         }
2181
2182         dd = hci_open_dev(dev_id);
2183         if (dd < 0) {
2184                 perror("HCI device open failed");
2185                 exit(1);
2186         }
2187
2188         cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
2189         if (!cr) {
2190                 perror("Can't allocate memory");
2191                 exit(1);
2192         }
2193
2194         bacpy(&cr->bdaddr, &bdaddr);
2195         cr->type = ACL_LINK;
2196         if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
2197                 perror("Get connection info failed");
2198                 exit(1);
2199         }
2200
2201         if (hci_change_link_key(dd, htobs(cr->conn_info->handle), 25000) < 0) {
2202                 perror("Changing link key failed");
2203                 exit(1);
2204         }
2205
2206         free(cr);
2207
2208         hci_close_dev(dd);
2209 }
2210
2211 /* Read clock offset */
2212
2213 static struct option clkoff_options[] = {
2214         { "help",       0, 0, 'h' },
2215         { 0, 0, 0, 0 }
2216 };
2217
2218 static const char *clkoff_help =
2219         "Usage:\n"
2220         "\tclkoff <bdaddr>\n";
2221
2222 static void cmd_clkoff(int dev_id, int argc, char **argv)
2223 {
2224         struct hci_conn_info_req *cr;
2225         bdaddr_t bdaddr;
2226         uint16_t offset;
2227         int opt, dd;
2228
2229         for_each_opt(opt, clkoff_options, NULL) {
2230                 switch (opt) {
2231                 default:
2232                         printf("%s", clkoff_help);
2233                         return;
2234                 }
2235         }
2236         helper_arg(1, 1, &argc, &argv, clkoff_help);
2237
2238         str2ba(argv[0], &bdaddr);
2239
2240         if (dev_id < 0) {
2241                 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
2242                 if (dev_id < 0) {
2243                         fprintf(stderr, "Not connected.\n");
2244                         exit(1);
2245                 }
2246         }
2247
2248         dd = hci_open_dev(dev_id);
2249         if (dd < 0) {
2250                 perror("HCI device open failed");
2251                 exit(1);
2252         }
2253
2254         cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
2255         if (!cr) {
2256                 perror("Can't allocate memory");
2257                 exit(1);
2258         }
2259
2260         bacpy(&cr->bdaddr, &bdaddr);
2261         cr->type = ACL_LINK;
2262         if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
2263                 perror("Get connection info failed");
2264                 exit(1);
2265         }
2266
2267         if (hci_read_clock_offset(dd, htobs(cr->conn_info->handle), &offset, 1000) < 0) {
2268                 perror("Reading clock offset failed");
2269                 exit(1);
2270         }
2271
2272         printf("Clock offset: 0x%4.4x\n", btohs(offset));
2273
2274         free(cr);
2275
2276         hci_close_dev(dd);
2277 }
2278
2279 /* Read clock */
2280
2281 static struct option clock_options[] = {
2282         { "help",       0, 0, 'h' },
2283         { 0, 0, 0, 0 }
2284 };
2285
2286 static const char *clock_help =
2287         "Usage:\n"
2288         "\tclock [bdaddr] [which clock]\n";
2289
2290 static void cmd_clock(int dev_id, int argc, char **argv)
2291 {
2292         struct hci_conn_info_req *cr;
2293         bdaddr_t bdaddr;
2294         uint8_t which;
2295         uint32_t handle, clock;
2296         uint16_t accuracy;
2297         int opt, dd;
2298
2299         for_each_opt(opt, clock_options, NULL) {
2300                 switch (opt) {
2301                 default:
2302                         printf("%s", clock_help);
2303                         return;
2304                 }
2305         }
2306         helper_arg(0, 2, &argc, &argv, clock_help);
2307
2308         if (argc > 0)
2309                 str2ba(argv[0], &bdaddr);
2310         else
2311                 bacpy(&bdaddr, BDADDR_ANY);
2312
2313         if (dev_id < 0 && !bacmp(&bdaddr, BDADDR_ANY))
2314                 dev_id = hci_get_route(NULL);
2315
2316         if (dev_id < 0) {
2317                 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
2318                 if (dev_id < 0) {
2319                         fprintf(stderr, "Not connected.\n");
2320                         exit(1);
2321                 }
2322         }
2323
2324         dd = hci_open_dev(dev_id);
2325         if (dd < 0) {
2326                 perror("HCI device open failed");
2327                 exit(1);
2328         }
2329
2330         if (bacmp(&bdaddr, BDADDR_ANY)) {
2331                 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
2332                 if (!cr) {
2333                         perror("Can't allocate memory");
2334                         exit(1);
2335                 }
2336
2337                 bacpy(&cr->bdaddr, &bdaddr);
2338                 cr->type = ACL_LINK;
2339                 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
2340                         perror("Get connection info failed");
2341                         free(cr);
2342                         exit(1);
2343                 }
2344
2345                 handle = htobs(cr->conn_info->handle);
2346                 which = (argc > 1) ? atoi(argv[1]) : 0x01;
2347
2348                 free(cr);
2349         } else {
2350                 handle = 0x00;
2351                 which = 0x00;
2352         }
2353
2354         if (hci_read_clock(dd, handle, which, &clock, &accuracy, 1000) < 0) {
2355                 perror("Reading clock failed");
2356                 exit(1);
2357         }
2358
2359         accuracy = btohs(accuracy);
2360
2361         printf("Clock:    0x%4.4x\n", btohl(clock));
2362         printf("Accuracy: %.2f msec\n", (float) accuracy * 0.3125);
2363
2364         hci_close_dev(dd);
2365 }
2366
2367 static int read_flags(uint8_t *flags, const uint8_t *data, size_t size)
2368 {
2369         size_t offset;
2370
2371         if (!flags || !data)
2372                 return -EINVAL;
2373
2374         offset = 0;
2375         while (offset < size) {
2376                 uint8_t len = data[offset];
2377                 uint8_t type;
2378
2379                 /* Check if it is the end of the significant part */
2380                 if (len == 0)
2381                         break;
2382
2383                 if (len + offset > size)
2384                         break;
2385
2386                 type = data[offset + 1];
2387
2388                 if (type == FLAGS_AD_TYPE) {
2389                         *flags = data[offset + 2];
2390                         return 0;
2391                 }
2392
2393                 offset += 1 + len;
2394         }
2395
2396         return -ENOENT;
2397 }
2398
2399 static int check_report_filter(uint8_t procedure, le_advertising_info *info)
2400 {
2401         uint8_t flags;
2402
2403         /* If no discovery procedure is set, all reports are treat as valid */
2404         if (procedure == 0)
2405                 return 1;
2406
2407         /* Read flags AD type value from the advertising report if it exists */
2408         if (read_flags(&flags, info->data, info->length))
2409                 return 0;
2410
2411         switch (procedure) {
2412         case 'l': /* Limited Discovery Procedure */
2413                 if (flags & FLAGS_LIMITED_MODE_BIT)
2414                         return 1;
2415                 break;
2416         case 'g': /* General Discovery Procedure */
2417                 if (flags & (FLAGS_LIMITED_MODE_BIT | FLAGS_GENERAL_MODE_BIT))
2418                         return 1;
2419                 break;
2420         default:
2421                 fprintf(stderr, "Unknown discovery procedure\n");
2422         }
2423
2424         return 0;
2425 }
2426
2427 static void sigint_handler(int sig)
2428 {
2429         signal_received = sig;
2430 }
2431
2432 static void eir_parse_name(uint8_t *eir, size_t eir_len,
2433                                                 char *buf, size_t buf_len)
2434 {
2435         size_t offset;
2436
2437         offset = 0;
2438         while (offset < eir_len) {
2439                 uint8_t field_len = eir[0];
2440                 size_t name_len;
2441
2442                 /* Check for the end of EIR */
2443                 if (field_len == 0)
2444                         break;
2445
2446                 if (offset + field_len > eir_len)
2447                         goto failed;
2448
2449                 switch (eir[1]) {
2450                 case EIR_NAME_SHORT:
2451                 case EIR_NAME_COMPLETE:
2452                         name_len = field_len - 1;
2453                         if (name_len > buf_len)
2454                                 goto failed;
2455
2456                         memcpy(buf, &eir[2], name_len);
2457                         return;
2458                 }
2459
2460                 offset += field_len + 1;
2461                 eir += field_len + 1;
2462         }
2463
2464 failed:
2465         snprintf(buf, buf_len, "(unknown)");
2466 }
2467
2468 static int print_advertising_devices(int dd, uint8_t filter_type)
2469 {
2470         unsigned char buf[HCI_MAX_EVENT_SIZE], *ptr;
2471         struct hci_filter nf, of;
2472         struct sigaction sa;
2473         socklen_t olen;
2474         int len;
2475
2476         olen = sizeof(of);
2477         if (getsockopt(dd, SOL_HCI, HCI_FILTER, &of, &olen) < 0) {
2478                 printf("Could not get socket options\n");
2479                 return -1;
2480         }
2481
2482         hci_filter_clear(&nf);
2483         hci_filter_set_ptype(HCI_EVENT_PKT, &nf);
2484         hci_filter_set_event(EVT_LE_META_EVENT, &nf);
2485
2486         if (setsockopt(dd, SOL_HCI, HCI_FILTER, &nf, sizeof(nf)) < 0) {
2487                 printf("Could not set socket options\n");
2488                 return -1;
2489         }
2490
2491         memset(&sa, 0, sizeof(sa));
2492         sa.sa_flags = SA_NOCLDSTOP;
2493         sa.sa_handler = sigint_handler;
2494         sigaction(SIGINT, &sa, NULL);
2495
2496         while (1) {
2497                 evt_le_meta_event *meta;
2498                 le_advertising_info *info;
2499                 char addr[18];
2500
2501                 while ((len = read(dd, buf, sizeof(buf))) < 0) {
2502                         if (errno == EINTR && signal_received == SIGINT) {
2503                                 len = 0;
2504                                 goto done;
2505                         }
2506
2507                         if (errno == EAGAIN || errno == EINTR)
2508                                 continue;
2509                         goto done;
2510                 }
2511
2512                 ptr = buf + (1 + HCI_EVENT_HDR_SIZE);
2513                 len -= (1 + HCI_EVENT_HDR_SIZE);
2514
2515                 meta = (void *) ptr;
2516
2517                 if (meta->subevent != 0x02)
2518                         goto done;
2519
2520                 /* Ignoring multiple reports */
2521                 info = (le_advertising_info *) (meta->data + 1);
2522                 if (check_report_filter(filter_type, info)) {
2523                         char name[30];
2524
2525                         memset(name, 0, sizeof(name));
2526
2527                         ba2str(&info->bdaddr, addr);
2528                         eir_parse_name(info->data, info->length,
2529                                                         name, sizeof(name) - 1);
2530
2531                         printf("%s %s\n", addr, name);
2532                 }
2533         }
2534
2535 done:
2536         setsockopt(dd, SOL_HCI, HCI_FILTER, &of, sizeof(of));
2537
2538         if (len < 0)
2539                 return -1;
2540
2541         return 0;
2542 }
2543
2544 static struct option lescan_options[] = {
2545         { "help",       0, 0, 'h' },
2546         { "static",     0, 0, 's' },
2547         { "privacy",    0, 0, 'p' },
2548         { "passive",    0, 0, 'P' },
2549         { "whitelist",  0, 0, 'w' },
2550         { "discovery",  1, 0, 'd' },
2551         { "duplicates", 0, 0, 'D' },
2552         { 0, 0, 0, 0 }
2553 };
2554
2555 static const char *lescan_help =
2556         "Usage:\n"
2557         "\tlescan [--privacy] enable privacy\n"
2558         "\tlescan [--passive] set scan type passive (default active)\n"
2559         "\tlescan [--whitelist] scan for address in the whitelist only\n"
2560         "\tlescan [--discovery=g|l] enable general or limited discovery"
2561                 "procedure\n"
2562         "\tlescan [--duplicates] don't filter duplicates\n";
2563
2564 static void cmd_lescan(int dev_id, int argc, char **argv)
2565 {
2566         int err, opt, dd;
2567         uint8_t own_type = LE_PUBLIC_ADDRESS;
2568         uint8_t scan_type = 0x01;
2569         uint8_t filter_type = 0;
2570         uint8_t filter_policy = 0x00;
2571         uint16_t interval = htobs(0x0010);
2572         uint16_t window = htobs(0x0010);
2573         uint8_t filter_dup = 0x01;
2574
2575         for_each_opt(opt, lescan_options, NULL) {
2576                 switch (opt) {
2577                 case 's':
2578                         own_type = LE_RANDOM_ADDRESS;
2579                         break;
2580                 case 'p':
2581                         own_type = LE_RANDOM_ADDRESS;
2582                         break;
2583                 case 'P':
2584                         scan_type = 0x00; /* Passive */
2585                         break;
2586                 case 'w':
2587                         filter_policy = 0x01; /* Whitelist */
2588                         break;
2589                 case 'd':
2590                         filter_type = optarg[0];
2591                         if (filter_type != 'g' && filter_type != 'l') {
2592                                 fprintf(stderr, "Unknown discovery procedure\n");
2593                                 exit(1);
2594                         }
2595
2596                         interval = htobs(0x0012);
2597                         window = htobs(0x0012);
2598                         break;
2599                 case 'D':
2600                         filter_dup = 0x00;
2601                         break;
2602                 default:
2603                         printf("%s", lescan_help);
2604                         return;
2605                 }
2606         }
2607         helper_arg(0, 1, &argc, &argv, lescan_help);
2608
2609         if (dev_id < 0)
2610                 dev_id = hci_get_route(NULL);
2611
2612         dd = hci_open_dev(dev_id);
2613         if (dd < 0) {
2614                 perror("Could not open device");
2615                 exit(1);
2616         }
2617
2618         err = hci_le_set_scan_parameters(dd, scan_type, interval, window,
2619                                                 own_type, filter_policy, 10000);
2620         if (err < 0) {
2621                 perror("Set scan parameters failed");
2622                 exit(1);
2623         }
2624
2625         err = hci_le_set_scan_enable(dd, 0x01, filter_dup, 10000);
2626         if (err < 0) {
2627                 perror("Enable scan failed");
2628                 exit(1);
2629         }
2630
2631         printf("LE Scan ...\n");
2632
2633         err = print_advertising_devices(dd, filter_type);
2634         if (err < 0) {
2635                 perror("Could not receive advertising events");
2636                 exit(1);
2637         }
2638
2639         err = hci_le_set_scan_enable(dd, 0x00, filter_dup, 10000);
2640         if (err < 0) {
2641                 perror("Disable scan failed");
2642                 exit(1);
2643         }
2644
2645         hci_close_dev(dd);
2646 }
2647
2648 static struct option leinfo_options[] = {
2649         { "help",       0, 0, 'h' },
2650         { "static",     0, 0, 's' },
2651         { "random",     0, 0, 'r' },
2652         { 0, 0, 0, 0 }
2653 };
2654
2655 static const char *leinfo_help =
2656         "Usage:\n"
2657         "\tleinfo [--static] [--random] <bdaddr>\n";
2658
2659 static void cmd_leinfo(int dev_id, int argc, char **argv)
2660 {
2661         bdaddr_t bdaddr;
2662         uint16_t handle;
2663         uint8_t features[8];
2664         struct hci_version version;
2665         uint16_t interval, latency, max_ce_length, max_interval, min_ce_length;
2666         uint16_t min_interval, supervision_timeout, window;
2667         uint8_t initiator_filter, own_bdaddr_type, peer_bdaddr_type;
2668         int opt, err, dd;
2669
2670         own_bdaddr_type = LE_PUBLIC_ADDRESS;
2671         peer_bdaddr_type = LE_PUBLIC_ADDRESS;
2672
2673         for_each_opt(opt, leinfo_options, NULL) {
2674                 switch (opt) {
2675                 case 's':
2676                         own_bdaddr_type = LE_RANDOM_ADDRESS;
2677                         break;
2678                 case 'r':
2679                         peer_bdaddr_type = LE_RANDOM_ADDRESS;
2680                         break;
2681                 default:
2682                         printf("%s", leinfo_help);
2683                         return;
2684                 }
2685         }
2686         helper_arg(1, 1, &argc, &argv, leinfo_help);
2687
2688         str2ba(argv[0], &bdaddr);
2689
2690         printf("Requesting information ...\n");
2691
2692         if (dev_id < 0)
2693                 dev_id = hci_get_route(NULL);
2694
2695         dd = hci_open_dev(dev_id);
2696         if (dd < 0) {
2697                 perror("Could not open device");
2698                 exit(1);
2699         }
2700
2701         interval = htobs(0x0004);
2702         window = htobs(0x0004);
2703         initiator_filter = 0;
2704         min_interval = htobs(0x000F);
2705         max_interval = htobs(0x000F);
2706         latency = htobs(0x0000);
2707         supervision_timeout = htobs(0x0C80);
2708         min_ce_length = htobs(0x0000);
2709         max_ce_length = htobs(0x0000);
2710
2711         err = hci_le_create_conn(dd, interval, window, initiator_filter,
2712                         peer_bdaddr_type, bdaddr, own_bdaddr_type, min_interval,
2713                         max_interval, latency, supervision_timeout,
2714                         min_ce_length, max_ce_length, &handle, 25000);
2715         if (err < 0) {
2716                 perror("Could not create connection");
2717                 exit(1);
2718         }
2719
2720         printf("\tHandle: %d (0x%04x)\n", handle, handle);
2721
2722         if (hci_read_remote_version(dd, handle, &version, 20000) == 0) {
2723                 char *ver = lmp_vertostr(version.lmp_ver);
2724                 printf("\tLMP Version: %s (0x%x) LMP Subversion: 0x%x\n"
2725                         "\tManufacturer: %s (%d)\n",
2726                         ver ? ver : "n/a",
2727                         version.lmp_ver,
2728                         version.lmp_subver,
2729                         bt_compidtostr(version.manufacturer),
2730                         version.manufacturer);
2731                 if (ver)
2732                         bt_free(ver);
2733         }
2734
2735         memset(features, 0, sizeof(features));
2736         hci_le_read_remote_features(dd, handle, features, 20000);
2737
2738         printf("\tFeatures: 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x "
2739                                 "0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n",
2740                 features[0], features[1], features[2], features[3],
2741                 features[4], features[5], features[6], features[7]);
2742
2743         usleep(10000);
2744         hci_disconnect(dd, handle, HCI_OE_USER_ENDED_CONNECTION, 10000);
2745
2746         hci_close_dev(dd);
2747 }
2748
2749 static struct option lecc_options[] = {
2750         { "help",       0, 0, 'h' },
2751         { "static",     0, 0, 's' },
2752         { "random",     0, 0, 'r' },
2753         { "whitelist",  0, 0, 'w' },
2754         { 0, 0, 0, 0 }
2755 };
2756
2757 static const char *lecc_help =
2758         "Usage:\n"
2759         "\tlecc [--static] [--random] <bdaddr>\n"
2760         "\tlecc --whitelist\n";
2761
2762 static void cmd_lecc(int dev_id, int argc, char **argv)
2763 {
2764         int err, opt, dd;
2765         bdaddr_t bdaddr;
2766         uint16_t interval, latency, max_ce_length, max_interval, min_ce_length;
2767         uint16_t min_interval, supervision_timeout, window, handle;
2768         uint8_t initiator_filter, own_bdaddr_type, peer_bdaddr_type;
2769
2770         own_bdaddr_type = LE_PUBLIC_ADDRESS;
2771         peer_bdaddr_type = LE_PUBLIC_ADDRESS;
2772         initiator_filter = 0; /* Use peer address */
2773
2774         for_each_opt(opt, lecc_options, NULL) {
2775                 switch (opt) {
2776                 case 's':
2777                         own_bdaddr_type = LE_RANDOM_ADDRESS;
2778                         break;
2779                 case 'r':
2780                         peer_bdaddr_type = LE_RANDOM_ADDRESS;
2781                         break;
2782                 case 'w':
2783                         initiator_filter = 0x01; /* Use white list */
2784                         break;
2785                 default:
2786                         printf("%s", lecc_help);
2787                         return;
2788                 }
2789         }
2790         helper_arg(0, 1, &argc, &argv, lecc_help);
2791
2792         if (dev_id < 0)
2793                 dev_id = hci_get_route(NULL);
2794
2795         dd = hci_open_dev(dev_id);
2796         if (dd < 0) {
2797                 perror("Could not open device");
2798                 exit(1);
2799         }
2800
2801         memset(&bdaddr, 0, sizeof(bdaddr_t));
2802         if (argv[0])
2803                 str2ba(argv[0], &bdaddr);
2804
2805         interval = htobs(0x0004);
2806         window = htobs(0x0004);
2807         min_interval = htobs(0x000F);
2808         max_interval = htobs(0x000F);
2809         latency = htobs(0x0000);
2810         supervision_timeout = htobs(0x0C80);
2811         min_ce_length = htobs(0x0001);
2812         max_ce_length = htobs(0x0001);
2813
2814         err = hci_le_create_conn(dd, interval, window, initiator_filter,
2815                         peer_bdaddr_type, bdaddr, own_bdaddr_type, min_interval,
2816                         max_interval, latency, supervision_timeout,
2817                         min_ce_length, max_ce_length, &handle, 25000);
2818         if (err < 0) {
2819                 perror("Could not create connection");
2820                 exit(1);
2821         }
2822
2823         printf("Connection handle %d\n", handle);
2824
2825         hci_close_dev(dd);
2826 }
2827
2828 static struct option lewladd_options[] = {
2829         { "help",       0, 0, 'h' },
2830         { "random",     0, 0, 'r' },
2831         { 0, 0, 0, 0 }
2832 };
2833
2834 static const char *lewladd_help =
2835         "Usage:\n"
2836         "\tlewladd [--random] <bdaddr>\n";
2837
2838 static void cmd_lewladd(int dev_id, int argc, char **argv)
2839 {
2840         int err, opt, dd;
2841         bdaddr_t bdaddr;
2842         uint8_t bdaddr_type = LE_PUBLIC_ADDRESS;
2843
2844         for_each_opt(opt, lewladd_options, NULL) {
2845                 switch (opt) {
2846                 case 'r':
2847                         bdaddr_type = LE_RANDOM_ADDRESS;
2848                         break;
2849                 default:
2850                         printf("%s", lewladd_help);
2851                         return;
2852                 }
2853         }
2854
2855         helper_arg(1, 1, &argc, &argv, lewladd_help);
2856
2857         if (dev_id < 0)
2858                 dev_id = hci_get_route(NULL);
2859
2860         dd = hci_open_dev(dev_id);
2861         if (dd < 0) {
2862                 perror("Could not open device");
2863                 exit(1);
2864         }
2865
2866         str2ba(argv[0], &bdaddr);
2867
2868         err = hci_le_add_white_list(dd, &bdaddr, bdaddr_type, 1000);
2869         hci_close_dev(dd);
2870
2871         if (err < 0) {
2872                 err = -errno;
2873                 fprintf(stderr, "Can't add to white list: %s(%d)\n",
2874                                                         strerror(-err), -err);
2875                 exit(1);
2876         }
2877 }
2878
2879 static struct option lewlrm_options[] = {
2880         { "help",       0, 0, 'h' },
2881         { 0, 0, 0, 0 }
2882 };
2883
2884 static const char *lewlrm_help =
2885         "Usage:\n"
2886         "\tlewlrm <bdaddr>\n";
2887
2888 static void cmd_lewlrm(int dev_id, int argc, char **argv)
2889 {
2890         int err, opt, dd;
2891         bdaddr_t bdaddr;
2892
2893         for_each_opt(opt, lewlrm_options, NULL) {
2894                 switch (opt) {
2895                 default:
2896                         printf("%s", lewlrm_help);
2897                         return;
2898                 }
2899         }
2900
2901         helper_arg(1, 1, &argc, &argv, lewlrm_help);
2902
2903         if (dev_id < 0)
2904                 dev_id = hci_get_route(NULL);
2905
2906         dd = hci_open_dev(dev_id);
2907         if (dd < 0) {
2908                 perror("Could not open device");
2909                 exit(1);
2910         }
2911
2912         str2ba(argv[0], &bdaddr);
2913
2914         err = hci_le_rm_white_list(dd, &bdaddr, LE_PUBLIC_ADDRESS, 1000);
2915         hci_close_dev(dd);
2916
2917         if (err < 0) {
2918                 err = errno;
2919                 fprintf(stderr, "Can't remove from white list: %s(%d)\n",
2920                                                         strerror(err), err);
2921                 exit(1);
2922         }
2923 }
2924
2925 static struct option lewlsz_options[] = {
2926         { "help",       0, 0, 'h' },
2927         { 0, 0, 0, 0 }
2928 };
2929
2930 static const char *lewlsz_help =
2931         "Usage:\n"
2932         "\tlewlsz\n";
2933
2934 static void cmd_lewlsz(int dev_id, int argc, char **argv)
2935 {
2936         int err, dd, opt;
2937         uint8_t size;
2938
2939         for_each_opt(opt, lewlsz_options, NULL) {
2940                 switch (opt) {
2941                 default:
2942                         printf("%s", lewlsz_help);
2943                         return;
2944                 }
2945         }
2946
2947         helper_arg(0, 0, &argc, &argv, lewlsz_help);
2948
2949         if (dev_id < 0)
2950                 dev_id = hci_get_route(NULL);
2951
2952         dd = hci_open_dev(dev_id);
2953         if (dd < 0) {
2954                 perror("Could not open device");
2955                 exit(1);
2956         }
2957
2958         err = hci_le_read_white_list_size(dd, &size, 1000);
2959         hci_close_dev(dd);
2960
2961         if (err < 0) {
2962                 err = -errno;
2963                 fprintf(stderr, "Can't read white list size: %s(%d)\n",
2964                                                         strerror(-err), -err);
2965                 exit(1);
2966         }
2967
2968         printf("White list size: %d\n", size);
2969 }
2970
2971 static struct option lewlclr_options[] = {
2972         { "help",       0, 0, 'h' },
2973         { 0, 0, 0, 0 }
2974 };
2975
2976 static const char *lewlclr_help =
2977         "Usage:\n"
2978         "\tlewlclr\n";
2979
2980 static void cmd_lewlclr(int dev_id, int argc, char **argv)
2981 {
2982         int err, dd, opt;
2983
2984         for_each_opt(opt, lewlclr_options, NULL) {
2985                 switch (opt) {
2986                 default:
2987                         printf("%s", lewlclr_help);
2988                         return;
2989                 }
2990         }
2991
2992         helper_arg(0, 0, &argc, &argv, lewlclr_help);
2993
2994         if (dev_id < 0)
2995                 dev_id = hci_get_route(NULL);
2996
2997         dd = hci_open_dev(dev_id);
2998         if (dd < 0) {
2999                 perror("Could not open device");
3000                 exit(1);
3001         }
3002
3003         err = hci_le_clear_white_list(dd, 1000);
3004         hci_close_dev(dd);
3005
3006         if (err < 0) {
3007                 err = -errno;
3008                 fprintf(stderr, "Can't clear white list: %s(%d)\n",
3009                                                         strerror(-err), -err);
3010                 exit(1);
3011         }
3012 }
3013
3014 static struct option lerladd_options[] = {
3015         { "help",       0, 0, 'h' },
3016         { "random",     0, 0, 'r' },
3017         { "local",      1, 0, 'l' },
3018         { "peer",       1, 0, 'p' },
3019         { 0, 0, 0, 0 }
3020 };
3021
3022 static const char *lerladd_help =
3023         "Usage:\n"
3024         "\tlerladd [--local irk] [--peer irk] [--random] <bdaddr>\n";
3025
3026 static void cmd_lerladd(int dev_id, int argc, char **argv)
3027 {
3028         int err, opt, dd;
3029         bdaddr_t bdaddr;
3030         uint8_t bdaddr_type = LE_PUBLIC_ADDRESS;
3031         uint8_t local_irk[16], peer_irk[16];
3032
3033         memset(local_irk, 0, 16);
3034         memset(peer_irk, 0, 16);
3035
3036         for_each_opt(opt, lerladd_options, NULL) {
3037                 switch (opt) {
3038                 case 'r':
3039                         bdaddr_type = LE_RANDOM_ADDRESS;
3040                         break;
3041                 case 'l':
3042                         str2buf(optarg, local_irk, 16);
3043                         break;
3044                 case 'p':
3045                         str2buf(optarg, peer_irk, 16);
3046                         break;
3047                 default:
3048                         printf("%s", lerladd_help);
3049                         return;
3050                 }
3051         }
3052
3053         helper_arg(1, 1, &argc, &argv, lerladd_help);
3054
3055         if (dev_id < 0)
3056                 dev_id = hci_get_route(NULL);
3057
3058         dd = hci_open_dev(dev_id);
3059         if (dd < 0) {
3060                 perror("Could not open device");
3061                 exit(1);
3062         }
3063
3064         str2ba(argv[0], &bdaddr);
3065
3066         err = hci_le_add_resolving_list(dd, &bdaddr, bdaddr_type,
3067                                                 peer_irk, local_irk, 1000);
3068         hci_close_dev(dd);
3069
3070         if (err < 0) {
3071                 err = -errno;
3072                 fprintf(stderr, "Can't add to resolving list: %s(%d)\n",
3073                                                         strerror(-err), -err);
3074                 exit(1);
3075         }
3076 }
3077
3078 static struct option lerlrm_options[] = {
3079         { "help",       0, 0, 'h' },
3080         { 0, 0, 0, 0 }
3081 };
3082
3083 static const char *lerlrm_help =
3084         "Usage:\n"
3085         "\tlerlrm <bdaddr>\n";
3086
3087 static void cmd_lerlrm(int dev_id, int argc, char **argv)
3088 {
3089         int err, opt, dd;
3090         bdaddr_t bdaddr;
3091
3092         for_each_opt(opt, lerlrm_options, NULL) {
3093                 switch (opt) {
3094                 default:
3095                         printf("%s", lerlrm_help);
3096                         return;
3097                 }
3098         }
3099
3100         helper_arg(1, 1, &argc, &argv, lerlrm_help);
3101
3102         if (dev_id < 0)
3103                 dev_id = hci_get_route(NULL);
3104
3105         dd = hci_open_dev(dev_id);
3106         if (dd < 0) {
3107                 perror("Could not open device");
3108                 exit(1);
3109         }
3110
3111         str2ba(argv[0], &bdaddr);
3112
3113         err = hci_le_rm_resolving_list(dd, &bdaddr, LE_PUBLIC_ADDRESS, 1000);
3114         hci_close_dev(dd);
3115
3116         if (err < 0) {
3117                 err = errno;
3118                 fprintf(stderr, "Can't remove from resolving list: %s(%d)\n",
3119                                                         strerror(err), err);
3120                 exit(1);
3121         }
3122 }
3123
3124 static struct option lerlclr_options[] = {
3125         { "help",       0, 0, 'h' },
3126         { 0, 0, 0, 0 }
3127 };
3128
3129 static const char *lerlclr_help =
3130         "Usage:\n"
3131         "\tlerlclr\n";
3132
3133 static void cmd_lerlclr(int dev_id, int argc, char **argv)
3134 {
3135         int err, dd, opt;
3136
3137         for_each_opt(opt, lerlclr_options, NULL) {
3138                 switch (opt) {
3139                 default:
3140                         printf("%s", lerlclr_help);
3141                         return;
3142                 }
3143         }
3144
3145         helper_arg(0, 0, &argc, &argv, lerlclr_help);
3146
3147         if (dev_id < 0)
3148                 dev_id = hci_get_route(NULL);
3149
3150         dd = hci_open_dev(dev_id);
3151         if (dd < 0) {
3152                 perror("Could not open device");
3153                 exit(1);
3154         }
3155
3156         err = hci_le_clear_resolving_list(dd, 1000);
3157         hci_close_dev(dd);
3158
3159         if (err < 0) {
3160                 err = -errno;
3161                 fprintf(stderr, "Can't clear resolving list: %s(%d)\n",
3162                                                         strerror(-err), -err);
3163                 exit(1);
3164         }
3165 }
3166
3167 static struct option lerlsz_options[] = {
3168         { "help",       0, 0, 'h' },
3169         { 0, 0, 0, 0 }
3170 };
3171
3172 static const char *lerlsz_help =
3173         "Usage:\n"
3174         "\tlerlsz\n";
3175
3176 static void cmd_lerlsz(int dev_id, int argc, char **argv)
3177 {
3178         int err, dd, opt;
3179         uint8_t size;
3180
3181         for_each_opt(opt, lerlsz_options, NULL) {
3182                 switch (opt) {
3183                 default:
3184                         printf("%s", lerlsz_help);
3185                         return;
3186                 }
3187         }
3188
3189         helper_arg(0, 0, &argc, &argv, lerlsz_help);
3190
3191         if (dev_id < 0)
3192                 dev_id = hci_get_route(NULL);
3193
3194         dd = hci_open_dev(dev_id);
3195         if (dd < 0) {
3196                 perror("Could not open device");
3197                 exit(1);
3198         }
3199
3200         err = hci_le_read_resolving_list_size(dd, &size, 1000);
3201         hci_close_dev(dd);
3202
3203         if (err < 0) {
3204                 err = -errno;
3205                 fprintf(stderr, "Can't read resolving list size: %s(%d)\n",
3206                                                         strerror(-err), -err);
3207                 exit(1);
3208         }
3209
3210         printf("Resolving list size: %d\n", size);
3211 }
3212
3213 static struct option lerlon_options[] = {
3214         { "help",       0, 0, 'h' },
3215         { 0, 0, 0, 0 }
3216 };
3217
3218 static const char *lerlon_help =
3219         "Usage:\n"
3220         "\tlerlon\n";
3221
3222 static void cmd_lerlon(int dev_id, int argc, char **argv)
3223 {
3224         int err, dd, opt;
3225
3226         for_each_opt(opt, lerlon_options, NULL) {
3227                 switch (opt) {
3228                 default:
3229                         printf("%s", lerlon_help);
3230                         return;
3231                 }
3232         }
3233
3234         helper_arg(0, 0, &argc, &argv, lerlon_help);
3235
3236         if (dev_id < 0)
3237                 dev_id = hci_get_route(NULL);
3238
3239         dd = hci_open_dev(dev_id);
3240         if (dd < 0) {
3241                 perror("Could not open device");
3242                 exit(1);
3243         }
3244
3245         err = hci_le_set_address_resolution_enable(dd, 0x01, 1000);
3246         hci_close_dev(dd);
3247
3248         if (err < 0) {
3249                 err = -errno;
3250                 fprintf(stderr, "Can't set address resolution enable: %s(%d)\n",
3251                                                         strerror(-err), -err);
3252                 exit(1);
3253         }
3254 }
3255
3256 static struct option lerloff_options[] = {
3257         { "help",       0, 0, 'h' },
3258         { 0, 0, 0, 0 }
3259 };
3260
3261 static const char *lerloff_help =
3262         "Usage:\n"
3263         "\tlerloff\n";
3264
3265 static void cmd_lerloff(int dev_id, int argc, char **argv)
3266 {
3267         int err, dd, opt;
3268
3269         for_each_opt(opt, lerloff_options, NULL) {
3270                 switch (opt) {
3271                 default:
3272                         printf("%s", lerloff_help);
3273                         return;
3274                 }
3275         }
3276
3277         helper_arg(0, 0, &argc, &argv, lerloff_help);
3278
3279         if (dev_id < 0)
3280                 dev_id = hci_get_route(NULL);
3281
3282         dd = hci_open_dev(dev_id);
3283         if (dd < 0) {
3284                 perror("Could not open device");
3285                 exit(1);
3286         }
3287
3288         err = hci_le_set_address_resolution_enable(dd, 0x00, 1000);
3289         hci_close_dev(dd);
3290
3291         if (err < 0) {
3292                 err = -errno;
3293                 fprintf(stderr, "Can't set address resolution enable: %s(%d)\n",
3294                                                         strerror(-err), -err);
3295                 exit(1);
3296         }
3297 }
3298
3299 static struct option ledc_options[] = {
3300         { "help",       0, 0, 'h' },
3301         { 0, 0, 0, 0 }
3302 };
3303
3304 static const char *ledc_help =
3305         "Usage:\n"
3306         "\tledc <handle> [reason]\n";
3307
3308 static void cmd_ledc(int dev_id, int argc, char **argv)
3309 {
3310         int err, opt, dd;
3311         uint16_t handle;
3312         uint8_t reason;
3313
3314         for_each_opt(opt, ledc_options, NULL) {
3315                 switch (opt) {
3316                 default:
3317                         printf("%s", ledc_help);
3318                         return;
3319                 }
3320         }
3321         helper_arg(1, 2, &argc, &argv, ledc_help);
3322
3323         if (dev_id < 0)
3324                 dev_id = hci_get_route(NULL);
3325
3326         dd = hci_open_dev(dev_id);
3327         if (dd < 0) {
3328                 perror("Could not open device");
3329                 exit(1);
3330         }
3331
3332         handle = atoi(argv[0]);
3333
3334         reason = (argc > 1) ? atoi(argv[1]) : HCI_OE_USER_ENDED_CONNECTION;
3335
3336         err = hci_disconnect(dd, handle, reason, 10000);
3337         if (err < 0) {
3338                 perror("Could not disconnect");
3339                 exit(1);
3340         }
3341
3342         hci_close_dev(dd);
3343 }
3344
3345 static struct option lecup_options[] = {
3346         { "help",       0, 0, 'h' },
3347         { "handle",     1, 0, 'H' },
3348         { "min",        1, 0, 'm' },
3349         { "max",        1, 0, 'M' },
3350         { "latency",    1, 0, 'l' },
3351         { "timeout",    1, 0, 't' },
3352         { 0, 0, 0, 0 }
3353 };
3354
3355 static const char *lecup_help =
3356         "Usage:\n"
3357         "\tlecup <handle> <min> <max> <latency> <timeout>\n"
3358         "\tOptions:\n"
3359         "\t    --handle=<0xXXXX>  LE connection handle\n"
3360         "\t    --min=<interval>   Range: 0x0006 to 0x0C80\n"
3361         "\t    --max=<interval>   Range: 0x0006 to 0x0C80\n"
3362         "\t    --latency=<range>  Slave latency. Range: 0x0000 to 0x03E8\n"
3363         "\t    --timeout=<time>   N * 10ms. Range: 0x000A to 0x0C80\n"
3364         "\n\t min/max range: 7.5ms to 4s. Multiply factor: 1.25ms"
3365         "\n\t timeout range: 100ms to 32.0s. Larger than max interval\n";
3366
3367 static void cmd_lecup(int dev_id, int argc, char **argv)
3368 {
3369         uint16_t handle = 0, min, max, latency, timeout;
3370         int opt, dd;
3371         int options = 0;
3372
3373         /* Aleatory valid values */
3374         min = 0x0C8;
3375         max = 0x0960;
3376         latency = 0x0007;
3377         timeout = 0x0C80;
3378
3379         for_each_opt(opt, lecup_options, NULL) {
3380                 switch (opt) {
3381                 case 'H':
3382                         handle = strtoul(optarg, NULL, 0);
3383                         break;
3384                 case 'm':
3385                         min = strtoul(optarg, NULL, 0);
3386                         break;
3387                 case 'M':
3388                         max = strtoul(optarg, NULL, 0);
3389                         break;
3390                 case 'l':
3391                         latency = strtoul(optarg, NULL, 0);
3392                         break;
3393                 case 't':
3394                         timeout = strtoul(optarg, NULL, 0);
3395                         break;
3396                 default:
3397                         printf("%s", lecup_help);
3398                         return;
3399                 }
3400
3401                 options = 1;
3402         }
3403
3404         if (options == 0) {
3405                 helper_arg(5, 5, &argc, &argv, lecup_help);
3406
3407                 handle = strtoul(argv[0], NULL, 0);
3408                 min = strtoul(argv[1], NULL, 0);
3409                 max = strtoul(argv[2], NULL, 0);
3410                 latency = strtoul(argv[3], NULL, 0);
3411                 timeout = strtoul(argv[4], NULL, 0);
3412         }
3413
3414         if (handle == 0) {
3415                 printf("%s", lecup_help);
3416                 return;
3417         }
3418
3419         if (dev_id < 0)
3420                 dev_id = hci_get_route(NULL);
3421
3422         dd = hci_open_dev(dev_id);
3423         if (dd < 0) {
3424                 fprintf(stderr, "HCI device open failed\n");
3425                 exit(1);
3426         }
3427
3428         if (hci_le_conn_update(dd, htobs(handle), htobs(min), htobs(max),
3429                                 htobs(latency), htobs(timeout), 5000) < 0) {
3430                 int err = -errno;
3431                 fprintf(stderr, "Could not change connection params: %s(%d)\n",
3432                                                         strerror(-err), -err);
3433         }
3434
3435         hci_close_dev(dd);
3436 }
3437
3438 static struct {
3439         char *cmd;
3440         void (*func)(int dev_id, int argc, char **argv);
3441         char *doc;
3442 } command[] = {
3443         { "dev",      cmd_dev,     "Display local devices"                },
3444         { "inq",      cmd_inq,     "Inquire remote devices"               },
3445         { "scan",     cmd_scan,    "Scan for remote devices"              },
3446         { "name",     cmd_name,    "Get name from remote device"          },
3447         { "info",     cmd_info,    "Get information from remote device"   },
3448         { "spinq",    cmd_spinq,   "Start periodic inquiry"               },
3449         { "epinq",    cmd_epinq,   "Exit periodic inquiry"                },
3450         { "cmd",      cmd_cmd,     "Submit arbitrary HCI commands"        },
3451 #ifdef __TIZEN_PATCH__
3452         { "acl",      cmd_data,    "Submit arbitrary ACL data"            },
3453 #endif
3454         { "con",      cmd_con,     "Display active connections"           },
3455         { "cc",       cmd_cc,      "Create connection to remote device"   },
3456         { "dc",       cmd_dc,      "Disconnect from remote device"        },
3457         { "sr",       cmd_sr,      "Switch master/slave role"             },
3458         { "cpt",      cmd_cpt,     "Change connection packet type"        },
3459         { "rssi",     cmd_rssi,    "Display connection RSSI"              },
3460         { "lq",       cmd_lq,      "Display link quality"                 },
3461         { "tpl",      cmd_tpl,     "Display transmit power level"         },
3462         { "afh",      cmd_afh,     "Display AFH channel map"              },
3463         { "lp",       cmd_lp,      "Set/display link policy settings"     },
3464         { "lst",      cmd_lst,     "Set/display link supervision timeout" },
3465         { "auth",     cmd_auth,    "Request authentication"               },
3466         { "enc",      cmd_enc,     "Set connection encryption"            },
3467         { "key",      cmd_key,     "Change connection link key"           },
3468         { "clkoff",   cmd_clkoff,  "Read clock offset"                    },
3469         { "clock",    cmd_clock,   "Read local or remote clock"           },
3470         { "lescan",   cmd_lescan,  "Start LE scan"                        },
3471         { "leinfo",   cmd_leinfo,  "Get LE remote information"            },
3472         { "lewladd",  cmd_lewladd, "Add device to LE White List"          },
3473         { "lewlrm",   cmd_lewlrm,  "Remove device from LE White List"     },
3474         { "lewlsz",   cmd_lewlsz,  "Read size of LE White List"           },
3475         { "lewlclr",  cmd_lewlclr, "Clear LE White List"                  },
3476         { "lerladd",  cmd_lerladd, "Add device to LE Resolving List"      },
3477         { "lerlrm",   cmd_lerlrm,  "Remove device from LE Resolving List" },
3478         { "lerlclr",  cmd_lerlclr, "Clear LE Resolving List"              },
3479         { "lerlsz",   cmd_lerlsz,  "Read size of LE Resolving List"       },
3480         { "lerlon",   cmd_lerlon,  "Enable LE Address Resolution"         },
3481         { "lerloff",  cmd_lerloff, "Disable LE Address Resolution"        },
3482         { "lecc",     cmd_lecc,    "Create a LE Connection"               },
3483         { "ledc",     cmd_ledc,    "Disconnect a LE Connection"           },
3484         { "lecup",    cmd_lecup,   "LE Connection Update"                 },
3485         { NULL, NULL, 0 }
3486 };
3487
3488 static void usage(void)
3489 {
3490         int i;
3491
3492         printf("hcitool - HCI Tool ver %s\n", VERSION);
3493         printf("Usage:\n"
3494                 "\thcitool [options] <command> [command parameters]\n");
3495         printf("Options:\n"
3496                 "\t--help\tDisplay help\n"
3497                 "\t-i dev\tHCI device\n");
3498         printf("Commands:\n");
3499         for (i = 0; command[i].cmd; i++)
3500                 printf("\t%-4s\t%s\n", command[i].cmd,
3501                 command[i].doc);
3502         printf("\n"
3503                 "For more information on the usage of each command use:\n"
3504                 "\thcitool <command> --help\n" );
3505 }
3506
3507 static struct option main_options[] = {
3508         { "help",       0, 0, 'h' },
3509         { "device",     1, 0, 'i' },
3510         { 0, 0, 0, 0 }
3511 };
3512
3513 int main(int argc, char *argv[])
3514 {
3515         int opt, i, dev_id = -1;
3516         bdaddr_t ba;
3517
3518         while ((opt=getopt_long(argc, argv, "+i:h", main_options, NULL)) != -1) {
3519                 switch (opt) {
3520                 case 'i':
3521                         dev_id = hci_devid(optarg);
3522                         if (dev_id < 0) {
3523                                 perror("Invalid device");
3524                                 exit(1);
3525                         }
3526                         break;
3527
3528                 case 'h':
3529                 default:
3530                         usage();
3531                         exit(0);
3532                 }
3533         }
3534
3535         argc -= optind;
3536         argv += optind;
3537         optind = 0;
3538
3539         if (argc < 1) {
3540                 usage();
3541                 exit(0);
3542         }
3543
3544         if (dev_id != -1 && hci_devba(dev_id, &ba) < 0) {
3545                 perror("Device is not available");
3546                 exit(1);
3547         }
3548
3549         for (i = 0; command[i].cmd; i++) {
3550                 if (strncmp(command[i].cmd,
3551                                 argv[0], strlen(command[i].cmd)))
3552                         continue;
3553
3554                 command[i].func(dev_id, argc, argv);
3555                 break;
3556         }
3557
3558         if (command[i].cmd == 0) {
3559                 fprintf(stderr, "Unknown command - \"%s\"\n", *argv);
3560                 exit(1);
3561         }
3562
3563         return 0;
3564 }