2 // Copyright (C) 2015 Google, Inc.
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:
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "service/low_energy_client.h"
19 #include <base/logging.h>
21 #include "service/adapter.h"
22 #include "service/common/bluetooth/util/address_helper.h"
23 #include "service/logging_helpers.h"
24 #include "stack/include/bt_types.h"
25 #include "stack/include/hcidefs.h"
27 using std::lock_guard;
34 // 31 + 31 for advertising data and scan response. This is the maximum length
35 // TODO(armansito): Fix the HAL to return a concatenated blob that contains the
36 // true length of each field and also provide a length parameter so that we
37 // can support advertising length extensions in the future.
38 const size_t kScanRecordLength = 62;
40 BLEStatus GetBLEStatus(int status) {
41 if (status == BT_STATUS_FAIL)
42 return BLE_STATUS_FAILURE;
44 return static_cast<BLEStatus>(status);
47 // Returns the length of the given scan record array. We have to calculate this
48 // based on the maximum possible data length and the TLV data. See TODO above
49 // |kScanRecordLength|.
50 size_t GetScanRecordLength(uint8_t* bytes) {
51 for (size_t i = 0, field_len = 0; i < kScanRecordLength;
52 i += (field_len + 1)) {
55 // Assert here that the data returned from the stack is correctly formatted
56 // in TLV form and that the length of the current field won't exceed the
58 CHECK(i + field_len < kScanRecordLength);
60 // If the field length is zero and we haven't reached the maximum length,
61 // then we have found the length, as the stack will pad the data with zeros
67 // We have reached the end.
68 return kScanRecordLength;
71 // TODO(armansito): BTIF currently expects each advertising field in a
72 // specific format passed directly in arguments. We should fix BTIF to accept
73 // the advertising data directly instead.
74 struct HALAdvertiseData {
75 std::vector<uint8_t> manufacturer_data;
76 std::vector<uint8_t> service_data;
77 std::vector<uint8_t> service_uuid;
80 bool ProcessUUID(const uint8_t* uuid_data, size_t uuid_len, UUID* out_uuid) {
81 // BTIF expects a single 128-bit UUID to be passed in little-endian form, so
82 // we need to convert into that from raw data.
83 // TODO(armansito): We have three repeated if bodies below only because UUID
84 // accepts std::array which requires constexpr lengths. We should just have a
85 // single UUID constructor that takes in an std::vector instead.
86 if (uuid_len == UUID::kNumBytes16) {
87 UUID::UUID16Bit uuid_bytes;
88 for (size_t i = 0; i < uuid_len; ++i)
89 uuid_bytes[uuid_len - i - 1] = uuid_data[i];
90 *out_uuid = UUID(uuid_bytes);
91 } else if (uuid_len == UUID::kNumBytes32) {
92 UUID::UUID32Bit uuid_bytes;
93 for (size_t i = 0; i < uuid_len; ++i)
94 uuid_bytes[uuid_len - i - 1] = uuid_data[i];
95 *out_uuid = UUID(uuid_bytes);
96 } else if (uuid_len == UUID::kNumBytes128) {
97 UUID::UUID128Bit uuid_bytes;
98 for (size_t i = 0; i < uuid_len; ++i)
99 uuid_bytes[uuid_len - i - 1] = uuid_data[i];
100 *out_uuid = UUID(uuid_bytes);
102 LOG(ERROR) << "Invalid UUID length";
109 bool ProcessServiceData(const uint8_t* data,
111 HALAdvertiseData* out_data) {
112 size_t field_len = data[0];
114 // Minimum packet size should be equal to the uuid length + 1 to include
115 // the byte for the type of packet
116 if (field_len < uuid_len + 1) {
117 // Invalid packet size
121 if (!out_data->service_data.empty()) {
122 // More than one Service Data is not allowed due to the limitations
123 // of the HAL API. We error in order to make sure there
124 // is no ambiguity on which data to send.
125 VLOG(1) << "More than one Service Data entry not allowed";
129 const uint8_t* service_uuid = data + 2;
131 if (!ProcessUUID(service_uuid, uuid_len, &uuid))
134 UUID::UUID128Bit uuid_bytes = uuid.GetFullLittleEndian();
135 const std::vector<uint8_t> temp_uuid(
136 uuid_bytes.data(), uuid_bytes.data() + uuid_bytes.size());
138 // This section is to make sure that there is no UUID conflict
139 if (out_data->service_uuid.empty()) {
140 out_data->service_uuid = temp_uuid;
141 } else if (out_data->service_uuid != temp_uuid) {
142 // Mismatch in uuid passed through service data and uuid passed
143 // through uuid field
144 VLOG(1) << "More than one UUID entry not allowed";
146 } // else do nothing as UUID is already properly assigned
148 // Use + uuid_len + 2 here in order to skip over a
149 // uuid contained in the beggining of the field
150 const uint8_t* srv_data = data + uuid_len + 2;
153 out_data->service_data.insert(
154 out_data->service_data.begin(),
155 srv_data, srv_data + field_len - uuid_len - 1);
160 bool ProcessAdvertiseData(const AdvertiseData& adv,
161 HALAdvertiseData* out_data) {
163 CHECK(out_data->manufacturer_data.empty());
164 CHECK(out_data->service_data.empty());
165 CHECK(out_data->service_uuid.empty());
167 const auto& data = adv.data();
168 size_t len = data.size();
169 for (size_t i = 0, field_len = 0; i < len; i += (field_len + 1)) {
170 // The length byte is the first byte in the adv. "TLV" format.
173 // The type byte is the next byte in the adv. "TLV" format.
174 uint8_t type = data[i + 1];
178 case HCI_EIR_MANUFACTURER_SPECIFIC_TYPE: {
179 // TODO(armansito): BTIF doesn't allow setting more than one
180 // manufacturer-specific data entry. This is something we should fix. For
181 // now, fail if more than one entry was set.
182 if (!out_data->manufacturer_data.empty()) {
183 LOG(ERROR) << "More than one Manufacturer Specific Data entry not allowed";
187 // The value bytes start at the next byte in the "TLV" format.
188 const uint8_t* mnf_data = data.data() + i + 2;
189 out_data->manufacturer_data.insert(
190 out_data->manufacturer_data.begin(),
191 mnf_data, mnf_data + field_len - 1);
194 case HCI_EIR_MORE_16BITS_UUID_TYPE:
195 case HCI_EIR_COMPLETE_16BITS_UUID_TYPE:
196 case HCI_EIR_MORE_32BITS_UUID_TYPE:
197 case HCI_EIR_COMPLETE_32BITS_UUID_TYPE:
198 case HCI_EIR_MORE_128BITS_UUID_TYPE:
199 case HCI_EIR_COMPLETE_128BITS_UUID_TYPE: {
200 const uint8_t* uuid_data = data.data() + i + 2;
201 size_t uuid_len = field_len - 1;
203 if (!ProcessUUID(uuid_data, uuid_len, &uuid))
206 UUID::UUID128Bit uuid_bytes = uuid.GetFullLittleEndian();
208 if (!out_data->service_uuid.empty() &&
209 memcmp(out_data->service_uuid.data(),
210 uuid_bytes.data(), uuid_bytes.size()) != 0) {
211 // More than one UUID is not allowed due to the limitations
212 // of the HAL API. We error in order to make sure there
213 // is no ambiguity on which UUID to send. Also makes sure that
214 // UUID Hasn't been set by service data first
215 LOG(ERROR) << "More than one UUID entry not allowed";
219 out_data->service_uuid.assign(
220 uuid_bytes.data(), uuid_bytes.data() + UUID::kNumBytes128);
223 case HCI_EIR_SERVICE_DATA_16BITS_UUID_TYPE: {
224 if (!ProcessServiceData(data.data() + i, 2, out_data))
228 case HCI_EIR_SERVICE_DATA_32BITS_UUID_TYPE: {
229 if (!ProcessServiceData(data.data() + i, 4, out_data))
233 case HCI_EIR_SERVICE_DATA_128BITS_UUID_TYPE: {
234 if (!ProcessServiceData(data.data() + i, 16, out_data))
238 // TODO(armansito): Support other fields.
240 VLOG(1) << "Unrecognized EIR field: " << type;
248 // The Bluetooth Core Specification defines time interval (e.g. Page Scan
249 // Interval, Advertising Interval, etc) units as 0.625 milliseconds (or 1
250 // Baseband slot). The HAL advertising functions expect the interval in this
251 // unit. This function maps an AdvertiseSettings::Mode value to the
252 // corresponding time unit.
253 int GetAdvertisingIntervalUnit(AdvertiseSettings::Mode mode) {
257 case AdvertiseSettings::MODE_BALANCED:
258 ms = kAdvertisingIntervalMediumMs;
260 case AdvertiseSettings::MODE_LOW_LATENCY:
261 ms = kAdvertisingIntervalLowMs;
263 case AdvertiseSettings::MODE_LOW_POWER:
266 ms = kAdvertisingIntervalHighMs;
270 // Convert milliseconds Bluetooth units.
271 return (ms * 1000) / 625;
274 struct AdvertiseParams {
282 void GetAdvertiseParams(const AdvertiseSettings& settings, bool has_scan_rsp,
283 AdvertiseParams* out_params) {
286 out_params->min_interval = GetAdvertisingIntervalUnit(settings.mode());
287 out_params->max_interval =
288 out_params->min_interval + kAdvertisingIntervalDeltaUnit;
290 if (settings.connectable())
291 out_params->event_type = kAdvertisingEventTypeConnectable;
292 else if (has_scan_rsp)
293 out_params->event_type = kAdvertisingEventTypeScannable;
295 out_params->event_type = kAdvertisingEventTypeNonConnectable;
297 out_params->tx_power_level = settings.tx_power_level();
298 out_params->timeout_s = settings.timeout().InSeconds();
303 // LowEnergyClient implementation
304 // ========================================================
306 LowEnergyClient::LowEnergyClient(
307 Adapter& adapter, const UUID& uuid, int client_id)
309 app_identifier_(uuid),
310 client_id_(client_id),
311 adv_data_needs_update_(false),
312 scan_rsp_needs_update_(false),
313 is_setting_adv_data_(false),
315 adv_start_callback_(nullptr),
316 adv_stop_callback_(nullptr),
317 scan_started_(false) {
320 LowEnergyClient::~LowEnergyClient() {
321 // Automatically unregister the client.
322 VLOG(1) << "LowEnergyClient unregistering client: " << client_id_;
324 // Unregister as observer so we no longer receive any callbacks.
325 hal::BluetoothGattInterface::Get()->RemoveClientObserver(this);
327 // Stop advertising and ignore the result.
328 hal::BluetoothGattInterface::Get()->
329 GetClientHALInterface()->multi_adv_disable(client_id_);
330 hal::BluetoothGattInterface::Get()->
331 GetClientHALInterface()->unregister_client(client_id_);
333 // Stop any scans started by this client.
334 if (scan_started_.load())
338 bool LowEnergyClient::Connect(std::string address, bool is_direct) {
339 VLOG(2) << __func__ << "Address: " << address << " is_direct: " << is_direct;
342 util::BdAddrFromString(address, &bda);
344 bt_status_t status = hal::BluetoothGattInterface::Get()->
345 GetClientHALInterface()->connect(client_id_, &bda, is_direct,
347 if (status != BT_STATUS_SUCCESS) {
348 LOG(ERROR) << "HAL call to connect failed";
355 bool LowEnergyClient::Disconnect(std::string address) {
356 VLOG(2) << __func__ << "Address: " << address;
359 util::BdAddrFromString(address, &bda);
361 std::map<const bt_bdaddr_t, int>::iterator conn_id;
363 lock_guard<mutex> lock(connection_fields_lock_);
364 conn_id = connection_ids_.find(bda);
365 if (conn_id == connection_ids_.end()) {
366 LOG(WARNING) << "Can't disconnect, no existing connection to " << address;
371 bt_status_t status = hal::BluetoothGattInterface::Get()->
372 GetClientHALInterface()->disconnect(client_id_, &bda, conn_id->second);
373 if (status != BT_STATUS_SUCCESS) {
374 LOG(ERROR) << "HAL call to disconnect failed";
381 void LowEnergyClient::SetDelegate(Delegate* delegate) {
382 lock_guard<mutex> lock(delegate_mutex_);
383 delegate_ = delegate;
386 bool LowEnergyClient::StartScan(const ScanSettings& settings,
387 const std::vector<ScanFilter>& filters) {
390 // Cannot start a scan if the adapter is not enabled.
391 if (!adapter_.IsEnabled()) {
392 LOG(ERROR) << "Cannot scan while Bluetooth is disabled";
396 // TODO(jpawlowski): Push settings and filtering logic below the HAL.
397 bt_status_t status = hal::BluetoothGattInterface::Get()->
398 StartScan(client_id_);
399 if (status != BT_STATUS_SUCCESS) {
400 LOG(ERROR) << "Failed to initiate scanning for client: " << client_id_;
404 scan_started_ = true;
408 bool LowEnergyClient::StopScan() {
411 // TODO(armansito): We don't support batch scanning yet so call
412 // StopRegularScanForClient directly. In the future we will need to
413 // conditionally call a batch scan API here.
414 bt_status_t status = hal::BluetoothGattInterface::Get()->
415 StopScan(client_id_);
416 if (status != BT_STATUS_SUCCESS) {
417 LOG(ERROR) << "Failed to stop scan for client: " << client_id_;
421 scan_started_ = false;
425 bool LowEnergyClient::StartAdvertising(const AdvertiseSettings& settings,
426 const AdvertiseData& advertise_data,
427 const AdvertiseData& scan_response,
428 const StatusCallback& callback) {
430 lock_guard<mutex> lock(adv_fields_lock_);
432 if (IsAdvertisingStarted()) {
433 LOG(WARNING) << "Already advertising";
437 if (IsStartingAdvertising()) {
438 LOG(WARNING) << "StartAdvertising already pending";
442 if (!advertise_data.IsValid()) {
443 LOG(ERROR) << "Invalid advertising data";
447 if (!scan_response.IsValid()) {
448 LOG(ERROR) << "Invalid scan response data";
452 CHECK(!adv_data_needs_update_.load());
453 CHECK(!scan_rsp_needs_update_.load());
455 adv_data_ = advertise_data;
456 scan_response_ = scan_response;
457 advertise_settings_ = settings;
459 AdvertiseParams params;
460 GetAdvertiseParams(settings, !scan_response_.data().empty(), ¶ms);
462 bt_status_t status = hal::BluetoothGattInterface::Get()->
463 GetClientHALInterface()->multi_adv_enable(
468 kAdvertisingChannelAll,
469 params.tx_power_level,
471 if (status != BT_STATUS_SUCCESS) {
472 LOG(ERROR) << "Failed to initiate call to enable multi-advertising";
476 // Always update advertising data.
477 adv_data_needs_update_ = true;
479 // Update scan response only if it has data, since otherwise we just won't
480 // send ADV_SCAN_IND.
481 if (!scan_response_.data().empty())
482 scan_rsp_needs_update_ = true;
484 // OK to set this at the end since we're still holding |adv_fields_lock_|.
485 adv_start_callback_.reset(new StatusCallback(callback));
490 bool LowEnergyClient::StopAdvertising(const StatusCallback& callback) {
492 lock_guard<mutex> lock(adv_fields_lock_);
494 if (!IsAdvertisingStarted()) {
495 LOG(ERROR) << "Not advertising";
499 if (IsStoppingAdvertising()) {
500 LOG(ERROR) << "StopAdvertising already pending";
504 CHECK(!adv_start_callback_);
506 bt_status_t status = hal::BluetoothGattInterface::Get()->
507 GetClientHALInterface()->multi_adv_disable(client_id_);
508 if (status != BT_STATUS_SUCCESS) {
509 LOG(ERROR) << "Failed to initiate call to disable multi-advertising";
513 // OK to set this at the end since we're still holding |adv_fields_lock_|.
514 adv_stop_callback_.reset(new StatusCallback(callback));
519 bool LowEnergyClient::IsAdvertisingStarted() const {
520 return adv_started_.load();
523 bool LowEnergyClient::IsStartingAdvertising() const {
524 return !IsAdvertisingStarted() && adv_start_callback_;
527 bool LowEnergyClient::IsStoppingAdvertising() const {
528 return IsAdvertisingStarted() && adv_stop_callback_;
531 const UUID& LowEnergyClient::GetAppIdentifier() const {
532 return app_identifier_;
535 int LowEnergyClient::GetInstanceId() const {
539 void LowEnergyClient::ScanResultCallback(
540 hal::BluetoothGattInterface* gatt_iface,
541 const bt_bdaddr_t& bda, int rssi, uint8_t* adv_data) {
542 // Ignore scan results if this client didn't start a scan.
543 if (!scan_started_.load())
546 lock_guard<mutex> lock(delegate_mutex_);
550 // TODO(armansito): Apply software filters here.
552 size_t record_len = GetScanRecordLength(adv_data);
553 std::vector<uint8_t> scan_record(adv_data, adv_data + record_len);
555 ScanResult result(BtAddrString(&bda), scan_record, rssi);
557 delegate_->OnScanResult(this, result);
560 void LowEnergyClient::ConnectCallback(
561 hal::BluetoothGattInterface* gatt_iface, int conn_id, int status,
562 int client_id, const bt_bdaddr_t& bda) {
563 if (client_id != client_id_)
566 VLOG(1) << __func__ << "client_id: " << client_id << " status: " << status;
569 lock_guard<mutex> lock(connection_fields_lock_);
570 auto success = connection_ids_.emplace(bda, conn_id);
571 if (!success.second) {
572 LOG(ERROR) << __func__ << " Insertion into connection_ids_ failed!";
577 delegate_->OnConnectionState(this, status, BtAddrString(&bda).c_str(),
581 void LowEnergyClient::DisconnectCallback(
582 hal::BluetoothGattInterface* gatt_iface, int conn_id, int status,
583 int client_id, const bt_bdaddr_t& bda) {
584 if (client_id != client_id_)
587 VLOG(1) << __func__ << " client_id: " << client_id << " status: " << status;
589 lock_guard<mutex> lock(connection_fields_lock_);
590 if (!connection_ids_.erase(bda)) {
591 LOG(ERROR) << __func__ << " Erasing from connection_ids_ failed!";
596 delegate_->OnConnectionState(this, status, BtAddrString(&bda).c_str(),
600 void LowEnergyClient::MultiAdvEnableCallback(
601 hal::BluetoothGattInterface* gatt_iface,
602 int client_id, int status) {
603 if (client_id != client_id_)
606 lock_guard<mutex> lock(adv_fields_lock_);
608 VLOG(1) << __func__ << "client_id: " << client_id << " status: " << status;
610 CHECK(adv_start_callback_);
611 CHECK(!adv_stop_callback_);
613 // Terminate operation in case of error.
614 if (status != BT_STATUS_SUCCESS) {
615 LOG(ERROR) << "Failed to enable multi-advertising";
616 InvokeAndClearStartCallback(GetBLEStatus(status));
620 // Now handle deferred tasks.
621 HandleDeferredAdvertiseData(gatt_iface);
624 void LowEnergyClient::MultiAdvDataCallback(
625 hal::BluetoothGattInterface* gatt_iface,
626 int client_id, int status) {
627 if (client_id != client_id_)
630 lock_guard<mutex> lock(adv_fields_lock_);
632 VLOG(1) << __func__ << "client_id: " << client_id << " status: " << status;
634 is_setting_adv_data_ = false;
636 // Terminate operation in case of error.
637 if (status != BT_STATUS_SUCCESS) {
638 LOG(ERROR) << "Failed to set advertising data";
639 InvokeAndClearStartCallback(GetBLEStatus(status));
643 // Now handle deferred tasks.
644 HandleDeferredAdvertiseData(gatt_iface);
647 void LowEnergyClient::MultiAdvDisableCallback(
648 hal::BluetoothGattInterface* /* gatt_iface */,
649 int client_id, int status) {
650 if (client_id != client_id_)
653 lock_guard<mutex> lock(adv_fields_lock_);
655 VLOG(1) << __func__ << "client_id: " << client_id << " status: " << status;
657 CHECK(!adv_start_callback_);
658 CHECK(adv_stop_callback_);
660 if (status == BT_STATUS_SUCCESS) {
661 VLOG(1) << "Multi-advertising stopped for client_id: " << client_id;
662 adv_started_ = false;
664 LOG(ERROR) << "Failed to stop multi-advertising";
667 InvokeAndClearStopCallback(GetBLEStatus(status));
670 bt_status_t LowEnergyClient::SetAdvertiseData(
671 hal::BluetoothGattInterface* gatt_iface,
672 const AdvertiseData& data,
676 HALAdvertiseData hal_data;
678 // TODO(armansito): The stack should check that the length is valid when other
679 // fields inserted by the stack (e.g. flags, device name, tx-power) are taken
680 // into account. At the moment we are skipping this check; this means that if
681 // the given data is too long then the stack will truncate it.
682 if (!ProcessAdvertiseData(data, &hal_data)) {
683 LOG(ERROR) << "Malformed advertise data given";
684 return BT_STATUS_FAIL;
687 if (is_setting_adv_data_.load()) {
688 LOG(ERROR) << "Setting advertising data already in progress.";
689 return BT_STATUS_FAIL;
692 // TODO(armansito): The length fields in the BTIF function below are signed
693 // integers so a call to std::vector::size might get capped. This is very
694 // unlikely anyway but it's safer to stop using signed-integer types for
695 // length in APIs, so we should change that.
696 bt_status_t status = gatt_iface->GetClientHALInterface()->
697 multi_adv_set_inst_data(
700 data.include_device_name(),
701 data.include_tx_power_level(),
702 0, // This is what Bluetooth.apk current hardcodes for "appearance".
703 hal_data.manufacturer_data.size(),
704 reinterpret_cast<char*>(hal_data.manufacturer_data.data()),
705 hal_data.service_data.size(),
706 reinterpret_cast<char*>(hal_data.service_data.data()),
707 hal_data.service_uuid.size(),
708 reinterpret_cast<char*>(hal_data.service_uuid.data()));
710 if (status != BT_STATUS_SUCCESS) {
711 LOG(ERROR) << "Failed to set instance advertising data.";
716 scan_rsp_needs_update_ = false;
718 adv_data_needs_update_ = false;
720 is_setting_adv_data_ = true;
725 void LowEnergyClient::HandleDeferredAdvertiseData(
726 hal::BluetoothGattInterface* gatt_iface) {
729 CHECK(!IsAdvertisingStarted());
730 CHECK(!IsStoppingAdvertising());
731 CHECK(IsStartingAdvertising());
732 CHECK(!is_setting_adv_data_.load());
734 if (adv_data_needs_update_.load()) {
735 bt_status_t status = SetAdvertiseData(gatt_iface, adv_data_, false);
736 if (status != BT_STATUS_SUCCESS) {
737 LOG(ERROR) << "Failed setting advertisement data";
738 InvokeAndClearStartCallback(GetBLEStatus(status));
743 if (scan_rsp_needs_update_.load()) {
744 bt_status_t status = SetAdvertiseData(gatt_iface, scan_response_, true);
745 if (status != BT_STATUS_SUCCESS) {
746 LOG(ERROR) << "Failed setting scan response data";
747 InvokeAndClearStartCallback(GetBLEStatus(status));
752 // All pending tasks are complete. Report success.
754 InvokeAndClearStartCallback(BLE_STATUS_SUCCESS);
757 void LowEnergyClient::InvokeAndClearStartCallback(BLEStatus status) {
758 adv_data_needs_update_ = false;
759 scan_rsp_needs_update_ = false;
761 // We allow NULL callbacks.
762 if (*adv_start_callback_)
763 (*adv_start_callback_)(status);
765 adv_start_callback_ = nullptr;
768 void LowEnergyClient::InvokeAndClearStopCallback(BLEStatus status) {
769 // We allow NULL callbacks.
770 if (*adv_stop_callback_)
771 (*adv_stop_callback_)(status);
773 adv_stop_callback_ = nullptr;
776 // LowEnergyClientFactory implementation
777 // ========================================================
779 LowEnergyClientFactory::LowEnergyClientFactory(Adapter& adapter)
780 : adapter_(adapter) {
781 hal::BluetoothGattInterface::Get()->AddClientObserver(this);
784 LowEnergyClientFactory::~LowEnergyClientFactory() {
785 hal::BluetoothGattInterface::Get()->RemoveClientObserver(this);
788 bool LowEnergyClientFactory::RegisterInstance(
790 const RegisterCallback& callback) {
791 VLOG(1) << __func__ << " - UUID: " << uuid.ToString();
792 lock_guard<mutex> lock(pending_calls_lock_);
794 if (pending_calls_.find(uuid) != pending_calls_.end()) {
795 LOG(ERROR) << "Low-Energy client with given UUID already registered - "
796 << "UUID: " << uuid.ToString();
800 const btgatt_client_interface_t* hal_iface =
801 hal::BluetoothGattInterface::Get()->GetClientHALInterface();
802 bt_uuid_t app_uuid = uuid.GetBlueDroid();
804 if (hal_iface->register_client(&app_uuid) != BT_STATUS_SUCCESS)
807 pending_calls_[uuid] = callback;
812 void LowEnergyClientFactory::RegisterClientCallback(
813 hal::BluetoothGattInterface* gatt_iface,
814 int status, int client_id,
815 const bt_uuid_t& app_uuid) {
818 VLOG(1) << __func__ << " - UUID: " << uuid.ToString();
819 lock_guard<mutex> lock(pending_calls_lock_);
821 auto iter = pending_calls_.find(uuid);
822 if (iter == pending_calls_.end()) {
823 VLOG(1) << "Ignoring callback for unknown app_id: " << uuid.ToString();
827 // No need to construct a client if the call wasn't successful.
828 std::unique_ptr<LowEnergyClient> client;
829 BLEStatus result = BLE_STATUS_FAILURE;
830 if (status == BT_STATUS_SUCCESS) {
831 client.reset(new LowEnergyClient(adapter_, uuid, client_id));
833 gatt_iface->AddClientObserver(client.get());
835 result = BLE_STATUS_SUCCESS;
838 // Notify the result via the result callback.
839 iter->second(result, uuid, std::move(client));
841 pending_calls_.erase(iter);
844 } // namespace bluetooth