From dc1a278ce73a2500c53268eb0866d81f20e3c981 Mon Sep 17 00:00:00 2001 From: huangxin Date: Thu, 10 Oct 2019 16:36:08 +0800 Subject: [PATCH] =?UTF-8?q?Mod=20=20aaa-12=20=E5=A2=9E=E5=8A=A0=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E7=AE=A1=E7=90=86=E9=85=8D=E7=BD=AE=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=20RCA=EF=BC=9A=20SOL=EF=BC=9A=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E4=BA=BA=EF=BC=9Ahuangxin=20=E6=A3=80=E8=A7=86?= =?UTF-8?q?=E4=BA=BA=EF=BC=9Ahuangxin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 20 +- Common/config_manager.h | 22 +- Platform/build/user.configm.Makefile | 13 +- Platform/build/user.configmtest.Makefile | 2 +- .../user/configm/config-server/configserver.c | 166 ++-- .../configm/config-server/include/configm.h | 47 +- .../config-server/object_manager/hexdump.c | 326 +++++++ .../config-server/object_manager/log.c | 441 ++++++++++ .../config-server/object_manager/log.h | 208 +++++ .../object_manager/object_manager.c | 43 + .../object_manager/object_manager.h | 22 + Product/build/user.object.Makefile | 9 +- Product/common/common.h | 2 +- Product/user/object_manager/json_interface.c | 73 +- Product/user/object_manager/json_interface.h | 2 +- Product/user/object_manager/log.c | 12 +- Product/user/object_manager/main.c | 825 +++++++++--------- 17 files changed, 1664 insertions(+), 569 deletions(-) create mode 100644 Platform/user/configm/config-server/object_manager/hexdump.c create mode 100644 Platform/user/configm/config-server/object_manager/log.c create mode 100644 Platform/user/configm/config-server/object_manager/log.h create mode 100644 Platform/user/configm/config-server/object_manager/object_manager.c create mode 100644 Platform/user/configm/config-server/object_manager/object_manager.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 10703ea41..642c14b22 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,19 +1,37 @@ cmake_minimum_required(VERSION 3.10) project(drivers) # 工程名字,随你怎么叫都行 +set(PLAT_CFG_DIR "./Platform/user/configm/config-server") #include(kernel_config.cmake) # 一堆的 add_definitions(xxx),就不细说了 #add_definitions(-D__KERNEL__=1) +add_definitions(-D_GNU_SOURCE=1) +add_definitions(-D__USE_XOPEN=1) +add_definitions(-DOBJECT_DEMO=1) + aux_source_directory(./Product/user/object_manager DIR_SRCS) aux_source_directory(./libs/src/lighttpd-1.4.51/src DIR_SRCS) +aux_source_directory(${PLAT_CFG_DIR} DIR_SRCS) +aux_source_directory(${PLAT_CFG_DIR}/object_manager DIR_SRCS) +aux_source_directory(${PLAT_CFG_DIR}/web_config DIR_SRCS) + + set(MY_ANDROID_ROOT_DIR "./kernel/linux-4.14.83") # 定义一个变量,方便后面使用 #set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -nostdinc") # C 编译器设置 -add_executable(drivers ${DIR_SRCS}) # dummy target,没有这个的话external libraries就是空的, +add_executable(drivers ${DIR_SRCS} Platform/user/configm/config-server/object_manager/object_manager.h Platform/user/configm/config-server/object_manager/object_manager.c) # dummy target,没有这个的话external libraries就是空的, # 弄一个main.c糊弄糊弄Clion +include_directories( + ./Common + ${PLAT_CFG_DIR}/include + ./Platform/common/configm/include + ./Platform/common + ./Product/common +) + #include_directories( # "${MY_ANDROID_ROOT_DIR}/kernel/include" # "${MY_ANDROID_ROOT_DIR}/kernel/arch/arm/include" diff --git a/Common/config_manager.h b/Common/config_manager.h index cac1e5675..1c88266c5 100755 --- a/Common/config_manager.h +++ b/Common/config_manager.h @@ -12,6 +12,22 @@ /***********************操作类型定义结束 ********************/ /*************************** 模块定义 ************************/ +typedef enum{ + NETCONFIG_MODULE = 0x00000001, ///< NET CONFIG + USER_MANAGER_CONFIG_MODULE = 0x00000002, ///< USER MANAGER CONFIG + LOCALAUTH_CONFIG_MODULE = 0x00000003, ///< PORTAL SERVER CONFIG + LOG_CONFIG_MODULE = 0x00000004, ///< + VLAN_CONFIG_MODULE = 0x00000005, ///< vlan config + DHCP_CONFIG_MODULE = 0x00000007, ///< DHCP CONFIG + LTE_CONFIG_MODULE = 0x00000008, ///< 4G config + NAT_CONFIG_MODULE = 0x00000009, ///< nat config + STATIC_ROUTING_CONFIG_MODULE = 0x00000009, ///< static routing + OBJECT_MANAGER_CONFIG_MODULE = 0x0000000A, ///< Object manager config id + + CONFIG_ID_MAX, +} CONFIG_ID_TYPE; + +#if 0 /* NET CONFIG */ #define NETCONFIG_MODULE 0x00000001 @@ -37,7 +53,7 @@ /*4G config*/ #define LTE_CONFIG_MODULE 0x00000007 - +#endif /************************* 模块定义结束 **********************/ @@ -80,6 +96,10 @@ #define GET_ALL_ROUTING_INFO (uint64)((uint64)STATIC_ROUTING_CONFIG_MODULE<<32|2) #define LTE_CONFIG (uint64)((uint64)LTE_CONFIG_MODULE<<32|1) + +#define OBJECT_CONFIG (uint64)((uint64)OBJECT_MANAGER_CONFIG_MODULE<<32|1) +#define OBJECT_GETALL_CONFIG (uint64)((uint64)OBJECT_MANAGER_CONFIG_MODULE<<32|2) + /************************ config id定义 end**********************/ #endif diff --git a/Platform/build/user.configm.Makefile b/Platform/build/user.configm.Makefile index a00567783..8081a162c 100755 --- a/Platform/build/user.configm.Makefile +++ b/Platform/build/user.configm.Makefile @@ -28,7 +28,8 @@ VPATH = ../user/configm/config-server \ ../user/configm/config-server/web_config \ ../user/configm/config-server/dhcp_config \ ../user/configm/config-server/user_manager_config \ - ../user/configm/config-server/log_config + ../user/configm/config-server/log_config \ + ../user/configm/config-server/object_manager # source code @@ -71,7 +72,10 @@ COMMON_SRCS = configserver.c \ dhcp_shared_network_config.c \ dhcp_subnet_config.c\ static_routing_config/static_routing_config.c \ - LTE_config/LTE_config.c + LTE_config/LTE_config.c \ + object_manager.c \ + log.c \ + hexdump.c @@ -84,7 +88,8 @@ COMMOM_CFLAGS = -I../user/configm/config-server/include \ -I../../Common -I../common/redismq -I../common/database \ -I../common/configm -I../common/rpc -I../common/rpc/hashtable \ -I../common/ulog -I../user/configm/config-server/netconfig/ \ - -I../user/configm/config-server/netconfig/bridge/include + -I../user/configm/config-server/netconfig/bridge/include \ + -I../common -I../../Producd/common # gcc CFLAGS PLAT_ARM64_CFLAGS := $(COMMOM_CFLAGS) @@ -98,7 +103,7 @@ COMMON_STD_LIB := -lpthread -lm -lcjson -levent -ljson-c -lhiredis -lodbc -lev - PLAT_ARM64_LIBS := -lopenrpc-$(ARM64_OBJ_TARGET) -lnetlinku-$(ARM64_OBJ_TARGET) -lredismq-$(ARM64_OBJ_TARGET) PLAT_ARM64_LIBS += -lulogapi-$(ARM64_OBJ_TARGET) -ldatabase-$(ARM64_OBJ_TARGET) $(COMMON_STD_LIB) -PLAT_LINUX_LIBS := -lopenrpc-$(LINUX_OBJ_TARGET) -lnetlinku-$(LINUX_OBJ_TARGET) -lredismq-$(LINUX_OBJ_TARGET) +PLAT_LINUX_LIBS := -lopenrpc-$(LINUX_OBJ_TARGET) -lnetlinku-$(LINUX_OBJ_TARGET) -lredismq-$(LINUX_OBJ_TARGET) PLAT_LINUX_LIBS += -lulogapi-$(LINUX_OBJ_TARGET) -ldatabase-$(LINUX_OBJ_TARGET) $(COMMON_STD_LIB) diff --git a/Platform/build/user.configmtest.Makefile b/Platform/build/user.configmtest.Makefile index 24decc8b5..dd7908358 100644 --- a/Platform/build/user.configmtest.Makefile +++ b/Platform/build/user.configmtest.Makefile @@ -35,7 +35,7 @@ PLAT_LINUX_SRCS = $(COMMON_SRCS) PLAT_ARM64_SRCS = $(COMMON_SRCS) # gcc CFLAGS -PLAT_ARM64_CFLAGS := -I../user/configm/config-server/include -I../../Common -I../common/configm -I../common/rpc -I../common/rpc/hashtable +PLAT_ARM64_CFLAGS := -I../user/configm/config-server/include -I../../Common -I../common/configm -I../common -I../common/rpc -I../common/rpc/hashtable PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS) diff --git a/Platform/user/configm/config-server/configserver.c b/Platform/user/configm/config-server/configserver.c index 41fe10dbb..f0632e4bf 100644 --- a/Platform/user/configm/config-server/configserver.c +++ b/Platform/user/configm/config-server/configserver.c @@ -19,7 +19,7 @@ int cm_format_data(ret_code ret_code, cJSON *item_obj, char *output) char *ret_char = NULL; config_result_t tem_buff = {0}; config_result_t *config_ret = &tem_buff; - + snprintf(config_ret->resultCode, RET_CODE_LEN - 1, "%d", ret_code); config_ret->message = (char *)rpc_code_format(ret_code); @@ -32,7 +32,7 @@ int cm_format_data(ret_code ret_code, cJSON *item_obj, char *output) s2j_json_set_basic_element(json_obj, config_ret, string, resultCode); s2j_json_set_basic_element(json_obj, config_ret, string, message); - + cJSON_AddItemToObject(json_obj, "data", item_obj); ret_char = cJSON_PrintUnformatted(json_obj); @@ -80,7 +80,7 @@ void cm_return(rpc_conn *conn, ret_code err_code, char* err_message) ret_char = cJSON_PrintUnformatted(json_obj); - rpc_return(conn, ret_char, strlen(ret_char)+1); + rpc_return(conn, ret_char, strlen(ret_char)+1); rpc_free(ret_char); cJSON_Delete(json_obj); @@ -106,40 +106,40 @@ config_service_t *cm_config_service_get(uint64 config_id) ret_code cm_config_rec_pre(char **input, int *input_len, char **output, int *output_len) { - /*if((access(CONFIG_RECOVERY_DONE, F_OK))!=-1) - { + /*if((access(CONFIG_RECOVERY_DONE, F_OK))!=-1) + { return RET_ERR; - } */ - - *input = rpc_new(char, CM_BUFF_SIZE); - *output = rpc_new(char, CM_BUFF_SIZE); + } */ + + *input = rpc_new(char, CM_BUFF_SIZE); + *output = rpc_new(char, CM_BUFF_SIZE); *input_len = CM_BUFF_SIZE; *output_len = CM_BUFF_SIZE; - - if(*input == NULL + + if(*input == NULL || *output == NULL) { return RET_NOMEM; } - + memset(*input, 0, CM_BUFF_SIZE); memset(*output, 0, CM_BUFF_SIZE); - + rpc_log_info("==>config revocery start\n"); - + return RET_OK; } ret_code cm_config_rec_done(char *input, char *output) { int fd; - + memset(input, 0, CM_BUFF_SIZE); memset(output, 0, CM_BUFF_SIZE); - free(input); + free(input); free(output); - + fd = open(CONFIG_RECOVERY_DONE, O_CREAT, 0777); if(fd <= 0) { @@ -149,40 +149,40 @@ ret_code cm_config_rec_done(char *input, char *output) { close(fd); } - + rpc_log_info("==>config revocery done!\n"); return RET_OK; } ret_code cm_config_get_allconfig(uint source, - config_service_t *config_svr, + config_service_t *config_svr, char *input, int *intput_len) -{ +{ ret_code ret = RET_OK; - + memset(input, 0, CM_BUFF_SIZE); - + ret = config_svr->getall_callback(source, input, intput_len); ASSERT_RET(ret); - + return ret; } ret_code cm_config_config_rec(uint source, - config_service_t *config_svr, + config_service_t *config_svr, char *input, int input_len, char *output, int *output_len) - { + { int config_type = CM_CONFIG_SET; ret_code ret = RET_OK; - + ret = config_svr->chk_callback(source, &config_type, - input, &input_len, + input, &input_len, output, output_len); if(ret == RET_OK) { - config_svr->proc_callback(source, config_type, + config_svr->proc_callback(source, config_type, input, input_len, output, output_len); } @@ -193,17 +193,17 @@ ret_code cm_config_get_allconfig(uint source, void cm_config_module_init() { int len = sizeof(g_config_init) / sizeof(config_init_t); - config_init_t *config_svr; + config_init_t *config_svr; int config_idx; for(config_idx = 0; config_idx < len; config_idx++) { config_svr = &(g_config_init[config_idx]); - + rpc_log_info("MODULE %d init\n", config_svr->config_mudlue); if(config_svr->init_callback) { - config_svr->init_callback(); + config_svr->init_callback(); } } } @@ -212,11 +212,11 @@ void cm_config_module_init() void cm_config_recovery() { int len = sizeof(g_config_service) / sizeof(config_service_t); - config_service_t *config_svr; + config_service_t *config_svr; int input_len = 0; int output_len = 0; char *input = NULL; - char *output = NULL; + char *output = NULL; ret_code ret = RET_OK; int config_idx; int recover_idx = 1,recover_phase; @@ -238,7 +238,7 @@ void cm_config_recovery() { break; } - + for(config_idx = 0; config_idx < len; config_idx++) { config_svr = &(g_config_service[config_idx]); @@ -247,26 +247,26 @@ void cm_config_recovery() { continue; } - - if(config_svr->chk_callback == NULL + + if(config_svr->chk_callback == NULL || config_svr->proc_callback == NULL) { continue; } - cm_config_get_allconfig(recover_phase, config_svr, + cm_config_get_allconfig(recover_phase, config_svr, input, &input_len); - cm_config_config_rec(recover_phase, config_svr, - input, input_len, + cm_config_config_rec(recover_phase, config_svr, + input, input_len, output, &output_len); } - + recover_idx++; } - + cm_config_rec_done(input, output); - + return; } @@ -284,27 +284,27 @@ ret_code cm_config_proc_pre(char **input, char **output, int *buff_len) { cm_get_buff = rpc_new(char, CM_BUFF_SIZE); } - + if(cm_get_buff == NULL || cm_set_buff == NULL) { return RET_NOMEM; } - + memset(cm_set_buff, 0, CM_BUFF_SIZE); memset(cm_get_buff, 0, CM_BUFF_SIZE); *input = cm_set_buff; *output = cm_get_buff; - + *buff_len = CM_BUFF_SIZE; - + return RET_OK; } /* 配置处理入口函数,所有的配置,均在此函数中处理 */ void cm_config_process(rpc_conn *conn, pointer input, int input_len, void* data) -{ +{ config_service_t *config_svr; config_msg_t *config_msg; char *cm_get_buff = NULL; @@ -325,21 +325,21 @@ void cm_config_process(rpc_conn *conn, pointer input, int input_len, void* data) cm_return(conn, RET_NOMEM, "not enough memery"); return; } - + config_msg = (config_msg_t *)input; config_len = input_len - sizeof(config_msg_t); rpc_log_info("recv config from %d, config type is %d config id is 0x%llx ,len is %d\r\n", config_msg->source, config_msg->config_type, config_msg->config_id, config_len); - + config_svr = cm_config_service_get(config_msg->config_id); if(config_svr == NULL) { cm_return(conn, RET_NOCMID, "can not find config id"); return; } - + /*source check*/ if(!(config_svr->config_src & config_msg->source)) { @@ -352,17 +352,17 @@ void cm_config_process(rpc_conn *conn, pointer input, int input_len, void* data) cm_return(conn, RET_NOMEM, "not enough memory"); return; } - + memcpy(cm_set_buff, config_msg->config_buff, config_len); - + /*config check*/ if(config_svr->chk_callback) { ret = config_svr->chk_callback(config_msg->source, &(config_msg->config_type), - cm_set_buff, - &config_len, - cm_get_buff, + cm_set_buff, + &config_len, + cm_get_buff, &buff_len); if(ret != RET_OK) { @@ -370,28 +370,28 @@ void cm_config_process(rpc_conn *conn, pointer input, int input_len, void* data) return; } } - + /*config exec*/ switch(config_msg->config_type) - { + { case CM_CONFIG_GET: if(config_svr->get_callback) { - ret = config_svr->get_callback(config_msg->source, - cm_set_buff, - config_len, - cm_get_buff, + ret = config_svr->get_callback(config_msg->source, + cm_set_buff, + config_len, + cm_get_buff, &buff_len); } break; - + case CM_CONFIG_GET_ALL: if(config_svr->getall_callback) - { - - ret = config_svr->getall_callback(config_msg->source, - cm_get_buff, - &buff_len); + { + + ret = config_svr->getall_callback(config_msg->source, + cm_get_buff, + &buff_len); } break; case CM_CONFIG_ADD: @@ -399,48 +399,48 @@ void cm_config_process(rpc_conn *conn, pointer input, int input_len, void* data) case CM_CONFIG_SET: if(config_svr->proc_callback) { - ret = config_svr->proc_callback(config_msg->source, + ret = config_svr->proc_callback(config_msg->source, config_msg->config_type, - cm_set_buff, - config_len, - cm_get_buff, + cm_set_buff, + config_len, + cm_get_buff, &buff_len); } break; default: ret = RET_INPUTERR; break; - + } - + if(buff_len > CM_BUFF_SIZE) { ret = RET_NOMEM; } - + if(ret != RET_OK) { cm_return(conn, ret, cm_get_buff); return; } - + rpc_return(conn, cm_get_buff, buff_len); - + return; } -int main(int argc, char **argv) +int main(int argc, char **argv) { rpc_server *server; signal(SIGPIPE,SIG_IGN); - - if (server= rpc_server_create_ex("ConfigManger#0")) + + if (server= rpc_server_create_ex("ConfigManger#0")) { rpc_log_info("start server\n"); - } - else + } + else { rpc_log_error("start server error\n"); return EXIT_FAILURE; @@ -448,13 +448,13 @@ int main(int argc, char **argv) /* 模块初始化 */ cm_config_module_init(); - + /* 配置恢复 */ cm_config_recovery(); /* 注册配置处理函数 */ rpc_server_regservice(server, "ConfigManger#0", "cm_config_process", cm_config_process); - + for(;;) { rpc_sleep(1000); diff --git a/Platform/user/configm/config-server/include/configm.h b/Platform/user/configm/config-server/include/configm.h index d6dfa7e20..29432780e 100755 --- a/Platform/user/configm/config-server/include/configm.h +++ b/Platform/user/configm/config-server/include/configm.h @@ -17,6 +17,7 @@ #include "dhcp_lib.h" #include "static_routing.h" #include "LTE_config.h" +#include "../object_manager/object_manager.h" #define RET_CODE_LEN 16 #define RET_MSG_LEN 128 @@ -42,18 +43,22 @@ { \ LTE_CONFIG_MODULE, \ LTE_config_init \ - } \ + }, \ + { \ + OBJECT_MANAGER_CONFIG_MODULE, \ + object_config_init \ + } \ } -/* +/* 1、配置ID,全局唯一,用于寻找对应的配置业务 2、配置源检查,全局唯一,用于寻找对应的配置业务, - 从低位到高位,第一位表示WEB,后续配置扩展 + 从低位到高位,第一位表示WEB,后续配置扩展 3、是否配置恢复 4、是否是多实例 - 5、配置校验回调函数 - 6、配置处理接口 - 7、配置获取接口 + 5、配置校验回调函数 + 6、配置处理接口 + 7、配置获取接口 8、配置全部获取接口 */ #define CONFIG_SERVICE_ARRAY \ @@ -309,29 +314,47 @@ LTE_config_proc, \ NULL, \ LTE_config_get_all \ - }\ + }, \ + { \ + OBJECT_CONFIG, \ + CONFIG_FROM_WEB, \ + FALSE, \ + object_config_chk, \ + object_config_proc, \ + NULL, \ + NULL, \ + }, \ + { \ + OBJECT_GETALL_CONFIG, \ + CONFIG_FROM_WEB, \ + FALSE, \ + object_config_chk, \ + NULL, \ + object_config_get, \ + object_config_get_all, \ + } \ } typedef ret_code (*cm_config_init)(); typedef ret_code (*cm_config_chk)(uint source, uint *config_type, - pointer input, int *input_len, + pointer input, int *input_len, pointer output, int *output_len); typedef ret_code (*cm_config_proc)(uint source, uint config_type, - pointer input, int input_len, + pointer input, int input_len, pointer output, int *output_len); -typedef ret_code (*cm_config_get)(uint source, +typedef ret_code (*cm_config_get)(uint source, pointer input, int input_len, pointer output, int *output_len); -typedef ret_code (*cm_config_get_all)(uint source, +typedef ret_code (*cm_config_get_all)(uint source, pointer output, int *output_len); /* 配置注册 */ struct _config_init { - uint config_mudlue; + uint config_mudlue; cm_config_init init_callback; }; typedef struct _config_init config_init_t; diff --git a/Platform/user/configm/config-server/object_manager/hexdump.c b/Platform/user/configm/config-server/object_manager/hexdump.c new file mode 100644 index 000000000..736372f7c --- /dev/null +++ b/Platform/user/configm/config-server/object_manager/hexdump.c @@ -0,0 +1,326 @@ +#ifndef __KERNEL__ +#include +#include +#include +#include +#include + +#include "log.h" + +static const char hex_asc[] = "0123456789abcdef"; + +#define hex_asc_lo(x) hex_asc[((x) & 0x0f)] +#define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4] + +char* IHW_bin2hex(char *p, const unsigned char *cp, int count) +{ + while (count) { + unsigned char c = *cp++; + /* put lowercase hex digits */ + *p++ = 0x20 | hex_asc[c >> 4]; + *p++ = 0x20 | hex_asc[c & 0xf]; + count--; + } + + return p; +} + +/** + * hex_to_bin - convert a hex digit to its real value + * @ch: ascii character represents hex digit + * + * hex_to_bin() converts one hex digit to its actual value or -1 in case of bad + * input. + */ +int hex_to_bin(char ch) +{ + if((ch >= '0') && (ch <= '9')) + { + return ch - '0'; + } + + ch = tolower(ch); + + if((ch >= 'a') && (ch <= 'f')) + { + return ch - 'a' + 10; + } + + return -1; +} + +/** + * hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory + * @buf: data blob to dump + * @len: number of bytes in the @buf + * @rowsize: number of bytes to print per line; must be 16 or 32 + * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1) + * @linebuf: where to put the converted data + * @linebuflen: total size of @linebuf, including space for terminating NUL + * @ascii: include ASCII after the hex output + * + * hex_dump_to_buffer() works on one "line" of output at a time, i.e., + * 16 or 32 bytes of input data converted to hex + ASCII output. + * + * Given a buffer of u8 data, hex_dump_to_buffer() converts the input data + * to a hex + ASCII dump at the supplied memory location. + * The converted output is always NUL-terminated. + * + * E.g.: + * hex_dump_to_buffer(frame->data, frame->len, 16, 1, + * linebuf, sizeof(linebuf), true); + * + * example output buffer: + * 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO + */ +void hex_dump_to_buffer(const void* buf, int len, int rowsize, + int groupsize, char* linebuf, size_t linebuflen, + int ascii) +{ + const unsigned char* ptr = (const unsigned char *)buf; + unsigned char ch; + int j, lx = 0; + int ascii_column; + + if(rowsize != 16 && rowsize != 32) + { + rowsize = 16; + } + + if(!len) + { + goto nil; + } + + if(len > rowsize) /* limit to one line at a time */ + { + len = rowsize; + } + + if((len % groupsize) != 0) /* no mixed size output */ + { + groupsize = 1; + } + + switch(groupsize) + { + case 8: + { + const unsigned long long* ptr8 = (const unsigned long long *)buf; + int ngroups = len / groupsize; + + for(j = 0; j < ngroups; j++) + lx += snprintf(linebuf + lx, linebuflen - lx, + "%s%16.16llx", j ? " " : "", + (unsigned long long) * (ptr8 + j)); + + ascii_column = 17 * ngroups + 2; + break; + } + + case 4: + { + const unsigned int* ptr4 = (const unsigned int *)buf; + int ngroups = len / groupsize; + + for(j = 0; j < ngroups; j++) + lx += snprintf(linebuf + lx, linebuflen - lx, + "%s%8.8x", j ? " " : "", *(ptr4 + j)); + + ascii_column = 9 * ngroups + 2; + break; + } + + case 2: + { + const unsigned short* ptr2 = (const unsigned short *)buf; + int ngroups = len / groupsize; + + for(j = 0; j < ngroups; j++) + lx += snprintf(linebuf + lx, linebuflen - lx, + "%s%4.4x", j ? " " : "", *(ptr2 + j)); + + ascii_column = 5 * ngroups + 2; + break; + } + + default: + for(j = 0; (j < len) && (lx + 3) <= linebuflen; j++) + { + ch = ptr[j]; + linebuf[lx++] = hex_asc_hi(ch); + linebuf[lx++] = hex_asc_lo(ch); + linebuf[lx++] = ' '; + } + + if(j) + { + lx--; + } + + ascii_column = 3 * rowsize + 2; + break; + } + + if(!ascii) + { + goto nil; + } + + while(lx < (linebuflen - 1) && lx < (ascii_column - 1)) + { + linebuf[lx++] = ' '; + } + + for(j = 0; (j < len) && (lx + 2) < linebuflen; j++) + { + ch = ptr[j]; + linebuf[lx++] = (isascii(ch) && isprint(ch)) ? ch : '.'; + } + +nil: + linebuf[lx++] = '\0'; +} + +/** + * print_hex_dump - print a text hex dump to syslog for a binary blob of data + * @level: kernel log level (e.g. KERN_DEBUG) + * @prefix_str: string to prefix each line with; + * caller supplies trailing spaces for alignment if desired + * @prefix_type: controls whether prefix of an offset, address, or none + * is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE) + * @rowsize: number of bytes to print per line; must be 16 or 32 + * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1) + * @buf: data blob to dump + * @len: number of bytes in the @buf + * @ascii: include ASCII after the hex output + * + * Given a buffer of u8 data, print_hex_dump() prints a hex + ASCII dump + * to the kernel log at the specified kernel log level, with an optional + * leading prefix. + * + * print_hex_dump() works on one "line" of output at a time, i.e., + * 16 or 32 bytes of input data converted to hex + ASCII output. + * print_hex_dump() iterates over the entire input @buf, breaking it into + * "line size" chunks to format and print. + * + * E.g.: + * print_hex_dump(KERN_DEBUG, "raw data: ", DUMP_PREFIX_ADDRESS, + * 16, 1, frame->data, frame->len, true); + * + * Example output using %DUMP_PREFIX_OFFSET and 1-byte mode: + * 0009ab42: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO + * Example output using %DUMP_PREFIX_ADDRESS and 4-byte mode: + * ffffffff88089af0: 73727170 77767574 7b7a7978 7f7e7d7c pqrstuvwxyz{|}~. + */ +void print_hex_dump(const char* prefix_str, int prefix_type, + int rowsize, int groupsize, + const void* buf, int len, int ascii) +{ + const unsigned char* ptr = (const unsigned char *)buf; + int i, remaining = len; + unsigned char linebuf[32 * 3 + 2 + 32 + 1]; + + if(rowsize != 16 && rowsize != 32) + { + rowsize = 16; + } + + for(i = 0; i < len; i += rowsize) + { + int linelen = MIN(remaining, rowsize); + remaining -= rowsize; + + hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize, + (char *)linebuf, sizeof(linebuf), ascii); + + switch(prefix_type) + { + case DUMP_PREFIX_ADDRESS: + print("%s%p: %s\n", + prefix_str, ptr + i, linebuf); + break; + + case DUMP_PREFIX_OFFSET: + print("%s%.8x: %s\n", prefix_str, i, linebuf); + break; + + default: + print("%s%.8x: %s\n", prefix_str, i, linebuf); + break; + } + } + + print("%s", "\n"); +} + +/** + * print_hex_dump_bytes - shorthand form of print_hex_dump() with default params + * @prefix_str: string to prefix each line with; + * caller supplies trailing spaces for alignment if desired + * @prefix_type: controls whether prefix of an offset, address, or none + * is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE) + * @buf: data blob to dump + * @len: number of bytes in the @buf + * + * Calls print_hex_dump(), with log level of KERN_DEBUG, + * rowsize of 16, groupsize of 1, and ASCII output included. + */ +void print_hex_dump_bytes(const char* prefix_str, int prefix_type, + const void* buf, int len) +{ + print_hex_dump(prefix_str, prefix_type, 16, 1, + buf, len, 1); +} + +const char* format_hex_buf(const char* prefix_str, int prefix_type, + int rowsize, int groupsize, + const void* buf, int len, int ascii) +{ + UT_string* pLogStr = NULL; + const char* pFormatStr; + const unsigned char* ptr = (const unsigned char *)buf; + int i, remaining = len; + unsigned char linebuf[32 * 3 + 2 + 32 + 1]; + + if(rowsize != 16 && rowsize != 32) + { + rowsize = 16; + } + + utstring_new(pLogStr); + + for(i = 0; i < len; i += rowsize) + { + int linelen = MIN(remaining, rowsize); + remaining -= rowsize; + + hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize, + (char *)linebuf, sizeof(linebuf), ascii); + + switch(prefix_type) + { + case DUMP_PREFIX_ADDRESS: + utstring_printf(pLogStr, "%s%p: %s\n", + prefix_str, ptr + i, linebuf); + break; + + case DUMP_PREFIX_OFFSET: + utstring_printf(pLogStr, "%s%.8x: %s\n", + prefix_str, i, linebuf); + break; + + default: + utstring_printf(pLogStr, "%s%.8x: %s\n", + prefix_str, i, linebuf); + break; + } + } + + pFormatStr = strdup(utstring_body(pLogStr)); + utstring_free(pLogStr); + + return pFormatStr; +} + +#endif diff --git a/Platform/user/configm/config-server/object_manager/log.c b/Platform/user/configm/config-server/object_manager/log.c new file mode 100644 index 000000000..7d385545b --- /dev/null +++ b/Platform/user/configm/config-server/object_manager/log.c @@ -0,0 +1,441 @@ +/** @file log.c + @brief 系统日志接口文件 + @version 1.0.0 +*/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "log.h" +#include "../../../../Product/common/common.h" + +#define SHOW_CONSOLE_RED ("\033[31;48m\033[1m%s\033[0m%c") +#define SHOW_CONSOLE_YELLOW ("\033[33;48m\033[1m%s\033[0m%c") +#define SHOW_CONSOLE_GREEN ("\033[32;48m\033[1m%s\033[0m%c") +#define SHOW_CONSOLE_BLUE ("\033[34;48m\033[1m%s\033[0m%c") + +#define LOG_FILE_BASEDIR (".") + +#define MAX_LOG_FILE_SIZE (1024 * 1024) +#define MAX_PATH (256) + +typedef struct LOG_ITEM +{ + LOG_LEVEL level; + int isPrinted; + int isAddTags; + struct timeval timestamp; + char *pLogContent; + struct LOG_ITEM *next, *prev; +}*PLOG_ITEM; + +typedef struct +{ + pid_t pid; + char exeName[MAX_PATH]; + char logFilePath[MAX_PATH]; + FILE *pLogFile; +} LOG_PROCESS_INFO, *PLOG_PROCESS_INFO; + +static int g_bEnableLog = TRUE; // 是否启用 Log 功能 +static int g_bEnLogToFile = TRUE; +static char g_strLogTag[32]; // Log 标志 +static unsigned int g_LogRdPos = 0; +static pthread_t g_logThreadId; +static LOG_PROCESS_INFO g_LogProcessInfo; +static pthread_mutex_t g_logLock; +static PLOG_ITEM g_pLogItemList = NULL; + +static int g_iMinLevel = LOG_Fatal | LOG_Error | LOG_Warn | LOG_Debug | LOG_Info | LOG_Step; + +/** + * @brief Log 调试等级转字符串 + * @param level 调试等级 + * @return 调试等级对应的字符串 + */ +const char* LogLevelToStr(LOG_LEVEL level) +{ + switch(level) + { + case LOG_Test: + return "T"; + + case LOG_Info: + return "I"; + + case LOG_Call: + return "C"; + + case LOG_Debug: + return "D"; + + case LOG_Warn: + return "W"; + + case LOG_Error: + return "E"; + + case LOG_Fatal: + return "F"; + + case LOG_Step: + return "S"; + + case LOG_Devp: + return "V"; + + case LOG_Unknown: + return "U"; + + case LOG_All: + return "A"; + + default: + return "?"; + } +} + +static void __logColorOutput(const char* pColFmt, UT_string* pLog) +{ + if(pLog == NULL) + { + return; + } + + if(pColFmt == NULL) + { + print("%s", utstring_body(pLog)); + } + else + { + if(utstring_find(pLog, -1, "\n", 1) == utstring_len(pLog) - 1) + { + char* pLogArray = utstring_body(pLog); + pLogArray[utstring_len(pLog) - 1] = 0; + + print(pColFmt, pLogArray, '\n'); + + strcat(pLogArray, "\n"); + } + else + { + print(pColFmt, utstring_body(pLog), '\0'); + } + } +} + +static void* __logOutputThread(void *p) +{ + (void)p; + while(TRUE) + { + int isWriteLog = FALSE; + PLOG_ITEM pItem = NULL, pTmp = NULL; + + pthread_mutex_lock(&g_logLock); + + LL_FOREACH_SAFE(g_pLogItemList, pItem, pTmp) + { + UT_string *pLogStr; + struct tm lTime; + int logFileSize = 0; + + if(++g_LogRdPos % 100 == 0) + { + GET_FILE_SIZE(g_LogProcessInfo.logFilePath, logFileSize); + } + + localtime_r(&(pItem->timestamp.tv_sec), &lTime); + + utstring_new(pLogStr); + + if(pItem->isAddTags) + { + utstring_printf(pLogStr, "[%04d-%02d-%02d %02d:%02d:%02d.%03ld] [%s] %s", + lTime.tm_year + 1900, lTime.tm_mon + 1, lTime.tm_mday, + lTime.tm_hour, lTime.tm_min, lTime.tm_sec, pItem->timestamp.tv_usec / 1000, + LogLevelToStr(pItem->level), pItem->pLogContent); + } + else + { + utstring_printf(pLogStr, "%s", pItem->pLogContent); + } + + if(pItem->isPrinted == FALSE) + { + if(pItem->level & LOG_Error + || pItem->level & LOG_Fatal) + { + __logColorOutput(SHOW_CONSOLE_RED, pLogStr); + } + else if(pItem->level & LOG_Warn + || pItem->level & LOG_Unknown) + { + __logColorOutput(SHOW_CONSOLE_YELLOW, pLogStr); + } + else if(pItem->level & LOG_Test + || pItem->level & LOG_Call) + { + __logColorOutput(SHOW_CONSOLE_BLUE, pLogStr); + } + else if(pItem->level & LOG_Devp) + { + __logColorOutput(SHOW_CONSOLE_GREEN, pLogStr); + } + else + { + print("%s", utstring_body(pLogStr)); + } + + pItem->isPrinted = TRUE; + } + + if(g_LogProcessInfo.pLogFile != NULL && g_bEnLogToFile) + { + if(logFileSize >= MAX_LOG_FILE_SIZE) + { + fflush(g_LogProcessInfo.pLogFile); + fclose(g_LogProcessInfo.pLogFile); + + g_LogProcessInfo.pLogFile = fopen(g_LogProcessInfo.logFilePath, "w+"); + } + + if(g_LogProcessInfo.pLogFile) + { + fwrite(utstring_body(pLogStr), 1, utstring_len(pLogStr), g_LogProcessInfo.pLogFile); + } + + isWriteLog = TRUE; + } + + LL_DELETE(g_pLogItemList, pItem); + + utstring_free(pLogStr); + free(pItem->pLogContent); + free(pItem); + + if(g_LogRdPos % 100 == 0) + { + break; + } + } + + pthread_mutex_unlock(&g_logLock); + + usleep(1000); + if(g_LogProcessInfo.pLogFile != NULL && isWriteLog) + { + fflush(g_LogProcessInfo.pLogFile); + } + } +} + +/** + * @brief 设置调试等级 + * @param level 调试等级 + * @param iEnable 1 打开调试等级, 0 关闭调试等级 + */ +void IHW_EnableLogLevel(LOG_LEVEL level, int iEnable) +{ + if(iEnable > 0) + { + g_iMinLevel |= level; + } + else + { + g_iMinLevel &= ~(level); + } +} + +/** + * @brief 初始化系统日志功能 + * @param pLogTag 系统日志标志 + * @param pPath 系统日志保存路径 + * @param bEnable 打开/关闭调试信息 + */ +void IHW_InitLOG(const char* pLogTag, const char* pPath, int bEnable) +{ + char strPath[MAX_PATH * 2]; + (void)pPath; + + pthread_mutex_init(&g_logLock, 0); + + g_LogRdPos = 0; + memset(g_strLogTag, 0, 32); + + memset(&g_LogProcessInfo, 0, sizeof(LOG_PROCESS_INFO)); + + if(pLogTag == NULL) + { + strcpy(g_strLogTag, ""); + } + else + { + strncpy(g_strLogTag, pLogTag, 31); + } + + memset(strPath, 0, MAX_PATH * 2); + + g_LogProcessInfo.pid = getpid(); + if(readlink("/proc/self/exe", strPath, MAX_PATH) == -1) + { + strcpy(g_LogProcessInfo.exeName, pLogTag); + } + else + { + char *pExeName = strrchr(strPath, '/'); + + if(pExeName == NULL) + { + strncpy(g_LogProcessInfo.exeName, strPath, MAX_PATH - 1); + } + else + { + strncpy(g_LogProcessInfo.exeName, pExeName + 1, MAX_PATH - 1); + } + } + + memset(g_LogProcessInfo.logFilePath, 0, MAX_PATH); + sprintf(g_LogProcessInfo.logFilePath, "%s/%s_%d.log", LOG_FILE_BASEDIR, g_LogProcessInfo.exeName, g_LogProcessInfo.pid); + + memset(strPath, 0, MAX_PATH); + //sprintf(strPath, "rm -f %s/%s_*.log > /dev/zero", LOG_FILE_BASEDIR, g_LogProcessInfo.exeName); + //ret = system(strPath); + + //g_LogProcessInfo.pLogFile = fopen(g_LogProcessInfo.logFilePath, "w+"); + + g_bEnableLog = bEnable; + + IHW_RunLogService(); +} + +void IHW_RunLogService(void) +{ + pthread_create(&g_logThreadId, NULL, __logOutputThread, NULL); +} + +static void __logTo(LOG_LEVEL level, int isAddTag, char* pMsg, int isPrint) +{ + PLOG_ITEM pLogItem; + + pLogItem = (PLOG_ITEM)malloc(sizeof(struct LOG_ITEM)); + + if(pLogItem == NULL) + { + return; + } + + pLogItem->pLogContent = strdup(pMsg); + pLogItem->isPrinted = isPrint ? FALSE : TRUE; + pLogItem->level = level; + pLogItem->isAddTags = isAddTag ? TRUE : FALSE; + gettimeofday(&(pLogItem->timestamp), NULL); + + pthread_mutex_lock(&g_logLock); + LL_APPEND(g_pLogItemList, pLogItem); + pthread_mutex_unlock(&g_logLock); +} + +void IHW_LogStrWithoutPrint(int level, char* pMsg) +{ + __logTo(level, TRUE, pMsg, FALSE); +} + +void IHW_LogRawString(int level, char* pMsg) +{ + __logTo(level, TRUE, pMsg, TRUE); +} + +/** + * @brief 输出调试信息 + * @param cFlag 调试信息开关 + * @param pMsg 调试信息内容 + */ +void IHW_LOG_UNTAG(LOG_LEVEL level, const char* pMsg, ...) +{ + va_list arg_ptr; + UT_string *pLogContent; + + if(!g_bEnableLog) + { + return; + } + + // 检查调试等级 + if(!(g_iMinLevel & level)) + { + return; + } + + utstring_new(pLogContent); + va_start(arg_ptr, pMsg); + utstring_printf_va(pLogContent, pMsg, arg_ptr); + va_end(arg_ptr); + + __logTo(level, FALSE, utstring_body(pLogContent), TRUE); + + utstring_free(pLogContent); +} + +/** + * @brief 输出调试信息 + * @param cFlag 调试信息开关 + * @param pMsg 调试信息内容 + */ +void IHW_LOG(LOG_LEVEL level, const char* pMsg, ...) +{ + UT_string* pLogContent = NULL; + va_list arg_ptr; + + if(!g_bEnableLog) + { + fprintf(stdout, "skip2\n"); + return; + } + + // 检查调试等级 + if(!(g_iMinLevel & level)) + { + fprintf(stdout, "skip1\n"); + return; + } + + utstring_new(pLogContent); + va_start(arg_ptr, pMsg); + utstring_printf_va(pLogContent, pMsg, arg_ptr); + va_end(arg_ptr); + + __logTo(level, TRUE, utstring_body(pLogContent), TRUE); + + utstring_free(pLogContent); +} + +const char* LogLeveToString(LOG_LEVEL lv) +{ + switch(lv) + { + case LOG_Fatal: return "LOG_Fatal"; + case LOG_Error: return "LOG_Error"; + case LOG_Warn: return "LOG_Warn"; + case LOG_Debug: return "LOG_Debug"; + case LOG_Info: return "LOG_Info"; + case LOG_Test: return "LOG_Test"; + case LOG_Call: return "LOG_Call"; + case LOG_Devp: return "LOG_Devp"; + case LOG_Step: return "LOG_Step"; + case LOG_Unknown: return "LOG_Unknown"; + case LOG_All: return "LOG_All"; + case LOG_Close: return "LOG_Close"; + } + + return "Unknown"; +} diff --git a/Platform/user/configm/config-server/object_manager/log.h b/Platform/user/configm/config-server/object_manager/log.h new file mode 100644 index 000000000..da32e1ada --- /dev/null +++ b/Platform/user/configm/config-server/object_manager/log.h @@ -0,0 +1,208 @@ +/** @file log.h + @brief + @details + @version 1.0.0 +*/ +#ifndef LOG_H_ +#define LOG_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef __KERNEL__ +#include +#include +#else +#include +#endif + +#ifndef TRUE +#define TRUE (1) +#endif + +#ifndef FALSE +#define FALSE (0) +#endif + +#ifndef MAX +/** @def MAX + @brief 取最大值 +*/ +#define MAX(a, b) (((a) > (b)) ? (a) : (b)) +#endif + +#ifndef MIN +/** @def MIN + @brief 取最小值 +*/ +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) +#endif + +#define TIMEZONE_EAST_8H (8 * 3600) + +#ifndef __KERNEL__ +#define DUMP_PREFIX_ADDRESS (1) +#define DUMP_PREFIX_OFFSET (2) +#endif + +#define print(format, ...) fprintf(stdout, format, __VA_ARGS__) + +typedef enum +{ + CMD_LOG_ENABLE = 0, + CMD_LOG_FILE = 1, + CMD_LOG_MAIL = 2, + CMD_LOG_LEVEL = 3, + CMD_LOG_NETWORK = 4, + CMD_LOG_SERVER = 5 +} LOG_CFG_CMD; + +/** @enum _LOG_LEVEL_ + * LOG等级枚举变量 + */ +typedef enum +{ + LOG_Fatal = (1 << 0), + LOG_Error = (1 << 1), + LOG_Warn = (1 << 2), + LOG_Debug = (1 << 3), + LOG_Info = (1 << 4), + LOG_Test = (1 << 5), + LOG_Call = (1 << 6), + LOG_Devp = (1 << 7), + LOG_Step = (1 << 8), + LOG_Unknown = (1 << 9), + LOG_All = (0xFFFFFFFF), + LOG_Close = 0x0, +} LOG_LEVEL; +/** @var typedef _LOG_LEVEL_ LOG_LEVEL + * @brief 错误值枚举类型 + */ + +#ifdef DISABLE_LOG +#define LOG_BUF(level, buf, len) +#define LOG_EX(level, format, args...) +#define LOG_EX2(level, format, args...) +#define LOG_TAG_EX(tag, level, format, args...) +#define DEBUG_CODE_LINE() +#define DEBUG_FUNCTION_BEGIN() +#define DEBUG_FUNCTION_END() +#else +#define LOG_BUF(level, buf, len) do { \ + const char* pFmtBuf = format_hex_buf("", DUMP_PREFIX_ADDRESS, 16, 1, buf, len, 1); \ + IHW_LOG(level, "[%s] - %s(%04d): %s[0-%d]:\n%s", basename_v2(__FILE__), __FUNCTION__, __LINE__, \ + #buf, len, pFmtBuf); \ + free((void*)pFmtBuf); \ +} while(0); +/*! \def LOG_EX + \brief 系统日志调试宏标识 +*/ +#define LOG_EX(level, format, args...) (IHW_LOG(level, "[%s] - %s(%04d):" format , basename_v2(__FILE__), __FUNCTION__, __LINE__, ##args)) + +/*! \def LOG_TAG_EX + \brief 系统日志调试宏标识 +*/ +#define LOG_TAG_EX(tag, level, format, args...) (IHW_LOG(level, "{%s} [%s] %s(%04d):" format , tag, basename_v2(__FILE__), __FUNCTION__, __LINE__, ##args)) + +#define LOG_EX2(level, format, args...) (IHW_LOG_UNTAG(level, format , ##args)) + +/*! @def APP_BUILD_INFO + @brief 应用程序编译信息 +*/ +#define APP_BUILD_INFO(appname, appver) (IHW_LOG(LOG_Info, "%s Ver:%s (Build: %s %s GCC Ver:%s) With %d(bits) OS\n", \ + appname, appver, __DATE__, __TIME__, __VERSION__, sizeof(int*) * 8)) + + +/*! @def DEBUG_CODE_LINE + @brief 输出当前函数名,行号 +*/ +#define DEBUG_CODE_LINE() (LOG_EX(LOG_Info, "\n")) + +/*! @def DEBUG_FUNCTION_BEGIN + @brief 函数入口标志 +*/ +#define DEBUG_FUNCTION_BEGIN() (LOG_EX(LOG_Call, "+++++\n")) + +/*! @def DEBUG_FUNCTION_END + @brief 函数出口标志 +*/ +#define DEBUG_FUNCTION_END() (LOG_EX(LOG_Call, "-----\n")) + +/** + * @brief 输出调试信息 + * @param level 调试信息开关 + * @param pMsg 调试信息内容 + */ +void IHW_LOG(LOG_LEVEL level, const char* pMsg, ...); +void IHW_LOG_UNTAG(LOG_LEVEL level, const char* pMsg, ...); + +void IHW_LogStrWithoutPrint(int level, char* pMsg); +void IHW_LogRawString(int level, char* pMsg); + +/** + * @brief 设置调试等级 + * @param level 调试等级 + * @param iEnable 1 打开调试等级, 0 关闭调试等级 + */ +void IHW_EnableLogLevel(LOG_LEVEL level, int iEnable); + +/** + * @brief 初始化系统日志功能 + * @param pLogTag 系统日志标志 + * @param pPath 系统日志保存路径 + * @param bEnable 打开/关闭调试信息 + */ +void IHW_InitLOG(const char* pLogTag, const char* pPath, int bEnable); + +void IHW_RunLogService(void); + +/** + * @brief 判断文件、路径是否存在 + * @param pPath - 文件路径 + * @return int 存在返回 1, 否则返回 0; + */ +int IHW_IsFileExist(const char* pPath); + +char* IHW_bin2hex(char *p, const unsigned char *cp, int count); + +/* Return the last part of a pathname */ +static inline const char* basename_v2(const char* path) +{ + const char* tail = strrchr(path, '/'); + return tail ? tail + 1 : path; +} + +static inline int dirname_v2(const char* path, char* dir) +{ + const char* tail = strrchr(path, '/'); + + if(tail) + { + memcpy(dir, path, tail - path); + dir[tail - path] = 0; + } + else + { + strcpy(dir, "./"); + } + + return 0; +} +#endif + +const char* LogLeveToString(LOG_LEVEL lv); + +const char* format_hex_buf(const char* prefix_str, int prefix_type, + int rowsize, int groupsize, + const void* buf, int len, int ascii); +#ifndef __KERNEL__ +void print_hex_dump_bytes(const char* prefix_str, int prefix_type, + const void* buf, int len); +#endif + +#ifdef __cplusplus +} +#endif +#endif //LOG_H_ + diff --git a/Platform/user/configm/config-server/object_manager/object_manager.c b/Platform/user/configm/config-server/object_manager/object_manager.c new file mode 100644 index 000000000..ddecdf911 --- /dev/null +++ b/Platform/user/configm/config-server/object_manager/object_manager.c @@ -0,0 +1,43 @@ +// +// Created by xajhu on 2019/10/10 0010. +// +#include "object_manager.h" +#include "configm.h" + +#include "log.h" + +ret_code object_config_init() +{ + IHW_InitLOG("obj", NULL, TRUE); + return RET_OK; +} + +ret_code object_config_chk(uint source, uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len) +{ + return RET_OK; +} + +ret_code object_config_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len) +{ + LOG_EX(LOG_Debug, "[%016X]:type = %u, %s\n", source, config_type, (const char*)input); + return RET_OK; +} + +ret_code object_config_get(uint source, + pointer input, int input_len, + pointer output, int *output_len) +{ + LOG_EX(LOG_Debug, "[%016X]: %s\n", source, (const char*)input); + return RET_OK; +} + +ret_code object_config_get_all(uint source, + pointer output, int *output_len) +{ + LOG_EX(LOG_Debug, "[%016X]: \n", source); + return RET_OK; +} diff --git a/Platform/user/configm/config-server/object_manager/object_manager.h b/Platform/user/configm/config-server/object_manager/object_manager.h new file mode 100644 index 000000000..1ab6aaab8 --- /dev/null +++ b/Platform/user/configm/config-server/object_manager/object_manager.h @@ -0,0 +1,22 @@ +// +// Created by xajhu on 2019/10/10 0010. +// + +#ifndef DRIVERS_OBJECT_MANAGER_H +#define DRIVERS_OBJECT_MANAGER_H +#include "rpc/rpc_common.h" + +ret_code object_config_init(); + +ret_code object_config_chk(uint source, uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len); +ret_code object_config_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len); +ret_code object_config_get(uint source, + pointer input, int input_len, + pointer output, int *output_len); +ret_code object_config_get_all(uint source, + pointer output, int *output_len); +#endif //DRIVERS_OBJECT_MANAGER_H diff --git a/Product/build/user.object.Makefile b/Product/build/user.object.Makefile index 32d5ff920..6a021016c 100755 --- a/Product/build/user.object.Makefile +++ b/Product/build/user.object.Makefile @@ -35,13 +35,16 @@ PLAT_LINUX_SRCS := $(subst $(SRC_DIR)/, , $(wildcard $(SRC_DIR)/*.c)) PLAT_ARM64_SRCS := $(PLAT_LINUX_SRCS) # gcc CFLAGS -COMMON_CFLAGS := -I../../Common +COMMON_CFLAGS := -I../../Common -I../../Platform/common -I../../Product/common -DOBJECT_DEMO=1 PLAT_LINUX_CFLAGS += $(COMMON_CFLAGS) PLAT_ARM64_CFLAGS += $(COMMON_CFLAGS) -D_GNU_SOURCE -D__USE_XOPEN +PLAT_ARM64_LDFLAGS := -L ../../Platform/build/debug +PLAT_LINUX_LDFLAGS := $(PLAT_ARM64_LDFLAGS) + COMMON_LIBS := -lcjson -lpthread -lpcre -PLAT_LINUX_LIBS := $(COMMON_LIBS) -PLAT_ARM64_LIBS := $(COMMON_LIBS) +PLAT_ARM64_LIBS := -lopenrpc-$(ARM64_OBJ_TARGET) -lconfigmapi-$(ARM64_OBJ_TARGET) $(COMMON_LIBS) +PLAT_LINUX_LIBS := -lopenrpc-$(LINUX_OBJ_TARGET) -lconfigmapi-$(LINUX_OBJ_TARGET) $(COMMON_LIBS) # this line must be at below of thus, because of... include ../../Common/common.Makefile diff --git a/Product/common/common.h b/Product/common/common.h index 483676dcb..9e34686e7 100644 --- a/Product/common/common.h +++ b/Product/common/common.h @@ -28,7 +28,7 @@ } \ } while (0) -#if 1 +#if OBJECT_DEMO #define RET_OK 0 #define RET_ERR 1 #define RET_UNKNOWN 2 diff --git a/Product/user/object_manager/json_interface.c b/Product/user/object_manager/json_interface.c index 62a23dc44..08157d803 100644 --- a/Product/user/object_manager/json_interface.c +++ b/Product/user/object_manager/json_interface.c @@ -1,5 +1,3 @@ -#include -#include #include #include #include @@ -865,8 +863,7 @@ static int __obj_search_decode(const char *pJsonS, void **pStruct) { cJSON *pRoot, *pVal; PIFACE_SEARCH_OBJ pData; - int arraySize; - int i; + *pStruct = malloc(sizeof(IFACE_SEARCH_OBJ)); if(*pStruct == NULL) { @@ -908,55 +905,55 @@ static int __obj_search_decode(const char *pJsonS, void **pStruct) return RET_OK; } -static int __obj_search_encode(const char *pJsonS, void **pStruct) +static const char *__obj_search_encode(void *pData) { - cJSON *pRoot, *pVal; - PIFACE_SEARCH_LIST pData; - *pStruct = malloc(sizeof(IFACE_SEARCH_LIST)); + const char *pJsonS; + PIFC_RET_MSG p = (PIFC_RET_MSG)pData; + cJSON *pRoot = cJSON_CreateObject(); - if(*pStruct == NULL) { - return -RET_NOMEM; + if(!p) { + return NULL; } - pData = (PIFACE_SEARCH_LIST) * pStruct; - pRoot = cJSON_Parse(pJsonS); + cJSON_AddNumberToObject(pRoot, "retCode", p->ret_code); - if(!pRoot) { - return -RET_SYSERR; + if(p->mesg) { + cJSON_AddStringToObject(pRoot, "mesg", p->mesg); } - memset(pData, 0, sizeof(IFACE_SEARCH_LIST)); - pVal = cJSON_GetObjectItem(pRoot, "type"); + if(p->n_items > 0) { + cJSON *pArray = cJSON_AddArrayToObject(pRoot, "data"); - if(cJSON_IsNumber(pVal)) { - pData->type = pVal->valueint; - } - - pVal = cJSON_GetObjectItem(pRoot, "start"); - - if(cJSON_IsNumber(pVal)) { - pData->start = pVal->valueint; - } - - pVal = cJSON_GetObjectItem(pRoot, "end"); - - if(cJSON_IsNumber(pVal)) { - pData->end = pVal->valueint; - } - - pVal = cJSON_GetObjectItem(pRoot, "session"); - - if(cJSON_IsString(pVal)) { - strncpy(pData->session, pVal->valuestring, MAX_SESSION - 1); + if(pArray) { + int i; + + for(i = 0; i < p->n_items; i++) { + cJSON *pItem = cJSON_CreateObject(); + + if(!pItem) { + continue; + } + + cJSON_AddStringToObject(pItem, "name", p->data[i].name); + cJSON_AddNumberToObject(pItem, "retCode", p->data[i].ret_code); + + if(p->data[i].mesg) { + cJSON_AddStringToObject(pItem, "mesg", p->data[i].mesg); + } + + cJSON_AddItemToArray(pArray, pItem); + } + } } + pJsonS = cJSON_Print(pRoot); cJSON_Delete(pRoot); - return RET_OK; + return pJsonS; } static JSON_ENGINE g_jSonEngine[] = { {JE_INTERFACE, __interface_encode, __interface_decode, NULL}, - {OBJ_CMD_ADD, __obj_add_encode, __obj_add_decode, NULL}, + {JE_OBJ_ADD, __obj_add_encode, __obj_add_decode, NULL}, {JE_OBJ_DEL, __obj_del_encode, __obj_del_decode, NULL}, {JE_OBJ_MOD, __obj_mod_encode, __obj_mod_decode, NULL}, {JE_OBJ_QUERYLIST, __obj_query_list_encode, __obj_query_list_decode, NULL}, diff --git a/Product/user/object_manager/json_interface.h b/Product/user/object_manager/json_interface.h index 1feea2d28..03cb773eb 100644 --- a/Product/user/object_manager/json_interface.h +++ b/Product/user/object_manager/json_interface.h @@ -82,7 +82,7 @@ typedef struct { typedef struct { char name[MAX_NAME_LEN]; char *mesg; - char* desc[MAX_DESC]; + char desc[MAX_DESC]; int prio; int ret_code; POBJECT_K pObjK; diff --git a/Product/user/object_manager/log.c b/Product/user/object_manager/log.c index 23133bca6..d07d7a276 100644 --- a/Product/user/object_manager/log.c +++ b/Product/user/object_manager/log.c @@ -6,17 +6,14 @@ #include #include #include -#include #include #include #include -#include #include #include #include #include #include -#include #include "log.h" #include "../../common/common.h" @@ -28,9 +25,7 @@ #define LOG_FILE_BASEDIR (".") -#define MAX_LOG_ITEM (1000) #define MAX_LOG_FILE_SIZE (1024 * 1024) -#define LOG_PRE_SIZE (512) #define MAX_PATH (256) typedef struct LOG_ITEM @@ -107,8 +102,6 @@ const char* LogLevelToStr(LOG_LEVEL level) default: return "?"; } - - return "U"; } static void __logColorOutput(const char* pColFmt, UT_string* pLog) @@ -142,6 +135,7 @@ static void __logColorOutput(const char* pColFmt, UT_string* pLog) static void* __logOutputThread(void *p) { + (void)p; while(TRUE) { int isWriteLog = FALSE; @@ -243,9 +237,6 @@ static void* __logOutputThread(void *p) fflush(g_LogProcessInfo.pLogFile); } } - - pthread_detach(pthread_self()); - return (NULL); } /** @@ -274,6 +265,7 @@ void IHW_EnableLogLevel(LOG_LEVEL level, int iEnable) void IHW_InitLOG(const char* pLogTag, const char* pPath, int bEnable) { char strPath[MAX_PATH * 2]; + (void)pPath; pthread_mutex_init(&g_logLock, 0); diff --git a/Product/user/object_manager/main.c b/Product/user/object_manager/main.c index 95dcd2175..33275de9a 100644 --- a/Product/user/object_manager/main.c +++ b/Product/user/object_manager/main.c @@ -1,22 +1,22 @@ #include #include #include -#include -#include #include -#include #include #include #include #include #include #include +#include #include "compile.h" #include "log.h" #include "object_manager.h" #include "json_interface.h" #include "regex_table.h" +#include "rpc/rpc.h" +#include "ret_errno.h" #define MAX_OBJ (300) @@ -28,39 +28,39 @@ #define DETAIL_JS_FILE (JS_FILE_ROOT"/secogateway/Product/user/object_manager/json_test/detail.json") #define SEARCH_JS_FILE (JS_FILE_ROOT"/secogateway/Product/user/object_manager/json_test/search.json") -static PSPLIT_PAGE g_pSplitPage = NULL; +static PSPLIT_PAGE g_pSplitPage = NULL; static PCMHI_OBJECT g_pObject = NULL; static OBJECT_K g_objItem[MAX_OBJ]; static pthread_mutex_t g_obj_lock = PTHREAD_MUTEX_INITIALIZER; -static void random_string(unsigned char *buf, int size) +static void create_session(unsigned char *buf, int size) { int i; const char *asc_char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; int max = strlen(asc_char); srand(time(NULL)); - for(i = 0; i < size && buf; i++) { + for (i = 0; i < size && buf; i++) { buf[i] = asc_char[rand() % max]; } - LOG_EX(LOG_Debug, "random string(%d): %s\n", strlen(buf), (char *)buf); + LOG_EX(LOG_Debug, "random string(%d): %s\n", strlen((const char *) buf), (char *) buf); } static int split_params(char *pInput, char **pFirst, char **pSecond, const char *split) { - char *pStr = (char *)pInput; + char *pStr = (char *) pInput; char *p; - if(!pInput || !pFirst || !pSecond) { + if (!pInput || !pFirst || !pSecond) { LOG_EX(LOG_Error, "Input params error: %p, %p, %p\n", pInput, pFirst, pSecond); return -RET_INPUTERR; } p = strsep(&pStr, split); - if(strlen(p) == 0) { + if (strlen(p) == 0) { *pFirst = pInput; *pSecond = NULL; } else { @@ -75,8 +75,8 @@ static int get_free_pos(void) { int i; - for(i = 0; i < MAX_OBJ; i++) { - if(strlen(g_objItem[i].memTag) == 0) { + for (i = 0; i < MAX_OBJ; i++) { + if (strlen(g_objItem[i].memTag) == 0) { return i; } } @@ -98,13 +98,13 @@ static void dump_object(void) LOG_EX(LOG_Info, "refs: %d\n", g_objItem[pObj->obj_index].ref_count); LOG_EX(LOG_Info, "items: %d\n", g_objItem[pObj->obj_index].ctx_num); - for(i = 0; i < g_objItem[pObj->obj_index].ctx_num; i++) { + for (i = 0; i < g_objItem[pObj->obj_index].ctx_num; i++) { struct tm *pTm; char buf[256]; time_t t; POBJECT_K pObjK = &g_objItem[pObj->obj_index]; - switch(pObjK->type / 10) { + switch (pObjK->type / 10) { case OBJ_TYPE_SERVER: LOG_EX(LOG_Info, " porType: %d\n", pObjK->objs.svr_obj[i].pro_type); LOG_EX(LOG_Info, " min Port: %d\n", pObjK->objs.svr_obj[i].min_port); @@ -115,7 +115,7 @@ static void dump_object(void) LOG_EX(LOG_Info, " ip_version: %d\n", pObjK->objs.addr_obj[i].ip_version); LOG_EX(LOG_Info, " netmask: %u\n", pObjK->objs.addr_obj[i].net_mask); - if(IP_V4 == pObjK->objs.addr_obj[i].ip_version) { + if (IP_V4 == pObjK->objs.addr_obj[i].ip_version) { LOG_EX(LOG_Info, " min addr: %08X\n", pObjK->objs.addr_obj[i].min_addr.addr_v4.ip4_addr); LOG_EX(LOG_Info, " max addr: %08X\n", @@ -165,43 +165,41 @@ static void dump_object(void) int object_split_page(char *pSession, int type, char *outSession) { - PSPLIT_PAGE p; - - if(pSession == NULL || strlen(pSession) == 0) { + if (pSession == NULL || strlen(pSession) == 0) { PCMHI_OBJECT pObj, pTmp; int sess_type = OBJ_TYPE_MAX; - PSPLIT_PAGE pNew = (PSPLIT_PAGE)malloc(sizeof(SPLIT_PAGE)); + PSPLIT_PAGE pNew = (PSPLIT_PAGE) malloc(sizeof(SPLIT_PAGE)); - if(!pNew) { + if (!pNew) { LOG_EX(LOG_Error, "Malloc memory error\n"); return -RET_NOMEM; } memset(pNew, 0, sizeof(SPLIT_PAGE)); - random_string(pNew->session, MAX_SESSION - 1); + create_session((unsigned char *) pNew->session, MAX_SESSION - 1); - if(outSession) { + if (outSession) { strcpy(outSession, pNew->session); } HASH_ADD_STR(g_pSplitPage, session, pNew); - if(type == 1) { + if (type == 1) { sess_type = OBJ_TYPE_SERVER; - } else if(type == 2) { + } else if (type == 2) { sess_type = OBJ_TYPE_ADDR; - } else if(type == 3) { + } else if (type == 3) { sess_type = OBJ_TYPE_DATETIME; } pthread_mutex_lock(&g_obj_lock); HASH_ITER(hh, g_pObject, pObj, pTmp) { - if(g_objItem[pObj->obj_index].type / 10 == sess_type - || sess_type == OBJ_TYPE_MAX) { + if (g_objItem[pObj->obj_index].type / 10 == sess_type + || sess_type == OBJ_TYPE_MAX) { char *pStr = strdup(pObj->name); pNew->last_time = time(NULL); - if(pNew->name_list == NULL) { + if (pNew->name_list == NULL) { utarray_new(pNew->name_list, &ut_str_icd); } @@ -217,11 +215,11 @@ int object_split_page(char *pSession, int type, char *outSession) } } +#if 0 int get_page_items(char *pSession, int type, int start, int end) { int ret = RET_OK; PSPLIT_PAGE p; - char **pStr; int i, iCount = 0; char session[MAX_SESSION]; @@ -244,17 +242,20 @@ int get_page_items(char *pSession, int type, int start, int end) } for(i = start; i < end && iCount < MAX_PAGE_ITEMS && iCount < utarray_len(p->name_list); i++) { - pStr = (char **)utarray_eltptr(p->name_list, i); + char ** pStr = (char **)utarray_eltptr(p->name_list, i); LOG_EX(LOG_Debug, "%s[%03d]: %s\n", p->session, iCount++, *pStr); } + + return ret; } +#endif int object_add(PCMHI_OBJECT pObj, POBJECT_K pObjK) { int i, pos; PCMHI_OBJECT p; - if(!pObj || !pObjK) { + if (!pObj || !pObjK) { LOG_EX(LOG_Error, "Input params error %p, %p\n", pObj, pObjK); return -RET_INPUTERR; } @@ -263,33 +264,33 @@ int object_add(PCMHI_OBJECT pObj, POBJECT_K pObjK) HASH_FIND_STR(g_pObject, pObj->name, p); pthread_mutex_unlock(&g_obj_lock); - if(p != NULL) { + if (p != NULL) { LOG_EX(LOG_Error, "Item %s exists\n", pObj->name); return -RET_EXIST; } pos = get_free_pos(); - if(pos < 0) { + if (pos < 0) { LOG_EX(LOG_Error, "Share memory full\n"); return -RET_NOMEM; } strcpy(pObjK->memTag, OBJ_MEM_TAG); - for(i = 0; i < pObjK->ctx_num && i < MAX_OBJ_CONTENT; i++) { - char *pStart = NULL, *pEnd = NULL; - char *pSubStart = NULL, *pSubEnd = NULL; - PSVR_OBJECT pSvr = &pObjK->objs.svr_obj[i]; - PADDR_OBJECT pAddr = &pObjK->objs.addr_obj[i]; - PDT_OBJECT pDt = &pObjK->objs.dt_obj[i]; + for (i = 0; i < pObjK->ctx_num && i < MAX_OBJ_CONTENT; i++) { + char *pStart = NULL, *pEnd = NULL; + char *pSubStart = NULL, *pSubEnd = NULL; + PSVR_OBJECT pSvr = &pObjK->objs.svr_obj[i]; + PADDR_OBJECT pAddr = &pObjK->objs.addr_obj[i]; + PDT_OBJECT pDt = &pObjK->objs.dt_obj[i]; struct tm tm; - switch(pObjK->type / 10) { + switch (pObjK->type / 10) { case OBJ_TYPE_SERVER: - split_params((char *)pSvr->str_val, &pStart, &pEnd, "-"); + split_params((char *) pSvr->str_val, &pStart, &pEnd, "-"); - if(!pcre_match(REGEX_SVR_PORT, pStart)) { + if (!pcre_match(REGEX_SVR_PORT, pStart)) { LOG_EX(LOG_Error, "Input %s format error\n", pStart); return -RET_INPUTERR; } else { @@ -299,53 +300,45 @@ int object_add(PCMHI_OBJECT pObj, POBJECT_K pObjK) pSvr->max_port = 0; } - if(pEnd && strlen(pEnd) > 0) { + if (pEnd && strlen(pEnd) > 0) { int last; - if(!pcre_match(REGEX_SVR_PORT, pEnd)) { + if (!pcre_match(REGEX_SVR_PORT, pEnd)) { LOG_EX(LOG_Error, "Input %s format error\n", pEnd); return -RET_INPUTERR; } - //first = strtoul(pStart, NULL, 10); last = strtoul(pEnd, NULL, 10); - if(pSvr->pro_type <= PORT_UDP) { - //pSvr->min_port = htonl(first) & 0xFFFF; - pSvr->max_port = last; - } else { - //ICMP , min_port --> type, max_port --> code - //pSvr->min_port = first; - pSvr->max_port = last; - } + pSvr->max_port = last; } break; case OBJ_TYPE_ADDR: - split_params((char *)pAddr->str_val, &pStart, &pEnd, "-"); + split_params((char *) pAddr->str_val, &pStart, &pEnd, "-"); - if(!pcre_match(REGEX_IP_ADDR, pStart)) { + if (!pcre_match(REGEX_IP_ADDR, pStart)) { LOG_EX(LOG_Error, "Input %s format error\n", pStart); return -RET_INPUTERR; } - split_params((char *)pStart, &pSubStart, &pSubEnd, "/"); + split_params((char *) pStart, &pSubStart, &pSubEnd, "/"); - if(pSubEnd && strlen(pSubEnd) > 0) { + if (pSubEnd && strlen(pSubEnd) > 0) { pAddr->net_mask = strtoul(pSubEnd, NULL, 10); } // IPv4 格式 - if(strchr(pStart, ':') == NULL) { - if(inet_pton(AF_INET, pSubStart, &pAddr->min_addr.addr_v4.ip4_addr) != 1) { + if (strchr(pStart, ':') == NULL) { + if (inet_pton(AF_INET, pSubStart, &pAddr->min_addr.addr_v4.ip4_addr) != 1) { LOG_EX(LOG_Error, "%s convert to ip address error\n", pStart); return -RET_INPUTERR; } pAddr->ip_version = IP_V4; } else { - if(inet_pton(AF_INET6, pSubStart, &pAddr->min_addr.addr_v6.addr.addr8) != 1) { + if (inet_pton(AF_INET6, pSubStart, &pAddr->min_addr.addr_v6.addr.addr8) != 1) { LOG_EX(LOG_Error, "%s convert to ip address error\n", pStart); return -RET_INPUTERR; } @@ -353,19 +346,19 @@ int object_add(PCMHI_OBJECT pObj, POBJECT_K pObjK) pAddr->ip_version = IP_V6; } - if(pEnd && strlen(pEnd) > 0) { - if(!pcre_match(REGEX_IP_ADDR, pEnd)) { + if (pEnd && strlen(pEnd) > 0) { + if (!pcre_match(REGEX_IP_ADDR, pEnd)) { LOG_EX(LOG_Error, "Input %s format error\n", pEnd); return -RET_INPUTERR; } - if(pAddr->ip_version == IP_V4) { - if(inet_pton(AF_INET, pEnd, &pAddr->max_addr.addr_v4.ip4_addr) != 1) { + if (pAddr->ip_version == IP_V4) { + if (inet_pton(AF_INET, pEnd, &pAddr->max_addr.addr_v4.ip4_addr) != 1) { LOG_EX(LOG_Error, "%s convert to ip address error\n", pEnd); return -RET_INPUTERR; } } else { - if(inet_pton(AF_INET6, pEnd, &pAddr->max_addr.addr_v6.addr.addr8) != 1) { + if (inet_pton(AF_INET6, pEnd, &pAddr->max_addr.addr_v6.addr.addr8) != 1) { LOG_EX(LOG_Error, "%s convert to ip address error\n", pEnd); return -RET_INPUTERR; } @@ -376,26 +369,26 @@ int object_add(PCMHI_OBJECT pObj, POBJECT_K pObjK) break; case OBJ_TYPE_DATETIME: - split_params((char *)pDt->str_val, &pStart, &pEnd, "-"); + split_params((char *) pDt->str_val, &pStart, &pEnd, "-"); - if(!pcre_match(REGEX_DT, pStart)) { + if (!pcre_match(REGEX_DT, pStart)) { LOG_EX(LOG_Error, "Input %s format error\n", pStart); return -RET_INPUTERR; } else { memset(&tm, 0, sizeof(struct tm)); // YYYY/MM/DD HH:MM 格式 - if(strlen(pStart) > strlen("00:00")) { + if (strlen(pStart) > strlen("00:00")) { strptime(pStart, "%Y/%m/%d %H:%M", &tm); } else { strptime(pStart, "%H:%M", &tm); } - if(tm.tm_year < 119) { + if (tm.tm_year < 119) { tm.tm_year = 119; } - if(tm.tm_mday < 1) { + if (tm.tm_mday < 1) { tm.tm_mday = 1; } @@ -403,25 +396,25 @@ int object_add(PCMHI_OBJECT pObj, POBJECT_K pObjK) pDt->max_time = 0; } - if(pEnd && strlen(pEnd) > 0) { - if(!pcre_match(REGEX_DT, pEnd)) { + if (pEnd && strlen(pEnd) > 0) { + if (!pcre_match(REGEX_DT, pEnd)) { LOG_EX(LOG_Error, "Input %s format error\n", pEnd); return -RET_INPUTERR; } memset(&tm, 0, sizeof(struct tm)); - if(strlen(pEnd) > strlen("00:00")) { + if (strlen(pEnd) > strlen("00:00")) { strptime(pEnd, "%Y/%m/%d %H:%M", &tm); } else { strptime(pEnd, "%H:%M", &tm); } - if(tm.tm_year < 119) { + if (tm.tm_year < 119) { tm.tm_year = 119; } - if(tm.tm_mday < 1) { + if (tm.tm_mday < 1) { tm.tm_mday = 1; } @@ -446,9 +439,9 @@ int object_add(PCMHI_OBJECT pObj, POBJECT_K pObjK) int object_del(const char *pName) { int i; - PCMHI_OBJECT p, pTmp; + PCMHI_OBJECT p; - if(!pName || strlen(pName) == 0) { + if (!pName || strlen(pName) == 0) { return -RET_INPUTERR; } @@ -456,12 +449,12 @@ int object_del(const char *pName) HASH_FIND_STR(g_pObject, pName, p); pthread_mutex_unlock(&g_obj_lock); - if(p == NULL) { + if (p == NULL) { LOG_EX(LOG_Error, "Item %s not exists\n", pName); return -RET_NOTFOUND; } - if(g_objItem[p->obj_index].ref_count > 0) { + if (g_objItem[p->obj_index].ref_count > 0) { LOG_EX(LOG_Error, "Item %s used %d\n", p->name, g_objItem[p->obj_index].ref_count); return -RET_USED; @@ -471,29 +464,29 @@ int object_del(const char *pName) HASH_DEL(g_pObject, p); pthread_mutex_unlock(&g_obj_lock); - for(i = 0; i < g_objItem[p->obj_index].ctx_num && i < MAX_OBJ_CONTENT; i++) { - PSVR_OBJECT pSvr = &g_objItem[p->obj_index].objs.svr_obj[i]; - PADDR_OBJECT pAddr = &g_objItem[p->obj_index].objs.addr_obj[i]; - PDT_OBJECT pDt = &g_objItem[p->obj_index].objs.dt_obj[i]; + for (i = 0; i < g_objItem[p->obj_index].ctx_num && i < MAX_OBJ_CONTENT; i++) { + PSVR_OBJECT pSvr = &g_objItem[p->obj_index].objs.svr_obj[i]; + PADDR_OBJECT pAddr = &g_objItem[p->obj_index].objs.addr_obj[i]; + PDT_OBJECT pDt = &g_objItem[p->obj_index].objs.dt_obj[i]; - switch(g_objItem[p->obj_index].type / 10) { + switch (g_objItem[p->obj_index].type / 10) { case OBJ_TYPE_SERVER: - if(pSvr->str_val) { - free(pSvr->str_val); + if (pSvr->str_val) { + free((void *) pSvr->str_val); } break; case OBJ_TYPE_ADDR: - if(pAddr->str_val) { - free(pAddr->str_val); + if (pAddr->str_val) { + free((void *) pAddr->str_val); } break; case OBJ_TYPE_DATETIME: - if(pDt->str_val) { - free(pDt->str_val); + if (pDt->str_val) { + free((void *) pDt->str_val); } break; @@ -510,7 +503,7 @@ int object_mod(PCMHI_OBJECT pObj, POBJECT_K pObjK) int i; PCMHI_OBJECT p; - if(!pObj || !pObjK) { + if (!pObj || !pObjK) { LOG_EX(LOG_Error, "Input params error %p, %p\n", pObj, pObjK); return -RET_INPUTERR; } @@ -519,7 +512,7 @@ int object_mod(PCMHI_OBJECT pObj, POBJECT_K pObjK) HASH_FIND_STR(g_pObject, pObj->name, p); pthread_mutex_unlock(&g_obj_lock); - if(p == NULL) { + if (p == NULL) { LOG_EX(LOG_Error, "Item %s not exists\n", pObj->name); return -RET_EXIST; } @@ -528,48 +521,48 @@ int object_mod(PCMHI_OBJECT pObj, POBJECT_K pObjK) strncpy(p->desc, pObj->desc, MAX_DESC - 1); // 清理缓存的配置命令资源 - for(i = 0; i < g_objItem[p->obj_index].ctx_num && i < MAX_OBJ_CONTENT; i++) { - PSVR_OBJECT pSvr = &g_objItem[p->obj_index].objs.svr_obj[i]; - PADDR_OBJECT pAddr = &g_objItem[p->obj_index].objs.addr_obj[i]; - PDT_OBJECT pDt = &g_objItem[p->obj_index].objs.dt_obj[i]; + for (i = 0; i < g_objItem[p->obj_index].ctx_num && i < MAX_OBJ_CONTENT; i++) { + PSVR_OBJECT pSvr = &g_objItem[p->obj_index].objs.svr_obj[i]; + PADDR_OBJECT pAddr = &g_objItem[p->obj_index].objs.addr_obj[i]; + PDT_OBJECT pDt = &g_objItem[p->obj_index].objs.dt_obj[i]; - switch(g_objItem[p->obj_index].type / 10) { + switch (g_objItem[p->obj_index].type / 10) { case OBJ_TYPE_SERVER: - if(pSvr->str_val) { - free(pSvr->str_val); + if (pSvr->str_val) { + free((void *) pSvr->str_val); } break; case OBJ_TYPE_ADDR: - if(pAddr->str_val) { - free(pAddr->str_val); + if (pAddr->str_val) { + free((void *) pAddr->str_val); } break; case OBJ_TYPE_DATETIME: - if(pDt->str_val) { - free(pDt->str_val); + if (pDt->str_val) { + free((void *) pDt->str_val); } break; } } - for(i = 0; i < pObjK->ctx_num && i < MAX_OBJ_CONTENT; i++) { - char *pStart = NULL, *pEnd = NULL; - char *pSubStart = NULL, *pSubEnd = NULL; - PSVR_OBJECT pSvr = &pObjK->objs.svr_obj[i]; - PADDR_OBJECT pAddr = &pObjK->objs.addr_obj[i]; - PDT_OBJECT pDt = &pObjK->objs.dt_obj[i]; + for (i = 0; i < pObjK->ctx_num && i < MAX_OBJ_CONTENT; i++) { + char *pStart = NULL, *pEnd = NULL; + char *pSubStart = NULL, *pSubEnd = NULL; + PSVR_OBJECT pSvr = &pObjK->objs.svr_obj[i]; + PADDR_OBJECT pAddr = &pObjK->objs.addr_obj[i]; + PDT_OBJECT pDt = &pObjK->objs.dt_obj[i]; struct tm tm; - switch(pObjK->type / 10) { + switch (pObjK->type / 10) { case OBJ_TYPE_SERVER: - split_params((char *)pSvr->str_val, &pStart, &pEnd, "-"); + split_params((char *) pSvr->str_val, &pStart, &pEnd, "-"); - if(!pcre_match(REGEX_SVR_PORT, pStart)) { + if (!pcre_match(REGEX_SVR_PORT, pStart)) { LOG_EX(LOG_Error, "Input %s format error\n", pStart); return -RET_INPUTERR; } else { @@ -579,10 +572,10 @@ int object_mod(PCMHI_OBJECT pObj, POBJECT_K pObjK) pSvr->max_port = 0; } - if(pEnd && strlen(pEnd) > 0) { + if (pEnd && strlen(pEnd) > 0) { int last; - if(!pcre_match(REGEX_SVR_PORT, pEnd)) { + if (!pcre_match(REGEX_SVR_PORT, pEnd)) { LOG_EX(LOG_Error, "Input %s format error\n", pEnd); return -RET_INPUTERR; } @@ -590,42 +583,35 @@ int object_mod(PCMHI_OBJECT pObj, POBJECT_K pObjK) //first = strtoul(pStart, NULL, 10); last = strtoul(pEnd, NULL, 10); - if(pSvr->pro_type <= PORT_UDP) { - //pSvr->min_port = htonl(first) & 0xFFFF; - pSvr->max_port = last; - } else { - //ICMP , min_port --> type, max_port --> code - //pSvr->min_port = first; - pSvr->max_port = last; - } + pSvr->max_port = last; } break; case OBJ_TYPE_ADDR: - split_params((char *)pAddr->str_val, &pStart, &pEnd, "-"); + split_params((char *) pAddr->str_val, &pStart, &pEnd, "-"); - if(!pcre_match(REGEX_IP_ADDR, pStart)) { + if (!pcre_match(REGEX_IP_ADDR, pStart)) { LOG_EX(LOG_Error, "Input %s format error\n", pStart); return -RET_INPUTERR; } - split_params((char *)pStart, &pSubStart, &pSubEnd, "/"); + split_params((char *) pStart, &pSubStart, &pSubEnd, "/"); - if(pSubEnd && strlen(pSubEnd) > 0) { + if (pSubEnd && strlen(pSubEnd) > 0) { pAddr->net_mask = strtoul(pSubEnd, NULL, 10); } // IPv4 格式 - if(strchr(pStart, ':') == NULL) { - if(inet_pton(AF_INET, pSubStart, &pAddr->min_addr.addr_v4.ip4_addr) != 1) { + if (strchr(pStart, ':') == NULL) { + if (inet_pton(AF_INET, pSubStart, &pAddr->min_addr.addr_v4.ip4_addr) != 1) { LOG_EX(LOG_Error, "%s convert to ip address error\n", pStart); return -RET_INPUTERR; } pAddr->ip_version = IP_V4; } else { - if(inet_pton(AF_INET6, pSubStart, &pAddr->min_addr.addr_v6.addr.addr8) != 1) { + if (inet_pton(AF_INET6, pSubStart, &pAddr->min_addr.addr_v6.addr.addr8) != 1) { LOG_EX(LOG_Error, "%s convert to ip address error\n", pStart); return -RET_INPUTERR; } @@ -633,19 +619,19 @@ int object_mod(PCMHI_OBJECT pObj, POBJECT_K pObjK) pAddr->ip_version = IP_V6; } - if(pEnd && strlen(pEnd) > 0) { - if(!pcre_match(REGEX_IP_ADDR, pEnd)) { + if (pEnd && strlen(pEnd) > 0) { + if (!pcre_match(REGEX_IP_ADDR, pEnd)) { LOG_EX(LOG_Error, "Input %s format error\n", pEnd); return -RET_INPUTERR; } - if(pAddr->ip_version == IP_V4) { - if(inet_pton(AF_INET, pEnd, &pAddr->max_addr.addr_v4.ip4_addr) != 1) { + if (pAddr->ip_version == IP_V4) { + if (inet_pton(AF_INET, pEnd, &pAddr->max_addr.addr_v4.ip4_addr) != 1) { LOG_EX(LOG_Error, "%s convert to ip address error\n", pEnd); return -RET_INPUTERR; } } else { - if(inet_pton(AF_INET6, pEnd, &pAddr->max_addr.addr_v6.addr.addr8) != 1) { + if (inet_pton(AF_INET6, pEnd, &pAddr->max_addr.addr_v6.addr.addr8) != 1) { LOG_EX(LOG_Error, "%s convert to ip address error\n", pEnd); return -RET_INPUTERR; } @@ -656,26 +642,26 @@ int object_mod(PCMHI_OBJECT pObj, POBJECT_K pObjK) break; case OBJ_TYPE_DATETIME: - split_params((char *)pDt->str_val, &pStart, &pEnd, "-"); + split_params((char *) pDt->str_val, &pStart, &pEnd, "-"); - if(!pcre_match(REGEX_DT, pStart)) { + if (!pcre_match(REGEX_DT, pStart)) { LOG_EX(LOG_Error, "Input %s format error\n", pStart); return -RET_INPUTERR; } else { memset(&tm, 0, sizeof(struct tm)); // YYYY/MM/DD HH:MM 格式 - if(strlen(pStart) > strlen("00:00")) { + if (strlen(pStart) > strlen("00:00")) { strptime(pStart, "%Y/%m/%d %H:%M", &tm); } else { strptime(pStart, "%H:%M", &tm); } - if(tm.tm_year < 119) { + if (tm.tm_year < 119) { tm.tm_year = 119; } - if(tm.tm_mday < 1) { + if (tm.tm_mday < 1) { tm.tm_mday = 1; } @@ -683,25 +669,25 @@ int object_mod(PCMHI_OBJECT pObj, POBJECT_K pObjK) pDt->max_time = 0; } - if(pEnd && strlen(pEnd) > 0) { - if(!pcre_match(REGEX_DT, pEnd)) { + if (pEnd && strlen(pEnd) > 0) { + if (!pcre_match(REGEX_DT, pEnd)) { LOG_EX(LOG_Error, "Input %s format error\n", pEnd); return -RET_INPUTERR; } memset(&tm, 0, sizeof(struct tm)); - if(strlen(pEnd) > strlen("00:00")) { + if (strlen(pEnd) > strlen("00:00")) { strptime(pEnd, "%Y/%m/%d %H:%M", &tm); } else { strptime(pEnd, "%H:%M", &tm); } - if(tm.tm_year < 119) { + if (tm.tm_year < 119) { tm.tm_year = 119; } - if(tm.tm_mday < 1) { + if (tm.tm_mday < 1) { tm.tm_mday = 1; } @@ -714,9 +700,9 @@ int object_mod(PCMHI_OBJECT pObj, POBJECT_K pObjK) } } - p->prio = pObj->prio; + p->prio = pObj->prio; g_objItem[p->obj_index].ctx_num = pObjK->ctx_num; - g_objItem[p->obj_index].type = pObjK->type; + g_objItem[p->obj_index].type = pObjK->type; memcpy(&g_objItem[p->obj_index].objs, &pObjK->objs, sizeof(g_objItem[p->obj_index].objs)); return RET_OK; } @@ -728,21 +714,21 @@ static const char *read_json_file(const char *pPath) int f_size = 0; GET_FILE_SIZE(pPath, f_size); - if(f_size <= 0) { + if (f_size <= 0) { LOG_EX(LOG_Error, "Get file %p size error: %d\n", pPath, f_size); return NULL; } - pBuf = (unsigned char *)malloc(f_size + 1); + pBuf = (unsigned char *) malloc(f_size + 1); - if(!pBuf) { + if (!pBuf) { LOG_EX(LOG_Error, "Malloc %d bytes memory error: %d\n", f_size + 1); return NULL; } pFile = fopen(pPath, "r"); - if(!pFile) { + if (!pFile) { LOG_EX(LOG_Error, "Open file %p size error\n", pPath); free(pBuf); return NULL; @@ -751,7 +737,7 @@ static const char *read_json_file(const char *pPath) fread(pBuf, 1, f_size, pFile); fclose(pFile); pBuf[f_size] = 0; - return (const char *)pBuf; + return (const char *) pBuf; } static void test_del_object(void) @@ -759,19 +745,19 @@ static void test_del_object(void) int i, ret; const char *pRetJson; IFC_RET_MSG retCtx; - PIFACE_DEL_OBJ pDel = NULL; - PJSON_INTERFACE p = NULL; - const char *pJson = read_json_file(DEL_JS_FILE); + PIFACE_DEL_OBJ pDel = NULL; + PJSON_INTERFACE p = NULL; + const char *pJson = read_json_file(DEL_JS_FILE); ret = Json2Struct(pJson, &p, JE_INTERFACE, FALSE); memset(&retCtx, 0, sizeof(IFC_RET_MSG)); - if(ret != RET_OK || p == NULL) { + if (ret != RET_OK || p == NULL) { LOG_EX(LOG_Error, "Decode json error: %d\n", ret); - free((void *)pJson); + free((void *) pJson); - if(p) { - if(p->msgContent) { - free((void *)p->msgContent); + if (p) { + if (p->msgContent) { + free((void *) p->msgContent); } free(p); @@ -782,13 +768,13 @@ static void test_del_object(void) retCtx.n_items = 0; pRetJson = Struct2Json(&retCtx, JE_OBJ_DEL, FALSE, &ret); - if(!pRetJson || ret != RET_OK) { + if (!pRetJson || ret != RET_OK) { LOG_EX(LOG_Error, "Json format error: %d\n", ret); return; } LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson); - free((void *)pRetJson); + free((void *) pRetJson); return; } @@ -799,40 +785,40 @@ static void test_del_object(void) LOG_EX(LOG_Info, "msgContent: %s\n", p->msgContent); ret = Json2Struct(p->msgContent, &pDel, JE_OBJ_DEL, FALSE); - if(ret != RET_OK || pDel == NULL) { + if (ret != RET_OK || pDel == NULL) { LOG_EX(LOG_Error, "Decode json error: %d\n", ret); - if(pDel) { + if (pDel) { free(pDel); } - if(p->msgContent) { - free((void *)p->msgContent); + if (p->msgContent) { + free((void *) p->msgContent); } free(p); - free((void *)pJson); + free((void *) pJson); retCtx.ret_code = -RET_JSONERR; retCtx.mesg = get_err_message(RET_JSONERR); retCtx.n_items = 0; pRetJson = Struct2Json(&retCtx, JE_OBJ_DEL, FALSE, &ret); - if(!pRetJson || ret != RET_OK) { + if (!pRetJson || ret != RET_OK) { LOG_EX(LOG_Error, "Json format error: %d\n", ret); return; } LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson); - free((void *)pRetJson); + free((void *) pRetJson); return; } else { retCtx.n_items = pDel->n_obj; } - for(i = 0; i < pDel->n_obj; i++) { + for (i = 0; i < pDel->n_obj; i++) { ret = object_del(pDel->name[i]); - if(ret != RET_OK && retCtx.ret_code == RET_OK) { + if (ret != RET_OK && retCtx.ret_code == RET_OK) { retCtx.ret_code = ret; } @@ -845,34 +831,34 @@ static void test_del_object(void) retCtx.mesg = get_err_message(retCtx.ret_code); pRetJson = Struct2Json(&retCtx, JE_OBJ_DEL, FALSE, &ret); - if(!pRetJson || ret != RET_OK) { + if (!pRetJson || ret != RET_OK) { LOG_EX(LOG_Error, "Json format error: %d\n", ret); - if(pRetJson) { - free((void *)pRetJson); + if (pRetJson) { + free((void *) pRetJson); } free(pDel); - if(p->msgContent) { - free((void *)p->msgContent); + if (p->msgContent) { + free((void *) p->msgContent); } free(p); - free((void *)pJson); + free((void *) pJson); return; } LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson); - free((void *)pRetJson); + free((void *) pRetJson); free(pDel); - if(p->msgContent) { - free((void *)p->msgContent); + if (p->msgContent) { + free((void *) p->msgContent); } free(p); - free((void *)pJson); + free((void *) pJson); } static void test_add_object(void) @@ -880,19 +866,19 @@ static void test_add_object(void) int i, ret; const char *pRetJson; IFC_RET_MSG retCtx; - PJSON_INTERFACE p = NULL; - PIFACE_ADD_OBJ pAdd = NULL; - const char *pJson = read_json_file(ADD_JS_FILE); + PJSON_INTERFACE p = NULL; + PIFACE_ADD_OBJ pAdd = NULL; + const char *pJson = read_json_file(ADD_JS_FILE); ret = Json2Struct(pJson, &p, JE_INTERFACE, FALSE); memset(&retCtx, 0, sizeof(IFC_RET_MSG)); - if(ret != RET_OK || p == NULL) { + if (ret != RET_OK || p == NULL) { LOG_EX(LOG_Error, "Decode json error: %d\n", ret); - free((void *)pJson); + free((void *) pJson); - if(p) { - if(p->msgContent) { - free((void *)p->msgContent); + if (p) { + if (p->msgContent) { + free((void *) p->msgContent); } free(p); @@ -903,13 +889,13 @@ static void test_add_object(void) retCtx.n_items = 0; pRetJson = Struct2Json(&retCtx, JE_OBJ_ADD, FALSE, &ret); - if(!pRetJson || ret != RET_OK) { + if (!pRetJson || ret != RET_OK) { LOG_EX(LOG_Error, "Json format error: %d\n", ret); return; } LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson); - free((void *)pRetJson); + free((void *) pRetJson); return; } @@ -920,45 +906,45 @@ static void test_add_object(void) LOG_EX(LOG_Info, "msgContent: %s\n", p->msgContent); ret = Json2Struct(p->msgContent, &pAdd, JE_OBJ_ADD, FALSE); - if(ret != RET_OK || pAdd == NULL || pAdd->pCtx == NULL) { + if (ret != RET_OK || pAdd == NULL || pAdd->pCtx == NULL) { LOG_EX(LOG_Error, "Decode json error: %d\n", ret); - if(pAdd) { - if(pAdd->pCtx) { + if (pAdd) { + if (pAdd->pCtx) { free(pAdd->pCtx); } free(pAdd); } - if(p->msgContent) { - free((void *)p->msgContent); + if (p->msgContent) { + free((void *) p->msgContent); } free(p); - free((void *)pJson); + free((void *) pJson); retCtx.ret_code = -RET_JSONERR; retCtx.mesg = get_err_message(RET_JSONERR); retCtx.n_items = 0; pRetJson = Struct2Json(&retCtx, JE_OBJ_ADD, FALSE, &ret); - if(!pRetJson || ret != RET_OK) { + if (!pRetJson || ret != RET_OK) { LOG_EX(LOG_Error, "Json format error: %d\n", ret); return; } LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson); - free((void *)pRetJson); + free((void *) pRetJson); return; } else { retCtx.n_items = pAdd->n_obj; } - for(i = 0; i < pAdd->n_obj; i++) { + for (i = 0; i < pAdd->n_obj; i++) { LOG_EX(LOG_Debug, "%d: %s\n", i, pAdd->pCtx[i].pObj->name); ret = object_add(pAdd->pCtx[i].pObj, &pAdd->pCtx[i].objk); - if(ret != RET_OK && retCtx.ret_code == RET_OK) { + if (ret != RET_OK && retCtx.ret_code == RET_OK) { retCtx.ret_code = ret; } @@ -967,11 +953,11 @@ static void test_add_object(void) retCtx.data[i].mesg = get_err_message(ret); } - for(i = 0; i < 100; i++) { + for (i = 0; i < 100; i++) { PCMHI_OBJECT pObjItem = malloc(sizeof(CMHI_OBJECT)); POBJECT_K pObjKItem = malloc(sizeof(OBJECT_K)); - if(pObjItem && pObjKItem) { + if (pObjItem && pObjKItem) { memset(pObjKItem, 0, sizeof(OBJECT_K)); memcpy(pObjItem, pAdd->pCtx[0].pObj, sizeof(CMHI_OBJECT)); memcpy(pObjKItem, &pAdd->pCtx[0].objk, sizeof(OBJECT_K)); @@ -988,43 +974,43 @@ static void test_add_object(void) retCtx.mesg = get_err_message(retCtx.ret_code); pRetJson = Struct2Json(&retCtx, JE_OBJ_ADD, FALSE, &ret); - if(!pRetJson || ret != RET_OK) { + if (!pRetJson || ret != RET_OK) { LOG_EX(LOG_Error, "Json format error: %d\n", ret); - if(pRetJson) { - free((void *)pRetJson); + if (pRetJson) { + free((void *) pRetJson); } - if(pAdd->pCtx) { + if (pAdd->pCtx) { free(pAdd->pCtx); } free(pAdd); - if(p->msgContent) { - free((void *)p->msgContent); + if (p->msgContent) { + free((void *) p->msgContent); } free(p); - free((void *)pJson); + free((void *) pJson); return; } LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson); - free((void *)pRetJson); + free((void *) pRetJson); - if(pAdd->pCtx) { + if (pAdd->pCtx) { free(pAdd->pCtx); } free(pAdd); - if(p->msgContent) { - free((void *)p->msgContent); + if (p->msgContent) { + free((void *) p->msgContent); } free(p); - free((void *)pJson); + free((void *) pJson); } static void test_mod_object(void) @@ -1032,19 +1018,19 @@ static void test_mod_object(void) int i, ret; const char *pRetJson; IFC_RET_MSG retCtx; - PJSON_INTERFACE p = NULL; - PIFACE_ADD_OBJ pAdd = NULL; - const char *pJson = read_json_file(MOD_JS_FILE); + PJSON_INTERFACE p = NULL; + PIFACE_ADD_OBJ pAdd = NULL; + const char *pJson = read_json_file(MOD_JS_FILE); ret = Json2Struct(pJson, &p, JE_INTERFACE, FALSE); memset(&retCtx, 0, sizeof(IFC_RET_MSG)); - if(ret != RET_OK || p == NULL) { + if (ret != RET_OK || p == NULL) { LOG_EX(LOG_Error, "Decode json error: %d\n", ret); - free((void *)pJson); + free((void *) pJson); - if(p) { - if(p->msgContent) { - free((void *)p->msgContent); + if (p) { + if (p->msgContent) { + free((void *) p->msgContent); } free(p); @@ -1055,13 +1041,13 @@ static void test_mod_object(void) retCtx.n_items = 0; pRetJson = Struct2Json(&retCtx, JE_OBJ_MOD, FALSE, &ret); - if(!pRetJson || ret != RET_OK) { + if (!pRetJson || ret != RET_OK) { LOG_EX(LOG_Error, "Json format error: %d\n", ret); return; } LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson); - free((void *)pRetJson); + free((void *) pRetJson); return; } @@ -1072,45 +1058,45 @@ static void test_mod_object(void) LOG_EX(LOG_Info, "msgContent: %s\n", p->msgContent); ret = Json2Struct(p->msgContent, &pAdd, JE_OBJ_MOD, FALSE); - if(ret != RET_OK || pAdd == NULL || pAdd->pCtx == NULL) { + if (ret != RET_OK || pAdd == NULL || pAdd->pCtx == NULL) { LOG_EX(LOG_Error, "Decode json error: %d\n", ret); - if(pAdd) { - if(pAdd->pCtx) { + if (pAdd) { + if (pAdd->pCtx) { free(pAdd->pCtx); } free(pAdd); } - if(p->msgContent) { - free((void *)p->msgContent); + if (p->msgContent) { + free((void *) p->msgContent); } free(p); - free((void *)pJson); + free((void *) pJson); retCtx.ret_code = -RET_JSONERR; retCtx.mesg = get_err_message(RET_JSONERR); retCtx.n_items = 0; pRetJson = Struct2Json(&retCtx, JE_OBJ_MOD, FALSE, &ret); - if(!pRetJson || ret != RET_OK) { + if (!pRetJson || ret != RET_OK) { LOG_EX(LOG_Error, "Json format error: %d\n", ret); return; } LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson); - free((void *)pRetJson); + free((void *) pRetJson); return; } else { retCtx.n_items = pAdd->n_obj; } - for(i = 0; i < pAdd->n_obj; i++) { + for (i = 0; i < pAdd->n_obj; i++) { LOG_EX(LOG_Debug, "%d: %s\n", i, pAdd->pCtx[i].pObj->name); ret = object_mod(pAdd->pCtx[i].pObj, &pAdd->pCtx[i].objk); - if(ret != RET_OK && retCtx.ret_code == RET_OK) { + if (ret != RET_OK && retCtx.ret_code == RET_OK) { retCtx.ret_code = ret; } @@ -1123,43 +1109,43 @@ static void test_mod_object(void) retCtx.mesg = get_err_message(retCtx.ret_code); pRetJson = Struct2Json(&retCtx, JE_OBJ_MOD, FALSE, &ret); - if(!pRetJson || ret != RET_OK) { + if (!pRetJson || ret != RET_OK) { LOG_EX(LOG_Error, "Json format error: %d\n", ret); - if(pRetJson) { - free((void *)pRetJson); + if (pRetJson) { + free((void *) pRetJson); } - if(pAdd->pCtx) { + if (pAdd->pCtx) { free(pAdd->pCtx); } free(pAdd); - if(p->msgContent) { - free((void *)p->msgContent); + if (p->msgContent) { + free((void *) p->msgContent); } free(p); - free((void *)pJson); + free((void *) pJson); return; } LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson); - free((void *)pRetJson); + free((void *) pRetJson); - if(pAdd->pCtx) { + if (pAdd->pCtx) { free(pAdd->pCtx); } free(pAdd); - if(p->msgContent) { - free((void *)p->msgContent); + if (p->msgContent) { + free((void *) p->msgContent); } free(p); - free((void *)pJson); + free((void *) pJson); } static void test_strsep(char *pVal) @@ -1167,7 +1153,7 @@ static void test_strsep(char *pVal) char *pStart = NULL, *pEnd = NULL; split_params(pVal, &pStart, &pEnd, "-"); - if(pEnd) { + if (pEnd) { LOG_EX(LOG_Debug, "[%s] Split to First = %s, Second = %s\n", pVal, pStart, pEnd); } else { @@ -1177,7 +1163,7 @@ static void test_strsep(char *pVal) static void test_regex(char *pVal) { - if(pcre_match(REGEX_IP_ADDR, pVal)) { + if (pcre_match(REGEX_IP_ADDR, pVal)) { LOG_EX(LOG_Debug, "[%s] ==> Match\n", pVal); } else { LOG_EX(LOG_Error, "[%s] ==> Not Match\n", pVal); @@ -1195,11 +1181,11 @@ static void test_ipaddr_format(char *pVal) memset(&ip_v6, 0, sizeof(IP6_ADDR)); ret = pcre_match(REGEX_IP_ADDR, pVal); - if(ret != TRUE) { + if (ret != TRUE) { LOG_EX(LOG_Error, "Verify ipadd format error\n"); } - if(strchr(pVal, ':') == NULL) { + if (strchr(pVal, ':') == NULL) { ret = inet_pton(AF_INET, pVal, &ip_v4.ip4_addr); print_hex_dump_bytes("IP", 0, &ip_v4, sizeof(IP4_ADDR)); } else { @@ -1215,7 +1201,7 @@ static void test_str_time(char *pVal) struct tm tm; char buf[255]; - if(!pVal || strlen(pVal) == 0) { + if (!pVal || strlen(pVal) == 0) { LOG_EX(LOG_Error, "Input params error\n"); return; } @@ -1226,11 +1212,11 @@ static void test_str_time(char *pVal) LOG_EX(LOG_Debug, "year = %d, month = %d, day = %d\n", tm.tm_year, tm.tm_mon, tm.tm_mday); - if(tm.tm_year < 119) { + if (tm.tm_year < 119) { tm.tm_year = 119; } - if(tm.tm_mday < 1) { + if (tm.tm_mday < 1) { tm.tm_mday = 1; } @@ -1243,6 +1229,7 @@ static void test_str_time(char *pVal) LOG_EX(LOG_Debug, "asctime: %s / %d\n", buf, mktime(&tm)); } +#if 0 static void test_random_string(char *pVal) { int first; @@ -1263,30 +1250,32 @@ static void test_random_string(char *pVal) free(pBuf); } } +#endif static void test_page_object(char *pVal) { int i, ret; - char **pStr; PSPLIT_PAGE pPage; PCMHI_OBJECT pObj; const char *pRetJson; IFC_RET_PAGE_MSG retCtx; - char session[MAX_SESSION]; int iCount = 0; - PIFACE_QUERY_LIST pQuery = NULL; - PJSON_INTERFACE p = NULL; - const char *pJson = read_json_file(QUERY_LIST_JS_FILE); + PIFACE_QUERY_LIST pQuery = NULL; + PJSON_INTERFACE p = NULL; + const char *pJson = read_json_file(QUERY_LIST_JS_FILE); + + (void) pVal; + ret = Json2Struct(pJson, &p, JE_INTERFACE, FALSE); memset(&retCtx, 0, sizeof(IFC_RET_PAGE_MSG)); - if(ret != RET_OK || p == NULL) { + if (ret != RET_OK || p == NULL) { LOG_EX(LOG_Error, "Decode json error: %d\n", ret); - free((void *)pJson); + free((void *) pJson); - if(p) { - if(p->msgContent) { - free((void *)p->msgContent); + if (p) { + if (p->msgContent) { + free((void *) p->msgContent); } free(p); @@ -1297,13 +1286,13 @@ static void test_page_object(char *pVal) retCtx.n_items = 0; pRetJson = Struct2Json(&retCtx, JE_OBJ_QUERYLIST, FALSE, &ret); - if(!pRetJson || ret != RET_OK) { + if (!pRetJson || ret != RET_OK) { LOG_EX(LOG_Error, "Json format error: %d\n", ret); return; } LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson); - free((void *)pRetJson); + free((void *) pRetJson); return; } @@ -1314,94 +1303,90 @@ static void test_page_object(char *pVal) LOG_EX(LOG_Info, "msgContent: %s\n", p->msgContent); ret = Json2Struct(p->msgContent, &pQuery, JE_OBJ_QUERYLIST, FALSE); - if(ret != RET_OK || pQuery == NULL) { + if (ret != RET_OK || pQuery == NULL) { LOG_EX(LOG_Error, "Decode json error: %d\n", ret); - if(pQuery) { + if (pQuery) { free(pQuery); } - if(p->msgContent) { - free((void *)p->msgContent); + if (p->msgContent) { + free((void *) p->msgContent); } free(p); - free((void *)pJson); + free((void *) pJson); retCtx.ret_code = -RET_JSONERR; retCtx.mesg = get_err_message(RET_JSONERR); retCtx.n_items = 0; pRetJson = Struct2Json(&retCtx, JE_OBJ_QUERYLIST, FALSE, &ret); - if(!pRetJson || ret != RET_OK) { + if (!pRetJson || ret != RET_OK) { LOG_EX(LOG_Error, "Json format error: %d\n", ret); return; } LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson); - free((void *)pRetJson); + free((void *) pRetJson); return; } - if(strlen(pQuery->session) == 0) { + if (strlen(pQuery->session) == 0) { memset(pQuery->session, 0, MAX_SESSION); ret = object_split_page("", pQuery->type, pQuery->session); - if(ret != RET_OK) { + if (ret != RET_OK) { LOG_EX(LOG_Error, "object_split_page error: %d\n", ret); - if(pQuery) { - free(pQuery); - } + free(pQuery); - if(p->msgContent) { - free((void *)p->msgContent); + if (p->msgContent) { + free((void *) p->msgContent); } free(p); - free((void *)pJson); + free((void *) pJson); retCtx.ret_code = ret; retCtx.mesg = get_err_message(retCtx.ret_code); retCtx.n_items = 0; pRetJson = Struct2Json(&retCtx, JE_OBJ_QUERYLIST, FALSE, &ret); - if(!pRetJson || ret != RET_OK) { + if (!pRetJson || ret != RET_OK) { LOG_EX(LOG_Error, "Json format error: %d\n", ret); return; } LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson); - free((void *)pRetJson); + free((void *) pRetJson); return; } } HASH_FIND_STR(g_pSplitPage, pQuery->session, pPage); - if(pPage == NULL) { + if (pPage == NULL) { LOG_EX(LOG_Debug, "Can't found session: %s\n", pQuery->session); - if(pQuery) { - free(pQuery); - } + free(pQuery); - if(p->msgContent) { - free((void *)p->msgContent); + if (p->msgContent) { + free((void *) p->msgContent); } free(p); - free((void *)pJson); + free((void *) pJson); retCtx.ret_code = -RET_NOTFOUND; retCtx.mesg = get_err_message(retCtx.ret_code); retCtx.n_items = 0; pRetJson = Struct2Json(&retCtx, JE_OBJ_QUERYLIST, FALSE, &ret); - if(!pRetJson || ret != RET_OK) { + if (!pRetJson || ret != RET_OK) { LOG_EX(LOG_Error, "Json format error: %d\n", ret); return; } LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson); - free((void *)pRetJson); + free((void *) pRetJson); return; } @@ -1410,13 +1395,13 @@ static void test_page_object(char *pVal) retCtx.session = pQuery->session; retCtx.type = pQuery->type; - if(pPage->name_list) { + if (pPage->name_list) { retCtx.tolItems = utarray_len(pPage->name_list); } else { retCtx.tolItems = 0; } - if(pQuery->start > retCtx.tolItems) { + if (pQuery->start > retCtx.tolItems) { retCtx.start = 0; } else { retCtx.start = pQuery->start; @@ -1425,17 +1410,17 @@ static void test_page_object(char *pVal) retCtx.n_items = 0; pPage->last_time = time(NULL); - for(i = retCtx.start; i < pQuery->end && iCount < MAX_PAGE_ITEMS && iCount < retCtx.tolItems; i++) { - pStr = (char **)utarray_eltptr(pPage->name_list, i); + for (i = retCtx.start; i < pQuery->end && iCount < MAX_PAGE_ITEMS && iCount < retCtx.tolItems; i++) { + char **pStr = (char **) utarray_eltptr(pPage->name_list, i); pthread_mutex_lock(&g_obj_lock); HASH_FIND_STR(g_pObject, *pStr, pObj); pthread_mutex_unlock(&g_obj_lock); - if(pObj) { - retCtx.data[retCtx.n_items].name = pObj->name; - retCtx.data[retCtx.n_items].desc = pObj->desc; + if (pObj) { + retCtx.data[retCtx.n_items].name = pObj->name; + retCtx.data[retCtx.n_items].desc = pObj->desc; retCtx.data[retCtx.n_items].ref_count = g_objItem[pObj->obj_index].ref_count; - retCtx.data[retCtx.n_items].type = g_objItem[pObj->obj_index].type; + retCtx.data[retCtx.n_items].type = g_objItem[pObj->obj_index].type; retCtx.n_items++; } @@ -1445,32 +1430,20 @@ static void test_page_object(char *pVal) retCtx.end = retCtx.start + iCount - 1; pRetJson = Struct2Json(&retCtx, JE_OBJ_QUERYLIST, FALSE, &ret); - if(!pRetJson || ret != RET_OK) { + if (!pRetJson || ret != RET_OK) { LOG_EX(LOG_Error, "Json format error: %d\n", ret); return; } LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson); - free((void *)pRetJson); + free((void *) pRetJson); - /* char **pStr; - int iCount = 0; - PSPLIT_PAGE p, pTmp; - test_add_object(); - get_page_items("", 1, 30, 100); */ - /* object_split_page("", 1); - HASH_ITER(hh, g_pSplitPage, p, pTmp) { - while((pStr = (char **)utarray_next(p->name_list, pStr))) { - LOG_EX(LOG_Debug, "%s[%03d]: %s\n", p->session, iCount++, *pStr); - } - } */ - - if(p->msgContent) { - free((void *)p->msgContent); + if (p->msgContent) { + free((void *) p->msgContent); } free(p); - free((void *)pJson); + free((void *) pJson); } static void test_detail_object(void) @@ -1478,19 +1451,19 @@ static void test_detail_object(void) int i, ret; const char *pRetJson; IFC_RET_DETAIL_MSG retCtx; - PIFACE_DETAIL_OBJ pDetail = NULL; - PJSON_INTERFACE p = NULL; - const char *pJson = read_json_file(DETAIL_JS_FILE); + PIFACE_DETAIL_OBJ pDetail = NULL; + PJSON_INTERFACE p = NULL; + const char *pJson = read_json_file(DETAIL_JS_FILE); ret = Json2Struct(pJson, &p, JE_INTERFACE, FALSE); memset(&retCtx, 0, sizeof(IFC_RET_DETAIL_MSG)); - if(ret != RET_OK || p == NULL) { + if (ret != RET_OK || p == NULL) { LOG_EX(LOG_Error, "Decode json error: %d\n", ret); - free((void *)pJson); + free((void *) pJson); - if(p) { - if(p->msgContent) { - free((void *)p->msgContent); + if (p) { + if (p->msgContent) { + free((void *) p->msgContent); } free(p); @@ -1501,13 +1474,13 @@ static void test_detail_object(void) retCtx.n_items = 0; pRetJson = Struct2Json(&retCtx, JE_OBJ_QUERYDETAIL, FALSE, &ret); - if(!pRetJson || ret != RET_OK) { + if (!pRetJson || ret != RET_OK) { LOG_EX(LOG_Error, "Json format error: %d\n", ret); return; } LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson); - free((void *)pRetJson); + free((void *) pRetJson); return; } @@ -1518,31 +1491,31 @@ static void test_detail_object(void) LOG_EX(LOG_Info, "msgContent: %s\n", p->msgContent); ret = Json2Struct(p->msgContent, &pDetail, JE_OBJ_QUERYDETAIL, FALSE); - if(ret != RET_OK || pDetail == NULL) { + if (ret != RET_OK || pDetail == NULL) { LOG_EX(LOG_Error, "Decode json error: %d\n", ret); - if(pDetail) { + if (pDetail) { free(pDetail); } - if(p->msgContent) { - free((void *)p->msgContent); + if (p->msgContent) { + free((void *) p->msgContent); } free(p); - free((void *)pJson); + free((void *) pJson); retCtx.ret_code = -RET_JSONERR; retCtx.mesg = get_err_message(RET_JSONERR); retCtx.n_items = 0; pRetJson = Struct2Json(&retCtx, JE_OBJ_QUERYDETAIL, FALSE, &ret); - if(!pRetJson || ret != RET_OK) { + if (!pRetJson || ret != RET_OK) { LOG_EX(LOG_Error, "Json format error: %d\n", ret); return; } LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson); - free((void *)pRetJson); + free((void *) pRetJson); return; } else { retCtx.n_items = pDetail->n_obj; @@ -1550,14 +1523,14 @@ static void test_detail_object(void) retCtx.ret_code = RET_OK; - for(i = 0; i < pDetail->n_obj; i++) { + for (i = 0; i < pDetail->n_obj; i++) { PCMHI_OBJECT pObj = NULL; pthread_mutex_lock(&g_obj_lock); HASH_FIND_STR(g_pObject, pDetail->name[i], pObj); pthread_mutex_unlock(&g_obj_lock); strncpy(retCtx.data[i].name, pDetail->name[i], MAX_NAME_LEN - 1); - if(pObj) { + if (pObj) { retCtx.data[i].ret_code = RET_OK; retCtx.data[i].mesg = get_err_message(RET_OK); strncpy(retCtx.data[i].desc, pObj->desc, MAX_DESC - 1); @@ -1573,67 +1546,68 @@ static void test_detail_object(void) retCtx.mesg = get_err_message(retCtx.ret_code); pRetJson = Struct2Json(&retCtx, JE_OBJ_QUERYDETAIL, FALSE, &ret); - if(!pRetJson || ret != RET_OK) { + if (!pRetJson || ret != RET_OK) { LOG_EX(LOG_Error, "Json format error: %d\n", ret); - if(pRetJson) { - free((void *)pRetJson); + if (pRetJson) { + free((void *) pRetJson); } free(pDetail); - if(p->msgContent) { - free((void *)p->msgContent); + if (p->msgContent) { + free((void *) p->msgContent); } free(p); - free((void *)pJson); + free((void *) pJson); return; } LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson); - free((void *)pRetJson); + free((void *) pRetJson); free(pDetail); - if(p->msgContent) { - free((void *)p->msgContent); + if (p->msgContent) { + free((void *) p->msgContent); } free(p); - free((void *)pJson); + free((void *) pJson); } static void test_search_object(char *pVal) { - int i, ret, size; + int i, ret; PCMHI_OBJECT pObj, pTmp; - char **pStr; - PSPLIT_PAGE pPage; char regex_str[MAX_REGEX_LEN]; IFC_RET_PAGE_MSG retCtx; int pos = 0; - const char *pRetJson = NULL; - PIFACE_SEARCH_OBJ pSearch = NULL; - PJSON_INTERFACE p = NULL; - int sess_type = OBJ_TYPE_MAX; - PSPLIT_PAGE pNew = (PSPLIT_PAGE)malloc(sizeof(SPLIT_PAGE)); - const char *pJson = read_json_file(SEARCH_JS_FILE); + const char *pRetJson = NULL; + PIFACE_SEARCH_OBJ pSearch = NULL; + PJSON_INTERFACE p = NULL; + int sess_type = OBJ_TYPE_MAX; + PSPLIT_PAGE pNew = (PSPLIT_PAGE) malloc(sizeof(SPLIT_PAGE)); + const char *pJson = read_json_file(SEARCH_JS_FILE); + + (void) pVal; + ret = Json2Struct(pJson, &p, JE_INTERFACE, FALSE); memset(&retCtx, 0, sizeof(IFC_RET_PAGE_MSG)); - if(ret != RET_OK || p == NULL || pNew == NULL) { + if (ret != RET_OK || p == NULL || pNew == NULL) { LOG_EX(LOG_Error, "Decode json error: %d\n", ret); - free((void *)pJson); + free((void *) pJson); - if(p) { - if(p->msgContent) { - free((void *)p->msgContent); + if (p) { + if (p->msgContent) { + free((void *) p->msgContent); } free(p); } - if(pNew == NULL) { + if (pNew == NULL) { retCtx.ret_code = -RET_NOMEM; } else { retCtx.ret_code = -RET_JSONERR; @@ -1644,13 +1618,13 @@ static void test_search_object(char *pVal) retCtx.n_items = 0; pRetJson = Struct2Json(&retCtx, JE_OBJ_SEARCH, FALSE, &ret); - if(!pRetJson || ret != RET_OK) { + if (!pRetJson || ret != RET_OK) { LOG_EX(LOG_Error, "Json format error: %d\n", ret); return; } LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson); - free((void *)pRetJson); + free((void *) pRetJson); return; } @@ -1661,45 +1635,45 @@ static void test_search_object(char *pVal) LOG_EX(LOG_Info, "msgContent: %s\n", p->msgContent); ret = Json2Struct(p->msgContent, &pSearch, JE_OBJ_SEARCH, FALSE); - if(ret != RET_OK || pSearch == NULL) { + if (ret != RET_OK || pSearch == NULL) { LOG_EX(LOG_Error, "Decode json error: %d\n", ret); free(pNew); - if(pSearch) { + if (pSearch) { free(pSearch); } - if(p->msgContent) { - free((void *)p->msgContent); + if (p->msgContent) { + free((void *) p->msgContent); } free(p); - free((void *)pJson); + free((void *) pJson); retCtx.ret_code = -RET_JSONERR; retCtx.mesg = get_err_message(RET_JSONERR); retCtx.n_items = 0; pRetJson = Struct2Json(&retCtx, JE_OBJ_QUERYLIST, FALSE, &ret); - if(!pRetJson || ret != RET_OK) { + if (!pRetJson || ret != RET_OK) { LOG_EX(LOG_Error, "Json format error: %d\n", ret); return; } LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson); - free((void *)pRetJson); + free((void *) pRetJson); return; } memset(regex_str, 0, MAX_REGEX_LEN); - if(strlen(pSearch->key) == 0) { + if (strlen(pSearch->key) == 0) { regex_str[0] = '.'; regex_str[1] = '*'; - } else if(pSearch->regex) { + } else if (pSearch->regex) { strncpy(regex_str, pSearch->key, MAX_REGEX_LEN - 1); } else { - for(i = 0; i < strlen(pSearch->key); i++) { - if(pSearch->key[i] == '*') { + for (i = 0; i < strlen(pSearch->key); i++) { + if (pSearch->key[i] == '*') { regex_str[pos++] = '.'; regex_str[pos++] = '*'; } else { @@ -1708,11 +1682,11 @@ static void test_search_object(char *pVal) } } - if(pSearch->type == 1) { + if (pSearch->type == 1) { sess_type = OBJ_TYPE_SERVER; - } else if(pSearch->type == 2) { + } else if (pSearch->type == 2) { sess_type = OBJ_TYPE_ADDR; - } else if(pSearch->type == 3) { + } else if (pSearch->type == 3) { sess_type = OBJ_TYPE_DATETIME; } @@ -1721,24 +1695,24 @@ static void test_search_object(char *pVal) pthread_mutex_lock(&g_obj_lock); HASH_ITER(hh, g_pObject, pObj, pTmp) { - if(g_objItem[pObj->obj_index].type / 10 == sess_type - || sess_type == OBJ_TYPE_MAX) { - if(pcre_match_str(regex_str, pObj->name) == TRUE) { + if (g_objItem[pObj->obj_index].type / 10 == sess_type + || sess_type == OBJ_TYPE_MAX) { + if (pcre_match_str(regex_str, pObj->name) == TRUE) { char *pStr = strdup(pObj->name); pNew->last_time = time(NULL); - if(pNew->name_list == NULL) { + if (pNew->name_list == NULL) { utarray_new(pNew->name_list, &ut_str_icd); } utarray_push_back(pNew->name_list, &pStr); retCtx.tolItems++; - if(retCtx.n_items < MAX_PAGE_ITEMS) { - retCtx.data[retCtx.n_items].name = pObj->name; - retCtx.data[retCtx.n_items].desc = pObj->desc; + if (retCtx.n_items < MAX_PAGE_ITEMS) { + retCtx.data[retCtx.n_items].name = pObj->name; + retCtx.data[retCtx.n_items].desc = pObj->desc; retCtx.data[retCtx.n_items].ref_count = g_objItem[pObj->obj_index].ref_count; - retCtx.data[retCtx.n_items].type = g_objItem[pObj->obj_index].type; + retCtx.data[retCtx.n_items].type = g_objItem[pObj->obj_index].type; retCtx.n_items++; } @@ -1748,8 +1722,8 @@ static void test_search_object(char *pVal) } pthread_mutex_unlock(&g_obj_lock); - if(retCtx.tolItems > MAX_PAGE_ITEMS) { - random_string(pNew->session, MAX_SESSION - 1); + if (retCtx.tolItems > MAX_PAGE_ITEMS) { + create_session((unsigned char *) pNew->session, MAX_SESSION - 1); HASH_ADD_STR(g_pSplitPage, session, pNew); retCtx.session = pNew->session; } else { @@ -1764,34 +1738,51 @@ static void test_search_object(char *pVal) pRetJson = Struct2Json(&retCtx, JE_OBJ_QUERYLIST, FALSE, &ret); - if(!pRetJson || ret != RET_OK) { + if (!pRetJson || ret != RET_OK) { LOG_EX(LOG_Error, "Json format error: %d\n", ret); - if(pRetJson) { - free((void *)pRetJson); + if (pRetJson) { + free((void *) pRetJson); } free(pSearch); - if(p->msgContent) { - free((void *)p->msgContent); + if (p->msgContent) { + free((void *) p->msgContent); } free(p); - free((void *)pJson); + free((void *) pJson); return; } LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson); - free((void *)pRetJson); + free((void *) pRetJson); free(pSearch); - if(p->msgContent) { - free((void *)p->msgContent); + if (p->msgContent) { + free((void *) p->msgContent); } free(p); - free((void *)pJson); + free((void *) pJson); +} + +static void test_configm_object(char* pVal) +{ + char* output = NULL; + int output_len; + int cmd = strtoul(pVal, 0, 10); + const char *pJson = read_json_file(SEARCH_JS_FILE); + + web_config_exec_sync(cmd, OBJECT_CONFIG, + (void*)pJson, strlen(pJson) + 1, &output, &output_len); + + if(output) { + free(output); + } + + free((void*)pJson); } /** @@ -1804,18 +1795,19 @@ int main(int argc, char **argv) { int c, optidx = 0; static const struct option long_opts[] = { - { "help", no_argument, NULL, 'h'}, - { "version", no_argument, NULL, 'v'}, - // TODO 添加其它需要处理的参数配置 - { "interface", no_argument, NULL, 'a'}, - { "regex", required_argument, NULL, 'b'}, - { "split_str", required_argument, NULL, 'c'}, - { "ip_addr", required_argument, NULL, 'd'}, - { "str_time", required_argument, NULL, 'e'}, - { "page_split", optional_argument, NULL, 'f'}, - { "detail", optional_argument, NULL, 'g'}, - { "search", optional_argument, NULL, 'i'}, - {NULL, 0, NULL, 0} + {"help", no_argument, NULL, 'h'}, + {"version", no_argument, NULL, 'v'}, + // TODO 添加其它需要处理的参数配置 + {"interface", no_argument, NULL, 'a'}, + {"regex", required_argument, NULL, 'b'}, + {"split_str", required_argument, NULL, 'c'}, + {"ip_addr", required_argument, NULL, 'd'}, + {"str_time", required_argument, NULL, 'e'}, + {"page_split", optional_argument, NULL, 'f'}, + {"detail", optional_argument, NULL, 'g'}, + {"search", optional_argument, NULL, 'i'}, + {"config", optional_argument, NULL, 'j'}, + {NULL, 0, NULL, 0} }; IHW_InitLOG("obj", NULL, TRUE); LOG_EX(LOG_Debug, "OBJECT_K = %u bytes\n", sizeof(OBJECT_K)); @@ -1828,8 +1820,8 @@ int main(int argc, char **argv) LOG_EX(LOG_Debug, "SVR_OBJECT = %u bytes\n", sizeof(SVR_OBJECT) * MAX_OBJ_CONTENT); LOG_EX(LOG_Debug, "ADDR_OBJECT = %u bytes\n", sizeof(ADDR_OBJECT) * MAX_OBJ_CONTENT); - while((c = getopt_long(argc, argv, "ab:c:d:e:f::i::ghv", long_opts, &optidx)) != -1) { - switch(c) { + while ((c = getopt_long(argc, argv, "ab:c:d:e:f::i::j::ghv", long_opts, &optidx)) != -1) { + switch (c) { case 'v': LOG_EX(LOG_Info, "User demo version: %s(%s)\n", sGATE_GIT_TAGS, sGATE_GIT_VERS); break; @@ -1843,11 +1835,11 @@ int main(int argc, char **argv) LOG_EX2(LOG_Info, "\t-i, --interface test object add interface\n"); break; - //TODO 添加其它必要处理参数过程 + //TODO 添加其它必要处理参数过程 case 'a': test_add_object(); - //test_del_object(); - //test_mod_object(); + test_del_object(); + test_mod_object(); break; case 'b': @@ -1880,10 +1872,15 @@ int main(int argc, char **argv) test_add_object(); test_search_object(optarg); break; + + + case 'j': + test_configm_object(optarg); + break; } } - while(TRUE) { + while (TRUE) { sleep(1); }