• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 <iostream>
17 
18 #include "begetctl.h"
19 #include "idevmgr_hdi.h"
20 #include "v1_0/ipartition_slot.h"
21 
22 using namespace OHOS::HDI::Partitionslot::V1_0;
23 using OHOS::HDI::DeviceManager::V1_0::IDeviceManager;
24 static const int32_t PARTITION_ARGC = 2;
25 
LoadDevice()26 static int LoadDevice()
27 {
28     auto devmgr = IDeviceManager::Get();
29     if (devmgr != nullptr) {
30         return devmgr->LoadDevice("partition_slot_service");
31     } else {
32         std::cout << "Load devmgr failed" << std::endl;
33         return -1;
34     }
35 }
36 
UnloadDevice()37 static void UnloadDevice()
38 {
39     auto devmgr = IDeviceManager::Get();
40     if (devmgr != nullptr) {
41         devmgr->UnloadDevice("partition_slot_service");
42     }
43 }
44 
GetSlot(BShellHandle handle,int32_t argc,char * argv[])45 static int GetSlot(BShellHandle handle, int32_t argc, char *argv[])
46 {
47     if (LoadDevice() != 0) {
48         return -1;
49     }
50     std::cout << "Command: partitionslot getslot" << std::endl;
51     sptr<IPartitionSlot> partitionslot = IPartitionSlot::Get();
52     if (partitionslot != nullptr) {
53         int bootSlots = 0;
54         int currentSlot = 0;
55         partitionslot->GetCurrentSlot(currentSlot, bootSlots);
56         std::cout << "The number of slots: " << bootSlots << "," <<  "current slot: " << currentSlot << std::endl;
57     }
58     UnloadDevice();
59     return 0;
60 }
61 
GetSuffix(BShellHandle handle,int32_t argc,char * argv[])62 static int GetSuffix(BShellHandle handle, int32_t argc, char *argv[])
63 {
64     if (argc != PARTITION_ARGC) {
65         BShellCmdHelp(handle, argc, argv);
66         return -1;
67     }
68     if (LoadDevice() != 0) {
69         return -1;
70     }
71     std::cout << "Command: partitionslot getsuffix" << std::endl;
72     int slot = atoi(argv[1]);
73     sptr<IPartitionSlot> partitionslot = IPartitionSlot::Get();
74     if (partitionslot != nullptr) {
75         std::string suffix = "";
76         partitionslot->GetSlotSuffix(slot, suffix);
77         std::cout << "The slot " << slot << " matches with suffix: " << suffix << std::endl;
78     }
79     UnloadDevice();
80     return 0;
81 }
82 
SetActiveSlot(BShellHandle handle,int32_t argc,char * argv[])83 static int SetActiveSlot(BShellHandle handle, int32_t argc, char *argv[])
84 {
85     if (argc != PARTITION_ARGC) {
86         BShellCmdHelp(handle, argc, argv);
87         return -1;
88     }
89     if (LoadDevice() != 0) {
90         return -1;
91     }
92     std::cout << "Command: partitionslot setactive" << std::endl;
93     int slot = atoi(argv[1]);
94     sptr<IPartitionSlot> partitionslot = IPartitionSlot::Get();
95     if (partitionslot != nullptr) {
96         partitionslot->SetActiveSlot(slot);
97         std::cout << "Set active slot: " << slot << std::endl;
98     }
99     UnloadDevice();
100     return 0;
101 }
102 
SetUnbootSlot(BShellHandle handle,int32_t argc,char * argv[])103 static int SetUnbootSlot(BShellHandle handle, int32_t argc, char *argv[])
104 {
105     if (argc != PARTITION_ARGC) {
106         BShellCmdHelp(handle, argc, argv);
107         return -1;
108     }
109     if (LoadDevice() != 0) {
110         return -1;
111     }
112     std::cout << "Command: partitionslot setunboot" << std::endl;
113     int slot = atoi(argv[1]);
114     sptr<IPartitionSlot> partitionslot = IPartitionSlot::Get();
115     if (partitionslot != nullptr) {
116         partitionslot->SetSlotUnbootable(slot);
117         std::cout << "Set unboot slot: " << slot << std::endl;
118     }
119     UnloadDevice();
120     return 0;
121 }
122 
MODULE_CONSTRUCTOR(void)123 MODULE_CONSTRUCTOR(void)
124 {
125     CmdInfo infos[] = {
126         {
127             const_cast<char *>("partitionslot"), GetSlot,
128             const_cast<char *>("get the number of slots and current slot"),
129             const_cast<char *>("partitionslot getslot"), const_cast<char *>("partitionslot getslot")
130         },
131         {
132             const_cast<char *>("partitionslot"), GetSuffix,
133             const_cast<char *>("get suffix that matches with the slot"),
134             const_cast<char *>("partitionslot getsuffix [slot]"), const_cast<char *>("partitionslot getsuffix")
135         },
136         {
137             const_cast<char *>("partitionslot"), SetActiveSlot,
138             const_cast<char *>("set active slot"),
139             const_cast<char *>("partitionslot setactive [slot]"), const_cast<char *>("partitionslot setactive")
140         },
141         {
142             const_cast<char *>("partitionslot"), SetUnbootSlot,
143             const_cast<char *>("set unboot slot"),
144             const_cast<char *>("partitionslot setunboot [slot]"), const_cast<char *>("partitionslot setunboot")
145         }
146     };
147     for (size_t i = sizeof(infos) / sizeof(infos[0]); i > 0; i--) {
148         BShellEnvRegisterCmd(GetShellHandle(), &infos[i - 1]);
149     }
150 }
151