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 <errno.h>
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 #include <dirent.h>
25
26 #include <netdb.h>
27 #include <netinet/in.h>
28
29 #include <sys/mman.h>
30 #include <sys/socket.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <sys/un.h>
34
35 #include <utils/RefBase.h>
36
37 #include <fstream>
38 #include <iostream>
39 #include <sstream>
40 #include <string>
41 #include <vector>
42
43 #include "AgentRequestHandler.h"
44 #include "test/vts/proto/VtsDriverControlMessage.pb.h"
45
46 #include "BinderClientToDriver.h"
47 #include "SocketClientToDriver.h"
48
49 #define LOCALHOST_IP "127.0.0.1"
50
51 using namespace std;
52
53 namespace android {
54 namespace vts {
55
56
Exit()57 bool VtsDriverSocketClient::Exit() {
58 VtsDriverControlCommandMessage command_message;
59 command_message.set_command_type(EXIT);
60 if (!VtsSocketSendMessage(command_message)) return false;
61
62 VtsDriverControlResponseMessage response_message;
63 if (!VtsSocketRecvMessage(&response_message)) return false;
64 return true;
65 }
66
LoadHal(const string & file_path,int target_class,int target_type,float target_version,const string & target_package,const string & target_component_name,const string & hw_binder_service_name,const string & module_name)67 int32_t VtsDriverSocketClient::LoadHal(const string& file_path,
68 int target_class, int target_type,
69 float target_version,
70 const string& target_package,
71 const string& target_component_name,
72 const string& hw_binder_service_name,
73 const string& module_name) {
74 VtsDriverControlCommandMessage command_message;
75 command_message.set_command_type(LOAD_HAL);
76 command_message.set_file_path(file_path);
77 command_message.set_target_class(target_class);
78 command_message.set_target_type(target_type);
79 command_message.set_target_version(target_version);
80 command_message.set_target_package(target_package);
81 command_message.set_target_component_name(target_component_name);
82 command_message.set_module_name(module_name);
83 command_message.set_hw_binder_service_name(hw_binder_service_name);
84 if (!VtsSocketSendMessage(command_message)) return -1;
85
86 VtsDriverControlResponseMessage response_message;
87 if (!VtsSocketRecvMessage(&response_message)) return -1;
88 cout << __func__ << " response code: " << response_message.response_code()
89 << endl;
90 return response_message.response_code();
91 }
92
GetFunctions()93 const char* VtsDriverSocketClient::GetFunctions() {
94 cout << "[agent->driver] LIST_FUNCTIONS" << endl;
95
96 VtsDriverControlCommandMessage command_message;
97 command_message.set_command_type(LIST_FUNCTIONS);
98 if (!VtsSocketSendMessage(command_message)) return NULL;
99
100 VtsDriverControlResponseMessage response_message;
101 if (!VtsSocketRecvMessage(&response_message)) return NULL;
102
103 char* result =
104 (char*)malloc(strlen(response_message.return_message().c_str()) + 1);
105 if (!result) {
106 cerr << __func__ << " ERROR result is NULL" << endl;
107 return NULL;
108 }
109 strcpy(result, response_message.return_message().c_str());
110 return result;
111 }
112
ReadSpecification(const string & component_name,int target_class,int target_type,float target_version,const string & target_package)113 const char* VtsDriverSocketClient::ReadSpecification(
114 const string& component_name,
115 int target_class,
116 int target_type,
117 float target_version,
118 const string& target_package) {
119 cout << "[agent->driver] LIST_FUNCTIONS" << endl;
120
121 VtsDriverControlCommandMessage command_message;
122 command_message.set_command_type(
123 VTS_DRIVER_COMMAND_READ_SPECIFICATION);
124 command_message.set_module_name(component_name);
125 command_message.set_target_class(target_class);
126 command_message.set_target_type(target_type);
127 command_message.set_target_version(target_version);
128 command_message.set_target_package(target_package);
129
130 if (!VtsSocketSendMessage(command_message)) return NULL;
131
132 VtsDriverControlResponseMessage response_message;
133 if (!VtsSocketRecvMessage(&response_message)) return NULL;
134
135 char* result =
136 (char*)malloc(strlen(response_message.return_message().c_str()) + 1);
137 if (!result) {
138 cerr << __func__ << " ERROR result is NULL" << endl;
139 return NULL;
140 }
141 strcpy(result, response_message.return_message().c_str());
142 return result;
143 }
144
Call(const string & arg,const string & uid)145 const char* VtsDriverSocketClient::Call(const string& arg, const string& uid) {
146 VtsDriverControlCommandMessage command_message;
147 command_message.set_command_type(CALL_FUNCTION);
148 command_message.set_arg(arg);
149 command_message.set_driver_caller_uid(uid);
150 if (!VtsSocketSendMessage(command_message)) return NULL;
151
152 VtsDriverControlResponseMessage response_message;
153 if (!VtsSocketRecvMessage(&response_message)) return NULL;
154
155 char* result =
156 (char*)malloc(strlen(response_message.return_message().c_str()) + 1);
157 if (!result) {
158 cerr << __func__ << " ERROR result is NULL" << endl;
159 return NULL;
160 }
161 strcpy(result, response_message.return_message().c_str());
162 return result;
163 }
164
GetAttribute(const string & arg)165 const char* VtsDriverSocketClient::GetAttribute(const string& arg) {
166 VtsDriverControlCommandMessage command_message;
167 command_message.set_command_type(GET_ATTRIBUTE);
168 command_message.set_arg(arg);
169 if (!VtsSocketSendMessage(command_message)) return NULL;
170
171 VtsDriverControlResponseMessage response_message;
172 if (!VtsSocketRecvMessage(&response_message)) return NULL;
173
174 char* result =
175 (char*)malloc(strlen(response_message.return_message().c_str()) + 1);
176 if (!result) {
177 cerr << __func__ << " ERROR result is NULL" << endl;
178 return NULL;
179 }
180 strcpy(result, response_message.return_message().c_str());
181 return result;
182 }
183
ExecuteShellCommand(const::google::protobuf::RepeatedPtrField<::std::string> shell_command)184 VtsDriverControlResponseMessage* VtsDriverSocketClient::ExecuteShellCommand(
185 const ::google::protobuf::RepeatedPtrField<::std::string> shell_command) {
186 VtsDriverControlCommandMessage command_message;
187 command_message.set_command_type(EXECUTE_COMMAND);
188 for (const auto& cmd : shell_command) {
189 command_message.add_shell_command(cmd);
190 }
191 if (!VtsSocketSendMessage(command_message)) return NULL;
192
193 VtsDriverControlResponseMessage* response_message =
194 new VtsDriverControlResponseMessage();
195 if (!VtsSocketRecvMessage(response_message)) return NULL;
196
197 return response_message;
198 }
199
Status(int32_t type)200 int32_t VtsDriverSocketClient::Status(int32_t type) {
201 VtsDriverControlCommandMessage command_message;
202 command_message.set_command_type(CALL_FUNCTION);
203 command_message.set_status_type(type);
204 if (!VtsSocketSendMessage(command_message)) return 0;
205
206 VtsDriverControlResponseMessage response_message;
207 if (!VtsSocketRecvMessage(&response_message)) return 0;
208 return response_message.return_value();
209 }
210
GetSocketPortFilePath(const string & service_name)211 string GetSocketPortFilePath(const string& service_name) {
212 string result("/data/local/tmp/");
213 result += service_name;
214 // static int count = 0;
215 // result += std::to_string(count++);
216 return result;
217 }
218
IsDriverRunning(const string & service_name,int retry_count)219 bool IsDriverRunning(const string& service_name, int retry_count) {
220 for (int retry = 0; retry < retry_count; retry++) {
221 VtsDriverSocketClient* client = GetDriverSocketClient(service_name);
222 if (client) {
223 client->Exit();
224 delete client;
225 return true;
226 }
227 sleep(1);
228 }
229 cout << __func__ << " "
230 << "couldn't connect to " << service_name << endl;
231 return false;
232 }
233
GetDriverSocketClient(const string & service_name)234 VtsDriverSocketClient* GetDriverSocketClient(const string& service_name) {
235 string socket_port_file_path = GetSocketPortFilePath(service_name);
236 VtsDriverSocketClient* client = new VtsDriverSocketClient();
237 if (!client->Connect(socket_port_file_path)) {
238 cerr << __func__ << " can't connect to " << socket_port_file_path << endl;
239 delete client;
240 return NULL;
241 }
242 return client;
243 }
244
245 } // namespace vts
246 } // namespace android
247