blob: 9d045ed7c317d9cfd855d0eb597aafa1cd02e72e [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>
Wei-Han Chend21ec072020-01-14 20:01:42 +080010#include <memory>
Yong Hong42dd1f82019-04-18 14:49:22 +080011
12#include <base/logging.h>
13#include <brillo/flag_helper.h>
14#include <brillo/syslog_logging.h>
15
16#include "hardware_verifier/cli.h"
Wei-Han Chend21ec072020-01-14 20:01:42 +080017#include "hardware_verifier/metrics.h"
Yong Hong42dd1f82019-04-18 14:49:22 +080018
19namespace {
20
21enum ExitStatus {
22 kSuccess = EXIT_SUCCESS, // 0
23
24 // The verification report shows the device is not complicant.
25 kVerifiedFail = 1,
26
27 kUnknownError = 10,
28
29 // Some of the argument is invalid.
30 kInvalidArgument = 11,
31};
32
33// Translate the error code from |hardware_verifier::CLI::Run()| to
34// the corresponding return code.
35ExitStatus ConvertCLIVerificationResultToExitStatus(
36 const hardware_verifier::CLIVerificationResult& verification_result) {
37 using hardware_verifier::CLIVerificationResult;
38 switch (verification_result) {
39 case CLIVerificationResult::kPass:
40 return ExitStatus::kSuccess;
41 case CLIVerificationResult::kInvalidHwVerificationSpecFile:
42 case CLIVerificationResult::kInvalidProbeResultFile:
43 return ExitStatus::kInvalidArgument;
44 default:
45 return ExitStatus::kUnknownError;
46 }
47}
48
49int SafeConvertVerbosityFlagToLogLevel(int verbosity_flag) {
50 if (verbosity_flag < 0 || 5 < verbosity_flag) {
51 LOG(ERROR) << "The verbosity value (" << verbosity_flag
52 << ") is out of range.";
53 exit(EX_USAGE);
54 }
55 // We would like to print |VLOG(K)| messages if the verbosity level is `K`.
56 // Since the corresponding log level is `-K`, we can resolve the correct
57 // log level by simply taking a negative sign.
58 return -verbosity_flag;
59}
60
61hardware_verifier::CLIOutputFormat SafeConvertOutputFormatFlagToEnum(
62 const std::string& output_format_flag) {
63 if (output_format_flag == "proto") {
64 return hardware_verifier::CLIOutputFormat::kProtoBin;
65 }
66 if (output_format_flag == "text") {
Yong Hong85a4dff2019-05-25 04:22:47 +080067 return hardware_verifier::CLIOutputFormat::kText;
Yong Hong42dd1f82019-04-18 14:49:22 +080068 }
69 LOG(ERROR) << "The output format (" << output_format_flag << ") is invalid.";
70 exit(EX_USAGE);
71}
72
73} // namespace
74
75int main(int argc, char* argv[]) {
76 DEFINE_int32(verbosity, 0,
77 "Verbosity level, range from 0 to 5. The greater number is "
78 "set, the more detail messages will be printed.");
79 DEFINE_string(probe_result_file, "",
80 "File path to the probe result in prototxt format, empty to "
81 "get directly from |runtime_probe| D-Bus service.");
82 DEFINE_string(hw_verification_spec_file, "",
83 "File path to the hardware verification spec in prototxt "
84 "format, empty to use the default one.");
85 DEFINE_string(output_format, "proto",
86 "Format of the output verification report, can be either "
87 "\"proto\" for protobuf binary format or \"text\" for human "
88 "readable text format.");
Wei-Han Chend21ec072020-01-14 20:01:42 +080089 DEFINE_bool(send_to_uma, false, "Send data to UMA.");
Yong Hong42dd1f82019-04-18 14:49:22 +080090 brillo::FlagHelper::Init(argc, argv, "ChromeOS Hardware Verifier Tool");
91
92 brillo::InitLog(brillo::kLogToSyslog | brillo::kLogToStderr);
93
94 // Validate the non-trivial flags and convert them to the proper value types.
95 const auto log_level = SafeConvertVerbosityFlagToLogLevel(FLAGS_verbosity);
96 const auto output_format =
97 SafeConvertOutputFormatFlagToEnum(FLAGS_output_format);
98
99 logging::SetMinLogLevel(log_level);
100
Wei-Han Chend21ec072020-01-14 20:01:42 +0800101 std::unique_ptr<hardware_verifier::Metrics> metrics;
102
103 if (FLAGS_send_to_uma) {
104 metrics = std::make_unique<hardware_verifier::UMAMetrics>();
105 } else {
106 metrics = std::make_unique<hardware_verifier::DummyMetrics>();
107 }
108
109 metrics->StartTimer(hardware_verifier::kMetricTimeToFinish);
Yong Hong42dd1f82019-04-18 14:49:22 +0800110 // TODO(yhong): Add the D-Bus service mode.
111
112 hardware_verifier::CLI cli;
113 const auto cli_result = cli.Run(
114 FLAGS_probe_result_file, FLAGS_hw_verification_spec_file, output_format);
Wei-Han Chend21ec072020-01-14 20:01:42 +0800115
116 const auto exit_status = ConvertCLIVerificationResultToExitStatus(cli_result);
117
118 metrics->StopTimer(hardware_verifier::kMetricTimeToFinish);
119 return exit_status;
Yong Hong42dd1f82019-04-18 14:49:22 +0800120}