blob: bf8766f173a63fe673bac8e1e76defe491b0ba03 [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
Kevin Lin7d809c92020-02-19 11:54:52 +080012#include <base/at_exit.h>
Yong Hong42dd1f82019-04-18 14:49:22 +080013#include <base/logging.h>
14#include <brillo/flag_helper.h>
15#include <brillo/syslog_logging.h>
16
17#include "hardware_verifier/cli.h"
Wei-Han Chen5af45462020-03-20 18:06:11 +080018#include "hardware_verifier/observer.h"
Yong Hong42dd1f82019-04-18 14:49:22 +080019
20namespace {
21
22enum ExitStatus {
23 kSuccess = EXIT_SUCCESS, // 0
24
25 // The verification report shows the device is not complicant.
26 kVerifiedFail = 1,
27
28 kUnknownError = 10,
29
30 // Some of the argument is invalid.
31 kInvalidArgument = 11,
32};
33
34// Translate the error code from |hardware_verifier::CLI::Run()| to
35// the corresponding return code.
36ExitStatus ConvertCLIVerificationResultToExitStatus(
37 const hardware_verifier::CLIVerificationResult& verification_result) {
38 using hardware_verifier::CLIVerificationResult;
39 switch (verification_result) {
40 case CLIVerificationResult::kPass:
41 return ExitStatus::kSuccess;
Clark Chung1000c382020-03-13 11:44:27 +080042 case CLIVerificationResult::kFail:
43 return ExitStatus::kVerifiedFail;
Yong Hong42dd1f82019-04-18 14:49:22 +080044 case CLIVerificationResult::kInvalidHwVerificationSpecFile:
45 case CLIVerificationResult::kInvalidProbeResultFile:
46 return ExitStatus::kInvalidArgument;
47 default:
48 return ExitStatus::kUnknownError;
49 }
50}
51
52int SafeConvertVerbosityFlagToLogLevel(int verbosity_flag) {
53 if (verbosity_flag < 0 || 5 < verbosity_flag) {
54 LOG(ERROR) << "The verbosity value (" << verbosity_flag
55 << ") is out of range.";
56 exit(EX_USAGE);
57 }
58 // We would like to print |VLOG(K)| messages if the verbosity level is `K`.
59 // Since the corresponding log level is `-K`, we can resolve the correct
60 // log level by simply taking a negative sign.
61 return -verbosity_flag;
62}
63
64hardware_verifier::CLIOutputFormat SafeConvertOutputFormatFlagToEnum(
65 const std::string& output_format_flag) {
66 if (output_format_flag == "proto") {
67 return hardware_verifier::CLIOutputFormat::kProtoBin;
68 }
69 if (output_format_flag == "text") {
Yong Hong85a4dff2019-05-25 04:22:47 +080070 return hardware_verifier::CLIOutputFormat::kText;
Yong Hong42dd1f82019-04-18 14:49:22 +080071 }
72 LOG(ERROR) << "The output format (" << output_format_flag << ") is invalid.";
73 exit(EX_USAGE);
74}
75
76} // namespace
77
78int main(int argc, char* argv[]) {
Kevin Lin7d809c92020-02-19 11:54:52 +080079 // Required by dbus in libchrome.
80 base::AtExitManager at_exit_manager;
81
Yong Hong42dd1f82019-04-18 14:49:22 +080082 DEFINE_int32(verbosity, 0,
83 "Verbosity level, range from 0 to 5. The greater number is "
84 "set, the more detail messages will be printed.");
85 DEFINE_string(probe_result_file, "",
86 "File path to the probe result in prototxt format, empty to "
87 "get directly from |runtime_probe| D-Bus service.");
88 DEFINE_string(hw_verification_spec_file, "",
89 "File path to the hardware verification spec in prototxt "
90 "format, empty to use the default one.");
91 DEFINE_string(output_format, "proto",
92 "Format of the output verification report, can be either "
93 "\"proto\" for protobuf binary format or \"text\" for human "
94 "readable text format.");
Wei-Han Chend21ec072020-01-14 20:01:42 +080095 DEFINE_bool(send_to_uma, false, "Send data to UMA.");
Yong Hong42dd1f82019-04-18 14:49:22 +080096 brillo::FlagHelper::Init(argc, argv, "ChromeOS Hardware Verifier Tool");
97
98 brillo::InitLog(brillo::kLogToSyslog | brillo::kLogToStderr);
99
100 // Validate the non-trivial flags and convert them to the proper value types.
101 const auto log_level = SafeConvertVerbosityFlagToLogLevel(FLAGS_verbosity);
102 const auto output_format =
103 SafeConvertOutputFormatFlagToEnum(FLAGS_output_format);
104
105 logging::SetMinLogLevel(log_level);
106
Wei-Han Chen5af45462020-03-20 18:06:11 +0800107 std::unique_ptr<hardware_verifier::Observer> observer;
Wei-Han Chend21ec072020-01-14 20:01:42 +0800108
109 if (FLAGS_send_to_uma) {
Wei-Han Chen5af45462020-03-20 18:06:11 +0800110 observer->SetMetricsLibrary(std::make_unique<MetricsLibrary>());
Wei-Han Chend21ec072020-01-14 20:01:42 +0800111 }
Wei-Han Chen5af45462020-03-20 18:06:11 +0800112 observer->StartTimer(hardware_verifier::kMetricTimeToFinish);
Yong Hong42dd1f82019-04-18 14:49:22 +0800113 // TODO(yhong): Add the D-Bus service mode.
114
115 hardware_verifier::CLI cli;
Wei-Han Chen9a4f7592020-01-21 16:23:27 +0800116 const auto cli_result =
117 cli.Run(FLAGS_probe_result_file, FLAGS_hw_verification_spec_file,
Wei-Han Chen5af45462020-03-20 18:06:11 +0800118 output_format, observer.get());
Wei-Han Chend21ec072020-01-14 20:01:42 +0800119
120 const auto exit_status = ConvertCLIVerificationResultToExitStatus(cli_result);
121
Wei-Han Chen5af45462020-03-20 18:06:11 +0800122 observer->StopTimer(hardware_verifier::kMetricTimeToFinish);
Wei-Han Chend21ec072020-01-14 20:01:42 +0800123 return exit_status;
Yong Hong42dd1f82019-04-18 14:49:22 +0800124}