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_GeneratorHub_H_ 18 #define android_hardware_automotive_vehicle_V2_0_impl_GeneratorHub_H_ 19 20 #include <atomic> 21 #include <chrono> 22 #include <condition_variable> 23 #include <iostream> 24 #include <queue> 25 #include <thread> 26 #include <unordered_map> 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 /** 39 * This is the scheduler for all VHAL event generators. It manages all generators and uses priority 40 * queue to maintain generated events ordered by timestamp. The scheduler uses a single thread to 41 * keep querying and updating the event queue to make sure events from all generators are produced 42 * in order. 43 */ 44 class GeneratorHub { 45 private: 46 struct VhalEvent { 47 int32_t cookie; // Cookie is used to find the associated generator. 48 VehiclePropValue val; 49 }; 50 // Comparator used by priority queue to keep track of soonest event. 51 struct GreaterByTime { operatorGreaterByTime52 bool operator()(const VhalEvent& lhs, const VhalEvent& rhs) const { 53 return lhs.val.timestamp > rhs.val.timestamp; 54 } 55 }; 56 57 using OnHalEvent = std::function<void(const VehiclePropValue& event)>; 58 59 public: 60 GeneratorHub(const OnHalEvent& onHalEvent); 61 ~GeneratorHub() = default; 62 63 /** 64 * Register a new generator. The generator will be discarded if it could not produce next event. 65 * The existing generator will be overridden if it has the same cookie. 66 */ 67 void registerGenerator(int32_t cookie, FakeValueGeneratorPtr generator); 68 69 void unregisterGenerator(int32_t cookie); 70 71 private: 72 /** 73 * Main loop of the single thread to producing event and updating event queue. 74 */ 75 void run(); 76 77 bool hasNext(int32_t cookie); 78 79 private: 80 std::priority_queue<VhalEvent, std::vector<VhalEvent>, GreaterByTime> mEventQueue; 81 std::unordered_map<int32_t, FakeValueGeneratorPtr> mGenerators; 82 OnHalEvent mOnHalEvent; 83 84 mutable std::mutex mLock; 85 std::condition_variable mCond; 86 std::thread mThread; 87 }; 88 89 } // namespace impl 90 91 } // namespace V2_0 92 } // namespace vehicle 93 } // namespace automotive 94 } // namespace hardware 95 } // namespace android 96 97 #endif // android_hardware_automotive_vehicle_V2_0_impl_GeneratorHub_H_ 98