• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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_VehicleHalEmulator_H_
18 #define android_hardware_automotive_vehicle_V2_0_impl_VehicleHalEmulator_H_
19 
20 #include <log/log.h>
21 #include <memory>
22 #include <thread>
23 #include <vector>
24 
25 #include "vhal_v2_0/VehicleHal.h"
26 
27 #include "CommBase.h"
28 #include "VehicleHalProto.pb.h"
29 
30 namespace android {
31 namespace hardware {
32 namespace automotive {
33 namespace vehicle {
34 namespace V2_0 {
35 
36 namespace impl {
37 
38 class VehicleEmulator;  // Forward declaration.
39 
40 /** Extension of VehicleHal that used by VehicleEmulator. */
41 class EmulatedVehicleHalIface : public VehicleHal {
42 public:
43     virtual bool setPropertyFromVehicle(const VehiclePropValue& propValue) = 0;
44     virtual std::vector<VehiclePropValue> getAllProperties() const = 0;
45 
registerEmulator(VehicleEmulator * emulator)46     void registerEmulator(VehicleEmulator* emulator) {
47         ALOGI("%s, emulator: %p", __func__, emulator);
48         std::lock_guard<std::mutex> g(mEmulatorLock);
49         mEmulator = emulator;
50     }
51 
52 protected:
getEmulatorOrDie()53     VehicleEmulator* getEmulatorOrDie() {
54         std::lock_guard<std::mutex> g(mEmulatorLock);
55         if (mEmulator == nullptr) abort();
56         return mEmulator;
57     }
58 
59 private:
60     mutable std::mutex mEmulatorLock;
61     VehicleEmulator* mEmulator;
62 };
63 
64 struct CommFactory {
65     static std::unique_ptr<CommBase> create();
66 };
67 
68 /**
69  * Emulates vehicle by providing controlling interface from host side either through ADB or Pipe.
70  */
71 class VehicleEmulator {
72 public:
73     VehicleEmulator(EmulatedVehicleHalIface* hal,
74                     std::unique_ptr<CommBase> comm = CommFactory::create())
75             : mHal { hal },
76               mComm(comm.release()),
77               mThread { &VehicleEmulator::rxThread, this} {
78         mHal->registerEmulator(this);
79     }
80     virtual ~VehicleEmulator();
81 
82     void doSetValueFromClient(const VehiclePropValue& propValue);
83 
84 private:
85     using EmulatorMessage = emulator::EmulatorMessage;
86 
87     void doGetConfig(EmulatorMessage& rxMsg, EmulatorMessage& respMsg);
88     void doGetConfigAll(EmulatorMessage& rxMsg, EmulatorMessage& respMsg);
89     void doGetProperty(EmulatorMessage& rxMsg, EmulatorMessage& respMsg);
90     void doGetPropertyAll(EmulatorMessage& rxMsg, EmulatorMessage& respMsg);
91     void doSetProperty(EmulatorMessage& rxMsg, EmulatorMessage& respMsg);
92     void txMsg(emulator::EmulatorMessage& txMsg);
93     void parseRxProtoBuf(std::vector<uint8_t>& msg);
94     void populateProtoVehicleConfig(emulator::VehiclePropConfig* protoCfg,
95                                     const VehiclePropConfig& cfg);
96     void populateProtoVehiclePropValue(emulator::VehiclePropValue* protoVal,
97                                        const VehiclePropValue* val);
98     void rxMsg();
99     void rxThread();
100 
101 private:
102     std::atomic<bool> mExit { false };
103     EmulatedVehicleHalIface* mHal;
104     std::unique_ptr<CommBase> mComm;
105     std::thread mThread;
106 };
107 
108 }  // impl
109 
110 }  // namespace V2_0
111 }  // namespace vehicle
112 }  // namespace automotive
113 }  // namespace hardware
114 }  // namespace android
115 
116 #endif // android_hardware_automotive_vehicle_V2_0_impl_VehicleHalEmulator_H_
117