Home | History | Annotate | Download | only in replayer
      1 /*
      2  * Copyright (C) 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 #include "VtsHidlHalReplayer.h"
     17 
     18 #include <fcntl.h>
     19 #include <unistd.h>
     20 #include <fstream>
     21 #include <iostream>
     22 #include <string>
     23 
     24 #include <google/protobuf/io/zero_copy_stream_impl.h>
     25 #include <google/protobuf/text_format.h>
     26 
     27 #include "VtsProfilingUtil.h"
     28 #include "driver_base/DriverBase.h"
     29 #include "utils/InterfaceSpecUtil.h"
     30 #include "utils/StringUtil.h"
     31 
     32 using namespace std;
     33 
     34 namespace android {
     35 namespace vts {
     36 
     37 bool VtsHidlHalReplayer::ReplayTrace(const string& trace_file,
     38                                      const string& hal_service_name) {
     39   // Parse the trace file to get the sequence of function calls.
     40   int fd =
     41       open(trace_file.c_str(), O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
     42   if (fd < 0) {
     43     cerr << "Can not open trace file: " << trace_file
     44          << "error: " << std::strerror(errno);
     45     return false;
     46   }
     47 
     48   google::protobuf::io::FileInputStream input(fd);
     49 
     50   VtsProfilingRecord call_msg;
     51   VtsProfilingRecord expected_result_msg;
     52   while (readOneDelimited(&call_msg, &input) &&
     53          readOneDelimited(&expected_result_msg, &input)) {
     54     if (call_msg.event() != InstrumentationEventType::SERVER_API_ENTRY &&
     55         call_msg.event() != InstrumentationEventType::CLIENT_API_ENTRY &&
     56         call_msg.event() != InstrumentationEventType::SYNC_CALLBACK_ENTRY &&
     57         call_msg.event() != InstrumentationEventType::ASYNC_CALLBACK_ENTRY &&
     58         call_msg.event() != InstrumentationEventType::PASSTHROUGH_ENTRY) {
     59       cerr << "Expected a call message but got message with event: "
     60            << call_msg.event();
     61       continue;
     62     }
     63     if (expected_result_msg.event() !=
     64             InstrumentationEventType::SERVER_API_EXIT &&
     65         expected_result_msg.event() !=
     66             InstrumentationEventType::CLIENT_API_EXIT &&
     67         expected_result_msg.event() !=
     68             InstrumentationEventType::SYNC_CALLBACK_EXIT &&
     69         expected_result_msg.event() !=
     70             InstrumentationEventType::ASYNC_CALLBACK_EXIT &&
     71         expected_result_msg.event() !=
     72             InstrumentationEventType::PASSTHROUGH_EXIT) {
     73       cerr << "Expected a result message but got message with event: "
     74            << call_msg.event();
     75       continue;
     76     }
     77 
     78     cout << __func__ << ": replay function: " << call_msg.func_msg().name();
     79 
     80     string package_name = call_msg.package();
     81     float version = call_msg.version();
     82     string interface_name = call_msg.interface();
     83     DriverBase* driver = driver_manager_->GetDriverForHidlHalInterface(
     84         package_name, version, interface_name, hal_service_name);
     85     if (!driver) {
     86       cerr << __func__ << ": couldn't get a driver base class" << endl;
     87       return false;
     88     }
     89 
     90     vts::FunctionSpecificationMessage result_msg;
     91     if (!driver->CallFunction(call_msg.func_msg(), "" /*callback_socket_name*/,
     92                               &result_msg)) {
     93       cerr << __func__ << ": replay function fail." << endl;
     94       return false;
     95     }
     96     if (!driver->VerifyResults(expected_result_msg.func_msg(), result_msg)) {
     97       // Verification is not strict, i.e. if fail, output error message and
     98       // continue the process.
     99       cerr << __func__ << ": verification fail." << endl;
    100     }
    101     call_msg.Clear();
    102     expected_result_msg.Clear();
    103   }
    104   return true;
    105 }
    106 
    107 }  // namespace vts
    108 }  // namespace android
    109