Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2015 Google, Inc. |
| 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 | |
| 17 | #include "service/low_energy_client.h" |
| 18 | |
| 19 | #include <base/logging.h> |
| 20 | |
Jakub Pawlowski | 60b0e8f | 2016-01-12 13:51:35 -0800 | [diff] [blame] | 21 | #include "service/adapter.h" |
Arman Uguray | 1233840 | 2015-09-16 18:00:05 -0700 | [diff] [blame] | 22 | #include "stack/include/bt_types.h" |
| 23 | #include "stack/include/hcidefs.h" |
| 24 | |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 25 | using std::lock_guard; |
| 26 | using std::mutex; |
| 27 | |
| 28 | namespace bluetooth { |
| 29 | |
Arman Uguray | 1233840 | 2015-09-16 18:00:05 -0700 | [diff] [blame] | 30 | namespace { |
| 31 | |
| 32 | BLEStatus GetBLEStatus(int status) { |
| 33 | if (status == BT_STATUS_FAIL) |
| 34 | return BLE_STATUS_FAILURE; |
| 35 | |
| 36 | return static_cast<BLEStatus>(status); |
| 37 | } |
| 38 | |
| 39 | // TODO(armansito): BTIF currently expects each advertising field in a |
| 40 | // specific format passed directly in arguments. We should fix BTIF to accept |
| 41 | // the advertising data directly instead. |
| 42 | struct HALAdvertiseData { |
| 43 | std::vector<uint8_t> manufacturer_data; |
| 44 | std::vector<uint8_t> service_data; |
| 45 | std::vector<uint8_t> service_uuid; |
| 46 | }; |
| 47 | |
Arman Uguray | 9fc7d81 | 2015-10-14 12:22:27 -0700 | [diff] [blame] | 48 | bool ProcessUUID(const uint8_t* uuid_data, size_t uuid_len, UUID* out_uuid) { |
| 49 | // BTIF expects a single 128-bit UUID to be passed in little-endian form, so |
| 50 | // we need to convert into that from raw data. |
| 51 | // TODO(armansito): We have three repeated if bodies below only because UUID |
| 52 | // accepts std::array which requires constexpr lengths. We should just have a |
| 53 | // single UUID constructor that takes in an std::vector instead. |
| 54 | if (uuid_len == UUID::kNumBytes16) { |
| 55 | UUID::UUID16Bit uuid_bytes; |
| 56 | for (size_t i = 0; i < uuid_len; ++i) |
| 57 | uuid_bytes[uuid_len - i - 1] = uuid_data[i]; |
| 58 | *out_uuid = UUID(uuid_bytes); |
| 59 | } else if (uuid_len == UUID::kNumBytes32) { |
| 60 | UUID::UUID32Bit uuid_bytes; |
| 61 | for (size_t i = 0; i < uuid_len; ++i) |
| 62 | uuid_bytes[uuid_len - i - 1] = uuid_data[i]; |
| 63 | *out_uuid = UUID(uuid_bytes); |
| 64 | } else if (uuid_len == UUID::kNumBytes128) { |
| 65 | UUID::UUID128Bit uuid_bytes; |
| 66 | for (size_t i = 0; i < uuid_len; ++i) |
| 67 | uuid_bytes[uuid_len - i - 1] = uuid_data[i]; |
| 68 | *out_uuid = UUID(uuid_bytes); |
| 69 | } else { |
| 70 | LOG(ERROR) << "Invalid UUID length"; |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | return true; |
| 75 | } |
| 76 | |
Ajay Panicker | ff651b7 | 2015-09-30 15:49:47 -0700 | [diff] [blame] | 77 | bool ProcessServiceData(const uint8_t* data, |
Arman Uguray | 9fc7d81 | 2015-10-14 12:22:27 -0700 | [diff] [blame] | 78 | uint8_t uuid_len, |
Ajay Panicker | ff651b7 | 2015-09-30 15:49:47 -0700 | [diff] [blame] | 79 | HALAdvertiseData* out_data){ |
| 80 | size_t field_len = data[0]; |
| 81 | |
| 82 | // Minimum packet size should be equal to the uuid length + 1 to include |
| 83 | // the byte for the type of packet |
Arman Uguray | 9fc7d81 | 2015-10-14 12:22:27 -0700 | [diff] [blame] | 84 | if (field_len < uuid_len + 1) { |
Ajay Panicker | ff651b7 | 2015-09-30 15:49:47 -0700 | [diff] [blame] | 85 | // Invalid packet size |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | if (!out_data->service_data.empty()) { |
| 90 | // More than one Service Data is not allowed due to the limitations |
| 91 | // of the HAL API. We error in order to make sure there |
| 92 | // is no ambiguity on which data to send. |
| 93 | VLOG(1) << "More than one Service Data entry not allowed"; |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | const uint8_t* service_uuid = data + 2; |
Arman Uguray | 9fc7d81 | 2015-10-14 12:22:27 -0700 | [diff] [blame] | 98 | UUID uuid; |
| 99 | if (!ProcessUUID(service_uuid, uuid_len, &uuid)) |
| 100 | return false; |
| 101 | |
| 102 | UUID::UUID128Bit uuid_bytes = uuid.GetFullLittleEndian(); |
| 103 | const std::vector<uint8_t> temp_uuid( |
| 104 | uuid_bytes.data(), uuid_bytes.data() + uuid_bytes.size()); |
Ajay Panicker | ff651b7 | 2015-09-30 15:49:47 -0700 | [diff] [blame] | 105 | |
| 106 | // This section is to make sure that there is no UUID conflict |
| 107 | if (out_data->service_uuid.empty()) { |
| 108 | out_data->service_uuid = temp_uuid; |
| 109 | } else if (out_data->service_uuid != temp_uuid) { |
| 110 | // Mismatch in uuid passed through service data and uuid passed |
| 111 | // through uuid field |
| 112 | VLOG(1) << "More than one UUID entry not allowed"; |
| 113 | return false; |
| 114 | } // else do nothing as UUID is already properly assigned |
| 115 | |
Arman Uguray | 9fc7d81 | 2015-10-14 12:22:27 -0700 | [diff] [blame] | 116 | // Use + uuid_len + 2 here in order to skip over a |
Ajay Panicker | ff651b7 | 2015-09-30 15:49:47 -0700 | [diff] [blame] | 117 | // uuid contained in the beggining of the field |
Arman Uguray | 9fc7d81 | 2015-10-14 12:22:27 -0700 | [diff] [blame] | 118 | const uint8_t* srv_data = data + uuid_len + 2; |
Ajay Panicker | ff651b7 | 2015-09-30 15:49:47 -0700 | [diff] [blame] | 119 | |
| 120 | |
| 121 | out_data->service_data.insert( |
| 122 | out_data->service_data.begin(), |
Arman Uguray | 9fc7d81 | 2015-10-14 12:22:27 -0700 | [diff] [blame] | 123 | srv_data, srv_data + field_len - uuid_len - 1); |
Ajay Panicker | ff651b7 | 2015-09-30 15:49:47 -0700 | [diff] [blame] | 124 | |
| 125 | return true; |
| 126 | } |
| 127 | |
Arman Uguray | 1233840 | 2015-09-16 18:00:05 -0700 | [diff] [blame] | 128 | bool ProcessAdvertiseData(const AdvertiseData& adv, |
| 129 | HALAdvertiseData* out_data) { |
| 130 | CHECK(out_data); |
| 131 | CHECK(out_data->manufacturer_data.empty()); |
| 132 | CHECK(out_data->service_data.empty()); |
| 133 | CHECK(out_data->service_uuid.empty()); |
| 134 | |
| 135 | const auto& data = adv.data(); |
| 136 | size_t len = data.size(); |
| 137 | for (size_t i = 0, field_len = 0; i < len; i += (field_len + 1)) { |
| 138 | // The length byte is the first byte in the adv. "TLV" format. |
| 139 | field_len = data[i]; |
| 140 | |
| 141 | // The type byte is the next byte in the adv. "TLV" format. |
| 142 | uint8_t type = data[i + 1]; |
| 143 | size_t uuid_len = 0; |
| 144 | |
| 145 | switch (type) { |
| 146 | case HCI_EIR_MANUFACTURER_SPECIFIC_TYPE: { |
| 147 | // TODO(armansito): BTIF doesn't allow setting more than one |
| 148 | // manufacturer-specific data entry. This is something we should fix. For |
| 149 | // now, fail if more than one entry was set. |
| 150 | if (!out_data->manufacturer_data.empty()) { |
Arman Uguray | 9fc7d81 | 2015-10-14 12:22:27 -0700 | [diff] [blame] | 151 | LOG(ERROR) << "More than one Manufacturer Specific Data entry not allowed"; |
Arman Uguray | 1233840 | 2015-09-16 18:00:05 -0700 | [diff] [blame] | 152 | return false; |
| 153 | } |
| 154 | |
| 155 | // The value bytes start at the next byte in the "TLV" format. |
| 156 | const uint8_t* mnf_data = data.data() + i + 2; |
| 157 | out_data->manufacturer_data.insert( |
| 158 | out_data->manufacturer_data.begin(), |
| 159 | mnf_data, mnf_data + field_len - 1); |
| 160 | break; |
| 161 | } |
Ajay Panicker | ff651b7 | 2015-09-30 15:49:47 -0700 | [diff] [blame] | 162 | case HCI_EIR_MORE_16BITS_UUID_TYPE: |
| 163 | case HCI_EIR_COMPLETE_16BITS_UUID_TYPE: |
| 164 | case HCI_EIR_MORE_32BITS_UUID_TYPE: |
| 165 | case HCI_EIR_COMPLETE_32BITS_UUID_TYPE: |
| 166 | case HCI_EIR_MORE_128BITS_UUID_TYPE: |
| 167 | case HCI_EIR_COMPLETE_128BITS_UUID_TYPE: { |
| 168 | const uint8_t* uuid_data = data.data() + i + 2; |
Arman Uguray | 9fc7d81 | 2015-10-14 12:22:27 -0700 | [diff] [blame] | 169 | size_t uuid_len = field_len - 1; |
| 170 | UUID uuid; |
| 171 | if (!ProcessUUID(uuid_data, uuid_len, &uuid)) |
| 172 | return false; |
| 173 | |
| 174 | UUID::UUID128Bit uuid_bytes = uuid.GetFullLittleEndian(); |
Ajay Panicker | ff651b7 | 2015-09-30 15:49:47 -0700 | [diff] [blame] | 175 | |
| 176 | if (!out_data->service_uuid.empty() && |
Arman Uguray | 9fc7d81 | 2015-10-14 12:22:27 -0700 | [diff] [blame] | 177 | memcmp(out_data->service_uuid.data(), |
| 178 | uuid_bytes.data(), uuid_bytes.size()) != 0) { |
Ajay Panicker | ff651b7 | 2015-09-30 15:49:47 -0700 | [diff] [blame] | 179 | // More than one UUID is not allowed due to the limitations |
| 180 | // of the HAL API. We error in order to make sure there |
| 181 | // is no ambiguity on which UUID to send. Also makes sure that |
| 182 | // UUID Hasn't been set by service data first |
Arman Uguray | 9fc7d81 | 2015-10-14 12:22:27 -0700 | [diff] [blame] | 183 | LOG(ERROR) << "More than one UUID entry not allowed"; |
Ajay Panicker | ff651b7 | 2015-09-30 15:49:47 -0700 | [diff] [blame] | 184 | return false; |
| 185 | } |
| 186 | |
| 187 | out_data->service_uuid.assign( |
Arman Uguray | 9fc7d81 | 2015-10-14 12:22:27 -0700 | [diff] [blame] | 188 | uuid_bytes.data(), uuid_bytes.data() + UUID::kNumBytes128); |
Ajay Panicker | ff651b7 | 2015-09-30 15:49:47 -0700 | [diff] [blame] | 189 | break; |
| 190 | } |
| 191 | case HCI_EIR_SERVICE_DATA_16BITS_UUID_TYPE: { |
| 192 | if (!ProcessServiceData(data.data() + i, 2, out_data)) |
| 193 | return false; |
| 194 | break; |
| 195 | } |
| 196 | case HCI_EIR_SERVICE_DATA_32BITS_UUID_TYPE: { |
| 197 | if (!ProcessServiceData(data.data() + i, 4, out_data)) |
| 198 | return false; |
| 199 | break; |
| 200 | } |
| 201 | case HCI_EIR_SERVICE_DATA_128BITS_UUID_TYPE: { |
| 202 | if (!ProcessServiceData(data.data() + i, 16, out_data)) |
| 203 | return false; |
| 204 | break; |
| 205 | } |
Arman Uguray | 1233840 | 2015-09-16 18:00:05 -0700 | [diff] [blame] | 206 | // TODO(armansito): Support other fields. |
| 207 | default: |
| 208 | VLOG(1) << "Unrecognized EIR field: " << type; |
| 209 | return false; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | return true; |
| 214 | } |
| 215 | |
| 216 | // The Bluetooth Core Specification defines time interval (e.g. Page Scan |
| 217 | // Interval, Advertising Interval, etc) units as 0.625 milliseconds (or 1 |
| 218 | // Baseband slot). The HAL advertising functions expect the interval in this |
| 219 | // unit. This function maps an AdvertiseSettings::Mode value to the |
| 220 | // corresponding time unit. |
| 221 | int GetAdvertisingIntervalUnit(AdvertiseSettings::Mode mode) { |
| 222 | int ms; |
| 223 | |
| 224 | switch (mode) { |
| 225 | case AdvertiseSettings::MODE_BALANCED: |
| 226 | ms = kAdvertisingIntervalMediumMs; |
| 227 | break; |
| 228 | case AdvertiseSettings::MODE_LOW_LATENCY: |
| 229 | ms = kAdvertisingIntervalLowMs; |
| 230 | break; |
| 231 | case AdvertiseSettings::MODE_LOW_POWER: |
| 232 | // Fall through |
| 233 | default: |
| 234 | ms = kAdvertisingIntervalHighMs; |
| 235 | break; |
| 236 | } |
| 237 | |
| 238 | // Convert milliseconds Bluetooth units. |
| 239 | return (ms * 1000) / 625; |
| 240 | } |
| 241 | |
| 242 | struct AdvertiseParams { |
| 243 | int min_interval; |
| 244 | int max_interval; |
| 245 | int event_type; |
| 246 | int tx_power_level; |
| 247 | int timeout_s; |
| 248 | }; |
| 249 | |
| 250 | void GetAdvertiseParams(const AdvertiseSettings& settings, bool has_scan_rsp, |
| 251 | AdvertiseParams* out_params) { |
| 252 | CHECK(out_params); |
| 253 | |
| 254 | out_params->min_interval = GetAdvertisingIntervalUnit(settings.mode()); |
| 255 | out_params->max_interval = |
| 256 | out_params->min_interval + kAdvertisingIntervalDeltaUnit; |
| 257 | |
| 258 | if (settings.connectable()) |
| 259 | out_params->event_type = kAdvertisingEventTypeConnectable; |
| 260 | else if (has_scan_rsp) |
| 261 | out_params->event_type = kAdvertisingEventTypeScannable; |
| 262 | else |
| 263 | out_params->event_type = kAdvertisingEventTypeNonConnectable; |
| 264 | |
| 265 | out_params->tx_power_level = settings.tx_power_level(); |
| 266 | out_params->timeout_s = settings.timeout().InSeconds(); |
| 267 | } |
| 268 | |
| 269 | } // namespace |
| 270 | |
| 271 | // LowEnergyClient implementation |
| 272 | // ======================================================== |
| 273 | |
Jakub Pawlowski | 60b0e8f | 2016-01-12 13:51:35 -0800 | [diff] [blame] | 274 | LowEnergyClient::LowEnergyClient( |
| 275 | Adapter& adapter, const UUID& uuid, int client_id) |
| 276 | : adapter_(adapter), |
| 277 | app_identifier_(uuid), |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 278 | client_id_(client_id), |
Arman Uguray | 1233840 | 2015-09-16 18:00:05 -0700 | [diff] [blame] | 279 | adv_data_needs_update_(false), |
| 280 | scan_rsp_needs_update_(false), |
| 281 | is_setting_adv_data_(false), |
| 282 | adv_started_(false), |
| 283 | adv_start_callback_(nullptr), |
Arman Uguray | 480174f | 2015-11-30 15:36:17 -0800 | [diff] [blame^] | 284 | adv_stop_callback_(nullptr), |
| 285 | scan_started_(false) { |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | LowEnergyClient::~LowEnergyClient() { |
| 289 | // Automatically unregister the client. |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 290 | VLOG(1) << "LowEnergyClient unregistering client: " << client_id_; |
Arman Uguray | 1233840 | 2015-09-16 18:00:05 -0700 | [diff] [blame] | 291 | |
| 292 | // Unregister as observer so we no longer receive any callbacks. |
| 293 | hal::BluetoothGattInterface::Get()->RemoveClientObserver(this); |
| 294 | |
| 295 | // Stop advertising and ignore the result. |
| 296 | hal::BluetoothGattInterface::Get()-> |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 297 | GetClientHALInterface()->multi_adv_disable(client_id_); |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 298 | hal::BluetoothGattInterface::Get()-> |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 299 | GetClientHALInterface()->unregister_client(client_id_); |
Arman Uguray | 480174f | 2015-11-30 15:36:17 -0800 | [diff] [blame^] | 300 | |
| 301 | // Stop any scans started by this client. |
| 302 | if (scan_started_.load()) |
| 303 | StopScan(); |
| 304 | } |
| 305 | |
| 306 | bool LowEnergyClient::StartScan(const ScanSettings& settings, |
| 307 | const std::vector<ScanFilter>& filters) { |
| 308 | VLOG(2) << __func__; |
| 309 | |
| 310 | // Cannot start a scan if the adapter is not enabled. |
| 311 | if (!adapter_.IsEnabled()) { |
| 312 | LOG(ERROR) << "Cannot scan while Bluetooth is disabled"; |
| 313 | return false; |
| 314 | } |
| 315 | |
| 316 | // TODO(jpawlowski): Push settings and filtering logic below the HAL. |
| 317 | bt_status_t status = hal::BluetoothGattInterface::Get()-> |
| 318 | StartScan(client_id_); |
| 319 | if (status != BT_STATUS_SUCCESS) { |
| 320 | LOG(ERROR) << "Failed to initiate scanning for client: " << client_id_; |
| 321 | return false; |
| 322 | } |
| 323 | |
| 324 | scan_started_ = true; |
| 325 | return true; |
| 326 | } |
| 327 | |
| 328 | bool LowEnergyClient::StopScan() { |
| 329 | VLOG(2) << __func__; |
| 330 | |
| 331 | // TODO(armansito): We don't support batch scanning yet so call |
| 332 | // StopRegularScanForClient directly. In the future we will need to |
| 333 | // conditionally call a batch scan API here. |
| 334 | bt_status_t status = hal::BluetoothGattInterface::Get()-> |
| 335 | StopScan(client_id_); |
| 336 | if (status != BT_STATUS_SUCCESS) { |
| 337 | LOG(ERROR) << "Failed to stop scan for client: " << client_id_; |
| 338 | return false; |
| 339 | } |
| 340 | |
| 341 | scan_started_ = false; |
| 342 | return true; |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Arman Uguray | 1233840 | 2015-09-16 18:00:05 -0700 | [diff] [blame] | 345 | bool LowEnergyClient::StartAdvertising(const AdvertiseSettings& settings, |
| 346 | const AdvertiseData& advertise_data, |
| 347 | const AdvertiseData& scan_response, |
| 348 | const StatusCallback& callback) { |
| 349 | VLOG(2) << __func__; |
| 350 | lock_guard<mutex> lock(adv_fields_lock_); |
| 351 | |
| 352 | if (IsAdvertisingStarted()) { |
| 353 | LOG(WARNING) << "Already advertising"; |
| 354 | return false; |
| 355 | } |
| 356 | |
| 357 | if (IsStartingAdvertising()) { |
| 358 | LOG(WARNING) << "StartAdvertising already pending"; |
| 359 | return false; |
| 360 | } |
| 361 | |
| 362 | if (!advertise_data.IsValid()) { |
| 363 | LOG(ERROR) << "Invalid advertising data"; |
| 364 | return false; |
| 365 | } |
| 366 | |
| 367 | if (!scan_response.IsValid()) { |
| 368 | LOG(ERROR) << "Invalid scan response data"; |
| 369 | return false; |
| 370 | } |
| 371 | |
| 372 | CHECK(!adv_data_needs_update_.load()); |
| 373 | CHECK(!scan_rsp_needs_update_.load()); |
| 374 | |
| 375 | adv_data_ = advertise_data; |
| 376 | scan_response_ = scan_response; |
Jakub Pawlowski | d748ef2 | 2016-01-12 13:43:33 -0800 | [diff] [blame] | 377 | advertise_settings_ = settings; |
Arman Uguray | 1233840 | 2015-09-16 18:00:05 -0700 | [diff] [blame] | 378 | |
| 379 | AdvertiseParams params; |
| 380 | GetAdvertiseParams(settings, !scan_response_.data().empty(), ¶ms); |
| 381 | |
| 382 | bt_status_t status = hal::BluetoothGattInterface::Get()-> |
| 383 | GetClientHALInterface()->multi_adv_enable( |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 384 | client_id_, |
Arman Uguray | 1233840 | 2015-09-16 18:00:05 -0700 | [diff] [blame] | 385 | params.min_interval, |
| 386 | params.max_interval, |
| 387 | params.event_type, |
| 388 | kAdvertisingChannelAll, |
| 389 | params.tx_power_level, |
| 390 | params.timeout_s); |
| 391 | if (status != BT_STATUS_SUCCESS) { |
| 392 | LOG(ERROR) << "Failed to initiate call to enable multi-advertising"; |
| 393 | return false; |
| 394 | } |
| 395 | |
| 396 | // Always update advertising data. |
| 397 | adv_data_needs_update_ = true; |
| 398 | |
| 399 | // Update scan response only if it has data, since otherwise we just won't |
| 400 | // send ADV_SCAN_IND. |
| 401 | if (!scan_response_.data().empty()) |
| 402 | scan_rsp_needs_update_ = true; |
| 403 | |
| 404 | // OK to set this at the end since we're still holding |adv_fields_lock_|. |
| 405 | adv_start_callback_.reset(new StatusCallback(callback)); |
| 406 | |
| 407 | return true; |
| 408 | } |
| 409 | |
| 410 | bool LowEnergyClient::StopAdvertising(const StatusCallback& callback) { |
| 411 | VLOG(2) << __func__; |
| 412 | lock_guard<mutex> lock(adv_fields_lock_); |
| 413 | |
| 414 | if (!IsAdvertisingStarted()) { |
| 415 | LOG(ERROR) << "Not advertising"; |
| 416 | return false; |
| 417 | } |
| 418 | |
| 419 | if (IsStoppingAdvertising()) { |
| 420 | LOG(ERROR) << "StopAdvertising already pending"; |
| 421 | return false; |
| 422 | } |
| 423 | |
| 424 | CHECK(!adv_start_callback_); |
| 425 | |
| 426 | bt_status_t status = hal::BluetoothGattInterface::Get()-> |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 427 | GetClientHALInterface()->multi_adv_disable(client_id_); |
Arman Uguray | 1233840 | 2015-09-16 18:00:05 -0700 | [diff] [blame] | 428 | if (status != BT_STATUS_SUCCESS) { |
| 429 | LOG(ERROR) << "Failed to initiate call to disable multi-advertising"; |
| 430 | return false; |
| 431 | } |
| 432 | |
| 433 | // OK to set this at the end since we're still holding |adv_fields_lock_|. |
| 434 | adv_stop_callback_.reset(new StatusCallback(callback)); |
| 435 | |
| 436 | return true; |
| 437 | } |
| 438 | |
| 439 | bool LowEnergyClient::IsAdvertisingStarted() const { |
| 440 | return adv_started_.load(); |
| 441 | } |
| 442 | |
| 443 | bool LowEnergyClient::IsStartingAdvertising() const { |
| 444 | return !IsAdvertisingStarted() && adv_start_callback_; |
| 445 | } |
| 446 | |
| 447 | bool LowEnergyClient::IsStoppingAdvertising() const { |
| 448 | return IsAdvertisingStarted() && adv_stop_callback_; |
| 449 | } |
| 450 | |
Arman Uguray | 08f80eb | 2015-09-21 11:17:07 -0700 | [diff] [blame] | 451 | const UUID& LowEnergyClient::GetAppIdentifier() const { |
| 452 | return app_identifier_; |
| 453 | } |
| 454 | |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 455 | int LowEnergyClient::GetInstanceId() const { |
| 456 | return client_id_; |
Arman Uguray | 08f80eb | 2015-09-21 11:17:07 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Arman Uguray | 1233840 | 2015-09-16 18:00:05 -0700 | [diff] [blame] | 459 | void LowEnergyClient::MultiAdvEnableCallback( |
| 460 | hal::BluetoothGattInterface* gatt_iface, |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 461 | int client_id, int status) { |
| 462 | if (client_id != client_id_) |
Arman Uguray | 1233840 | 2015-09-16 18:00:05 -0700 | [diff] [blame] | 463 | return; |
| 464 | |
| 465 | lock_guard<mutex> lock(adv_fields_lock_); |
| 466 | |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 467 | VLOG(1) << __func__ << "client_id: " << client_id << " status: " << status; |
Arman Uguray | 1233840 | 2015-09-16 18:00:05 -0700 | [diff] [blame] | 468 | |
| 469 | CHECK(adv_start_callback_); |
| 470 | CHECK(!adv_stop_callback_); |
| 471 | |
| 472 | // Terminate operation in case of error. |
| 473 | if (status != BT_STATUS_SUCCESS) { |
| 474 | LOG(ERROR) << "Failed to enable multi-advertising"; |
| 475 | InvokeAndClearStartCallback(GetBLEStatus(status)); |
| 476 | return; |
| 477 | } |
| 478 | |
| 479 | // Now handle deferred tasks. |
| 480 | HandleDeferredAdvertiseData(gatt_iface); |
| 481 | } |
| 482 | |
| 483 | void LowEnergyClient::MultiAdvDataCallback( |
| 484 | hal::BluetoothGattInterface* gatt_iface, |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 485 | int client_id, int status) { |
| 486 | if (client_id != client_id_) |
Arman Uguray | 1233840 | 2015-09-16 18:00:05 -0700 | [diff] [blame] | 487 | return; |
| 488 | |
| 489 | lock_guard<mutex> lock(adv_fields_lock_); |
| 490 | |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 491 | VLOG(1) << __func__ << "client_id: " << client_id << " status: " << status; |
Arman Uguray | 1233840 | 2015-09-16 18:00:05 -0700 | [diff] [blame] | 492 | |
| 493 | is_setting_adv_data_ = false; |
| 494 | |
| 495 | // Terminate operation in case of error. |
| 496 | if (status != BT_STATUS_SUCCESS) { |
| 497 | LOG(ERROR) << "Failed to set advertising data"; |
| 498 | InvokeAndClearStartCallback(GetBLEStatus(status)); |
| 499 | return; |
| 500 | } |
| 501 | |
| 502 | // Now handle deferred tasks. |
| 503 | HandleDeferredAdvertiseData(gatt_iface); |
| 504 | } |
| 505 | |
| 506 | void LowEnergyClient::MultiAdvDisableCallback( |
| 507 | hal::BluetoothGattInterface* /* gatt_iface */, |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 508 | int client_id, int status) { |
| 509 | if (client_id != client_id_) |
Arman Uguray | 1233840 | 2015-09-16 18:00:05 -0700 | [diff] [blame] | 510 | return; |
| 511 | |
| 512 | lock_guard<mutex> lock(adv_fields_lock_); |
| 513 | |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 514 | VLOG(1) << __func__ << "client_id: " << client_id << " status: " << status; |
Arman Uguray | 1233840 | 2015-09-16 18:00:05 -0700 | [diff] [blame] | 515 | |
| 516 | CHECK(!adv_start_callback_); |
| 517 | CHECK(adv_stop_callback_); |
| 518 | |
| 519 | if (status == BT_STATUS_SUCCESS) { |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 520 | VLOG(1) << "Multi-advertising stopped for client_id: " << client_id; |
Arman Uguray | 1233840 | 2015-09-16 18:00:05 -0700 | [diff] [blame] | 521 | adv_started_ = false; |
| 522 | } else { |
| 523 | LOG(ERROR) << "Failed to stop multi-advertising"; |
| 524 | } |
| 525 | |
| 526 | InvokeAndClearStopCallback(GetBLEStatus(status)); |
| 527 | } |
| 528 | |
| 529 | bt_status_t LowEnergyClient::SetAdvertiseData( |
| 530 | hal::BluetoothGattInterface* gatt_iface, |
| 531 | const AdvertiseData& data, |
| 532 | bool set_scan_rsp) { |
| 533 | VLOG(2) << __func__; |
| 534 | |
| 535 | HALAdvertiseData hal_data; |
| 536 | |
| 537 | // TODO(armansito): The stack should check that the length is valid when other |
| 538 | // fields inserted by the stack (e.g. flags, device name, tx-power) are taken |
| 539 | // into account. At the moment we are skipping this check; this means that if |
| 540 | // the given data is too long then the stack will truncate it. |
| 541 | if (!ProcessAdvertiseData(data, &hal_data)) { |
Ajay Panicker | ff651b7 | 2015-09-30 15:49:47 -0700 | [diff] [blame] | 542 | LOG(ERROR) << "Malformed advertise data given"; |
Arman Uguray | 1233840 | 2015-09-16 18:00:05 -0700 | [diff] [blame] | 543 | return BT_STATUS_FAIL; |
| 544 | } |
| 545 | |
| 546 | if (is_setting_adv_data_.load()) { |
Ajay Panicker | ff651b7 | 2015-09-30 15:49:47 -0700 | [diff] [blame] | 547 | LOG(ERROR) << "Setting advertising data already in progress."; |
Arman Uguray | 1233840 | 2015-09-16 18:00:05 -0700 | [diff] [blame] | 548 | return BT_STATUS_FAIL; |
| 549 | } |
| 550 | |
| 551 | // TODO(armansito): The length fields in the BTIF function below are signed |
| 552 | // integers so a call to std::vector::size might get capped. This is very |
| 553 | // unlikely anyway but it's safer to stop using signed-integer types for |
| 554 | // length in APIs, so we should change that. |
| 555 | bt_status_t status = gatt_iface->GetClientHALInterface()-> |
| 556 | multi_adv_set_inst_data( |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 557 | client_id_, |
Arman Uguray | 1233840 | 2015-09-16 18:00:05 -0700 | [diff] [blame] | 558 | set_scan_rsp, |
| 559 | data.include_device_name(), |
| 560 | data.include_tx_power_level(), |
| 561 | 0, // This is what Bluetooth.apk current hardcodes for "appearance". |
| 562 | hal_data.manufacturer_data.size(), |
| 563 | reinterpret_cast<char*>(hal_data.manufacturer_data.data()), |
Ajay Panicker | ff651b7 | 2015-09-30 15:49:47 -0700 | [diff] [blame] | 564 | hal_data.service_data.size(), |
| 565 | reinterpret_cast<char*>(hal_data.service_data.data()), |
| 566 | hal_data.service_uuid.size(), |
| 567 | reinterpret_cast<char*>(hal_data.service_uuid.data())); |
| 568 | |
Arman Uguray | 1233840 | 2015-09-16 18:00:05 -0700 | [diff] [blame] | 569 | if (status != BT_STATUS_SUCCESS) { |
| 570 | LOG(ERROR) << "Failed to set instance advertising data."; |
| 571 | return status; |
| 572 | } |
| 573 | |
| 574 | if (set_scan_rsp) |
| 575 | scan_rsp_needs_update_ = false; |
| 576 | else |
| 577 | adv_data_needs_update_ = false; |
| 578 | |
| 579 | is_setting_adv_data_ = true; |
| 580 | |
| 581 | return status; |
| 582 | } |
| 583 | |
| 584 | void LowEnergyClient::HandleDeferredAdvertiseData( |
| 585 | hal::BluetoothGattInterface* gatt_iface) { |
| 586 | VLOG(2) << __func__; |
| 587 | |
| 588 | CHECK(!IsAdvertisingStarted()); |
| 589 | CHECK(!IsStoppingAdvertising()); |
| 590 | CHECK(IsStartingAdvertising()); |
| 591 | CHECK(!is_setting_adv_data_.load()); |
| 592 | |
| 593 | if (adv_data_needs_update_.load()) { |
| 594 | bt_status_t status = SetAdvertiseData(gatt_iface, adv_data_, false); |
| 595 | if (status != BT_STATUS_SUCCESS) { |
| 596 | LOG(ERROR) << "Failed setting advertisement data"; |
| 597 | InvokeAndClearStartCallback(GetBLEStatus(status)); |
| 598 | } |
| 599 | return; |
| 600 | } |
| 601 | |
| 602 | if (scan_rsp_needs_update_.load()) { |
| 603 | bt_status_t status = SetAdvertiseData(gatt_iface, scan_response_, true); |
| 604 | if (status != BT_STATUS_SUCCESS) { |
| 605 | LOG(ERROR) << "Failed setting scan response data"; |
| 606 | InvokeAndClearStartCallback(GetBLEStatus(status)); |
| 607 | } |
| 608 | return; |
| 609 | } |
| 610 | |
| 611 | // All pending tasks are complete. Report success. |
| 612 | adv_started_ = true; |
| 613 | InvokeAndClearStartCallback(BLE_STATUS_SUCCESS); |
| 614 | } |
| 615 | |
| 616 | void LowEnergyClient::InvokeAndClearStartCallback(BLEStatus status) { |
| 617 | adv_data_needs_update_ = false; |
| 618 | scan_rsp_needs_update_ = false; |
| 619 | |
| 620 | // We allow NULL callbacks. |
| 621 | if (*adv_start_callback_) |
| 622 | (*adv_start_callback_)(status); |
| 623 | |
| 624 | adv_start_callback_ = nullptr; |
| 625 | } |
| 626 | |
| 627 | void LowEnergyClient::InvokeAndClearStopCallback(BLEStatus status) { |
| 628 | // We allow NULL callbacks. |
| 629 | if (*adv_stop_callback_) |
| 630 | (*adv_stop_callback_)(status); |
| 631 | |
| 632 | adv_stop_callback_ = nullptr; |
| 633 | } |
| 634 | |
| 635 | // LowEnergyClientFactory implementation |
| 636 | // ======================================================== |
| 637 | |
Jakub Pawlowski | 60b0e8f | 2016-01-12 13:51:35 -0800 | [diff] [blame] | 638 | LowEnergyClientFactory::LowEnergyClientFactory(Adapter& adapter) |
| 639 | : adapter_(adapter) { |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 640 | hal::BluetoothGattInterface::Get()->AddClientObserver(this); |
| 641 | } |
| 642 | |
| 643 | LowEnergyClientFactory::~LowEnergyClientFactory() { |
| 644 | hal::BluetoothGattInterface::Get()->RemoveClientObserver(this); |
| 645 | } |
| 646 | |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 647 | bool LowEnergyClientFactory::RegisterInstance( |
| 648 | const UUID& uuid, |
| 649 | const RegisterCallback& callback) { |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 650 | VLOG(1) << __func__ << " - UUID: " << uuid.ToString(); |
| 651 | lock_guard<mutex> lock(pending_calls_lock_); |
| 652 | |
| 653 | if (pending_calls_.find(uuid) != pending_calls_.end()) { |
| 654 | LOG(ERROR) << "Low-Energy client with given UUID already registered - " |
| 655 | << "UUID: " << uuid.ToString(); |
| 656 | return false; |
| 657 | } |
| 658 | |
| 659 | const btgatt_client_interface_t* hal_iface = |
| 660 | hal::BluetoothGattInterface::Get()->GetClientHALInterface(); |
| 661 | bt_uuid_t app_uuid = uuid.GetBlueDroid(); |
| 662 | |
| 663 | if (hal_iface->register_client(&app_uuid) != BT_STATUS_SUCCESS) |
| 664 | return false; |
| 665 | |
| 666 | pending_calls_[uuid] = callback; |
| 667 | |
| 668 | return true; |
| 669 | } |
| 670 | |
| 671 | void LowEnergyClientFactory::RegisterClientCallback( |
Arman Uguray | 1233840 | 2015-09-16 18:00:05 -0700 | [diff] [blame] | 672 | hal::BluetoothGattInterface* gatt_iface, |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 673 | int status, int client_id, |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 674 | const bt_uuid_t& app_uuid) { |
| 675 | UUID uuid(app_uuid); |
| 676 | |
| 677 | VLOG(1) << __func__ << " - UUID: " << uuid.ToString(); |
| 678 | lock_guard<mutex> lock(pending_calls_lock_); |
| 679 | |
| 680 | auto iter = pending_calls_.find(uuid); |
| 681 | if (iter == pending_calls_.end()) { |
| 682 | VLOG(1) << "Ignoring callback for unknown app_id: " << uuid.ToString(); |
| 683 | return; |
| 684 | } |
| 685 | |
| 686 | // No need to construct a client if the call wasn't successful. |
| 687 | std::unique_ptr<LowEnergyClient> client; |
| 688 | BLEStatus result = BLE_STATUS_FAILURE; |
| 689 | if (status == BT_STATUS_SUCCESS) { |
Jakub Pawlowski | 60b0e8f | 2016-01-12 13:51:35 -0800 | [diff] [blame] | 690 | client.reset(new LowEnergyClient(adapter_, uuid, client_id)); |
Arman Uguray | 1233840 | 2015-09-16 18:00:05 -0700 | [diff] [blame] | 691 | |
| 692 | // Use the unsafe variant to register this as an observer, since |
| 693 | // LowEnergyClient instances only get created by LowEnergyClientCallback |
| 694 | // from inside this GATT client observer event, which would otherwise cause |
| 695 | // a deadlock. |
| 696 | gatt_iface->AddClientObserverUnsafe(client.get()); |
| 697 | |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 698 | result = BLE_STATUS_SUCCESS; |
| 699 | } |
| 700 | |
Arman Uguray | 08f80eb | 2015-09-21 11:17:07 -0700 | [diff] [blame] | 701 | // Notify the result via the result callback. |
Arman Uguray | c2fc0f2 | 2015-09-03 15:09:41 -0700 | [diff] [blame] | 702 | iter->second(result, uuid, std::move(client)); |
| 703 | |
| 704 | pending_calls_.erase(iter); |
| 705 | } |
| 706 | |
| 707 | } // namespace bluetooth |