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 "EmulatedUserHal.h"
33 #include "EmulatedVehicleConnector.h"
34 #include "GeneratorHub.h"
35 #include "PropertyUtils.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     void getAllPropertiesOverride();
67 
68 private:
hertzToNanoseconds(float hz)69     constexpr std::chrono::nanoseconds hertzToNanoseconds(float hz) const {
70         return std::chrono::nanoseconds(static_cast<int64_t>(1000000000L / hz));
71     }
72 
73     StatusCode handleGenerateFakeDataRequest(const VehiclePropValue& request);
74     void onPropertyValue(const VehiclePropValue& value, bool updateStatus);
75 
76     void onContinuousPropertyTimer(const std::vector<int32_t>& properties);
77     bool isContinuousProperty(int32_t propId) const;
78     void initStaticConfig();
79     void initObd2LiveFrame(const VehiclePropConfig& propConfig);
80     void initObd2FreezeFrame(const VehiclePropConfig& propConfig);
81     StatusCode fillObd2FreezeFrame(const VehiclePropValue& requestedPropValue,
82                                    VehiclePropValue* outValue);
83     StatusCode fillObd2DtcInfo(VehiclePropValue* outValue);
84     StatusCode clearObd2FreezeFrames(const VehiclePropValue& propValue);
85     VehicleHal::VehiclePropValuePtr doInternalHealthCheck();
86     VehicleHal::VehiclePropValuePtr createVhalHeartBeatProp();
87 
88     /* Private members */
89     VehiclePropertyStore* mPropStore;
90     std::unordered_set<int32_t> mHvacPowerProps;
91     RecurrentTimer mRecurrentTimer;
92     VehicleHalClient* mVehicleClient;
93     bool mInEmulator;
94     bool mInitVhalValueOverride;
95     std::vector<VehiclePropValue> mVehiclePropertiesOverride;
96     EmulatedUserHal* mEmulatedUserHal;
97 };
98 
99 }  // impl
100 
101 }  // namespace V2_0
102 }  // namespace vehicle
103 }  // namespace automotive
104 }  // namespace hardware
105 }  // namespace android
106 
107 
108 #endif  // android_hardware_automotive_vehicle_V2_0_impl_EmulatedVehicleHal_H_
109