1 /*
2  * Copyright (C) 2022 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_VEHICLEEMULATOR_EMULATEDVEHICLEHARDWARE_H
18 #define ANDROID_VEHICLEEMULATOR_EMULATEDVEHICLEHARDWARE_H
19 
20 #include <aidl/device/generic/car/emulator/IVehicleBus.h>
21 #include <aidl/device/generic/car/emulator/BnVehicleBusCallback.h>
22 
23 #include <CommConn.h>
24 #include <FakeVehicleHardware.h>
25 #include <VehicleUtils.h>
26 
27 namespace android {
28 namespace hardware {
29 namespace automotive {
30 namespace vehicle {
31 namespace fake {
32 
33 class VehicleEmulator;
34 class VehicleEmulatorTest;
35 
36 class EmulatedVehicleHardware : public FakeVehicleHardware {
37   public:
38     using AidlVehiclePropValue = aidl::android::hardware::automotive::vehicle::VehiclePropValue;
39     using IVehicleBus = aidl::device::generic::car::emulator::IVehicleBus;
40     using BnVehicleBusCallback = aidl::device::generic::car::emulator::BnVehicleBusCallback;
41     using ConfigResultType = android::base::Result<aidl::android::hardware::automotive::vehicle::VehiclePropConfig, VhalError>;
42 
43     EmulatedVehicleHardware();
44     EmulatedVehicleHardware(std::string_view default_config_dir,
45                             std::string_view override_config_dir,
46                             bool force_override);
47 
48     ~EmulatedVehicleHardware();
49 
50     aidl::android::hardware::automotive::vehicle::StatusCode setValues(
51             std::shared_ptr<const SetValuesCallback> callback,
52             const std::vector<aidl::android::hardware::automotive::vehicle::SetValueRequest>&
53                     requests) override;
54 
55     std::vector<VehiclePropValuePool::RecyclableType> getAllProperties() const;
56 
57     ConfigResultType getPropConfig(int32_t propId) const;
58 
59   private:
60     friend class VehicleEmulator;
61     friend class VehicleEmulatorTest;
62 
63     bool mInQemu;
64 
65     class VehicleBusCallback : public BnVehicleBusCallback {
66       public:
VehicleBusCallback(EmulatedVehicleHardware * vehicleHardware)67         VehicleBusCallback(
68             EmulatedVehicleHardware* vehicleHardware) :
69             mVehicleHardware(vehicleHardware) {}
70 
71         ndk::ScopedAStatus onNewPropValues(
72             const std::vector<AidlVehiclePropValue>& propValues) override;
73 
74       private:
75         EmulatedVehicleHardware* mVehicleHardware;
76     };
77     std::shared_ptr<BnVehicleBusCallback> mVehicleBusCallback;
78     std::vector<std::shared_ptr<IVehicleBus>> mVehicleBuses;
79     std::unique_ptr<VehicleEmulator> mEmulator;
80 
81     // determine if it's running inside Android Emulator
82     static bool isInQemu();
83     void Init();
84     void startVehicleBuses();
85     void stopVehicleBuses();
86 
87     // For testing only.
88     EmulatedVehicleHardware(
89         bool inQEMU,
90         std::unique_ptr<V2_0::impl::MessageSender> socketComm,
91         std::unique_ptr<V2_0::impl::MessageSender> pipeComm);
92 
93     // For testing only.
94     VehicleEmulator* getEmulator();
95 };
96 
97 }  // namespace fake
98 }  // namespace vehicle
99 }  // namespace automotive
100 }  // namespace hardware
101 }  // namespace android
102 
103 #endif  // ANDROID_VEHICLEEMULATOR_EMULATEDVEHICLEHARDWARE_H
104