1 /*
2  * Copyright 2020 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 
17 #pragma once
18 
19 #include <chrono>
20 #include <cstdint>
21 #include <memory>
22 
23 #include "hci/address_with_type.h"
24 #include "hci/hci_packets.h"
25 #include "packets/link_layer_packets.h"
26 
27 namespace test_vendor_lib {
28 
29 // Track a single advertising instance
30 class LeAdvertiser {
31  public:
32   LeAdvertiser() = default;
33   virtual ~LeAdvertiser() = default;
34 
35   void Initialize(bluetooth::hci::AddressWithType address,
36                   bluetooth::hci::AddressWithType peer_address,
37                   bluetooth::hci::LeScanningFilterPolicy filter_policy,
38                   model::packets::AdvertisementType type,
39                   const std::vector<uint8_t>& advertisement,
40                   const std::vector<uint8_t>& scan_response,
41                   std::chrono::steady_clock::duration interval);
42 
43   void InitializeExtended(bluetooth::hci::AddressType address_type,
44                           bluetooth::hci::AddressWithType peer_address,
45                           bluetooth::hci::LeScanningFilterPolicy filter_policy,
46                           model::packets::AdvertisementType type,
47                           std::chrono::steady_clock::duration interval);
48 
49   void SetAddress(bluetooth::hci::Address address);
50 
51   void SetData(const std::vector<uint8_t>& data);
52 
53   std::unique_ptr<model::packets::LeAdvertisementBuilder> GetAdvertisement(
54       std::chrono::steady_clock::time_point);
55 
56   std::unique_ptr<model::packets::LeScanResponseBuilder> GetScanResponse(
57       bluetooth::hci::Address scanned_address,
58       bluetooth::hci::Address scanner_address);
59 
60   void Clear();
61 
62   void Disable();
63 
64   void Enable();
65 
66   void EnableExtended(std::chrono::steady_clock::duration duration);
67 
68   bool IsEnabled() const;
69 
70   bluetooth::hci::AddressWithType GetAddress() const;
71 
72  private:
73   bluetooth::hci::AddressWithType address_{};
74   bluetooth::hci::AddressWithType
75       peer_address_{};  // For directed advertisements
76   bluetooth::hci::LeScanningFilterPolicy filter_policy_{};
77   model::packets::AdvertisementType type_{};
78   std::vector<uint8_t> advertisement_;
79   std::vector<uint8_t> scan_response_;
80   std::chrono::steady_clock::duration interval_{};
81   std::chrono::steady_clock::time_point ending_time_{};
82   bool enabled_{false};
83   std::chrono::steady_clock::time_point last_le_advertisement_;
84 };
85 
86 }  // namespace test_vendor_lib
87