Home | History | Annotate | Download | only in src
      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>
     25 #include <vector>
     26 
     27 #include "ble_advertiser.h"
     28 #include "bta_closure_api.h"
     29 #include "btif_common.h"
     30 
     31 using base::Bind;
     32 using base::Owned;
     33 using std::vector;
     34 
     35 namespace {
     36 
     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();
     55 }
     56 
     57 template <typename T>
     58 static inline OwnedArrayWrapper<T> OwnedArray(T* o) {
     59   return OwnedArrayWrapper<T>(o);
     60 }
     61 
     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 
     85 class BleAdvertiserInterfaceImpl : public BleAdvertiserInterface {
     86   ~BleAdvertiserInterfaceImpl(){};
     87 
     88   void RegisterAdvertiserCb(IdStatusCallback cb, uint8_t advertiser_id,
     89                             uint8_t status) {
     90     LOG(INFO) << __func__ << " status: " << +status
     91               << " , adveriser_id: " << +advertiser_id;
     92     do_in_jni_thread(Bind(cb, advertiser_id, status));
     93   }
     94 
     95   void RegisterAdvertiser(IdStatusCallback cb) override {
     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 
    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 
    117   void SetParameters(uint8_t advertiser_id, AdvertiseParameters params,
    118                      ParametersCallback cb) override {
    119     VLOG(1) << __func__;
    120 
    121     tBTM_BLE_ADV_PARAMS* p_params = new tBTM_BLE_ADV_PARAMS;
    122     parseParams(p_params, params);
    123 
    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)));
    129   }
    130 
    131   void SetData(int advertiser_id, bool set_scan_rsp, vector<uint8_t> data,
    132                StatusCallback cb) override {
    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)));
    138   }
    139 
    140   void Enable(uint8_t advertiser_id, bool enable, StatusCallback cb,
    141               uint16_t duration, uint8_t maxExtAdvEvents,
    142               StatusCallback timeout_cb) override {
    143     VLOG(1) << __func__ << " advertiser_id: " << +advertiser_id
    144             << " ,enable: " << enable;
    145 
    146     do_in_bta_thread(
    147         FROM_HERE,
    148         Bind(&BleAdvertisingManager::Enable,
    149              base::Unretained(BleAdvertisingManager::Get()), advertiser_id,
    150              enable, jni_thread_wrapper(FROM_HERE, cb), duration,
    151              maxExtAdvEvents, jni_thread_wrapper(FROM_HERE, timeout_cb)));
    152   }
    153 
    154   void StartAdvertising(uint8_t advertiser_id, StatusCallback cb,
    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;
    162     parseParams(p_params, params);
    163 
    164     do_in_bta_thread(
    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),
    170              timeout_s * 100, jni_thread_wrapper(FROM_HERE, timeout_cb)));
    171   }
    172 
    173   void StartAdvertisingSet(IdTxPowerStatusCallback cb,
    174                            AdvertiseParameters params,
    175                            std::vector<uint8_t> advertise_data,
    176                            std::vector<uint8_t> scan_response_data,
    177                            PeriodicAdvertisingParameters periodic_params,
    178                            std::vector<uint8_t> periodic_data,
    179                            uint16_t duration, uint8_t maxExtAdvEvents,
    180                            IdStatusCallback timeout_cb) override {
    181     VLOG(1) << __func__;
    182 
    183     tBTM_BLE_ADV_PARAMS* p_params = new tBTM_BLE_ADV_PARAMS;
    184     parseParams(p_params, params);
    185 
    186     tBLE_PERIODIC_ADV_PARAMS* p_periodic_params = new tBLE_PERIODIC_ADV_PARAMS;
    187     parsePeriodicParams(p_periodic_params, periodic_params);
    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),
    195              base::Owned(p_periodic_params), std::move(periodic_data), duration,
    196              maxExtAdvEvents, jni_thread_wrapper(FROM_HERE, timeout_cb)));
    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   }
    237 };
    238 
    239 BleAdvertiserInterface* btLeAdvertiserInstance = nullptr;
    240 
    241 }  // namespace
    242 
    243 BleAdvertiserInterface* get_ble_advertiser_instance() {
    244   if (btLeAdvertiserInstance == nullptr)
    245     btLeAdvertiserInstance = new BleAdvertiserInterfaceImpl();
    246 
    247   return btLeAdvertiserInstance;
    248 }
    249