1 /* 2 * Copyright (C) 2018 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 #ifndef android_hardware_automotive_vehicle_V2_0_impl_JsonFakeValueGenerator_H_ 18 #define android_hardware_automotive_vehicle_V2_0_impl_JsonFakeValueGenerator_H_ 19 20 #include <atomic> 21 #include <chrono> 22 #include <condition_variable> 23 #include <iostream> 24 #include <thread> 25 26 #include <json/json.h> 27 28 #include "FakeValueGenerator.h" 29 30 namespace android { 31 namespace hardware { 32 namespace automotive { 33 namespace vehicle { 34 namespace V2_0 { 35 36 namespace impl { 37 38 class JsonFakeValueGenerator : public FakeValueGenerator { 39 private: 40 using Nanos = std::chrono::nanoseconds; 41 using Clock = std::chrono::steady_clock; 42 using TimePoint = std::chrono::time_point<Clock, Nanos>; 43 44 struct GeneratorCfg { 45 size_t index; 46 std::vector<VehiclePropValue> events; 47 }; 48 49 public: 50 JsonFakeValueGenerator(const OnHalEvent& onHalEvent); 51 ~JsonFakeValueGenerator(); 52 StatusCode start(const VehiclePropValue& request) override; 53 StatusCode stop(const VehiclePropValue& request) override; 54 55 private: 56 std::vector<VehiclePropValue> parseFakeValueJson(std::istream& is); 57 void loop(); 58 59 private: 60 OnHalEvent mOnHalEvent; 61 std::thread mThread; 62 mutable std::mutex mLock; 63 std::condition_variable mCond; 64 GeneratorCfg mGenCfg; 65 std::atomic_bool mStopRequested{false}; 66 }; 67 68 } // namespace impl 69 70 } // namespace V2_0 71 } // namespace vehicle 72 } // namespace automotive 73 } // namespace hardware 74 } // namespace android 75 76 #endif // android_hardware_automotive_vehicle_V2_0_impl_JsonFakeValueGenerator_H_ 77