• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "update_service_ab_update.h"
17 
18 #include "parameters.h"
19 
20 namespace OHOS {
21 namespace UpdateEngine {
22 sptr<OHOS::SysInstaller::ISysInstallerCallback> UpdateServiceAbUpdate::cb_ = nullptr;
23 const std::string PARAM_NAME_FOR_BOOTSLOTS = "ohos.boot.bootslots";
24 const std::string BOOTSLOTS_DEFAULT_VALUE = "0";
25 const std::string BOOTSLOTS_AB_UPDATE_VALUE = "2";
26 
IsAbUpdate()27 bool UpdateServiceAbUpdate::IsAbUpdate()
28 {
29     std::string bootslots = OHOS::system::GetParameter(PARAM_NAME_FOR_BOOTSLOTS, BOOTSLOTS_DEFAULT_VALUE);
30     ENGINE_LOGI("bootslots is [%s]", bootslots.c_str());
31     return (bootslots == BOOTSLOTS_AB_UPDATE_VALUE);
32 }
33 
DoAbUpdate(const UpgradeInfo & info,const std::string & packageName)34 int32_t UpdateServiceAbUpdate::DoAbUpdate(const UpgradeInfo &info, const std::string &packageName)
35 {
36     if (!IsAbUpdate()) {
37         ENGINE_LOGE("IsAbUpdate false, UpdateServiceAbUpdate fail");
38         return INT_CALL_FAIL;
39     }
40 
41     int32_t ret = OHOS::SysInstaller::SysInstallerKitsImpl::GetInstance().SysInstallerInit();
42     if (ret != 0) {
43         ENGINE_LOGE("SysInstallerInit fail, ret = %d, UpdateServiceAbUpdate fail", ret);
44         return INT_CALL_FAIL;
45     }
46 
47     auto updateStatus = OHOS::SysInstaller::SysInstallerKitsImpl::GetInstance().GetUpdateStatus();
48     if (updateStatus != OHOS::SysInstaller::UPDATE_STATE_INIT &&
49         updateStatus != OHOS::SysInstaller::UPDATE_STATE_FAILED) {
50         ENGINE_LOGE("AbUpgrade process status is invalid, status is %d, UpdateServiceAbUpdate fail", updateStatus);
51         return INT_CALL_FAIL;
52     }
53 
54     if (cb_ == nullptr) {
55         cb_ = new UpdateServiceAbCallback(info);
56     }
57     ret = OHOS::SysInstaller::SysInstallerKitsImpl::GetInstance().SetUpdateCallback(cb_);
58     if (ret != 0) {
59         ENGINE_LOGE("SetUpdateCallback fail, ret = %d, UpdateServiceAbUpdate fail", ret);
60         return INT_CALL_FAIL;
61     }
62 
63     ret = OHOS::SysInstaller::SysInstallerKitsImpl::GetInstance().StartUpdatePackageZip(packageName);
64     if (ret != 0) {
65         ENGINE_LOGE("StartUpdatePackageZip fail, ret = %d, UpdateServiceAbUpdate fail", ret);
66         return INT_CALL_FAIL;
67     }
68     return INT_CALL_SUCCESS;
69 }
70 } // namespace UpdateEngine
71 } // namespace OHOS
72