-
Notifications
You must be signed in to change notification settings - Fork 2
/
btmgmt.c
4762 lines (3918 loc) · 112 KB
/
btmgmt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2011 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define _GNU_SOURCE
#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <poll.h>
#include <getopt.h>
#include <stdbool.h>
#include <wordexp.h>
#include <ctype.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <bluetooth/sdp.h>
#include <bluetooth/sdp_lib.h>
#include "mgmt_main.h"
#include "uuid-helper.h"
#include "mainloop.h"
#include "io.h"
#include "util.h"
#include "mgmt.h"
#include "shell.h"
#define SCAN_TYPE_BREDR (1 << BDADDR_BREDR)
#define SCAN_TYPE_LE ((1 << BDADDR_LE_PUBLIC) | (1 << BDADDR_LE_RANDOM))
#define SCAN_TYPE_DUAL (SCAN_TYPE_BREDR | SCAN_TYPE_LE)
static struct mgmt *mgmt = NULL;
static uint16_t mgmt_index = MGMT_INDEX_NONE;
static bool discovery = false;
static bool resolve_names = true;
/* [AT] set show eir_data on/off */
static bool show_eir_data = false;
static struct {
uint16_t index;
uint16_t req;
struct mgmt_addr_info addr;
} prompt = {
.index = MGMT_INDEX_NONE,
};
static int pending_index = 0;
#ifndef MIN
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#endif
#define PROMPT_ON COLOR_BLUE "[mgmt]" COLOR_OFF "# "
static void set_index(const char *arg)
{
if (!arg || !strcmp(arg, "none") || !strcmp(arg, "any") ||
!strcmp(arg, "all"))
mgmt_index = MGMT_INDEX_NONE;
else if(strlen(arg) > 3 && !strncasecmp(arg, "hci", 3))
mgmt_index = atoi(&arg[3]);
else
mgmt_index = atoi(arg);
}
static void update_prompt(uint16_t index)
{
char str[32];
if (index == MGMT_INDEX_NONE)
snprintf(str, sizeof(str), "%s# ",
COLOR_BLUE "[mgmt]" COLOR_OFF);
else
snprintf(str, sizeof(str),
COLOR_BLUE "[hci%u]" COLOR_OFF "# ", index);
bt_shell_set_prompt(str);
}
#define print(fmt, arg...) do { \
bt_shell_printf(fmt "\n", ## arg); \
} while (0)
#define error(fmt, arg...) do { \
bt_shell_printf(COLOR_RED fmt "\n" COLOR_OFF, ## arg); \
} while (0)
static size_t hex2bin(const char *hexstr, uint8_t *buf, size_t buflen)
{
size_t i, len;
len = MIN((strlen(hexstr) / 2), buflen);
memset(buf, 0, len);
for (i = 0; i < len; i++)
sscanf(hexstr + (i * 2), "%02hhX", &buf[i]);
return len;
}
static size_t bin2hex(const uint8_t *buf, size_t buflen, char *str,
size_t strlen)
{
size_t i;
for (i = 0; i < buflen && i < (strlen / 2); i++)
sprintf(str + (i * 2), "%02x", buf[i]);
return i;
}
static void print_eir(const uint8_t *eir, uint16_t eir_len)
{
uint16_t parsed = 0;
char str[33];
while (parsed < eir_len - 1) {
uint8_t field_len = eir[0];
if (field_len == 0)
break;
parsed += field_len + 1;
if (parsed > eir_len)
break;
switch (eir[1]) {
case 0x01:
print("Flags: 0x%02x", eir[2]);
break;
case 0x0d:
print("Class of Device: 0x%02x%02x%02x",
eir[4], eir[3], eir[2]);
break;
case 0x0e:
bin2hex(eir + 2, 16, str, sizeof(str));
print("SSP Hash C-192: %s", str);
break;
case 0x0f:
bin2hex(eir + 2, 16, str, sizeof(str));
print("SSP Rand R-192: %s", str);
break;
case 0x1b:
ba2str((bdaddr_t *) (eir + 2), str);
print("LE Device Address: %s (%s)", str,
eir[8] ? "random" : "public");
break;
case 0x1c:
print("LE Role: 0x%02x", eir[2]);
break;
case 0x1d:
bin2hex(eir + 2, 16, str, sizeof(str));
print("SSP Hash C-256: %s", str);
break;
case 0x1e:
bin2hex(eir + 2, 16, str, sizeof(str));
print("SSP Rand R-256: %s", str);
break;
case 0x22:
bin2hex(eir + 2, 16, str, sizeof(str));
print("LE SC Confirmation Value: %s", str);
break;
case 0x23:
bin2hex(eir + 2, 16, str, sizeof(str));
print("LE SC Random Value: %s", str);
break;
default:
print("Type %u: %u byte%s", eir[1], field_len - 1,
(field_len - 1) == 1 ? "" : "s");
break;
}
eir += field_len + 1;
}
}
static bool load_identity(const char *path, struct mgmt_irk_info *irk)
{
char *addr, *key;
unsigned int type;
int n;
FILE *fp;
/* [AT] debug */
print("Loading IRK from file: %s", path);
fp = fopen(path, "r");
if (!fp) {
error("Failed to open identity file: %s", strerror(errno));
return false;
}
n = fscanf(fp, "%m[0-9a-f:] (type %u) %m[0-9a-f]", &addr, &type, &key);
fclose(fp);
if (n != 3)
return false;
/* [AT] debug */
print("Address: %s, IRK: %s", addr, key);
str2ba(addr, &irk->addr.bdaddr);
hex2bin(key, irk->val, sizeof(irk->val));
free(addr);
free(key);
switch (type) {
case 0:
irk->addr.type = BDADDR_LE_PUBLIC;
break;
case 1:
irk->addr.type = BDADDR_LE_RANDOM;
break;
default:
error("Invalid address type %u", type);
return false;
}
return true;
}
static void controller_error(uint16_t index, uint16_t len,
const void *param, void *user_data)
{
const struct mgmt_ev_controller_error *ev = param;
if (len < sizeof(*ev)) {
error("Too short (%u bytes) controller error event", len);
return;
}
print("hci%u error 0x%02x", index, ev->error_code);
}
static void index_added(uint16_t index, uint16_t len,
const void *param, void *user_data)
{
print("hci%u added", index);
}
static void index_removed(uint16_t index, uint16_t len,
const void *param, void *user_data)
{
print("hci%u removed", index);
}
static void unconf_index_added(uint16_t index, uint16_t len,
const void *param, void *user_data)
{
print("hci%u added (unconfigured)", index);
}
static void unconf_index_removed(uint16_t index, uint16_t len,
const void *param, void *user_data)
{
print("hci%u removed (unconfigured)", index);
}
static void ext_index_added(uint16_t index, uint16_t len,
const void *param, void *user_data)
{
const struct mgmt_ev_ext_index_added *ev = param;
print("hci%u added (type %u bus %u)", index, ev->type, ev->bus);
}
static void ext_index_removed(uint16_t index, uint16_t len,
const void *param, void *user_data)
{
const struct mgmt_ev_ext_index_removed *ev = param;
print("hci%u removed (type %u bus %u)", index, ev->type, ev->bus);
}
static const char *options_str[] = {
"external",
"public-address",
};
static const char *options2str(uint32_t options)
{
static char str[256];
unsigned i;
int off;
off = 0;
str[0] = '\0';
for (i = 0; i < NELEM(options_str); i++) {
if ((options & (1 << i)) != 0)
off += snprintf(str + off, sizeof(str) - off, "%s ",
options_str[i]);
}
return str;
}
static void new_config_options(uint16_t index, uint16_t len,
const void *param, void *user_data)
{
const uint32_t *ev = param;
if (len < sizeof(*ev)) {
error("Too short new_config_options event (%u)", len);
return;
}
print("hci%u new_config_options: %s", index, options2str(get_le32(ev)));
}
static const char *settings_str[] = {
"powered",
"connectable",
"fast-connectable",
"discoverable",
"bondable",
"link-security",
"ssp",
"br/edr",
"hs",
"le",
"advertising",
"secure-conn",
"debug-keys",
"privacy",
"configuration",
"static-addr",
};
static const char *settings2str(uint32_t settings)
{
static char str[256];
unsigned i;
int off;
off = 0;
str[0] = '\0';
for (i = 0; i < NELEM(settings_str); i++) {
if ((settings & (1 << i)) != 0)
off += snprintf(str + off, sizeof(str) - off, "%s ",
settings_str[i]);
}
return str;
}
static void new_settings(uint16_t index, uint16_t len,
const void *param, void *user_data)
{
const uint32_t *ev = param;
if (len < sizeof(*ev)) {
error("Too short new_settings event (%u)", len);
return;
}
print("hci%u new_settings: %s", index, settings2str(get_le32(ev)));
}
static void discovering(uint16_t index, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_ev_discovering *ev = param;
if (len < sizeof(*ev)) {
error("Too short (%u bytes) discovering event", len);
return;
}
print("hci%u type %u discovering %s", index, ev->type,
ev->discovering ? "on" : "off");
if (ev->discovering == 0 && discovery)
return bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
/*
#define MGMT_EV_NEW_IRK 0x0018
struct mgmt_ev_new_irk {
uint8_t store_hint;
bdaddr_t rpa;
struct mgmt_irk_info key;
} __packed;
struct mgmt_irk_info {
struct mgmt_addr_info addr;
uint8_t val[16];
} __packed;
*/
/* [AT]: save new IRK to disk (current directory). The IRK is saved in the format:
address (type type_of_addr) irk
For example
00:11:22:aa:bb:cc (type 0) 001122334455667799aabbccddeeff
*/
static void save_irk(char *addr, uint8_t hint, char *irk)
{
FILE *fp;
char *s=addr;
while(*s) {
*s=tolower(*s);
++s;
}
fp=fopen(addr,"w");
if(!fp) {
print("Failed writing IRK to file %s\n", addr);
return;
}
fprintf(fp,"%s (type %u) %s", addr, hint, irk);
fclose(fp);
print("IRK %s saved to file %s\n", irk, addr);
}
/* [AT]: added callback for new IRK event */
static void new_irk(uint16_t index, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_ev_new_irk *ev = param;
char addr[18];
char irk[33]="";
char s[4];
int i;
if (len != sizeof(*ev)) {
error("Invalid new_irk length (%u bytes)", len);
return;
}
/*
static size_t bin2hex(const uint8_t *buf, size_t buflen, char *str,
size_t strlen)
*/
/*
for(i=0; i<16; ++i) {
sprintf(s,"%2.2x", ev->key.val[i]);
strcat(irk, s);
}
*/
bin2hex(ev->key.val,16,irk,33);
ba2str(&ev->key.addr.bdaddr, addr);
print("hci%u new_irk %s store_hint %u irk %s",
index, addr, ev->store_hint, irk);
save_irk(addr, ev->store_hint, irk);
}
static void new_link_key(uint16_t index, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_ev_new_link_key *ev = param;
char addr[18];
if (len != sizeof(*ev)) {
error("Invalid new_link_key length (%u bytes)", len);
return;
}
ba2str(&ev->key.addr.bdaddr, addr);
print("hci%u new_link_key %s type 0x%02x pin_len %d store_hint %u",
index, addr, ev->key.type, ev->key.pin_len, ev->store_hint);
}
static const char *typestr(uint8_t type)
{
static const char *str[] = { "BR/EDR", "LE Public", "LE Random" };
if (type <= BDADDR_LE_RANDOM)
return str[type];
return "(unknown)";
}
static void connected(uint16_t index, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_ev_device_connected *ev = param;
uint16_t eir_len;
char addr[18];
if (len < sizeof(*ev)) {
error("Invalid connected event length (%u bytes)", len);
return;
}
eir_len = get_le16(&ev->eir_len);
if (len != sizeof(*ev) + eir_len) {
error("Invalid connected event length (%u != eir_len %u)",
len, eir_len);
return;
}
ba2str(&ev->addr.bdaddr, addr);
print("hci%u %s type %s connected eir_len %u", index, addr,
typestr(ev->addr.type), eir_len);
}
static void disconnected(uint16_t index, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_ev_device_disconnected *ev = param;
char addr[18];
uint8_t reason;
if (len < sizeof(struct mgmt_addr_info)) {
error("Invalid disconnected event length (%u bytes)", len);
return;
}
if (len < sizeof(*ev))
reason = MGMT_DEV_DISCONN_UNKNOWN;
else
reason = ev->reason;
ba2str(&ev->addr.bdaddr, addr);
print("hci%u %s type %s disconnected with reason %u",
index, addr, typestr(ev->addr.type), reason);
}
static void conn_failed(uint16_t index, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_ev_connect_failed *ev = param;
char addr[18];
if (len != sizeof(*ev)) {
error("Invalid connect_failed event length (%u bytes)", len);
return;
}
ba2str(&ev->addr.bdaddr, addr);
print("hci%u %s type %s connect failed (status 0x%02x, %s)",
index, addr, typestr(ev->addr.type), ev->status,
mgmt_errstr(ev->status));
}
static void auth_failed(uint16_t index, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_ev_auth_failed *ev = param;
char addr[18];
if (len != sizeof(*ev)) {
error("Invalid auth_failed event length (%u bytes)", len);
return;
}
ba2str(&ev->addr.bdaddr, addr);
print("hci%u %s auth failed with status 0x%02x (%s)",
index, addr, ev->status, mgmt_errstr(ev->status));
}
static void class_of_dev_changed(uint16_t index, uint16_t len,
const void *param, void *user_data)
{
const struct mgmt_ev_class_of_dev_changed *ev = param;
if (len != sizeof(*ev)) {
error("Invalid class_of_dev_changed length (%u bytes)", len);
return;
}
print("hci%u class of device changed: 0x%02x%02x%02x", index,
ev->dev_class[2], ev->dev_class[1], ev->dev_class[0]);
}
static void local_name_changed(uint16_t index, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_ev_local_name_changed *ev = param;
if (len != sizeof(*ev)) {
error("Invalid local_name_changed length (%u bytes)", len);
return;
}
print("hci%u name changed: %s", index, ev->name);
}
static void confirm_name_rsp(uint8_t status, uint16_t len,
const void *param, void *user_data)
{
const struct mgmt_rp_confirm_name *rp = param;
char addr[18];
if (len == 0 && status != 0) {
error("confirm_name failed with status 0x%02x (%s)", status,
mgmt_errstr(status));
return;
}
if (len != sizeof(*rp)) {
error("confirm_name rsp length %u instead of %zu",
len, sizeof(*rp));
return;
}
ba2str(&rp->addr.bdaddr, addr);
if (status != 0)
error("confirm_name for %s failed: 0x%02x (%s)",
addr, status, mgmt_errstr(status));
else
print("confirm_name succeeded for %s", addr);
}
static char *eir_get_name(const uint8_t *eir, uint16_t eir_len)
{
uint8_t parsed = 0;
if (eir_len < 2)
return NULL;
while (parsed < eir_len - 1) {
uint8_t field_len = eir[0];
if (field_len == 0)
break;
parsed += field_len + 1;
if (parsed > eir_len)
break;
/* Check for short of complete name */
if (eir[1] == 0x09 || eir[1] == 0x08)
return strndup((char *) &eir[2], field_len - 1);
eir += field_len + 1;
}
return NULL;
}
static unsigned int eir_get_flags(const uint8_t *eir, uint16_t eir_len)
{
uint8_t parsed = 0;
if (eir_len < 2)
return 0;
while (parsed < eir_len - 1) {
uint8_t field_len = eir[0];
if (field_len == 0)
break;
parsed += field_len + 1;
if (parsed > eir_len)
break;
/* Check for flags */
if (eir[1] == 0x01)
return eir[2];
eir += field_len + 1;
}
return 0;
}
/*
[AT] for reference
#define MGMT_EV_DEVICE_FOUND 0x0012
struct mgmt_ev_device_found {
struct mgmt_addr_info addr;
int8_t rssi;
uint32_t flags;
uint16_t eir_len;
uint8_t eir[0];
} __packed;
*/
/* [AT] print service data */
static void print_service_data(uint16_t eir_len, const uint8_t * eir)
{
int i;
char *str=(char*) malloc(eir_len * 2 + 1);
char b[3];
if(!show_eir_data)
return;
if(!eir) {
print("No service data\n");
return;
}
if(!str) {
print("Memory allocation failed in print_service_data");
return;
}
str[0]='\0';
i = 1+1+4+2; // skip: address type (1), rssi (1), flags (4), data length (2)
for(; i < eir_len; ++i) {
sprintf(b,"%2.2x", eir[i]);
strcat(str,b);
}
print("eir data: %s", str);
free(str);
}
static void device_found(uint16_t index, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_ev_device_found *ev = param;
struct mgmt *mgmt = user_data;
uint16_t eir_len;
uint32_t flags;
if (len < sizeof(*ev)) {
error("Too short device_found length (%u bytes)", len);
return;
}
flags = btohl(ev->flags);
eir_len = get_le16(&ev->eir_len);
if (len != sizeof(*ev) + eir_len) {
error("dev_found: expected %zu bytes, got %u bytes",
sizeof(*ev) + eir_len, len);
return;
}
if (discovery) {
char addr[18], *name;
ba2str(&ev->addr.bdaddr, addr);
print("hci%u dev_found: %s type %s rssi %d "
"flags 0x%04x ", index, addr,
typestr(ev->addr.type), ev->rssi, flags);
if (ev->addr.type != BDADDR_BREDR)
print("AD flags 0x%02x ",
eir_get_flags(ev->eir, eir_len));
name = eir_get_name(ev->eir, eir_len);
if (name)
print("name %s", name);
else
print("eir_len %u", eir_len);
free(name);
print_service_data(eir_len, ev->eir);
}
if (discovery && (flags & MGMT_DEV_FOUND_CONFIRM_NAME)) {
struct mgmt_cp_confirm_name cp;
memset(&cp, 0, sizeof(cp));
memcpy(&cp.addr, &ev->addr, sizeof(cp.addr));
if (resolve_names)
cp.name_known = 0;
else
cp.name_known = 1;
mgmt_reply(mgmt, MGMT_OP_CONFIRM_NAME, index, sizeof(cp), &cp,
confirm_name_rsp, NULL, NULL);
}
}
static void pin_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
if (status != 0) {
error("PIN Code reply failed with status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
print("PIN Reply successful");
}
static int mgmt_pin_reply(uint16_t index, const struct mgmt_addr_info *addr,
const char *pin, size_t len)
{
struct mgmt_cp_pin_code_reply cp;
memset(&cp, 0, sizeof(cp));
memcpy(&cp.addr, addr, sizeof(cp.addr));
cp.pin_len = len;
memcpy(cp.pin_code, pin, len);
return mgmt_reply(mgmt, MGMT_OP_PIN_CODE_REPLY, index,
sizeof(cp), &cp, pin_rsp, NULL, NULL);
}
static void pin_neg_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
if (status != 0) {
error("PIN Neg reply failed with status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
print("PIN Negative Reply successful");
}
static int mgmt_pin_neg_reply(uint16_t index, const struct mgmt_addr_info *addr)
{
struct mgmt_cp_pin_code_neg_reply cp;
memset(&cp, 0, sizeof(cp));
memcpy(&cp.addr, addr, sizeof(cp.addr));
return mgmt_reply(mgmt, MGMT_OP_PIN_CODE_NEG_REPLY, index,
sizeof(cp), &cp, pin_neg_rsp, NULL, NULL);
}
static void confirm_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
if (status != 0) {
error("User Confirm reply failed. status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
print("User Confirm Reply successful");
}
static int mgmt_confirm_reply(uint16_t index, const struct mgmt_addr_info *addr)
{
struct mgmt_cp_user_confirm_reply cp;
memset(&cp, 0, sizeof(cp));
memcpy(&cp.addr, addr, sizeof(*addr));
return mgmt_reply(mgmt, MGMT_OP_USER_CONFIRM_REPLY, index,
sizeof(cp), &cp, confirm_rsp, NULL, NULL);
}
static void confirm_neg_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
if (status != 0) {
error("Confirm Neg reply failed. status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
print("User Confirm Negative Reply successful");
}
static int mgmt_confirm_neg_reply(uint16_t index,
const struct mgmt_addr_info *addr)
{
struct mgmt_cp_user_confirm_reply cp;
memset(&cp, 0, sizeof(cp));
memcpy(&cp.addr, addr, sizeof(*addr));
return mgmt_reply(mgmt, MGMT_OP_USER_CONFIRM_NEG_REPLY, index,
sizeof(cp), &cp, confirm_neg_rsp, NULL, NULL);
}
static void passkey_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
if (status != 0) {
error("User Passkey reply failed. status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
print("User Passkey Reply successful");
}
static int mgmt_passkey_reply(uint16_t index, const struct mgmt_addr_info *addr,
uint32_t passkey)
{
struct mgmt_cp_user_passkey_reply cp;
memset(&cp, 0, sizeof(cp));
memcpy(&cp.addr, addr, sizeof(*addr));
put_le32(passkey, &cp.passkey);
return mgmt_reply(mgmt, MGMT_OP_USER_PASSKEY_REPLY, index,
sizeof(cp), &cp, passkey_rsp, NULL, NULL);
}
static void passkey_neg_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
if (status != 0) {
error("Passkey Neg reply failed. status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
print("User Passkey Negative Reply successful");
}
static int mgmt_passkey_neg_reply(uint16_t index,
const struct mgmt_addr_info *addr)
{
struct mgmt_cp_user_passkey_reply cp;
memset(&cp, 0, sizeof(cp));
memcpy(&cp.addr, addr, sizeof(*addr));
return mgmt_reply(mgmt, MGMT_OP_USER_PASSKEY_NEG_REPLY, index,
sizeof(cp), &cp, passkey_neg_rsp, NULL, NULL);
}
static void prompt_input(const char *input, void *user_data)
{
size_t len;
len = strlen(input);
switch (prompt.req) {
case MGMT_EV_PIN_CODE_REQUEST:
if (len)
mgmt_pin_reply(prompt.index, &prompt.addr, input, len);
else
mgmt_pin_neg_reply(prompt.index, &prompt.addr);
break;
case MGMT_EV_USER_PASSKEY_REQUEST:
if (strlen(input) > 0)
mgmt_passkey_reply(prompt.index, &prompt.addr,
atoi(input));
else
mgmt_passkey_neg_reply(prompt.index,
&prompt.addr);
break;
case MGMT_EV_USER_CONFIRM_REQUEST:
if (input[0] == 'y' || input[0] == 'Y')
mgmt_confirm_reply(prompt.index, &prompt.addr);
else
mgmt_confirm_neg_reply(prompt.index, &prompt.addr);
break;
}
}
static void ask(uint16_t index, uint16_t req, const struct mgmt_addr_info *addr,
const char *fmt, ...)
{
char msg[256];
va_list ap;
int off;
prompt.index = index;
prompt.req = req;
memcpy(&prompt.addr, addr, sizeof(*addr));
va_start(ap, fmt);
off = vsnprintf(msg, sizeof(msg), fmt, ap);
va_end(ap);