1 /*
2 * Copyright 2016 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 "VtsDriverCommUtil"
18 #include "VtsDriverCommUtil.h"
19
20 #include <errno.h>
21 #include <netdb.h>
22 #include <netinet/in.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/socket.h>
27 #include <sys/types.h>
28 #include <sys/un.h>
29 #include <unistd.h>
30
31 #include <android-base/logging.h>
32
33 #include <iostream>
34 #include <sstream>
35
36 #include "test/vts/proto/VtsDriverControlMessage.pb.h"
37
38 using namespace std;
39
40 #define MAX_HEADER_BUFFER_SIZE 128
41
42 namespace android {
43 namespace vts {
44
Connect(const string & socket_name)45 bool VtsDriverCommUtil::Connect(const string& socket_name) {
46 struct sockaddr_un serv_addr;
47 struct hostent* server;
48
49 LOG(INFO) << __func__ << " socket name: " << socket_name << endl;
50 sockfd_ = socket(PF_UNIX, SOCK_STREAM, 0);
51 if (sockfd_ < 0) {
52 cerr << __func__ << " ERROR opening socket" << endl;
53 return false;
54 }
55
56 server = gethostbyname("127.0.0.1");
57 if (server == NULL) {
58 cerr << __func__ << " ERROR can't resolve the host name, 127.0.0.1" << endl;
59 return false;
60 }
61
62 bzero((char*)&serv_addr, sizeof(serv_addr));
63 serv_addr.sun_family = AF_UNIX;
64 strcpy(serv_addr.sun_path, socket_name.c_str());
65
66 if (connect(sockfd_, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
67 cerr << __func__ << " ERROR connecting to " << socket_name
68 << " errno = " << errno << " " << strerror(errno) << endl;
69 sockfd_ = -1;
70 return false;
71 }
72 return true;
73 }
74
Close()75 int VtsDriverCommUtil::Close() {
76 int result = 0;
77 if (sockfd_ != -1) {
78 result = close(sockfd_);
79 if (result != 0) {
80 cerr << getpid() << " " << __func__ << ":" << __LINE__
81 << " ERROR closing socket (errno = " << errno << ")" << endl;
82 }
83
84 sockfd_ = -1;
85 }
86
87 return result;
88 }
89
VtsSocketSendBytes(const string & message)90 bool VtsDriverCommUtil::VtsSocketSendBytes(const string& message) {
91 if (sockfd_ == -1) {
92 cerr << __func__ << " ERROR sockfd not set" << endl;
93 return false;
94 }
95 std::stringstream header;
96 header << message.length() << "\n";
97 LOG(INFO) << "[agent->driver] len = " << message.length() << endl;
98 int n = write(sockfd_, header.str().c_str(), header.str().length());
99 if (n < 0) {
100 cerr << getpid() << " " << __func__ << ":" << __LINE__
101 << " ERROR writing to socket" << endl;
102 return false;
103 }
104
105 int bytes_sent = 0;
106 int msg_len = message.length();
107 while (bytes_sent < msg_len) {
108 n = write(sockfd_, &message.c_str()[bytes_sent], msg_len - bytes_sent);
109 if (n <= 0) {
110 cerr << getpid() << " " << __func__ << ":" << __LINE__
111 << " ERROR writing to socket" << endl;
112 return false;
113 }
114 bytes_sent += n;
115 }
116 return true;
117 }
118
VtsSocketRecvBytes()119 string VtsDriverCommUtil::VtsSocketRecvBytes() {
120 if (sockfd_ == -1) {
121 cerr << getpid() << " " << __func__ << " ERROR sockfd not set" << endl;
122 return string();
123 }
124
125 int header_index = 0;
126 char header_buffer[MAX_HEADER_BUFFER_SIZE];
127
128 for (header_index = 0; header_index < MAX_HEADER_BUFFER_SIZE;
129 header_index++) {
130 int ret = read(sockfd_, &header_buffer[header_index], 1);
131 if (ret != 1) {
132 int errno_save = errno;
133 cerr << getpid() << " " << __func__
134 << " ERROR reading the length ret = " << ret
135 << " sockfd = " << sockfd_ << " "
136 << " errno = " << errno_save << " " << strerror(errno_save) << endl;
137 return string();
138 }
139 if (header_buffer[header_index] == '\n' ||
140 header_buffer[header_index] == '\r') {
141 header_buffer[header_index] = '\0';
142 break;
143 }
144 }
145
146 int msg_len = atoi(header_buffer);
147 char* msg = (char*)malloc(msg_len + 1);
148 if (!msg) {
149 cerr << getpid() << " " << __func__ << " ERROR malloc failed" << endl;
150 return string();
151 }
152
153 int bytes_read = 0;
154 while (bytes_read < msg_len) {
155 int result = read(sockfd_, &msg[bytes_read], msg_len - bytes_read);
156 if (result <= 0) {
157 cerr << getpid() << " " << __func__ << " ERROR read failed" << endl;
158 return string();
159 }
160 bytes_read += result;
161 }
162 msg[msg_len] = '\0';
163 return string(msg, msg_len);
164 }
165
VtsSocketSendMessage(const google::protobuf::Message & message)166 bool VtsDriverCommUtil::VtsSocketSendMessage(
167 const google::protobuf::Message& message) {
168 if (sockfd_ == -1) {
169 cerr << getpid() << " " << __func__ << " ERROR sockfd not set" << endl;
170 return false;
171 }
172
173 string message_string;
174 if (!message.SerializeToString(&message_string)) {
175 cerr << getpid() << " " << __func__
176 << " ERROR can't serialize the message to a string." << endl;
177 return false;
178 }
179 return VtsSocketSendBytes(message_string);
180 }
181
VtsSocketRecvMessage(google::protobuf::Message * message)182 bool VtsDriverCommUtil::VtsSocketRecvMessage(
183 google::protobuf::Message* message) {
184 if (sockfd_ == -1) {
185 cerr << getpid() << " " << __func__ << " ERROR sockfd not set" << endl;
186 return false;
187 }
188
189 string message_string = VtsSocketRecvBytes();
190 if (message_string.length() == 0) {
191 cerr << getpid() << " " << __func__ << " ERROR message string zero length"
192 << endl;
193 return false;
194 }
195
196 return message->ParseFromString(message_string);
197 }
198
199 } // namespace vts
200 } // namespace android
201