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_H
20 #define BLE_ADVERTISER_H
21 
22 #include <base/bind.h>
23 #include <base/memory/weak_ptr.h>
24 #include <vector>
25 #include "btm_ble_api.h"
26 
27 #define BTM_BLE_MULTI_ADV_SUCCESS 0
28 #define BTM_BLE_MULTI_ADV_FAILURE 1
29 #define ADVERTISE_FAILED_TOO_MANY_ADVERTISERS 0x02
30 
31 using MultiAdvCb = base::Callback<void(uint8_t /* status */)>;
32 using ParametersCb =
33     base::Callback<void(uint8_t /* status */, int8_t /* tx_power */)>;
34 
35 // methods we must have defined
36 void btm_ble_update_dmt_flag_bits(uint8_t* flag_value,
37                                   const uint16_t connect_mode,
38                                   const uint16_t disc_mode);
39 void btm_acl_update_conn_addr(uint16_t conn_handle, const RawAddress& address);
40 
41 // methods we expose to c code:
42 void btm_ble_multi_adv_cleanup(void);
43 void btm_ble_multi_adv_init();
44 
45 typedef struct {
46   uint16_t advertising_event_properties;
47   uint32_t adv_int_min;
48   uint32_t adv_int_max;
49   tBTM_BLE_ADV_CHNL_MAP channel_map;
50   tBTM_BLE_AFP adv_filter_policy;
51   int8_t tx_power;
52   uint8_t primary_advertising_phy;
53   uint8_t secondary_advertising_phy;
54   uint8_t scan_request_notification_enable;
55 } tBTM_BLE_ADV_PARAMS;
56 
57 typedef struct {
58   uint8_t enable;
59   uint16_t min_interval;
60   uint16_t max_interval;
61   uint16_t periodic_advertising_properties;
62 } tBLE_PERIODIC_ADV_PARAMS;
63 
64 class BleAdvertiserHciInterface;
65 
66 class BleAdvertisingManager {
67  public:
68   virtual ~BleAdvertisingManager() = default;
69 
70   static const uint16_t advertising_prop_legacy_connectable = 0x0011;
71   static const uint16_t advertising_prop_legacy_non_connectable = 0x0010;
72 
73   static void Initialize(BleAdvertiserHciInterface* interface);
74   static void CleanUp();
75   static bool IsInitialized();
76   static base::WeakPtr<BleAdvertisingManager> Get();
77 
78   /* Register an advertising instance, status will be returned in |cb|
79    * callback, with assigned id, if operation succeeds. Instance is freed when
80    * advertising is disabled by calling |BTM_BleDisableAdvInstance|, or when any
81    * of the operations fails.
82    * The instance will have data set to |advertise_data|, scan response set to
83    * |scan_response_data|, and will be enabled.
84    */
85   virtual void StartAdvertising(uint8_t advertiser_id, MultiAdvCb cb,
86                                 tBTM_BLE_ADV_PARAMS* params,
87                                 std::vector<uint8_t> advertise_data,
88                                 std::vector<uint8_t> scan_response_data,
89                                 int duration, MultiAdvCb timeout_cb) = 0;
90 
91   /* Register an advertising instance, status will be returned in |cb|
92    * callback, with assigned id, if operation succeeds. Instance is freed when
93    * advertising is disabled by calling |BTM_BleDisableAdvInstance|, or when any
94    * of the operations fails.
95    * The instance will have data set to |advertise_data|, scan response set to
96    * |scan_response_data|, periodic data set to |periodic_data| and will be
97    * enabled.
98    */
99   virtual void StartAdvertisingSet(
100       base::Callback<void(uint8_t /* inst_id */, int8_t /* tx_power */,
101                           uint8_t /* status */)>
102           cb,
103       tBTM_BLE_ADV_PARAMS* params, std::vector<uint8_t> advertise_data,
104       std::vector<uint8_t> scan_response_data,
105       tBLE_PERIODIC_ADV_PARAMS* periodic_params,
106       std::vector<uint8_t> periodic_data, uint16_t duration,
107       uint8_t maxExtAdvEvents,
108       base::Callback<void(uint8_t /* inst_id */, uint8_t /* status */)>
109           timeout_cb) = 0;
110 
111   /* Register an advertising instance, status will be returned in |cb|
112    * callback, with assigned id, if operation succeeds. Instance is freed when
113    * advertising is disabled by calling |BTM_BleDisableAdvInstance|, or when any
114    * of the operations fails. */
115   virtual void RegisterAdvertiser(
116       base::Callback<void(uint8_t /* inst_id */, uint8_t /* status */)>) = 0;
117 
118   /* This function enables/disables an advertising instance. Operation status is
119    * returned in |cb| */
120   virtual void Enable(uint8_t inst_id, bool enable, MultiAdvCb cb,
121                       uint16_t duration, uint8_t maxExtAdvEvents,
122                       MultiAdvCb timeout_cb) = 0;
123 
124   /* This function update a Multi-ADV instance with the specififed adv
125    * parameters. */
126   virtual void SetParameters(uint8_t inst_id, tBTM_BLE_ADV_PARAMS* p_params,
127                              ParametersCb cb) = 0;
128 
129   /* This function configure a Multi-ADV instance with the specified adv data or
130    * scan response data.*/
131   virtual void SetData(uint8_t inst_id, bool is_scan_rsp,
132                        std::vector<uint8_t> data, MultiAdvCb cb) = 0;
133 
134   /* This function configure instance with the specified periodic parameters */
135   virtual void SetPeriodicAdvertisingParameters(
136       uint8_t inst_id, tBLE_PERIODIC_ADV_PARAMS* params, MultiAdvCb cb) = 0;
137 
138   /* This function configure instance with the specified periodic data */
139   virtual void SetPeriodicAdvertisingData(uint8_t inst_id,
140                                           std::vector<uint8_t> data,
141                                           MultiAdvCb cb) = 0;
142 
143   /* This function enables/disables periodic advertising on selected instance */
144   virtual void SetPeriodicAdvertisingEnable(uint8_t inst_id, uint8_t enable,
145                                             MultiAdvCb cb) = 0;
146 
147   /*  This function disable a Multi-ADV instance */
148   virtual void Unregister(uint8_t inst_id) = 0;
149 
150   /* When resolving list is used, we need to suspend and resume all advertising
151    * instances for the time of operation. Suspend() saves current state,
152    * Resume() resumes the advertising.
153    */
154   virtual void Suspend() = 0;
155   virtual void Resume() = 0;
156 
157   /* This method is a member of BleAdvertiserHciInterface, and is exposed here
158    * just for tests. It should never be called from upper layers*/
159   virtual void OnAdvertisingSetTerminated(
160       uint8_t status, uint8_t advertising_handle, uint16_t connection_handle,
161       uint8_t num_completed_extended_adv_events) = 0;
162 
163   using GetAddressCallback =
164       base::Callback<void(uint8_t /* address_type*/, RawAddress /*address*/)>;
165   virtual void GetOwnAddress(uint8_t inst_id, GetAddressCallback cb) = 0;
166 };
167 
168 #endif  // BLE_ADVERTISER_H
169