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 #include <vhal_v2_0/VehicleServer.h> 27 28 #include "CommConn.h" 29 #include "PipeComm.h" 30 #include "SocketComm.h" 31 #include "VehicleHalProto.pb.h" 32 33 namespace android { 34 namespace hardware { 35 namespace automotive { 36 namespace vehicle { 37 namespace V2_0 { 38 39 namespace impl { 40 41 class VehicleEmulator; // Forward declaration. 42 43 /** An interface used by VehicleEmulator. */ 44 class EmulatedServerIface { 45 public: 46 using VehiclePropValuePtr = VehicleHal::VehiclePropValuePtr; 47 virtual ~EmulatedServerIface() = default; 48 virtual bool setPropertyFromVehicle(const VehiclePropValue& propValue) = 0; 49 virtual std::vector<VehiclePropValue> getAllProperties() const = 0; 50 virtual std::vector<VehiclePropConfig> listProperties() = 0; 51 virtual VehiclePropValuePtr get(const VehiclePropValue& requestedPropValue, 52 StatusCode* outStatus) = 0; 53 virtual IVehicleServer::DumpResult debug(const std::vector<std::string>& options) = 0; 54 registerEmulator(VehicleEmulator * emulator)55 void registerEmulator(VehicleEmulator* emulator) { 56 ALOGI("%s, emulator: %p", __func__, emulator); 57 std::lock_guard<std::mutex> g(mEmulatorLock); 58 mEmulator = emulator; 59 } 60 61 protected: getEmulatorOrDie()62 VehicleEmulator* getEmulatorOrDie() { 63 std::lock_guard<std::mutex> g(mEmulatorLock); 64 if (mEmulator == nullptr) abort(); 65 return mEmulator; 66 } 67 68 private: 69 mutable std::mutex mEmulatorLock; 70 VehicleEmulator* mEmulator; 71 }; 72 73 /** 74 * Emulates vehicle by providing controlling interface from host side either through ADB or Pipe. 75 */ 76 class VehicleEmulator : public MessageProcessor { 77 public: 78 VehicleEmulator(EmulatedServerIface* hal); 79 virtual ~VehicleEmulator(); 80 81 void doSetValueFromClient(const VehiclePropValue& propValue); 82 void processMessage(const vhal_proto::EmulatorMessage& rxMsg, 83 vhal_proto::EmulatorMessage* respMsg) override; 84 85 private: 86 friend class ConnectionThread; 87 using EmulatorMessage = vhal_proto::EmulatorMessage; 88 89 void doGetConfig(const EmulatorMessage& rxMsg, EmulatorMessage* respMsg); 90 void doGetConfigAll(const EmulatorMessage& rxMsg, EmulatorMessage* respMsg); 91 void doGetProperty(const EmulatorMessage& rxMsg, EmulatorMessage* respMsg); 92 void doGetPropertyAll(const EmulatorMessage& rxMsg, EmulatorMessage* respMsg); 93 void doSetProperty(const EmulatorMessage& rxMsg, EmulatorMessage* respMsg); 94 void doDebug(const EmulatorMessage& rxMsg, EmulatorMessage* respMsg); 95 void populateProtoVehicleConfig(vhal_proto::VehiclePropConfig* protoCfg, 96 const VehiclePropConfig& cfg); 97 void populateProtoVehiclePropValue(vhal_proto::VehiclePropValue* protoVal, 98 const VehiclePropValue* val); 99 100 private: 101 EmulatedServerIface* mHal; 102 std::unique_ptr<SocketComm> mSocketComm; 103 std::unique_ptr<PipeComm> mPipeComm; 104 }; 105 106 // determine if it's running inside Android Emulator 107 bool isInQEMU(); 108 109 } // impl 110 111 } // namespace V2_0 112 } // namespace vehicle 113 } // namespace automotive 114 } // namespace hardware 115 } // namespace android 116 117 #endif // android_hardware_automotive_vehicle_V2_0_impl_VehicleHalEmulator_H_ 118