Home | History | Annotate | Download | only in hal
      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 
     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 
     40 int32_t VtsDriverSocketClient::LoadHal(const string& file_path,
     41                                        int target_class, int target_type,
     42                                        float target_version,
     43                                        const string& target_package,
     44                                        const string& target_component_name,
     45                                        const string& hw_binder_service_name,
     46                                        const string& module_name) {
     47   VtsDriverControlCommandMessage command_message;
     48   command_message.set_command_type(LOAD_HAL);
     49   command_message.set_file_path(file_path);
     50   command_message.set_target_class(target_class);
     51   command_message.set_target_type(target_type);
     52   command_message.set_target_version(target_version);
     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 
     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 
     84 string VtsDriverSocketClient::ReadSpecification(const string& component_name,
     85                                                 int target_class,
     86                                                 int target_type,
     87                                                 float target_version,
     88                                                 const string& target_package) {
     89   VtsDriverControlCommandMessage command_message;
     90   command_message.set_command_type(
     91       VTS_DRIVER_COMMAND_READ_SPECIFICATION);
     92   command_message.set_module_name(component_name);
     93   command_message.set_target_class(target_class);
     94   command_message.set_target_type(target_type);
     95   command_message.set_target_version(target_version);
     96   command_message.set_target_package(target_package);
     97 
     98   if (!VtsSocketSendMessage(command_message)) {
     99     return {};
    100   }
    101 
    102   VtsDriverControlResponseMessage response_message;
    103   if (!VtsSocketRecvMessage(&response_message)) {
    104     return {};
    105   }
    106 
    107   return response_message.return_message();
    108 }
    109 
    110 string VtsDriverSocketClient::Call(const string& arg, const string& uid) {
    111   VtsDriverControlCommandMessage command_message;
    112   command_message.set_command_type(CALL_FUNCTION);
    113   command_message.set_arg(arg);
    114   command_message.set_driver_caller_uid(uid);
    115   if (!VtsSocketSendMessage(command_message)) {
    116     return {};
    117   }
    118 
    119   VtsDriverControlResponseMessage response_message;
    120   if (!VtsSocketRecvMessage(&response_message)) {
    121     return {};
    122   }
    123 
    124   LOG(DEBUG) << "Result: " << response_message.return_message();
    125   return response_message.return_message();
    126 }
    127 
    128 string VtsDriverSocketClient::GetAttribute(const string& arg) {
    129   VtsDriverControlCommandMessage command_message;
    130   command_message.set_command_type(GET_ATTRIBUTE);
    131   command_message.set_arg(arg);
    132   if (!VtsSocketSendMessage(command_message)) {
    133     return {};
    134   }
    135 
    136   VtsDriverControlResponseMessage response_message;
    137   if (!VtsSocketRecvMessage(&response_message)) {
    138     return {};
    139   }
    140 
    141   return response_message.return_message();
    142 }
    143 
    144 unique_ptr<VtsDriverControlResponseMessage>
    145 VtsDriverSocketClient::ExecuteShellCommand(
    146     const ::google::protobuf::RepeatedPtrField<string> shell_command) {
    147   VtsDriverControlCommandMessage command_message;
    148   command_message.set_command_type(EXECUTE_COMMAND);
    149   for (const auto& cmd : shell_command) {
    150     command_message.add_shell_command(cmd);
    151   }
    152   if (!VtsSocketSendMessage(command_message)) return nullptr;
    153 
    154   auto response_message = make_unique<VtsDriverControlResponseMessage>();
    155   if (!VtsSocketRecvMessage(response_message.get())) return nullptr;
    156 
    157   return response_message;
    158 }
    159 
    160 int32_t VtsDriverSocketClient::Status(int32_t type) {
    161   VtsDriverControlCommandMessage command_message;
    162   command_message.set_command_type(CALL_FUNCTION);
    163   command_message.set_status_type(type);
    164   if (!VtsSocketSendMessage(command_message)) return 0;
    165 
    166   VtsDriverControlResponseMessage response_message;
    167   if (!VtsSocketRecvMessage(&response_message)) return 0;
    168   return response_message.return_value();
    169 }
    170 
    171 string GetSocketPortFilePath(const string& service_name) {
    172   string result("/data/local/tmp/");
    173   result += service_name;
    174   return result;
    175 }
    176 
    177 bool IsDriverRunning(const string& service_name, int retry_count) {
    178   for (int retry = 0; retry < retry_count; retry++) {
    179     VtsDriverSocketClient* client = GetDriverSocketClient(service_name);
    180     if (client) {
    181       client->Exit();
    182       delete client;
    183       return true;
    184     }
    185     sleep(1);
    186   }
    187   LOG(ERROR) << "Couldn't connect to " << service_name;
    188   return false;
    189 }
    190 
    191 VtsDriverSocketClient* GetDriverSocketClient(const string& service_name) {
    192   string socket_port_file_path = GetSocketPortFilePath(service_name);
    193   VtsDriverSocketClient* client = new VtsDriverSocketClient();
    194   if (!client->Connect(socket_port_file_path)) {
    195     LOG(ERROR) << "Can't connect to " << socket_port_file_path;
    196     delete client;
    197     return NULL;
    198   }
    199   return client;
    200 }
    201 
    202 }  // namespace vts
    203 }  // namespace android
    204