1 // Copyright 2023 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #pragma once
16 #include "pw_bluetooth_sapphire/internal/host/hci/low_energy_advertiser.h"
17 
18 namespace bt::hci {
19 
20 class Transport;
21 class SequentialCommandRunner;
22 
23 class LegacyLowEnergyAdvertiser final : public LowEnergyAdvertiser {
24  public:
LegacyLowEnergyAdvertiser(hci::Transport::WeakPtr hci)25   explicit LegacyLowEnergyAdvertiser(hci::Transport::WeakPtr hci)
26       : LowEnergyAdvertiser(std::move(hci),
27                             hci_spec::kMaxLEAdvertisingDataLength) {}
28   ~LegacyLowEnergyAdvertiser() override;
29 
30   // LowEnergyAdvertiser overrides:
MaxAdvertisements()31   size_t MaxAdvertisements() const override { return 1; }
AllowsRandomAddressChange()32   bool AllowsRandomAddressChange() const override {
33     return !starting_ && !IsAdvertising();
34   }
35 
36   // LegacyLowEnergyAdvertiser supports only a single advertising instance,
37   // hence it can report additional errors in the following conditions:
38   // 1. If called while a start request is pending, reports kRepeatedAttempts.
39   // 2. If called while a stop request is pending, then cancels the stop request
40   //    and proceeds with start.
41   void StartAdvertising(const DeviceAddress& address,
42                         const AdvertisingData& data,
43                         const AdvertisingData& scan_rsp,
44                         const AdvertisingOptions& options,
45                         ConnectionCallback connect_callback,
46                         ResultFunction<> result_callback) override;
47 
48   void StopAdvertising() override;
49 
50   // If called while a stop request is pending, returns false.
51   // If called while a start request is pending, then cancels the start
52   // request and proceeds with start.
53   // Returns false if called while not advertising.
54   // TODO(fxbug.dev/42127634): Update documentation.
55   void StopAdvertising(const DeviceAddress& address,
56                        bool extended_pdu) override;
57 
58   void OnIncomingConnection(
59       hci_spec::ConnectionHandle handle,
60       pw::bluetooth::emboss::ConnectionRole role,
61       const DeviceAddress& peer_address,
62       const hci_spec::LEConnectionParameters& conn_params) override;
63 
64  private:
65   CommandPacket BuildEnablePacket(
66       const DeviceAddress& address,
67       pw::bluetooth::emboss::GenericEnableParam enable,
68       bool extended_pdu) override;
69 
70   std::optional<CommandPacket> BuildSetAdvertisingParams(
71       const DeviceAddress& address,
72       const AdvertisingEventProperties& properties,
73       pw::bluetooth::emboss::LEOwnAddressType own_address_type,
74       const AdvertisingIntervalRange& interval,
75       bool extended_pdu) override;
76 
77   std::vector<CommandPacket> BuildSetAdvertisingData(
78       const DeviceAddress& address,
79       const AdvertisingData& data,
80       AdvFlags flags,
81       bool extended_pdu) override;
82 
83   CommandPacket BuildUnsetAdvertisingData(const DeviceAddress& address,
84                                           bool extended_pdu) override;
85 
86   std::vector<CommandPacket> BuildSetScanResponse(
87       const DeviceAddress& address,
88       const AdvertisingData& scan_rsp,
89       bool extended_pdu) override;
90 
91   CommandPacket BuildUnsetScanResponse(const DeviceAddress& address,
92                                        bool extended_pdu) override;
93 
94   CommandPacket BuildRemoveAdvertisingSet(const DeviceAddress& address,
95                                           bool extended_pdu) override;
96 
97   // |starting_| is set to true if a start is pending.
98   // |staged_params_| are the parameters that will be advertised.
99   struct StagedParams {
100     DeviceAddress address;
101     AdvertisingData data;
102     AdvertisingData scan_rsp;
103     AdvertisingOptions options;
104     ConnectionCallback connect_callback;
105     ResultFunction<> result_callback;
106   };
107   std::optional<StagedParams> staged_params_;
108   bool starting_ = false;
109   DeviceAddress local_address_;
110 
111   BT_DISALLOW_COPY_AND_ASSIGN_ALLOW_MOVE(LegacyLowEnergyAdvertiser);
112 };
113 
114 }  // namespace bt::hci
115