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 #define LOG_TAG "PipeComm"
18 
19 #include <android/hardware/automotive/vehicle/2.0/IVehicle.h>
20 #include <android/log.h>
21 #include <log/log.h>
22 #include <system/qemu_pipe.h>
23 
24 #include "PipeComm.h"
25 
26 #define CAR_SERVICE_NAME "pipe:qemud:car"
27 
28 
29 namespace android {
30 namespace hardware {
31 namespace automotive {
32 namespace vehicle {
33 namespace V2_0 {
34 
35 namespace impl {
36 
PipeComm()37 PipeComm::PipeComm() {
38     // Initialize member vars
39     mPipeFd = -1;
40 }
41 
42 
open()43 int PipeComm::open() {
44     int fd = qemu_pipe_open(CAR_SERVICE_NAME);
45 
46     if (fd < 0) {
47         ALOGE("%s: Could not open connection to service: %s %d", __FUNCTION__, strerror(errno), fd);
48         return -errno;
49     }
50 
51     ALOGI("%s: OPENED PIPE, fd=%d", __FUNCTION__, fd);
52     mPipeFd = fd;
53     return 0;
54 }
55 
read()56 std::vector<uint8_t> PipeComm::read() {
57     static constexpr int MAX_RX_MSG_SZ = 2048;
58     std::vector<uint8_t> msg = std::vector<uint8_t>(MAX_RX_MSG_SZ);
59     int numBytes;
60 
61     numBytes = qemu_pipe_frame_recv(mPipeFd, msg.data(), msg.size());
62 
63     if (numBytes == MAX_RX_MSG_SZ) {
64         ALOGE("%s:  Received max size = %d", __FUNCTION__, MAX_RX_MSG_SZ);
65     } else if (numBytes > 0) {
66         msg.resize(numBytes);
67         return msg;
68     } else {
69         ALOGD("%s: Connection terminated on pipe %d, numBytes=%d", __FUNCTION__, mPipeFd, numBytes);
70         {
71             std::lock_guard<std::mutex> lock(mMutex);
72             mPipeFd = -1;
73         }
74     }
75 
76     return std::vector<uint8_t>();
77 }
78 
write(const std::vector<uint8_t> & data)79 int PipeComm::write(const std::vector<uint8_t>& data) {
80     int retVal = 0;
81 
82     {
83         std::lock_guard<std::mutex> lock(mMutex);
84         if (mPipeFd != -1) {
85             retVal = qemu_pipe_frame_send(mPipeFd, data.data(), data.size());
86         }
87     }
88 
89     if (retVal < 0) {
90         retVal = -errno;
91         ALOGE("%s:  send_cmd: (fd=%d): ERROR: %s", __FUNCTION__, mPipeFd, strerror(errno));
92     }
93 
94     return retVal;
95 }
96 
97 
98 }  // impl
99 
100 }  // namespace V2_0
101 }  // namespace vehicle
102 }  // namespace automotive
103 }  // namespace hardware
104 }  // namespace android
105 
106 
107 
108