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