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