blob: ed393e40cfd7a8141eb80b2b08e46702b7fb983b [file] [log] [blame]
Yong Hong42dd1f82019-04-18 14:49:22 +08001/* Copyright 2019 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
6#include <sysexits.h>
7
8#include <cstdlib>
9#include <iostream>
10
11#include <base/logging.h>
12#include <brillo/flag_helper.h>
13#include <brillo/syslog_logging.h>
14
15#include "hardware_verifier/cli.h"
16
17namespace {
18
19enum ExitStatus {
20 kSuccess = EXIT_SUCCESS, // 0
21
22 // The verification report shows the device is not complicant.
23 kVerifiedFail = 1,
24
25 kUnknownError = 10,
26
27 // Some of the argument is invalid.
28 kInvalidArgument = 11,
29};
30
31// Translate the error code from |hardware_verifier::CLI::Run()| to
32// the corresponding return code.
33ExitStatus ConvertCLIVerificationResultToExitStatus(
34 const hardware_verifier::CLIVerificationResult& verification_result) {
35 using hardware_verifier::CLIVerificationResult;
36 switch (verification_result) {
37 case CLIVerificationResult::kPass:
38 return ExitStatus::kSuccess;
39 case CLIVerificationResult::kInvalidHwVerificationSpecFile:
40 case CLIVerificationResult::kInvalidProbeResultFile:
41 return ExitStatus::kInvalidArgument;
42 default:
43 return ExitStatus::kUnknownError;
44 }
45}
46
47int SafeConvertVerbosityFlagToLogLevel(int verbosity_flag) {
48 if (verbosity_flag < 0 || 5 < verbosity_flag) {
49 LOG(ERROR) << "The verbosity value (" << verbosity_flag
50 << ") is out of range.";
51 exit(EX_USAGE);
52 }
53 // We would like to print |VLOG(K)| messages if the verbosity level is `K`.
54 // Since the corresponding log level is `-K`, we can resolve the correct
55 // log level by simply taking a negative sign.
56 return -verbosity_flag;
57}
58
59hardware_verifier::CLIOutputFormat SafeConvertOutputFormatFlagToEnum(
60 const std::string& output_format_flag) {
61 if (output_format_flag == "proto") {
62 return hardware_verifier::CLIOutputFormat::kProtoBin;
63 }
64 if (output_format_flag == "text") {
Yong Hong85a4dff2019-05-25 04:22:47 +080065 return hardware_verifier::CLIOutputFormat::kText;
Yong Hong42dd1f82019-04-18 14:49:22 +080066 }
67 LOG(ERROR) << "The output format (" << output_format_flag << ") is invalid.";
68 exit(EX_USAGE);
69}
70
71} // namespace
72
73int main(int argc, char* argv[]) {
74 DEFINE_int32(verbosity, 0,
75 "Verbosity level, range from 0 to 5. The greater number is "
76 "set, the more detail messages will be printed.");
77 DEFINE_string(probe_result_file, "",
78 "File path to the probe result in prototxt format, empty to "
79 "get directly from |runtime_probe| D-Bus service.");
80 DEFINE_string(hw_verification_spec_file, "",
81 "File path to the hardware verification spec in prototxt "
82 "format, empty to use the default one.");
83 DEFINE_string(output_format, "proto",
84 "Format of the output verification report, can be either "
85 "\"proto\" for protobuf binary format or \"text\" for human "
86 "readable text format.");
87 brillo::FlagHelper::Init(argc, argv, "ChromeOS Hardware Verifier Tool");
88
89 brillo::InitLog(brillo::kLogToSyslog | brillo::kLogToStderr);
90
91 // Validate the non-trivial flags and convert them to the proper value types.
92 const auto log_level = SafeConvertVerbosityFlagToLogLevel(FLAGS_verbosity);
93 const auto output_format =
94 SafeConvertOutputFormatFlagToEnum(FLAGS_output_format);
95
96 logging::SetMinLogLevel(log_level);
97
98 // TODO(yhong): Add the D-Bus service mode.
99
100 hardware_verifier::CLI cli;
101 const auto cli_result = cli.Run(
102 FLAGS_probe_result_file, FLAGS_hw_verification_spec_file, output_format);
103 return ConvertCLIVerificationResultToExitStatus(cli_result);
104}