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_EMULATORCOMMCONN_COMMCONN_H 18 #define ANDROID_EMULATORCOMMCONN_COMMCONN_H 19 20 #include <string> 21 #include <thread> 22 #include <vector> 23 24 #include "VehicleHalProto.pb.h" 25 26 namespace android { 27 namespace hardware { 28 namespace automotive { 29 namespace vehicle { 30 namespace V2_0 { 31 32 namespace impl { 33 34 /** 35 * MessageProcess is an interface implemented by VehicleEmulator to process messages received 36 * over a CommConn. 37 */ 38 class MessageProcessor { 39 public: 40 virtual ~MessageProcessor() = default; 41 42 /** 43 * Process a single message received over a CommConn. Populate the given respMsg with the reply 44 * message we should send. 45 */ 46 virtual void processMessage(const vhal_proto::EmulatorMessage& rxMsg, 47 vhal_proto::EmulatorMessage* respMsg) = 0; 48 }; 49 50 /** 51 * This is a pure virtual interface that could start/stop and send message. 52 * This is implemented by CommConn and SocketComm. 53 */ 54 class MessageSender { 55 public: ~MessageSender()56 virtual ~MessageSender() {} 57 58 /** 59 * Starts the read thread reading messages from this connection. 60 */ 61 virtual void start() = 0; 62 63 /** 64 * Closes a connection if it is open. 65 */ 66 virtual void stop() = 0; 67 68 /** 69 * Serializes and sends the given message to the other side. 70 */ 71 virtual void sendMessage(const vhal_proto::EmulatorMessage& msg) = 0; 72 }; 73 74 /** 75 * This is the interface that both PipeComm and SocketComm use to represent a connection. The 76 * connection will listen for commands on a separate 'read' thread. 77 */ 78 class CommConn : public MessageSender { 79 public: CommConn(MessageProcessor * messageProcessor)80 CommConn(MessageProcessor* messageProcessor) : mMessageProcessor(messageProcessor) {} 81 ~CommConn()82 virtual ~CommConn() {} 83 84 /** 85 * Start the read thread reading messages from this connection. 86 */ 87 void start() override; 88 89 /** 90 * Closes a connection if it is open. 91 */ 92 void stop() override; 93 94 /** 95 * Returns true if the connection is open and available to send/receive. 96 */ 97 virtual bool isOpen() = 0; 98 99 /** 100 * Serialized and send the given message to the other side. 101 */ 102 void sendMessage(const vhal_proto::EmulatorMessage& msg) final; 103 104 protected: 105 MessageProcessor* mMessageProcessor; 106 107 private: 108 std::unique_ptr<std::thread> mReadThread; 109 std::mutex mSendMessageLock; 110 111 /** 112 * A thread that reads messages in a loop, and responds. You can stop this thread by calling 113 * stop(). 114 */ 115 void readThread(); 116 117 /** 118 * Blocking call to read data from the connection. 119 * 120 * @return std::vector<uint8_t> Serialized protobuf data received from emulator. This will be 121 * an empty vector if the connection was closed or some other error occurred. 122 */ 123 virtual std::vector<uint8_t> read() = 0; 124 125 /** 126 * Transmits a string of data to the emulator. 127 * 128 * @param data Serialized protobuf data to transmit. 129 * 130 * @return int Number of bytes transmitted, or -1 if failed. 131 */ 132 virtual int write(const std::vector<uint8_t>& data) = 0; 133 }; 134 135 } // namespace impl 136 137 } // namespace V2_0 138 } // namespace vehicle 139 } // namespace automotive 140 } // namespace hardware 141 } // namespace android 142 143 #endif // ANDROID_EMULATORCOMMCONN_COMMCONN_H 144