1d9f0492fSopenharmony_ci/*
2d9f0492fSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3d9f0492fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4d9f0492fSopenharmony_ci * you may not use this file except in compliance with the License.
5d9f0492fSopenharmony_ci * You may obtain a copy of the License at
6d9f0492fSopenharmony_ci *
7d9f0492fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8d9f0492fSopenharmony_ci *
9d9f0492fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10d9f0492fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11d9f0492fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12d9f0492fSopenharmony_ci * See the License for the specific language governing permissions and
13d9f0492fSopenharmony_ci * limitations under the License.
14d9f0492fSopenharmony_ci */
15d9f0492fSopenharmony_ci#include <fcntl.h>
16d9f0492fSopenharmony_ci#include <pthread.h>
17d9f0492fSopenharmony_ci#include <string.h>
18d9f0492fSopenharmony_ci#include <sys/time.h>
19d9f0492fSopenharmony_ci
20d9f0492fSopenharmony_ci#include "begetctl.h"
21d9f0492fSopenharmony_ci#include "init_param.h"
22d9f0492fSopenharmony_ci#include "param_init.h"
23d9f0492fSopenharmony_ci#include "init_utils.h"
24d9f0492fSopenharmony_ci#include "loop_event.h"
25d9f0492fSopenharmony_ci#include "parameter.h"
26d9f0492fSopenharmony_ci#include "plugin_test.h"
27d9f0492fSopenharmony_ci#include "service_watcher.h"
28d9f0492fSopenharmony_ci#include "parameter.h"
29d9f0492fSopenharmony_ci#include "param_base.h"
30d9f0492fSopenharmony_ci
31d9f0492fSopenharmony_ci#define MAX_THREAD_NUMBER 100
32d9f0492fSopenharmony_ci#define MAX_NUMBER 10
33d9f0492fSopenharmony_ci#define READ_DURATION 100000
34d9f0492fSopenharmony_ci#define CMD_INDEX 2
35d9f0492fSopenharmony_ci
36d9f0492fSopenharmony_cistatic char *GetLocalBuffer(uint32_t *buffSize)
37d9f0492fSopenharmony_ci{
38d9f0492fSopenharmony_ci    static char buffer[PARAM_NAME_LEN_MAX + PARAM_CONST_VALUE_LEN_MAX] = {0};
39d9f0492fSopenharmony_ci    if (buffSize != NULL) {
40d9f0492fSopenharmony_ci        *buffSize = sizeof(buffer);
41d9f0492fSopenharmony_ci    }
42d9f0492fSopenharmony_ci    return buffer;
43d9f0492fSopenharmony_ci}
44d9f0492fSopenharmony_ci
45d9f0492fSopenharmony_ciint g_stop = 0;
46d9f0492fSopenharmony_cistatic void *CmdReader(void *args)
47d9f0492fSopenharmony_ci{
48d9f0492fSopenharmony_ci    (void)srand((unsigned)time(NULL));
49d9f0492fSopenharmony_ci    uint32_t buffSize = 0;
50d9f0492fSopenharmony_ci    char *buffer = GetLocalBuffer(&buffSize);
51d9f0492fSopenharmony_ci    while (g_stop == 0) {
52d9f0492fSopenharmony_ci        int wait = READ_DURATION + READ_DURATION;  // 100ms rand
53d9f0492fSopenharmony_ci        uint32_t size = buffSize;
54d9f0492fSopenharmony_ci        int ret = SystemGetParameter("test.randrom.read", buffer, &size);
55d9f0492fSopenharmony_ci        if (ret == 0) {
56d9f0492fSopenharmony_ci            printf("SystemGetParameter value %s %d \n", buffer, wait);
57d9f0492fSopenharmony_ci        } else {
58d9f0492fSopenharmony_ci            printf("SystemGetParameter fail %d \n", wait);
59d9f0492fSopenharmony_ci        }
60d9f0492fSopenharmony_ci        usleep(wait);
61d9f0492fSopenharmony_ci    }
62d9f0492fSopenharmony_ci    return NULL;
63d9f0492fSopenharmony_ci}
64d9f0492fSopenharmony_ci
65d9f0492fSopenharmony_cistatic int32_t BShellParamCmdRead(BShellHandle shell, int32_t argc, char *argv[])
66d9f0492fSopenharmony_ci{
67d9f0492fSopenharmony_ci    PLUGIN_CHECK(argc > 1, return -1, "Invalid parameter");
68d9f0492fSopenharmony_ci    static pthread_t thread = 0;
69d9f0492fSopenharmony_ci    PLUGIN_LOGV("BShellParamCmdWatch %s, threadId %d", argv[1], thread);
70d9f0492fSopenharmony_ci    if (strcmp(argv[1], "start") == 0) {
71d9f0492fSopenharmony_ci        if (thread != 0) {
72d9f0492fSopenharmony_ci            return 0;
73d9f0492fSopenharmony_ci        }
74d9f0492fSopenharmony_ci        SystemSetParameter("test.randrom.read.start", "1");
75d9f0492fSopenharmony_ci        pthread_create(&thread, NULL, CmdReader, argv[1]);
76d9f0492fSopenharmony_ci    } else if (strcmp(argv[1], "stop") == 0) {
77d9f0492fSopenharmony_ci        if (thread == 0) {
78d9f0492fSopenharmony_ci            return 0;
79d9f0492fSopenharmony_ci        }
80d9f0492fSopenharmony_ci        SystemSetParameter("test.randrom.read.start", "0");
81d9f0492fSopenharmony_ci        g_stop = 1;
82d9f0492fSopenharmony_ci        pthread_join(thread, NULL);
83d9f0492fSopenharmony_ci        thread = 0;
84d9f0492fSopenharmony_ci    }
85d9f0492fSopenharmony_ci    return 0;
86d9f0492fSopenharmony_ci}
87d9f0492fSopenharmony_ci
88d9f0492fSopenharmony_citypedef struct {
89d9f0492fSopenharmony_ci    char name[PARAM_NAME_LEN_MAX];
90d9f0492fSopenharmony_ci    pthread_t thread;
91d9f0492fSopenharmony_ci} TestWatchContext;
92d9f0492fSopenharmony_ci
93d9f0492fSopenharmony_cistatic void HandleParamChange2(const char *key, const char *value, void *context)
94d9f0492fSopenharmony_ci{
95d9f0492fSopenharmony_ci    PLUGIN_CHECK(key != NULL && value != NULL, return, "Invalid parameter");
96d9f0492fSopenharmony_ci    long long commit = GetSystemCommitId();
97d9f0492fSopenharmony_ci    size_t index = (size_t)context;
98d9f0492fSopenharmony_ci    printf("[%zu] Receive parameter commit %lld change %s %s \n", index, commit, key, value);
99d9f0492fSopenharmony_ci    static int addWatcher = 0;
100d9f0492fSopenharmony_ci    int ret = 0;
101d9f0492fSopenharmony_ci    if ((index == 4) && !addWatcher) { // 4 when context == 4 add
102d9f0492fSopenharmony_ci        index = 5; // 5 add context
103d9f0492fSopenharmony_ci        ret = SystemWatchParameter(key, HandleParamChange2, (void *)index);
104d9f0492fSopenharmony_ci        if (ret != 0) {
105d9f0492fSopenharmony_ci            printf("Add watcher %s fail %zu \n", key, index);
106d9f0492fSopenharmony_ci        }
107d9f0492fSopenharmony_ci        addWatcher = 1;
108d9f0492fSopenharmony_ci        return;
109d9f0492fSopenharmony_ci    }
110d9f0492fSopenharmony_ci    if (index == 2) { // 2 when context == 2 delete 3
111d9f0492fSopenharmony_ci        index = 3; // 3 delete context
112d9f0492fSopenharmony_ci        RemoveParameterWatcher(key, HandleParamChange2, (void *)index);
113d9f0492fSopenharmony_ci        if (ret != 0) {
114d9f0492fSopenharmony_ci            printf("Remove watcher fail %zu  \n", index);
115d9f0492fSopenharmony_ci        }
116d9f0492fSopenharmony_ci        return;
117d9f0492fSopenharmony_ci    }
118d9f0492fSopenharmony_ci    if (index == 1) { // 1 when context == 1 delete 1
119d9f0492fSopenharmony_ci        RemoveParameterWatcher(key, HandleParamChange2, (void *)index);
120d9f0492fSopenharmony_ci        if (ret != 0) {
121d9f0492fSopenharmony_ci            printf("Remove watcher fail %zu  \n", index);
122d9f0492fSopenharmony_ci        }
123d9f0492fSopenharmony_ci        return;
124d9f0492fSopenharmony_ci    }
125d9f0492fSopenharmony_ci    if ((index == 5) && (addWatcher == 1)) {  // 5 when context == 5 delete 5
126d9f0492fSopenharmony_ci        RemoveParameterWatcher(key, HandleParamChange2, (void *)index);
127d9f0492fSopenharmony_ci        if (ret != 0) {
128d9f0492fSopenharmony_ci            printf("Remove watcher fail %zu  \n", index);
129d9f0492fSopenharmony_ci        }
130d9f0492fSopenharmony_ci        addWatcher = 0;
131d9f0492fSopenharmony_ci    }
132d9f0492fSopenharmony_ci}
133d9f0492fSopenharmony_ci
134d9f0492fSopenharmony_cistatic void HandleParamChange1(const char *key, const char *value, void *context)
135d9f0492fSopenharmony_ci{
136d9f0492fSopenharmony_ci    PLUGIN_CHECK(key != NULL && value != NULL, return, "Invalid parameter");
137d9f0492fSopenharmony_ci    long long commit = GetSystemCommitId();
138d9f0492fSopenharmony_ci    size_t index = (size_t)context;
139d9f0492fSopenharmony_ci    printf("[%zu] Receive parameter commit %lld change %s %s \n", index, commit, key, value);
140d9f0492fSopenharmony_ci}
141d9f0492fSopenharmony_ci
142d9f0492fSopenharmony_cistatic void *CmdThreadWatcher(void *args)
143d9f0492fSopenharmony_ci{
144d9f0492fSopenharmony_ci    TestWatchContext *context = (TestWatchContext *)args;
145d9f0492fSopenharmony_ci    for (size_t i = 1; i <= MAX_NUMBER; i++) {
146d9f0492fSopenharmony_ci        int ret = SystemWatchParameter(context->name, HandleParamChange2, (void *)i);
147d9f0492fSopenharmony_ci        if (ret != 0) {
148d9f0492fSopenharmony_ci            printf("Add watcher %s fail %zu \n", context->name, i);
149d9f0492fSopenharmony_ci        }
150d9f0492fSopenharmony_ci        ret = SetParameter(context->name, context->name);
151d9f0492fSopenharmony_ci        if (ret != 0) {
152d9f0492fSopenharmony_ci            printf("Set parameter %s fail %zu \n", context->name, i);
153d9f0492fSopenharmony_ci        }
154d9f0492fSopenharmony_ci    }
155d9f0492fSopenharmony_ci    sleep(1);
156d9f0492fSopenharmony_ci    for (size_t i = 1; i <= MAX_NUMBER; i++) {
157d9f0492fSopenharmony_ci        int ret = RemoveParameterWatcher(context->name, HandleParamChange2, (void *)i);
158d9f0492fSopenharmony_ci        if (ret != 0) {
159d9f0492fSopenharmony_ci            printf("Remove watcher %s fail %zu \n", context->name, i);
160d9f0492fSopenharmony_ci        }
161d9f0492fSopenharmony_ci    }
162d9f0492fSopenharmony_ci    free(context);
163d9f0492fSopenharmony_ci    return NULL;
164d9f0492fSopenharmony_ci}
165d9f0492fSopenharmony_ci
166d9f0492fSopenharmony_cistatic void StartWatcherInThread(const char *prefix)
167d9f0492fSopenharmony_ci{
168d9f0492fSopenharmony_ci    TestWatchContext *context[MAX_THREAD_NUMBER] = { NULL };
169d9f0492fSopenharmony_ci    for (size_t i = 0; i < MAX_THREAD_NUMBER; i++) {
170d9f0492fSopenharmony_ci        context[i] = calloc(1, sizeof(TestWatchContext));
171d9f0492fSopenharmony_ci        PLUGIN_CHECK(context[i] != NULL, return, "Failed to alloc context");
172d9f0492fSopenharmony_ci        int len = sprintf_s(context[i]->name, sizeof(context[i]->name), "%s.%zu", prefix, i);
173d9f0492fSopenharmony_ci        if (len > 0) {
174d9f0492fSopenharmony_ci            printf("Add watcher %s \n", context[i]->name);
175d9f0492fSopenharmony_ci            pthread_create(&context[i]->thread, NULL, CmdThreadWatcher, context[i]);
176d9f0492fSopenharmony_ci        }
177d9f0492fSopenharmony_ci    }
178d9f0492fSopenharmony_ci}
179d9f0492fSopenharmony_ci
180d9f0492fSopenharmony_cistatic void StartWatcher(const char *prefix)
181d9f0492fSopenharmony_ci{
182d9f0492fSopenharmony_ci    static size_t index = 0;
183d9f0492fSopenharmony_ci    int ret = SystemWatchParameter(prefix, HandleParamChange1, (void *)index);
184d9f0492fSopenharmony_ci    if (ret != 0) {
185d9f0492fSopenharmony_ci        printf("Add watcher %s fail \n", prefix);
186d9f0492fSopenharmony_ci        return;
187d9f0492fSopenharmony_ci    }
188d9f0492fSopenharmony_ci    index++;
189d9f0492fSopenharmony_ci}
190d9f0492fSopenharmony_ci
191d9f0492fSopenharmony_cistatic int32_t BShellParamCmdWatch(BShellHandle shell, int32_t argc, char *argv[])
192d9f0492fSopenharmony_ci{
193d9f0492fSopenharmony_ci    PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
194d9f0492fSopenharmony_ci    PLUGIN_LOGV("BShellParamCmdWatch %s", argv[1]);
195d9f0492fSopenharmony_ci    StartWatcher(argv[1]);
196d9f0492fSopenharmony_ci
197d9f0492fSopenharmony_ci    if (argc <= CMD_INDEX) {
198d9f0492fSopenharmony_ci        return 0;
199d9f0492fSopenharmony_ci    }
200d9f0492fSopenharmony_ci    if (strcmp(argv[CMD_INDEX], "thread") == 0) { // 2 cmd index
201d9f0492fSopenharmony_ci        StartWatcherInThread(argv[1]);
202d9f0492fSopenharmony_ci        return 0;
203d9f0492fSopenharmony_ci    }
204d9f0492fSopenharmony_ci
205d9f0492fSopenharmony_ci    int maxCount = StringToInt(argv[CMD_INDEX], -1); // 2 cmd index
206d9f0492fSopenharmony_ci    if (maxCount <= 0 || maxCount > 65535) { // 65535 max count
207d9f0492fSopenharmony_ci        PLUGIN_LOGE("Invalid input %s", argv[CMD_INDEX]);
208d9f0492fSopenharmony_ci        return 0;
209d9f0492fSopenharmony_ci    }
210d9f0492fSopenharmony_ci    uint32_t buffSize = 0;
211d9f0492fSopenharmony_ci    char *buffer = GetLocalBuffer(&buffSize);
212d9f0492fSopenharmony_ci    size_t count = 0;
213d9f0492fSopenharmony_ci    while (count < (size_t)maxCount) { // 100 max count
214d9f0492fSopenharmony_ci        int len = sprintf_s(buffer, buffSize, "%s.%zu", argv[1], count);
215d9f0492fSopenharmony_ci        PLUGIN_CHECK(len > 0, return -1, "Invalid buffer");
216d9f0492fSopenharmony_ci        buffer[len] = '\0';
217d9f0492fSopenharmony_ci        StartWatcher(buffer);
218d9f0492fSopenharmony_ci        count++;
219d9f0492fSopenharmony_ci    }
220d9f0492fSopenharmony_ci    return 0;
221d9f0492fSopenharmony_ci}
222d9f0492fSopenharmony_ci
223d9f0492fSopenharmony_cistatic int32_t BShellParamCmdInstall(BShellHandle shell, int32_t argc, char *argv[])
224d9f0492fSopenharmony_ci{
225d9f0492fSopenharmony_ci    PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
226d9f0492fSopenharmony_ci    PLUGIN_LOGV("BShellParamCmdInstall %s %s", argv[0], argv[1]);
227d9f0492fSopenharmony_ci    uint32_t buffSize = 0;
228d9f0492fSopenharmony_ci    char *buffer = GetLocalBuffer(&buffSize);
229d9f0492fSopenharmony_ci    int ret = sprintf_s(buffer, buffSize, "ohos.servicectrl.%s", argv[0]);
230d9f0492fSopenharmony_ci    PLUGIN_CHECK(ret > 0, return -1, "Invalid buffer");
231d9f0492fSopenharmony_ci    buffer[ret] = '\0';
232d9f0492fSopenharmony_ci    SystemSetParameter(buffer, argv[1]);
233d9f0492fSopenharmony_ci    return 0;
234d9f0492fSopenharmony_ci}
235d9f0492fSopenharmony_ci
236d9f0492fSopenharmony_cistatic int32_t BShellParamCmdDisplay(BShellHandle shell, int32_t argc, char *argv[])
237d9f0492fSopenharmony_ci{
238d9f0492fSopenharmony_ci    PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
239d9f0492fSopenharmony_ci    PLUGIN_LOGV("BShellParamCmdDisplay %s %s", argv[0], argv[1]);
240d9f0492fSopenharmony_ci    SystemSetParameter("ohos.servicectrl.display", argv[1]);
241d9f0492fSopenharmony_ci    return 0;
242d9f0492fSopenharmony_ci}
243d9f0492fSopenharmony_ci
244d9f0492fSopenharmony_civoid ServiceStatusChangeTest(const char *key, const ServiceInfo *status)
245d9f0492fSopenharmony_ci{
246d9f0492fSopenharmony_ci    PLUGIN_LOGI("group-test-stage3: wait service %s status: %d", key, status->status);
247d9f0492fSopenharmony_ci    if (status->status == SERVICE_READY || status->status == SERVICE_STARTED) {
248d9f0492fSopenharmony_ci        PLUGIN_LOGI("Service %s start work", key);
249d9f0492fSopenharmony_ci    }
250d9f0492fSopenharmony_ci}
251d9f0492fSopenharmony_ci
252d9f0492fSopenharmony_cistatic int32_t BShellParamCmdGroupTest(BShellHandle shell, int32_t argc, char *argv[])
253d9f0492fSopenharmony_ci{
254d9f0492fSopenharmony_ci    PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
255d9f0492fSopenharmony_ci    PLUGIN_LOGI("BShellParamCmdGroupTest %s stage: %s", argv[0], argv[1]);
256d9f0492fSopenharmony_ci    if (argc > 2 && strcmp(argv[1], "wait") == 0) {                  // 2 service name index
257d9f0492fSopenharmony_ci        PLUGIN_LOGI("group-test-stage3: wait service %s", argv[2]);  // 2 service name index
258d9f0492fSopenharmony_ci        ServiceWatchForStatus(argv[2], ServiceStatusChangeTest);     // 2 service name index
259d9f0492fSopenharmony_ci        LE_RunLoop(LE_GetDefaultLoop());
260d9f0492fSopenharmony_ci        LE_CloseLoop(LE_GetDefaultLoop());
261d9f0492fSopenharmony_ci    }
262d9f0492fSopenharmony_ci    return 0;
263d9f0492fSopenharmony_ci}
264d9f0492fSopenharmony_ci
265d9f0492fSopenharmony_cistatic int32_t BShellParamCmdUdidGet(BShellHandle shell, int32_t argc, char *argv[])
266d9f0492fSopenharmony_ci{
267d9f0492fSopenharmony_ci    PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
268d9f0492fSopenharmony_ci    PLUGIN_LOGI("BShellParamCmdUdidGet ");
269d9f0492fSopenharmony_ci    char localDeviceId[65] = {0};      // 65 udid len
270d9f0492fSopenharmony_ci    AclGetDevUdid(localDeviceId, 65);  // 65 udid len
271d9f0492fSopenharmony_ci    BShellEnvOutput(shell, "    udid: %s\r\n", localDeviceId);
272d9f0492fSopenharmony_ci    return 0;
273d9f0492fSopenharmony_ci}
274d9f0492fSopenharmony_ci
275d9f0492fSopenharmony_cistatic int CalcValue(const char *value)
276d9f0492fSopenharmony_ci{
277d9f0492fSopenharmony_ci    char *begin = (char *)value;
278d9f0492fSopenharmony_ci    while (*begin != '\0') {
279d9f0492fSopenharmony_ci        if (*begin == ' ') {
280d9f0492fSopenharmony_ci            begin++;
281d9f0492fSopenharmony_ci        } else {
282d9f0492fSopenharmony_ci            break;
283d9f0492fSopenharmony_ci        }
284d9f0492fSopenharmony_ci    }
285d9f0492fSopenharmony_ci    char *end = begin + strlen(begin);
286d9f0492fSopenharmony_ci    while (end > begin) {
287d9f0492fSopenharmony_ci        if (*end > '9' || *end < '0') {
288d9f0492fSopenharmony_ci            *end = '\0';
289d9f0492fSopenharmony_ci        }
290d9f0492fSopenharmony_ci        end--;
291d9f0492fSopenharmony_ci    }
292d9f0492fSopenharmony_ci    return StringToInt(begin, -1);
293d9f0492fSopenharmony_ci}
294d9f0492fSopenharmony_ci
295d9f0492fSopenharmony_cistatic int32_t BShellParamCmdMemGet(BShellHandle shell, int32_t argc, char *argv[])
296d9f0492fSopenharmony_ci{
297d9f0492fSopenharmony_ci    PLUGIN_CHECK(argc > 1, return -1, "Invalid parameter");
298d9f0492fSopenharmony_ci    uint32_t buffSize = 0;  // 1024 max buffer for decode
299d9f0492fSopenharmony_ci    char *buff = GetLocalBuffer(&buffSize);
300d9f0492fSopenharmony_ci    PLUGIN_CHECK(buff != NULL, return -1, "Failed to get local buffer");
301d9f0492fSopenharmony_ci    int ret = sprintf_s(buff, buffSize - 1, "/proc/%s/smaps", argv[1]);
302d9f0492fSopenharmony_ci    PLUGIN_CHECK(ret > 0, return -1, "Failed to format path %s", argv[1]);
303d9f0492fSopenharmony_ci    buff[ret] = '\0';
304d9f0492fSopenharmony_ci    char *realPath = GetRealPath(buff);
305d9f0492fSopenharmony_ci    PLUGIN_CHECK(realPath != NULL, return -1, "Failed to get real path");
306d9f0492fSopenharmony_ci    int all = 0;
307d9f0492fSopenharmony_ci    if (argc > 2 && strcmp(argv[2], "all") == 0) {  // 2 2 max arg
308d9f0492fSopenharmony_ci        all = 1;
309d9f0492fSopenharmony_ci    }
310d9f0492fSopenharmony_ci    FILE *fp = fopen(realPath, "r");
311d9f0492fSopenharmony_ci    free(realPath);
312d9f0492fSopenharmony_ci    int value = 0;
313d9f0492fSopenharmony_ci    while (fp != NULL && buff != NULL && fgets(buff, buffSize, fp) != NULL) {
314d9f0492fSopenharmony_ci        buff[buffSize - 1] = '\0';
315d9f0492fSopenharmony_ci        if (strncmp(buff, "Pss:", strlen("Pss:")) == 0) {
316d9f0492fSopenharmony_ci            int v = CalcValue(buff + strlen("Pss:"));
317d9f0492fSopenharmony_ci            if (all) {
318d9f0492fSopenharmony_ci                printf("Pss:     %d kb\n", v);
319d9f0492fSopenharmony_ci            }
320d9f0492fSopenharmony_ci            value += v;
321d9f0492fSopenharmony_ci        } else if (strncmp(buff, "SwapPss:", strlen("SwapPss:")) == 0) {
322d9f0492fSopenharmony_ci            int v = CalcValue(buff + strlen("SwapPss:"));
323d9f0492fSopenharmony_ci            if (all) {
324d9f0492fSopenharmony_ci                printf("SwapPss: %d kb\n", v);
325d9f0492fSopenharmony_ci            }
326d9f0492fSopenharmony_ci            value += v;
327d9f0492fSopenharmony_ci        }
328d9f0492fSopenharmony_ci    }
329d9f0492fSopenharmony_ci    if (fp != NULL) {
330d9f0492fSopenharmony_ci        fclose(fp);
331d9f0492fSopenharmony_ci        printf("Total mem %d kb\n", value);
332d9f0492fSopenharmony_ci    } else {
333d9f0492fSopenharmony_ci        printf("Failed to get memory for %s %s \n", argv[1], buff);
334d9f0492fSopenharmony_ci    }
335d9f0492fSopenharmony_ci    return 0;
336d9f0492fSopenharmony_ci}
337d9f0492fSopenharmony_ci
338d9f0492fSopenharmony_civoid CmdServiceStatusChange(const char *key, const ServiceInfo *status)
339d9f0492fSopenharmony_ci{
340d9f0492fSopenharmony_ci    static const char *serviceStatusMap[] = {
341d9f0492fSopenharmony_ci        "idle",
342d9f0492fSopenharmony_ci        "starting",
343d9f0492fSopenharmony_ci        "running",
344d9f0492fSopenharmony_ci        "ready",
345d9f0492fSopenharmony_ci        "stopping",
346d9f0492fSopenharmony_ci        "stopped",
347d9f0492fSopenharmony_ci        "error",
348d9f0492fSopenharmony_ci        "suspended",
349d9f0492fSopenharmony_ci        "freezed",
350d9f0492fSopenharmony_ci        "disabled",
351d9f0492fSopenharmony_ci        "critical",
352d9f0492fSopenharmony_ci    };
353d9f0492fSopenharmony_ci    PLUGIN_CHECK(key != NULL && status != NULL, return, "Invalid parameter");
354d9f0492fSopenharmony_ci    if (status->status == SERVICE_STARTED || status->status == SERVICE_READY) {
355d9f0492fSopenharmony_ci        printf("Service %s status: %s pid %d \n", key,
356d9f0492fSopenharmony_ci            ((status->status < ARRAY_LENGTH(serviceStatusMap)) ? serviceStatusMap[status->status] : "unknown"),
357d9f0492fSopenharmony_ci            status->pid);
358d9f0492fSopenharmony_ci    } else {
359d9f0492fSopenharmony_ci        printf("Service %s status: %s \n", key,
360d9f0492fSopenharmony_ci            (status->status < ARRAY_LENGTH(serviceStatusMap)) ? serviceStatusMap[status->status] : "unknown");
361d9f0492fSopenharmony_ci    }
362d9f0492fSopenharmony_ci}
363d9f0492fSopenharmony_ci
364d9f0492fSopenharmony_cistatic int32_t BShellParamCmdWatchService(BShellHandle shell, int32_t argc, char *argv[])
365d9f0492fSopenharmony_ci{
366d9f0492fSopenharmony_ci    PLUGIN_CHECK(argc > 1, return -1, "Invalid parameter");
367d9f0492fSopenharmony_ci    return ServiceWatchForStatus(argv[1], CmdServiceStatusChange);
368d9f0492fSopenharmony_ci}
369d9f0492fSopenharmony_ci
370d9f0492fSopenharmony_ciint32_t BShellCmdRegister(BShellHandle shell, int execMode)
371d9f0492fSopenharmony_ci{
372d9f0492fSopenharmony_ci    if (execMode == 0) {
373d9f0492fSopenharmony_ci        const CmdInfo infos[] = {
374d9f0492fSopenharmony_ci            {"init", BShellParamCmdGroupTest, "init group test", "init group test [stage]", "init group test"},
375d9f0492fSopenharmony_ci            {"display", BShellParamCmdMemGet, "display memory pid", "display memory [pid]", "display memory"},
376d9f0492fSopenharmony_ci        };
377d9f0492fSopenharmony_ci        for (size_t i = 0; i < sizeof(infos) / sizeof(infos[0]); i++) {
378d9f0492fSopenharmony_ci            BShellEnvRegisterCmd(shell, &infos[i]);
379d9f0492fSopenharmony_ci        }
380d9f0492fSopenharmony_ci    } else {
381d9f0492fSopenharmony_ci        const CmdInfo infos[] = {
382d9f0492fSopenharmony_ci            {"display", BShellParamCmdDisplay, "display system service", "display service", "display service"},
383d9f0492fSopenharmony_ci            {"read", BShellParamCmdRead, "read system parameter", "read [start | stop]", ""},
384d9f0492fSopenharmony_ci            {"watcher", BShellParamCmdWatch,
385d9f0492fSopenharmony_ci                "watcher system parameter", "watcher parameter [name]", "watcher parameter"},
386d9f0492fSopenharmony_ci            {"install", BShellParamCmdInstall, "install plugin", "install [name]", ""},
387d9f0492fSopenharmony_ci            {"uninstall", BShellParamCmdInstall, "uninstall plugin", "uninstall [name]", ""},
388d9f0492fSopenharmony_ci            {"group", BShellParamCmdGroupTest, "group test", "group test [stage]", "group test"},
389d9f0492fSopenharmony_ci            {"display", BShellParamCmdUdidGet, "display udid", "display udid", "display udid"},
390d9f0492fSopenharmony_ci            {"display", BShellParamCmdMemGet, "display memory pid", "display memory [pid]", "display memory"},
391d9f0492fSopenharmony_ci            {"watcher", BShellParamCmdWatchService,
392d9f0492fSopenharmony_ci                "watcher service status", "watcher service [name]", "watcher service"},
393d9f0492fSopenharmony_ci        };
394d9f0492fSopenharmony_ci        for (size_t i = 0; i < sizeof(infos) / sizeof(infos[0]); i++) {
395d9f0492fSopenharmony_ci            BShellEnvRegisterCmd(GetShellHandle(), &infos[i]);
396d9f0492fSopenharmony_ci        }
397d9f0492fSopenharmony_ci    }
398d9f0492fSopenharmony_ci    return 0;
399d9f0492fSopenharmony_ci}
400