Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 1 | /****************************************************************************** |
| 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 Pawlowski | 9eaf776 | 2016-10-04 19:30:09 -0700 | [diff] [blame] | 25 | #include <vector> |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 26 | |
Jakub Pawlowski | 0148726 | 2016-09-05 04:31:34 -0700 | [diff] [blame] | 27 | #include "ble_advertiser.h" |
Jakub Pawlowski | e47b769 | 2016-09-28 07:36:54 -0700 | [diff] [blame] | 28 | #include "bta_closure_api.h" |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 29 | #include "btif_common.h" |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 30 | |
| 31 | using base::Bind; |
| 32 | using base::Owned; |
| 33 | using std::vector; |
| 34 | |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 35 | namespace { |
| 36 | |
Jakub Pawlowski | 9eaf776 | 2016-10-04 19:30:09 -0700 | [diff] [blame] | 37 | template <typename T> |
| 38 | class 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 | |
| 52 | template <typename T> |
| 53 | T* Unwrap(const OwnedArrayWrapper<T>& o) { |
| 54 | return o.get(); |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Jakub Pawlowski | 9eaf776 | 2016-10-04 19:30:09 -0700 | [diff] [blame] | 57 | template <typename T> |
| 58 | static inline OwnedArrayWrapper<T> OwnedArray(T* o) { |
| 59 | return OwnedArrayWrapper<T>(o); |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Jakub Pawlowski | ff32af6 | 2017-03-17 18:05:38 -0700 | [diff] [blame] | 62 | void 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 | |
| 76 | void 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 Pawlowski | 74ef54b | 2016-10-06 16:52:30 -0700 | [diff] [blame] | 85 | class BleAdvertiserInterfaceImpl : public BleAdvertiserInterface { |
| 86 | ~BleAdvertiserInterfaceImpl(){}; |
Jakub Pawlowski | 9eaf776 | 2016-10-04 19:30:09 -0700 | [diff] [blame] | 87 | |
Jakub Pawlowski | 16da921 | 2017-03-11 16:46:00 -0800 | [diff] [blame] | 88 | void RegisterAdvertiserCb(IdStatusCallback cb, uint8_t advertiser_id, |
| 89 | uint8_t status) { |
Jakub Pawlowski | d964cf9 | 2016-11-03 15:41:50 -0700 | [diff] [blame] | 90 | LOG(INFO) << __func__ << " status: " << +status |
| 91 | << " , adveriser_id: " << +advertiser_id; |
Jakub Pawlowski | 74ef54b | 2016-10-06 16:52:30 -0700 | [diff] [blame] | 92 | do_in_jni_thread(Bind(cb, advertiser_id, status)); |
Jakub Pawlowski | 9eaf776 | 2016-10-04 19:30:09 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Jakub Pawlowski | 16da921 | 2017-03-11 16:46:00 -0800 | [diff] [blame] | 95 | void RegisterAdvertiser(IdStatusCallback cb) override { |
Jakub Pawlowski | 74ef54b | 2016-10-06 16:52:30 -0700 | [diff] [blame] | 96 | 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 Pawlowski | 779b4fd | 2017-04-19 07:05:00 -0700 | [diff] [blame^] | 110 | 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 Pawlowski | ff32af6 | 2017-03-17 18:05:38 -0700 | [diff] [blame] | 117 | void SetParameters(uint8_t advertiser_id, AdvertiseParameters params, |
| 118 | ParametersCallback cb) override { |
| 119 | VLOG(1) << __func__; |
Jakub Pawlowski | a8d73ef | 2016-10-31 11:15:20 -0700 | [diff] [blame] | 120 | |
Jakub Pawlowski | ff32af6 | 2017-03-17 18:05:38 -0700 | [diff] [blame] | 121 | tBTM_BLE_ADV_PARAMS* p_params = new tBTM_BLE_ADV_PARAMS; |
| 122 | parseParams(p_params, params); |
Jakub Pawlowski | e0750ca | 2016-12-05 11:46:23 -0800 | [diff] [blame] | 123 | |
Jakub Pawlowski | ff32af6 | 2017-03-17 18:05:38 -0700 | [diff] [blame] | 124 | 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 Pawlowski | 74ef54b | 2016-10-06 16:52:30 -0700 | [diff] [blame] | 129 | } |
Jakub Pawlowski | e47b769 | 2016-09-28 07:36:54 -0700 | [diff] [blame] | 130 | |
Jakub Pawlowski | d964cf9 | 2016-11-03 15:41:50 -0700 | [diff] [blame] | 131 | void SetData(int advertiser_id, bool set_scan_rsp, vector<uint8_t> data, |
Jakub Pawlowski | 16da921 | 2017-03-11 16:46:00 -0800 | [diff] [blame] | 132 | StatusCallback cb) override { |
Jakub Pawlowski | 0482c8a | 2017-02-17 12:18:59 -0800 | [diff] [blame] | 133 | 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 Pawlowski | 74ef54b | 2016-10-06 16:52:30 -0700 | [diff] [blame] | 138 | } |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 139 | |
Jakub Pawlowski | 16da921 | 2017-03-11 16:46:00 -0800 | [diff] [blame] | 140 | void Enable(uint8_t advertiser_id, bool enable, StatusCallback cb, |
Jakub Pawlowski | 5204c62 | 2017-03-30 20:41:02 -0700 | [diff] [blame] | 141 | uint16_t duration, uint8_t maxExtAdvEvents, |
| 142 | StatusCallback timeout_cb) override { |
Jakub Pawlowski | 74ef54b | 2016-10-06 16:52:30 -0700 | [diff] [blame] | 143 | VLOG(1) << __func__ << " advertiser_id: " << +advertiser_id |
| 144 | << " ,enable: " << enable; |
Jakub Pawlowski | e47b769 | 2016-09-28 07:36:54 -0700 | [diff] [blame] | 145 | |
Jakub Pawlowski | 74ef54b | 2016-10-06 16:52:30 -0700 | [diff] [blame] | 146 | do_in_bta_thread( |
| 147 | FROM_HERE, |
| 148 | Bind(&BleAdvertisingManager::Enable, |
| 149 | base::Unretained(BleAdvertisingManager::Get()), advertiser_id, |
Jakub Pawlowski | 5204c62 | 2017-03-30 20:41:02 -0700 | [diff] [blame] | 150 | enable, jni_thread_wrapper(FROM_HERE, cb), duration, |
| 151 | maxExtAdvEvents, jni_thread_wrapper(FROM_HERE, timeout_cb))); |
Jakub Pawlowski | 74ef54b | 2016-10-06 16:52:30 -0700 | [diff] [blame] | 152 | } |
Jakub Pawlowski | a75087e | 2016-11-17 13:27:28 -0800 | [diff] [blame] | 153 | |
Jakub Pawlowski | 16da921 | 2017-03-11 16:46:00 -0800 | [diff] [blame] | 154 | void StartAdvertising(uint8_t advertiser_id, StatusCallback cb, |
Jakub Pawlowski | a75087e | 2016-11-17 13:27:28 -0800 | [diff] [blame] | 155 | 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 Pawlowski | ff32af6 | 2017-03-17 18:05:38 -0700 | [diff] [blame] | 162 | parseParams(p_params, params); |
Jakub Pawlowski | a75087e | 2016-11-17 13:27:28 -0800 | [diff] [blame] | 163 | |
| 164 | do_in_bta_thread( |
Jakub Pawlowski | 0482c8a | 2017-02-17 12:18:59 -0800 | [diff] [blame] | 165 | 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 Pawlowski | 5204c62 | 2017-03-30 20:41:02 -0700 | [diff] [blame] | 170 | timeout_s * 100, jni_thread_wrapper(FROM_HERE, timeout_cb))); |
Jakub Pawlowski | a75087e | 2016-11-17 13:27:28 -0800 | [diff] [blame] | 171 | } |
Jakub Pawlowski | 16da921 | 2017-03-11 16:46:00 -0800 | [diff] [blame] | 172 | |
Jakub Pawlowski | ff32af6 | 2017-03-17 18:05:38 -0700 | [diff] [blame] | 173 | void StartAdvertisingSet(IdTxPowerStatusCallback cb, |
| 174 | AdvertiseParameters params, |
Jakub Pawlowski | 16da921 | 2017-03-11 16:46:00 -0800 | [diff] [blame] | 175 | std::vector<uint8_t> advertise_data, |
| 176 | std::vector<uint8_t> scan_response_data, |
| 177 | PeriodicAdvertisingParameters periodic_params, |
Jakub Pawlowski | 5204c62 | 2017-03-30 20:41:02 -0700 | [diff] [blame] | 178 | std::vector<uint8_t> periodic_data, |
| 179 | uint16_t duration, uint8_t maxExtAdvEvents, |
Jakub Pawlowski | ff32af6 | 2017-03-17 18:05:38 -0700 | [diff] [blame] | 180 | IdStatusCallback timeout_cb) override { |
Jakub Pawlowski | 16da921 | 2017-03-11 16:46:00 -0800 | [diff] [blame] | 181 | VLOG(1) << __func__; |
| 182 | |
| 183 | tBTM_BLE_ADV_PARAMS* p_params = new tBTM_BLE_ADV_PARAMS; |
Jakub Pawlowski | ff32af6 | 2017-03-17 18:05:38 -0700 | [diff] [blame] | 184 | parseParams(p_params, params); |
Jakub Pawlowski | 16da921 | 2017-03-11 16:46:00 -0800 | [diff] [blame] | 185 | |
| 186 | tBLE_PERIODIC_ADV_PARAMS* p_periodic_params = new tBLE_PERIODIC_ADV_PARAMS; |
Jakub Pawlowski | ff32af6 | 2017-03-17 18:05:38 -0700 | [diff] [blame] | 187 | parsePeriodicParams(p_periodic_params, periodic_params); |
Jakub Pawlowski | 16da921 | 2017-03-11 16:46:00 -0800 | [diff] [blame] | 188 | |
| 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 Pawlowski | 5204c62 | 2017-03-30 20:41:02 -0700 | [diff] [blame] | 195 | base::Owned(p_periodic_params), std::move(periodic_data), duration, |
| 196 | maxExtAdvEvents, jni_thread_wrapper(FROM_HERE, timeout_cb))); |
Jakub Pawlowski | ff32af6 | 2017-03-17 18:05:38 -0700 | [diff] [blame] | 197 | } |
| 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 Pawlowski | 74ef54b | 2016-10-06 16:52:30 -0700 | [diff] [blame] | 237 | }; |
Jakub Pawlowski | 9eaf776 | 2016-10-04 19:30:09 -0700 | [diff] [blame] | 238 | |
Jakub Pawlowski | 74ef54b | 2016-10-06 16:52:30 -0700 | [diff] [blame] | 239 | BleAdvertiserInterface* btLeAdvertiserInstance = nullptr; |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 240 | |
| 241 | } // namespace |
| 242 | |
Jakub Pawlowski | 74ef54b | 2016-10-06 16:52:30 -0700 | [diff] [blame] | 243 | BleAdvertiserInterface* get_ble_advertiser_instance() { |
| 244 | if (btLeAdvertiserInstance == nullptr) |
| 245 | btLeAdvertiserInstance = new BleAdvertiserInterfaceImpl(); |
| 246 | |
| 247 | return btLeAdvertiserInstance; |
| 248 | } |