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