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