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 }
GetActivePartitionSuffix(std::string & suffix)29 void GetActivePartitionSuffix(std::string &suffix)
30 {
31 suffix = "";
32 }
SetActiveSlot()33 void SetActiveSlot()
34 {
35 }
36 #else
37 void GetPartitionSuffix(std::string &suffix)
38 {
39 OHOS::HDI::Partitionslot::V1_0::PartitionSlotManager psMgr;
40 int32_t curSlot = -1;
41 int32_t numOfSlots = 0;
42 int32_t ret = psMgr.GetCurrentSlot(curSlot, numOfSlots);
43 LOG(INFO) << "Get slot info, curSlot: " << curSlot << "numOfSlots :" << numOfSlots;
44 if (ret != 0 || curSlot <= 0 || curSlot > 2 || numOfSlots != 2) { // 2: max slot num
45 suffix = "";
46 return;
47 }
48
49 int32_t updateSlot = curSlot == 1 ? 2 : 1;
50 ret = psMgr.GetSlotSuffix(updateSlot, suffix);
51 if (ret != 0) {
52 LOG(ERROR) << "Get slot suffix error, partitionPath: " << suffix;
53 suffix = "";
54 }
55 }
56
57 void GetActivePartitionSuffix(std::string &suffix)
58 {
59 OHOS::HDI::Partitionslot::V1_0::PartitionSlotManager psMgr;
60 int32_t curSlot = -1;
61 int32_t numOfSlots = 0;
62 int32_t ret = psMgr.GetCurrentSlot(curSlot, numOfSlots);
63 LOG(INFO) << "Get slot info, curSlot: " << curSlot << "numOfSlots :" << numOfSlots;
64 if (ret != 0 || curSlot <= 0 || curSlot > 2 || numOfSlots != 2) { // 2: max slot num
65 suffix = "";
66 return;
67 }
68
69 ret = psMgr.GetSlotSuffix(curSlot, suffix);
70 if (ret != 0) {
71 LOG(ERROR) << "Get slot suffix error, partitionPath: " << suffix;
72 suffix = "";
73 }
74 }
75
76 void SetActiveSlot()
77 {
78 OHOS::HDI::Partitionslot::V1_0::PartitionSlotManager psMgr;
79 int32_t curSlot = -1;
80 int32_t numOfSlots = 0;
81 int32_t ret = psMgr.GetCurrentSlot(curSlot, numOfSlots);
82 LOG(INFO) << "Get slot info, curSlot: " << curSlot << "numOfSlots :" << numOfSlots;
83 if (ret != 0 || curSlot <= 0 || curSlot > 2 || numOfSlots != 2) { // 2: max slot num
84 return;
85 }
86
87 int32_t activeSlot = curSlot == 1 ? 2 : 1;
88 ret = psMgr.SetActiveSlot(activeSlot);
89 if (ret != 0) {
90 LOG(ERROR) << "Set active slot error, slot: " << activeSlot;
91 }
92 }
93 #endif
94 } // Updater
95