blob: a37b09532111c1317407b4f6b7843a50108c297c [file] [log] [blame]
Jakub Pawlowski67d5a252016-07-13 11:55:16 -07001/******************************************************************************
2 *
3 * Copyright (C) 2016 Google Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19#define LOG_TAG "bt_btif_ble_advertiser"
20
21#include <hardware/bluetooth.h>
22#include <hardware/bt_gatt.h>
23
24#include <base/bind.h>
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -070025#include <vector>
Jakub Pawlowski67d5a252016-07-13 11:55:16 -070026
Jakub Pawlowski01487262016-09-05 04:31:34 -070027#include "ble_advertiser.h"
Jakub Pawlowskie47b7692016-09-28 07:36:54 -070028#include "bta_closure_api.h"
Jakub Pawlowski67d5a252016-07-13 11:55:16 -070029#include "btif_common.h"
Jakub Pawlowski67d5a252016-07-13 11:55:16 -070030
31using base::Bind;
32using base::Owned;
33using std::vector;
34
Jakub Pawlowski67d5a252016-07-13 11:55:16 -070035namespace {
36
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -070037template <typename T>
38class OwnedArrayWrapper {
39 public:
40 explicit OwnedArrayWrapper(T* o) : ptr_(o) {}
41 ~OwnedArrayWrapper() { delete[] ptr_; }
42 T* get() const { return ptr_; }
43 OwnedArrayWrapper(OwnedArrayWrapper&& other) {
44 ptr_ = other.ptr_;
45 other.ptr_ = NULL;
46 }
47
48 private:
49 mutable T* ptr_;
50};
51
52template <typename T>
53T* Unwrap(const OwnedArrayWrapper<T>& o) {
54 return o.get();
Jakub Pawlowski67d5a252016-07-13 11:55:16 -070055}
56
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -070057template <typename T>
58static inline OwnedArrayWrapper<T> OwnedArray(T* o) {
59 return OwnedArrayWrapper<T>(o);
Jakub Pawlowski67d5a252016-07-13 11:55:16 -070060}
61
Jakub Pawlowskiff32af62017-03-17 18:05:38 -070062void parseParams(tBTM_BLE_ADV_PARAMS* p_params,
63 const AdvertiseParameters& params) {
64 p_params->advertising_event_properties = params.advertising_event_properties;
65 p_params->adv_int_min = params.min_interval;
66 p_params->adv_int_max = params.max_interval;
67 p_params->channel_map = params.channel_map;
68 p_params->adv_filter_policy = 0;
69 p_params->tx_power = params.tx_power;
70 p_params->primary_advertising_phy = params.primary_advertising_phy;
71 p_params->secondary_advertising_phy = params.secondary_advertising_phy;
72 p_params->scan_request_notification_enable =
73 params.scan_request_notification_enable;
74}
75
76void parsePeriodicParams(tBLE_PERIODIC_ADV_PARAMS* p_periodic_params,
77 PeriodicAdvertisingParameters periodic_params) {
78 p_periodic_params->enable = periodic_params.enable;
79 p_periodic_params->min_interval = periodic_params.min_interval;
80 p_periodic_params->max_interval = periodic_params.max_interval;
81 p_periodic_params->periodic_advertising_properties =
82 periodic_params.periodic_advertising_properties;
83}
84
Jakub Pawlowski74ef54b2016-10-06 16:52:30 -070085class BleAdvertiserInterfaceImpl : public BleAdvertiserInterface {
86 ~BleAdvertiserInterfaceImpl(){};
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -070087
Jakub Pawlowski16da9212017-03-11 16:46:00 -080088 void RegisterAdvertiserCb(IdStatusCallback cb, uint8_t advertiser_id,
89 uint8_t status) {
Jakub Pawlowskid964cf92016-11-03 15:41:50 -070090 LOG(INFO) << __func__ << " status: " << +status
91 << " , adveriser_id: " << +advertiser_id;
Jakub Pawlowski74ef54b2016-10-06 16:52:30 -070092 do_in_jni_thread(Bind(cb, advertiser_id, status));
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -070093 }
94
Jakub Pawlowski16da9212017-03-11 16:46:00 -080095 void RegisterAdvertiser(IdStatusCallback cb) override {
Jakub Pawlowski74ef54b2016-10-06 16:52:30 -070096 do_in_bta_thread(
97 FROM_HERE, Bind(&BleAdvertisingManager::RegisterAdvertiser,
98 base::Unretained(BleAdvertisingManager::Get()),
99 Bind(&BleAdvertiserInterfaceImpl::RegisterAdvertiserCb,
100 base::Unretained(this), cb)));
101 }
102
103 void Unregister(uint8_t advertiser_id) override {
104 do_in_bta_thread(
105 FROM_HERE,
106 Bind(&BleAdvertisingManager::Unregister,
107 base::Unretained(BleAdvertisingManager::Get()), advertiser_id));
108 }
109
Jakub Pawlowski779b4fd2017-04-19 07:05:00 -0700110 void GetOwnAddress(uint8_t advertiser_id, GetAddressCallback cb) override {
111 do_in_bta_thread(FROM_HERE,
112 Bind(&BleAdvertisingManager::GetOwnAddress,
113 base::Unretained(BleAdvertisingManager::Get()),
114 advertiser_id, jni_thread_wrapper(FROM_HERE, cb)));
115 }
116
Jakub Pawlowskiff32af62017-03-17 18:05:38 -0700117 void SetParameters(uint8_t advertiser_id, AdvertiseParameters params,
118 ParametersCallback cb) override {
119 VLOG(1) << __func__;
Jakub Pawlowskia8d73ef2016-10-31 11:15:20 -0700120
Jakub Pawlowskiff32af62017-03-17 18:05:38 -0700121 tBTM_BLE_ADV_PARAMS* p_params = new tBTM_BLE_ADV_PARAMS;
122 parseParams(p_params, params);
Jakub Pawlowskie0750ca2016-12-05 11:46:23 -0800123
Jakub Pawlowskiff32af62017-03-17 18:05:38 -0700124 do_in_bta_thread(
125 FROM_HERE,
126 Bind(&BleAdvertisingManager::SetParameters,
127 base::Unretained(BleAdvertisingManager::Get()), advertiser_id,
128 base::Owned(p_params), jni_thread_wrapper(FROM_HERE, cb)));
Jakub Pawlowski74ef54b2016-10-06 16:52:30 -0700129 }
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700130
Jakub Pawlowskid964cf92016-11-03 15:41:50 -0700131 void SetData(int advertiser_id, bool set_scan_rsp, vector<uint8_t> data,
Jakub Pawlowski16da9212017-03-11 16:46:00 -0800132 StatusCallback cb) override {
Jakub Pawlowski0482c8a2017-02-17 12:18:59 -0800133 do_in_bta_thread(
134 FROM_HERE,
135 Bind(&BleAdvertisingManager::SetData,
136 base::Unretained(BleAdvertisingManager::Get()), advertiser_id,
137 set_scan_rsp, std::move(data), jni_thread_wrapper(FROM_HERE, cb)));
Jakub Pawlowski74ef54b2016-10-06 16:52:30 -0700138 }
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700139
Jakub Pawlowski16da9212017-03-11 16:46:00 -0800140 void Enable(uint8_t advertiser_id, bool enable, StatusCallback cb,
Jakub Pawlowski5204c622017-03-30 20:41:02 -0700141 uint16_t duration, uint8_t maxExtAdvEvents,
142 StatusCallback timeout_cb) override {
Jakub Pawlowski74ef54b2016-10-06 16:52:30 -0700143 VLOG(1) << __func__ << " advertiser_id: " << +advertiser_id
144 << " ,enable: " << enable;
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700145
Jakub Pawlowski74ef54b2016-10-06 16:52:30 -0700146 do_in_bta_thread(
147 FROM_HERE,
148 Bind(&BleAdvertisingManager::Enable,
149 base::Unretained(BleAdvertisingManager::Get()), advertiser_id,
Jakub Pawlowski5204c622017-03-30 20:41:02 -0700150 enable, jni_thread_wrapper(FROM_HERE, cb), duration,
151 maxExtAdvEvents, jni_thread_wrapper(FROM_HERE, timeout_cb)));
Jakub Pawlowski74ef54b2016-10-06 16:52:30 -0700152 }
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800153
Jakub Pawlowski16da9212017-03-11 16:46:00 -0800154 void StartAdvertising(uint8_t advertiser_id, StatusCallback cb,
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800155 AdvertiseParameters params,
156 std::vector<uint8_t> advertise_data,
157 std::vector<uint8_t> scan_response_data, int timeout_s,
158 MultiAdvCb timeout_cb) override {
159 VLOG(1) << __func__;
160
161 tBTM_BLE_ADV_PARAMS* p_params = new tBTM_BLE_ADV_PARAMS;
Jakub Pawlowskiff32af62017-03-17 18:05:38 -0700162 parseParams(p_params, params);
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800163
164 do_in_bta_thread(
Jakub Pawlowski0482c8a2017-02-17 12:18:59 -0800165 FROM_HERE,
166 Bind(&BleAdvertisingManager::StartAdvertising,
167 base::Unretained(BleAdvertisingManager::Get()), advertiser_id,
168 jni_thread_wrapper(FROM_HERE, cb), base::Owned(p_params),
169 std::move(advertise_data), std::move(scan_response_data),
Jakub Pawlowski5204c622017-03-30 20:41:02 -0700170 timeout_s * 100, jni_thread_wrapper(FROM_HERE, timeout_cb)));
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800171 }
Jakub Pawlowski16da9212017-03-11 16:46:00 -0800172
Jakub Pawlowskiff32af62017-03-17 18:05:38 -0700173 void StartAdvertisingSet(IdTxPowerStatusCallback cb,
174 AdvertiseParameters params,
Jakub Pawlowski16da9212017-03-11 16:46:00 -0800175 std::vector<uint8_t> advertise_data,
176 std::vector<uint8_t> scan_response_data,
177 PeriodicAdvertisingParameters periodic_params,
Jakub Pawlowski5204c622017-03-30 20:41:02 -0700178 std::vector<uint8_t> periodic_data,
179 uint16_t duration, uint8_t maxExtAdvEvents,
Jakub Pawlowskiff32af62017-03-17 18:05:38 -0700180 IdStatusCallback timeout_cb) override {
Jakub Pawlowski16da9212017-03-11 16:46:00 -0800181 VLOG(1) << __func__;
182
183 tBTM_BLE_ADV_PARAMS* p_params = new tBTM_BLE_ADV_PARAMS;
Jakub Pawlowskiff32af62017-03-17 18:05:38 -0700184 parseParams(p_params, params);
Jakub Pawlowski16da9212017-03-11 16:46:00 -0800185
186 tBLE_PERIODIC_ADV_PARAMS* p_periodic_params = new tBLE_PERIODIC_ADV_PARAMS;
Jakub Pawlowskiff32af62017-03-17 18:05:38 -0700187 parsePeriodicParams(p_periodic_params, periodic_params);
Jakub Pawlowski16da9212017-03-11 16:46:00 -0800188
189 do_in_bta_thread(
190 FROM_HERE,
191 Bind(&BleAdvertisingManager::StartAdvertisingSet,
192 base::Unretained(BleAdvertisingManager::Get()),
193 jni_thread_wrapper(FROM_HERE, cb), base::Owned(p_params),
194 std::move(advertise_data), std::move(scan_response_data),
Jakub Pawlowski5204c622017-03-30 20:41:02 -0700195 base::Owned(p_periodic_params), std::move(periodic_data), duration,
196 maxExtAdvEvents, jni_thread_wrapper(FROM_HERE, timeout_cb)));
Jakub Pawlowskiff32af62017-03-17 18:05:38 -0700197 }
198
199 void SetPeriodicAdvertisingParameters(
200 int advertiser_id, PeriodicAdvertisingParameters periodic_params,
201 StatusCallback cb) override {
202 VLOG(1) << __func__ << " advertiser_id: " << +advertiser_id;
203
204 tBLE_PERIODIC_ADV_PARAMS* p_periodic_params = new tBLE_PERIODIC_ADV_PARAMS;
205 parsePeriodicParams(p_periodic_params, periodic_params);
206
207 do_in_bta_thread(
208 FROM_HERE,
209 Bind(&BleAdvertisingManager::SetPeriodicAdvertisingParameters,
210 base::Unretained(BleAdvertisingManager::Get()), advertiser_id,
211 base::Owned(p_periodic_params),
212 jni_thread_wrapper(FROM_HERE, cb)));
213 }
214
215 void SetPeriodicAdvertisingData(int advertiser_id, std::vector<uint8_t> data,
216 StatusCallback cb) override {
217 VLOG(1) << __func__ << " advertiser_id: " << +advertiser_id;
218
219 do_in_bta_thread(
220 FROM_HERE,
221 Bind(&BleAdvertisingManager::SetPeriodicAdvertisingData,
222 base::Unretained(BleAdvertisingManager::Get()), advertiser_id,
223 std::move(data), jni_thread_wrapper(FROM_HERE, cb)));
224 }
225
226 void SetPeriodicAdvertisingEnable(int advertiser_id, bool enable,
227 StatusCallback cb) override {
228 VLOG(1) << __func__ << " advertiser_id: " << +advertiser_id
229 << " ,enable: " << enable;
230
231 do_in_bta_thread(
232 FROM_HERE,
233 Bind(&BleAdvertisingManager::SetPeriodicAdvertisingEnable,
234 base::Unretained(BleAdvertisingManager::Get()), advertiser_id,
235 enable, jni_thread_wrapper(FROM_HERE, cb)));
236 }
Jakub Pawlowski74ef54b2016-10-06 16:52:30 -0700237};
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -0700238
Jakub Pawlowski74ef54b2016-10-06 16:52:30 -0700239BleAdvertiserInterface* btLeAdvertiserInstance = nullptr;
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700240
241} // namespace
242
Jakub Pawlowski74ef54b2016-10-06 16:52:30 -0700243BleAdvertiserInterface* get_ble_advertiser_instance() {
244 if (btLeAdvertiserInstance == nullptr)
245 btLeAdvertiserInstance = new BleAdvertiserInterfaceImpl();
246
247 return btLeAdvertiserInstance;
248}