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