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_PipeComm_H_
18 #define android_hardware_automotive_vehicle_V2_0_impl_PipeComm_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  * PipeComm uses a qemu pipe interface to connect to the Goldfish Emulator.
34  */
35 class PipeComm : public CommBase {
36 public:
37     PipeComm();
38 
39     /**
40      * Opens a pipe and begins listening.
41      *
42      * @return int Returns 0 on success.
43      */
44     int open() override;
45 
46     /**
47      * Blocking call to read data from the connection.
48      *
49      * @return std::vector<uint8_t> Serialized protobuf data received from emulator.  This will be
50      *              an empty vector if the connection was closed or some other error occurred.
51      */
52     std::vector<uint8_t> read() override;
53 
54     /**
55      * Transmits a string of data to the emulator.
56      *
57      * @param data Serialized protobuf data to transmit.
58      *
59      * @return int Number of bytes transmitted, or -1 if failed.
60      */
61     int write(const std::vector<uint8_t>& data) override;
62 
63 private:
64     std::mutex mMutex;
65     int mPipeFd;
66 };
67 
68 }  // impl
69 
70 }  // namespace V2_0
71 }  // namespace vehicle
72 }  // namespace automotive
73 }  // namespace hardware
74 }  // namespace android
75 
76 
77 #endif  // android_hardware_automotive_vehicle_V2_0_impl_PipeComm_H_
78