• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_install.h"
17 
18 #include <dirent.h>
19 #include <iostream>
20 #include <fstream>
21 #include <string>
22 #include <unistd.h>
23 #include <sys/stat.h>
24 #include <sys/statfs.h>
25 
26 #include "dupdate_errno.h"
27 #include "firmware_log.h"
28 #include "file_utils.h"
29 
30 namespace OHOS {
31 namespace UpdateService {
StartInstall(const std::vector<FirmwareComponent> & componentList,FirmwareInstallCallback & cb)32 void FirmwareInstall::StartInstall(const std::vector<FirmwareComponent> &componentList, FirmwareInstallCallback &cb)
33 {
34     if (IsInstalling()) {
35         FIRMWARE_LOGE("install is busying");
36         CallbackFailedResult(cb, "install is busying", DUPDATE_ERR_SYSTEM_BUSY_ON_INSTALL);
37         return;
38     }
39 
40     if (!IsComponentLegal(componentList)) {
41         FIRMWARE_LOGE("check component failed!");
42         CallbackFailedResult(cb, "check component failed!", DUPDATE_ERR_UPDATE_PRECHECK_FAIL);
43         return;
44     }
45 
46     SetIsInstalling(true);
47     onInstallCallback_ = cb;
48     UpgradeStatus status;
49     bool result = PerformInstall(componentList, status);
50     SetIsInstalling(false);
51     CallbackResult(cb, result, status);
52 }
53 
IsInstalling()54 bool FirmwareInstall::IsInstalling()
55 {
56     std::lock_guard<std::mutex> lock(mutex_);
57     return isInstalling_;
58 }
59 
SetIsInstalling(bool isInstalling)60 void FirmwareInstall::SetIsInstalling(bool isInstalling)
61 {
62     std::lock_guard<std::mutex> lock(mutex_);
63     isInstalling_ = isInstalling;
64 }
65 
CallbackFailedResult(FirmwareInstallCallback & cb,const std::string & errorMsg,int32_t errCode)66 void FirmwareInstall::CallbackFailedResult(FirmwareInstallCallback &cb, const std::string &errorMsg, int32_t errCode)
67 {
68     ErrorMessage errMsg;
69     errMsg.errorCode = errCode;
70     errMsg.errorMessage = errorMsg;
71     cb.onFirmwareEvent(false, errMsg, UpgradeStatus::INSTALL_FAIL);
72 }
73 
CallbackResult(FirmwareInstallCallback & cb,bool result,UpgradeStatus status)74 void FirmwareInstall::CallbackResult(FirmwareInstallCallback &cb, bool result, UpgradeStatus status)
75 {
76     cb.onFirmwareEvent(result, errMsg_, status);
77 }
78 } // namespace UpdateService
79 } // namespace OHOS
80