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_SocketComm_H_ 18 #define android_hardware_automotive_vehicle_V2_0_impl_SocketComm_H_ 19 20 #include <mutex> 21 #include <vector> 22 #include "CommBase.h" 23 24 namespace android { 25 namespace hardware { 26 namespace automotive { 27 namespace vehicle { 28 namespace V2_0 { 29 30 namespace impl { 31 32 /** 33 * SocketComm opens a socket via adb's TCP port forwarding to enable a Host PC to connect to 34 * the VehicleHAL. 35 */ 36 class SocketComm : public CommBase { 37 public: 38 SocketComm(); 39 virtual ~SocketComm(); 40 41 /** 42 * Creates a connection to the other side. 43 * 44 * @return int Returns fd or socket number if connection is successful. 45 * Otherwise, returns -1 if no connection is availble. 46 */ 47 int connect() override; 48 49 /** 50 * Opens a socket and begins listening. 51 * 52 * @return int Returns 0 on success. 53 */ 54 int open() override; 55 56 /** 57 * Blocking call to read data from the connection. 58 * 59 * @return std::vector<uint8_t> Serialized protobuf data received from emulator. This will be 60 * an empty vector if the connection was closed or some other error occurred. 61 */ 62 std::vector<uint8_t> read() override; 63 64 /** 65 * Closes a connection if it is open. 66 */ 67 void stop() override; 68 69 /** 70 * Transmits a string of data to the emulator. 71 * 72 * @param data Serialized protobuf data to transmit. 73 * 74 * @return int Number of bytes transmitted, or -1 if failed. 75 */ 76 int write(const std::vector<uint8_t>& data) override; 77 78 private: 79 int mCurSockFd; 80 std::atomic<int> mExit; 81 std::mutex mMutex; 82 int mSockFd; 83 }; 84 85 } // impl 86 87 } // namespace V2_0 88 } // namespace vehicle 89 } // namespace automotive 90 } // namespace hardware 91 } // namespace android 92 93 94 #endif // android_hardware_automotive_vehicle_V2_0_impl_SocketComm_H_ 95