1 /******************************************************************************
2  *
3  *  Copyright (C) 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 "stack/include/bt_types.h"
24 
25 /* This class is an abstraction of HCI commands used for managing
26  * advertisements. Please see VSC HCI SPEC at
27  * https://static.googleusercontent.com/media/source.android.com/en//devices/Android-6.0-Bluetooth-HCI-Reqs.pdf
28  * and Bluetooth 5.0 "Advertising Extension" feature for more details  */
29 class BleAdvertiserHciInterface {
30  public:
31   using status_cb = base::Callback<void(uint8_t /* status */)>;
32   using parameters_cb =
33       base::Callback<void(uint8_t /* status */, int8_t /* tx_power */)>;
34 
35   static void Initialize();
36   static BleAdvertiserHciInterface* Get();
37   static void CleanUp();
38 
39   virtual ~BleAdvertiserHciInterface() = default;
40 
41   class AdvertisingEventObserver {
42    public:
43     virtual ~AdvertisingEventObserver() = default;
44     virtual void OnAdvertisingSetTerminated(
45         uint8_t status, uint8_t advertising_handle, uint16_t connection_handle,
46         uint8_t num_completed_extended_adv_events) = 0;
47   };
48 
49   virtual void SetAdvertisingEventObserver(
50       AdvertisingEventObserver* observer) = 0;
51   virtual void ReadInstanceCount(
52       base::Callback<void(uint8_t /* inst_cnt*/)> cb) = 0;
53   virtual void SetParameters(uint8_t handle, uint16_t properties,
54                              uint32_t adv_int_min, uint32_t adv_int_max,
55                              uint8_t channel_map, uint8_t own_address_type,
56                              BD_ADDR own_address, uint8_t peer_address_type,
57                              BD_ADDR peer_address, uint8_t filter_policy,
58                              int8_t tx_power, uint8_t primary_phy,
59                              uint8_t secondary_max_skip, uint8_t secondary_phy,
60                              uint8_t advertising_sid,
61                              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, BD_ADDR random_address,
73                                 status_cb command_complete) = 0;
74   virtual void Enable(uint8_t enable, uint8_t handle, uint16_t duration,
75                       uint8_t max_extended_advertising_events,
76                       status_cb command_complete) = 0;
77   virtual void SetPeriodicAdvertisingParameters(uint8_t handle,
78                                                 uint16_t periodic_adv_int_min,
79                                                 uint16_t periodic_adv_int_max,
80                                                 uint16_t periodic_properties,
81                                                 status_cb command_complete) = 0;
82   virtual void SetPeriodicAdvertisingData(uint8_t handle, uint8_t operation,
83                                           uint8_t adv_data_length,
84                                           uint8_t* adv_data,
85                                           status_cb command_complete) = 0;
86   virtual void SetPeriodicAdvertisingEnable(uint8_t enable, uint8_t handle,
87                                             status_cb command_complete) = 0;
88   virtual void RemoveAdvertisingSet(uint8_t handle,
89                                     status_cb command_complete) = 0;
90 
91   // Some implementation don't behave well when handle value 0 is used.
QuirkAdvertiserZeroHandle()92   virtual bool QuirkAdvertiserZeroHandle() { return 0; }
93 };
94 
95 #endif  // BLE_ADVERTISER_HCI_INTERFACE_H
96