lin
2025-07-30 fcd736bf35fd93b563e9bbf594f2aa7b62028cc9
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
//
//  Copyright 2015 Google, Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at:
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//
 
#include <iostream>
#include <string>
 
#include <base/at_exit.h>
#include <base/command_line.h>
#include <base/logging.h>
#include <base/macros.h>
#include <base/strings/string_number_conversions.h>
#include <base/strings/string_split.h>
#include <base/strings/string_util.h>
#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <binder/ProcessState.h>
 
#include <android/bluetooth/BnBluetoothCallback.h>
#include <android/bluetooth/BnBluetoothGattClientCallback.h>
#include <android/bluetooth/BnBluetoothLeAdvertiserCallback.h>
#include <android/bluetooth/BnBluetoothLeScannerCallback.h>
#include <android/bluetooth/BnBluetoothLowEnergyCallback.h>
#include <android/bluetooth/IBluetooth.h>
#include <android/bluetooth/IBluetoothGattClient.h>
#include <android/bluetooth/IBluetoothLeAdvertiser.h>
#include <android/bluetooth/IBluetoothLeScanner.h>
#include <android/bluetooth/IBluetoothLowEnergy.h>
#include <bluetooth/adapter_state.h>
#include <bluetooth/low_energy_constants.h>
#include <bluetooth/scan_filter.h>
#include <bluetooth/scan_settings.h>
#include <bluetooth/uuid.h>
 
using namespace std;
 
using android::sp;
using android::String8;
using android::String16;
using android::binder::Status;
using android::OK;
using android::getService;
 
using android::bluetooth::IBluetooth;
using android::bluetooth::IBluetoothGattClient;
using android::bluetooth::IBluetoothLeAdvertiser;
using android::bluetooth::IBluetoothLeScanner;
using android::bluetooth::IBluetoothLowEnergy;
 
namespace {
 
#define COLOR_OFF "\x1B[0m"
#define COLOR_RED "\x1B[0;91m"
#define COLOR_GREEN "\x1B[0;92m"
#define COLOR_YELLOW "\x1B[0;93m"
#define COLOR_BLUE "\x1B[0;94m"
#define COLOR_MAGENTA "\x1B[0;95m"
#define COLOR_BOLDGRAY "\x1B[1;30m"
#define COLOR_BOLDWHITE "\x1B[1;37m"
#define COLOR_BOLDYELLOW "\x1B[1;93m"
#define CLEAR_LINE "\x1B[2K"
 
#define CHECK_ARGS_COUNT(args, op, num, msg) \
  if (!((args).size() op num)) {             \
    PrintError(msg);                         \
    return;                                  \
  }
#define CHECK_NO_ARGS(args) \
  CHECK_ARGS_COUNT(args, ==, 0, "Expected no arguments")
 
// TODO(armansito): Clean up this code. Right now everything is in this
// monolithic file. We should organize this into different classes for command
// handling, console output/printing, callback handling, etc.
// (See http://b/23387611)
 
// Used to synchronize the printing of the command-line prompt and incoming
// Binder callbacks.
std::atomic_bool showing_prompt(false);
 
// The registered IBluetoothLowEnergy client handle. If |ble_registering| is
// true then an operation to register the client is in progress.
std::atomic_bool ble_registering(false);
std::atomic_int ble_client_id(0);
 
// The registered IBluetoothLeAdvertiser handle. If |ble_advertiser_registering|
// is true then an operation to register the advertiser is in progress.
const int invalid_advertiser_id = -1;
std::atomic_bool ble_advertiser_registering(false);
std::atomic_int ble_advertiser_id(invalid_advertiser_id);
 
// The registered IBluetoothLeScanner handle. If |ble_scanner_registering| is
// true then an operation to register the scanner is in progress.
std::atomic_bool ble_scanner_registering(false);
std::atomic_int ble_scanner_id(0);
 
// The registered IBluetoothGattClient client handle. If |gatt_registering| is
// true then an operation to register the client is in progress.
std::atomic_bool gatt_registering(false);
std::atomic_int gatt_client_id(0);
 
// True if we should dump the scan record bytes for incoming scan results.
std::atomic_bool dump_scan_record(false);
 
// True if the remote process has died and we should exit.
std::atomic_bool should_exit(false);
 
std::string kServiceName = "bluetooth-service";
 
void PrintPrompt() { cout << COLOR_BLUE "[FCLI] " COLOR_OFF << flush; }
 
void PrintError(const string& message) {
  cout << COLOR_RED << message << COLOR_OFF << endl;
}
 
void PrintOpStatus(const std::string& op, bool status) {
  cout << COLOR_BOLDWHITE << op << " status: " COLOR_OFF
       << (status ? (COLOR_GREEN "success") : (COLOR_RED "failure"))
       << COLOR_OFF << endl;
}
 
inline void BeginAsyncOut() {
  if (showing_prompt.load()) cout << CLEAR_LINE << "\r";
}
 
inline void EndAsyncOut() {
  std::flush(cout);
  if (showing_prompt.load())
    PrintPrompt();
  else
    cout << endl;
}
 
class CLIBluetoothCallback : public android::bluetooth::BnBluetoothCallback {
 public:
  CLIBluetoothCallback() = default;
  ~CLIBluetoothCallback() override = default;
 
  // IBluetoothCallback overrides:
  Status OnBluetoothStateChange(int32_t prev_state,
                                int32_t new_state) override {
    BeginAsyncOut();
    cout << COLOR_BOLDWHITE "Adapter state changed: " COLOR_OFF << COLOR_MAGENTA
         << AdapterStateToString(bluetooth::AdapterState(prev_state))
         << COLOR_OFF << COLOR_BOLDWHITE " -> " COLOR_OFF << COLOR_BOLDYELLOW
         << AdapterStateToString(bluetooth::AdapterState(new_state))
         << COLOR_OFF;
    EndAsyncOut();
 
    return Status::ok();
  }
 
  Status OnSspRequest(const String16& device_address,
                      const String16& device_name, int32_t cod,
                      int32_t pairing_variant, int32_t pass_key) override {
    // no-op
    return Status::ok();
  }
 
  Status OnGetBondedDevices(
      int32_t status,
      const ::std::vector<String16>& device_addresses) override {
    BeginAsyncOut();
    std::cout << "Bonded devices:\n";
    for (const auto& device_address : device_addresses) {
      std::cout << "    " << device_address << "\n";
    }
    EndAsyncOut();
    return Status::ok();
  }
 
  Status OnBondStateChanged(int32_t status, const String16& device_address,
                            int32_t state) override {
    BeginAsyncOut();
    std::cout << COLOR_BOLDWHITE "Device address: " << COLOR_BOLDYELLOW "["
              << device_address << " bond state: " << state << " ] "
              << COLOR_BOLDWHITE "- status: "
              << (status == 0 ? "SUCCESS" : "FAIL") << COLOR_OFF;
    EndAsyncOut();
    return Status::ok();
  }
 
  Status OnGetRemoteDeviceProperties(
      int32_t status, const String16& device_address,
      const android::bluetooth::BluetoothRemoteDeviceProps& props) override {
    // no-op
    return Status::ok();
  }
 
  Status OnDeviceFound(
      const android::bluetooth::BluetoothRemoteDeviceProps& props) override {
    // no-op
    return Status::ok();
  }
 
  Status OnDeviceConnectionStateChanged(const String16& device_address,
                                        bool connected) override {
    // no-op
    return Status::ok();
  }
 
  Status OnScanEnableChanged(bool scan_enabled) override {
    // no-op
    return Status::ok();
  }
 
 private:
  DISALLOW_COPY_AND_ASSIGN(CLIBluetoothCallback);
};
 
class CLIBluetoothLowEnergyCallback
    : public android::bluetooth::BnBluetoothLowEnergyCallback {
 public:
  CLIBluetoothLowEnergyCallback() = default;
  ~CLIBluetoothLowEnergyCallback() override = default;
 
  // IBluetoothLowEnergyCallback overrides:
  Status OnClientRegistered(int status, int client_id) override {
    BeginAsyncOut();
    if (status != bluetooth::BLE_STATUS_SUCCESS) {
      PrintError("Failed to register BLE client");
    } else {
      ble_client_id = client_id;
      cout << COLOR_BOLDWHITE "Registered BLE client with ID: " COLOR_OFF
           << COLOR_GREEN << client_id << COLOR_OFF;
    }
    EndAsyncOut();
 
    ble_registering = false;
    return Status::ok();
  }
 
  Status OnConnectionState(int status, int client_id, const String16& address,
                           bool connected) override {
    BeginAsyncOut();
    cout << COLOR_BOLDWHITE "Connection state: " << COLOR_BOLDYELLOW "["
         << address << " connected: " << (connected ? "true" : "false") << " ] "
         << COLOR_BOLDWHITE "- status: " << status
         << COLOR_BOLDWHITE " - client_id: " << client_id << COLOR_OFF;
    EndAsyncOut();
    return Status::ok();
  }
 
  Status OnMtuChanged(int status, const String16& address, int mtu) override {
    BeginAsyncOut();
    cout << COLOR_BOLDWHITE "MTU changed: " << COLOR_BOLDYELLOW "[" << address
         << " ] " << COLOR_BOLDWHITE " - status: " << status
         << COLOR_BOLDWHITE " - mtu: " << mtu << COLOR_OFF;
    EndAsyncOut();
    return Status::ok();
  }
 
 private:
  DISALLOW_COPY_AND_ASSIGN(CLIBluetoothLowEnergyCallback);
};
 
class CLIBluetoothLeAdvertiserCallback
    : public android::bluetooth::BnBluetoothLeAdvertiserCallback {
 public:
  CLIBluetoothLeAdvertiserCallback() = default;
  ~CLIBluetoothLeAdvertiserCallback() override = default;
 
  // IBluetoothLowEnergyCallback overrides:
  Status OnAdvertiserRegistered(int status, int advertiser_id) override {
    BeginAsyncOut();
    if (status != bluetooth::BLE_STATUS_SUCCESS) {
      PrintError("Failed to register BLE advertiser");
    } else {
      ble_advertiser_id = advertiser_id;
      cout << COLOR_BOLDWHITE "Registered BLE advertiser with ID: " COLOR_OFF
           << COLOR_GREEN << advertiser_id << COLOR_OFF;
    }
    EndAsyncOut();
 
    ble_advertiser_registering = false;
    return Status::ok();
  }
 
  Status OnMultiAdvertiseCallback(
      int status, bool is_start,
      const android::bluetooth::AdvertiseSettings& /* settings */) {
    BeginAsyncOut();
    std::string op = is_start ? "start" : "stop";
 
    PrintOpStatus("Advertising " + op, status == bluetooth::BLE_STATUS_SUCCESS);
    EndAsyncOut();
    return Status::ok();
  }
 
 private:
  DISALLOW_COPY_AND_ASSIGN(CLIBluetoothLeAdvertiserCallback);
};
 
class CLIBluetoothLeScannerCallback
    : public android::bluetooth::BnBluetoothLeScannerCallback {
 public:
  CLIBluetoothLeScannerCallback() = default;
  ~CLIBluetoothLeScannerCallback() override = default;
 
  // IBluetoothLowEnergyCallback overrides:
  Status OnScannerRegistered(int status, int scanner_id) override {
    BeginAsyncOut();
    if (status != bluetooth::BLE_STATUS_SUCCESS) {
      PrintError("Failed to register BLE client");
    } else {
      ble_scanner_id = scanner_id;
      cout << COLOR_BOLDWHITE "Registered BLE client with ID: " COLOR_OFF
           << COLOR_GREEN << scanner_id << COLOR_OFF;
    }
    EndAsyncOut();
 
    ble_scanner_registering = false;
    return Status::ok();
  }
 
  Status OnScanResult(
      const android::bluetooth::ScanResult& scan_result) override {
    BeginAsyncOut();
    cout << COLOR_BOLDWHITE "Scan result: " << COLOR_BOLDYELLOW "["
         << scan_result.device_address() << "] "
         << COLOR_BOLDWHITE "- RSSI: " << scan_result.rssi() << COLOR_OFF;
 
    if (dump_scan_record) {
      cout << " - Record: "
           << base::HexEncode(scan_result.scan_record().data(),
                              scan_result.scan_record().size());
    }
    EndAsyncOut();
    return Status::ok();
  }
 
 private:
  DISALLOW_COPY_AND_ASSIGN(CLIBluetoothLeScannerCallback);
};
 
class CLIGattClientCallback
    : public android::bluetooth::BnBluetoothGattClientCallback {
 public:
  CLIGattClientCallback() = default;
  ~CLIGattClientCallback() override = default;
 
  // IBluetoothGattClientCallback overrides:
  Status OnClientRegistered(int status, int client_id) override {
    BeginAsyncOut();
    if (status != bluetooth::BLE_STATUS_SUCCESS) {
      PrintError("Failed to register GATT client");
    } else {
      gatt_client_id = client_id;
      cout << COLOR_BOLDWHITE "Registered GATT client with ID: " COLOR_OFF
           << COLOR_GREEN << client_id << COLOR_OFF;
    }
    EndAsyncOut();
 
    gatt_registering = false;
    return Status::ok();
  }
 
 private:
  DISALLOW_COPY_AND_ASSIGN(CLIGattClientCallback);
};
 
void PrintCommandStatus(bool status) { PrintOpStatus("Command", status); }
 
void PrintFieldAndValue(const string& field, const string& value) {
  cout << COLOR_BOLDWHITE << field << ": " << COLOR_BOLDYELLOW << value
       << COLOR_OFF << endl;
}
 
void PrintFieldAndBoolValue(const string& field, bool value) {
  PrintFieldAndValue(field, (value ? "true" : "false"));
}
 
void HandleDisable(IBluetooth* bt_iface, const vector<string>& args) {
  CHECK_NO_ARGS(args);
  bool status;
  bt_iface->Disable(&status);
  PrintCommandStatus(status);
}
 
void HandleEnable(IBluetooth* bt_iface, const vector<string>& args) {
  bool status;
  bt_iface->Enable(&status);
  PrintCommandStatus(status);
}
 
void HandleGetState(IBluetooth* bt_iface, const vector<string>& args) {
  CHECK_NO_ARGS(args);
 
  int32_t st;
  bt_iface->GetState(&st);
  bluetooth::AdapterState state = static_cast<bluetooth::AdapterState>(st);
  PrintFieldAndValue("Adapter state", bluetooth::AdapterStateToString(state));
}
 
void HandleIsEnabled(IBluetooth* bt_iface, const vector<string>& args) {
  CHECK_NO_ARGS(args);
  bool enabled;
  bt_iface->IsEnabled(&enabled);
  PrintFieldAndBoolValue("Adapter enabled", enabled);
}
 
void HandleGetLocalAddress(IBluetooth* bt_iface, const vector<string>& args) {
  CHECK_NO_ARGS(args);
  String16 address;
  bt_iface->GetAddress(&address);
  PrintFieldAndValue("Adapter address", std::string(String8(address).string()));
}
 
void HandleSetLocalName(IBluetooth* bt_iface, const vector<string>& args) {
  CHECK_ARGS_COUNT(args, >=, 1, "No name was given");
 
  std::string name;
  for (const auto& arg : args) name += arg + " ";
 
  base::TrimWhitespaceASCII(name, base::TRIM_TRAILING, &name);
 
  bool status;
  bt_iface->SetName(String16(String8(name.c_str())), &status);
  PrintCommandStatus(status);
}
 
void HandleGetLocalName(IBluetooth* bt_iface, const vector<string>& args) {
  CHECK_NO_ARGS(args);
  String16 name;
  bt_iface->GetName(&name);
  PrintFieldAndValue("Adapter name", std::string(String8(name).string()));
}
 
void HandleAdapterInfo(IBluetooth* bt_iface, const vector<string>& args) {
  CHECK_NO_ARGS(args);
 
  cout << COLOR_BOLDWHITE "Adapter Properties: " COLOR_OFF << endl;
 
  String16 address;
  bt_iface->GetAddress(&address);
  PrintFieldAndValue("\tAddress", std::string(String8(address).string()));
 
  int adapter_state;
  bt_iface->GetState(&adapter_state);
  PrintFieldAndValue("\tState",
                     bluetooth::AdapterStateToString(
                         static_cast<bluetooth::AdapterState>(adapter_state)));
 
  String16 name;
  bt_iface->GetName(&name);
  PrintFieldAndValue("\tName", std::string(String8(name).string()));
 
  bool multi_adv;
  bt_iface->IsMultiAdvertisementSupported(&multi_adv);
  PrintFieldAndBoolValue("\tMulti-Adv. supported", multi_adv);
}
 
void HandleSupportsMultiAdv(IBluetooth* bt_iface, const vector<string>& args) {
  CHECK_NO_ARGS(args);
 
  bool multi_adv;
  bt_iface->IsMultiAdvertisementSupported(&multi_adv);
  PrintFieldAndBoolValue("Multi-advertisement support", multi_adv);
}
 
void HandleRegisterBLEAdvertiser(IBluetooth* bt_iface,
                                 const vector<string>& args) {
  CHECK_NO_ARGS(args);
 
  if (ble_advertiser_registering.load()) {
    PrintError("In progress");
    return;
  }
 
  if (ble_advertiser_id.load() != invalid_advertiser_id) {
    PrintError("Already registered");
    return;
  }
 
  sp<IBluetoothLeAdvertiser> ble_advertiser_iface;
  bt_iface->GetLeAdvertiserInterface(&ble_advertiser_iface);
  if (!ble_advertiser_iface.get()) {
    PrintError("Failed to obtain handle to Bluetooth Le Advertiser interface");
    return;
  }
 
  bool status;
  ble_advertiser_iface->RegisterAdvertiser(
      new CLIBluetoothLeAdvertiserCallback(), &status);
  ble_advertiser_registering = status;
  PrintCommandStatus(status);
}
 
void HandleUnregisterBLEAdvertiser(IBluetooth* bt_iface,
                                   const vector<string>& args) {
  CHECK_NO_ARGS(args);
 
  if (ble_advertiser_id.load() == invalid_advertiser_id) {
    PrintError("Not registered");
    return;
  }
 
  sp<IBluetoothLeAdvertiser> ble_advertiser_iface;
  bt_iface->GetLeAdvertiserInterface(&ble_advertiser_iface);
  if (!ble_advertiser_iface.get()) {
    PrintError("Failed to obtain handle to Bluetooth Low Energy interface");
    return;
  }
 
  ble_advertiser_iface->UnregisterAdvertiser(ble_advertiser_id.load());
  ble_advertiser_id = invalid_advertiser_id;
  PrintCommandStatus(true);
}
 
void HandleRegisterBLE(IBluetooth* bt_iface, const vector<string>& args) {
  CHECK_NO_ARGS(args);
 
  if (ble_registering.load()) {
    PrintError("In progress");
    return;
  }
 
  if (ble_client_id.load()) {
    PrintError("Already registered");
    return;
  }
 
  sp<IBluetoothLowEnergy> ble_iface;
  bt_iface->GetLowEnergyInterface(&ble_iface);
  if (!ble_iface.get()) {
    PrintError("Failed to obtain handle to Bluetooth Low Energy interface");
    return;
  }
 
  bool status;
  ble_iface->RegisterClient(new CLIBluetoothLowEnergyCallback(), &status);
  ble_registering = status;
  PrintCommandStatus(status);
}
 
void HandleUnregisterBLE(IBluetooth* bt_iface, const vector<string>& args) {
  CHECK_NO_ARGS(args);
 
  if (!ble_client_id.load()) {
    PrintError("Not registered");
    return;
  }
 
  sp<IBluetoothLowEnergy> ble_iface;
  bt_iface->GetLowEnergyInterface(&ble_iface);
  if (!ble_iface.get()) {
    PrintError("Failed to obtain handle to Bluetooth Low Energy interface");
    return;
  }
 
  ble_iface->UnregisterClient(ble_client_id.load());
  ble_client_id = 0;
  PrintCommandStatus(true);
}
 
void HandleUnregisterAllBLE(IBluetooth* bt_iface, const vector<string>& args) {
  CHECK_NO_ARGS(args);
 
  sp<IBluetoothLowEnergy> ble_iface;
  bt_iface->GetLowEnergyInterface(&ble_iface);
  if (!ble_iface.get()) {
    PrintError("Failed to obtain handle to Bluetooth Low Energy interface");
    return;
  }
 
  ble_iface->UnregisterAll();
  PrintCommandStatus(true);
}
 
void HandleRegisterGATT(IBluetooth* bt_iface, const vector<string>& args) {
  CHECK_NO_ARGS(args);
 
  if (gatt_registering.load()) {
    PrintError("In progress");
    return;
  }
 
  if (gatt_client_id.load()) {
    PrintError("Already registered");
    return;
  }
 
  sp<IBluetoothGattClient> gatt_iface;
  bt_iface->GetGattClientInterface(&gatt_iface);
  if (!gatt_iface.get()) {
    PrintError("Failed to obtain handle to Bluetooth GATT Client interface");
    return;
  }
 
  bool status;
  gatt_iface->RegisterClient(new CLIGattClientCallback(), &status);
  gatt_registering = status;
  PrintCommandStatus(status);
}
 
void HandleUnregisterGATT(IBluetooth* bt_iface, const vector<string>& args) {
  CHECK_NO_ARGS(args);
 
  if (!gatt_client_id.load()) {
    PrintError("Not registered");
    return;
  }
 
  sp<IBluetoothGattClient> gatt_iface;
  bt_iface->GetGattClientInterface(&gatt_iface);
  if (!gatt_iface.get()) {
    PrintError("Failed to obtain handle to Bluetooth GATT Client interface");
    return;
  }
 
  gatt_iface->UnregisterClient(gatt_client_id.load());
  gatt_client_id = 0;
  PrintCommandStatus(true);
}
 
void HandleStartAdv(IBluetooth* bt_iface, const vector<string>& args) {
  bool include_name = false;
  bool include_tx_power = false;
  bool connectable = false;
  bool set_manufacturer_data = false;
  bool set_uuid = false;
  bluetooth::Uuid uuid;
 
  for (auto iter = args.begin(); iter != args.end(); ++iter) {
    const std::string& arg = *iter;
    if (arg == "-n")
      include_name = true;
    else if (arg == "-t")
      include_tx_power = true;
    else if (arg == "-c")
      connectable = true;
    else if (arg == "-m")
      set_manufacturer_data = true;
    else if (arg == "-u") {
      // This flag has a single argument.
      ++iter;
      if (iter == args.end()) {
        PrintError("Expected a Uuid after -u");
        return;
      }
 
      std::string uuid_str = *iter;
      bool is_valid = false;
      uuid = bluetooth::Uuid::FromString(uuid_str, &is_valid);
      if (!is_valid) {
        PrintError("Invalid Uuid: " + uuid_str);
        return;
      }
 
      set_uuid = true;
    } else if (arg == "-h") {
      static const char kUsage[] =
          "Usage: start-adv [flags]\n"
          "\n"
          "Flags:\n"
          "\t-n\tInclude device name\n"
          "\t-t\tInclude TX power\n"
          "\t-c\tSend connectable adv. packets (default is non-connectable)\n"
          "\t-m\tInclude random manufacturer data\n"
          "\t-h\tShow this help message\n";
      cout << kUsage << endl;
      return;
    } else {
      PrintError("Unrecognized option: " + arg);
      return;
    }
  }
 
  if (ble_advertiser_id.load() == invalid_advertiser_id) {
    PrintError("BLE advertiser not registered");
    return;
  }
 
  sp<IBluetoothLeAdvertiser> ble_advertiser_iface;
  bt_iface->GetLeAdvertiserInterface(&ble_advertiser_iface);
  if (!ble_advertiser_iface.get()) {
    PrintError("Failed to obtain handle to Bluetooth Le Advertiser interface");
    return;
  }
 
  std::vector<uint8_t> data;
  if (set_manufacturer_data) {
    data = {{0x07, bluetooth::kEIRTypeManufacturerSpecificData, 0xe0, 0x00, 'T',
             'e', 's', 't'}};
  }
 
  if (set_uuid) {
    // Determine the type and length bytes.
    int uuid_size = uuid.GetShortestRepresentationSize();
    uint8_t type;
    if (uuid_size == bluetooth::Uuid::kNumBytes128)
      type = bluetooth::kEIRTypeComplete128BitUuids;
    else if (uuid_size == bluetooth::Uuid::kNumBytes32)
      type = bluetooth::kEIRTypeComplete32BitUuids;
    else if (uuid_size == bluetooth::Uuid::kNumBytes16)
      type = bluetooth::kEIRTypeComplete16BitUuids;
    else
      NOTREACHED() << "Unexpected size: " << uuid_size;
 
    data.push_back(uuid_size + 1);
    data.push_back(type);
 
    auto uuid_bytes = uuid.To128BitLE();
    int index = (uuid_size == 16) ? 0 : 12;
    data.insert(data.end(), uuid_bytes.data() + index,
                uuid_bytes.data() + index + uuid_size);
  }
 
  base::TimeDelta timeout;
 
  bluetooth::AdvertiseSettings settings(
      bluetooth::AdvertiseSettings::MODE_LOW_POWER, timeout,
      bluetooth::AdvertiseSettings::TX_POWER_LEVEL_MEDIUM, connectable);
 
  if (include_tx_power) {
    data.push_back(0x02);
    data.push_back(bluetooth::kEIRTypeTxPower);
    data.push_back(0x00);
  }
 
  bluetooth::AdvertiseData adv_data(data);
 
  if (include_name) {
    String16 name_param;
    bt_iface->GetName(&name_param);
    std::string name(String8(name_param).string());
    data.push_back(name.length() + 1);
    data.push_back(bluetooth::kEIRTypeCompleteLocalName);
    data.insert(data.begin(), name.c_str(), name.c_str() + name.length());
  }
 
  bluetooth::AdvertiseData scan_rsp;
 
  bool status;
  ble_advertiser_iface->StartMultiAdvertising(
      ble_advertiser_id.load(), adv_data, scan_rsp, settings, &status);
  PrintCommandStatus(status);
}
 
void HandleStopAdv(IBluetooth* bt_iface, const vector<string>& args) {
  if (ble_advertiser_id.load() == invalid_advertiser_id) {
    PrintError("BLE advertiser not registered");
    return;
  }
 
  sp<IBluetoothLeAdvertiser> ble_advertiser_iface;
  bt_iface->GetLeAdvertiserInterface(&ble_advertiser_iface);
  if (!ble_advertiser_iface.get()) {
    PrintError("Failed to obtain handle to Bluetooth Low Energy interface");
    return;
  }
 
  bool status;
  ble_advertiser_iface->StopMultiAdvertising(ble_advertiser_id.load(), &status);
  PrintCommandStatus(status);
}
 
void HandleConnect(IBluetooth* bt_iface, const vector<string>& args) {
  string address;
 
  if (args.size() != 1) {
    PrintError("Expected MAC address as only argument");
    return;
  }
 
  address = args[0];
 
  if (!ble_client_id.load()) {
    PrintError("BLE not registered");
    return;
  }
 
  sp<IBluetoothLowEnergy> ble_iface;
  bt_iface->GetLowEnergyInterface(&ble_iface);
  if (!ble_iface.get()) {
    PrintError("Failed to obtain handle to Bluetooth Low Energy interface");
    return;
  }
 
  bool status;
  ble_iface->Connect(ble_client_id.load(),
                     String16(address.c_str(), address.length()),
                     false /*  is_direct */, &status);
 
  PrintCommandStatus(status);
}
 
void HandleDisconnect(IBluetooth* bt_iface, const vector<string>& args) {
  string address;
 
  if (args.size() != 1) {
    PrintError("Expected MAC address as only argument");
    return;
  }
 
  address = args[0];
 
  if (!ble_client_id.load()) {
    PrintError("BLE not registered");
    return;
  }
 
  sp<IBluetoothLowEnergy> ble_iface;
  bt_iface->GetLowEnergyInterface(&ble_iface);
  if (!ble_iface.get()) {
    PrintError("Failed to obtain handle to Bluetooth Low Energy interface");
    return;
  }
 
  bool status;
  ble_iface->Disconnect(ble_client_id.load(),
                        String16(address.c_str(), address.length()), &status);
  PrintCommandStatus(status);
}
 
void HandleSetMtu(IBluetooth* bt_iface, const vector<string>& args) {
  string address;
  int mtu;
 
  if (args.size() != 2) {
    PrintError("Usage: set-mtu [address] [mtu]");
    return;
  }
 
  address = args[0];
  mtu = std::stoi(args[1]);
 
  if (mtu < 23) {
    PrintError("MTU must be 23 or larger");
    return;
  }
 
  if (!ble_client_id.load()) {
    PrintError("BLE not registered");
    return;
  }
 
  sp<IBluetoothLowEnergy> ble_iface;
  bt_iface->GetLowEnergyInterface(&ble_iface);
  if (!ble_iface.get()) {
    PrintError("Failed to obtain handle to Bluetooth Low Energy interface");
    return;
  }
 
  bool status;
  ble_iface->SetMtu(ble_client_id.load(),
                    String16(address.c_str(), address.length()), mtu, &status);
  PrintCommandStatus(status);
}
 
void HandleRegisterBLEScanner(IBluetooth* bt_iface,
                              const vector<string>& args) {
  CHECK_NO_ARGS(args);
 
  if (ble_scanner_registering.load()) {
    PrintError("In progress");
    return;
  }
 
  if (ble_scanner_id.load()) {
    PrintError("Already registered");
    return;
  }
 
  sp<IBluetoothLeScanner> ble_scanner_iface;
  bt_iface->GetLeScannerInterface(&ble_scanner_iface);
  if (!ble_scanner_iface.get()) {
    PrintError("Failed to obtain handle to Bluetooth LE Scanner interface");
    return;
  }
 
  bool status;
  ble_scanner_iface->RegisterScanner(new CLIBluetoothLeScannerCallback(),
                                     &status);
  ble_scanner_registering = status;
  PrintCommandStatus(status);
}
 
void HandleUnregisterBLEScanner(IBluetooth* bt_iface,
                                const vector<string>& args) {
  CHECK_NO_ARGS(args);
 
  if (!ble_scanner_id.load()) {
    PrintError("Not registered");
    return;
  }
 
  sp<IBluetoothLeScanner> ble_scanner_iface;
  bt_iface->GetLeScannerInterface(&ble_scanner_iface);
  if (!ble_scanner_iface.get()) {
    PrintError("Failed to obtain handle to Bluetooth LE scanner interface");
    return;
  }
 
  ble_scanner_iface->UnregisterScanner(ble_scanner_id.load());
  ble_scanner_id = 0;
  PrintCommandStatus(true);
}
 
void HandleStartLeScan(IBluetooth* bt_iface, const vector<string>& args) {
  if (!ble_client_id.load()) {
    PrintError("BLE not registered");
    return;
  }
 
  for (const auto& arg : args) {
    if (arg == "-d") {
      dump_scan_record = true;
    } else if (arg == "-h") {
      static const char kUsage[] =
          "Usage: start-le-scan [flags]\n"
          "\n"
          "Flags:\n"
          "\t-d\tDump scan record\n"
          "\t-h\tShow this help message\n";
      cout << kUsage << endl;
      return;
    }
  }
 
  sp<IBluetoothLeScanner> ble_scanner_iface;
  bt_iface->GetLeScannerInterface(&ble_scanner_iface);
  if (!ble_scanner_iface.get()) {
    PrintError("Failed to obtain handle to Bluetooth LE scanner interface");
    return;
  }
 
  bluetooth::ScanSettings settings;
  std::vector<android::bluetooth::ScanFilter> filters;
 
  bool status;
  ble_scanner_iface->StartScan(ble_scanner_id.load(), settings, filters,
                               &status);
  PrintCommandStatus(status);
}
 
void HandleStopLeScan(IBluetooth* bt_iface, const vector<string>& args) {
  if (!ble_client_id.load()) {
    PrintError("BLE not registered");
    return;
  }
 
  sp<IBluetoothLeScanner> ble_scanner_iface;
  bt_iface->GetLeScannerInterface(&ble_scanner_iface);
  if (!ble_scanner_iface.get()) {
    PrintError("Failed to obtain handle to Bluetooth LE scanner interface");
    return;
  }
 
  bool status;
  ble_scanner_iface->StopScan(ble_scanner_id.load(), &status);
  PrintCommandStatus(status);
}
 
void HandleHelp(IBluetooth* bt_iface, const vector<string>& args);
 
struct {
  string command;
  void (*func)(IBluetooth*, const vector<string>& args);
  string help;
} kCommandMap[] = {
    {"help", HandleHelp, "\t\t\tDisplay this message"},
    {"disable", HandleDisable, "\t\t\tDisable Bluetooth"},
    {"enable", HandleEnable, "\t\t\tEnable Bluetooth (-h for options)"},
    {"get-state", HandleGetState, "\t\tGet the current adapter state"},
    {"is-enabled", HandleIsEnabled, "\t\tReturn if Bluetooth is enabled"},
    {"get-local-address", HandleGetLocalAddress,
     "\tGet the local adapter address"},
    {"set-local-name", HandleSetLocalName, "\t\tSet the local adapter name"},
    {"get-local-name", HandleGetLocalName, "\t\tGet the local adapter name"},
    {"adapter-info", HandleAdapterInfo, "\t\tPrint adapter properties"},
    {"supports-multi-adv", HandleSupportsMultiAdv,
     "\tWhether multi-advertisement is currently supported"},
    {"register-le-advertiser", HandleRegisterBLEAdvertiser,
     "\t\tRegister with the Bluetooth Low Energy Advertiser interface"},
    {"unregister-le-advertiser", HandleUnregisterBLEAdvertiser,
     "\t\tUnregister from the Bluetooth LE Advertiser interface"},
    {"register-ble", HandleRegisterBLE,
     "\t\tRegister with the Bluetooth Low Energy interface"},
    {"unregister-ble", HandleUnregisterBLE,
     "\t\tUnregister from the Bluetooth Low Energy interface"},
    {"unregister-all-ble", HandleUnregisterAllBLE,
     "\tUnregister all clients from the Bluetooth Low Energy interface"},
    {"register-gatt", HandleRegisterGATT,
     "\t\tRegister with the Bluetooth GATT Client interface"},
    {"unregister-gatt", HandleUnregisterGATT,
     "\t\tUnregister from the Bluetooth GATT Client interface"},
    {"connect-le", HandleConnect, "\t\tConnect to LE device (-h for options)"},
    {"disconnect-le", HandleDisconnect,
     "\t\tDisconnect LE device (-h for options)"},
    {"set-mtu", HandleSetMtu, "\t\tSet MTU (-h for options)"},
    {"start-adv", HandleStartAdv, "\t\tStart advertising (-h for options)"},
    {"stop-adv", HandleStopAdv, "\t\tStop advertising"},
    {"register-le-scanner", HandleRegisterBLEScanner,
     "\t\tRegister with the Bluetooth Low Energy scanner interface"},
    {"unregister-le-scanner", HandleUnregisterBLEScanner,
     "\t\tUnregister from the Bluetooth LE scanner interface"},
    {"start-le-scan", HandleStartLeScan,
     "\t\tStart LE device scan (-h for options)"},
    {"stop-le-scan", HandleStopLeScan, "\t\tStop LE device scan"},
    {},
};
 
void HandleHelp(IBluetooth* /* bt_iface */, const vector<string>& /* args */) {
  cout << endl;
  for (int i = 0; kCommandMap[i].func; i++)
    cout << "\t" << kCommandMap[i].command << kCommandMap[i].help << endl;
  cout << endl;
}
 
const char kExecuteLong[] = "exec";
const char kExecuteShort[] = "e";
 
bool ExecuteCommand(const sp<IBluetooth>& bt_iface, std::string& command) {
  vector<string> args = base::SplitString(command, " ", base::TRIM_WHITESPACE,
                                          base::SPLIT_WANT_ALL);
 
  if (args.empty()) return true;
 
  // The first argument is the command while the remaining are what we pass to
  // the handler functions.
  command = args[0];
  args.erase(args.begin());
 
  for (int i = 0; kCommandMap[i].func; i++) {
    if (command == kCommandMap[i].command) {
      kCommandMap[i].func(bt_iface.get(), args);
      return true;
    }
  }
 
  cout << "Unrecognized command: " << command << endl;
  return false;
}
 
}  // namespace
 
class BluetoothDeathRecipient : public android::IBinder::DeathRecipient {
 public:
  BluetoothDeathRecipient() = default;
  ~BluetoothDeathRecipient() override = default;
 
  // android::IBinder::DeathRecipient override:
  void binderDied(const android::wp<android::IBinder>& /* who */) override {
    BeginAsyncOut();
    cout << COLOR_BOLDWHITE "The Bluetooth daemon has died" COLOR_OFF << endl;
    cout << "\nPress 'ENTER' to exit.";
    EndAsyncOut();
 
    android::IPCThreadState::self()->stopProcess();
    should_exit = true;
  }
 
 private:
  DISALLOW_COPY_AND_ASSIGN(BluetoothDeathRecipient);
};
 
int main(int argc, char* argv[]) {
  base::AtExitManager exit_manager;
  base::CommandLine::Init(argc, argv);
  logging::LoggingSettings log_settings;
 
  if (!logging::InitLogging(log_settings)) {
    LOG(ERROR) << "Failed to set up logging";
    return EXIT_FAILURE;
  }
 
  sp<IBluetooth> bt_iface;
  status_t status = getService(String16(kServiceName.c_str()), &bt_iface);
  if (status != OK) {
    LOG(ERROR) << "Failed to get service binder: '" << kServiceName
               << "' status=" << status;
    return EXIT_FAILURE;
  }
 
  sp<BluetoothDeathRecipient> dr(new BluetoothDeathRecipient());
  if (android::IInterface::asBinder(bt_iface.get())->linkToDeath(dr) !=
      android::NO_ERROR) {
    LOG(ERROR) << "Failed to register DeathRecipient for IBluetooth";
    return EXIT_FAILURE;
  }
 
  // Initialize the Binder process thread pool. We have to set this up,
  // otherwise, incoming callbacks from IBluetoothCallback will block the main
  // thread (in other words, we have to do this as we are a "Binder server").
  android::ProcessState::self()->startThreadPool();
 
  // Register Adapter state-change callback
  sp<CLIBluetoothCallback> callback = new CLIBluetoothCallback();
  bt_iface->RegisterCallback(callback);
 
  cout << COLOR_BOLDWHITE << "Fluoride Command-Line Interface\n"
       << COLOR_OFF << endl
       << "Type \"help\" to see possible commands.\n"
       << endl;
 
  string command;
 
  // Add commands from the command line, if they exist.
  auto command_line = base::CommandLine::ForCurrentProcess();
  if (command_line->HasSwitch(kExecuteLong)) {
    command += command_line->GetSwitchValueASCII(kExecuteLong);
  }
 
  if (command_line->HasSwitch(kExecuteShort)) {
    if (!command.empty()) command += " ; ";
    command += command_line->GetSwitchValueASCII(kExecuteShort);
  }
 
  while (true) {
    vector<string> commands = base::SplitString(
        command, ";", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
    for (string command : commands) {
      if (!ExecuteCommand(bt_iface, command)) break;
    }
 
    commands.clear();
 
    PrintPrompt();
 
    showing_prompt = true;
    auto& istream = getline(cin, command);
    showing_prompt = false;
 
    if (istream.eof() || should_exit.load()) {
      cout << "\nExiting" << endl;
      return EXIT_SUCCESS;
    }
 
    if (!istream.good()) {
      LOG(ERROR) << "An error occured while reading input";
      return EXIT_FAILURE;
    }
  }
 
  return EXIT_SUCCESS;
}