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
22using namespace OHOS::HDI::Partitionslot::V1_0;
23using OHOS::HDI::DeviceManager::V1_0::IDeviceManager;
24static const int32_t PARTITION_ARGC = 2;
25
26static 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
37static void UnloadDevice()
38{
39    auto devmgr = IDeviceManager::Get();
40    if (devmgr != nullptr) {
41        devmgr->UnloadDevice("partition_slot_service");
42    }
43}
44
45static 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
62static 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
83static 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#ifndef STARTUP_INIT_TEST
97        partitionslot->SetActiveSlot(slot);
98#endif
99        std::cout << "Set active slot: " << slot << std::endl;
100    }
101    UnloadDevice();
102    return 0;
103}
104
105static int SetUnbootSlot(BShellHandle handle, int32_t argc, char *argv[])
106{
107    if (argc != PARTITION_ARGC) {
108        BShellCmdHelp(handle, argc, argv);
109        return -1;
110    }
111    if (LoadDevice() != 0) {
112        return -1;
113    }
114    std::cout << "Command: partitionslot setunboot" << std::endl;
115    int slot = atoi(argv[1]);
116    sptr<IPartitionSlot> partitionslot = IPartitionSlot::Get();
117    if (partitionslot != nullptr) {
118        partitionslot->SetSlotUnbootable(slot);
119        std::cout << "Set unboot slot: " << slot << std::endl;
120    }
121    UnloadDevice();
122    return 0;
123}
124
125MODULE_CONSTRUCTOR(void)
126{
127    CmdInfo infos[] = {
128        {
129            const_cast<char *>("partitionslot"), GetSlot,
130            const_cast<char *>("get the number of slots and current slot"),
131            const_cast<char *>("partitionslot getslot"), const_cast<char *>("partitionslot getslot")
132        },
133        {
134            const_cast<char *>("partitionslot"), GetSuffix,
135            const_cast<char *>("get suffix that matches with the slot"),
136            const_cast<char *>("partitionslot getsuffix [slot]"), const_cast<char *>("partitionslot getsuffix")
137        },
138        {
139            const_cast<char *>("partitionslot"), SetActiveSlot,
140            const_cast<char *>("set active slot"),
141            const_cast<char *>("partitionslot setactive [slot]"), const_cast<char *>("partitionslot setactive")
142        },
143        {
144            const_cast<char *>("partitionslot"), SetUnbootSlot,
145            const_cast<char *>("set unboot slot"),
146            const_cast<char *>("partitionslot setunboot [slot]"), const_cast<char *>("partitionslot setunboot")
147        }
148    };
149    for (size_t i = sizeof(infos) / sizeof(infos[0]); i > 0; i--) {
150        BShellEnvRegisterCmd(GetShellHandle(), &infos[i - 1]);
151    }
152}
153