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_sys_installer_callback.h"
17
18 #include "firmware_log.h"
19 #include "firmware_constant.h"
20
21 namespace OHOS {
22 namespace UpdateService {
SysInstallerCallback(SysInstallerExecutorCallback & installCallback)23 SysInstallerCallback::SysInstallerCallback(SysInstallerExecutorCallback &installCallback)
24 {
25 sysInstallCallback_ = installCallback;
26 }
27
OnUpgradeProgress(SysInstaller::UpdateStatus updateStatus,int percent,const std::string & resultMsg)28 void SysInstallerCallback::OnUpgradeProgress(SysInstaller::UpdateStatus updateStatus, int percent,
29 const std::string &resultMsg)
30 {
31 FIRMWARE_LOGI("sysInstallerCallback OnUpgradeProgress status %{public}d "
32 "percent %{public}d", updateStatus, percent);
33 InstallProgress installProgress = {};
34 switch (updateStatus) {
35 case SysInstaller::UpdateStatus::UPDATE_STATE_INIT:
36 case SysInstaller::UpdateStatus::UPDATE_STATE_ONGOING:
37 installProgress.progress.status = UpgradeStatus::INSTALLING;
38 break;
39 case SysInstaller::UpdateStatus::UPDATE_STATE_SUCCESSFUL:
40 installProgress.progress.status = UpgradeStatus::INSTALL_SUCCESS;
41 break;
42 default:
43 installProgress.progress.status = UpgradeStatus::INSTALL_FAIL;
44 installProgress.errMsg.errorMessage = resultMsg;
45 break;
46 }
47
48 installProgress.progress.percent = static_cast<uint32_t>(percent);
49 installProgress.errMsg.errorCode = CAST_INT(updateStatus);
50 if (sysInstallCallback_.onSysInstallerCallback == nullptr) {
51 FIRMWARE_LOGE("SysInstallerCallback OnUpgradeProgress onSysInstallerCallback is null");
52 return;
53 }
54 sysInstallCallback_.onSysInstallerCallback(installProgress);
55 }
56
OnUpgradeDealLen(SysInstaller::UpdateStatus updateStatus,int dealLen,const std::string & resultMsg)57 void SysInstallerCallback::OnUpgradeDealLen(SysInstaller::UpdateStatus updateStatus, int dealLen,
58 const std::string &resultMsg)
59 {
60 FIRMWARE_LOGI("sysInstallerCallback OnUpgradeDealLen status %{public}d "
61 "dealLen %{public}d", updateStatus, dealLen);
62 InstallProgress installProgress = {};
63 switch (updateStatus) {
64 case SysInstaller::UpdateStatus::UPDATE_STATE_INIT:
65 case SysInstaller::UpdateStatus::UPDATE_STATE_ONGOING:
66 installProgress.progress.status = UpgradeStatus::INSTALLING;
67 break;
68 case SysInstaller::UpdateStatus::UPDATE_STATE_SUCCESSFUL:
69 installProgress.progress.status = UpgradeStatus::INSTALL_SUCCESS;
70 installProgress.progress.percent = Firmware::ONE_HUNDRED;
71 break;
72 default:
73 installProgress.progress.status = UpgradeStatus::INSTALL_FAIL;
74 installProgress.errMsg.errorMessage = resultMsg;
75 break;
76 }
77
78 installProgress.errMsg.errorCode = CAST_INT(updateStatus);
79 installProgress.dealLen = dealLen;
80 if (sysInstallCallback_.onSysInstallerCallback == nullptr) {
81 FIRMWARE_LOGE("SysInstallerCallback OnUpgradeDealLen onSysInstallerCallback is null");
82 return;
83 }
84 sysInstallCallback_.onSysInstallerCallback(installProgress);
85 }
86 } // namespace UpdateService
87 } // namespace OHOS
88