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