mirror of
https://github.com/nyanmisaka/mpp.git
synced 2026-01-24 04:10:39 +01:00
feat[mpp_cfg_io]: Add mpp cfg io module
1. Add mpp_cfg output to log. 2. Add mpp_cfg input from json. Signed-off-by: Herman Chen <herman.chen@rock-chips.com> Change-Id: I21fa491a9531918c1b05083ec2f1853ae69f3005
This commit is contained in:
parent
6275e90171
commit
f2eb2a2b59
5 changed files with 1925 additions and 0 deletions
|
|
@ -23,6 +23,7 @@ add_library(mpp_base OBJECT
|
|||
mpp_bitwrite.c
|
||||
mpp_bitread.c
|
||||
mpp_bitput.c
|
||||
mpp_cfg_io.c
|
||||
mpp_cfg.cpp
|
||||
mpp_2str.c
|
||||
mpp_dec_hdr_meta.c
|
||||
|
|
|
|||
100
mpp/base/inc/mpp_cfg_io.h
Normal file
100
mpp/base/inc/mpp_cfg_io.h
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
/* SPDX-License-Identifier: Apache-2.0 OR MIT */
|
||||
/*
|
||||
* Copyright (c) 2025 Rockchip Electronics Co., Ltd.
|
||||
*/
|
||||
|
||||
#ifndef __MPP_CFG_IO__
|
||||
#define __MPP_CFG_IO__
|
||||
|
||||
#include "mpp_trie.h"
|
||||
#include "mpp_cfg.h"
|
||||
|
||||
typedef enum MppCfgType_e {
|
||||
MPP_CFG_TYPE_INVALID = 0,
|
||||
|
||||
/* invalid or empty value type */
|
||||
MPP_CFG_TYPE_NULL,
|
||||
|
||||
/* leaf type must with name */
|
||||
MPP_CFG_TYPE_BOOL,
|
||||
MPP_CFG_TYPE_S32,
|
||||
MPP_CFG_TYPE_U32,
|
||||
MPP_CFG_TYPE_S64,
|
||||
MPP_CFG_TYPE_U64,
|
||||
MPP_CFG_TYPE_F32,
|
||||
MPP_CFG_TYPE_F64,
|
||||
MPP_CFG_TYPE_STRING,
|
||||
MPP_CFG_TYPE_RAW,
|
||||
|
||||
/* branch type */
|
||||
MPP_CFG_TYPE_OBJECT,
|
||||
MPP_CFG_TYPE_ARRAY,
|
||||
|
||||
MPP_CFG_TYPE_BUTT,
|
||||
} MppCfgType;
|
||||
|
||||
typedef enum MppCfgStrFmt_e {
|
||||
MPP_CFG_STR_FMT_LOG,
|
||||
MPP_CFG_STR_FMT_JSON,
|
||||
MPP_CFG_STR_FMT_TOML,
|
||||
MPP_CFG_STR_FMT_BUTT,
|
||||
} MppCfgStrFmt;
|
||||
|
||||
typedef union MppCfgVal_u {
|
||||
rk_bool b1;
|
||||
rk_s32 s32;
|
||||
rk_u32 u32;
|
||||
rk_s64 s64;
|
||||
rk_u64 u64;
|
||||
rk_float f32;
|
||||
rk_double f64;
|
||||
void *str;
|
||||
void *ptr;
|
||||
} MppCfgVal;
|
||||
|
||||
typedef void* MppCfgObj;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
rk_s32 mpp_cfg_get_object(MppCfgObj *obj, const char *name, MppCfgType type, MppCfgVal *val);
|
||||
rk_s32 mpp_cfg_get_array(MppCfgObj *obj, const char *name, rk_s32 count);
|
||||
rk_s32 mpp_cfg_put(MppCfgObj obj);
|
||||
rk_s32 mpp_cfg_put_all(MppCfgObj obj);
|
||||
|
||||
/* object tree build */
|
||||
rk_s32 mpp_cfg_add(MppCfgObj root, MppCfgObj leaf);
|
||||
/* object tree release */
|
||||
rk_s32 mpp_cfg_del(MppCfgObj obj);
|
||||
|
||||
/* attach MppCfgInfo for access location */
|
||||
rk_s32 mpp_cfg_set_info(MppCfgObj obj, MppCfgInfo *info);
|
||||
/* all MppCfgObject ready and build trie for string access */
|
||||
rk_s32 mpp_cfg_build_trie(MppCfgObj obj, MppTrie *trie);
|
||||
|
||||
void mpp_cfg_dump(MppCfgObj obj, const char *func);
|
||||
#define mpp_cfg_dump_f(obj) mpp_cfg_dump(obj, __FUNCTION__)
|
||||
|
||||
/* mpp_cfg output to string and input from string */
|
||||
rk_s32 mpp_cfg_to_string(MppCfgObj obj, MppCfgStrFmt fmt, char **buf);
|
||||
rk_s32 mpp_cfg_from_string(MppCfgObj *obj, MppCfgStrFmt fmt, const char *buf);
|
||||
|
||||
/*
|
||||
* obj - read from file or string and get an object as source
|
||||
* type - struct type object root for location table indexing and access
|
||||
* st - struct body to write obj values to
|
||||
*/
|
||||
rk_s32 mpp_cfg_to_struct(MppCfgObj obj, MppCfgObj type, void *st);
|
||||
/*
|
||||
* obj - output object root for the struct values
|
||||
* type - struct type object root for location table access
|
||||
* st - struct body to write obj values
|
||||
*/
|
||||
rk_s32 mpp_cfg_from_struct(MppCfgObj *obj, MppCfgObj type, void *st);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __MPP_CFG_IO__ */
|
||||
1626
mpp/base/mpp_cfg_io.c
Normal file
1626
mpp/base/mpp_cfg_io.c
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -57,3 +57,6 @@ add_mpp_base_test(mpp_sys_cfg)
|
|||
|
||||
# mpp_sys_cfgi unit test
|
||||
add_mpp_base_test(mpp_sys_cfg_st)
|
||||
|
||||
# mpp_cfg_io unit test
|
||||
add_mpp_base_test(mpp_cfg)
|
||||
|
|
|
|||
195
mpp/base/test/mpp_cfg_test.c
Normal file
195
mpp/base/test/mpp_cfg_test.c
Normal file
|
|
@ -0,0 +1,195 @@
|
|||
/* SPDX-License-Identifier: Apache-2.0 OR MIT */
|
||||
/*
|
||||
* Copyright (c) 2025 Rockchip Electronics Co., Ltd.
|
||||
*/
|
||||
|
||||
#define MODULE_TAG "mpp_cfg_test"
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
#include "mpp_log.h"
|
||||
|
||||
#include "mpp_cfg_io.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
MppCfgObj root = NULL;
|
||||
MppCfgObj array = NULL;
|
||||
MppCfgObj obj = NULL;
|
||||
MppCfgVal val;
|
||||
rk_s32 array_size = 4;
|
||||
rk_s32 ret = rk_nok;
|
||||
rk_s32 i;
|
||||
|
||||
mpp_log("start\n");
|
||||
|
||||
if (argc > 1) {
|
||||
char *path = argv[1];
|
||||
void *buf = NULL;
|
||||
rk_s32 fd = -1;
|
||||
rk_s32 size = 0;
|
||||
|
||||
fd = open(path, O_RDWR);
|
||||
if (fd < 0) {
|
||||
mpp_err("open %s failed\n", path);
|
||||
goto FILE_DONE;
|
||||
}
|
||||
|
||||
mpp_log("open file %s\n", path);
|
||||
|
||||
size = lseek(fd, 0, SEEK_END);
|
||||
if (size < 0) {
|
||||
mpp_err("lseek failed\n");
|
||||
goto FILE_DONE;
|
||||
}
|
||||
|
||||
lseek(fd, 0, SEEK_SET);
|
||||
mpp_log("get file size %d\n", size);
|
||||
|
||||
buf = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
if (!buf) {
|
||||
mpp_err("mmap fd %d size %d failed\n", fd, size);
|
||||
goto FILE_DONE;
|
||||
}
|
||||
|
||||
mpp_log("mmap size %d to %p content:\n", size, buf);
|
||||
mpp_log("%s", buf);
|
||||
|
||||
ret = mpp_cfg_from_string(&root, MPP_CFG_STR_FMT_JSON, buf);
|
||||
if (ret) {
|
||||
mpp_err("mpp_cfg_from_string failed\n");
|
||||
goto FILE_DONE;
|
||||
}
|
||||
|
||||
mpp_log("get cfg object %p from file\n", root);
|
||||
|
||||
mpp_cfg_dump_f(root);
|
||||
|
||||
mpp_cfg_put_all(root);
|
||||
root = NULL;
|
||||
ret = rk_ok;
|
||||
|
||||
mpp_log("put cfg object\n");
|
||||
|
||||
FILE_DONE:
|
||||
if (buf) {
|
||||
munmap(buf, size);
|
||||
buf = NULL;
|
||||
}
|
||||
if (fd >= 0) {
|
||||
close(fd);
|
||||
fd = -1;
|
||||
}
|
||||
|
||||
if (ret)
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = mpp_cfg_get_object(&root, NULL, MPP_CFG_TYPE_OBJECT, NULL);
|
||||
if (ret) {
|
||||
mpp_err("mpp_cfg_get_object failed\n");
|
||||
goto DONE;
|
||||
}
|
||||
|
||||
mpp_log("get root object\n");
|
||||
|
||||
ret = mpp_cfg_get_array(&array, NULL, array_size);
|
||||
if (ret) {
|
||||
mpp_err("mpp_cfg_get_array failed\n");
|
||||
goto DONE;
|
||||
}
|
||||
|
||||
mpp_log("get array\n");
|
||||
|
||||
for (i = 0; i < array_size; i++) {
|
||||
obj = NULL;
|
||||
val.s32 = i;
|
||||
ret = mpp_cfg_get_object(&obj, NULL, MPP_CFG_TYPE_S32, &val);
|
||||
if (ret) {
|
||||
mpp_err("mpp_cfg_get_object array element failed\n");
|
||||
goto DONE;
|
||||
}
|
||||
|
||||
ret = mpp_cfg_add(array, obj);
|
||||
if (ret) {
|
||||
mpp_err("mpp_cfg_add array element failed\n");
|
||||
goto DONE;
|
||||
}
|
||||
}
|
||||
|
||||
ret = mpp_cfg_add(root, array);
|
||||
if (ret) {
|
||||
mpp_err("mpp_cfg_add failed\n");
|
||||
goto DONE;
|
||||
}
|
||||
|
||||
mpp_log("add array to root\n");
|
||||
|
||||
obj = NULL;
|
||||
val.s32 = 1920;
|
||||
ret = mpp_cfg_get_object(&obj, "width", MPP_CFG_TYPE_S32, &val);
|
||||
if (ret) {
|
||||
mpp_err("mpp_cfg_get s32 failed\n");
|
||||
goto DONE;
|
||||
}
|
||||
|
||||
ret = mpp_cfg_add(root, obj);
|
||||
if (ret) {
|
||||
mpp_err("mpp_cfg_add s32 failed\n");
|
||||
goto DONE;
|
||||
}
|
||||
|
||||
mpp_log("add s32 to root\n");
|
||||
|
||||
obj = NULL;
|
||||
val.u32 = 1080;
|
||||
ret = mpp_cfg_get_object(&obj, "height", MPP_CFG_TYPE_U32, &val);
|
||||
if (ret) {
|
||||
mpp_err("mpp_cfg_get u32 failed\n");
|
||||
goto DONE;
|
||||
}
|
||||
|
||||
ret = mpp_cfg_add(root, obj);
|
||||
if (ret) {
|
||||
mpp_err("mpp_cfg_add u32 failed\n");
|
||||
goto DONE;
|
||||
}
|
||||
|
||||
mpp_log("set u32 to root\n");
|
||||
|
||||
obj = NULL;
|
||||
val.str = "hello world";
|
||||
ret = mpp_cfg_get_object(&obj, "test", MPP_CFG_TYPE_STRING, &val);
|
||||
if (ret) {
|
||||
mpp_err("mpp_cfg_get string failed\n");
|
||||
goto DONE;
|
||||
}
|
||||
|
||||
ret = mpp_cfg_add(root, obj);
|
||||
if (ret) {
|
||||
mpp_err("mpp_cfg_add string failed\n");
|
||||
goto DONE;
|
||||
}
|
||||
|
||||
mpp_log("set string to root\n");
|
||||
|
||||
mpp_cfg_dump_f(root);
|
||||
|
||||
ret = mpp_cfg_del(array);
|
||||
if (ret) {
|
||||
mpp_err("mpp_cfg_del failed\n");
|
||||
goto DONE;
|
||||
}
|
||||
|
||||
mpp_log("del array from root\n");
|
||||
|
||||
DONE:
|
||||
mpp_cfg_put_all(root);
|
||||
|
||||
mpp_log("done %s\n", ret ? "failed" : "success");
|
||||
|
||||
return ret;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue