-
Notifications
You must be signed in to change notification settings - Fork 53
/
blueutil.m
1244 lines (1030 loc) · 39.6 KB
/
blueutil.m
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
// blueutil
//
// CLI for bluetooth on OSX: power, discoverable state, list, inquire devices, connect, info, …
// Uses private API from IOBluetooth framework (i.e. IOBluetoothPreference*()).
// https://github.com/toy/blueutil
//
// Originally written by Frederik Seiffert <[email protected]> http://www.frederikseiffert.de/blueutil/
//
// Copyright (c) 2011-2024 Ivan Kuchin. See <LICENSE.txt> for details.
#define VERSION "2.10.0"
#import <IOBluetooth/IOBluetooth.h>
#include <getopt.h>
#include <regex.h>
#include <sysexits.h>
// https://stackoverflow.com/a/12648993/96823
#define STRINGIFY_(x) #x
#define STRINGIFY(x) STRINGIFY_(x)
#define eprintf(...) fprintf(stderr, ##__VA_ARGS__)
void *assert_alloc(void *pointer) {
if (pointer == NULL) {
eprintf("%s\n", strerror(errno));
exit(EX_OSERR);
}
return pointer;
}
int assert_reg(int errcode, const regex_t *restrict preg, char *reason) {
if (errcode == 0 || errcode == REG_NOMATCH) return errcode;
size_t errbuf_size = regerror(errcode, preg, NULL, 0);
char *restrict errbuf = assert_alloc(malloc(errbuf_size));
regerror(errcode, preg, errbuf, errbuf_size);
eprintf("%s: %s\n", reason, errbuf);
exit(EX_SOFTWARE);
}
// private methods
int IOBluetoothPreferencesAvailable();
int IOBluetoothPreferenceGetControllerPowerState();
void IOBluetoothPreferenceSetControllerPowerState(int state);
int IOBluetoothPreferenceGetDiscoverableState();
void IOBluetoothPreferenceSetDiscoverableState(int state);
// short names
typedef int (*GetterFunc)();
typedef bool (*SetterFunc)(int);
enum state {
toggle = -1,
off = 0,
on = 1,
};
bool BTSetParamState(enum state state, GetterFunc getter, void (*setter)(int), const char *name) {
if (state == toggle) state = !getter();
if (state == getter()) return true;
setter(state);
for (int i = 0; i <= 100; i++) {
if (i) usleep(100000);
if (state == getter()) return true;
}
eprintf("Failed to switch bluetooth %s %s in 10 seconds\n", name, state ? "on" : "off");
return false;
}
#define BTAvaliable IOBluetoothPreferencesAvailable
#define BTPowerState IOBluetoothPreferenceGetControllerPowerState
bool BTSetPowerState(enum state state) {
return BTSetParamState(state, BTPowerState, IOBluetoothPreferenceSetControllerPowerState, "power");
}
#define BTDiscoverableState IOBluetoothPreferenceGetDiscoverableState
bool BTSetDiscoverableState(enum state state) {
return BTSetParamState(state, BTDiscoverableState, IOBluetoothPreferenceSetDiscoverableState, "discoverable");
}
void check_power_on_for(const char *command) {
if (BTPowerState()) return;
eprintf("Power is required to be on for %s command\n", command);
}
void usage(FILE *io) {
static const char *lines[] = {
"blueutil v" VERSION,
"",
"Usage:",
" blueutil [options]",
"",
"Without options outputs current state",
"",
" -p, --power output power state as 1 or 0",
" -p, --power STATE set power state",
" -d, --discoverable output discoverable state as 1 or 0",
" -d, --discoverable STATE set discoverable state",
"",
" --favourites, --favorites",
" list favourite devices; returns empty list starting with macOS 12/Monterey",
" --inquiry [T] inquiry devices in range, 10 seconds duration by default excluding time for name updates",
" --paired list paired devices",
" --recent [N] list recently used devices, 10 by default, 0 to list all; returns empty list starting with macOS 12/Monterey",
" --connected list connected devices",
"",
" --info ID show information about device",
" --is-connected ID connected state of device as 1 or 0",
" --connect ID create a connection to device",
" --disconnect ID close the connection to device",
" --pair ID [PIN] pair with device, optional PIN of up to 16 characters will be used instead of interactive input if requested in specific pair mode",
" --unpair ID EXPERIMENTAL unpair the device",
" --add-favourite ID, --add-favorite ID",
" add to favourites; does nothing starting with macOS 12/Monterey",
" --remove-favourite ID, --remove-favorite ID",
" remove from favourites; does nothing starting with macOS 12/Monterey",
"",
" --format FORMAT change output format of info and all listing commands",
"",
" --wait-connect ID [TIMEOUT]",
" EXPERIMENTAL wait for device to connect",
" --wait-disconnect ID [TIMEOUT]",
" EXPERIMENTAL wait for device to disconnect",
" --wait-rssi ID OP VALUE [PERIOD [TIMEOUT]]",
" EXPERIMENTAL wait for device RSSI value which is 0 for golden range, -129 if it cannot be read (e.g. device is disconnected)",
"",
" -h, --help this help",
" -v, --version show version",
"",
"STATE can be one of: 1, on, 0, off, toggle",
"ID can be either address in form xxxxxxxxxxxx, xx-xx-xx-xx-xx-xx or xx:xx:xx:xx:xx:xx, or name of device to search in paired or recent devices",
"OP can be one of: >, >=, <, <=, =, !=; or equivalents: gt, ge, lt, le, eq, ne",
"PERIOD is in seconds, defaults to 1",
"TIMEOUT is in seconds, default value 0 doesn't add timeout",
"FORMAT can be one of:",
" default - human readable text output not intended for consumption by scripts",
" new-default - human readable comma separated key-value pairs (EXPERIMENTAL, THE BEHAVIOUR MAY CHANGE)",
" json - compact JSON",
" json-pretty - pretty printed JSON",
"",
"Favourite devices and recent access date are not stored starting with macOS 12/Monterey, current time is returned for recent access date by framework instead.",
"",
"Due to possible problems, blueutil will refuse to run as root user (see https://github.com/toy/blueutil/issues/41).",
"Use environment variable BLUEUTIL_ALLOW_ROOT=1 to override (sudo BLUEUTIL_ALLOW_ROOT=1 blueutil …).",
"",
"Exit codes:",
" " STRINGIFY(EXIT_SUCCESS) " Success",
" " STRINGIFY(EXIT_FAILURE) " General failure",
" " STRINGIFY(EX_USAGE) " Wrong usage like missing or unexpected arguments, wrong parameters",
" " STRINGIFY(EX_UNAVAILABLE) " Bluetooth or interface not available",
" " STRINGIFY(EX_SOFTWARE) " Internal error",
" " STRINGIFY(EX_OSERR) " System error like shortage of memory",
" " STRINGIFY(EX_TEMPFAIL) " Timeout error",
};
for (size_t i = 0, _i = sizeof(lines) / sizeof(lines[0]); i < _i; i++) {
fprintf(io, "%s\n", lines[i]);
}
}
char *next_arg(int argc, char *argv[], bool required) {
if (optind < argc && NULL != argv[optind] && (required || '-' != argv[optind][0])) {
return argv[optind++];
} else {
return NULL;
}
}
char *next_reqarg(int argc, char *argv[]) {
return next_arg(argc, argv, true);
}
char *next_optarg(int argc, char *argv[]) {
return next_arg(argc, argv, false);
}
// getopt_long doesn't consume optional argument separated by space
// https://stackoverflow.com/a/32575314
void extend_optarg(int argc, char *argv[]) {
if (!optarg) optarg = next_optarg(argc, argv);
}
bool parse_state_arg(char *arg, enum state *state) {
if (0 == strcmp(arg, "1") || 0 == strcasecmp(arg, "on")) {
*state = on;
return true;
}
if (0 == strcmp(arg, "0") || 0 == strcasecmp(arg, "off")) {
*state = off;
return true;
}
if (0 == strcasecmp(arg, "toggle")) {
*state = toggle;
return true;
}
return false;
}
bool check_device_address_arg(char *arg) {
regex_t regex;
int result;
result = regcomp(®ex,
"^[0-9a-f]{2}([0-9a-f]{10}|(-[0-9a-f]{2}){5}|(:[0-9a-f]{2}){5})$",
REG_EXTENDED | REG_ICASE | REG_NOSUB);
assert_reg(result, ®ex, "Compiling device address regex");
result = regexec(®ex, arg, 0, NULL, 0);
assert_reg(result, ®ex, "Matching device address regex");
regfree(®ex);
return result == 0;
}
bool parse_unsigned_long_arg(char *arg, unsigned long *number) {
regex_t regex;
int result;
result = regcomp(®ex, "^[[:digit:]]+$", REG_EXTENDED | REG_NOSUB);
assert_reg(result, ®ex, "Compiling number regex");
result = regexec(®ex, arg, 0, NULL, 0);
assert_reg(result, ®ex, "Matching number regex");
regfree(®ex);
if (result == 0) {
*number = strtoul(arg, NULL, 10);
return true;
} else {
return false;
}
}
bool parse_signed_long_arg(char *arg, long *number) {
regex_t regex;
int result;
result = regcomp(®ex, "^-?[[:digit:]]+$", REG_EXTENDED | REG_NOSUB);
assert_reg(result, ®ex, "Compiling number regex");
result = regexec(®ex, arg, 0, NULL, 0);
assert_reg(result, ®ex, "Matching number regex");
regfree(®ex);
if (result == 0) {
*number = strtol(arg, NULL, 10);
return true;
} else {
return false;
}
}
IOBluetoothDevice *get_device(char *id) {
NSString *nsId = [NSString stringWithCString:id encoding:[NSString defaultCStringEncoding]];
IOBluetoothDevice *device = nil;
if (check_device_address_arg(id)) {
device = [IOBluetoothDevice deviceWithAddressString:nsId];
if (!device) {
eprintf("Device not found by address: %s\n", id);
exit(EXIT_FAILURE);
}
} else {
NSMutableArray *searchDevices = [NSMutableArray new];
NSArray *pairedDevices = [IOBluetoothDevice pairedDevices];
if (pairedDevices) {
[searchDevices addObjectsFromArray:pairedDevices];
}
NSArray *recentDevices = [IOBluetoothDevice recentDevices:0];
if (recentDevices) {
[searchDevices addObjectsFromArray:recentDevices];
}
if (searchDevices.count <= 0) {
eprintf("No paired or recent devices to search for: %s\n", id);
exit(EXIT_FAILURE);
}
NSArray *byName = [searchDevices filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name == %@", nsId]];
if (byName.count > 0) {
device = byName.firstObject;
}
if (!device) {
eprintf("Device not found by name: %s\n", id);
exit(EXIT_FAILURE);
}
}
return device;
}
void list_devices_default(NSArray *devices, bool first_only) {
for (IOBluetoothDevice *device in devices) {
printf("address: %s", [[device addressString] UTF8String]);
if ([device isConnected]) {
printf(", connected (%s, %d dBm)", [device isIncoming] ? "slave" : "master", [device rawRSSI]);
} else {
printf(", not connected");
}
printf(", %s", [device isFavorite] ? "favourite" : "not favourite");
printf(", %s", [device isPaired] ? "paired" : "not paired");
printf(", name: \"%s\"", [device name] ? [[device name] UTF8String] : "-");
printf(", recent access date: %s",
[device recentAccessDate] ? [[[device recentAccessDate] description] UTF8String] : "-");
printf("\n");
if (first_only) break;
}
}
void list_devices_new_default(NSArray *devices, bool first_only) {
const char *separator = first_only ? "\n" : ", ";
for (IOBluetoothDevice *device in devices) {
printf("address: %s%s", [[device addressString] UTF8String], separator);
printf("recent access: %s%s",
[device recentAccessDate] ? [[[device recentAccessDate] description] UTF8String] : "-",
separator);
printf("favourite: %s%s", [device isFavorite] ? "yes" : "no", separator);
printf("paired: %s%s", [device isPaired] ? "yes" : "no", separator);
printf("connected: %s%s", [device isConnected] ? ([device isIncoming] ? "slave" : "master") : "no", separator);
printf("rssi: %s%s",
[device isConnected] ? [[NSString stringWithFormat:@"%d", [device RSSI]] UTF8String] : "-",
separator);
printf("raw rssi: %s%s",
[device isConnected] ? [[NSString stringWithFormat:@"%d", [device rawRSSI]] UTF8String] : "-",
separator);
printf("name: %s\n", [device name] ? [[device name] UTF8String] : "-");
if (first_only) break;
}
}
void list_devices_json(NSArray *devices, bool first_only, bool pretty) {
NSMutableArray *descriptions = [NSMutableArray arrayWithCapacity:[devices count]];
@autoreleasepool {
// https://stackoverflow.com/a/16254918/96823
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];
[dateFormatter setCalendar:[NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian]];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
for (IOBluetoothDevice *device in devices) {
NSMutableDictionary *description = [NSMutableDictionary dictionaryWithDictionary:@{
@"address": [device addressString],
@"name": [device name] ? [device name] : [NSNull null],
@"recentAccessDate": [device recentAccessDate] ? [dateFormatter stringFromDate:[device recentAccessDate]]
: [NSNull null],
@"favourite": [device isFavorite] ? @(YES) : @(NO),
@"paired": [device isPaired] ? @(YES) : @(NO),
@"connected": [device isConnected] ? @(YES) : @(NO),
}];
if ([device isConnected]) {
description[@"slave"] = [device isIncoming] ? @(YES) : @(NO);
description[@"RSSI"] = [NSNumber numberWithChar:[device RSSI]];
description[@"rawRSSI"] = [NSNumber numberWithChar:[device rawRSSI]];
}
[descriptions addObject:description];
}
}
NSOutputStream *stdout = [NSOutputStream outputStreamToFileAtPath:@"/dev/stdout" append:NO];
[stdout open];
id object = first_only ? [descriptions firstObject] : descriptions;
NSJSONWritingOptions options = pretty ? NSJSONWritingPrettyPrinted : 0;
[NSJSONSerialization writeJSONObject:object toStream:stdout options:options error:NULL];
if (pretty) {
[stdout write:(const uint8_t *)"\n" maxLength:1];
}
[stdout close];
}
void list_devices_json_default(NSArray *devices, bool first_only) {
list_devices_json(devices, first_only, false);
}
void list_devices_json_pretty(NSArray *devices, bool first_only) {
list_devices_json(devices, first_only, true);
}
typedef void (*FormatterFunc)(NSArray *, bool);
bool parse_output_formatter(char *arg, FormatterFunc *formatter) {
if (0 == strcasecmp(arg, "default")) {
*formatter = list_devices_default;
return true;
}
if (0 == strcasecmp(arg, "new-default")) {
*formatter = list_devices_new_default;
return true;
}
if (0 == strcasecmp(arg, "json")) {
*formatter = list_devices_json_default;
return true;
}
if (0 == strcasecmp(arg, "json-pretty")) {
*formatter = list_devices_json_pretty;
return true;
}
return false;
}
@interface DeviceInquiryRunLoopStopper : NSObject <IOBluetoothDeviceInquiryDelegate>
@end
@implementation DeviceInquiryRunLoopStopper
- (void)deviceInquiryComplete:(__unused IOBluetoothDeviceInquiry *)sender
error:(__unused IOReturn)error
aborted:(__unused BOOL)aborted {
CFRunLoopStop(CFRunLoopGetCurrent());
}
@end
static inline bool is_caseabbr(const char *name, const char *str) {
size_t length = strlen(str);
if (length < 1) length = 1;
return strncasecmp(name, str, length) == 0;
}
const char *hci_error_descriptions[] = {
[0x01] = "Unknown HCI Command",
[0x02] = "No Connection",
[0x03] = "Hardware Failure",
[0x04] = "Page Timeout",
[0x05] = "Authentication Failure",
[0x06] = "Key Missing",
[0x07] = "Memory Full",
[0x08] = "Connection Timeout",
[0x09] = "Max Number of Connections",
[0x0a] = "Max Number of SCO Connections to a Device",
[0x0b] = "ACL Connection Already Exists",
[0x0c] = "Command Disallowed",
[0x0d] = "Host Rejected Limited Resources",
[0x0e] = "Host Rejected Security Reasons",
[0x0f] = "Host Rejected Remote Device Is Personal / Host Rejected Unacceptable Device Address (2.0+)",
[0x10] = "Host Timeout",
[0x11] = "Unsupported Feature or Parameter Value",
[0x12] = "Invalid HCI Command Parameters",
[0x13] = "Other End Terminated Connection User Ended",
[0x14] = "Other End Terminated Connection Low Resources",
[0x15] = "Other End Terminated Connection About to Power Off",
[0x16] = "Connection Terminated by Local Host",
[0x17] = "Repeated Attempts",
[0x18] = "Pairing Not Allowed",
[0x19] = "Unknown LMP PDU",
[0x1a] = "Unsupported Remote Feature",
[0x1b] = "SCO Offset Rejected",
[0x1c] = "SCO Interval Rejected",
[0x1d] = "SCO Air Mode Rejected",
[0x1e] = "Invalid LMP Parameters",
[0x1f] = "Unspecified Error",
[0x20] = "Unsupported LMP Parameter Value",
[0x21] = "Role Change Not Allowed",
[0x22] = "LMP Response Timeout",
[0x23] = "LMP Error Transaction Collision",
[0x24] = "LMP PDU Not Allowed",
[0x25] = "Encryption Mode Not Acceptable",
[0x26] = "Unit Key Used",
[0x27] = "QoS Not Supported",
[0x28] = "Instant Passed",
[0x29] = "Pairing With Unit Key Not Supported",
[0x2a] = "Different Transaction Collision",
[0x2c] = "QoS Unacceptable Parameter",
[0x2d] = "QoS Rejected",
[0x2e] = "Channel Classification Not Supported",
[0x2f] = "Insufficient Security",
[0x30] = "Parameter Out of Mandatory Range",
[0x31] = "Role Switch Pending",
[0x34] = "Reserved Slot Violation",
[0x35] = "Role Switch Failed",
[0x36] = "Extended Inquiry Response Too Large",
[0x37] = "Secure Simple Pairing Not Supported by Host",
[0x38] = "Host Busy Pairing",
[0x39] = "Connection Rejected Due to No Suitable Channel Found",
[0x3a] = "Controller Busy",
[0x3b] = "Unacceptable Connection Interval",
[0x3c] = "Directed Advertising Timeout",
[0x3d] = "Connection Terminated Due to MIC Failure",
[0x3e] = "Connection Failed to Be Established",
[0x3f] = "MAC Connection Failed",
[0x40] = "Coarse Clock Adjustment Rejected",
};
@interface DevicePairDelegate : NSObject <IOBluetoothDevicePairDelegate>
@property (readonly) IOReturn errorCode;
@property char *requestedPin;
@end
@implementation DevicePairDelegate
- (const char *)errorDescription {
if (_errorCode >= 0 && (unsigned)_errorCode < sizeof(hci_error_descriptions) / sizeof(hci_error_descriptions[0]) &&
hci_error_descriptions[_errorCode]) {
return hci_error_descriptions[_errorCode];
} else {
return "UNKNOWN ERROR";
}
}
- (void)devicePairingFinished:(__unused id)sender error:(IOReturn)error {
_errorCode = error;
CFRunLoopStop(CFRunLoopGetCurrent());
}
- (void)devicePairingPINCodeRequest:(id)sender {
BluetoothPINCode pinCode;
ByteCount pinCodeSize;
if (_requestedPin) {
eprintf("Input pin %.16s on \"%s\" (%s)\n",
_requestedPin,
[[[sender device] name] UTF8String],
[[[sender device] addressString] UTF8String]);
pinCodeSize = strlen(_requestedPin);
if (pinCodeSize > 16) pinCodeSize = 16;
strncpy((char *)pinCode.data, _requestedPin, pinCodeSize);
} else {
eprintf("Type pin code (up to 16 characters) for \"%s\" (%s) and press Enter: ",
[[[sender device] name] UTF8String],
[[[sender device] addressString] UTF8String]);
uint input_size = 16 + 2;
char input[input_size];
fgets(input, input_size, stdin);
input[strcspn(input, "\n")] = 0;
pinCodeSize = strlen(input);
strncpy((char *)pinCode.data, input, pinCodeSize);
}
[sender replyPINCode:pinCodeSize PINCode:&pinCode];
}
- (void)devicePairingUserConfirmationRequest:(id)sender numericValue:(BluetoothNumericValue)numericValue {
eprintf("Does \"%s\" (%s) display number %06u (yes/no)? ",
[[[sender device] name] UTF8String],
[[[sender device] addressString] UTF8String],
numericValue);
uint input_size = 3 + 2;
char input[input_size];
fgets(input, input_size, stdin);
input[strcspn(input, "\n")] = 0;
if (is_caseabbr("yes", input)) {
[sender replyUserConfirmation:YES];
return;
}
if (is_caseabbr("no", input)) {
[sender replyUserConfirmation:NO];
return;
}
}
- (void)devicePairingUserPasskeyNotification:(id)sender passkey:(BluetoothPasskey)passkey {
eprintf("Input passkey %06u on \"%s\" (%s)\n",
passkey,
[[[sender device] name] UTF8String],
[[[sender device] addressString] UTF8String]);
}
@end
#define OP_FUNC(name, operator) \
bool op_##name(const long a, const long b) { \
return a operator b; \
}
OP_FUNC(gt, >);
OP_FUNC(ge, >=);
OP_FUNC(lt, <);
OP_FUNC(le, <=);
OP_FUNC(eq, ==);
OP_FUNC(ne, !=);
typedef bool (*OpFunc)(const long a, const long b);
#define PARSE_OP_ARG_MATCHER(name, operator) \
if (0 == strcmp(arg, #name) || 0 == strcmp(arg, #operator)) { \
*op = op_##name; \
*op_name = #operator; \
return true; \
}
bool parse_op_arg(const char *arg, OpFunc *op, const char **op_name) {
PARSE_OP_ARG_MATCHER(gt, >);
PARSE_OP_ARG_MATCHER(ge, >=);
PARSE_OP_ARG_MATCHER(lt, <);
PARSE_OP_ARG_MATCHER(le, <=);
PARSE_OP_ARG_MATCHER(eq, =);
PARSE_OP_ARG_MATCHER(ne, !=);
return false;
}
@interface DeviceNotificationRunLoopStopper : NSObject
@end
@implementation DeviceNotificationRunLoopStopper {
IOBluetoothDevice *expectedDevice;
}
- (id)initWithExpectedDevice:(IOBluetoothDevice *)device {
expectedDevice = device;
return self;
}
- (void)notification:(IOBluetoothUserNotification *)notification fromDevice:(IOBluetoothDevice *)device {
if ([expectedDevice isEqual:device]) {
[notification unregister];
CFRunLoopStop(CFRunLoopGetCurrent());
}
}
@end
struct args_state_get {
GetterFunc func;
};
struct args_state_set {
SetterFunc func;
enum state state;
};
struct args_inquiry {
unsigned long duration;
};
struct args_recent {
unsigned long max;
};
struct args_device_id {
char *device_id;
};
struct args_pair {
char *device_id;
char *pin_code;
};
struct args_wait_connection_change {
char *device_id;
bool wait_connect;
unsigned long timeout;
};
struct args_wait_rssi {
char *device_id;
OpFunc op;
const char *op_name;
long value;
unsigned long period;
unsigned long timeout;
};
typedef int (^cmd)(void *args);
struct cmd_with_args {
cmd cmd;
void *args;
};
#define CMD_CHUNK 8
struct cmd_with_args *cmds = NULL;
size_t cmd_n = 0, cmd_reserved = 0;
#define ALLOC_ARGS(type) struct args_##type *args = assert_alloc(malloc(sizeof(struct args_##type)))
void add_cmd(void *args, cmd cmd) {
if (cmd_n >= cmd_reserved) {
cmd_reserved += CMD_CHUNK;
cmds = assert_alloc(reallocf(cmds, sizeof(struct cmd_with_args) * cmd_reserved));
}
cmds[cmd_n++] = (struct cmd_with_args){.cmd = cmd, .args = args};
}
FormatterFunc list_devices = list_devices_default;
int main(int argc, char *argv[]) {
if (geteuid() == 0) {
char *allow_root = getenv("BLUEUTIL_ALLOW_ROOT");
if (NULL == allow_root || 0 != strcmp(allow_root, "1")) {
eprintf("Error: Not running as root user without environment variable BLUEUTIL_ALLOW_ROOT=1\n");
return EXIT_FAILURE;
}
}
if (!BTAvaliable()) {
eprintf("Error: Bluetooth not available!\n");
return EX_UNAVAILABLE;
}
if (argc == 1) {
printf("Power: %d\nDiscoverable: %d\n", BTPowerState(), BTDiscoverableState());
return EXIT_SUCCESS;
}
enum {
arg_power = 'p',
arg_discoverable = 'd',
arg_help = 'h',
arg_version = 'v',
arg_favourites = 256,
arg_inquiry,
arg_paired,
arg_recent,
arg_connected,
arg_info,
arg_is_connected,
arg_connect,
arg_disconnect,
arg_pair,
arg_unpair,
arg_add_favourite,
arg_remove_favourite,
arg_format,
arg_wait_connect,
arg_wait_disconnect,
arg_wait_rssi,
};
const char *optstring = "p::d::hv";
// clang-format off
static struct option long_options[] = {
{"power", optional_argument, NULL, arg_power},
{"discoverable", optional_argument, NULL, arg_discoverable},
{"favourites", no_argument, NULL, arg_favourites},
{"favorites", no_argument, NULL, arg_favourites},
{"inquiry", optional_argument, NULL, arg_inquiry},
{"paired", no_argument, NULL, arg_paired},
{"recent", optional_argument, NULL, arg_recent},
{"connected", no_argument, NULL, arg_connected},
{"info", required_argument, NULL, arg_info},
{"is-connected", required_argument, NULL, arg_is_connected},
{"connect", required_argument, NULL, arg_connect},
{"disconnect", required_argument, NULL, arg_disconnect},
{"pair", required_argument, NULL, arg_pair},
{"unpair", required_argument, NULL, arg_unpair},
{"add-favourite", required_argument, NULL, arg_add_favourite},
{"add-favorite", required_argument, NULL, arg_add_favourite},
{"remove-favourite", required_argument, NULL, arg_remove_favourite},
{"remove-favorite", required_argument, NULL, arg_remove_favourite},
{"format", required_argument, NULL, arg_format},
{"wait-connect", required_argument, NULL, arg_wait_connect},
{"wait-disconnect", required_argument, NULL, arg_wait_disconnect},
{"wait-rssi", required_argument, NULL, arg_wait_rssi},
{"help", no_argument, NULL, arg_help},
{"version", no_argument, NULL, arg_version},
{NULL, 0, NULL, 0}
};
// clang-format on
int arg;
while ((arg = getopt_long(argc, argv, optstring, long_options, NULL)) != -1) {
switch (arg) {
case arg_power:
case arg_discoverable: {
extend_optarg(argc, argv);
if (optarg) {
ALLOC_ARGS(state_set);
args->func = arg == arg_power ? BTSetPowerState : BTSetDiscoverableState;
if (!parse_state_arg(optarg, &args->state)) {
eprintf("Unexpected value: %s\n", optarg);
return EX_USAGE;
}
add_cmd(args, ^int(void *_args) {
struct args_state_set *args = (struct args_state_set *)_args;
return args->func(args->state) ? EXIT_SUCCESS : EX_TEMPFAIL;
});
} else {
ALLOC_ARGS(state_get);
args->func = arg == arg_power ? BTPowerState : BTDiscoverableState;
add_cmd(args, ^int(void *_args) {
struct args_state_get *args = (struct args_state_get *)_args;
printf("%d\n", args->func());
return EXIT_SUCCESS;
});
}
} break;
case arg_favourites: {
add_cmd(NULL, ^int(__unused void *_args) {
list_devices([IOBluetoothDevice favoriteDevices], false);
return EXIT_SUCCESS;
});
} break;
case arg_paired: {
add_cmd(NULL, ^int(__unused void *_args) {
list_devices([IOBluetoothDevice pairedDevices], false);
return EXIT_SUCCESS;
});
} break;
case arg_inquiry: {
ALLOC_ARGS(inquiry);
extend_optarg(argc, argv);
args->duration = 10;
if (optarg) {
if (!parse_unsigned_long_arg(optarg, &args->duration)) {
eprintf("Expected numeric duration, got: %s\n", optarg);
return EX_USAGE;
}
}
add_cmd(args, ^int(void *_args) {
struct args_inquiry *args = (struct args_inquiry *)_args;
check_power_on_for("inquiry");
@autoreleasepool {
DeviceInquiryRunLoopStopper *stopper = [[[DeviceInquiryRunLoopStopper alloc] init] autorelease];
IOBluetoothDeviceInquiry *inquirer = [IOBluetoothDeviceInquiry inquiryWithDelegate:stopper];
[inquirer start];
// inquiry length seems to be ingored starting with Monterey
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, args->duration * NSEC_PER_SEC),
dispatch_get_main_queue(),
^{
[inquirer stop];
});
CFRunLoopRun();
list_devices([inquirer foundDevices], false);
}
return EXIT_SUCCESS;
});
} break;
case arg_recent: {
ALLOC_ARGS(recent);
extend_optarg(argc, argv);
args->max = 10;
if (optarg) {
if (!parse_unsigned_long_arg(optarg, &args->max)) {
eprintf("Expected numeric count, got: %s\n", optarg);
return EX_USAGE;
}
}
add_cmd(args, ^int(void *_args) {
struct args_recent *args = (struct args_recent *)_args;
list_devices([IOBluetoothDevice recentDevices:args->max], false);
return EXIT_SUCCESS;
});
} break;
case arg_connected: {
add_cmd(NULL, ^int(__unused void *_args) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"isConnected == YES"];
list_devices([[IOBluetoothDevice pairedDevices] filteredArrayUsingPredicate:predicate], false);
return EXIT_SUCCESS;
});
} break;
case arg_info: {
ALLOC_ARGS(device_id);
args->device_id = optarg;
add_cmd(args, ^int(void *_args) {
struct args_device_id *args = (struct args_device_id *)_args;
list_devices(@[get_device(args->device_id)], true);
return EXIT_SUCCESS;
});
} break;
case arg_is_connected: {
ALLOC_ARGS(device_id);
args->device_id = optarg;
add_cmd(args, ^int(void *_args) {
struct args_device_id *args = (struct args_device_id *)_args;
printf("%d\n", [get_device(args->device_id) isConnected] ? 1 : 0);
return EXIT_SUCCESS;
});
} break;
case arg_connect: {
ALLOC_ARGS(device_id);
args->device_id = optarg;
add_cmd(args, ^int(void *_args) {
struct args_device_id *args = (struct args_device_id *)_args;
check_power_on_for("connect");
if ([get_device(args->device_id) openConnection] != kIOReturnSuccess) {
eprintf("Failed to connect \"%s\"\n", args->device_id);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
});
} break;
case arg_disconnect: {
ALLOC_ARGS(device_id);
args->device_id = optarg;
add_cmd(args, ^int(void *_args) {
struct args_device_id *args = (struct args_device_id *)_args;
check_power_on_for("disconnect");
@autoreleasepool {
IOBluetoothDevice *device = get_device(args->device_id);
DeviceNotificationRunLoopStopper *stopper =
[[[DeviceNotificationRunLoopStopper alloc] initWithExpectedDevice:device] autorelease];
[device registerForDisconnectNotification:stopper selector:@selector(notification:fromDevice:)];
if ([device closeConnection] != kIOReturnSuccess) {
eprintf("Failed to disconnect \"%s\"\n", args->device_id);
return EXIT_FAILURE;
}
CFRunLoopRun();
}
return EXIT_SUCCESS;
});
} break;
case arg_add_favourite: {
ALLOC_ARGS(device_id);
args->device_id = optarg;
add_cmd(args, ^int(void *_args) {
struct args_device_id *args = (struct args_device_id *)_args;
if ([get_device(args->device_id) addToFavorites] != kIOReturnSuccess) {
eprintf("Failed to add \"%s\" to favourites\n", args->device_id);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
});
} break;
case arg_remove_favourite: {
ALLOC_ARGS(device_id);
args->device_id = optarg;
add_cmd(args, ^int(void *_args) {
struct args_device_id *args = (struct args_device_id *)_args;
if ([get_device(args->device_id) removeFromFavorites] != kIOReturnSuccess) {
eprintf("Failed to remove \"%s\" from favourites\n", args->device_id);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
});
} break;