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