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 "slot_info/slot_info.h"
17
18 #include "log/log.h"
19 #ifdef UPDATER_AB_SUPPORT
20 #include "partitionslot_manager.h"
21 #endif
22
23 namespace Updater {
24 #ifndef UPDATER_AB_SUPPORT
GetPartitionSuffix(std::string & suffix)25 void GetPartitionSuffix(std::string &suffix)
26 {
27 suffix = "";
28 }
SetActiveSlot()29 void SetActiveSlot()
30 {
31 }
32 #else
33 void GetPartitionSuffix(std::string &suffix)
34 {
35 OHOS::HDI::Partitionslot::V1_0::PartitionSlotManager psMgr;
36 int32_t curSlot = -1;
37 int32_t numOfSlots = 0;
38 int32_t ret = psMgr.GetCurrentSlot(curSlot, numOfSlots);
39 LOG(INFO) << "Get slot info, curSlot: " << curSlot << "numOfSlots :" << numOfSlots;
40 if (ret != 0 || curSlot <= 0 || curSlot > 2 || numOfSlots != 2) { // 2: max slot num
41 suffix = "";
42 return;
43 }
44
45 int32_t updateSlot = curSlot == 1 ? 2 : 1;
46 ret = psMgr.GetSlotSuffix(updateSlot, suffix);
47 if (ret != 0) {
48 LOG(ERROR) << "Get slot suffix error, partitionPath: " << suffix;
49 suffix = "";
50 }
51 }
52
53 void SetActiveSlot()
54 {
55 OHOS::HDI::Partitionslot::V1_0::PartitionSlotManager psMgr;
56 int32_t curSlot = -1;
57 int32_t numOfSlots = 0;
58 int32_t ret = psMgr.GetCurrentSlot(curSlot, numOfSlots);
59 LOG(INFO) << "Get slot info, curSlot: " << curSlot << "numOfSlots :" << numOfSlots;
60 if (ret != 0 || curSlot <= 0 || curSlot > 2 || numOfSlots != 2) { // 2: max slot num
61 return;
62 }
63
64 int32_t activeSlot = curSlot == 1 ? 2 : 1;
65 ret = psMgr.SetActiveSlot(activeSlot);
66 if (ret != 0) {
67 LOG(ERROR) << "Set active slot error, slot: " << activeSlot;
68 }
69 }
70 #endif
71 } // Updater
72