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_driver
20  *  $ vts_hal_driver --spec_dir_path /data/local/tmp/spec/
21  *    --callback_socket_name /data/local/tmp/vts_agent_callback
22  *    --server_socket_path /data/local/tmp/vts_tcp_server_port
23  */
24 #include <getopt.h>
25 #include <iostream>
26 #include <string>
27 
28 #include <android-base/logging.h>
29 
30 #include <resource_manager/VtsResourceManager.h>
31 #include "BinderServer.h"
32 #include "SocketServer.h"
33 #include "binder/VtsFuzzerBinderService.h"
34 #include "driver_manager/VtsHalDriverManager.h"
35 
36 using namespace std;
37 
38 static constexpr const char* kDefaultSpecDirPath = "/data/local/tmp/spec/";
39 static constexpr const char* kInterfaceSpecLibName =
40     "libvts_interfacespecification.so";
41 static const int kDefaultEpochCount = 100;
42 
ShowUsage()43 void ShowUsage() {
44   cout << "Usage: vts_hal_driver [options] <interface spec lib>\n"
45           "--spec_dir_path <path>:         Set path that store the vts spec "
46           "files\n"
47           "--callback_socket_name <name>:  Set the callback (agent) socket "
48           "name\n"
49           "--server_socket_path <path>:    Set the driver server socket path\n"
50           "--service_name <name>:          Set the binder service name\n"
51           "--help:                         Show help\n";
52   exit(1);
53 }
54 
main(int argc,char ** argv)55 int main(int argc, char** argv) {
56   android::base::InitLogging(argv, android::base::StderrLogger);
57 
58   const char* const short_opts = "h:d:c:s:n";
59   const option long_opts[] = {
60       {"help", no_argument, nullptr, 'h'},
61       {"spec_dir_path", optional_argument, nullptr, 'd'},
62       {"callback_socket_name", optional_argument, nullptr, 'c'},
63 #ifndef VTS_AGENT_DRIVER_COMM_BINDER  // socket
64       {"server_socket_path", optional_argument, NULL, 's'},
65 #else  // binder
66       {"service_name", required_argument, NULL, 'n'},
67 #endif
68       {nullptr, 0, nullptr, 0},
69   };
70 
71   string spec_dir_path = kDefaultSpecDirPath;
72   string callback_socket_name = "";
73   string server_socket_path;
74   string service_name;
75 
76   while (true) {
77     int opt = getopt_long(argc, argv, short_opts, long_opts, nullptr);
78     if (opt == -1) {
79       break;
80     }
81 
82     switch (opt) {
83       case 'h':
84       case '?':
85         ShowUsage();
86         return 0;
87       case 'd': {
88         spec_dir_path = string(optarg);
89         break;
90       }
91       case 'c': {
92         callback_socket_name = string(optarg);
93         break;
94       }
95 #ifndef VTS_AGENT_DRIVER_COMM_BINDER  // socket
96       case 's':
97         server_socket_path = string(optarg);
98         break;
99 #else  // binder
100       case 'n':
101         service_name = string(optarg);
102         break;
103 #endif
104       default:
105         cerr << "getopt_long returned unexpected value " << opt << endl;
106         return 2;
107     }
108   }
109 
110   android::vts::VtsResourceManager resource_manager;
111   android::vts::VtsHalDriverManager driver_manager(
112       spec_dir_path, kDefaultEpochCount, callback_socket_name,
113       &resource_manager);
114 
115 #ifndef VTS_AGENT_DRIVER_COMM_BINDER  // socket
116   android::vts::StartSocketServer(server_socket_path, &driver_manager,
117                                   &resource_manager, kInterfaceSpecLibName);
118 #else  // binder
119   android::vts::StartBinderServer(service_name, &driver_manager,
120                                   kInterfaceSpecLibName);
121 #endif
122 
123   return 0;
124 }
125