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