blob: 7bc5f259c676914a9d15872b89df4ada0374fb66 [file] [log] [blame]
Jakub Pawlowski67d5a252016-07-13 11:55:16 -07001//
Jakub Pawlowski5b790fe2017-09-18 09:00:20 -07002// Copyright 2015 Google, Inc.
Jakub Pawlowski67d5a252016-07-13 11:55:16 -07003//
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_advertiser.h"
18
Jakub Pawlowski67d5a252016-07-13 11:55:16 -070019#include "service/adapter.h"
Jakub Pawlowski67d5a252016-07-13 11:55:16 -070020#include "service/logging_helpers.h"
21#include "stack/include/bt_types.h"
22#include "stack/include/hcidefs.h"
23
Jakub Pawlowski67f5f372018-07-23 10:00:25 -070024#include <base/bind.h>
25#include <base/bind_helpers.h>
26#include <base/callback.h>
27#include <base/logging.h>
28
Jakub Pawlowski67d5a252016-07-13 11:55:16 -070029using std::lock_guard;
30using std::mutex;
31
32namespace bluetooth {
33
34namespace {
35
36BLEStatus GetBLEStatus(int status) {
Myles Watson911d1ae2016-11-28 16:44:40 -080037 if (status == BT_STATUS_FAIL) return BLE_STATUS_FAILURE;
Jakub Pawlowski67d5a252016-07-13 11:55:16 -070038
39 return static_cast<BLEStatus>(status);
40}
41
Jakub Pawlowski67d5a252016-07-13 11:55:16 -070042// The Bluetooth Core Specification defines time interval (e.g. Page Scan
43// Interval, Advertising Interval, etc) units as 0.625 milliseconds (or 1
44// Baseband slot). The HAL advertising functions expect the interval in this
45// unit. This function maps an AdvertiseSettings::Mode value to the
46// corresponding time unit.
47int GetAdvertisingIntervalUnit(AdvertiseSettings::Mode mode) {
48 int ms;
49
50 switch (mode) {
Myles Watson911d1ae2016-11-28 16:44:40 -080051 case AdvertiseSettings::MODE_BALANCED:
52 ms = kAdvertisingIntervalMediumMs;
53 break;
54 case AdvertiseSettings::MODE_LOW_LATENCY:
55 ms = kAdvertisingIntervalLowMs;
56 break;
57 case AdvertiseSettings::MODE_LOW_POWER:
Chih-Hung Hsiehd4646582018-09-12 15:20:43 -070058 FALLTHROUGH_INTENDED; /* FALLTHROUGH */
Myles Watson911d1ae2016-11-28 16:44:40 -080059 default:
60 ms = kAdvertisingIntervalHighMs;
61 break;
Jakub Pawlowski67d5a252016-07-13 11:55:16 -070062 }
63
64 // Convert milliseconds to Bluetooth units.
65 return (ms * 1000) / 625;
66}
67
Jakub Pawlowskie0750ca2016-12-05 11:46:23 -080068int8_t GetAdvertisingTxPower(AdvertiseSettings::TxPowerLevel tx_power) {
69 int8_t power;
70
71 switch (tx_power) {
72 case AdvertiseSettings::TX_POWER_LEVEL_ULTRA_LOW:
73 power = -21;
74 break;
75 case AdvertiseSettings::TX_POWER_LEVEL_LOW:
76 power = -15;
77 break;
78 case AdvertiseSettings::TX_POWER_LEVEL_MEDIUM:
79 power = -7;
80 break;
81 case AdvertiseSettings::TX_POWER_LEVEL_HIGH:
Chih-Hung Hsiehd4646582018-09-12 15:20:43 -070082 FALLTHROUGH_INTENDED; /* FALLTHROUGH */
Jakub Pawlowskie0750ca2016-12-05 11:46:23 -080083 default:
84 power = 1;
85 break;
86 }
87
88 return power;
89}
90
Jakub Pawlowski67d5a252016-07-13 11:55:16 -070091void GetAdvertiseParams(const AdvertiseSettings& settings, bool has_scan_rsp,
Jakub Pawlowskia75087e2016-11-17 13:27:28 -080092 AdvertiseParameters* out_params) {
Jakub Pawlowski67d5a252016-07-13 11:55:16 -070093 CHECK(out_params);
94
95 out_params->min_interval = GetAdvertisingIntervalUnit(settings.mode());
96 out_params->max_interval =
97 out_params->min_interval + kAdvertisingIntervalDeltaUnit;
98
99 if (settings.connectable())
Jakub Pawlowskie0750ca2016-12-05 11:46:23 -0800100 out_params->advertising_event_properties =
101 kAdvertisingEventTypeLegacyConnectable;
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700102 else if (has_scan_rsp)
Jakub Pawlowskie0750ca2016-12-05 11:46:23 -0800103 out_params->advertising_event_properties =
104 kAdvertisingEventTypeLegacyScannable;
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700105 else
Jakub Pawlowskie0750ca2016-12-05 11:46:23 -0800106 out_params->advertising_event_properties =
107 kAdvertisingEventTypeLegacyNonConnectable;
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700108
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800109 out_params->channel_map = kAdvertisingChannelAll;
Jakub Pawlowskie0750ca2016-12-05 11:46:23 -0800110 out_params->tx_power = GetAdvertisingTxPower(settings.tx_power_level());
111
112 // TODO: expose those new setting through AdvertiseSettings
113 out_params->primary_advertising_phy = 0x01;
114 out_params->secondary_advertising_phy = 0x01;
115 out_params->scan_request_notification_enable = 0;
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700116}
117
118} // namespace
119
120// LowEnergyAdvertiser implementation
121// ========================================================
122
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700123LowEnergyAdvertiser::LowEnergyAdvertiser(const Uuid& uuid, int advertiser_id)
Myles Watson911d1ae2016-11-28 16:44:40 -0800124 : app_identifier_(uuid),
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700125 advertiser_id_(advertiser_id),
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700126 adv_started_(false),
127 adv_start_callback_(nullptr),
Myles Watson911d1ae2016-11-28 16:44:40 -0800128 adv_stop_callback_(nullptr) {}
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700129
130LowEnergyAdvertiser::~LowEnergyAdvertiser() {
131 // Automatically unregister the advertiser.
132 VLOG(1) << "LowEnergyAdvertiser unregistering advertiser: " << advertiser_id_;
133
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700134 // Stop advertising and ignore the result.
Myles Watson911d1ae2016-11-28 16:44:40 -0800135 hal::BluetoothGattInterface::Get()->GetAdvertiserHALInterface()->Enable(
Jakub Pawlowski67f5f372018-07-23 10:00:25 -0700136 advertiser_id_, false, base::DoNothing(), 0, 0, base::DoNothing());
Myles Watson911d1ae2016-11-28 16:44:40 -0800137 hal::BluetoothGattInterface::Get()->GetAdvertiserHALInterface()->Unregister(
138 advertiser_id_);
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700139}
140
141bool LowEnergyAdvertiser::StartAdvertising(const AdvertiseSettings& settings,
Myles Watson911d1ae2016-11-28 16:44:40 -0800142 const AdvertiseData& advertise_data,
143 const AdvertiseData& scan_response,
144 const StatusCallback& callback) {
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700145 VLOG(2) << __func__;
146 lock_guard<mutex> lock(adv_fields_lock_);
147
148 if (IsAdvertisingStarted()) {
149 LOG(WARNING) << "Already advertising";
150 return false;
151 }
152
153 if (IsStartingAdvertising()) {
154 LOG(WARNING) << "StartAdvertising already pending";
155 return false;
156 }
157
158 if (!advertise_data.IsValid()) {
159 LOG(ERROR) << "Invalid advertising data";
160 return false;
161 }
162
163 if (!scan_response.IsValid()) {
164 LOG(ERROR) << "Invalid scan response data";
165 return false;
166 }
167
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700168 advertise_settings_ = settings;
169
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800170 AdvertiseParameters params;
171 GetAdvertiseParams(settings, !scan_response.data().empty(), &params);
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700172
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800173 hal::BluetoothGattInterface::Get()
174 ->GetAdvertiserHALInterface()
175 ->StartAdvertising(
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700176 advertiser_id_,
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800177 base::Bind(&LowEnergyAdvertiser::EnableCallback,
178 base::Unretained(this), true, advertiser_id_),
179 params, advertise_data.data(), scan_response.data(),
180 settings.timeout().InSeconds(),
181 base::Bind(&LowEnergyAdvertiser::EnableCallback,
182 base::Unretained(this), false, advertiser_id_));
183 ;
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700184
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700185 adv_start_callback_.reset(new StatusCallback(callback));
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700186 return true;
187}
188
189bool LowEnergyAdvertiser::StopAdvertising(const StatusCallback& callback) {
190 VLOG(2) << __func__;
191 lock_guard<mutex> lock(adv_fields_lock_);
192
193 if (!IsAdvertisingStarted()) {
194 LOG(ERROR) << "Not advertising";
195 return false;
196 }
197
198 if (IsStoppingAdvertising()) {
199 LOG(ERROR) << "StopAdvertising already pending";
200 return false;
201 }
202
Myles Watson911d1ae2016-11-28 16:44:40 -0800203 hal::BluetoothGattInterface::Get()->GetAdvertiserHALInterface()->Enable(
204 advertiser_id_, false,
205 base::Bind(&LowEnergyAdvertiser::EnableCallback, base::Unretained(this),
206 false, advertiser_id_),
Jakub Pawlowski5204c622017-03-30 20:41:02 -0700207 0, 0, base::Bind(&LowEnergyAdvertiser::EnableCallback,
208 base::Unretained(this), false, advertiser_id_));
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700209
210 // OK to set this at the end since we're still holding |adv_fields_lock_|.
211 adv_stop_callback_.reset(new StatusCallback(callback));
212
213 return true;
214}
215
216bool LowEnergyAdvertiser::IsAdvertisingStarted() const {
217 return adv_started_.load();
218}
219
220bool LowEnergyAdvertiser::IsStartingAdvertising() const {
221 return !IsAdvertisingStarted() && adv_start_callback_;
222}
223
224bool LowEnergyAdvertiser::IsStoppingAdvertising() const {
225 return IsAdvertisingStarted() && adv_stop_callback_;
226}
227
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700228const Uuid& LowEnergyAdvertiser::GetAppIdentifier() const {
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700229 return app_identifier_;
230}
231
Myles Watson911d1ae2016-11-28 16:44:40 -0800232int LowEnergyAdvertiser::GetInstanceId() const { return advertiser_id_; }
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700233
Myles Watson911d1ae2016-11-28 16:44:40 -0800234void LowEnergyAdvertiser::EnableCallback(bool enable, uint8_t advertiser_id,
235 uint8_t status) {
236 if (advertiser_id != advertiser_id_) return;
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700237
238 lock_guard<mutex> lock(adv_fields_lock_);
239
Jakub Pawlowski73679d02016-08-18 14:42:54 -0700240 VLOG(1) << __func__ << "advertiser_id: " << advertiser_id
Myles Watson911d1ae2016-11-28 16:44:40 -0800241 << " status: " << status << " enable: " << enable;
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700242
Jakub Pawlowski73679d02016-08-18 14:42:54 -0700243 if (enable) {
244 CHECK(adv_start_callback_);
245 CHECK(!adv_stop_callback_);
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700246
Jakub Pawlowski73679d02016-08-18 14:42:54 -0700247 // Terminate operation in case of error.
248 if (status != BT_STATUS_SUCCESS) {
249 LOG(ERROR) << "Failed to enable multi-advertising";
250 InvokeAndClearStartCallback(GetBLEStatus(status));
251 return;
252 }
253
254 // All pending tasks are complete. Report success.
255 adv_started_ = true;
256 InvokeAndClearStartCallback(BLE_STATUS_SUCCESS);
257
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700258 } else {
Jakub Pawlowski73679d02016-08-18 14:42:54 -0700259 CHECK(!adv_start_callback_);
260 CHECK(adv_stop_callback_);
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700261
Jakub Pawlowski73679d02016-08-18 14:42:54 -0700262 if (status == BT_STATUS_SUCCESS) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800263 VLOG(1) << "Multi-advertising stopped for advertiser_id: "
264 << advertiser_id;
Jakub Pawlowski73679d02016-08-18 14:42:54 -0700265 adv_started_ = false;
266 } else {
267 LOG(ERROR) << "Failed to stop multi-advertising";
268 }
269
270 InvokeAndClearStopCallback(GetBLEStatus(status));
271 }
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700272}
273
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700274void LowEnergyAdvertiser::InvokeAndClearStartCallback(BLEStatus status) {
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700275 // We allow NULL callbacks.
Myles Watson911d1ae2016-11-28 16:44:40 -0800276 if (*adv_start_callback_) (*adv_start_callback_)(status);
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700277
278 adv_start_callback_ = nullptr;
279}
280
281void LowEnergyAdvertiser::InvokeAndClearStopCallback(BLEStatus status) {
282 // We allow NULL callbacks.
Myles Watson911d1ae2016-11-28 16:44:40 -0800283 if (*adv_stop_callback_) (*adv_stop_callback_)(status);
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700284
285 adv_stop_callback_ = nullptr;
286}
287
288// LowEnergyAdvertiserFactory implementation
289// ========================================================
290
Myles Watson911d1ae2016-11-28 16:44:40 -0800291LowEnergyAdvertiserFactory::LowEnergyAdvertiserFactory() {}
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700292
Myles Watson911d1ae2016-11-28 16:44:40 -0800293LowEnergyAdvertiserFactory::~LowEnergyAdvertiserFactory() {}
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700294
295bool LowEnergyAdvertiserFactory::RegisterInstance(
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700296 const Uuid& app_uuid, const RegisterCallback& callback) {
Jakub Pawlowski74ef54b2016-10-06 16:52:30 -0700297 VLOG(1) << __func__;
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700298 lock_guard<mutex> lock(pending_calls_lock_);
299
Jakub Pawlowski74ef54b2016-10-06 16:52:30 -0700300 if (pending_calls_.find(app_uuid) != pending_calls_.end()) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700301 LOG(ERROR) << "Low-Energy advertiser with given Uuid already registered - "
302 << "Uuid: " << app_uuid.ToString();
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700303 return false;
304 }
305
Jakub Pawlowski74ef54b2016-10-06 16:52:30 -0700306 BleAdvertiserInterface* hal_iface =
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700307 hal::BluetoothGattInterface::Get()->GetAdvertiserHALInterface();
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700308
Jakub Pawlowski74ef54b2016-10-06 16:52:30 -0700309 VLOG(1) << __func__ << " calling register!";
310 hal_iface->RegisterAdvertiser(
311 base::Bind(&LowEnergyAdvertiserFactory::RegisterAdvertiserCallback,
312 base::Unretained(this), callback, app_uuid));
313 VLOG(1) << __func__ << " call finished!";
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700314
Jakub Pawlowski74ef54b2016-10-06 16:52:30 -0700315 pending_calls_.insert(app_uuid);
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700316
317 return true;
318}
319
320void LowEnergyAdvertiserFactory::RegisterAdvertiserCallback(
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700321 const RegisterCallback& callback, const Uuid& app_uuid,
Jakub Pawlowski74ef54b2016-10-06 16:52:30 -0700322 uint8_t advertiser_id, uint8_t status) {
323 VLOG(1) << __func__;
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700324 lock_guard<mutex> lock(pending_calls_lock_);
325
Jakub Pawlowski74ef54b2016-10-06 16:52:30 -0700326 auto iter = pending_calls_.find(app_uuid);
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700327 if (iter == pending_calls_.end()) {
Jakub Pawlowski74ef54b2016-10-06 16:52:30 -0700328 VLOG(1) << "Ignoring callback for unknown app_id: " << app_uuid.ToString();
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700329 return;
330 }
331
332 // No need to construct a advertiser if the call wasn't successful.
333 std::unique_ptr<LowEnergyAdvertiser> advertiser;
334 BLEStatus result = BLE_STATUS_FAILURE;
335 if (status == BT_STATUS_SUCCESS) {
Jakub Pawlowski74ef54b2016-10-06 16:52:30 -0700336 advertiser.reset(new LowEnergyAdvertiser(app_uuid, advertiser_id));
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700337
338 result = BLE_STATUS_SUCCESS;
339 }
340
341 // Notify the result via the result callback.
Jakub Pawlowski74ef54b2016-10-06 16:52:30 -0700342 callback(result, app_uuid, std::move(advertiser));
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700343
344 pending_calls_.erase(iter);
345}
346
347} // namespace bluetooth