1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "firmware_check_executor.h"
17 
18 #include <thread>
19 
20 #include "config_parse.h"
21 #include "firmware_common.h"
22 #include "firmware_constant.h"
23 #include "firmware_file_utils.h"
24 #include "firmware_icheck.h"
25 #include "firmware_log.h"
26 #include "firmware_status_cache.h"
27 #include "firmware_update_helper.h"
28 
29 namespace OHOS {
30 namespace UpdateEngine {
Execute()31 void FirmwareCheckExecutor::Execute()
32 {
33     FIRMWARE_LOGI("FirmwareCheckExecutor::Execute");
34     DelayedSingleton<FirmwareStatusCache>::GetInstance()->SetIsChecking(true);
35     std::thread checkThread([this] { this->DoCheck(); });
36     checkThread.detach();
37 }
38 
Complete()39 void FirmwareCheckExecutor::Complete()
40 {
41     FIRMWARE_LOGI("FirmwareCheckExecutor::complete");
42     DelayedSingleton<FirmwareStatusCache>::GetInstance()->SetIsChecking(false);
43     if (checkComponentCallback_.firmwareComponentCallback == nullptr) {
44         FIRMWARE_LOGE("FirmwareCheckExecutor firmwareComponentCallback is null");
45         return;
46     }
47     checkComponentCallback_.firmwareComponentCallback(status_, duration_, componentList_, checkAndAuthInfo_);
48 }
49 
DoCheck()50 void FirmwareCheckExecutor::DoCheck()
51 {
52     FIRMWARE_LOGI("FirmwareCheckExecutor::DoCheck");
53     FirmwareCheckCallback cb{
54         [=](CheckStatus status, const Duration &duration, const std::vector<FirmwareComponent> &firmwareCheckResultList,
55             const CheckAndAuthInfo &checkAndAuthInfo) {
56         status_ = status;
57         duration_ = duration;
58         componentList_ = firmwareCheckResultList;
59         checkAndAuthInfo_ = checkAndAuthInfo;
60         FIRMWARE_LOGI("CheckComplete status: %{public}d size: %{public}d", status_, CAST_INT(componentList_.size()));
61         if (status_ == CheckStatus::CHECK_FAIL || componentList_.size() == 0) {
62             Complete();
63             return;
64         }
65         SetComponentCheckStatus();
66         Complete();
67     }};
68 
69     std::shared_ptr<FirmwareICheck> check = nullptr;
70     check = std::make_shared<FirmwareICheck>(RequestType::CHECK);
71     if (check == nullptr) {
72         FIRMWARE_LOGE("check is nullptr");
73         return;
74     }
75     check->DoAction(cb);
76 }
77 
SetComponentCheckStatus()78 void FirmwareCheckExecutor::SetComponentCheckStatus()
79 {
80     FIRMWARE_LOGI("FirmwareCheckExecutor::SetComponentCheckStatus");
81     for (auto &component : componentList_) {
82         component.status = UpgradeStatus::CHECK_VERSION_SUCCESS;
83     }
84 }
85 } // namespace UpdateEngine
86 } // namespace OHOS
87