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