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