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 <cstdint> 20 #include <fstream> 21 #include <vector> 22 23 #include "model/devices/beacon.h" 24 #include "model/devices/scripted_beacon_ble_payload.pb.h" 25 26 using android::bluetooth::rootcanal::model::devices::ScriptedBeaconBleAdProto::PlaybackEvent; 27 28 namespace rootcanal { 29 // Pretend to be a lot of beacons by advertising from a file. 30 class ScriptedBeacon : public Beacon { 31 public: 32 ScriptedBeacon(const std::vector<std::string>& args); 33 virtual ~ScriptedBeacon() = default; 34 Create(const std::vector<std::string> & args)35 static std::shared_ptr<Device> Create(const std::vector<std::string>& args) { 36 return std::make_shared<ScriptedBeacon>(args); 37 } 38 39 // Return a string representation of the type of device. GetTypeString()40 virtual std::string GetTypeString() const override { return "scripted_beacon"; } 41 ToString()42 virtual std::string ToString() const override { return "scripted_beacon " + config_file_; } 43 44 void Tick() override; 45 void ReceiveLinkLayerPacket(model::packets::LinkLayerPacketView packet_view, Phy::Type type, 46 int8_t rssi) override; 47 48 private: 49 static bool registered_; 50 std::string config_file_{}; 51 std::string events_file_{}; 52 std::ofstream events_ostream_; 53 struct Advertisement { 54 std::vector<uint8_t> ad; 55 Address address; 56 std::chrono::steady_clock::time_point ad_time; 57 }; 58 59 void get_next_advertisement(); 60 61 void set_state(PlaybackEvent::PlaybackEventType state); 62 63 Advertisement next_ad_{}; 64 int packet_num_{0}; 65 PlaybackEvent::PlaybackEventType current_state_{PlaybackEvent::UNKNOWN}; 66 std::chrono::steady_clock::time_point next_check_time_{}; 67 android::bluetooth::rootcanal::model::devices::ScriptedBeaconBleAdProto::BleAdvertisementList 68 ble_ad_list_; 69 }; 70 } // namespace rootcanal 71