Signed-off-by: zhongning5 <zhongning5@huawei.com>
Change-Id: Ib10f4099a7f127df6e17f006956697b043538169
This commit is contained in:
zhongning5 2024-07-05 11:35:25 +08:00
parent 6369470ae1
commit 9d72fed32f
8 changed files with 31 additions and 10 deletions

View File

@ -71,6 +71,7 @@ typedef enum {
#define LOAD_PARAM_NORMAL 0x00
#define LOAD_PARAM_ONLY_ADD 0x01
#define LOAD_PARAM_PERSIST 0x02
typedef uint32_t ParamHandle;

View File

@ -29,7 +29,9 @@ static int LoadOnePersistParam_(const uint32_t *context, const char *name, const
{
UNUSED(context);
uint32_t dataIndex = 0;
return WriteParam(name, value, &dataIndex, 0);
int mode = 0;
mode |= LOAD_PARAM_PERSIST;
return WriteParam(name, value, &dataIndex, mode);
}
static void LoadPersistParam_(const char *fileName, char *buffer, uint32_t buffSize)

View File

@ -269,7 +269,7 @@ INIT_LOCAL_API uint32_t AddParamSecurityNode(WorkSpace *workSpace, const ParamAu
}
INIT_LOCAL_API uint32_t AddParamNode(WorkSpace *workSpace, uint8_t type,
const char *key, uint32_t keyLen, const char *value, uint32_t valueLen)
const char *key, uint32_t keyLen, const char *value, uint32_t valueLen, int mode)
{
PARAM_CHECK(key != NULL && value != NULL, return OFFSET_ERR, "Invalid param");
PARAM_CHECK(CheckWorkSpace(workSpace) == 0, return OFFSET_ERR, "Invalid workSpace %s", key);
@ -294,6 +294,10 @@ INIT_LOCAL_API uint32_t AddParamNode(WorkSpace *workSpace, uint8_t type,
node->valueLength = valueLen;
int ret = PARAM_SPRINTF(node->data, realLen, "%s=%s", key, value);
PARAM_CHECK(ret > 0, return OFFSET_ERR, "Failed to sprint key and value");
if ((mode & LOAD_PARAM_PERSIST) != 0) {
node->commitId |= PARAM_FLAGS_PERSIST;
}
uint32_t offset = workSpace->area->currOffset;
workSpace->area->currOffset += realLen;
workSpace->area->paramNodeCount++;
@ -362,7 +366,7 @@ INIT_LOCAL_API int AddParamEntry(uint32_t index, uint8_t type, const char *name,
PARAM_CHECK(node != NULL, return PARAM_CODE_REACHED_MAX, "Failed to add node");
ParamNode *entry = (ParamNode *)GetTrieNode(workSpace, node->dataIndex);
if (entry == NULL) {
uint32_t offset = AddParamNode(workSpace, type, name, strlen(name), value, strlen(value));
uint32_t offset = AddParamNode(workSpace, type, name, strlen(name), value, strlen(value), 0);
PARAM_CHECK(offset > 0, return PARAM_CODE_REACHED_MAX, "Failed to allocate name %s", name);
SaveIndex(&node->dataIndex, offset);
}

View File

@ -65,6 +65,7 @@ typedef struct {
#define PARAM_FLAGS_MODIFY 0x80000000
#define PARAM_FLAGS_TRIGGED 0x40000000
#define PARAM_FLAGS_WAITED 0x20000000
#define PARAM_FLAGS_PERSIST 0x10000000
#define PARAM_FLAGS_COMMITID 0x0000ffff
#define PARAM_TYPE_MASK 0x0f

View File

@ -53,7 +53,7 @@ INIT_LOCAL_API int TraversalTrieNode(const WorkSpace *workSpace,
INIT_LOCAL_API uint32_t AddParamSecurityNode(WorkSpace *workSpace, const ParamAuditData *auditData);
INIT_LOCAL_API uint32_t AddParamNode(WorkSpace *workSpace, uint8_t type,
const char *key, uint32_t keyLen, const char *value, uint32_t valueLen);
const char *key, uint32_t keyLen, const char *value, uint32_t valueLen, int mode);
INIT_LOCAL_API uint32_t GetParamMaxLen(uint8_t type);
INIT_LOCAL_API ParamNode *GetParamNode(uint32_t index, const char *name);

View File

@ -141,9 +141,13 @@ static int SystemSetParam(const char *name, const char *value, const ParamSecuri
int ret = CheckParameterSet(name, value, srcLabel, &ctrlService);
PARAM_CHECK(ret == 0, return ret, "Forbid to set parameter %s", name);
int mode = 0;
if (strncmp(name, PARAM_PERSIST_PREFIX, strlen(PARAM_PERSIST_PREFIX)) == 0) {
mode |= LOAD_PARAM_PERSIST;
}
if ((ctrlService & PARAM_CTRL_SERVICE) != PARAM_CTRL_SERVICE) { // ctrl param
uint32_t dataIndex = 0;
ret = WriteParam(name, value, &dataIndex, 0);
ret = WriteParam(name, value, &dataIndex, mode);
PARAM_CHECK(ret == 0, return ret, "Failed to set param %d name %s %s", ret, name, value);
ret = WritePersistParam(name, value);
PARAM_CHECK(ret == 0, return ret, "Failed to set persist param name %s", name);

View File

@ -420,14 +420,14 @@ int SystemGetParameterName(ParamHandle handle, char *name, unsigned int len)
return ReadParamName(handle, name, len);
}
static int AddParam(WorkSpace *workSpace, uint8_t type, const char *name, const char *value, uint32_t *dataIndex)
static int AddParam(WorkSpace *workSpace, uint8_t type, const char *name, const char *value, uint32_t *dataIndex, int mode)
{
ParamTrieNode *node = AddTrieNode(workSpace, name, strlen(name));
PARAM_CHECK(node != NULL, return PARAM_CODE_REACHED_MAX,
"Failed to add node name %s space %s", name, workSpace->fileName);
ParamNode *entry = (ParamNode *)GetTrieNode(workSpace, node->dataIndex);
if (entry == NULL) {
uint32_t offset = AddParamNode(workSpace, type, name, strlen(name), value, strlen(value));
uint32_t offset = AddParamNode(workSpace, type, name, strlen(name), value, strlen(value), mode);
PARAM_CHECK(offset > 0, return PARAM_CODE_REACHED_MAX,
"Failed to allocate name %s space %s", name, workSpace->fileName);
SaveIndex(&node->dataIndex, offset);
@ -446,7 +446,7 @@ static int AddParam(WorkSpace *workSpace, uint8_t type, const char *name, const
return 0;
}
static int UpdateParam(const WorkSpace *workSpace, uint32_t *dataIndex, const char *name, const char *value)
static int UpdateParam(const WorkSpace *workSpace, uint32_t *dataIndex, const char *name, const char *value, int mode)
{
ParamNode *entry = (ParamNode *)GetTrieNode(workSpace, *dataIndex);
PARAM_CHECK(entry != NULL, return PARAM_CODE_REACHED_MAX, "Failed to update param value %s %u", name, *dataIndex);
@ -460,6 +460,7 @@ static int UpdateParam(const WorkSpace *workSpace, uint32_t *dataIndex, const ch
PARAM_CHECK(ret == 0, return PARAM_CODE_INVALID_VALUE, "Failed to copy value");
entry->valueLength = valueLen;
}
uint32_t flags = commitId & ~PARAM_FLAGS_COMMITID;
uint32_t commitIdCount = (++commitId) & PARAM_FLAGS_COMMITID;
ATOMIC_STORE_EXPLICIT(&entry->commitId, flags | commitIdCount, MEMORY_ORDER_RELEASE);
@ -471,6 +472,10 @@ static int UpdateParam(const WorkSpace *workSpace, uint32_t *dataIndex, const ch
}
#endif
PARAM_LOGV("UpdateParam name %s value: %s", name, value);
if ((mode & LOAD_PARAM_PERSIST) != 0) {
entry->commitId |= PARAM_FLAGS_PERSIST;
}
futex_wake(&entry->commitId, INT_MAX);
return 0;
}
@ -506,14 +511,14 @@ INIT_LOCAL_API int WriteParam(const char *name, const char *value, uint32_t *dat
ret = CheckParamValue(node, name, value, entry->type);
PARAM_CHECK(ret == 0, return ret, "Invalid param value param: %s=%s", name, value);
PARAMSPACE_AREA_RW_LOCK(workSpace);
ret = UpdateParam(workSpace, &node->dataIndex, name, value);
ret = UpdateParam(workSpace, &node->dataIndex, name, value, mode);
PARAMSPACE_AREA_RW_UNLOCK(workSpace);
} else {
uint8_t type = GetParamValueType(name);
ret = CheckParamValue(node, name, value, type);
PARAM_CHECK(ret == 0, return ret, "Invalid param value param: %s=%s", name, value);
PARAMSPACE_AREA_RW_LOCK(workSpace);
ret = AddParam((WorkSpace *)workSpace, type, name, value, dataIndex);
ret = AddParam((WorkSpace *)workSpace, type, name, value, dataIndex, mode);
PARAMSPACE_AREA_RW_UNLOCK(workSpace);
}
if ((ret == PARAM_CODE_REACHED_MAX) && (flag == 1)) {

View File

@ -76,6 +76,10 @@ static int SavePersistParam(const WorkSpace *workSpace, const ParamTrieNode *nod
if (!IsNeedToSave(entry->data)) {
return 0;
}
if ((entry->commitId & PARAM_FLAGS_PERSIST) == 0) {
return 0;
}
static char name[PARAM_NAME_LEN_MAX] = {0};
int ret = memcpy_s(name, PARAM_NAME_LEN_MAX - 1, entry->data, entry->keyLength);
PARAM_CHECK(ret == EOK, return -1, "Failed to read param name %s", entry->data);