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 #define LOG_TAG "VtsAgentSocketClient"
17
18 #include "SocketClientToDriver.h"
19
20 #include <android-base/logging.h>
21
22 #include "AgentRequestHandler.h"
23 #include "test/vts/proto/VtsDriverControlMessage.pb.h"
24
25 #define LOCALHOST_IP "127.0.0.1"
26
27 namespace android {
28 namespace vts {
29
Exit()30 bool VtsDriverSocketClient::Exit() {
31 VtsDriverControlCommandMessage command_message;
32 command_message.set_command_type(EXIT);
33 if (!VtsSocketSendMessage(command_message)) return false;
34
35 VtsDriverControlResponseMessage response_message;
36 if (!VtsSocketRecvMessage(&response_message)) return false;
37 return true;
38 }
39
LoadHal(const string & file_path,int target_class,int target_type,int target_version_major,int target_version_minor,const string & target_package,const string & target_component_name,const string & hw_binder_service_name,const string & module_name)40 int32_t VtsDriverSocketClient::LoadHal(
41 const string& file_path, int target_class, int target_type,
42 int target_version_major, int target_version_minor,
43 const string& target_package, const string& target_component_name,
44 const string& hw_binder_service_name, const string& module_name) {
45 VtsDriverControlCommandMessage command_message;
46
47 command_message.set_command_type(LOAD_HAL);
48 command_message.set_file_path(file_path);
49 command_message.set_target_class(target_class);
50 command_message.set_target_type(target_type);
51 command_message.set_target_version_major(target_version_major);
52 command_message.set_target_version_minor(target_version_minor);
53 command_message.set_target_package(target_package);
54 command_message.set_target_component_name(target_component_name);
55 command_message.set_module_name(module_name);
56 command_message.set_hw_binder_service_name(hw_binder_service_name);
57 if (!VtsSocketSendMessage(command_message)) return -1;
58
59 VtsDriverControlResponseMessage response_message;
60 if (!VtsSocketRecvMessage(&response_message)) return -1;
61 if (response_message.response_code() != VTS_DRIVER_RESPONSE_SUCCESS) {
62 LOG(ERROR) << "Failed to load the selected HAL.";
63 return -1;
64 }
65 LOG(INFO) << "Loaded the selected HAL.";
66 return response_message.return_value();
67 }
68
GetFunctions()69 string VtsDriverSocketClient::GetFunctions() {
70 VtsDriverControlCommandMessage command_message;
71 command_message.set_command_type(LIST_FUNCTIONS);
72 if (!VtsSocketSendMessage(command_message)) {
73 return {};
74 }
75
76 VtsDriverControlResponseMessage response_message;
77 if (!VtsSocketRecvMessage(&response_message)) {
78 return {};
79 }
80
81 return response_message.return_message();
82 }
83
ReadSpecification(const string & component_name,int target_class,int target_type,int target_version_major,int target_version_minor,const string & target_package)84 string VtsDriverSocketClient::ReadSpecification(const string& component_name,
85 int target_class,
86 int target_type,
87 int target_version_major,
88 int target_version_minor,
89 const string& target_package) {
90 VtsDriverControlCommandMessage command_message;
91
92 command_message.set_command_type(
93 VTS_DRIVER_COMMAND_READ_SPECIFICATION);
94 command_message.set_module_name(component_name);
95 command_message.set_target_class(target_class);
96 command_message.set_target_type(target_type);
97 command_message.set_target_version_major(target_version_major);
98 command_message.set_target_version_minor(target_version_minor);
99 command_message.set_target_package(target_package);
100
101 if (!VtsSocketSendMessage(command_message)) {
102 return {};
103 }
104
105 VtsDriverControlResponseMessage response_message;
106 if (!VtsSocketRecvMessage(&response_message)) {
107 return {};
108 }
109
110 return response_message.return_message();
111 }
112
Call(const string & arg,const string & uid)113 string VtsDriverSocketClient::Call(const string& arg, const string& uid) {
114 VtsDriverControlCommandMessage command_message;
115 command_message.set_command_type(CALL_FUNCTION);
116 command_message.set_arg(arg);
117 command_message.set_driver_caller_uid(uid);
118 if (!VtsSocketSendMessage(command_message)) {
119 return {};
120 }
121
122 VtsDriverControlResponseMessage response_message;
123 if (!VtsSocketRecvMessage(&response_message)) {
124 return {};
125 }
126
127 LOG(DEBUG) << "Result: " << response_message.return_message();
128 return response_message.return_message();
129 }
130
GetAttribute(const string & arg)131 string VtsDriverSocketClient::GetAttribute(const string& arg) {
132 VtsDriverControlCommandMessage command_message;
133 command_message.set_command_type(GET_ATTRIBUTE);
134 command_message.set_arg(arg);
135 if (!VtsSocketSendMessage(command_message)) {
136 return {};
137 }
138
139 VtsDriverControlResponseMessage response_message;
140 if (!VtsSocketRecvMessage(&response_message)) {
141 return {};
142 }
143
144 return response_message.return_message();
145 }
146
147 unique_ptr<VtsDriverControlResponseMessage>
ExecuteShellCommand(const::google::protobuf::RepeatedPtrField<string> shell_command)148 VtsDriverSocketClient::ExecuteShellCommand(
149 const ::google::protobuf::RepeatedPtrField<string> shell_command) {
150 VtsDriverControlCommandMessage command_message;
151 command_message.set_command_type(EXECUTE_COMMAND);
152 for (const auto& cmd : shell_command) {
153 command_message.add_shell_command(cmd);
154 }
155 if (!VtsSocketSendMessage(command_message)) return nullptr;
156
157 auto response_message = make_unique<VtsDriverControlResponseMessage>();
158 if (!VtsSocketRecvMessage(response_message.get())) return nullptr;
159
160 return response_message;
161 }
162
ProcessFmqCommand(const FmqRequestMessage & fmq_request,FmqResponseMessage * fmq_response)163 bool VtsDriverSocketClient::ProcessFmqCommand(
164 const FmqRequestMessage& fmq_request, FmqResponseMessage* fmq_response) {
165 VtsDriverControlCommandMessage command_message;
166 VtsDriverControlResponseMessage response_message;
167 command_message.set_command_type(FMQ_OPERATION);
168 (command_message.mutable_fmq_request())->CopyFrom(fmq_request);
169
170 if (!VtsSocketSendMessage(command_message)) return false;
171 if (!VtsSocketRecvMessage(&response_message)) return false;
172
173 fmq_response->CopyFrom(response_message.fmq_response());
174 return true;
175 }
176
ProcessHidlMemoryCommand(const HidlMemoryRequestMessage & hidl_memory_request,HidlMemoryResponseMessage * hidl_memory_response)177 bool VtsDriverSocketClient::ProcessHidlMemoryCommand(
178 const HidlMemoryRequestMessage& hidl_memory_request,
179 HidlMemoryResponseMessage* hidl_memory_response) {
180 VtsDriverControlCommandMessage command_message;
181 VtsDriverControlResponseMessage response_message;
182 command_message.set_command_type(HIDL_MEMORY_OPERATION);
183 (command_message.mutable_hidl_memory_request())
184 ->CopyFrom(hidl_memory_request);
185
186 if (!VtsSocketSendMessage(command_message)) return false;
187 if (!VtsSocketRecvMessage(&response_message)) return false;
188
189 hidl_memory_response->CopyFrom(response_message.hidl_memory_response());
190 return true;
191 }
192
ProcessHidlHandleCommand(const HidlHandleRequestMessage & hidl_handle_request,HidlHandleResponseMessage * hidl_handle_response)193 bool VtsDriverSocketClient::ProcessHidlHandleCommand(
194 const HidlHandleRequestMessage& hidl_handle_request,
195 HidlHandleResponseMessage* hidl_handle_response) {
196 VtsDriverControlCommandMessage command_message;
197 VtsDriverControlResponseMessage response_message;
198 command_message.set_command_type(HIDL_HANDLE_OPERATION);
199 (command_message.mutable_hidl_handle_request())
200 ->CopyFrom(hidl_handle_request);
201
202 if (!VtsSocketSendMessage(command_message)) {
203 LOG(ERROR) << "Unable to send hidl_handle command from agent to driver.";
204 return false;
205 }
206 if (!VtsSocketRecvMessage(&response_message)) {
207 LOG(ERROR) << "Unable to receive hidl_handle message from driver to agent";
208 return false;
209 }
210
211 hidl_handle_response->CopyFrom(response_message.hidl_handle_response());
212 return true;
213 }
214
Status(int32_t type)215 int32_t VtsDriverSocketClient::Status(int32_t type) {
216 VtsDriverControlCommandMessage command_message;
217 command_message.set_command_type(CALL_FUNCTION);
218 command_message.set_status_type(type);
219 if (!VtsSocketSendMessage(command_message)) return 0;
220
221 VtsDriverControlResponseMessage response_message;
222 if (!VtsSocketRecvMessage(&response_message)) return 0;
223 return response_message.return_value();
224 }
225
GetSocketPortFilePath(const string & service_name)226 string GetSocketPortFilePath(const string& service_name) {
227 string result("/data/local/tmp/");
228 result += service_name;
229 return result;
230 }
231
IsDriverRunning(const string & service_name,int retry_count)232 bool IsDriverRunning(const string& service_name, int retry_count) {
233 for (int retry = 0; retry < retry_count; retry++) {
234 VtsDriverSocketClient* client = GetDriverSocketClient(service_name);
235 if (client) {
236 client->Exit();
237 delete client;
238 return true;
239 }
240 sleep(1);
241 }
242 LOG(ERROR) << "Couldn't connect to " << service_name;
243 return false;
244 }
245
GetDriverSocketClient(const string & service_name)246 VtsDriverSocketClient* GetDriverSocketClient(const string& service_name) {
247 string socket_port_file_path = GetSocketPortFilePath(service_name);
248 VtsDriverSocketClient* client = new VtsDriverSocketClient();
249 if (!client->Connect(socket_port_file_path)) {
250 LOG(ERROR) << "Can't connect to " << socket_port_file_path;
251 delete client;
252 return NULL;
253 }
254 return client;
255 }
256
257 } // namespace vts
258 } // namespace android
259