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 "VehicleEmulator.h" 34 #include "FakeValueGenerator.h" 35 36 namespace android { 37 namespace hardware { 38 namespace automotive { 39 namespace vehicle { 40 namespace V2_0 { 41 42 namespace impl { 43 44 /** Implementation of VehicleHal that connected to emulator instead of real vehicle network. */ 45 class EmulatedVehicleHal : public EmulatedVehicleHalIface { 46 public: 47 EmulatedVehicleHal(VehiclePropertyStore* propStore); 48 ~EmulatedVehicleHal() = default; 49 50 // Methods from VehicleHal 51 void onCreate() override; 52 std::vector<VehiclePropConfig> listProperties() override; 53 VehiclePropValuePtr get(const VehiclePropValue& requestedPropValue, 54 StatusCode* outStatus) override; 55 StatusCode set(const VehiclePropValue& propValue) override; 56 StatusCode subscribe(int32_t property, int32_t areas, float sampleRate) override; 57 StatusCode unsubscribe(int32_t property) override; 58 59 // Methods from EmulatedVehicleHalIface 60 bool setPropertyFromVehicle(const VehiclePropValue& propValue) override; 61 std::vector<VehiclePropValue> getAllProperties() const override; 62 63 private: hertzToNanoseconds(float hz)64 constexpr std::chrono::nanoseconds hertzToNanoseconds(float hz) const { 65 return std::chrono::nanoseconds(static_cast<int64_t>(1000000000L / hz)); 66 } 67 68 StatusCode handleGenerateFakeDataRequest(const VehiclePropValue& request); 69 void onFakeValueGenerated(int32_t propId, float value); 70 71 void onContinuousPropertyTimer(const std::vector<int32_t>& properties); 72 bool isContinuousProperty(int32_t propId) const; 73 void initStaticConfig(); 74 void initObd2LiveFrame(const VehiclePropConfig& propConfig); 75 void initObd2FreezeFrame(const VehiclePropConfig& propConfig); 76 StatusCode fillObd2FreezeFrame(const VehiclePropValue& requestedPropValue, 77 VehiclePropValue* outValue); 78 StatusCode fillObd2DtcInfo(VehiclePropValue* outValue); 79 StatusCode clearObd2FreezeFrames(const VehiclePropValue& propValue); 80 81 /* Private members */ 82 VehiclePropertyStore* mPropStore; 83 std::unordered_set<int32_t> mHvacPowerProps; 84 RecurrentTimer mRecurrentTimer; 85 FakeValueGenerator mFakeValueGenerator; 86 }; 87 88 } // impl 89 90 } // namespace V2_0 91 } // namespace vehicle 92 } // namespace automotive 93 } // namespace hardware 94 } // namespace android 95 96 97 #endif // android_hardware_automotive_vehicle_V2_0_impl_EmulatedVehicleHal_H_ 98