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 "FakeValueGenerator.h"
34
35#include "VehicleEmulator.h"
36
37namespace android {
38namespace hardware {
39namespace automotive {
40namespace vehicle {
41namespace V2_0 {
42
43namespace impl {
44
45/** Implementation of VehicleHal that connected to emulator instead of real vehicle network. */
46class EmulatedVehicleHal : public EmulatedVehicleHalIface {
47public:
48    EmulatedVehicleHal(VehiclePropertyStore* propStore);
49    ~EmulatedVehicleHal() = default;
50
51    //  Methods from VehicleHal
52    void onCreate() override;
53    std::vector<VehiclePropConfig> listProperties() override;
54    VehiclePropValuePtr get(const VehiclePropValue& requestedPropValue,
55                            StatusCode* outStatus) override;
56    StatusCode set(const VehiclePropValue& propValue) override;
57    StatusCode subscribe(int32_t property, float sampleRate) override;
58    StatusCode unsubscribe(int32_t property) override;
59
60    //  Methods from EmulatedVehicleHalIface
61    bool setPropertyFromVehicle(const VehiclePropValue& propValue) override;
62    std::vector<VehiclePropValue> getAllProperties() const override;
63
64private:
65    constexpr std::chrono::nanoseconds hertzToNanoseconds(float hz) const {
66        return std::chrono::nanoseconds(static_cast<int64_t>(1000000000L / hz));
67    }
68
69    StatusCode handleGenerateFakeDataRequest(const VehiclePropValue& request);
70    void onFakeValueGenerated(const VehiclePropValue& value);
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    std::unique_ptr<FakeValueGenerator> mLinearFakeValueGenerator;
89    std::unique_ptr<FakeValueGenerator> mJsonFakeValueGenerator;
90};
91
92}  // impl
93
94}  // namespace V2_0
95}  // namespace vehicle
96}  // namespace automotive
97}  // namespace hardware
98}  // namespace android
99
100
101#endif  // android_hardware_automotive_vehicle_V2_0_impl_EmulatedVehicleHal_H_
102