• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <memory>
19 
20 #include "common/callback.h"
21 #include "hci/address_with_type.h"
22 #include "hci/hci_packets.h"
23 #include "module.h"
24 
25 namespace bluetooth {
26 namespace hci {
27 
28 class PeriodicAdvertisingParameters {
29  public:
30   uint16_t min_interval;
31   uint16_t max_interval;
32   uint16_t properties;
33   enum AdvertisingProperty { INCLUDE_TX_POWER = 0x06 };
34 };
35 
36 class AdvertisingConfig {
37  public:
38   std::vector<GapData> advertisement;
39   std::vector<GapData> scan_response;
40   uint16_t interval_min;
41   uint16_t interval_max;
42   AdvertisingType advertising_type;
43   OwnAddressType own_address_type;
44   PeerAddressType peer_address_type;
45   Address peer_address;
46   uint8_t channel_map;
47   AdvertisingFilterPolicy filter_policy;
48   uint8_t tx_power;  // -127 to +20 (0x7f is no preference)
49 };
50 
51 class ExtendedAdvertisingConfig : public AdvertisingConfig {
52  public:
53   bool connectable = false;
54   bool scannable = false;
55   bool directed = false;
56   bool high_duty_directed_connectable = false;
57   bool legacy_pdus = false;
58   bool anonymous = false;
59   bool include_tx_power = false;
60   bool use_le_coded_phy;       // Primary advertisement PHY is LE Coded
61   uint8_t secondary_max_skip;  // maximum advertising events to be skipped, 0x0 send AUX_ADV_IND prior ot the next event
62   SecondaryPhyType secondary_advertising_phy;
63   uint8_t sid = 0x00;
64   Enable enable_scan_request_notifications = Enable::DISABLED;
65   std::vector<GapData> periodic_data;
66   PeriodicAdvertisingParameters periodic_advertising_parameters;
67   ExtendedAdvertisingConfig() = default;
68   ExtendedAdvertisingConfig(const AdvertisingConfig& config);
69 };
70 
71 using AdvertiserId = uint8_t;
72 
73 class AdvertisingCallback {
74  public:
75   enum AdvertisingStatus {
76     SUCCESS,
77     DATA_TOO_LARGE,
78     TOO_MANY_ADVERTISERS,
79     ALREADY_STARTED,
80     INTERNAL_ERROR,
81     FEATURE_UNSUPPORTED
82   };
83 
84   virtual ~AdvertisingCallback() = default;
85   virtual void OnAdvertisingSetStarted(
86       int reg_id, uint8_t advertiser_id, int8_t tx_power, AdvertisingStatus status) = 0;
87   virtual void OnAdvertisingEnabled(uint8_t advertiser_id, bool enable, uint8_t status) = 0;
88   virtual void OnAdvertisingDataSet(uint8_t advertiser_id, uint8_t status) = 0;
89   virtual void OnScanResponseDataSet(uint8_t advertiser_id, uint8_t status) = 0;
90   virtual void OnAdvertisingParametersUpdated(uint8_t advertiser_id, int8_t tx_power, uint8_t status) = 0;
91   virtual void OnPeriodicAdvertisingParametersUpdated(uint8_t advertiser_id, uint8_t status) = 0;
92   virtual void OnPeriodicAdvertisingDataSet(uint8_t advertiser_id, uint8_t status) = 0;
93   virtual void OnPeriodicAdvertisingEnabled(uint8_t advertiser_id, bool enable, uint8_t status) = 0;
94   virtual void OnOwnAddressRead(uint8_t advertiser_id, uint8_t address_type, Address address) = 0;
95 };
96 
97 class LeAdvertisingManager : public bluetooth::Module {
98  public:
99   static constexpr AdvertiserId kInvalidId = 0xFF;
100   static constexpr uint8_t kInvalidHandle = 0xFF;
101   static constexpr uint8_t kAdvertisingSetIdMask = 0x0F;
102   static constexpr uint16_t kLeMaximumFragmentLength = 251;
103   static constexpr FragmentPreference kFragment_preference = FragmentPreference::CONTROLLER_SHOULD_NOT;
104   LeAdvertisingManager();
105   LeAdvertisingManager(const LeAdvertisingManager&) = delete;
106   LeAdvertisingManager& operator=(const LeAdvertisingManager&) = delete;
107 
108   size_t GetNumberOfAdvertisingInstances() const;
109 
110   AdvertiserId ExtendedCreateAdvertiser(
111       int reg_id,
112       const ExtendedAdvertisingConfig config,
113       const common::Callback<void(Address, AddressType)>& scan_callback,
114       const common::Callback<void(ErrorCode, uint8_t, uint8_t)>& set_terminated_callback,
115       uint16_t duration,
116       uint8_t max_extended_advertising_events,
117       os::Handler* handler);
118 
119   void StartAdvertising(
120       AdvertiserId advertiser_id,
121       const ExtendedAdvertisingConfig config,
122       uint16_t duration,
123       base::OnceCallback<void(uint8_t /* status */)> status_callback,
124       base::OnceCallback<void(uint8_t /* status */)> timeout_callback,
125       const common::Callback<void(Address, AddressType)>& scan_callback,
126       const common::Callback<void(ErrorCode, uint8_t, uint8_t)>& set_terminated_callback,
127       os::Handler* handler);
128 
129   void GetOwnAddress(uint8_t advertiser_id);
130 
131   void RegisterAdvertiser(base::OnceCallback<void(uint8_t /* inst_id */, uint8_t /* status */)> callback);
132 
133   void SetParameters(AdvertiserId advertiser_id, ExtendedAdvertisingConfig config);
134 
135   void SetData(AdvertiserId advertiser_id, bool set_scan_rsp, std::vector<GapData> data);
136 
137   void EnableAdvertiser(
138       AdvertiserId advertiser_id, bool enable, uint16_t duration, uint8_t max_extended_advertising_events);
139 
140   void SetPeriodicParameters(AdvertiserId advertiser_id, PeriodicAdvertisingParameters periodic_advertising_parameters);
141 
142   void SetPeriodicData(AdvertiserId advertiser_id, std::vector<GapData> data);
143 
144   void EnablePeriodicAdvertising(AdvertiserId advertiser_id, bool enable);
145 
146   void RemoveAdvertiser(AdvertiserId advertiser_id);
147 
148   void RegisterAdvertisingCallback(AdvertisingCallback* advertising_callback);
149 
150   static const ModuleFactory Factory;
151 
152  protected:
153   void ListDependencies(ModuleList* list) const override;
154 
155   void Start() override;
156 
157   void Stop() override;
158 
159   std::string ToString() const override;
160 
161  private:
162   // Return -1 if the advertiser was not created, otherwise the advertiser ID.
163   AdvertiserId create_advertiser(
164       int reg_id,
165       const AdvertisingConfig config,
166       const common::Callback<void(Address, AddressType)>& scan_callback,
167       const common::Callback<void(ErrorCode, uint8_t, uint8_t)>& set_terminated_callback,
168       os::Handler* handler);
169   struct impl;
170   std::unique_ptr<impl> pimpl_;
171 };
172 
173 }  // namespace hci
174 }  // namespace bluetooth
175