Arman Uguray | f520952 | 2015-08-05 21:22:39 -0700 | [diff] [blame] | 1 | // |
Jakub Pawlowski | 5b790fe | 2017-09-18 09:00:20 -0700 | [diff] [blame] | 2 | // Copyright 2015 Google, Inc. |
Arman Uguray | f520952 | 2015-08-05 21:22:39 -0700 | [diff] [blame] | 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at: |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
Arman Uguray | fcf2e03 | 2015-08-13 17:05:03 -0700 | [diff] [blame] | 17 | #include <iostream> |
| 18 | #include <string> |
| 19 | |
Arman Uguray | ae43de6 | 2015-09-24 15:05:21 -0700 | [diff] [blame] | 20 | #include <base/at_exit.h> |
| 21 | #include <base/command_line.h> |
Arman Uguray | f520952 | 2015-08-05 21:22:39 -0700 | [diff] [blame] | 22 | #include <base/logging.h> |
Arman Uguray | 39a66be | 2015-08-20 12:47:55 -0700 | [diff] [blame] | 23 | #include <base/macros.h> |
Jakub Pawlowski | ba197a2 | 2016-01-13 11:28:08 -0800 | [diff] [blame] | 24 | #include <base/strings/string_number_conversions.h> |
Arman Uguray | 5192309 | 2015-08-17 17:25:04 -0700 | [diff] [blame] | 25 | #include <base/strings/string_split.h> |
| 26 | #include <base/strings/string_util.h> |
Arman Uguray | 2e43414 | 2015-09-04 13:17:39 -0700 | [diff] [blame] | 27 | #include <binder/IPCThreadState.h> |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 28 | #include <binder/IServiceManager.h> |
Arman Uguray | 39a66be | 2015-08-20 12:47:55 -0700 | [diff] [blame] | 29 | #include <binder/ProcessState.h> |
Arman Uguray | f520952 | 2015-08-05 21:22:39 -0700 | [diff] [blame] | 30 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 31 | #include <android/bluetooth/BnBluetoothCallback.h> |
| 32 | #include <android/bluetooth/BnBluetoothGattClientCallback.h> |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 33 | #include <android/bluetooth/BnBluetoothLeAdvertiserCallback.h> |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 34 | #include <android/bluetooth/BnBluetoothLeScannerCallback.h> |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 35 | #include <android/bluetooth/BnBluetoothLowEnergyCallback.h> |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 36 | #include <android/bluetooth/IBluetooth.h> |
| 37 | #include <android/bluetooth/IBluetoothGattClient.h> |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 38 | #include <android/bluetooth/IBluetoothLeAdvertiser.h> |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 39 | #include <android/bluetooth/IBluetoothLeScanner.h> |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 40 | #include <android/bluetooth/IBluetoothLowEnergy.h> |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 41 | #include <bluetooth/adapter_state.h> |
Arman Uguray | 234138e | 2015-10-06 15:56:36 -0700 | [diff] [blame] | 42 | #include <bluetooth/low_energy_constants.h> |
Jakub Pawlowski | ba197a2 | 2016-01-13 11:28:08 -0800 | [diff] [blame] | 43 | #include <bluetooth/scan_filter.h> |
| 44 | #include <bluetooth/scan_settings.h> |
Arman Uguray | 87222e0 | 2015-10-09 12:32:55 -0700 | [diff] [blame] | 45 | #include <bluetooth/uuid.h> |
Arman Uguray | f520952 | 2015-08-05 21:22:39 -0700 | [diff] [blame] | 46 | |
Arman Uguray | fcf2e03 | 2015-08-13 17:05:03 -0700 | [diff] [blame] | 47 | using namespace std; |
| 48 | |
Arman Uguray | f520952 | 2015-08-05 21:22:39 -0700 | [diff] [blame] | 49 | using android::sp; |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 50 | using android::String8; |
| 51 | using android::String16; |
| 52 | using android::binder::Status; |
| 53 | using android::OK; |
| 54 | using android::getService; |
Arman Uguray | f520952 | 2015-08-05 21:22:39 -0700 | [diff] [blame] | 55 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 56 | using android::bluetooth::IBluetooth; |
| 57 | using android::bluetooth::IBluetoothGattClient; |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 58 | using android::bluetooth::IBluetoothLeAdvertiser; |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 59 | using android::bluetooth::IBluetoothLeScanner; |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 60 | using android::bluetooth::IBluetoothLowEnergy; |
Arman Uguray | f520952 | 2015-08-05 21:22:39 -0700 | [diff] [blame] | 61 | |
Arman Uguray | 2117e52 | 2015-08-14 17:23:47 -0700 | [diff] [blame] | 62 | namespace { |
| 63 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 64 | #define COLOR_OFF "\x1B[0m" |
| 65 | #define COLOR_RED "\x1B[0;91m" |
| 66 | #define COLOR_GREEN "\x1B[0;92m" |
| 67 | #define COLOR_YELLOW "\x1B[0;93m" |
| 68 | #define COLOR_BLUE "\x1B[0;94m" |
| 69 | #define COLOR_MAGENTA "\x1B[0;95m" |
| 70 | #define COLOR_BOLDGRAY "\x1B[1;30m" |
| 71 | #define COLOR_BOLDWHITE "\x1B[1;37m" |
| 72 | #define COLOR_BOLDYELLOW "\x1B[1;93m" |
| 73 | #define CLEAR_LINE "\x1B[2K" |
Arman Uguray | fcf2e03 | 2015-08-13 17:05:03 -0700 | [diff] [blame] | 74 | |
Arman Uguray | 5192309 | 2015-08-17 17:25:04 -0700 | [diff] [blame] | 75 | #define CHECK_ARGS_COUNT(args, op, num, msg) \ |
Chih-Hung Hsieh | 63b0519 | 2016-05-20 10:29:31 -0700 | [diff] [blame] | 76 | if (!((args).size() op num)) { \ |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 77 | PrintError(msg); \ |
| 78 | return; \ |
Arman Uguray | 5192309 | 2015-08-17 17:25:04 -0700 | [diff] [blame] | 79 | } |
| 80 | #define CHECK_NO_ARGS(args) \ |
| 81 | CHECK_ARGS_COUNT(args, ==, 0, "Expected no arguments") |
| 82 | |
Arman Uguray | 39a66be | 2015-08-20 12:47:55 -0700 | [diff] [blame] | 83 | // TODO(armansito): Clean up this code. Right now everything is in this |
| 84 | // monolithic file. We should organize this into different classes for command |
| 85 | // handling, console output/printing, callback handling, etc. |
| 86 | // (See http://b/23387611) |
| 87 | |
| 88 | // Used to synchronize the printing of the command-line prompt and incoming |
| 89 | // Binder callbacks. |
| 90 | std::atomic_bool showing_prompt(false); |
| 91 | |
Arman Uguray | 2e43414 | 2015-09-04 13:17:39 -0700 | [diff] [blame] | 92 | // The registered IBluetoothLowEnergy client handle. If |ble_registering| is |
| 93 | // true then an operation to register the client is in progress. |
| 94 | std::atomic_bool ble_registering(false); |
Arman Uguray | 4fbbf60 | 2015-11-10 18:05:33 -0800 | [diff] [blame] | 95 | std::atomic_int ble_client_id(0); |
| 96 | |
Myles Watson | 9ca0709 | 2016-11-28 16:41:53 -0800 | [diff] [blame] | 97 | // The registered IBluetoothLeAdvertiser handle. If |ble_advertiser_registering| |
| 98 | // is true then an operation to register the advertiser is in progress. |
Jakub Pawlowski | daa8a99 | 2017-01-09 06:53:36 -0800 | [diff] [blame] | 99 | const int invalid_advertiser_id = -1; |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 100 | std::atomic_bool ble_advertiser_registering(false); |
Jakub Pawlowski | daa8a99 | 2017-01-09 06:53:36 -0800 | [diff] [blame] | 101 | std::atomic_int ble_advertiser_id(invalid_advertiser_id); |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 102 | |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 103 | // The registered IBluetoothLeScanner handle. If |ble_scanner_registering| is |
| 104 | // true then an operation to register the scanner is in progress. |
| 105 | std::atomic_bool ble_scanner_registering(false); |
| 106 | std::atomic_int ble_scanner_id(0); |
| 107 | |
Arman Uguray | 4fbbf60 | 2015-11-10 18:05:33 -0800 | [diff] [blame] | 108 | // The registered IBluetoothGattClient client handle. If |gatt_registering| is |
| 109 | // true then an operation to register the client is in progress. |
| 110 | std::atomic_bool gatt_registering(false); |
| 111 | std::atomic_int gatt_client_id(0); |
Arman Uguray | 2e43414 | 2015-09-04 13:17:39 -0700 | [diff] [blame] | 112 | |
Jakub Pawlowski | ba197a2 | 2016-01-13 11:28:08 -0800 | [diff] [blame] | 113 | // True if we should dump the scan record bytes for incoming scan results. |
| 114 | std::atomic_bool dump_scan_record(false); |
| 115 | |
Arman Uguray | 2e43414 | 2015-09-04 13:17:39 -0700 | [diff] [blame] | 116 | // True if the remote process has died and we should exit. |
| 117 | std::atomic_bool should_exit(false); |
| 118 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 119 | std::string kServiceName = "bluetooth-service"; |
| 120 | |
| 121 | void PrintPrompt() { cout << COLOR_BLUE "[FCLI] " COLOR_OFF << flush; } |
Arman Uguray | 39a66be | 2015-08-20 12:47:55 -0700 | [diff] [blame] | 122 | |
Arman Uguray | 2e43414 | 2015-09-04 13:17:39 -0700 | [diff] [blame] | 123 | void PrintError(const string& message) { |
| 124 | cout << COLOR_RED << message << COLOR_OFF << endl; |
| 125 | } |
| 126 | |
Arman Uguray | d19bc04 | 2015-09-16 18:04:32 -0700 | [diff] [blame] | 127 | void PrintOpStatus(const std::string& op, bool status) { |
| 128 | cout << COLOR_BOLDWHITE << op << " status: " COLOR_OFF |
| 129 | << (status ? (COLOR_GREEN "success") : (COLOR_RED "failure")) |
Jakub Pawlowski | 64401bf | 2016-01-28 23:57:17 -0800 | [diff] [blame] | 130 | << COLOR_OFF << endl; |
| 131 | } |
| 132 | |
| 133 | inline void BeginAsyncOut() { |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 134 | if (showing_prompt.load()) cout << CLEAR_LINE << "\r"; |
Jakub Pawlowski | 64401bf | 2016-01-28 23:57:17 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | inline void EndAsyncOut() { |
| 138 | std::flush(cout); |
| 139 | if (showing_prompt.load()) |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 140 | PrintPrompt(); |
Jakub Pawlowski | 64401bf | 2016-01-28 23:57:17 -0800 | [diff] [blame] | 141 | else |
| 142 | cout << endl; |
Arman Uguray | d19bc04 | 2015-09-16 18:04:32 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 145 | class CLIBluetoothCallback : public android::bluetooth::BnBluetoothCallback { |
Arman Uguray | 39a66be | 2015-08-20 12:47:55 -0700 | [diff] [blame] | 146 | public: |
| 147 | CLIBluetoothCallback() = default; |
| 148 | ~CLIBluetoothCallback() override = default; |
| 149 | |
Arman Uguray | 2e43414 | 2015-09-04 13:17:39 -0700 | [diff] [blame] | 150 | // IBluetoothCallback overrides: |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 151 | Status OnBluetoothStateChange(int32_t prev_state, |
| 152 | int32_t new_state) override { |
Jakub Pawlowski | 64401bf | 2016-01-28 23:57:17 -0800 | [diff] [blame] | 153 | BeginAsyncOut(); |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 154 | cout << COLOR_BOLDWHITE "Adapter state changed: " COLOR_OFF << COLOR_MAGENTA |
| 155 | << AdapterStateToString(bluetooth::AdapterState(prev_state)) |
| 156 | << COLOR_OFF << COLOR_BOLDWHITE " -> " COLOR_OFF << COLOR_BOLDYELLOW |
| 157 | << AdapterStateToString(bluetooth::AdapterState(new_state)) |
| 158 | << COLOR_OFF; |
Jakub Pawlowski | 64401bf | 2016-01-28 23:57:17 -0800 | [diff] [blame] | 159 | EndAsyncOut(); |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 160 | |
| 161 | return Status::ok(); |
| 162 | } |
Arman Uguray | 39a66be | 2015-08-20 12:47:55 -0700 | [diff] [blame] | 163 | |
Bailey Forrest | 62aa15f | 2017-01-30 17:38:58 -0800 | [diff] [blame] | 164 | Status OnSspRequest(const String16& device_address, |
| 165 | const String16& device_name, int32_t cod, |
| 166 | int32_t pairing_variant, int32_t pass_key) override { |
| 167 | // no-op |
| 168 | return Status::ok(); |
| 169 | } |
| 170 | |
| 171 | Status OnGetBondedDevices( |
| 172 | int32_t status, |
| 173 | const ::std::vector<String16>& device_addresses) override { |
| 174 | BeginAsyncOut(); |
| 175 | std::cout << "Bonded devices:\n"; |
| 176 | for (const auto& device_address : device_addresses) { |
| 177 | std::cout << " " << device_address << "\n"; |
| 178 | } |
| 179 | EndAsyncOut(); |
| 180 | return Status::ok(); |
| 181 | } |
| 182 | |
| 183 | Status OnBondStateChanged(int32_t status, const String16& device_address, |
| 184 | int32_t state) override { |
| 185 | BeginAsyncOut(); |
| 186 | std::cout << COLOR_BOLDWHITE "Device address: " << COLOR_BOLDYELLOW "[" |
| 187 | << device_address << " bond state: " << state << " ] " |
| 188 | << COLOR_BOLDWHITE "- status: " |
| 189 | << (status == 0 ? "SUCCESS" : "FAIL") << COLOR_OFF; |
| 190 | EndAsyncOut(); |
| 191 | return Status::ok(); |
| 192 | } |
| 193 | |
| 194 | Status OnGetRemoteDeviceProperties( |
| 195 | int32_t status, const String16& device_address, |
| 196 | const android::bluetooth::BluetoothRemoteDeviceProps& props) override { |
| 197 | // no-op |
| 198 | return Status::ok(); |
| 199 | } |
| 200 | |
| 201 | Status OnDeviceFound( |
| 202 | const android::bluetooth::BluetoothRemoteDeviceProps& props) override { |
| 203 | // no-op |
| 204 | return Status::ok(); |
| 205 | } |
| 206 | |
| 207 | Status OnDeviceConnectionStateChanged(const String16& device_address, |
| 208 | bool connected) override { |
| 209 | // no-op |
| 210 | return Status::ok(); |
| 211 | } |
| 212 | |
| 213 | Status OnScanEnableChanged(bool scan_enabled) override { |
| 214 | // no-op |
| 215 | return Status::ok(); |
| 216 | } |
| 217 | |
Arman Uguray | 39a66be | 2015-08-20 12:47:55 -0700 | [diff] [blame] | 218 | private: |
| 219 | DISALLOW_COPY_AND_ASSIGN(CLIBluetoothCallback); |
| 220 | }; |
| 221 | |
Arman Uguray | 2e43414 | 2015-09-04 13:17:39 -0700 | [diff] [blame] | 222 | class CLIBluetoothLowEnergyCallback |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 223 | : public android::bluetooth::BnBluetoothLowEnergyCallback { |
Arman Uguray | 2e43414 | 2015-09-04 13:17:39 -0700 | [diff] [blame] | 224 | public: |
| 225 | CLIBluetoothLowEnergyCallback() = default; |
Arman Uguray | 4fbbf60 | 2015-11-10 18:05:33 -0800 | [diff] [blame] | 226 | ~CLIBluetoothLowEnergyCallback() override = default; |
Arman Uguray | 2e43414 | 2015-09-04 13:17:39 -0700 | [diff] [blame] | 227 | |
| 228 | // IBluetoothLowEnergyCallback overrides: |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 229 | Status OnClientRegistered(int status, int client_id) override { |
Jakub Pawlowski | 64401bf | 2016-01-28 23:57:17 -0800 | [diff] [blame] | 230 | BeginAsyncOut(); |
Arman Uguray | 2e43414 | 2015-09-04 13:17:39 -0700 | [diff] [blame] | 231 | if (status != bluetooth::BLE_STATUS_SUCCESS) { |
| 232 | PrintError("Failed to register BLE client"); |
| 233 | } else { |
Arman Uguray | 4fbbf60 | 2015-11-10 18:05:33 -0800 | [diff] [blame] | 234 | ble_client_id = client_id; |
Arman Uguray | 2e43414 | 2015-09-04 13:17:39 -0700 | [diff] [blame] | 235 | cout << COLOR_BOLDWHITE "Registered BLE client with ID: " COLOR_OFF |
Jakub Pawlowski | 64401bf | 2016-01-28 23:57:17 -0800 | [diff] [blame] | 236 | << COLOR_GREEN << client_id << COLOR_OFF; |
Arman Uguray | 2e43414 | 2015-09-04 13:17:39 -0700 | [diff] [blame] | 237 | } |
Jakub Pawlowski | 64401bf | 2016-01-28 23:57:17 -0800 | [diff] [blame] | 238 | EndAsyncOut(); |
Arman Uguray | 2e43414 | 2015-09-04 13:17:39 -0700 | [diff] [blame] | 239 | |
| 240 | ble_registering = false; |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 241 | return Status::ok(); |
Arman Uguray | 2e43414 | 2015-09-04 13:17:39 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 244 | Status OnConnectionState(int status, int client_id, const String16& address, |
| 245 | bool connected) override { |
Jakub Pawlowski | 64401bf | 2016-01-28 23:57:17 -0800 | [diff] [blame] | 246 | BeginAsyncOut(); |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 247 | cout << COLOR_BOLDWHITE "Connection state: " << COLOR_BOLDYELLOW "[" |
| 248 | << address << " connected: " << (connected ? "true" : "false") << " ] " |
Jakub Pawlowski | 608762d | 2016-01-19 17:07:53 -0800 | [diff] [blame] | 249 | << COLOR_BOLDWHITE "- status: " << status |
| 250 | << COLOR_BOLDWHITE " - client_id: " << client_id << COLOR_OFF; |
Jakub Pawlowski | 64401bf | 2016-01-28 23:57:17 -0800 | [diff] [blame] | 251 | EndAsyncOut(); |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 252 | return Status::ok(); |
Jakub Pawlowski | 455dc8f | 2016-01-19 17:00:16 -0800 | [diff] [blame] | 253 | } |
| 254 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 255 | Status OnMtuChanged(int status, const String16& address, int mtu) override { |
Jakub Pawlowski | 64401bf | 2016-01-28 23:57:17 -0800 | [diff] [blame] | 256 | BeginAsyncOut(); |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 257 | cout << COLOR_BOLDWHITE "MTU changed: " << COLOR_BOLDYELLOW "[" << address |
| 258 | << " ] " << COLOR_BOLDWHITE " - status: " << status |
Jakub Pawlowski | a655107 | 2016-01-26 12:58:47 -0800 | [diff] [blame] | 259 | << COLOR_BOLDWHITE " - mtu: " << mtu << COLOR_OFF; |
Jakub Pawlowski | 64401bf | 2016-01-28 23:57:17 -0800 | [diff] [blame] | 260 | EndAsyncOut(); |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 261 | return Status::ok(); |
Jakub Pawlowski | a655107 | 2016-01-26 12:58:47 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 264 | private: |
| 265 | DISALLOW_COPY_AND_ASSIGN(CLIBluetoothLowEnergyCallback); |
| 266 | }; |
| 267 | |
| 268 | class CLIBluetoothLeAdvertiserCallback |
| 269 | : public android::bluetooth::BnBluetoothLeAdvertiserCallback { |
| 270 | public: |
| 271 | CLIBluetoothLeAdvertiserCallback() = default; |
| 272 | ~CLIBluetoothLeAdvertiserCallback() override = default; |
| 273 | |
| 274 | // IBluetoothLowEnergyCallback overrides: |
| 275 | Status OnAdvertiserRegistered(int status, int advertiser_id) override { |
| 276 | BeginAsyncOut(); |
| 277 | if (status != bluetooth::BLE_STATUS_SUCCESS) { |
| 278 | PrintError("Failed to register BLE advertiser"); |
| 279 | } else { |
| 280 | ble_advertiser_id = advertiser_id; |
| 281 | cout << COLOR_BOLDWHITE "Registered BLE advertiser with ID: " COLOR_OFF |
| 282 | << COLOR_GREEN << advertiser_id << COLOR_OFF; |
| 283 | } |
| 284 | EndAsyncOut(); |
| 285 | |
| 286 | ble_advertiser_registering = false; |
| 287 | return Status::ok(); |
| 288 | } |
| 289 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 290 | Status OnMultiAdvertiseCallback( |
Arman Uguray | 52bfc60 | 2015-09-10 14:28:54 -0700 | [diff] [blame] | 291 | int status, bool is_start, |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 292 | const android::bluetooth::AdvertiseSettings& /* settings */) { |
Jakub Pawlowski | 64401bf | 2016-01-28 23:57:17 -0800 | [diff] [blame] | 293 | BeginAsyncOut(); |
Arman Uguray | d19bc04 | 2015-09-16 18:04:32 -0700 | [diff] [blame] | 294 | std::string op = is_start ? "start" : "stop"; |
| 295 | |
| 296 | PrintOpStatus("Advertising " + op, status == bluetooth::BLE_STATUS_SUCCESS); |
Jakub Pawlowski | 64401bf | 2016-01-28 23:57:17 -0800 | [diff] [blame] | 297 | EndAsyncOut(); |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 298 | return Status::ok(); |
Arman Uguray | 52bfc60 | 2015-09-10 14:28:54 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Arman Uguray | 2e43414 | 2015-09-04 13:17:39 -0700 | [diff] [blame] | 301 | private: |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 302 | DISALLOW_COPY_AND_ASSIGN(CLIBluetoothLeAdvertiserCallback); |
Arman Uguray | 2e43414 | 2015-09-04 13:17:39 -0700 | [diff] [blame] | 303 | }; |
Arman Uguray | 5192309 | 2015-08-17 17:25:04 -0700 | [diff] [blame] | 304 | |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 305 | class CLIBluetoothLeScannerCallback |
| 306 | : public android::bluetooth::BnBluetoothLeScannerCallback { |
| 307 | public: |
| 308 | CLIBluetoothLeScannerCallback() = default; |
| 309 | ~CLIBluetoothLeScannerCallback() override = default; |
| 310 | |
| 311 | // IBluetoothLowEnergyCallback overrides: |
| 312 | Status OnScannerRegistered(int status, int scanner_id) override { |
| 313 | BeginAsyncOut(); |
| 314 | if (status != bluetooth::BLE_STATUS_SUCCESS) { |
| 315 | PrintError("Failed to register BLE client"); |
| 316 | } else { |
| 317 | ble_scanner_id = scanner_id; |
| 318 | cout << COLOR_BOLDWHITE "Registered BLE client with ID: " COLOR_OFF |
| 319 | << COLOR_GREEN << scanner_id << COLOR_OFF; |
| 320 | } |
| 321 | EndAsyncOut(); |
| 322 | |
| 323 | ble_scanner_registering = false; |
| 324 | return Status::ok(); |
| 325 | } |
| 326 | |
| 327 | Status OnScanResult( |
| 328 | const android::bluetooth::ScanResult& scan_result) override { |
| 329 | BeginAsyncOut(); |
| 330 | cout << COLOR_BOLDWHITE "Scan result: " << COLOR_BOLDYELLOW "[" |
| 331 | << scan_result.device_address() << "] " |
| 332 | << COLOR_BOLDWHITE "- RSSI: " << scan_result.rssi() << COLOR_OFF; |
| 333 | |
| 334 | if (dump_scan_record) { |
| 335 | cout << " - Record: " |
| 336 | << base::HexEncode(scan_result.scan_record().data(), |
| 337 | scan_result.scan_record().size()); |
| 338 | } |
| 339 | EndAsyncOut(); |
| 340 | return Status::ok(); |
| 341 | } |
| 342 | |
| 343 | private: |
| 344 | DISALLOW_COPY_AND_ASSIGN(CLIBluetoothLeScannerCallback); |
| 345 | }; |
| 346 | |
Arman Uguray | 4fbbf60 | 2015-11-10 18:05:33 -0800 | [diff] [blame] | 347 | class CLIGattClientCallback |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 348 | : public android::bluetooth::BnBluetoothGattClientCallback { |
Arman Uguray | 4fbbf60 | 2015-11-10 18:05:33 -0800 | [diff] [blame] | 349 | public: |
| 350 | CLIGattClientCallback() = default; |
| 351 | ~CLIGattClientCallback() override = default; |
| 352 | |
| 353 | // IBluetoothGattClientCallback overrides: |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 354 | Status OnClientRegistered(int status, int client_id) override { |
Jakub Pawlowski | 64401bf | 2016-01-28 23:57:17 -0800 | [diff] [blame] | 355 | BeginAsyncOut(); |
Arman Uguray | 4fbbf60 | 2015-11-10 18:05:33 -0800 | [diff] [blame] | 356 | if (status != bluetooth::BLE_STATUS_SUCCESS) { |
| 357 | PrintError("Failed to register GATT client"); |
| 358 | } else { |
| 359 | gatt_client_id = client_id; |
| 360 | cout << COLOR_BOLDWHITE "Registered GATT client with ID: " COLOR_OFF |
Jakub Pawlowski | 64401bf | 2016-01-28 23:57:17 -0800 | [diff] [blame] | 361 | << COLOR_GREEN << client_id << COLOR_OFF; |
Arman Uguray | 4fbbf60 | 2015-11-10 18:05:33 -0800 | [diff] [blame] | 362 | } |
Jakub Pawlowski | 64401bf | 2016-01-28 23:57:17 -0800 | [diff] [blame] | 363 | EndAsyncOut(); |
Arman Uguray | 4fbbf60 | 2015-11-10 18:05:33 -0800 | [diff] [blame] | 364 | |
| 365 | gatt_registering = false; |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 366 | return Status::ok(); |
Arman Uguray | 4fbbf60 | 2015-11-10 18:05:33 -0800 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | private: |
| 370 | DISALLOW_COPY_AND_ASSIGN(CLIGattClientCallback); |
| 371 | }; |
| 372 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 373 | void PrintCommandStatus(bool status) { PrintOpStatus("Command", status); } |
Arman Uguray | fcf2e03 | 2015-08-13 17:05:03 -0700 | [diff] [blame] | 374 | |
Arman Uguray | 5192309 | 2015-08-17 17:25:04 -0700 | [diff] [blame] | 375 | void PrintFieldAndValue(const string& field, const string& value) { |
| 376 | cout << COLOR_BOLDWHITE << field << ": " << COLOR_BOLDYELLOW << value |
| 377 | << COLOR_OFF << endl; |
| 378 | } |
| 379 | |
| 380 | void PrintFieldAndBoolValue(const string& field, bool value) { |
| 381 | PrintFieldAndValue(field, (value ? "true" : "false")); |
| 382 | } |
| 383 | |
| 384 | void HandleDisable(IBluetooth* bt_iface, const vector<string>& args) { |
| 385 | CHECK_NO_ARGS(args); |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 386 | bool status; |
| 387 | bt_iface->Disable(&status); |
| 388 | PrintCommandStatus(status); |
Arman Uguray | fcf2e03 | 2015-08-13 17:05:03 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Arman Uguray | 5192309 | 2015-08-17 17:25:04 -0700 | [diff] [blame] | 391 | void HandleEnable(IBluetooth* bt_iface, const vector<string>& args) { |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 392 | bool status; |
Martin Brabham | 1a88ace | 2019-05-10 12:42:15 -0700 | [diff] [blame] | 393 | bt_iface->Enable(&status); |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 394 | PrintCommandStatus(status); |
Arman Uguray | fcf2e03 | 2015-08-13 17:05:03 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Arman Uguray | 5192309 | 2015-08-17 17:25:04 -0700 | [diff] [blame] | 397 | void HandleGetState(IBluetooth* bt_iface, const vector<string>& args) { |
| 398 | CHECK_NO_ARGS(args); |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 399 | |
| 400 | int32_t st; |
| 401 | bt_iface->GetState(&st); |
| 402 | bluetooth::AdapterState state = static_cast<bluetooth::AdapterState>(st); |
Arman Uguray | 5192309 | 2015-08-17 17:25:04 -0700 | [diff] [blame] | 403 | PrintFieldAndValue("Adapter state", bluetooth::AdapterStateToString(state)); |
Arman Uguray | fcf2e03 | 2015-08-13 17:05:03 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Arman Uguray | 5192309 | 2015-08-17 17:25:04 -0700 | [diff] [blame] | 406 | void HandleIsEnabled(IBluetooth* bt_iface, const vector<string>& args) { |
| 407 | CHECK_NO_ARGS(args); |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 408 | bool enabled; |
| 409 | bt_iface->IsEnabled(&enabled); |
Arman Uguray | 5192309 | 2015-08-17 17:25:04 -0700 | [diff] [blame] | 410 | PrintFieldAndBoolValue("Adapter enabled", enabled); |
Arman Uguray | fcf2e03 | 2015-08-13 17:05:03 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Arman Uguray | 5192309 | 2015-08-17 17:25:04 -0700 | [diff] [blame] | 413 | void HandleGetLocalAddress(IBluetooth* bt_iface, const vector<string>& args) { |
| 414 | CHECK_NO_ARGS(args); |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 415 | String16 address; |
| 416 | bt_iface->GetAddress(&address); |
| 417 | PrintFieldAndValue("Adapter address", std::string(String8(address).string())); |
Arman Uguray | 5192309 | 2015-08-17 17:25:04 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | void HandleSetLocalName(IBluetooth* bt_iface, const vector<string>& args) { |
| 421 | CHECK_ARGS_COUNT(args, >=, 1, "No name was given"); |
| 422 | |
| 423 | std::string name; |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 424 | for (const auto& arg : args) name += arg + " "; |
Arman Uguray | 5192309 | 2015-08-17 17:25:04 -0700 | [diff] [blame] | 425 | |
| 426 | base::TrimWhitespaceASCII(name, base::TRIM_TRAILING, &name); |
| 427 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 428 | bool status; |
| 429 | bt_iface->SetName(String16(String8(name.c_str())), &status); |
| 430 | PrintCommandStatus(status); |
Arman Uguray | 5192309 | 2015-08-17 17:25:04 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | void HandleGetLocalName(IBluetooth* bt_iface, const vector<string>& args) { |
| 434 | CHECK_NO_ARGS(args); |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 435 | String16 name; |
| 436 | bt_iface->GetName(&name); |
| 437 | PrintFieldAndValue("Adapter name", std::string(String8(name).string())); |
Arman Uguray | 5192309 | 2015-08-17 17:25:04 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | void HandleAdapterInfo(IBluetooth* bt_iface, const vector<string>& args) { |
| 441 | CHECK_NO_ARGS(args); |
| 442 | |
| 443 | cout << COLOR_BOLDWHITE "Adapter Properties: " COLOR_OFF << endl; |
| 444 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 445 | String16 address; |
| 446 | bt_iface->GetAddress(&address); |
| 447 | PrintFieldAndValue("\tAddress", std::string(String8(address).string())); |
| 448 | |
| 449 | int adapter_state; |
| 450 | bt_iface->GetState(&adapter_state); |
| 451 | PrintFieldAndValue("\tState", |
| 452 | bluetooth::AdapterStateToString( |
| 453 | static_cast<bluetooth::AdapterState>(adapter_state))); |
| 454 | |
| 455 | String16 name; |
| 456 | bt_iface->GetName(&name); |
| 457 | PrintFieldAndValue("\tName", std::string(String8(name).string())); |
| 458 | |
| 459 | bool multi_adv; |
| 460 | bt_iface->IsMultiAdvertisementSupported(&multi_adv); |
| 461 | PrintFieldAndBoolValue("\tMulti-Adv. supported", multi_adv); |
Arman Uguray | 10b54c4 | 2015-08-21 14:59:57 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | void HandleSupportsMultiAdv(IBluetooth* bt_iface, const vector<string>& args) { |
| 465 | CHECK_NO_ARGS(args); |
| 466 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 467 | bool multi_adv; |
| 468 | bt_iface->IsMultiAdvertisementSupported(&multi_adv); |
| 469 | PrintFieldAndBoolValue("Multi-advertisement support", multi_adv); |
Arman Uguray | 5192309 | 2015-08-17 17:25:04 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 472 | void HandleRegisterBLEAdvertiser(IBluetooth* bt_iface, |
| 473 | const vector<string>& args) { |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 474 | CHECK_NO_ARGS(args); |
| 475 | |
| 476 | if (ble_advertiser_registering.load()) { |
| 477 | PrintError("In progress"); |
| 478 | return; |
| 479 | } |
| 480 | |
Jakub Pawlowski | daa8a99 | 2017-01-09 06:53:36 -0800 | [diff] [blame] | 481 | if (ble_advertiser_id.load() != invalid_advertiser_id) { |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 482 | PrintError("Already registered"); |
| 483 | return; |
| 484 | } |
| 485 | |
| 486 | sp<IBluetoothLeAdvertiser> ble_advertiser_iface; |
| 487 | bt_iface->GetLeAdvertiserInterface(&ble_advertiser_iface); |
| 488 | if (!ble_advertiser_iface.get()) { |
| 489 | PrintError("Failed to obtain handle to Bluetooth Le Advertiser interface"); |
| 490 | return; |
| 491 | } |
| 492 | |
| 493 | bool status; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 494 | ble_advertiser_iface->RegisterAdvertiser( |
| 495 | new CLIBluetoothLeAdvertiserCallback(), &status); |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 496 | ble_advertiser_registering = status; |
| 497 | PrintCommandStatus(status); |
| 498 | } |
| 499 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 500 | void HandleUnregisterBLEAdvertiser(IBluetooth* bt_iface, |
| 501 | const vector<string>& args) { |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 502 | CHECK_NO_ARGS(args); |
| 503 | |
Jakub Pawlowski | daa8a99 | 2017-01-09 06:53:36 -0800 | [diff] [blame] | 504 | if (ble_advertiser_id.load() == invalid_advertiser_id) { |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 505 | PrintError("Not registered"); |
| 506 | return; |
| 507 | } |
| 508 | |
| 509 | sp<IBluetoothLeAdvertiser> ble_advertiser_iface; |
| 510 | bt_iface->GetLeAdvertiserInterface(&ble_advertiser_iface); |
| 511 | if (!ble_advertiser_iface.get()) { |
| 512 | PrintError("Failed to obtain handle to Bluetooth Low Energy interface"); |
| 513 | return; |
| 514 | } |
| 515 | |
| 516 | ble_advertiser_iface->UnregisterAdvertiser(ble_advertiser_id.load()); |
Jakub Pawlowski | daa8a99 | 2017-01-09 06:53:36 -0800 | [diff] [blame] | 517 | ble_advertiser_id = invalid_advertiser_id; |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 518 | PrintCommandStatus(true); |
| 519 | } |
| 520 | |
Arman Uguray | 2e43414 | 2015-09-04 13:17:39 -0700 | [diff] [blame] | 521 | void HandleRegisterBLE(IBluetooth* bt_iface, const vector<string>& args) { |
| 522 | CHECK_NO_ARGS(args); |
| 523 | |
| 524 | if (ble_registering.load()) { |
| 525 | PrintError("In progress"); |
| 526 | return; |
| 527 | } |
| 528 | |
Arman Uguray | 4fbbf60 | 2015-11-10 18:05:33 -0800 | [diff] [blame] | 529 | if (ble_client_id.load()) { |
Arman Uguray | 2e43414 | 2015-09-04 13:17:39 -0700 | [diff] [blame] | 530 | PrintError("Already registered"); |
| 531 | return; |
| 532 | } |
| 533 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 534 | sp<IBluetoothLowEnergy> ble_iface; |
| 535 | bt_iface->GetLowEnergyInterface(&ble_iface); |
Arman Uguray | 2e43414 | 2015-09-04 13:17:39 -0700 | [diff] [blame] | 536 | if (!ble_iface.get()) { |
| 537 | PrintError("Failed to obtain handle to Bluetooth Low Energy interface"); |
| 538 | return; |
| 539 | } |
| 540 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 541 | bool status; |
| 542 | ble_iface->RegisterClient(new CLIBluetoothLowEnergyCallback(), &status); |
Arman Uguray | ae43de6 | 2015-09-24 15:05:21 -0700 | [diff] [blame] | 543 | ble_registering = status; |
| 544 | PrintCommandStatus(status); |
Arman Uguray | 2e43414 | 2015-09-04 13:17:39 -0700 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | void HandleUnregisterBLE(IBluetooth* bt_iface, const vector<string>& args) { |
| 548 | CHECK_NO_ARGS(args); |
| 549 | |
Arman Uguray | 4fbbf60 | 2015-11-10 18:05:33 -0800 | [diff] [blame] | 550 | if (!ble_client_id.load()) { |
Arman Uguray | 2e43414 | 2015-09-04 13:17:39 -0700 | [diff] [blame] | 551 | PrintError("Not registered"); |
| 552 | return; |
| 553 | } |
| 554 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 555 | sp<IBluetoothLowEnergy> ble_iface; |
| 556 | bt_iface->GetLowEnergyInterface(&ble_iface); |
Arman Uguray | 2e43414 | 2015-09-04 13:17:39 -0700 | [diff] [blame] | 557 | if (!ble_iface.get()) { |
| 558 | PrintError("Failed to obtain handle to Bluetooth Low Energy interface"); |
| 559 | return; |
| 560 | } |
| 561 | |
Arman Uguray | 4fbbf60 | 2015-11-10 18:05:33 -0800 | [diff] [blame] | 562 | ble_iface->UnregisterClient(ble_client_id.load()); |
| 563 | ble_client_id = 0; |
Arman Uguray | 2e43414 | 2015-09-04 13:17:39 -0700 | [diff] [blame] | 564 | PrintCommandStatus(true); |
| 565 | } |
| 566 | |
| 567 | void HandleUnregisterAllBLE(IBluetooth* bt_iface, const vector<string>& args) { |
| 568 | CHECK_NO_ARGS(args); |
| 569 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 570 | sp<IBluetoothLowEnergy> ble_iface; |
| 571 | bt_iface->GetLowEnergyInterface(&ble_iface); |
Arman Uguray | 2e43414 | 2015-09-04 13:17:39 -0700 | [diff] [blame] | 572 | if (!ble_iface.get()) { |
| 573 | PrintError("Failed to obtain handle to Bluetooth Low Energy interface"); |
| 574 | return; |
| 575 | } |
| 576 | |
| 577 | ble_iface->UnregisterAll(); |
| 578 | PrintCommandStatus(true); |
| 579 | } |
| 580 | |
Arman Uguray | 4fbbf60 | 2015-11-10 18:05:33 -0800 | [diff] [blame] | 581 | void HandleRegisterGATT(IBluetooth* bt_iface, const vector<string>& args) { |
| 582 | CHECK_NO_ARGS(args); |
| 583 | |
| 584 | if (gatt_registering.load()) { |
| 585 | PrintError("In progress"); |
| 586 | return; |
| 587 | } |
| 588 | |
| 589 | if (gatt_client_id.load()) { |
| 590 | PrintError("Already registered"); |
| 591 | return; |
| 592 | } |
| 593 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 594 | sp<IBluetoothGattClient> gatt_iface; |
| 595 | bt_iface->GetGattClientInterface(&gatt_iface); |
Arman Uguray | 4fbbf60 | 2015-11-10 18:05:33 -0800 | [diff] [blame] | 596 | if (!gatt_iface.get()) { |
| 597 | PrintError("Failed to obtain handle to Bluetooth GATT Client interface"); |
| 598 | return; |
| 599 | } |
| 600 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 601 | bool status; |
| 602 | gatt_iface->RegisterClient(new CLIGattClientCallback(), &status); |
Arman Uguray | 4fbbf60 | 2015-11-10 18:05:33 -0800 | [diff] [blame] | 603 | gatt_registering = status; |
| 604 | PrintCommandStatus(status); |
| 605 | } |
| 606 | |
| 607 | void HandleUnregisterGATT(IBluetooth* bt_iface, const vector<string>& args) { |
| 608 | CHECK_NO_ARGS(args); |
| 609 | |
| 610 | if (!gatt_client_id.load()) { |
| 611 | PrintError("Not registered"); |
| 612 | return; |
| 613 | } |
| 614 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 615 | sp<IBluetoothGattClient> gatt_iface; |
| 616 | bt_iface->GetGattClientInterface(&gatt_iface); |
Arman Uguray | 4fbbf60 | 2015-11-10 18:05:33 -0800 | [diff] [blame] | 617 | if (!gatt_iface.get()) { |
| 618 | PrintError("Failed to obtain handle to Bluetooth GATT Client interface"); |
| 619 | return; |
| 620 | } |
| 621 | |
| 622 | gatt_iface->UnregisterClient(gatt_client_id.load()); |
| 623 | gatt_client_id = 0; |
| 624 | PrintCommandStatus(true); |
| 625 | } |
| 626 | |
Arman Uguray | d19bc04 | 2015-09-16 18:04:32 -0700 | [diff] [blame] | 627 | void HandleStartAdv(IBluetooth* bt_iface, const vector<string>& args) { |
| 628 | bool include_name = false; |
| 629 | bool include_tx_power = false; |
| 630 | bool connectable = false; |
| 631 | bool set_manufacturer_data = false; |
Arman Uguray | 87222e0 | 2015-10-09 12:32:55 -0700 | [diff] [blame] | 632 | bool set_uuid = false; |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 633 | bluetooth::Uuid uuid; |
Arman Uguray | d19bc04 | 2015-09-16 18:04:32 -0700 | [diff] [blame] | 634 | |
Arman Uguray | 87222e0 | 2015-10-09 12:32:55 -0700 | [diff] [blame] | 635 | for (auto iter = args.begin(); iter != args.end(); ++iter) { |
| 636 | const std::string& arg = *iter; |
Arman Uguray | d19bc04 | 2015-09-16 18:04:32 -0700 | [diff] [blame] | 637 | if (arg == "-n") |
| 638 | include_name = true; |
| 639 | else if (arg == "-t") |
| 640 | include_tx_power = true; |
| 641 | else if (arg == "-c") |
| 642 | connectable = true; |
| 643 | else if (arg == "-m") |
| 644 | set_manufacturer_data = true; |
Arman Uguray | 87222e0 | 2015-10-09 12:32:55 -0700 | [diff] [blame] | 645 | else if (arg == "-u") { |
| 646 | // This flag has a single argument. |
| 647 | ++iter; |
| 648 | if (iter == args.end()) { |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 649 | PrintError("Expected a Uuid after -u"); |
Arman Uguray | 87222e0 | 2015-10-09 12:32:55 -0700 | [diff] [blame] | 650 | return; |
| 651 | } |
| 652 | |
| 653 | std::string uuid_str = *iter; |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 654 | bool is_valid = false; |
| 655 | uuid = bluetooth::Uuid::FromString(uuid_str, &is_valid); |
| 656 | if (!is_valid) { |
| 657 | PrintError("Invalid Uuid: " + uuid_str); |
Arman Uguray | 87222e0 | 2015-10-09 12:32:55 -0700 | [diff] [blame] | 658 | return; |
| 659 | } |
| 660 | |
| 661 | set_uuid = true; |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 662 | } else if (arg == "-h") { |
Jakub Pawlowski | ba197a2 | 2016-01-13 11:28:08 -0800 | [diff] [blame] | 663 | static const char kUsage[] = |
Arman Uguray | d19bc04 | 2015-09-16 18:04:32 -0700 | [diff] [blame] | 664 | "Usage: start-adv [flags]\n" |
| 665 | "\n" |
Jakub Pawlowski | ba197a2 | 2016-01-13 11:28:08 -0800 | [diff] [blame] | 666 | "Flags:\n" |
Arman Uguray | d19bc04 | 2015-09-16 18:04:32 -0700 | [diff] [blame] | 667 | "\t-n\tInclude device name\n" |
| 668 | "\t-t\tInclude TX power\n" |
| 669 | "\t-c\tSend connectable adv. packets (default is non-connectable)\n" |
| 670 | "\t-m\tInclude random manufacturer data\n" |
| 671 | "\t-h\tShow this help message\n"; |
| 672 | cout << kUsage << endl; |
| 673 | return; |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 674 | } else { |
Arman Uguray | d19bc04 | 2015-09-16 18:04:32 -0700 | [diff] [blame] | 675 | PrintError("Unrecognized option: " + arg); |
| 676 | return; |
| 677 | } |
| 678 | } |
| 679 | |
Jakub Pawlowski | daa8a99 | 2017-01-09 06:53:36 -0800 | [diff] [blame] | 680 | if (ble_advertiser_id.load() == invalid_advertiser_id) { |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 681 | PrintError("BLE advertiser not registered"); |
Arman Uguray | d19bc04 | 2015-09-16 18:04:32 -0700 | [diff] [blame] | 682 | return; |
| 683 | } |
| 684 | |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 685 | sp<IBluetoothLeAdvertiser> ble_advertiser_iface; |
| 686 | bt_iface->GetLeAdvertiserInterface(&ble_advertiser_iface); |
| 687 | if (!ble_advertiser_iface.get()) { |
| 688 | PrintError("Failed to obtain handle to Bluetooth Le Advertiser interface"); |
Arman Uguray | d19bc04 | 2015-09-16 18:04:32 -0700 | [diff] [blame] | 689 | return; |
| 690 | } |
| 691 | |
| 692 | std::vector<uint8_t> data; |
| 693 | if (set_manufacturer_data) { |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 694 | data = {{0x07, bluetooth::kEIRTypeManufacturerSpecificData, 0xe0, 0x00, 'T', |
| 695 | 'e', 's', 't'}}; |
Arman Uguray | d19bc04 | 2015-09-16 18:04:32 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Arman Uguray | 87222e0 | 2015-10-09 12:32:55 -0700 | [diff] [blame] | 698 | if (set_uuid) { |
| 699 | // Determine the type and length bytes. |
| 700 | int uuid_size = uuid.GetShortestRepresentationSize(); |
| 701 | uint8_t type; |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 702 | if (uuid_size == bluetooth::Uuid::kNumBytes128) |
| 703 | type = bluetooth::kEIRTypeComplete128BitUuids; |
| 704 | else if (uuid_size == bluetooth::Uuid::kNumBytes32) |
| 705 | type = bluetooth::kEIRTypeComplete32BitUuids; |
| 706 | else if (uuid_size == bluetooth::Uuid::kNumBytes16) |
| 707 | type = bluetooth::kEIRTypeComplete16BitUuids; |
Arman Uguray | 87222e0 | 2015-10-09 12:32:55 -0700 | [diff] [blame] | 708 | else |
| 709 | NOTREACHED() << "Unexpected size: " << uuid_size; |
| 710 | |
| 711 | data.push_back(uuid_size + 1); |
| 712 | data.push_back(type); |
| 713 | |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 714 | auto uuid_bytes = uuid.To128BitLE(); |
Arman Uguray | 87222e0 | 2015-10-09 12:32:55 -0700 | [diff] [blame] | 715 | int index = (uuid_size == 16) ? 0 : 12; |
| 716 | data.insert(data.end(), uuid_bytes.data() + index, |
| 717 | uuid_bytes.data() + index + uuid_size); |
| 718 | } |
| 719 | |
Arman Uguray | d19bc04 | 2015-09-16 18:04:32 -0700 | [diff] [blame] | 720 | base::TimeDelta timeout; |
| 721 | |
| 722 | bluetooth::AdvertiseSettings settings( |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 723 | bluetooth::AdvertiseSettings::MODE_LOW_POWER, timeout, |
| 724 | bluetooth::AdvertiseSettings::TX_POWER_LEVEL_MEDIUM, connectable); |
Arman Uguray | d19bc04 | 2015-09-16 18:04:32 -0700 | [diff] [blame] | 725 | |
Jakub Pawlowski | 0ecddf3 | 2016-10-19 14:46:09 -0700 | [diff] [blame] | 726 | if (include_tx_power) { |
| 727 | data.push_back(0x02); |
| 728 | data.push_back(bluetooth::kEIRTypeTxPower); |
| 729 | data.push_back(0x00); |
| 730 | } |
| 731 | |
Arman Uguray | d19bc04 | 2015-09-16 18:04:32 -0700 | [diff] [blame] | 732 | bluetooth::AdvertiseData adv_data(data); |
Jakub Pawlowski | 0ecddf3 | 2016-10-19 14:46:09 -0700 | [diff] [blame] | 733 | |
| 734 | if (include_name) { |
| 735 | String16 name_param; |
| 736 | bt_iface->GetName(&name_param); |
| 737 | std::string name(String8(name_param).string()); |
| 738 | data.push_back(name.length() + 1); |
| 739 | data.push_back(bluetooth::kEIRTypeCompleteLocalName); |
| 740 | data.insert(data.begin(), name.c_str(), name.c_str() + name.length()); |
| 741 | } |
Arman Uguray | d19bc04 | 2015-09-16 18:04:32 -0700 | [diff] [blame] | 742 | |
| 743 | bluetooth::AdvertiseData scan_rsp; |
| 744 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 745 | bool status; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 746 | ble_advertiser_iface->StartMultiAdvertising( |
| 747 | ble_advertiser_id.load(), adv_data, scan_rsp, settings, &status); |
Arman Uguray | ae43de6 | 2015-09-24 15:05:21 -0700 | [diff] [blame] | 748 | PrintCommandStatus(status); |
Arman Uguray | d19bc04 | 2015-09-16 18:04:32 -0700 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | void HandleStopAdv(IBluetooth* bt_iface, const vector<string>& args) { |
Jakub Pawlowski | daa8a99 | 2017-01-09 06:53:36 -0800 | [diff] [blame] | 752 | if (ble_advertiser_id.load() == invalid_advertiser_id) { |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 753 | PrintError("BLE advertiser not registered"); |
Arman Uguray | d19bc04 | 2015-09-16 18:04:32 -0700 | [diff] [blame] | 754 | return; |
| 755 | } |
| 756 | |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 757 | sp<IBluetoothLeAdvertiser> ble_advertiser_iface; |
| 758 | bt_iface->GetLeAdvertiserInterface(&ble_advertiser_iface); |
| 759 | if (!ble_advertiser_iface.get()) { |
Arman Uguray | d19bc04 | 2015-09-16 18:04:32 -0700 | [diff] [blame] | 760 | PrintError("Failed to obtain handle to Bluetooth Low Energy interface"); |
| 761 | return; |
| 762 | } |
| 763 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 764 | bool status; |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 765 | ble_advertiser_iface->StopMultiAdvertising(ble_advertiser_id.load(), &status); |
Arman Uguray | ae43de6 | 2015-09-24 15:05:21 -0700 | [diff] [blame] | 766 | PrintCommandStatus(status); |
Arman Uguray | d19bc04 | 2015-09-16 18:04:32 -0700 | [diff] [blame] | 767 | } |
| 768 | |
Jakub Pawlowski | 608762d | 2016-01-19 17:07:53 -0800 | [diff] [blame] | 769 | void HandleConnect(IBluetooth* bt_iface, const vector<string>& args) { |
| 770 | string address; |
| 771 | |
| 772 | if (args.size() != 1) { |
| 773 | PrintError("Expected MAC address as only argument"); |
| 774 | return; |
| 775 | } |
| 776 | |
| 777 | address = args[0]; |
| 778 | |
| 779 | if (!ble_client_id.load()) { |
| 780 | PrintError("BLE not registered"); |
| 781 | return; |
| 782 | } |
| 783 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 784 | sp<IBluetoothLowEnergy> ble_iface; |
| 785 | bt_iface->GetLowEnergyInterface(&ble_iface); |
Jakub Pawlowski | 608762d | 2016-01-19 17:07:53 -0800 | [diff] [blame] | 786 | if (!ble_iface.get()) { |
| 787 | PrintError("Failed to obtain handle to Bluetooth Low Energy interface"); |
| 788 | return; |
| 789 | } |
| 790 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 791 | bool status; |
| 792 | ble_iface->Connect(ble_client_id.load(), |
| 793 | String16(address.c_str(), address.length()), |
| 794 | false /* is_direct */, &status); |
Jakub Pawlowski | 608762d | 2016-01-19 17:07:53 -0800 | [diff] [blame] | 795 | |
| 796 | PrintCommandStatus(status); |
| 797 | } |
| 798 | |
| 799 | void HandleDisconnect(IBluetooth* bt_iface, const vector<string>& args) { |
| 800 | string address; |
| 801 | |
| 802 | if (args.size() != 1) { |
| 803 | PrintError("Expected MAC address as only argument"); |
| 804 | return; |
| 805 | } |
| 806 | |
| 807 | address = args[0]; |
| 808 | |
| 809 | if (!ble_client_id.load()) { |
| 810 | PrintError("BLE not registered"); |
| 811 | return; |
| 812 | } |
| 813 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 814 | sp<IBluetoothLowEnergy> ble_iface; |
| 815 | bt_iface->GetLowEnergyInterface(&ble_iface); |
Jakub Pawlowski | 608762d | 2016-01-19 17:07:53 -0800 | [diff] [blame] | 816 | if (!ble_iface.get()) { |
| 817 | PrintError("Failed to obtain handle to Bluetooth Low Energy interface"); |
| 818 | return; |
| 819 | } |
| 820 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 821 | bool status; |
| 822 | ble_iface->Disconnect(ble_client_id.load(), |
| 823 | String16(address.c_str(), address.length()), &status); |
Jakub Pawlowski | 608762d | 2016-01-19 17:07:53 -0800 | [diff] [blame] | 824 | PrintCommandStatus(status); |
| 825 | } |
| 826 | |
Jakub Pawlowski | 756cc4b | 2016-01-26 14:56:21 -0800 | [diff] [blame] | 827 | void HandleSetMtu(IBluetooth* bt_iface, const vector<string>& args) { |
| 828 | string address; |
| 829 | int mtu; |
| 830 | |
| 831 | if (args.size() != 2) { |
| 832 | PrintError("Usage: set-mtu [address] [mtu]"); |
| 833 | return; |
| 834 | } |
| 835 | |
| 836 | address = args[0]; |
| 837 | mtu = std::stoi(args[1]); |
| 838 | |
| 839 | if (mtu < 23) { |
| 840 | PrintError("MTU must be 23 or larger"); |
| 841 | return; |
| 842 | } |
| 843 | |
| 844 | if (!ble_client_id.load()) { |
| 845 | PrintError("BLE not registered"); |
| 846 | return; |
| 847 | } |
| 848 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 849 | sp<IBluetoothLowEnergy> ble_iface; |
| 850 | bt_iface->GetLowEnergyInterface(&ble_iface); |
Jakub Pawlowski | 756cc4b | 2016-01-26 14:56:21 -0800 | [diff] [blame] | 851 | if (!ble_iface.get()) { |
| 852 | PrintError("Failed to obtain handle to Bluetooth Low Energy interface"); |
| 853 | return; |
| 854 | } |
| 855 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 856 | bool status; |
| 857 | ble_iface->SetMtu(ble_client_id.load(), |
| 858 | String16(address.c_str(), address.length()), mtu, &status); |
Jakub Pawlowski | 756cc4b | 2016-01-26 14:56:21 -0800 | [diff] [blame] | 859 | PrintCommandStatus(status); |
| 860 | } |
| 861 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 862 | void HandleRegisterBLEScanner(IBluetooth* bt_iface, |
| 863 | const vector<string>& args) { |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 864 | CHECK_NO_ARGS(args); |
| 865 | |
| 866 | if (ble_scanner_registering.load()) { |
| 867 | PrintError("In progress"); |
| 868 | return; |
| 869 | } |
| 870 | |
| 871 | if (ble_scanner_id.load()) { |
| 872 | PrintError("Already registered"); |
| 873 | return; |
| 874 | } |
| 875 | |
| 876 | sp<IBluetoothLeScanner> ble_scanner_iface; |
| 877 | bt_iface->GetLeScannerInterface(&ble_scanner_iface); |
| 878 | if (!ble_scanner_iface.get()) { |
| 879 | PrintError("Failed to obtain handle to Bluetooth LE Scanner interface"); |
| 880 | return; |
| 881 | } |
| 882 | |
| 883 | bool status; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 884 | ble_scanner_iface->RegisterScanner(new CLIBluetoothLeScannerCallback(), |
| 885 | &status); |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 886 | ble_scanner_registering = status; |
| 887 | PrintCommandStatus(status); |
| 888 | } |
| 889 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 890 | void HandleUnregisterBLEScanner(IBluetooth* bt_iface, |
| 891 | const vector<string>& args) { |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 892 | CHECK_NO_ARGS(args); |
| 893 | |
| 894 | if (!ble_scanner_id.load()) { |
| 895 | PrintError("Not registered"); |
| 896 | return; |
| 897 | } |
| 898 | |
| 899 | sp<IBluetoothLeScanner> ble_scanner_iface; |
| 900 | bt_iface->GetLeScannerInterface(&ble_scanner_iface); |
| 901 | if (!ble_scanner_iface.get()) { |
| 902 | PrintError("Failed to obtain handle to Bluetooth LE scanner interface"); |
| 903 | return; |
| 904 | } |
| 905 | |
| 906 | ble_scanner_iface->UnregisterScanner(ble_scanner_id.load()); |
| 907 | ble_scanner_id = 0; |
| 908 | PrintCommandStatus(true); |
| 909 | } |
| 910 | |
Jakub Pawlowski | ba197a2 | 2016-01-13 11:28:08 -0800 | [diff] [blame] | 911 | void HandleStartLeScan(IBluetooth* bt_iface, const vector<string>& args) { |
| 912 | if (!ble_client_id.load()) { |
| 913 | PrintError("BLE not registered"); |
| 914 | return; |
| 915 | } |
| 916 | |
Chih-Hung Hsieh | 5dc0d15 | 2016-08-17 14:12:51 -0700 | [diff] [blame] | 917 | for (const auto& arg : args) { |
Jakub Pawlowski | ba197a2 | 2016-01-13 11:28:08 -0800 | [diff] [blame] | 918 | if (arg == "-d") { |
| 919 | dump_scan_record = true; |
| 920 | } else if (arg == "-h") { |
| 921 | static const char kUsage[] = |
| 922 | "Usage: start-le-scan [flags]\n" |
| 923 | "\n" |
| 924 | "Flags:\n" |
| 925 | "\t-d\tDump scan record\n" |
| 926 | "\t-h\tShow this help message\n"; |
| 927 | cout << kUsage << endl; |
| 928 | return; |
| 929 | } |
| 930 | } |
| 931 | |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 932 | sp<IBluetoothLeScanner> ble_scanner_iface; |
| 933 | bt_iface->GetLeScannerInterface(&ble_scanner_iface); |
| 934 | if (!ble_scanner_iface.get()) { |
| 935 | PrintError("Failed to obtain handle to Bluetooth LE scanner interface"); |
Jakub Pawlowski | ba197a2 | 2016-01-13 11:28:08 -0800 | [diff] [blame] | 936 | return; |
| 937 | } |
| 938 | |
| 939 | bluetooth::ScanSettings settings; |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 940 | std::vector<android::bluetooth::ScanFilter> filters; |
Jakub Pawlowski | ba197a2 | 2016-01-13 11:28:08 -0800 | [diff] [blame] | 941 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 942 | bool status; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 943 | ble_scanner_iface->StartScan(ble_scanner_id.load(), settings, filters, |
| 944 | &status); |
Jakub Pawlowski | ba197a2 | 2016-01-13 11:28:08 -0800 | [diff] [blame] | 945 | PrintCommandStatus(status); |
| 946 | } |
| 947 | |
| 948 | void HandleStopLeScan(IBluetooth* bt_iface, const vector<string>& args) { |
| 949 | if (!ble_client_id.load()) { |
| 950 | PrintError("BLE not registered"); |
| 951 | return; |
| 952 | } |
| 953 | |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 954 | sp<IBluetoothLeScanner> ble_scanner_iface; |
| 955 | bt_iface->GetLeScannerInterface(&ble_scanner_iface); |
| 956 | if (!ble_scanner_iface.get()) { |
| 957 | PrintError("Failed to obtain handle to Bluetooth LE scanner interface"); |
Jakub Pawlowski | ba197a2 | 2016-01-13 11:28:08 -0800 | [diff] [blame] | 958 | return; |
| 959 | } |
| 960 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 961 | bool status; |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 962 | ble_scanner_iface->StopScan(ble_scanner_id.load(), &status); |
Jakub Pawlowski | ba197a2 | 2016-01-13 11:28:08 -0800 | [diff] [blame] | 963 | PrintCommandStatus(status); |
| 964 | } |
| 965 | |
Arman Uguray | 5192309 | 2015-08-17 17:25:04 -0700 | [diff] [blame] | 966 | void HandleHelp(IBluetooth* bt_iface, const vector<string>& args); |
Arman Uguray | 2117e52 | 2015-08-14 17:23:47 -0700 | [diff] [blame] | 967 | |
Arman Uguray | fcf2e03 | 2015-08-13 17:05:03 -0700 | [diff] [blame] | 968 | struct { |
| 969 | string command; |
Arman Uguray | 5192309 | 2015-08-17 17:25:04 -0700 | [diff] [blame] | 970 | void (*func)(IBluetooth*, const vector<string>& args); |
Arman Uguray | 2117e52 | 2015-08-14 17:23:47 -0700 | [diff] [blame] | 971 | string help; |
Arman Uguray | fcf2e03 | 2015-08-13 17:05:03 -0700 | [diff] [blame] | 972 | } kCommandMap[] = { |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 973 | {"help", HandleHelp, "\t\t\tDisplay this message"}, |
| 974 | {"disable", HandleDisable, "\t\t\tDisable Bluetooth"}, |
Ajay Panicker | 7b266be | 2016-03-17 17:09:24 -0700 | [diff] [blame] | 975 | {"enable", HandleEnable, "\t\t\tEnable Bluetooth (-h for options)"}, |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 976 | {"get-state", HandleGetState, "\t\tGet the current adapter state"}, |
| 977 | {"is-enabled", HandleIsEnabled, "\t\tReturn if Bluetooth is enabled"}, |
| 978 | {"get-local-address", HandleGetLocalAddress, |
| 979 | "\tGet the local adapter address"}, |
| 980 | {"set-local-name", HandleSetLocalName, "\t\tSet the local adapter name"}, |
| 981 | {"get-local-name", HandleGetLocalName, "\t\tGet the local adapter name"}, |
| 982 | {"adapter-info", HandleAdapterInfo, "\t\tPrint adapter properties"}, |
| 983 | {"supports-multi-adv", HandleSupportsMultiAdv, |
| 984 | "\tWhether multi-advertisement is currently supported"}, |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 985 | {"register-le-advertiser", HandleRegisterBLEAdvertiser, |
| 986 | "\t\tRegister with the Bluetooth Low Energy Advertiser interface"}, |
| 987 | {"unregister-le-advertiser", HandleUnregisterBLEAdvertiser, |
| 988 | "\t\tUnregister from the Bluetooth LE Advertiser interface"}, |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 989 | {"register-ble", HandleRegisterBLE, |
| 990 | "\t\tRegister with the Bluetooth Low Energy interface"}, |
| 991 | {"unregister-ble", HandleUnregisterBLE, |
| 992 | "\t\tUnregister from the Bluetooth Low Energy interface"}, |
| 993 | {"unregister-all-ble", HandleUnregisterAllBLE, |
| 994 | "\tUnregister all clients from the Bluetooth Low Energy interface"}, |
| 995 | {"register-gatt", HandleRegisterGATT, |
| 996 | "\t\tRegister with the Bluetooth GATT Client interface"}, |
| 997 | {"unregister-gatt", HandleUnregisterGATT, |
| 998 | "\t\tUnregister from the Bluetooth GATT Client interface"}, |
| 999 | {"connect-le", HandleConnect, "\t\tConnect to LE device (-h for options)"}, |
| 1000 | {"disconnect-le", HandleDisconnect, |
| 1001 | "\t\tDisconnect LE device (-h for options)"}, |
| 1002 | {"set-mtu", HandleSetMtu, "\t\tSet MTU (-h for options)"}, |
| 1003 | {"start-adv", HandleStartAdv, "\t\tStart advertising (-h for options)"}, |
| 1004 | {"stop-adv", HandleStopAdv, "\t\tStop advertising"}, |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 1005 | {"register-le-scanner", HandleRegisterBLEScanner, |
| 1006 | "\t\tRegister with the Bluetooth Low Energy scanner interface"}, |
| 1007 | {"unregister-le-scanner", HandleUnregisterBLEScanner, |
| 1008 | "\t\tUnregister from the Bluetooth LE scanner interface"}, |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 1009 | {"start-le-scan", HandleStartLeScan, |
| 1010 | "\t\tStart LE device scan (-h for options)"}, |
| 1011 | {"stop-le-scan", HandleStopLeScan, "\t\tStop LE device scan"}, |
| 1012 | {}, |
Arman Uguray | fcf2e03 | 2015-08-13 17:05:03 -0700 | [diff] [blame] | 1013 | }; |
Arman Uguray | f520952 | 2015-08-05 21:22:39 -0700 | [diff] [blame] | 1014 | |
Arman Uguray | 5192309 | 2015-08-17 17:25:04 -0700 | [diff] [blame] | 1015 | void HandleHelp(IBluetooth* /* bt_iface */, const vector<string>& /* args */) { |
Arman Uguray | 2117e52 | 2015-08-14 17:23:47 -0700 | [diff] [blame] | 1016 | cout << endl; |
| 1017 | for (int i = 0; kCommandMap[i].func; i++) |
| 1018 | cout << "\t" << kCommandMap[i].command << kCommandMap[i].help << endl; |
| 1019 | cout << endl; |
| 1020 | } |
| 1021 | |
Marie Janssen | 5d80cb9 | 2016-01-21 18:45:53 -0800 | [diff] [blame] | 1022 | const char kExecuteLong[] = "exec"; |
| 1023 | const char kExecuteShort[] = "e"; |
| 1024 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1025 | bool ExecuteCommand(const sp<IBluetooth>& bt_iface, std::string& command) { |
| 1026 | vector<string> args = base::SplitString(command, " ", base::TRIM_WHITESPACE, |
| 1027 | base::SPLIT_WANT_ALL); |
Marie Janssen | 5d80cb9 | 2016-01-21 18:45:53 -0800 | [diff] [blame] | 1028 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1029 | if (args.empty()) return true; |
Marie Janssen | 5d80cb9 | 2016-01-21 18:45:53 -0800 | [diff] [blame] | 1030 | |
| 1031 | // The first argument is the command while the remaining are what we pass to |
| 1032 | // the handler functions. |
| 1033 | command = args[0]; |
| 1034 | args.erase(args.begin()); |
| 1035 | |
| 1036 | for (int i = 0; kCommandMap[i].func; i++) { |
| 1037 | if (command == kCommandMap[i].command) { |
| 1038 | kCommandMap[i].func(bt_iface.get(), args); |
| 1039 | return true; |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | cout << "Unrecognized command: " << command << endl; |
| 1044 | return false; |
| 1045 | } |
| 1046 | |
Arman Uguray | 2117e52 | 2015-08-14 17:23:47 -0700 | [diff] [blame] | 1047 | } // namespace |
| 1048 | |
Arman Uguray | 2e43414 | 2015-09-04 13:17:39 -0700 | [diff] [blame] | 1049 | class BluetoothDeathRecipient : public android::IBinder::DeathRecipient { |
| 1050 | public: |
| 1051 | BluetoothDeathRecipient() = default; |
| 1052 | ~BluetoothDeathRecipient() override = default; |
| 1053 | |
| 1054 | // android::IBinder::DeathRecipient override: |
| 1055 | void binderDied(const android::wp<android::IBinder>& /* who */) override { |
Jakub Pawlowski | 64401bf | 2016-01-28 23:57:17 -0800 | [diff] [blame] | 1056 | BeginAsyncOut(); |
Arman Uguray | 2e43414 | 2015-09-04 13:17:39 -0700 | [diff] [blame] | 1057 | cout << COLOR_BOLDWHITE "The Bluetooth daemon has died" COLOR_OFF << endl; |
Jakub Pawlowski | 64401bf | 2016-01-28 23:57:17 -0800 | [diff] [blame] | 1058 | cout << "\nPress 'ENTER' to exit."; |
| 1059 | EndAsyncOut(); |
Arman Uguray | 2e43414 | 2015-09-04 13:17:39 -0700 | [diff] [blame] | 1060 | |
| 1061 | android::IPCThreadState::self()->stopProcess(); |
| 1062 | should_exit = true; |
| 1063 | } |
| 1064 | |
| 1065 | private: |
| 1066 | DISALLOW_COPY_AND_ASSIGN(BluetoothDeathRecipient); |
| 1067 | }; |
| 1068 | |
Arman Uguray | ae43de6 | 2015-09-24 15:05:21 -0700 | [diff] [blame] | 1069 | int main(int argc, char* argv[]) { |
| 1070 | base::AtExitManager exit_manager; |
| 1071 | base::CommandLine::Init(argc, argv); |
| 1072 | logging::LoggingSettings log_settings; |
| 1073 | |
| 1074 | if (!logging::InitLogging(log_settings)) { |
| 1075 | LOG(ERROR) << "Failed to set up logging"; |
| 1076 | return EXIT_FAILURE; |
| 1077 | } |
| 1078 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 1079 | sp<IBluetooth> bt_iface; |
| 1080 | status_t status = getService(String16(kServiceName.c_str()), &bt_iface); |
| 1081 | if (status != OK) { |
| 1082 | LOG(ERROR) << "Failed to get service binder: '" << kServiceName |
| 1083 | << "' status=" << status; |
Arman Uguray | f520952 | 2015-08-05 21:22:39 -0700 | [diff] [blame] | 1084 | return EXIT_FAILURE; |
| 1085 | } |
| 1086 | |
Arman Uguray | 2e43414 | 2015-09-04 13:17:39 -0700 | [diff] [blame] | 1087 | sp<BluetoothDeathRecipient> dr(new BluetoothDeathRecipient()); |
| 1088 | if (android::IInterface::asBinder(bt_iface.get())->linkToDeath(dr) != |
| 1089 | android::NO_ERROR) { |
| 1090 | LOG(ERROR) << "Failed to register DeathRecipient for IBluetooth"; |
| 1091 | return EXIT_FAILURE; |
| 1092 | } |
| 1093 | |
Arman Uguray | 39a66be | 2015-08-20 12:47:55 -0700 | [diff] [blame] | 1094 | // Initialize the Binder process thread pool. We have to set this up, |
| 1095 | // otherwise, incoming callbacks from IBluetoothCallback will block the main |
| 1096 | // thread (in other words, we have to do this as we are a "Binder server"). |
| 1097 | android::ProcessState::self()->startThreadPool(); |
| 1098 | |
| 1099 | // Register Adapter state-change callback |
| 1100 | sp<CLIBluetoothCallback> callback = new CLIBluetoothCallback(); |
| 1101 | bt_iface->RegisterCallback(callback); |
| 1102 | |
Jakub Pawlowski | a4bd0d2 | 2016-02-16 11:53:14 -0800 | [diff] [blame] | 1103 | cout << COLOR_BOLDWHITE << "Fluoride Command-Line Interface\n" |
| 1104 | << COLOR_OFF << endl |
Arman Uguray | 2117e52 | 2015-08-14 17:23:47 -0700 | [diff] [blame] | 1105 | << "Type \"help\" to see possible commands.\n" |
Arman Uguray | fcf2e03 | 2015-08-13 17:05:03 -0700 | [diff] [blame] | 1106 | << endl; |
| 1107 | |
Marie Janssen | 5d80cb9 | 2016-01-21 18:45:53 -0800 | [diff] [blame] | 1108 | string command; |
| 1109 | |
| 1110 | // Add commands from the command line, if they exist. |
| 1111 | auto command_line = base::CommandLine::ForCurrentProcess(); |
| 1112 | if (command_line->HasSwitch(kExecuteLong)) { |
| 1113 | command += command_line->GetSwitchValueASCII(kExecuteLong); |
| 1114 | } |
| 1115 | |
| 1116 | if (command_line->HasSwitch(kExecuteShort)) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1117 | if (!command.empty()) command += " ; "; |
Marie Janssen | 5d80cb9 | 2016-01-21 18:45:53 -0800 | [diff] [blame] | 1118 | command += command_line->GetSwitchValueASCII(kExecuteShort); |
| 1119 | } |
| 1120 | |
Arman Uguray | fcf2e03 | 2015-08-13 17:05:03 -0700 | [diff] [blame] | 1121 | while (true) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1122 | vector<string> commands = base::SplitString( |
| 1123 | command, ";", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
Marie Janssen | 5d80cb9 | 2016-01-21 18:45:53 -0800 | [diff] [blame] | 1124 | for (string command : commands) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1125 | if (!ExecuteCommand(bt_iface, command)) break; |
Marie Janssen | 5d80cb9 | 2016-01-21 18:45:53 -0800 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | commands.clear(); |
Arman Uguray | 5192309 | 2015-08-17 17:25:04 -0700 | [diff] [blame] | 1129 | |
Arman Uguray | 39a66be | 2015-08-20 12:47:55 -0700 | [diff] [blame] | 1130 | PrintPrompt(); |
| 1131 | |
| 1132 | showing_prompt = true; |
Arman Uguray | 91c59c9 | 2015-10-13 08:06:46 -0700 | [diff] [blame] | 1133 | auto& istream = getline(cin, command); |
Arman Uguray | 39a66be | 2015-08-20 12:47:55 -0700 | [diff] [blame] | 1134 | showing_prompt = false; |
Arman Uguray | fcf2e03 | 2015-08-13 17:05:03 -0700 | [diff] [blame] | 1135 | |
Arman Uguray | 91c59c9 | 2015-10-13 08:06:46 -0700 | [diff] [blame] | 1136 | if (istream.eof() || should_exit.load()) { |
| 1137 | cout << "\nExiting" << endl; |
Arman Uguray | 2e43414 | 2015-09-04 13:17:39 -0700 | [diff] [blame] | 1138 | return EXIT_SUCCESS; |
Arman Uguray | 91c59c9 | 2015-10-13 08:06:46 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | if (!istream.good()) { |
| 1142 | LOG(ERROR) << "An error occured while reading input"; |
| 1143 | return EXIT_FAILURE; |
| 1144 | } |
Arman Uguray | fcf2e03 | 2015-08-13 17:05:03 -0700 | [diff] [blame] | 1145 | } |
Arman Uguray | f520952 | 2015-08-05 21:22:39 -0700 | [diff] [blame] | 1146 | |
| 1147 | return EXIT_SUCCESS; |
| 1148 | } |