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 #include <map>
17 
18 #include <android-base/logging.h>
19 #include <getopt.h>
20 
21 #include <android-base/logging.h>
22 #include "TcpServerForRunner.h"
23 
24 static constexpr const char* kDefaultSpecDirPath = "/data/local/tmp/spec/";
25 static constexpr const char* kDefaultHalDriverPath32 = "./vts_hal_driver32";
26 static constexpr const char* kDefaultHalDriverPath64 = "./vts_hal_driver64";
27 static constexpr const char* kDefaultShellDriverPath32 = "./vts_shell_driver32";
28 static constexpr const char* kDefaultShellDriverPath64 = "./vts_shell_driver64";
29 
30 using namespace std;
31 
ShowUsage()32 void ShowUsage() {
33   printf(
34       "Usage: vts_hal_agent [options]\n"
35       "--hal_driver_path_32:   Set 32 bit hal driver binary path.\n"
36       "--hal_driver_path_32:   Set 64 bit hal driver binary path.\n"
37       "--spec_dir:             Set vts spec directory. \n"
38       "--shell_driver_path_32: Set 32 bit shell driver binary path\n"
39       "--shell_driver_path_64: Set 64 bit shell driver binary path\n"
40       "--log_severity:         Set log severity\n"
41       "--help:                 Show help\n");
42   exit(1);
43 }
44 
main(int argc,char ** argv)45 int main(int argc, char** argv) {
46   string hal_driver_path32 = kDefaultHalDriverPath32;
47   string hal_driver_path64 = kDefaultHalDriverPath64;
48   string shell_driver_path32 = kDefaultShellDriverPath32;
49   string shell_driver_path64 = kDefaultShellDriverPath64;
50   string spec_dir_path = kDefaultSpecDirPath;
51   string log_severity = "INFO";
52 
53   enum {
54     HAL_DRIVER_PATH32 = 1000,
55     HAL_DRIVER_PATH64,
56     SHELL_DRIVER_PATH32,
57     SHELL_DRIVER_PATH64,
58     SPEC_DIR
59   };
60 
61   const char* const short_opts = "hl:";
62   const option long_opts[] = {
63       {"help", no_argument, nullptr, 'h'},
64       {"hal_driver_path_32", required_argument, nullptr, HAL_DRIVER_PATH32},
65       {"hal_driver_path_64", required_argument, nullptr, HAL_DRIVER_PATH64},
66       {"shell_driver_path_32", required_argument, nullptr, SHELL_DRIVER_PATH32},
67       {"shell_driver_path_64", required_argument, nullptr, SHELL_DRIVER_PATH64},
68       {"spec_dir", required_argument, nullptr, SPEC_DIR},
69       {"log_severity", required_argument, nullptr, 'l'},
70       {nullptr, 0, nullptr, 0},
71   };
72 
73   while (true) {
74     int opt = getopt_long(argc, argv, short_opts, long_opts, nullptr);
75     if (opt == -1) {
76       break;
77     }
78 
79     switch (opt) {
80       case 'h':
81       case '?':
82         ShowUsage();
83         return 0;
84       case 'l': {
85         log_severity = string(optarg);
86         break;
87       }
88       case HAL_DRIVER_PATH32: {
89         hal_driver_path32 = string(optarg);
90         break;
91       }
92       case HAL_DRIVER_PATH64: {
93         hal_driver_path64 = string(optarg);
94         break;
95       }
96       case SHELL_DRIVER_PATH32: {
97         shell_driver_path32 = string(optarg);
98         break;
99       }
100       case SHELL_DRIVER_PATH64: {
101         shell_driver_path64 = string(optarg);
102         break;
103       }
104       case SPEC_DIR: {
105         spec_dir_path = string(optarg);
106         break;
107       }
108       default:
109         printf("getopt_long returned unexpected value: %d\n", opt);
110         return 2;
111     }
112   }
113 
114   map<string, string> log_map = {
115       {"ERROR", "*:e"}, {"WARNING", "*:w"}, {"INFO", "*:i"}, {"DEBUG", "*:d"},
116   };
117 
118   if (log_map.find(log_severity) != log_map.end()) {
119     setenv("ANDROID_LOG_TAGS", log_map[log_severity].c_str(), 1);
120   }
121 
122   android::base::InitLogging(argv, android::base::StderrLogger);
123 
124   LOG(INFO) << "|| VTS AGENT ||";
125 
126   char* dir_path;
127   dir_path = (char*)malloc(strlen(argv[0]) + 1);
128   strcpy(dir_path, argv[0]);
129   for (int index = strlen(argv[0]) - 2; index >= 0; index--) {
130     if (dir_path[index] == '/') {
131       dir_path[index] = '\0';
132       break;
133     }
134   }
135   chdir(dir_path);
136 
137   android::vts::StartTcpServerForRunner(
138       spec_dir_path.c_str(), hal_driver_path32.c_str(),
139       hal_driver_path64.c_str(), shell_driver_path32.c_str(),
140       shell_driver_path64.c_str());
141   return 0;
142 }
143