1 /*
2 * Copyright (c) 2021-2022 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 "param_persist.h"
16
17 #include <time.h>
18
19 #include "init_param.h"
20 #include "init_utils.h"
21 #include "param_manager.h"
22 #include "param_trie.h"
23 #include "param_osadp.h"
24 #include "securec.h"
25
26 static ParamPersistWorkSpace g_persistWorkSpace = {0, 0, NULL, {0}, {0}};
IsNeedToSave(const char * name)27 static int IsNeedToSave(const char *name)
28 {
29 #if defined(__LITEOS_M__) || defined(__LITEOS_A__)
30 return IS_READY_ONLY(name) ? 0 : 1;
31 #else
32 return (strncmp(name, PARAM_PERSIST_PREFIX, strlen(PARAM_PERSIST_PREFIX)) == 0) ? 1 : 0;
33 #endif
34 }
35
GetPersistCommitId(void)36 static long long GetPersistCommitId(void)
37 {
38 ParamWorkSpace *paramSpace = GetParamWorkSpace();
39 PARAM_CHECK(paramSpace != NULL, return 0, "Invalid paramSpace");
40 PARAM_WORKSPACE_CHECK(paramSpace, return 0, "Invalid space");
41 WorkSpace *space = GetWorkSpace(WORKSPACE_INDEX_DAC);
42 if (space == NULL) {
43 return 0;
44 }
45 PARAMSPACE_AREA_RD_LOCK(space);
46 long long globalCommitId = ATOMIC_UINT64_LOAD_EXPLICIT(&space->area->commitPersistId, MEMORY_ORDER_ACQUIRE);
47 PARAMSPACE_AREA_RW_UNLOCK(space);
48 return globalCommitId;
49 }
50
UpdatePersistCommitId(void)51 static void UpdatePersistCommitId(void)
52 {
53 ParamWorkSpace *paramSpace = GetParamWorkSpace();
54 PARAM_CHECK(paramSpace != NULL, return, "Invalid paramSpace");
55 PARAM_WORKSPACE_CHECK(paramSpace, return, "Invalid space");
56 WorkSpace *space = GetWorkSpace(WORKSPACE_INDEX_DAC);
57 if (space == NULL) {
58 return;
59 }
60 PARAMSPACE_AREA_RW_LOCK(space);
61 ATOMIC_SYNC_ADD_AND_FETCH(&space->area->commitPersistId, 1, MEMORY_ORDER_RELEASE);
62 PARAMSPACE_AREA_RW_UNLOCK(space);
63 }
64
SavePersistParam(const WorkSpace * workSpace,const ParamTrieNode * node,const void * cookie)65 static int SavePersistParam(const WorkSpace *workSpace, const ParamTrieNode *node, const void *cookie)
66 {
67 ParamTrieNode *current = (ParamTrieNode *)node;
68 if (current == NULL || current->dataIndex == 0) {
69 return 0;
70 }
71 ParamNode *entry = (ParamNode *)GetTrieNode(workSpace, current->dataIndex);
72 if (entry == NULL) {
73 return 0;
74 }
75
76 if (!IsNeedToSave(entry->data)) {
77 return 0;
78 }
79 static char name[PARAM_NAME_LEN_MAX] = {0};
80 int ret = memcpy_s(name, PARAM_NAME_LEN_MAX - 1, entry->data, entry->keyLength);
81 PARAM_CHECK(ret == EOK, return -1, "Failed to read param name %s", entry->data);
82 name[entry->keyLength] = '\0';
83 ret = g_persistWorkSpace.persistParamOps.batchSave(
84 (PERSIST_SAVE_HANDLE)cookie, name, entry->data + entry->keyLength + 1);
85 PARAM_CHECK(ret == 0, return -1, "Failed to write param %s", current->key);
86 return ret;
87 }
88
BatchSavePersistParam(void)89 static int BatchSavePersistParam(void)
90 {
91 PARAM_LOGV("BatchSavePersistParam");
92 if (g_persistWorkSpace.persistParamOps.batchSaveBegin == NULL ||
93 g_persistWorkSpace.persistParamOps.batchSave == NULL ||
94 g_persistWorkSpace.persistParamOps.batchSaveEnd == NULL) {
95 return 0;
96 }
97
98 PERSIST_SAVE_HANDLE handle;
99 int ret = g_persistWorkSpace.persistParamOps.batchSaveBegin(&handle);
100 PARAM_CHECK(ret == 0, return PARAM_CODE_INVALID_NAME, "Failed to save persist");
101 #if defined(__LITEOS_M__) || defined(__LITEOS_A__)
102 const char *prefix = "";
103 #else
104 const char *prefix = PARAM_PERSIST_PREFIX;
105 #endif
106 // walk and save persist param
107 WorkSpace *workSpace = GetNextWorkSpace(NULL);
108 while (workSpace != NULL) {
109 WorkSpace *next = GetNextWorkSpace(workSpace);
110 ParamTrieNode *root = FindTrieNode(workSpace, prefix, strlen(prefix), NULL);
111 PARAMSPACE_AREA_RD_LOCK(workSpace);
112 TraversalTrieNode(workSpace, root, SavePersistParam, (void *)handle);
113 PARAMSPACE_AREA_RW_UNLOCK(workSpace);
114 workSpace = next;
115 }
116 g_persistWorkSpace.persistParamOps.batchSaveEnd(handle);
117 PARAM_CHECK(ret == 0, return PARAM_CODE_INVALID_NAME, "Save persist param fail");
118
119 PARAM_CLEAR_FLAG(g_persistWorkSpace.flags, WORKSPACE_FLAGS_UPDATE);
120 (void)clock_gettime(CLOCK_MONOTONIC, &g_persistWorkSpace.lastSaveTimer);
121 return ret;
122 }
123
InitPersistParamWorkSpace(void)124 INIT_LOCAL_API int InitPersistParamWorkSpace(void)
125 {
126 if (PARAM_TEST_FLAG(g_persistWorkSpace.flags, WORKSPACE_FLAGS_INIT)) {
127 return 0;
128 }
129 (void)clock_gettime(CLOCK_MONOTONIC, &g_persistWorkSpace.lastSaveTimer);
130 RegisterPersistParamOps(&g_persistWorkSpace.persistParamOps);
131 PARAM_SET_FLAG(g_persistWorkSpace.flags, WORKSPACE_FLAGS_INIT);
132 return 0;
133 }
134
ClosePersistParamWorkSpace(void)135 INIT_LOCAL_API void ClosePersistParamWorkSpace(void)
136 {
137 if (g_persistWorkSpace.saveTimer != NULL) {
138 ParamTimerClose(g_persistWorkSpace.saveTimer);
139 }
140 g_persistWorkSpace.flags = 0;
141 }
142
CheckAndSavePersistParam(void)143 void CheckAndSavePersistParam(void)
144 {
145 // check commit
146 long long commit = GetPersistCommitId();
147 PARAM_LOGV("CheckAndSavePersistParam commit %lld %lld", commit, g_persistWorkSpace.commitId);
148 if (g_persistWorkSpace.commitId == commit) {
149 return;
150 }
151 g_persistWorkSpace.commitId = commit;
152 (void)BatchSavePersistParam();
153 }
154
TimerCallbackForSave(const ParamTaskPtr timer,void * context)155 PARAM_STATIC void TimerCallbackForSave(const ParamTaskPtr timer, void *context)
156 {
157 UNUSED(context);
158 UNUSED(timer);
159 PARAM_LOGV("TimerCallbackForSave ");
160 // for liteos-a must cycle check
161 #if (!defined(PARAM_SUPPORT_CYCLE_CHECK) || defined(PARAM_SUPPORT_REAL_CHECK))
162 ParamTimerClose(g_persistWorkSpace.saveTimer);
163 g_persistWorkSpace.saveTimer = NULL;
164 if (!PARAM_TEST_FLAG(g_persistWorkSpace.flags, WORKSPACE_FLAGS_UPDATE)) {
165 return;
166 }
167 #endif
168 CheckAndSavePersistParam();
169 }
170
WritePersistParam(const char * name,const char * value)171 INIT_LOCAL_API int WritePersistParam(const char *name, const char *value)
172 {
173 PARAM_CHECK(value != NULL && name != NULL, return PARAM_CODE_INVALID_PARAM, "Invalid param");
174 if (!IsNeedToSave(name)) {
175 return 0;
176 }
177 PARAM_LOGV("WritePersistParam name %s ", name);
178 if (g_persistWorkSpace.persistParamOps.save != NULL) {
179 g_persistWorkSpace.persistParamOps.save(name, value);
180 }
181 // update commit for check
182 UpdatePersistCommitId();
183 // for liteos-m, start task to check and save parameter
184 // for linux, start timer after set persist parameter
185 // for liteos-a, start timer in init to check and save parameter
186 #ifdef PARAM_SUPPORT_REAL_CHECK
187 if (!PARAM_TEST_FLAG(g_persistWorkSpace.flags, WORKSPACE_FLAGS_LOADED)) {
188 PARAM_LOGE("Can not save persist param before load %s ", name);
189 return 0;
190 }
191 if (g_persistWorkSpace.persistParamOps.batchSave == NULL) {
192 return 0;
193 }
194
195 // check timer for save all
196 struct timespec currTimer = {0};
197 (void)clock_gettime(CLOCK_MONOTONIC, &currTimer);
198 uint32_t diff = IntervalTime(&g_persistWorkSpace.lastSaveTimer, &currTimer);
199 if (diff > PARAM_MUST_SAVE_PARAM_DIFF) {
200 if (g_persistWorkSpace.saveTimer != NULL) {
201 ParamTimerClose(g_persistWorkSpace.saveTimer);
202 g_persistWorkSpace.saveTimer = NULL;
203 }
204 return BatchSavePersistParam();
205 }
206
207 PARAM_SET_FLAG(g_persistWorkSpace.flags, WORKSPACE_FLAGS_UPDATE);
208 if (g_persistWorkSpace.saveTimer == NULL) {
209 ParamTimerCreate(&g_persistWorkSpace.saveTimer, TimerCallbackForSave, NULL);
210 ParamTimerStart(g_persistWorkSpace.saveTimer, PARAM_MUST_SAVE_PARAM_DIFF * MS_UNIT, MS_UNIT);
211 }
212 #endif
213 return 0;
214 }
215
LoadPersistParams(void)216 int LoadPersistParams(void)
217 {
218 // get persist parameter
219 int ret = InitPersistParamWorkSpace();
220 PARAM_CHECK(ret == 0, return ret, "Failed to init persist param");
221 #ifndef STARTUP_INIT_TEST
222 if (PARAM_TEST_FLAG(g_persistWorkSpace.flags, WORKSPACE_FLAGS_LOADED)) {
223 PARAM_LOGE("Persist param has been loaded");
224 return 0;
225 }
226 #endif
227 if (g_persistWorkSpace.persistParamOps.load != NULL) {
228 (void)g_persistWorkSpace.persistParamOps.load();
229 PARAM_SET_FLAG(g_persistWorkSpace.flags, WORKSPACE_FLAGS_LOADED);
230 }
231 // save new persist param
232 ret = BatchSavePersistParam();
233 PARAM_CHECK(ret == 0, return ret, "Failed to load persist param");
234 // for liteos-a, start time to check in init
235 #ifdef PARAM_SUPPORT_CYCLE_CHECK
236 PARAM_LOGV("LoadPersistParams start check time ");
237 if (g_persistWorkSpace.saveTimer == NULL) {
238 ParamTimerCreate(&g_persistWorkSpace.saveTimer, TimerCallbackForSave, NULL);
239 ParamTimerStart(g_persistWorkSpace.saveTimer, PARAM_MUST_SAVE_PARAM_DIFF * MS_UNIT, MS_UNIT);
240 }
241 #endif
242 return 0;
243 }
244