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 "hci/hci_packets.h"
21 #include "module.h"
22 
23 namespace bluetooth {
24 namespace hci {
25 
26 class AdvertisingConfig {
27  public:
28   std::vector<GapData> advertisement;
29   std::vector<GapData> scan_response;
30   uint16_t interval_min;
31   uint16_t interval_max;
32   AdvertisingType event_type;
33   AddressType address_type;
34   PeerAddressType peer_address_type;
35   Address peer_address;
36   uint8_t channel_map;
37   AdvertisingFilterPolicy filter_policy;
38   uint8_t tx_power;  // -127 to +20 (0x7f is no preference)
39 };
40 
41 class ExtendedAdvertisingConfig : public AdvertisingConfig {
42  public:
43   bool connectable = false;
44   bool scannable = false;
45   bool directed = false;
46   bool high_duty_directed_connectable = false;
47   bool legacy_pdus = false;
48   bool anonymous = false;
49   bool include_tx_power = false;
50   bool use_le_coded_phy;       // Primary advertisement PHY is LE Coded
51   uint8_t secondary_max_skip;  // maximum advertising events to be skipped, 0x0 send AUX_ADV_IND prior ot the next event
52   SecondaryPhyType secondary_advertising_phy;
53   uint8_t sid = 0x00;
54   Enable enable_scan_request_notifications = Enable::DISABLED;
55   OwnAddressType own_address_type;
56   Operation operation;  // TODO(b/149221472): Support fragmentation
57   FragmentPreference fragment_preference = FragmentPreference::CONTROLLER_SHOULD_NOT;
58   ExtendedAdvertisingConfig() = default;
59   ExtendedAdvertisingConfig(const AdvertisingConfig& config);
60 };
61 
62 using AdvertiserId = int32_t;
63 
64 class LeAdvertisingManager : public bluetooth::Module {
65  public:
66   static constexpr AdvertiserId kInvalidId = -1;
67   LeAdvertisingManager();
68 
69   size_t GetNumberOfAdvertisingInstances() const;
70 
71   // Return -1 if the advertiser was not created, otherwise the advertiser ID.
72   AdvertiserId CreateAdvertiser(const AdvertisingConfig& config,
73                                 const common::Callback<void(Address, AddressType)>& scan_callback,
74                                 const common::Callback<void(ErrorCode, uint8_t, uint8_t)>& set_terminated_callback,
75                                 os::Handler* handler);
76   AdvertiserId ExtendedCreateAdvertiser(
77       const ExtendedAdvertisingConfig& config, const common::Callback<void(Address, AddressType)>& scan_callback,
78       const common::Callback<void(ErrorCode, uint8_t, uint8_t)>& set_terminated_callback, os::Handler* handler);
79 
80   void RemoveAdvertiser(AdvertiserId id);
81 
82   static const ModuleFactory Factory;
83 
84  protected:
85   void ListDependencies(ModuleList* list) override;
86 
87   void Start() override;
88 
89   void Stop() override;
90 
91   std::string ToString() const override;
92 
93  private:
94   struct impl;
95   std::unique_ptr<impl> pimpl_;
96   DISALLOW_COPY_AND_ASSIGN(LeAdvertisingManager);
97 };
98 
99 }  // namespace hci
100 }  // namespace bluetooth