• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright 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/functional/bind.h>
25 #include <base/logging.h>
26 #include <vector>
27 
28 #include "ble_advertiser.h"
29 #include "btif_common.h"
30 #include "main/shim/le_advertising_manager.h"
31 #include "main/shim/shim.h"
32 #include "osi/include/properties.h"
33 #include "stack/include/btu.h"
34 
35 using base::Bind;
36 using base::Owned;
37 using std::vector;
38 
39 namespace {
40 
41 template <typename T>
42 class OwnedArrayWrapper {
43  public:
OwnedArrayWrapper(T * o)44   explicit OwnedArrayWrapper(T* o) : ptr_(o) {}
~OwnedArrayWrapper()45   ~OwnedArrayWrapper() { delete[] ptr_; }
get() const46   T* get() const { return ptr_; }
OwnedArrayWrapper(OwnedArrayWrapper && other)47   OwnedArrayWrapper(OwnedArrayWrapper&& other) noexcept {
48     ptr_ = other.ptr_;
49     other.ptr_ = NULL;
50   }
51 
52  private:
53   mutable T* ptr_;
54 };
55 
56 template <typename T>
Unwrap(const OwnedArrayWrapper<T> & o)57 T* Unwrap(const OwnedArrayWrapper<T>& o) {
58   return o.get();
59 }
60 
61 template <typename T>
OwnedArray(T * o)62 static inline OwnedArrayWrapper<T> OwnedArray(T* o) {
63   return OwnedArrayWrapper<T>(o);
64 }
65 
parseParams(tBTM_BLE_ADV_PARAMS * p_params,const AdvertiseParameters & params)66 void parseParams(tBTM_BLE_ADV_PARAMS* p_params,
67                  const AdvertiseParameters& params) {
68   // By default use all channels, but have property for tweaking.
69   int channel_map = osi_property_get_int32(
70       "persist.bluetooth.advertising_channel_map", params.channel_map);
71 
72   p_params->advertising_event_properties = params.advertising_event_properties;
73   p_params->adv_int_min = params.min_interval;
74   p_params->adv_int_max = params.max_interval;
75   p_params->channel_map = channel_map;
76   p_params->adv_filter_policy = 0;
77   p_params->tx_power = params.tx_power;
78   p_params->primary_advertising_phy = params.primary_advertising_phy;
79   p_params->secondary_advertising_phy = params.secondary_advertising_phy;
80   p_params->scan_request_notification_enable =
81       params.scan_request_notification_enable;
82   p_params->own_address_type = params.own_address_type;
83 }
84 
parsePeriodicParams(tBLE_PERIODIC_ADV_PARAMS * p_periodic_params,PeriodicAdvertisingParameters periodic_params)85 void parsePeriodicParams(tBLE_PERIODIC_ADV_PARAMS* p_periodic_params,
86                          PeriodicAdvertisingParameters periodic_params) {
87   p_periodic_params->enable = periodic_params.enable;
88   p_periodic_params->include_adi = periodic_params.include_adi;
89   p_periodic_params->min_interval = periodic_params.min_interval;
90   p_periodic_params->max_interval = periodic_params.max_interval;
91   p_periodic_params->periodic_advertising_properties =
92       periodic_params.periodic_advertising_properties;
93 }
94 
95 class BleAdvertiserInterfaceImpl : public BleAdvertiserInterface {
~BleAdvertiserInterfaceImpl()96   ~BleAdvertiserInterfaceImpl() override{};
97 
RegisterAdvertiserCb(IdStatusCallback cb,uint8_t advertiser_id,uint8_t status)98   void RegisterAdvertiserCb(IdStatusCallback cb, uint8_t advertiser_id,
99                             uint8_t status) {
100     LOG(INFO) << __func__ << " status: " << +status
101               << " , adveriser_id: " << +advertiser_id;
102     do_in_jni_thread(Bind(cb, advertiser_id, status));
103   }
104 
RegisterAdvertiser(IdStatusCallback cb)105   void RegisterAdvertiser(IdStatusCallback cb) override {
106     do_in_main_thread(
107         FROM_HERE, Bind(&BleAdvertisingManager::RegisterAdvertiser,
108                         BleAdvertisingManager::Get(),
109                         Bind(&BleAdvertiserInterfaceImpl::RegisterAdvertiserCb,
110                              base::Unretained(this), cb)));
111   }
112 
Unregister(uint8_t advertiser_id)113   void Unregister(uint8_t advertiser_id) override {
114     do_in_main_thread(
115         FROM_HERE,
116         Bind(
117             [](uint8_t advertiser_id) {
118               if (!BleAdvertisingManager::IsInitialized()) {
119                 LOG(WARNING) << "Stack already shutdown";
120                 return;
121               }
122               BleAdvertisingManager::Get()->Unregister(advertiser_id);
123             },
124             advertiser_id));
125   }
126 
GetOwnAddress(uint8_t advertiser_id,GetAddressCallback cb)127   void GetOwnAddress(uint8_t advertiser_id, GetAddressCallback cb) override {
128     if (!BleAdvertisingManager::IsInitialized()) return;
129     do_in_main_thread(FROM_HERE,
130                       Bind(&BleAdvertisingManager::GetOwnAddress,
131                            BleAdvertisingManager::Get(), advertiser_id,
132                            jni_thread_wrapper(FROM_HERE, cb)));
133   }
134 
SetParameters(uint8_t advertiser_id,AdvertiseParameters params,ParametersCallback cb)135   void SetParameters(uint8_t advertiser_id, AdvertiseParameters params,
136                      ParametersCallback cb) override {
137     VLOG(1) << __func__;
138 
139     if (!BleAdvertisingManager::IsInitialized()) return;
140     tBTM_BLE_ADV_PARAMS* p_params = new tBTM_BLE_ADV_PARAMS;
141     parseParams(p_params, params);
142 
143     do_in_main_thread(FROM_HERE, Bind(&BleAdvertisingManager::SetParameters,
144                                       BleAdvertisingManager::Get(),
145                                       advertiser_id, base::Owned(p_params),
146                                       jni_thread_wrapper(FROM_HERE, cb)));
147   }
148 
SetData(int advertiser_id,bool set_scan_rsp,vector<uint8_t> data,StatusCallback cb)149   void SetData(int advertiser_id, bool set_scan_rsp, vector<uint8_t> data,
150                StatusCallback cb) override {
151     if (!BleAdvertisingManager::IsInitialized()) return;
152     do_in_main_thread(
153         FROM_HERE,
154         Bind(&BleAdvertisingManager::SetData, BleAdvertisingManager::Get(),
155              advertiser_id, set_scan_rsp, std::move(data),
156              jni_thread_wrapper(FROM_HERE, cb)));
157   }
158 
Enable(uint8_t advertiser_id,bool enable,StatusCallback cb,uint16_t duration,uint8_t maxExtAdvEvents,StatusCallback timeout_cb)159   void Enable(uint8_t advertiser_id, bool enable, StatusCallback cb,
160               uint16_t duration, uint8_t maxExtAdvEvents,
161               StatusCallback timeout_cb) override {
162     VLOG(1) << __func__ << " advertiser_id: " << +advertiser_id
163             << " ,enable: " << enable;
164 
165     if (!BleAdvertisingManager::IsInitialized()) return;
166     do_in_main_thread(
167         FROM_HERE,
168         Bind(&BleAdvertisingManager::Enable, BleAdvertisingManager::Get(),
169              advertiser_id, enable, jni_thread_wrapper(FROM_HERE, cb), duration,
170              maxExtAdvEvents, jni_thread_wrapper(FROM_HERE, timeout_cb)));
171   }
172 
StartAdvertising(uint8_t advertiser_id,StatusCallback cb,AdvertiseParameters params,std::vector<uint8_t> advertise_data,std::vector<uint8_t> scan_response_data,int timeout_s,MultiAdvCb timeout_cb)173   void StartAdvertising(uint8_t advertiser_id, StatusCallback cb,
174                         AdvertiseParameters params,
175                         std::vector<uint8_t> advertise_data,
176                         std::vector<uint8_t> scan_response_data, int timeout_s,
177                         MultiAdvCb timeout_cb) override {
178     VLOG(1) << __func__;
179 
180     if (!BleAdvertisingManager::IsInitialized()) return;
181     tBTM_BLE_ADV_PARAMS* p_params = new tBTM_BLE_ADV_PARAMS;
182     parseParams(p_params, params);
183 
184     do_in_main_thread(
185         FROM_HERE,
186         Bind(&BleAdvertisingManager::StartAdvertising,
187              BleAdvertisingManager::Get(), advertiser_id,
188              jni_thread_wrapper(FROM_HERE, cb), base::Owned(p_params),
189              std::move(advertise_data), std::move(scan_response_data),
190              timeout_s * 100, jni_thread_wrapper(FROM_HERE, timeout_cb)));
191   }
192 
StartAdvertisingSet(int reg_id,IdTxPowerStatusCallback cb,AdvertiseParameters params,std::vector<uint8_t> advertise_data,std::vector<uint8_t> scan_response_data,PeriodicAdvertisingParameters periodic_params,std::vector<uint8_t> periodic_data,uint16_t duration,uint8_t maxExtAdvEvents,IdStatusCallback timeout_cb)193   void StartAdvertisingSet(int reg_id, IdTxPowerStatusCallback cb,
194                            AdvertiseParameters params,
195                            std::vector<uint8_t> advertise_data,
196                            std::vector<uint8_t> scan_response_data,
197                            PeriodicAdvertisingParameters periodic_params,
198                            std::vector<uint8_t> periodic_data,
199                            uint16_t duration, uint8_t maxExtAdvEvents,
200                            IdStatusCallback timeout_cb) override {
201     VLOG(1) << __func__;
202 
203     if (!BleAdvertisingManager::IsInitialized()) return;
204     tBTM_BLE_ADV_PARAMS* p_params = new tBTM_BLE_ADV_PARAMS;
205     parseParams(p_params, params);
206 
207     tBLE_PERIODIC_ADV_PARAMS* p_periodic_params = new tBLE_PERIODIC_ADV_PARAMS;
208     parsePeriodicParams(p_periodic_params, periodic_params);
209 
210     do_in_main_thread(
211         FROM_HERE,
212         Bind(&BleAdvertisingManager::StartAdvertisingSet,
213              BleAdvertisingManager::Get(), jni_thread_wrapper(FROM_HERE, cb),
214              base::Owned(p_params), std::move(advertise_data),
215              std::move(scan_response_data), base::Owned(p_periodic_params),
216              std::move(periodic_data), duration, maxExtAdvEvents,
217              jni_thread_wrapper(FROM_HERE, timeout_cb)));
218 
219     return;
220   }
221 
SetPeriodicAdvertisingParameters(int advertiser_id,PeriodicAdvertisingParameters periodic_params,StatusCallback cb)222   void SetPeriodicAdvertisingParameters(
223       int advertiser_id, PeriodicAdvertisingParameters periodic_params,
224       StatusCallback cb) override {
225     VLOG(1) << __func__ << " advertiser_id: " << +advertiser_id;
226 
227     if (!BleAdvertisingManager::IsInitialized()) return;
228     tBLE_PERIODIC_ADV_PARAMS* p_periodic_params = new tBLE_PERIODIC_ADV_PARAMS;
229     parsePeriodicParams(p_periodic_params, periodic_params);
230 
231     do_in_main_thread(
232         FROM_HERE,
233         Bind(&BleAdvertisingManager::SetPeriodicAdvertisingParameters,
234              BleAdvertisingManager::Get(), advertiser_id,
235              base::Owned(p_periodic_params),
236              jni_thread_wrapper(FROM_HERE, cb)));
237   }
238 
SetPeriodicAdvertisingData(int advertiser_id,std::vector<uint8_t> data,StatusCallback cb)239   void SetPeriodicAdvertisingData(int advertiser_id, std::vector<uint8_t> data,
240                                   StatusCallback cb) override {
241     VLOG(1) << __func__ << " advertiser_id: " << +advertiser_id;
242 
243     if (!BleAdvertisingManager::IsInitialized()) return;
244     do_in_main_thread(FROM_HERE,
245                       Bind(&BleAdvertisingManager::SetPeriodicAdvertisingData,
246                            BleAdvertisingManager::Get(), advertiser_id,
247                            std::move(data), jni_thread_wrapper(FROM_HERE, cb)));
248   }
249 
SetPeriodicAdvertisingEnable(int advertiser_id,bool enable,bool include_adi,StatusCallback cb)250   void SetPeriodicAdvertisingEnable(int advertiser_id, bool enable,
251                                     bool include_adi,
252                                     StatusCallback cb) override {
253     VLOG(1) << __func__ << " advertiser_id: " << +advertiser_id
254             << " ,enable: " << enable;
255 
256     if (!BleAdvertisingManager::IsInitialized()) return;
257     do_in_main_thread(FROM_HERE,
258                       Bind(&BleAdvertisingManager::SetPeriodicAdvertisingEnable,
259                            BleAdvertisingManager::Get(), advertiser_id, enable,
260                            include_adi, jni_thread_wrapper(FROM_HERE, cb)));
261   }
262 
RegisterCallbacks(AdvertisingCallbacks * callbacks)263   void RegisterCallbacks(AdvertisingCallbacks* callbacks) {
264     // For GD only
265   }
266 };
267 
268 }  // namespace
269 
get_ble_advertiser_instance()270 BleAdvertiserInterface* get_ble_advertiser_instance() {
271   LOG(INFO) << __func__ << " use gd le advertiser";
272   return bluetooth::shim::get_ble_advertiser_instance();
273 }
274