1 /* 2 * Copyright 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #pragma once 17 18 #include <bluetooth/log.h> 19 20 #include <memory> 21 #include <vector> 22 23 #include "common/callback.h" 24 #include "hci/address_with_type.h" 25 #include "hci/hci_packets.h" 26 #include "module.h" 27 28 namespace bluetooth { 29 namespace hci { 30 31 class PeriodicAdvertisingParameters { 32 public: 33 bool enable; 34 bool include_adi; 35 uint16_t min_interval; 36 uint16_t max_interval; 37 uint16_t properties; 38 enum AdvertisingProperty { INCLUDE_TX_POWER = 0x06 }; 39 }; 40 41 enum class AdvertiserAddressType { 42 PUBLIC, 43 RESOLVABLE_RANDOM, 44 NONRESOLVABLE_RANDOM, 45 }; 46 47 class AdvertisingConfig { 48 public: 49 std::vector<GapData> advertisement; 50 std::vector<GapData> scan_response; 51 uint16_t interval_min; 52 uint16_t interval_max; 53 AdvertisingType advertising_type; 54 AdvertiserAddressType requested_advertiser_address_type; 55 PeerAddressType peer_address_type; 56 Address peer_address; 57 uint8_t channel_map; 58 AdvertisingFilterPolicy filter_policy; 59 uint8_t tx_power; // -127 to +20 (0x7f is no preference) 60 bool connectable = false; 61 bool discoverable = false; 62 bool scannable = false; 63 bool directed = false; 64 bool high_duty_directed_connectable = false; 65 bool legacy_pdus = false; 66 bool anonymous = false; 67 bool include_tx_power = false; 68 bool use_le_coded_phy; // Primary advertisement PHY is LE Coded 69 uint8_t secondary_max_skip; // maximum advertising events to be skipped, 0x0 send AUX_ADV_IND prior ot the next event 70 SecondaryPhyType secondary_advertising_phy; 71 uint8_t sid = 0x00; 72 Enable enable_scan_request_notifications = Enable::DISABLED; 73 std::vector<GapData> periodic_data; 74 PeriodicAdvertisingParameters periodic_advertising_parameters; 75 AdvertisingConfig() = default; 76 }; 77 78 using AdvertiserId = uint8_t; 79 80 class AdvertisingCallback { 81 public: 82 enum AdvertisingStatus { 83 SUCCESS, 84 DATA_TOO_LARGE, 85 TOO_MANY_ADVERTISERS, 86 ALREADY_STARTED, 87 INTERNAL_ERROR, 88 FEATURE_UNSUPPORTED 89 }; 90 91 virtual ~AdvertisingCallback() = default; 92 virtual void OnAdvertisingSetStarted( 93 int reg_id, uint8_t advertiser_id, int8_t tx_power, AdvertisingStatus status) = 0; 94 virtual void OnAdvertisingEnabled(uint8_t advertiser_id, bool enable, uint8_t status) = 0; 95 virtual void OnAdvertisingDataSet(uint8_t advertiser_id, uint8_t status) = 0; 96 virtual void OnScanResponseDataSet(uint8_t advertiser_id, uint8_t status) = 0; 97 virtual void OnAdvertisingParametersUpdated(uint8_t advertiser_id, int8_t tx_power, uint8_t status) = 0; 98 virtual void OnPeriodicAdvertisingParametersUpdated(uint8_t advertiser_id, uint8_t status) = 0; 99 virtual void OnPeriodicAdvertisingDataSet(uint8_t advertiser_id, uint8_t status) = 0; 100 virtual void OnPeriodicAdvertisingEnabled(uint8_t advertiser_id, bool enable, uint8_t status) = 0; 101 virtual void OnOwnAddressRead(uint8_t advertiser_id, uint8_t address_type, Address address) = 0; 102 }; 103 104 class LeAdvertisingManager : public bluetooth::Module { 105 public: 106 static constexpr AdvertiserId kInvalidId = 0xFF; 107 static constexpr uint8_t kInvalidHandle = 0xFF; 108 static constexpr uint8_t kAdvertisingSetIdMask = 0x0F; 109 static constexpr uint16_t kLeMaximumLegacyAdvertisingDataLength = 31; 110 static constexpr uint16_t kLeMaximumFragmentLength = 251; 111 static constexpr uint16_t kLeMaximumPeriodicDataFragmentLength = 252; 112 static constexpr uint16_t kLeMaximumGapDataLength = 255; 113 static constexpr FragmentPreference kFragment_preference = FragmentPreference::CONTROLLER_SHOULD_NOT; 114 LeAdvertisingManager(); 115 LeAdvertisingManager(const LeAdvertisingManager&) = delete; 116 LeAdvertisingManager& operator=(const LeAdvertisingManager&) = delete; 117 118 size_t GetNumberOfAdvertisingInstances() const; 119 120 size_t GetNumberOfAdvertisingInstancesInUse() const; 121 122 int GetAdvertiserRegId(AdvertiserId advertiser_id); 123 124 void ExtendedCreateAdvertiser( 125 uint8_t client_id, 126 int reg_id, 127 const AdvertisingConfig config, 128 common::Callback<void(Address, AddressType)> scan_callback, 129 common::Callback<void(ErrorCode, uint8_t, uint8_t)> set_terminated_callback, 130 uint16_t duration, 131 uint8_t max_extended_advertising_events, 132 os::Handler* handler); 133 134 void StartAdvertising( 135 AdvertiserId advertiser_id, 136 const AdvertisingConfig config, 137 uint16_t duration, 138 base::OnceCallback<void(uint8_t /* status */)> status_callback, 139 base::OnceCallback<void(uint8_t /* status */)> timeout_callback, 140 common::Callback<void(Address, AddressType)> scan_callback, 141 common::Callback<void(ErrorCode, uint8_t, uint8_t)> set_terminated_callback, 142 os::Handler* handler); 143 144 void GetOwnAddress(uint8_t advertiser_id); 145 146 void RegisterAdvertiser( 147 common::ContextualOnceCallback<void(uint8_t /* inst_id */, uint8_t /* status */)> callback); 148 149 void SetParameters(AdvertiserId advertiser_id, AdvertisingConfig config); 150 151 void SetData(AdvertiserId advertiser_id, bool set_scan_rsp, std::vector<GapData> data); 152 153 void EnableAdvertiser( 154 AdvertiserId advertiser_id, bool enable, uint16_t duration, uint8_t max_extended_advertising_events); 155 156 void SetPeriodicParameters(AdvertiserId advertiser_id, PeriodicAdvertisingParameters periodic_advertising_parameters); 157 158 void SetPeriodicData(AdvertiserId advertiser_id, std::vector<GapData> data); 159 160 void EnablePeriodicAdvertising(AdvertiserId advertiser_id, bool enable, bool include_adi); 161 162 void RemoveAdvertiser(AdvertiserId advertiser_id); 163 164 void RegisterAdvertisingCallback(AdvertisingCallback* advertising_callback); 165 166 static const ModuleFactory Factory; 167 168 protected: 169 void ListDependencies(ModuleList* list) const override; 170 171 void Start() override; 172 173 void Stop() override; 174 175 std::string ToString() const override; 176 177 private: 178 struct impl; 179 std::unique_ptr<impl> pimpl_; 180 }; 181 182 } // namespace hci 183 } // namespace bluetooth 184 185 namespace fmt { 186 template <> 187 struct formatter<bluetooth::hci::AdvertiserAddressType> 188 : enum_formatter<bluetooth::hci::AdvertiserAddressType> {}; 189 } // namespace fmt 190