1 /*
2  * Copyright 2017 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 /*
18  * Example usage
19  *  $ vts_hal_replayer /data/local/tmp/hal-trace/nfc/V1_0/nfc.vts.trace
20  *  $ vts_hal_replayer --spec_dir_path /data/local/tmp/spec
21  *    --hal_service_name default
22  *    /data/local/tmp/hal-trace/nfc/V1_0/nfc.vts.trace
23  */
24 
25 #include <getopt.h>
26 #include <iostream>
27 #include <string>
28 
29 #include <android-base/logging.h>
30 
31 #include <resource_manager/VtsResourceManager.h>
32 #include "VtsHidlHalReplayer.h"
33 #include "driver_manager/VtsHalDriverManager.h"
34 
35 using namespace std;
36 
37 static constexpr const char* kDefaultSpecDirPath = "/data/local/tmp/spec/";
38 static constexpr const char* kPassedMarker = "[  PASSED  ]";
39 static const int kDefaultEpochCount = 100;
40 
AddHalServiceInstance(const string & instance,map<string,string> * halServiceInstances)41 static void AddHalServiceInstance(const string& instance,
42                                   map<string, string>* halServiceInstances) {
43   if (halServiceInstances == nullptr) {
44     cerr << __func__ << ": halServiceInstances should not be null " << endl;
45     return;
46   }
47   // hal_service_instance follows the format:
48   // package@version::interface/service_name e.g.:
49   // android.hardware.vibrator@1.0::IVibrator/default
50   string instanceName = instance.substr(0, instance.find('/'));
51   string serviceName = instance.substr(instance.find('/') + 1);
52   // Fail the process if trying to pass multiple service names for the same
53   // service instance.
54   if (halServiceInstances->find(instanceName) != halServiceInstances->end()) {
55     cerr << "Exisitng instance " << instanceName << " with name "
56          << (*halServiceInstances)[instanceName] << endl;
57   } else {
58     (*halServiceInstances)[instanceName] = serviceName;
59   }
60 }
61 
ShowUsage()62 void ShowUsage() {
63   cout << "Usage: vts_hal_replayer [options] <trace file>\n"
64           "--spec_dir_path <path>:     Set path that store the vts spec files\n"
65           "--hal_service_instances <name>:  Set the hal service name\n"
66           "--help:                     Show help\n";
67   exit(1);
68 }
69 
main(int argc,char ** argv)70 int main(int argc, char** argv) {
71   android::base::InitLogging(argv, android::base::StderrLogger);
72 
73   const char* const short_opts = "hld:n:";
74   const option long_opts[] = {
75       {"help", no_argument, nullptr, 'h'},
76       {"list_service", no_argument, nullptr, 'l'},
77       {"spec_dir_path", optional_argument, nullptr, 'd'},
78       {"hal_service_instances", optional_argument, nullptr, 'n'},
79       {nullptr, 0, nullptr, 0}};
80 
81   string spec_dir_path = kDefaultSpecDirPath;
82   map<string, string> hal_service_instances;
83   bool list_service = false;
84 
85   while (true) {
86     int opt = getopt_long(argc, argv, short_opts, long_opts, nullptr);
87     if (opt == -1) {
88       break;
89     }
90 
91     switch (opt) {
92       case 'h':
93       case '?':
94         ShowUsage();
95         return 0;
96       case 'd': {
97         spec_dir_path = string(optarg);
98         break;
99       }
100       case 'n': {
101         AddHalServiceInstance(string(optarg), &hal_service_instances);
102         break;
103       }
104       case 'l': {
105         list_service = true;
106         break;
107       }
108       default:
109         cerr << "getopt_long returned unexpected value " << opt << endl;
110         return 2;
111     }
112   }
113 
114   if (optind != argc - 1) {
115     cerr << "Must specify the trace file (see --help).\n" << endl;
116     return 2;
117   }
118 
119   string trace_path = argv[optind];
120 
121   android::vts::VtsResourceManager resource_manager;
122   android::vts::VtsHalDriverManager driver_manager(
123       spec_dir_path, kDefaultEpochCount, "", &resource_manager);
124   android::vts::VtsHidlHalReplayer replayer(&driver_manager);
125 
126   if (list_service) {
127     replayer.ListTestServices(trace_path);
128   } else {
129     bool success = replayer.ReplayTrace(trace_path, hal_service_instances);
130     if (success) {
131       cout << endl << kPassedMarker << endl;
132     }
133   }
134   return 0;
135 }
136