blob: 79f70bb1f31176c3b0f4764e2c94630e66072d4b [file] [log] [blame]
tzaumin chiou056e8c22019-07-02 13:59:24 +08001// Copyright 2019 The Chromium OS Authors. All rights reserved.
tzaumincf3337b2018-08-23 16:49:52 +08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <memory>
6
7#include <brillo/flag_helper.h>
8#include <brillo/syslog_logging.h>
tzaumin chiou056e8c22019-07-02 13:59:24 +08009
tzaumincf3337b2018-08-23 16:49:52 +080010#include "utilities.h"
tzaumin chiou056e8c22019-07-02 13:59:24 +080011#include "usb_device.h"
12#include "model_one_device.h"
13#include "model_two_device.h"
14#include "target_device.h"
tzaumincf3337b2018-08-23 16:49:52 +080015
16namespace {
tzaumincf3337b2018-08-23 16:49:52 +080017/**
18 * @brief Configures the logging system.
19 * @param log_file Specifies the log file to redirect to or stdout for console
20 * output.
21 */
22void ConfigureLogging(std::string log_file) {
23 if (log_file.empty()) {
24 brillo::InitLog(brillo::InitFlags::kLogToSyslog |
25 brillo::InitFlags::kLogToStderrIfTty);
26 } else if (log_file == "stdout") {
27 logging::LoggingSettings logging_settings;
28 logging::InitLogging(logging_settings);
29 } else {
30 logging::LoggingSettings logging_settings;
31 logging_settings.logging_dest = logging::LOG_TO_FILE;
hscham5b1d92c2020-09-15 09:42:13 +090032#if BASE_VER < 780000
tzaumincf3337b2018-08-23 16:49:52 +080033 logging_settings.log_file = log_file.c_str();
hscham5b1d92c2020-09-15 09:42:13 +090034#else
35 logging_settings.log_file_path = log_file.c_str();
36#endif
tzaumincf3337b2018-08-23 16:49:52 +080037 logging_settings.lock_log = logging::DONT_LOCK_LOG_FILE;
38 logging::InitLogging(logging_settings);
39 }
40}
tzaumin chiou056e8c22019-07-02 13:59:24 +080041
42void LogStatus(AverStatus status) {
43 switch (status) {
44 case AverStatus::USB_HID_NOT_FOUND:
45 LOG(ERROR) << "Failed to open the hid device.";
46 break;
47 case AverStatus::USB_UVC_NOT_FOUND:
48 LOG(ERROR) << "Failed to open the uvc device.";
49 break;
50 case AverStatus::OPEN_FOLDER_PATH_FAILED:
51 LOG(ERROR) << "Failed to open file folder path.";
52 break;
53 case AverStatus::DEVICE_NOT_OPEN:
54 LOG(ERROR) << "Failed to open Video Device.";
55 break;
56 case AverStatus::IO_CONTROL_OPERATION_FAILED:
57 LOG(ERROR) << "Failed to do ioctl.";
58 break;
59 case AverStatus::DIR_CONTENT_ERR:
60 LOG(ERROR) << "Failed to get directory content.";
61 break;
62 case AverStatus::FW_ALREADY_UPDATE:
63 LOG(INFO) << "Firmware is up to date.";
64 break;
65 case AverStatus::READ_FW_TO_BUF_FAILED:
66 LOG(ERROR) << "Failed to read fimrware data.";
67 break;
68 case AverStatus::READ_COMPRESSED_FW_FAILED:
69 LOG(ERROR) << "Failed to read the compressed firmware.";
70 break;
71 case AverStatus::FAILED_CREATE_TMP_PATH:
72 LOG(ERROR) << "Failed to create the temp path.";
73 break;
74 case AverStatus::FAILED_EXTRACT_COMPRESSED_FW:
75 LOG(ERROR) << "Failed to extract the compressed firmware.";
76 break;
77 case AverStatus::FAILED_DELETE_FW:
78 LOG(ERROR) << "Failed to delete untar firmware.";
79 break;
80 default:
81 LOG(ERROR) << "Unexpected status.";
82 break;
83 }
tzaumincf3337b2018-08-23 16:49:52 +080084}
85
tzaumin chiou056e8c22019-07-02 13:59:24 +080086template <typename T, typename... Ts>
87std::unique_ptr<T> make_unique(Ts&& ... params) {
88 return std::unique_ptr<T>(new T(std::forward<Ts>(params)...));
89}
90} // namespace
91
92/**
93 * @brief main function.
94 * We restruct and modify our source codes :
95 * 1. Delete the source codes about VB342 FW update flow.
96 * 2. In order not to confuse functionality, change the category name :
97 * VideoDevice -> UsbDevice,
98 * CAM520Camera -> ModelOneDevice,
99 * FourKDevice -> ModelTwoDevice.
100 * 3. Add TargetDevice for present products & furture composite device product.
101 * 4. Remove product names from ModelOneDevice and ModelTwoDevice.
102 * 5. Simplify firmware update flow of two models.
103 * 6. Product ID from udev rule to verify which product hotplug-in.
104 * 7. Move uvc command from CAM520Camera to UsbDevice.
105 */
tzaumincf3337b2018-08-23 16:49:52 +0800106int main(int argc, char** argv) {
107 DEFINE_bool(update, false, "Perform firmware update.");
108 DEFINE_bool(force, false, "Force firmware update.");
109 DEFINE_bool(device_version, false, "Show firmware version in the device.");
110 DEFINE_bool(image_version, false, "Show firmware version in Chromebox stored position.");
111 DEFINE_bool(lock, true, "Make sure only 1 updater can be run during update");
tzaumin chiou056e8c22019-07-02 13:59:24 +0800112 DEFINE_uint64(which_device, 0, "Which aver product plug-in");
tzaumincf3337b2018-08-23 16:49:52 +0800113 DEFINE_string(log_to, "", "Specify log file to write messages to.");
114 brillo::FlagHelper::Init(argc, argv, "aver-updater");
115
116 ConfigureLogging(FLAGS_log_to);
117
118 if (FLAGS_lock && !LockUpdater()) {
119 LOG(ERROR) << "There is another aver-updater running. Exiting now...";
120 return 0;
121 }
122
tzaumin chiou056e8c22019-07-02 13:59:24 +0800123 AverStatus status = AverStatus::NO_ERROR;
tzaumincf3337b2018-08-23 16:49:52 +0800124
tzaumin chiou056e8c22019-07-02 13:59:24 +0800125 std::unique_ptr<TargetDevice> aver_device = make_unique<TargetDevice>();
tzaumincf3337b2018-08-23 16:49:52 +0800126
Tzaumin Chiou88aa4082019-11-18 09:17:02 +0800127 if (!aver_device->FindHidDevicesByPid(FLAGS_which_device)) {
tzaumin chiou056e8c22019-07-02 13:59:24 +0800128 LOG(ERROR) << "Failed to find product id.";
129 return 0;
130 }
131
Tzaumin Chiou88aa4082019-11-18 09:17:02 +0800132 if (!aver_device->AddDevice()) {
tzaumin chiou056e8c22019-07-02 13:59:24 +0800133 LOG(ERROR) << "No matching product.";
134 return 0;
135 }
136
137 status = aver_device->OpenDevice();
138 if (status != AverStatus::NO_ERROR) {
139 LogStatus(status);
140 return 1;
tzaumincf3337b2018-08-23 16:49:52 +0800141 }
142
143 if (FLAGS_device_version) {
144 std::string device_version;
tzaumin chiou056e8c22019-07-02 13:59:24 +0800145 status = aver_device->GetDeviceVersion(&device_version);
146 if (status != AverStatus::NO_ERROR) {
147 LogStatus(status);
148 return 1;
149 }
tzaumincf3337b2018-08-23 16:49:52 +0800150 LOG(INFO) << "Firmware version on the device:" << device_version;
151 }
152
153 if (FLAGS_image_version) {
154 std::string image_version;
tzaumin chiou056e8c22019-07-02 13:59:24 +0800155 status = aver_device->GetImageVersion(&image_version);
156 if (status != AverStatus::NO_ERROR) {
157 LogStatus(status);
158 return 1;
159 }
tzaumincf3337b2018-08-23 16:49:52 +0800160 LOG(INFO) << "Latest firmware available:" << image_version;
161 }
162
163 if (FLAGS_update) {
tzaumin chiou056e8c22019-07-02 13:59:24 +0800164 status = aver_device->IsDeviceUpToDate(FLAGS_force);
165 if (status != AverStatus::NO_ERROR) {
166 LogStatus(status);
167 return 1;
tzaumincf3337b2018-08-23 16:49:52 +0800168 }
169
170 LOG(INFO) << "Updating device.";
tzaumin chiou056e8c22019-07-02 13:59:24 +0800171 status = aver_device->PerformUpdate();
172 if (status != AverStatus::NO_ERROR) {
173 LogStatus(status);
174 return 1;
175 }
176 LOG(INFO) << "Done. Updated firmware successfully.";
tzaumincf3337b2018-08-23 16:49:52 +0800177 }
tzaumin chiou056e8c22019-07-02 13:59:24 +0800178 return 0;
hscham5b1d92c2020-09-15 09:42:13 +0900179}