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 
20 namespace OHOS {
21 namespace UpdateEngine {
SysInstallerCallback(SysInstallerExecutorCallback & installCallback)22 SysInstallerCallback::SysInstallerCallback(SysInstallerExecutorCallback &installCallback)
23 {
24     sysInstallCallback_ = installCallback;
25 }
26 
OnUpgradeProgress(SysInstaller::UpdateStatus updateStatus,int percent,const std::string & resultMsg)27 void SysInstallerCallback::OnUpgradeProgress(SysInstaller::UpdateStatus updateStatus, int percent,
28     const std::string &resultMsg)
29 {
30     FIRMWARE_LOGI("sysInstallerCallback OnUpgradeProgress status %{public}d percent %{public}d", updateStatus, percent);
31     InstallProgress installProgress = {};
32     switch (updateStatus) {
33         case SysInstaller::UpdateStatus::UPDATE_STATE_INIT:
34         case SysInstaller::UpdateStatus::UPDATE_STATE_ONGOING:
35             installProgress.progress.status = UpgradeStatus::INSTALLING;
36             break;
37         case SysInstaller::UpdateStatus::UPDATE_STATE_SUCCESSFUL:
38             installProgress.progress.status = UpgradeStatus::INSTALL_SUCCESS;
39             break;
40         default:
41             installProgress.progress.status = UpgradeStatus::INSTALL_FAIL;
42             installProgress.errMsg.errorMessage = resultMsg;
43             break;
44     }
45 
46     installProgress.progress.percent = static_cast<uint32_t>(percent);
47     installProgress.errMsg.errorCode = CAST_INT(updateStatus);
48     if (sysInstallCallback_.onSysInstallerCallback == nullptr) {
49         FIRMWARE_LOGE("SysInstallerCallback OnUpgradeProgress onSysInstallerCallback is null");
50         return;
51     }
52     sysInstallCallback_.onSysInstallerCallback(installProgress);
53 }
54 } // namespace UpdateEngine
55 } // namespace OHOS
56