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 <errno.h>
16#include <unistd.h>
17
18#include "init_log.h"
19#include "init_param.h"
20#include "init_utils.h"
21#include "param_manager.h"
22#ifdef PARAM_LOAD_CFG_FROM_CODE
23#include "param_cfg.h"
24#endif
25#ifdef __LITEOS_M__
26#include "ohos_init.h"
27#include "cmsis_os2.h"
28#include "parameter.h"
29#endif
30
31#ifdef PARAM_LOAD_CFG_FROM_CODE
32static const char *StringTrim(char *buffer, int size, const char *name)
33{
34    char *tmp = (char *)name;
35    while (*tmp != '\0' && *tmp != '"') {
36        tmp++;
37    }
38    if (*tmp == '\0') {
39        return name;
40    }
41    // skip "
42    tmp++;
43    int i = 0;
44    while (*tmp != '\0' && i < size) {
45        buffer[i++] = *tmp;
46        tmp++;
47    }
48    if (i >= size) {
49        return name;
50    }
51    while (i > 0) {
52        if (buffer[i] == '"') {
53            buffer[i] = '\0';
54            return buffer;
55        }
56        i--;
57    }
58    return name;
59}
60#endif
61
62int InitParamService(void)
63{
64    PARAM_LOGI("InitParamService %s", DATA_PATH);
65    CheckAndCreateDir(PARAM_STORAGE_PATH "/");
66    CheckAndCreateDir(DATA_PATH);
67    // param space
68    int ret = InitParamWorkSpace(0, NULL);
69    PARAM_CHECK(ret == 0, return ret, "Init parameter workspace fail");
70    ret = InitPersistParamWorkSpace();
71    PARAM_CHECK(ret == 0, return ret, "Init persist parameter workspace fail");
72
73    // from build
74    LoadParamFromBuild();
75#ifdef PARAM_LOAD_CFG_FROM_CODE
76    char *buffer = calloc(1, PARAM_VALUE_LEN_MAX);
77    PARAM_CHECK(buffer != NULL, return ret, "Failed to malloc for buffer");
78    for (size_t i = 0; i < ARRAY_LENGTH(g_paramDefCfgNodes); i++) {
79        PARAM_LOGV("InitParamService name %s = %s", g_paramDefCfgNodes[i].name, g_paramDefCfgNodes[i].value);
80        uint32_t dataIndex = 0;
81        ret = WriteParam(g_paramDefCfgNodes[i].name,
82            StringTrim(buffer, PARAM_VALUE_LEN_MAX, g_paramDefCfgNodes[i].value), &dataIndex, 0);
83        PARAM_CHECK(ret == 0, continue, "Failed to set param %d name %s %s",
84            ret, g_paramDefCfgNodes[i].name, g_paramDefCfgNodes[i].value);
85    }
86    free(buffer);
87#endif
88
89    return 0;
90}
91
92int StartParamService(void)
93{
94    return 0;
95}
96
97void StopParamService(void)
98{
99    PARAM_LOGI("StopParamService.");
100    ClosePersistParamWorkSpace();
101    CloseParamWorkSpace();
102}
103
104int SystemWriteParam(const char *name, const char *value)
105{
106    int ctrlService = 0;
107    int ret = CheckParameterSet(name, value, GetParamSecurityLabel(), &ctrlService);
108    PARAM_CHECK(ret == 0, return ret, "Forbid to set parameter %s", name);
109    PARAM_LOGV("SystemWriteParam name %s value: %s ctrlService %d", name, value, ctrlService);
110    if ((ctrlService & PARAM_CTRL_SERVICE) != PARAM_CTRL_SERVICE) { // ctrl param
111        uint32_t dataIndex = 0;
112        ret = WriteParam(name, value, &dataIndex, 0);
113        PARAM_CHECK(ret == 0, return ret, "Failed to set param %d name %s %s", ret, name, value);
114        ret = WritePersistParam(name, value);
115        PARAM_CHECK(ret == 0, return ret, "Failed to set persist param name %s", name);
116    } else {
117        PARAM_LOGE("SystemWriteParam can not support service ctrl parameter name %s", name);
118    }
119    return ret;
120}
121
122#ifdef __LITEOS_M__
123#define OS_DELAY 1000 // * 30 // 30s
124#define STACK_SIZE 1024
125
126static void ParamServiceTask(int *arg)
127{
128    (void)arg;
129    PARAM_LOGI("ParamServiceTask start");
130    while (1) {
131        CheckAndSavePersistParam();
132        osDelay(OS_DELAY);
133    }
134}
135
136void LiteParamService(void)
137{
138    static int init = 0;
139    if (init) {
140        return;
141    }
142    init = 1;
143    EnableInitLog(INIT_INFO);
144    InitParamService();
145    // get persist param
146    LoadPersistParams();
147}
148CORE_INIT(LiteParamService);
149#endif