Home | History | Annotate | Download | only in btm
      1 /******************************************************************************
      2  *
      3  *  Copyright 2016 The Android Open Source Project
      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 #ifndef BLE_ADVERTISER_HCI_INTERFACE_H
     20 #define BLE_ADVERTISER_HCI_INTERFACE_H
     21 
     22 #include <base/bind.h>
     23 #include <vector>
     24 #include "stack/include/bt_types.h"
     25 
     26 /* This class is an abstraction of HCI commands used for managing
     27  * advertisements. Please see VSC HCI SPEC at
     28  * https://static.googleusercontent.com/media/source.android.com/en//devices/Android-6.0-Bluetooth-HCI-Reqs.pdf
     29  * and Bluetooth 5.0 "Advertising Extension" feature for more details  */
     30 class BleAdvertiserHciInterface {
     31  public:
     32   using status_cb = base::Callback<void(uint8_t /* status */)>;
     33   using parameters_cb =
     34       base::Callback<void(uint8_t /* status */, int8_t /* tx_power */)>;
     35 
     36   static void Initialize();
     37   static BleAdvertiserHciInterface* Get();
     38   static void CleanUp();
     39 
     40   virtual ~BleAdvertiserHciInterface() = default;
     41 
     42   class AdvertisingEventObserver {
     43    public:
     44     virtual ~AdvertisingEventObserver() = default;
     45     virtual void OnAdvertisingSetTerminated(
     46         uint8_t status, uint8_t advertising_handle, uint16_t connection_handle,
     47         uint8_t num_completed_extended_adv_events) = 0;
     48   };
     49 
     50   virtual void SetAdvertisingEventObserver(
     51       AdvertisingEventObserver* observer) = 0;
     52   virtual void ReadInstanceCount(
     53       base::Callback<void(uint8_t /* inst_cnt*/)> cb) = 0;
     54   virtual void SetParameters(
     55       uint8_t handle, uint16_t properties, uint32_t adv_int_min,
     56       uint32_t adv_int_max, uint8_t channel_map, uint8_t own_address_type,
     57       const RawAddress& own_address, uint8_t peer_address_type,
     58       const RawAddress& peer_address, uint8_t filter_policy, int8_t tx_power,
     59       uint8_t primary_phy, uint8_t secondary_max_skip, uint8_t secondary_phy,
     60       uint8_t advertising_sid, uint8_t scan_request_notify_enable,
     61       parameters_cb command_complete) = 0;
     62   virtual void SetAdvertisingData(uint8_t handle, uint8_t operation,
     63                                   uint8_t fragment_preference,
     64                                   uint8_t data_length, uint8_t* data,
     65                                   status_cb command_complete) = 0;
     66   virtual void SetScanResponseData(uint8_t handle, uint8_t operation,
     67                                    uint8_t fragment_preference,
     68                                    uint8_t scan_response_data_length,
     69                                    uint8_t* scan_response_data,
     70                                    status_cb command_complete) = 0;
     71   virtual void SetRandomAddress(uint8_t handle,
     72                                 const RawAddress& random_address,
     73                                 status_cb command_complete) = 0;
     74 
     75   struct SetEnableData {
     76     uint8_t handle;
     77     uint16_t duration;
     78     uint8_t max_extended_advertising_events;
     79   };
     80   virtual void Enable(uint8_t enable, std::vector<SetEnableData> sets,
     81                       status_cb command_complete) = 0;
     82 
     83   void Enable(uint8_t enable, uint8_t handle, uint16_t duration,
     84               uint8_t max_extended_advertising_events,
     85               status_cb command_complete) {
     86     std::vector<SetEnableData> enableData;
     87     enableData.emplace_back(SetEnableData{
     88         .handle = handle,
     89         .duration = duration,
     90         .max_extended_advertising_events = max_extended_advertising_events});
     91     Enable(enable, enableData, command_complete);
     92   };
     93   virtual void SetPeriodicAdvertisingParameters(uint8_t handle,
     94                                                 uint16_t periodic_adv_int_min,
     95                                                 uint16_t periodic_adv_int_max,
     96                                                 uint16_t periodic_properties,
     97                                                 status_cb command_complete) = 0;
     98   virtual void SetPeriodicAdvertisingData(uint8_t handle, uint8_t operation,
     99                                           uint8_t adv_data_length,
    100                                           uint8_t* adv_data,
    101                                           status_cb command_complete) = 0;
    102   virtual void SetPeriodicAdvertisingEnable(uint8_t enable, uint8_t handle,
    103                                             status_cb command_complete) = 0;
    104   virtual void RemoveAdvertisingSet(uint8_t handle,
    105                                     status_cb command_complete) = 0;
    106 
    107   // Some implementation don't behave well when handle value 0 is used.
    108   virtual bool QuirkAdvertiserZeroHandle() { return 0; }
    109 };
    110 
    111 #endif  // BLE_ADVERTISER_HCI_INTERFACE_H
    112