1 /*
2 * Copyright (C) 2012 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 #include <cstdlib>
18 #include <getopt.h>
19 #include <cstring>
20 #include <iostream>
21
22 #include "TestSettings.h"
23
24 #include "TestForkerEventListener.h"
25
26 namespace android {
27 namespace camera2 {
28 namespace tests {
29
30 bool TestSettings::mForkingDisabled = false;
31 int TestSettings::mDeviceId = 0;
32 char* const* TestSettings::mArgv;
33
34 // --forking-disabled, false by default
ForkingDisabled()35 bool TestSettings::ForkingDisabled() {
36 return mForkingDisabled;
37 }
38
39 // reverse of --forking-disabled (not a flag), true by default
ForkingEnabled()40 bool TestSettings::ForkingEnabled() {
41 return !ForkingDisabled();
42 }
43
44 // --device-id, 0 by default
DeviceId()45 int TestSettings::DeviceId() {
46 return mDeviceId;
47 }
48
49 // returns false if usage should be printed and we should exit early
ParseArgs(int argc,char * const argv[])50 bool TestSettings::ParseArgs(int argc, char* const argv[])
51 {
52 {
53 char *env = getenv("CAMERA2_TEST_FORKING_DISABLED");
54 if (env) {
55 mForkingDisabled = atoi(env);
56 }
57
58 env = getenv("CAMERA2_TEST_DEVICE_ID");
59 if (env) {
60 mDeviceId = atoi(env);
61 }
62 }
63
64 bool printHelp = false;
65 bool unknownArgs = false;
66
67 opterr = 0; // do not print errors for unknown arguments
68 while (true) {
69 int c;
70 int option_index = 0;
71
72 static struct option long_options[] = {
73 /* name has_arg flag val */
74 {"forking-disabled", optional_argument, 0, 0 },
75 {"device-id", required_argument, 0, 0 },
76 {"help", no_argument, 0, 'h' },
77 {0, 0, 0, 0 }
78 };
79
80 // Note: '+' in optstring means do not mutate argv
81 c = getopt_long(argc, argv, "+h", long_options, &option_index);
82
83 if (c == -1) { // All arguments exhausted
84 break;
85 }
86 if (c == '?') { // Argument not in option lists
87 const char *arg = argv[optind-1];
88 // Anything beginning with gtest_ will get handled by gtest
89 if (strstr(arg, "--gtest_") != arg) {
90 std::cerr << "Unknown argument: " << arg << std::endl;
91 unknownArgs = true;
92 }
93 continue;
94 }
95
96 switch (c) {
97 case 0: // long option
98 switch (option_index) {
99 case 0: {
100 const char *arg = optarg ?: "1";
101 mForkingDisabled = atoi(arg);
102 break;
103 }
104 case 1: {
105 mDeviceId = atoi(optarg);
106 break;
107 }
108 default:
109 std::cerr << "Unknown long option: " << option_index << std::endl;
110 break;
111 }
112 break; // case 0
113 case 'h': // help
114 printHelp = true;
115 break;
116 default: // case '?'
117 std::cerr << "Unknown option: " << optarg << std::endl;
118 }
119 }
120
121 if (unknownArgs) {
122 std::cerr << std::endl;
123 }
124
125 mArgv = argv;
126
127 if (printHelp || unknownArgs) {
128 return false;
129 }
130
131 std::cerr << "Forking Disabled: "
132 << (mForkingDisabled ? "yes" : "no") << std::endl;
133
134 std::cerr << "Device ID: " << mDeviceId << std::endl;
135
136 return true;
137 }
138
139 // print usage/help list of commands (non-gtest)
PrintUsage()140 void TestSettings::PrintUsage() {
141 std::cerr << "Usage: " << mArgv[0] << " [OPTIONS]" << std::endl;
142 std::cerr << std::endl;
143
144 std::cerr << "Main modes of operation:"
145 << std::endl;
146 std::cerr << " --forking-disabled[=1] don't fork process before "
147 << std::endl
148 << " running a new test."
149 << std::endl
150 << " (default enabled)"
151 << std::endl;
152 std::cerr << " --device-id=ID specify a different camera ID"
153 << std::endl
154 << " (default 0)"
155 << std::endl;
156
157 std::cerr << " -h, --help print this help listing"
158 << std::endl;
159
160
161 std::cerr << std::endl;
162 }
163
164 }
165 }
166 }
167
168