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 "GeneratorHub.h" 34 #include "VehicleEmulator.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, 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(const VehiclePropValue& value); 70 VehiclePropValuePtr createApPowerStateReq(VehicleApPowerStateReq req, int32_t param); 71 VehiclePropValuePtr createHwInputKeyProp(VehicleHwKeyInputAction action, int32_t keyCode, 72 int32_t targetDisplay); 73 74 void onContinuousPropertyTimer(const std::vector<int32_t>& properties); 75 bool isContinuousProperty(int32_t propId) const; 76 void initStaticConfig(); 77 void initObd2LiveFrame(const VehiclePropConfig& propConfig); 78 void initObd2FreezeFrame(const VehiclePropConfig& propConfig); 79 StatusCode fillObd2FreezeFrame(const VehiclePropValue& requestedPropValue, 80 VehiclePropValue* outValue); 81 StatusCode fillObd2DtcInfo(VehiclePropValue* outValue); 82 StatusCode clearObd2FreezeFrames(const VehiclePropValue& propValue); 83 84 /* Private members */ 85 VehiclePropertyStore* mPropStore; 86 std::unordered_set<int32_t> mHvacPowerProps; 87 RecurrentTimer mRecurrentTimer; 88 GeneratorHub mGeneratorHub; 89 }; 90 91 } // impl 92 93 } // namespace V2_0 94 } // namespace vehicle 95 } // namespace automotive 96 } // namespace hardware 97 } // namespace android 98 99 100 #endif // android_hardware_automotive_vehicle_V2_0_impl_EmulatedVehicleHal_H_ 101