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 <ctype.h>
16d9f0492fSopenharmony_ci#include <unistd.h>
17d9f0492fSopenharmony_ci#include <time.h>
18d9f0492fSopenharmony_ci#include <stdlib.h>
19d9f0492fSopenharmony_ci
20d9f0492fSopenharmony_ci#include "init_param.h"
21d9f0492fSopenharmony_ci#include "modulemgr.h"
22d9f0492fSopenharmony_ci#include "init_module_engine.h"
23d9f0492fSopenharmony_ci#include "init_cmds.h"
24d9f0492fSopenharmony_ci#include "securec.h"
25d9f0492fSopenharmony_ci
26d9f0492fSopenharmony_ci#define MAX_COUNT 1
27d9f0492fSopenharmony_ci#define TEST_CMD_NAME "param_randrom_write"
28d9f0492fSopenharmony_ci
29d9f0492fSopenharmony_ci#define READ_DURATION 100000
30d9f0492fSopenharmony_ci
31d9f0492fSopenharmony_cistatic int g_testCmdIndex = 0;
32d9f0492fSopenharmony_cistatic int PluginParamCmdWriteParam(int id, const char *name, int argc, const char **argv)
33d9f0492fSopenharmony_ci{
34d9f0492fSopenharmony_ci    if ((argv == NULL) || (argc < 1)) {
35d9f0492fSopenharmony_ci        return -1;
36d9f0492fSopenharmony_ci    }
37d9f0492fSopenharmony_ci    int maxCount = MAX_COUNT;
38d9f0492fSopenharmony_ci    if (argc > 1) {
39d9f0492fSopenharmony_ci        maxCount = atoi(argv[1]);
40d9f0492fSopenharmony_ci    }
41d9f0492fSopenharmony_ci    (void)srand((unsigned)time(NULL));
42d9f0492fSopenharmony_ci    char buffer[32] = { 0 }; // 32 buffer size
43d9f0492fSopenharmony_ci    int count = 0;
44d9f0492fSopenharmony_ci    while (count < maxCount) {
45d9f0492fSopenharmony_ci        const int wait = READ_DURATION + READ_DURATION; // 100ms
46d9f0492fSopenharmony_ci        int ret = sprintf_s(buffer, sizeof(buffer), "%d", count);
47d9f0492fSopenharmony_ci        if (ret > 0) {
48d9f0492fSopenharmony_ci            SystemWriteParam(argv[0], buffer);
49d9f0492fSopenharmony_ci            usleep(wait);
50d9f0492fSopenharmony_ci        }
51d9f0492fSopenharmony_ci        count++;
52d9f0492fSopenharmony_ci    }
53d9f0492fSopenharmony_ci    return 0;
54d9f0492fSopenharmony_ci}
55d9f0492fSopenharmony_ci
56d9f0492fSopenharmony_ciMODULE_CONSTRUCTOR(void)
57d9f0492fSopenharmony_ci{
58d9f0492fSopenharmony_ci    g_testCmdIndex = AddCareContextCmdExecutor(TEST_CMD_NAME, PluginParamCmdWriteParam);
59d9f0492fSopenharmony_ci}
60d9f0492fSopenharmony_ci
61d9f0492fSopenharmony_ciMODULE_DESTRUCTOR(void)
62d9f0492fSopenharmony_ci{
63d9f0492fSopenharmony_ci    RemoveCmdExecutor(TEST_CMD_NAME, g_testCmdIndex);
64d9f0492fSopenharmony_ci}
65