1 /*
2  * Copyright 2016 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 <array>
20 #include <chrono>
21 #include <cstdint>
22 #include <memory>
23 #include <string>
24 #include <vector>
25 
26 #include "model/devices/device.h"
27 #include "packets/link_layer_packets.h"
28 #include "phy.h"
29 
30 namespace rootcanal {
31 
32 // Simple device that advertises with non-connectable advertising in general
33 // discoverable mode, and responds to LE scan requests.
34 class Beacon : public Device {
35  public:
36   Beacon();
37   Beacon(const std::vector<std::string>& args);
38   virtual ~Beacon() = default;
39 
Create(const std::vector<std::string> & args)40   static std::shared_ptr<Device> Create(const std::vector<std::string>& args) {
41     return std::make_shared<Beacon>(args);
42   }
43 
GetTypeString()44   virtual std::string GetTypeString() const override { return "beacon"; }
45 
46   virtual void Tick() override;
47   virtual void ReceiveLinkLayerPacket(
48       model::packets::LinkLayerPacketView packet, Phy::Type type,
49       int8_t rssi) override;
50 
51  protected:
52   model::packets::LegacyAdvertisingType advertising_type_{};
53   std::array<uint8_t, 31> advertising_data_{};
54   std::array<uint8_t, 31> scan_response_data_{};
55   std::chrono::steady_clock::duration advertising_interval_{};
56   std::chrono::steady_clock::time_point advertising_last_{};
57 
58  private:
59   static bool registered_;
60 };
61 
62 }  // namespace rootcanal
63