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 <vhal_v2_0/RecurrentTimer.h> 29 #include <vhal_v2_0/VehicleHal.h> 30 #include "vhal_v2_0/VehiclePropertyStore.h" 31 32 #include "DefaultConfig.h" 33 #include "EmulatedUserHal.h" 34 #include "EmulatedVehicleConnector.h" 35 #include "GeneratorHub.h" 36 #include "VehicleEmulator.h" 37 38 namespace android { 39 namespace hardware { 40 namespace automotive { 41 namespace vehicle { 42 namespace V2_0 { 43 44 namespace impl { 45 46 /** Implementation of VehicleHal that connected to emulator instead of real vehicle network. */ 47 class EmulatedVehicleHal : public EmulatedVehicleHalIface { 48 public: 49 EmulatedVehicleHal(VehiclePropertyStore* propStore, VehicleHalClient* client, 50 EmulatedUserHal* emulatedUserHal = nullptr); 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, float sampleRate) override; 60 StatusCode unsubscribe(int32_t property) override; 61 bool dump(const hidl_handle& fd, const hidl_vec<hidl_string>& options) override; 62 63 // Methods from EmulatedVehicleHalIface 64 bool setPropertyFromVehicle(const VehiclePropValue& propValue) override; 65 std::vector<VehiclePropValue> getAllProperties() const override; 66 67 private: hertzToNanoseconds(float hz)68 constexpr std::chrono::nanoseconds hertzToNanoseconds(float hz) const { 69 return std::chrono::nanoseconds(static_cast<int64_t>(1000000000L / hz)); 70 } 71 72 StatusCode handleGenerateFakeDataRequest(const VehiclePropValue& request); 73 void onPropertyValue(const VehiclePropValue& value, bool updateStatus); 74 75 void onContinuousPropertyTimer(const std::vector<int32_t>& properties); 76 bool isContinuousProperty(int32_t propId) const; 77 void initStaticConfig(); 78 void initObd2LiveFrame(const VehiclePropConfig& propConfig); 79 void initObd2FreezeFrame(const VehiclePropConfig& propConfig); 80 StatusCode fillObd2FreezeFrame(const VehiclePropValue& requestedPropValue, 81 VehiclePropValue* outValue); 82 StatusCode fillObd2DtcInfo(VehiclePropValue* outValue); 83 StatusCode clearObd2FreezeFrames(const VehiclePropValue& propValue); 84 85 /* Private members */ 86 VehiclePropertyStore* mPropStore; 87 std::unordered_set<int32_t> mHvacPowerProps; 88 RecurrentTimer mRecurrentTimer; 89 VehicleHalClient* mVehicleClient; 90 EmulatedUserHal* mEmulatedUserHal; 91 }; 92 93 } // impl 94 95 } // namespace V2_0 96 } // namespace vehicle 97 } // namespace automotive 98 } // namespace hardware 99 } // namespace android 100 101 102 #endif // android_hardware_automotive_vehicle_V2_0_impl_EmulatedVehicleHal_H_ 103