• 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 };
95 
96 class LeAdvertisingManager : public bluetooth::Module {
97  public:
98   static constexpr AdvertiserId kInvalidId = 0xFF;
99   static constexpr uint8_t kInvalidHandle = 0xFF;
100   static constexpr uint8_t kAdvertisingSetIdMask = 0x0F;
101   static constexpr uint16_t kLeMaximumFragmentLength = 251;
102   static constexpr FragmentPreference kFragment_preference = FragmentPreference::CONTROLLER_SHOULD_NOT;
103   LeAdvertisingManager();
104 
105   size_t GetNumberOfAdvertisingInstances() const;
106 
107   AdvertiserId ExtendedCreateAdvertiser(
108       int reg_id,
109       const ExtendedAdvertisingConfig config,
110       const common::Callback<void(Address, AddressType)>& scan_callback,
111       const common::Callback<void(ErrorCode, uint8_t, uint8_t)>& set_terminated_callback,
112       uint16_t duration,
113       uint8_t max_extended_advertising_events,
114       os::Handler* handler);
115 
116   void SetParameters(AdvertiserId advertiser_id, ExtendedAdvertisingConfig config);
117 
118   void SetData(AdvertiserId advertiser_id, bool set_scan_rsp, std::vector<GapData> data);
119 
120   void EnableAdvertiser(
121       AdvertiserId advertiser_id, bool enable, uint16_t duration, uint8_t max_extended_advertising_events);
122 
123   void SetPeriodicParameters(AdvertiserId advertiser_id, PeriodicAdvertisingParameters periodic_advertising_parameters);
124 
125   void SetPeriodicData(AdvertiserId advertiser_id, std::vector<GapData> data);
126 
127   void EnablePeriodicAdvertising(AdvertiserId advertiser_id, bool enable);
128 
129   void RemoveAdvertiser(AdvertiserId advertiser_id);
130 
131   void RegisterAdvertisingCallback(AdvertisingCallback* advertising_callback);
132 
133   static const ModuleFactory Factory;
134 
135  protected:
136   void ListDependencies(ModuleList* list) override;
137 
138   void Start() override;
139 
140   void Stop() override;
141 
142   std::string ToString() const override;
143 
144  private:
145   // Return -1 if the advertiser was not created, otherwise the advertiser ID.
146   AdvertiserId create_advertiser(
147       const AdvertisingConfig config,
148       const common::Callback<void(Address, AddressType)>& scan_callback,
149       const common::Callback<void(ErrorCode, uint8_t, uint8_t)>& set_terminated_callback,
150       os::Handler* handler);
151   struct impl;
152   std::unique_ptr<impl> pimpl_;
153   DISALLOW_COPY_AND_ASSIGN(LeAdvertisingManager);
154 };
155 
156 }  // namespace hci
157 }  // namespace bluetooth