1 /* 2 * Copyright (C) 2016 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_EmulatedVehicleHal_H_ 18 #define android_hardware_automotive_vehicle_V2_0_impl_EmulatedVehicleHal_H_ 19 20 #include <map> 21 #include <memory> 22 #include <sys/socket.h> 23 #include <thread> 24 #include <unordered_set> 25 26 #include <utils/SystemClock.h> 27 28 #include "VehicleHalProto.pb.h" 29 30 #include <vhal_v2_0/RecurrentTimer.h> 31 #include <vhal_v2_0/VehicleHal.h> 32 #include "vhal_v2_0/VehiclePropertyStore.h" 33 34 #include "DefaultConfig.h" 35 #include "VehicleHalProto.pb.h" 36 #include "VehicleEmulator.h" 37 #include "FakeValueGenerator.h" 38 39 namespace android { 40 namespace hardware { 41 namespace automotive { 42 namespace vehicle { 43 namespace V2_0 { 44 45 namespace impl { 46 47 /** Implementation of VehicleHal that connected to emulator instead of real vehicle network. */ 48 class EmulatedVehicleHal : public EmulatedVehicleHalIface { 49 public: 50 EmulatedVehicleHal(VehiclePropertyStore* propStore); 51 ~EmulatedVehicleHal() = default; 52 53 // Methods from VehicleHal 54 void onCreate() override; 55 std::vector<VehiclePropConfig> listProperties() override; 56 VehiclePropValuePtr get(const VehiclePropValue& requestedPropValue, 57 StatusCode* outStatus) override; 58 StatusCode set(const VehiclePropValue& propValue) override; 59 StatusCode subscribe(int32_t property, int32_t areas, float sampleRate) override; 60 StatusCode unsubscribe(int32_t property) override; 61 62 // Methods from EmulatedVehicleHalIface 63 bool setPropertyFromVehicle(const VehiclePropValue& propValue) override; 64 std::vector<VehiclePropValue> getAllProperties() const override; 65 66 private: 67 constexpr std::chrono::nanoseconds hertzToNanoseconds(float hz) const { 68 return std::chrono::nanoseconds(static_cast<int64_t>(1000000000L / hz)); 69 } 70 71 StatusCode handleGenerateFakeDataRequest(const VehiclePropValue& request); 72 void onFakeValueGenerated(int32_t propId, float value); 73 74 void onContinuousPropertyTimer(const std::vector<int32_t>& properties); 75 bool isContinuousProperty(int32_t propId) const; 76 77 private: 78 VehiclePropertyStore* mPropStore; 79 std::unordered_set<int32_t> mHvacPowerProps; 80 RecurrentTimer mRecurrentTimer; 81 FakeValueGenerator mFakeValueGenerator; 82 }; 83 84 } // impl 85 86 } // namespace V2_0 87 } // namespace vehicle 88 } // namespace automotive 89 } // namespace hardware 90 } // namespace android 91 92 93 #endif // android_hardware_automotive_vehicle_V2_0_impl_EmulatedVehicleHal_H_ 94