tzaumin chiou | 056e8c2 | 2019-07-02 13:59:24 +0800 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS Authors. All rights reserved. |
tzaumin | cf3337b | 2018-08-23 16:49:52 +0800 | [diff] [blame] | 2 | // 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 chiou | 056e8c2 | 2019-07-02 13:59:24 +0800 | [diff] [blame] | 9 | |
tzaumin | cf3337b | 2018-08-23 16:49:52 +0800 | [diff] [blame] | 10 | #include "utilities.h" |
tzaumin chiou | 056e8c2 | 2019-07-02 13:59:24 +0800 | [diff] [blame] | 11 | #include "usb_device.h" |
| 12 | #include "model_one_device.h" |
| 13 | #include "model_two_device.h" |
| 14 | #include "target_device.h" |
tzaumin | cf3337b | 2018-08-23 16:49:52 +0800 | [diff] [blame] | 15 | |
| 16 | namespace { |
tzaumin | cf3337b | 2018-08-23 16:49:52 +0800 | [diff] [blame] | 17 | /** |
| 18 | * @brief Configures the logging system. |
| 19 | * @param log_file Specifies the log file to redirect to or stdout for console |
| 20 | * output. |
| 21 | */ |
| 22 | void 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; |
hscham | 5b1d92c | 2020-09-15 09:42:13 +0900 | [diff] [blame] | 32 | #if BASE_VER < 780000 |
tzaumin | cf3337b | 2018-08-23 16:49:52 +0800 | [diff] [blame] | 33 | logging_settings.log_file = log_file.c_str(); |
hscham | 5b1d92c | 2020-09-15 09:42:13 +0900 | [diff] [blame] | 34 | #else |
| 35 | logging_settings.log_file_path = log_file.c_str(); |
| 36 | #endif |
tzaumin | cf3337b | 2018-08-23 16:49:52 +0800 | [diff] [blame] | 37 | logging_settings.lock_log = logging::DONT_LOCK_LOG_FILE; |
| 38 | logging::InitLogging(logging_settings); |
| 39 | } |
| 40 | } |
tzaumin chiou | 056e8c2 | 2019-07-02 13:59:24 +0800 | [diff] [blame] | 41 | |
| 42 | void 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 | } |
tzaumin | cf3337b | 2018-08-23 16:49:52 +0800 | [diff] [blame] | 84 | } |
| 85 | |
tzaumin chiou | 056e8c2 | 2019-07-02 13:59:24 +0800 | [diff] [blame] | 86 | template <typename T, typename... Ts> |
| 87 | std::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 | */ |
tzaumin | cf3337b | 2018-08-23 16:49:52 +0800 | [diff] [blame] | 106 | int 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 chiou | 056e8c2 | 2019-07-02 13:59:24 +0800 | [diff] [blame] | 112 | DEFINE_uint64(which_device, 0, "Which aver product plug-in"); |
tzaumin | cf3337b | 2018-08-23 16:49:52 +0800 | [diff] [blame] | 113 | 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 chiou | 056e8c2 | 2019-07-02 13:59:24 +0800 | [diff] [blame] | 123 | AverStatus status = AverStatus::NO_ERROR; |
tzaumin | cf3337b | 2018-08-23 16:49:52 +0800 | [diff] [blame] | 124 | |
tzaumin chiou | 056e8c2 | 2019-07-02 13:59:24 +0800 | [diff] [blame] | 125 | std::unique_ptr<TargetDevice> aver_device = make_unique<TargetDevice>(); |
tzaumin | cf3337b | 2018-08-23 16:49:52 +0800 | [diff] [blame] | 126 | |
Tzaumin Chiou | 88aa408 | 2019-11-18 09:17:02 +0800 | [diff] [blame] | 127 | if (!aver_device->FindHidDevicesByPid(FLAGS_which_device)) { |
tzaumin chiou | 056e8c2 | 2019-07-02 13:59:24 +0800 | [diff] [blame] | 128 | LOG(ERROR) << "Failed to find product id."; |
| 129 | return 0; |
| 130 | } |
| 131 | |
Tzaumin Chiou | 88aa408 | 2019-11-18 09:17:02 +0800 | [diff] [blame] | 132 | if (!aver_device->AddDevice()) { |
tzaumin chiou | 056e8c2 | 2019-07-02 13:59:24 +0800 | [diff] [blame] | 133 | 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; |
tzaumin | cf3337b | 2018-08-23 16:49:52 +0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | if (FLAGS_device_version) { |
| 144 | std::string device_version; |
tzaumin chiou | 056e8c2 | 2019-07-02 13:59:24 +0800 | [diff] [blame] | 145 | status = aver_device->GetDeviceVersion(&device_version); |
| 146 | if (status != AverStatus::NO_ERROR) { |
| 147 | LogStatus(status); |
| 148 | return 1; |
| 149 | } |
tzaumin | cf3337b | 2018-08-23 16:49:52 +0800 | [diff] [blame] | 150 | LOG(INFO) << "Firmware version on the device:" << device_version; |
| 151 | } |
| 152 | |
| 153 | if (FLAGS_image_version) { |
| 154 | std::string image_version; |
tzaumin chiou | 056e8c2 | 2019-07-02 13:59:24 +0800 | [diff] [blame] | 155 | status = aver_device->GetImageVersion(&image_version); |
| 156 | if (status != AverStatus::NO_ERROR) { |
| 157 | LogStatus(status); |
| 158 | return 1; |
| 159 | } |
tzaumin | cf3337b | 2018-08-23 16:49:52 +0800 | [diff] [blame] | 160 | LOG(INFO) << "Latest firmware available:" << image_version; |
| 161 | } |
| 162 | |
| 163 | if (FLAGS_update) { |
tzaumin chiou | 056e8c2 | 2019-07-02 13:59:24 +0800 | [diff] [blame] | 164 | status = aver_device->IsDeviceUpToDate(FLAGS_force); |
| 165 | if (status != AverStatus::NO_ERROR) { |
| 166 | LogStatus(status); |
| 167 | return 1; |
tzaumin | cf3337b | 2018-08-23 16:49:52 +0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | LOG(INFO) << "Updating device."; |
tzaumin chiou | 056e8c2 | 2019-07-02 13:59:24 +0800 | [diff] [blame] | 171 | status = aver_device->PerformUpdate(); |
| 172 | if (status != AverStatus::NO_ERROR) { |
| 173 | LogStatus(status); |
| 174 | return 1; |
| 175 | } |
| 176 | LOG(INFO) << "Done. Updated firmware successfully."; |
tzaumin | cf3337b | 2018-08-23 16:49:52 +0800 | [diff] [blame] | 177 | } |
tzaumin chiou | 056e8c2 | 2019-07-02 13:59:24 +0800 | [diff] [blame] | 178 | return 0; |
hscham | 5b1d92c | 2020-09-15 09:42:13 +0900 | [diff] [blame] | 179 | } |