OSDN Git Service

keep history after reset to mnc-dr-dev(03d171170c7ad4e40454a9575cfd4919d2e1ef2f)
[android-x86/system-bt.git] / service / low_energy_client.cpp
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
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"
26
27 using std::lock_guard;
28 using std::mutex;
29
30 namespace bluetooth {
31
32 namespace {
33
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;
39
40 BLEStatus GetBLEStatus(int status) {
41   if (status == BT_STATUS_FAIL)
42     return BLE_STATUS_FAILURE;
43
44   return static_cast<BLEStatus>(status);
45 }
46
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)) {
53     field_len = bytes[i];
54
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
57     // total data length.
58     CHECK(i + field_len < kScanRecordLength);
59
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
62     // accordingly.
63     if (field_len == 0)
64       return i;
65   }
66
67   // We have reached the end.
68   return kScanRecordLength;
69 }
70
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;
78 };
79
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);
101   } else {
102     LOG(ERROR) << "Invalid UUID length";
103     return false;
104   }
105
106   return true;
107 }
108
109 bool ProcessServiceData(const uint8_t* data,
110         uint8_t uuid_len,
111         HALAdvertiseData* out_data) {
112   size_t field_len = data[0];
113
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
118     return false;
119   }
120
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";
126     return false;
127   }
128
129   const uint8_t* service_uuid = data + 2;
130   UUID uuid;
131   if (!ProcessUUID(service_uuid, uuid_len, &uuid))
132     return false;
133
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());
137
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";
145     return false;
146   }  // else do nothing as UUID is already properly assigned
147
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;
151
152
153   out_data->service_data.insert(
154       out_data->service_data.begin(),
155       srv_data, srv_data + field_len - uuid_len - 1);
156
157   return true;
158 }
159
160 bool ProcessAdvertiseData(const AdvertiseData& adv,
161                           HALAdvertiseData* out_data) {
162   CHECK(out_data);
163   CHECK(out_data->manufacturer_data.empty());
164   CHECK(out_data->service_data.empty());
165   CHECK(out_data->service_uuid.empty());
166
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.
171     field_len = data[i];
172
173     // The type byte is the next byte in the adv. "TLV" format.
174     uint8_t type = data[i + 1];
175     size_t uuid_len = 0;
176
177     switch (type) {
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";
184         return false;
185       }
186
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);
192       break;
193     }
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;
202       UUID uuid;
203       if (!ProcessUUID(uuid_data, uuid_len, &uuid))
204         return false;
205
206       UUID::UUID128Bit uuid_bytes = uuid.GetFullLittleEndian();
207
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";
216         return false;
217       }
218
219       out_data->service_uuid.assign(
220           uuid_bytes.data(), uuid_bytes.data() + UUID::kNumBytes128);
221       break;
222     }
223     case HCI_EIR_SERVICE_DATA_16BITS_UUID_TYPE: {
224       if (!ProcessServiceData(data.data() + i, 2, out_data))
225         return false;
226       break;
227     }
228     case HCI_EIR_SERVICE_DATA_32BITS_UUID_TYPE: {
229       if (!ProcessServiceData(data.data() + i, 4, out_data))
230         return false;
231       break;
232     }
233     case HCI_EIR_SERVICE_DATA_128BITS_UUID_TYPE: {
234       if (!ProcessServiceData(data.data() + i, 16, out_data))
235         return false;
236       break;
237     }
238     // TODO(armansito): Support other fields.
239     default:
240       VLOG(1) << "Unrecognized EIR field: " << type;
241       return false;
242     }
243   }
244
245   return true;
246 }
247
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) {
254   int ms;
255
256   switch (mode) {
257   case AdvertiseSettings::MODE_BALANCED:
258     ms = kAdvertisingIntervalMediumMs;
259     break;
260   case AdvertiseSettings::MODE_LOW_LATENCY:
261     ms = kAdvertisingIntervalLowMs;
262     break;
263   case AdvertiseSettings::MODE_LOW_POWER:
264     // Fall through
265   default:
266     ms = kAdvertisingIntervalHighMs;
267     break;
268   }
269
270   // Convert milliseconds Bluetooth units.
271   return (ms * 1000) / 625;
272 }
273
274 struct AdvertiseParams {
275   int min_interval;
276   int max_interval;
277   int event_type;
278   int tx_power_level;
279   int timeout_s;
280 };
281
282 void GetAdvertiseParams(const AdvertiseSettings& settings, bool has_scan_rsp,
283                         AdvertiseParams* out_params) {
284   CHECK(out_params);
285
286   out_params->min_interval = GetAdvertisingIntervalUnit(settings.mode());
287   out_params->max_interval =
288       out_params->min_interval + kAdvertisingIntervalDeltaUnit;
289
290   if (settings.connectable())
291     out_params->event_type = kAdvertisingEventTypeConnectable;
292   else if (has_scan_rsp)
293     out_params->event_type = kAdvertisingEventTypeScannable;
294   else
295     out_params->event_type = kAdvertisingEventTypeNonConnectable;
296
297   out_params->tx_power_level = settings.tx_power_level();
298   out_params->timeout_s = settings.timeout().InSeconds();
299 }
300
301 }  // namespace
302
303 // LowEnergyClient implementation
304 // ========================================================
305
306 LowEnergyClient::LowEnergyClient(
307     Adapter& adapter, const UUID& uuid, int client_id)
308     : adapter_(adapter),
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),
314       adv_started_(false),
315       adv_start_callback_(nullptr),
316       adv_stop_callback_(nullptr),
317       scan_started_(false) {
318 }
319
320 LowEnergyClient::~LowEnergyClient() {
321   // Automatically unregister the client.
322   VLOG(1) << "LowEnergyClient unregistering client: " << client_id_;
323
324   // Unregister as observer so we no longer receive any callbacks.
325   hal::BluetoothGattInterface::Get()->RemoveClientObserver(this);
326
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_);
332
333   // Stop any scans started by this client.
334   if (scan_started_.load())
335     StopScan();
336 }
337
338 bool LowEnergyClient::Connect(std::string address, bool is_direct) {
339   VLOG(2) << __func__ << "Address: " << address << " is_direct: " << is_direct;
340
341   bt_bdaddr_t bda;
342   util::BdAddrFromString(address, &bda);
343
344   bt_status_t status = hal::BluetoothGattInterface::Get()->
345       GetClientHALInterface()->connect(client_id_, &bda, is_direct,
346                                        BT_TRANSPORT_LE);
347   if (status != BT_STATUS_SUCCESS) {
348     LOG(ERROR) << "HAL call to connect failed";
349     return false;
350   }
351
352   return true;
353 }
354
355 bool LowEnergyClient::Disconnect(std::string address) {
356   VLOG(2) << __func__ << "Address: " << address;
357
358   bt_bdaddr_t bda;
359   util::BdAddrFromString(address, &bda);
360
361   std::map<const bt_bdaddr_t, int>::iterator conn_id;
362   {
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;
367       return false;
368     }
369   }
370
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";
375     return false;
376   }
377
378   return true;
379 }
380
381 bool LowEnergyClient::SetMtu(std::string address, int mtu) {
382   VLOG(2) << __func__ << "Address: " << address
383           << " MTU: " << mtu;
384
385   bt_bdaddr_t bda;
386   util::BdAddrFromString(address, &bda);
387
388   std::map<const bt_bdaddr_t, int>::iterator conn_id;
389   {
390     lock_guard<mutex> lock(connection_fields_lock_);
391     conn_id = connection_ids_.find(bda);
392     if (conn_id == connection_ids_.end()) {
393       LOG(WARNING) << "Can't set MTU, no existing connection to " << address;
394       return false;
395     }
396   }
397
398   bt_status_t status = hal::BluetoothGattInterface::Get()->
399       GetClientHALInterface()->configure_mtu(conn_id->second, mtu);
400   if (status != BT_STATUS_SUCCESS) {
401     LOG(ERROR) << "HAL call to set MTU failed";
402     return false;
403   }
404
405   return true;
406 }
407
408 void LowEnergyClient::SetDelegate(Delegate* delegate) {
409   lock_guard<mutex> lock(delegate_mutex_);
410   delegate_ = delegate;
411 }
412
413 bool LowEnergyClient::StartScan(const ScanSettings& settings,
414                                 const std::vector<ScanFilter>& filters) {
415   VLOG(2) << __func__;
416
417   // Cannot start a scan if the adapter is not enabled.
418   if (!adapter_.IsEnabled()) {
419     LOG(ERROR) << "Cannot scan while Bluetooth is disabled";
420     return false;
421   }
422
423   // TODO(jpawlowski): Push settings and filtering logic below the HAL.
424   bt_status_t status = hal::BluetoothGattInterface::Get()->
425       StartScan(client_id_);
426   if (status != BT_STATUS_SUCCESS) {
427     LOG(ERROR) << "Failed to initiate scanning for client: " << client_id_;
428     return false;
429   }
430
431   scan_started_ = true;
432   return true;
433 }
434
435 bool LowEnergyClient::StopScan() {
436   VLOG(2) << __func__;
437
438   // TODO(armansito): We don't support batch scanning yet so call
439   // StopRegularScanForClient directly. In the future we will need to
440   // conditionally call a batch scan API here.
441   bt_status_t status = hal::BluetoothGattInterface::Get()->
442       StopScan(client_id_);
443   if (status != BT_STATUS_SUCCESS) {
444     LOG(ERROR) << "Failed to stop scan for client: " << client_id_;
445     return false;
446   }
447
448   scan_started_ = false;
449   return true;
450 }
451
452 bool LowEnergyClient::StartAdvertising(const AdvertiseSettings& settings,
453                                        const AdvertiseData& advertise_data,
454                                        const AdvertiseData& scan_response,
455                                        const StatusCallback& callback) {
456   VLOG(2) << __func__;
457   lock_guard<mutex> lock(adv_fields_lock_);
458
459   if (IsAdvertisingStarted()) {
460     LOG(WARNING) << "Already advertising";
461     return false;
462   }
463
464   if (IsStartingAdvertising()) {
465     LOG(WARNING) << "StartAdvertising already pending";
466     return false;
467   }
468
469   if (!advertise_data.IsValid()) {
470     LOG(ERROR) << "Invalid advertising data";
471     return false;
472   }
473
474   if (!scan_response.IsValid()) {
475     LOG(ERROR) << "Invalid scan response data";
476     return false;
477   }
478
479   CHECK(!adv_data_needs_update_.load());
480   CHECK(!scan_rsp_needs_update_.load());
481
482   adv_data_ = advertise_data;
483   scan_response_ = scan_response;
484   advertise_settings_ = settings;
485
486   AdvertiseParams params;
487   GetAdvertiseParams(settings, !scan_response_.data().empty(), &params);
488
489   bt_status_t status = hal::BluetoothGattInterface::Get()->
490       GetClientHALInterface()->multi_adv_enable(
491           client_id_,
492           params.min_interval,
493           params.max_interval,
494           params.event_type,
495           kAdvertisingChannelAll,
496           params.tx_power_level,
497           params.timeout_s);
498   if (status != BT_STATUS_SUCCESS) {
499     LOG(ERROR) << "Failed to initiate call to enable multi-advertising";
500     return false;
501   }
502
503   // Always update advertising data.
504   adv_data_needs_update_ = true;
505
506   // Update scan response only if it has data, since otherwise we just won't
507   // send ADV_SCAN_IND.
508   if (!scan_response_.data().empty())
509     scan_rsp_needs_update_ = true;
510
511   // OK to set this at the end since we're still holding |adv_fields_lock_|.
512   adv_start_callback_.reset(new StatusCallback(callback));
513
514   return true;
515 }
516
517 bool LowEnergyClient::StopAdvertising(const StatusCallback& callback) {
518   VLOG(2) << __func__;
519   lock_guard<mutex> lock(adv_fields_lock_);
520
521   if (!IsAdvertisingStarted()) {
522     LOG(ERROR) << "Not advertising";
523     return false;
524   }
525
526   if (IsStoppingAdvertising()) {
527     LOG(ERROR) << "StopAdvertising already pending";
528     return false;
529   }
530
531   CHECK(!adv_start_callback_);
532
533   bt_status_t status = hal::BluetoothGattInterface::Get()->
534       GetClientHALInterface()->multi_adv_disable(client_id_);
535   if (status != BT_STATUS_SUCCESS) {
536     LOG(ERROR) << "Failed to initiate call to disable multi-advertising";
537     return false;
538   }
539
540   // OK to set this at the end since we're still holding |adv_fields_lock_|.
541   adv_stop_callback_.reset(new StatusCallback(callback));
542
543   return true;
544 }
545
546 bool LowEnergyClient::IsAdvertisingStarted() const {
547   return adv_started_.load();
548 }
549
550 bool LowEnergyClient::IsStartingAdvertising() const {
551   return !IsAdvertisingStarted() && adv_start_callback_;
552 }
553
554 bool LowEnergyClient::IsStoppingAdvertising() const {
555   return IsAdvertisingStarted() && adv_stop_callback_;
556 }
557
558 const UUID& LowEnergyClient::GetAppIdentifier() const {
559   return app_identifier_;
560 }
561
562 int LowEnergyClient::GetInstanceId() const {
563   return client_id_;
564 }
565
566 void LowEnergyClient::ScanResultCallback(
567     hal::BluetoothGattInterface* gatt_iface,
568     const bt_bdaddr_t& bda, int rssi, uint8_t* adv_data) {
569   // Ignore scan results if this client didn't start a scan.
570   if (!scan_started_.load())
571     return;
572
573   lock_guard<mutex> lock(delegate_mutex_);
574   if (!delegate_)
575     return;
576
577   // TODO(armansito): Apply software filters here.
578
579   size_t record_len = GetScanRecordLength(adv_data);
580   std::vector<uint8_t> scan_record(adv_data, adv_data + record_len);
581
582   ScanResult result(BtAddrString(&bda), scan_record, rssi);
583
584   delegate_->OnScanResult(this, result);
585 }
586
587 void LowEnergyClient::ConnectCallback(
588       hal::BluetoothGattInterface* gatt_iface, int conn_id, int status,
589       int client_id, const bt_bdaddr_t& bda) {
590   if (client_id != client_id_)
591     return;
592
593   VLOG(1) << __func__ << "client_id: " << client_id << " status: " << status;
594
595   {
596     lock_guard<mutex> lock(connection_fields_lock_);
597     auto success = connection_ids_.emplace(bda, conn_id);
598     if (!success.second) {
599       LOG(ERROR) << __func__ << " Insertion into connection_ids_ failed!";
600     }
601   }
602
603   if (delegate_)
604     delegate_->OnConnectionState(this, status, BtAddrString(&bda).c_str(),
605                                  true);
606 }
607
608 void LowEnergyClient::DisconnectCallback(
609       hal::BluetoothGattInterface* gatt_iface, int conn_id, int status,
610       int client_id, const bt_bdaddr_t& bda) {
611   if (client_id != client_id_)
612     return;
613
614   VLOG(1) << __func__ << " client_id: " << client_id << " status: " << status;
615   {
616     lock_guard<mutex> lock(connection_fields_lock_);
617     if (!connection_ids_.erase(bda)) {
618       LOG(ERROR) << __func__ << " Erasing from connection_ids_ failed!";
619     }
620   }
621
622   if (delegate_)
623     delegate_->OnConnectionState(this, status, BtAddrString(&bda).c_str(),
624                                  false);
625 }
626
627 void LowEnergyClient::MtuChangedCallback(
628       hal::BluetoothGattInterface* gatt_iface, int conn_id, int status,
629       int mtu) {
630   VLOG(1) << __func__ << " conn_id: " << conn_id << " status: " << status
631           << " mtu: " << mtu;
632
633   const bt_bdaddr_t *bda = nullptr;
634   {
635     lock_guard<mutex> lock(connection_fields_lock_);
636     for (auto& connection: connection_ids_) {
637       if (connection.second == conn_id) {
638         bda = &connection.first;
639         break;
640       }
641     }
642   }
643
644   if (!bda)
645     return;
646
647   const char *addr = BtAddrString(bda).c_str();
648   if (delegate_)
649     delegate_->OnMtuChanged(this, status, addr, mtu);
650 }
651
652 void LowEnergyClient::MultiAdvEnableCallback(
653     hal::BluetoothGattInterface* gatt_iface,
654     int client_id, int status) {
655   if (client_id != client_id_)
656     return;
657
658   lock_guard<mutex> lock(adv_fields_lock_);
659
660   VLOG(1) << __func__ << "client_id: " << client_id << " status: " << status;
661
662   CHECK(adv_start_callback_);
663   CHECK(!adv_stop_callback_);
664
665   // Terminate operation in case of error.
666   if (status != BT_STATUS_SUCCESS) {
667     LOG(ERROR) << "Failed to enable multi-advertising";
668     InvokeAndClearStartCallback(GetBLEStatus(status));
669     return;
670   }
671
672   // Now handle deferred tasks.
673   HandleDeferredAdvertiseData(gatt_iface);
674 }
675
676 void LowEnergyClient::MultiAdvDataCallback(
677     hal::BluetoothGattInterface* gatt_iface,
678     int client_id, int status) {
679   if (client_id != client_id_)
680     return;
681
682   lock_guard<mutex> lock(adv_fields_lock_);
683
684   VLOG(1) << __func__ << "client_id: " << client_id << " status: " << status;
685
686   is_setting_adv_data_ = false;
687
688   // Terminate operation in case of error.
689   if (status != BT_STATUS_SUCCESS) {
690     LOG(ERROR) << "Failed to set advertising data";
691     InvokeAndClearStartCallback(GetBLEStatus(status));
692     return;
693   }
694
695   // Now handle deferred tasks.
696   HandleDeferredAdvertiseData(gatt_iface);
697 }
698
699 void LowEnergyClient::MultiAdvDisableCallback(
700     hal::BluetoothGattInterface* /* gatt_iface */,
701     int client_id, int status) {
702   if (client_id != client_id_)
703     return;
704
705   lock_guard<mutex> lock(adv_fields_lock_);
706
707   VLOG(1) << __func__ << "client_id: " << client_id << " status: " << status;
708
709   CHECK(!adv_start_callback_);
710   CHECK(adv_stop_callback_);
711
712   if (status == BT_STATUS_SUCCESS) {
713     VLOG(1) << "Multi-advertising stopped for client_id: " << client_id;
714     adv_started_ = false;
715   } else {
716     LOG(ERROR) << "Failed to stop multi-advertising";
717   }
718
719   InvokeAndClearStopCallback(GetBLEStatus(status));
720 }
721
722 bt_status_t LowEnergyClient::SetAdvertiseData(
723     hal::BluetoothGattInterface* gatt_iface,
724     const AdvertiseData& data,
725     bool set_scan_rsp) {
726   VLOG(2) << __func__;
727
728   HALAdvertiseData hal_data;
729
730   // TODO(armansito): The stack should check that the length is valid when other
731   // fields inserted by the stack (e.g. flags, device name, tx-power) are taken
732   // into account. At the moment we are skipping this check; this means that if
733   // the given data is too long then the stack will truncate it.
734   if (!ProcessAdvertiseData(data, &hal_data)) {
735     LOG(ERROR) << "Malformed advertise data given";
736     return BT_STATUS_FAIL;
737   }
738
739   if (is_setting_adv_data_.load()) {
740     LOG(ERROR) << "Setting advertising data already in progress.";
741     return BT_STATUS_FAIL;
742   }
743
744   // TODO(armansito): The length fields in the BTIF function below are signed
745   // integers so a call to std::vector::size might get capped. This is very
746   // unlikely anyway but it's safer to stop using signed-integer types for
747   // length in APIs, so we should change that.
748   bt_status_t status = gatt_iface->GetClientHALInterface()->
749       multi_adv_set_inst_data(
750           client_id_,
751           set_scan_rsp,
752           data.include_device_name(),
753           data.include_tx_power_level(),
754           0,  // This is what Bluetooth.apk current hardcodes for "appearance".
755           hal_data.manufacturer_data.size(),
756           reinterpret_cast<char*>(hal_data.manufacturer_data.data()),
757           hal_data.service_data.size(),
758           reinterpret_cast<char*>(hal_data.service_data.data()),
759           hal_data.service_uuid.size(),
760           reinterpret_cast<char*>(hal_data.service_uuid.data()));
761
762   if (status != BT_STATUS_SUCCESS) {
763     LOG(ERROR) << "Failed to set instance advertising data.";
764     return status;
765   }
766
767   if (set_scan_rsp)
768     scan_rsp_needs_update_ = false;
769   else
770     adv_data_needs_update_ = false;
771
772   is_setting_adv_data_ = true;
773
774   return status;
775 }
776
777 void LowEnergyClient::HandleDeferredAdvertiseData(
778     hal::BluetoothGattInterface* gatt_iface) {
779   VLOG(2) << __func__;
780
781   CHECK(!IsAdvertisingStarted());
782   CHECK(!IsStoppingAdvertising());
783   CHECK(IsStartingAdvertising());
784   CHECK(!is_setting_adv_data_.load());
785
786   if (adv_data_needs_update_.load()) {
787     bt_status_t status = SetAdvertiseData(gatt_iface, adv_data_, false);
788     if (status != BT_STATUS_SUCCESS) {
789       LOG(ERROR) << "Failed setting advertisement data";
790       InvokeAndClearStartCallback(GetBLEStatus(status));
791     }
792     return;
793   }
794
795   if (scan_rsp_needs_update_.load()) {
796     bt_status_t status = SetAdvertiseData(gatt_iface, scan_response_, true);
797     if (status != BT_STATUS_SUCCESS) {
798       LOG(ERROR) << "Failed setting scan response data";
799       InvokeAndClearStartCallback(GetBLEStatus(status));
800     }
801     return;
802   }
803
804   // All pending tasks are complete. Report success.
805   adv_started_ = true;
806   InvokeAndClearStartCallback(BLE_STATUS_SUCCESS);
807 }
808
809 void LowEnergyClient::InvokeAndClearStartCallback(BLEStatus status) {
810   adv_data_needs_update_ = false;
811   scan_rsp_needs_update_ = false;
812
813   // We allow NULL callbacks.
814   if (*adv_start_callback_)
815     (*adv_start_callback_)(status);
816
817   adv_start_callback_ = nullptr;
818 }
819
820 void LowEnergyClient::InvokeAndClearStopCallback(BLEStatus status) {
821   // We allow NULL callbacks.
822   if (*adv_stop_callback_)
823     (*adv_stop_callback_)(status);
824
825   adv_stop_callback_ = nullptr;
826 }
827
828 // LowEnergyClientFactory implementation
829 // ========================================================
830
831 LowEnergyClientFactory::LowEnergyClientFactory(Adapter& adapter)
832     : adapter_(adapter) {
833   hal::BluetoothGattInterface::Get()->AddClientObserver(this);
834 }
835
836 LowEnergyClientFactory::~LowEnergyClientFactory() {
837   hal::BluetoothGattInterface::Get()->RemoveClientObserver(this);
838 }
839
840 bool LowEnergyClientFactory::RegisterInstance(
841     const UUID& uuid,
842     const RegisterCallback& callback) {
843   VLOG(1) << __func__ << " - UUID: " << uuid.ToString();
844   lock_guard<mutex> lock(pending_calls_lock_);
845
846   if (pending_calls_.find(uuid) != pending_calls_.end()) {
847     LOG(ERROR) << "Low-Energy client with given UUID already registered - "
848                << "UUID: " << uuid.ToString();
849     return false;
850   }
851
852   const btgatt_client_interface_t* hal_iface =
853       hal::BluetoothGattInterface::Get()->GetClientHALInterface();
854   bt_uuid_t app_uuid = uuid.GetBlueDroid();
855
856   if (hal_iface->register_client(&app_uuid) != BT_STATUS_SUCCESS)
857     return false;
858
859   pending_calls_[uuid] = callback;
860
861   return true;
862 }
863
864 void LowEnergyClientFactory::RegisterClientCallback(
865     hal::BluetoothGattInterface* gatt_iface,
866     int status, int client_id,
867     const bt_uuid_t& app_uuid) {
868   UUID uuid(app_uuid);
869
870   VLOG(1) << __func__ << " - UUID: " << uuid.ToString();
871   lock_guard<mutex> lock(pending_calls_lock_);
872
873   auto iter = pending_calls_.find(uuid);
874   if (iter == pending_calls_.end()) {
875     VLOG(1) << "Ignoring callback for unknown app_id: " << uuid.ToString();
876     return;
877   }
878
879   // No need to construct a client if the call wasn't successful.
880   std::unique_ptr<LowEnergyClient> client;
881   BLEStatus result = BLE_STATUS_FAILURE;
882   if (status == BT_STATUS_SUCCESS) {
883     client.reset(new LowEnergyClient(adapter_, uuid, client_id));
884
885     gatt_iface->AddClientObserver(client.get());
886
887     result = BLE_STATUS_SUCCESS;
888   }
889
890   // Notify the result via the result callback.
891   iter->second(result, uuid, std::move(client));
892
893   pending_calls_.erase(iter);
894 }
895
896 }  // namespace bluetooth