diff --git a/confingure/prase_config.c b/confingure/prase_config.c index 6c3e451..732bffd 100644 --- a/confingure/prase_config.c +++ b/confingure/prase_config.c @@ -1,210 +1,428 @@ -// -// Created by xajhu on 2019/11/18 0018. -// -#include -#include -#include -#include -#include -#include -#include -#include - -#include "common.h" -#include "log.h" -#include "err_code.h" -#include "config.h" - -static char *g_ztpFilePath = NULL; -static config_t g_ztpCfg; -static pthread_t g_monThreadId; -static ZTP_CONFIG g_ztpInfo; - -int reload_ztp_config(const char *pPath) -{ - ZTP_CONFIG ztpInfo; - - memset(&ztpInfo, 0, sizeof(ZTP_CONFIG)); - - if(config_read_file(&g_ztpCfg, pPath) != CONFIG_TRUE) { - LOG_EX(LOG_Error, "Read ztp configre file %s error %s at %d\n", pPath, - config_error_text(&g_ztpCfg), config_error_file(&g_ztpCfg)); - - config_destroy(&g_ztpCfg); - return -ERR_READFILE; - } - - ztpInfo.svr_cfg.port = cfg_get_int_value("ztp.server.port", 10000); - strncpy(ztpInfo.svr_cfg.server_url, - cfg_get_string_value("ztp.server.domain", ""), MAX_PATH * 2 - 1); - strncpy(ztpInfo.dev_dir, - cfg_get_string_value("ztp.dev_dir", DEVICE_ZTP_PATH), MAX_PATH - 1); - - if(g_ztpInfo.svr_cfg.port != ztpInfo.svr_cfg.port) { - LOG_EX(LOG_Info, "Port Changed: [%d] --> [%d]\n", g_ztpInfo.svr_cfg.port, ztpInfo.svr_cfg.port); - g_ztpInfo.svr_cfg.port = ztpInfo.svr_cfg.port; - } - - if(strcmp(g_ztpInfo.svr_cfg.server_url, ztpInfo.svr_cfg.server_url) != 0) { - LOG_EX(LOG_Info, "Url Changed: [%s] --> [%s]\n", g_ztpInfo.svr_cfg.server_url, - ztpInfo.svr_cfg.server_url); - - memset(g_ztpInfo.svr_cfg.server_url, 0, MAX_PATH * 2); - strncpy(g_ztpInfo.svr_cfg.server_url, ztpInfo.svr_cfg.server_url, MAX_PATH * 2 - 1); - } - - if(strcmp(g_ztpInfo.dev_dir, ztpInfo.dev_dir) != 0) { - LOG_EX(LOG_Info, "Device Changed: [%s] --> [%s]\n", g_ztpInfo.dev_dir, ztpInfo.dev_dir); - memset(g_ztpInfo.dev_dir, 0, MAX_PATH); - strncpy(g_ztpInfo.dev_dir, ztpInfo.dev_dir, MAX_PATH - 1); - } - - return ERR_OK; -} - -static void *__cfg_file_monitor(void *p) -{ - int wd; - int fd = inotify_init(); - - if(fd == -1) { - LOG_EX(LOG_Error, "Create file watch error: %s\n", strerror(errno)); - return NULL; - } - - wd = inotify_add_watch(fd, g_ztpFilePath, - (IN_CREATE + IN_DELETE + IN_MODIFY + IN_MOVED_FROM + IN_MOVED_TO)); - - if(wd < 0) { - LOG_EX(LOG_Error, "Watch %s error %s\n", g_ztpFilePath, strerror(errno)); - close(fd); - free(g_ztpFilePath); - return NULL; - } - - while(g_ztpFilePath && strlen(g_ztpFilePath) > 0) { - unsigned char buf[MAX_PATH]; - struct inotify_event *event = NULL; - fd_set fds; - FD_ZERO(&fds); - FD_SET(fd, &fds); - - memset(buf, 0, MAX_PATH); - - if(select(fd + 1, &fds, NULL, NULL, NULL) > 0) { - unsigned int len, index = 0; - while(((len = read(fd, &buf, sizeof(buf))) < 0) && (errno == EINTR)); //没有读取到事件。 - while(index < len) { - event = (struct inotify_event *)(buf + index); - //LOG_EX(LOG_Info, "event->mask: 0x%08x\n", event->mask); - - switch(event->mask) { - case IN_MODIFY: - case IN_MOVED_FROM: - reload_ztp_config(g_ztpFilePath); - break; - - case IN_MOVED_TO: - case IN_DELETE: - case IN_CREATE: - config_destroy(&g_ztpCfg); - break; - } - //获取事件。 - index += sizeof(struct inotify_event) + event->len; //移动index指向下一个事件。 - } - } - } - - inotify_rm_watch(fd, wd); - - free(g_ztpFilePath); - pthread_detach(pthread_self()); -} - -int init_configure(const char *pPath) -{ - memset(&g_ztpInfo, 0, sizeof(ZTP_CONFIG)); - - if(!pPath || strlen(pPath) == 0) { - LOG_EX(LOG_Warn, "Load default ztp configure file: %s\n", DEVICE_ZTP_PATH); - pPath = DEVICE_ZTP_PATH; - } else { - LOG_EX(LOG_Debug, "Load ztp configure file: %s\n", pPath); - } - - if(access(pPath, F_OK) != 0) { - LOG_EX(LOG_Error, "Configure file %s not exists\n", pPath); - return -ERR_NOTFOUND; - } - - config_init(&g_ztpCfg); - config_set_tab_width(&g_ztpCfg, 4u); - g_ztpFilePath = strdup(pPath); - - reload_ztp_config(g_ztpFilePath); - - pthread_create(&g_monThreadId, NULL, __cfg_file_monitor, &g_ztpCfg); - - return ERR_OK; -} - -char *cfg_get_string_value(const char *pTags, char *pDefValue) -{ - char *pValue = pDefValue; - - if(pTags == NULL || strlen(pTags) == 0) { - return pDefValue; - } - - if(config_lookup_string(&g_ztpCfg, pTags, (const char **)&pValue) != CONFIG_TRUE) { - return pDefValue; - } - - return pValue; -} - -int cfg_get_int_value(const char *pTags, int defValue) -{ - int iValue = defValue; - - if(pTags == NULL || strlen(pTags) == 0) { - return defValue; - } - - if(config_lookup_int(&g_ztpCfg, pTags, &iValue) != CONFIG_TRUE) { - return defValue; - } - - return iValue; -} - -double cfg_get_float_value(const char *pTags, double defValue) -{ - double dValue = defValue; - - if(pTags == NULL || strlen(pTags) == 0) { - return defValue; - } - - if(config_lookup_float(&g_ztpCfg, pTags, &dValue) != CONFIG_TRUE) { - return defValue; - } - - return dValue; -} - -int cfg_get_bool_value(const char *pTags, int defValue) -{ - int iValue = defValue; - - if(pTags == NULL || strlen(pTags) == 0) { - return defValue; - } - - if(config_lookup_bool(&g_ztpCfg, pTags, &iValue) != CONFIG_TRUE) { - return defValue; - } - - return iValue; -} +// +// Created by xajhu on 2019/11/18 0018. +// +#include +#include +#include +#include +#include +#include +#include + +#include "common.h" +#include "log.h" +#include "err_code.h" +#include "config.h" + +static char *g_ztpFilePath = NULL; +static char *g_devFilePath = NULL; +static pthread_t g_monThreadId; +static ZTP_CONFIG g_ztpInfo; +static DEVICE_CONFIG g_devInfo; + +static void __init_dev_config(PDEVICE_CONFIG pCfg) +{ + int i; + memset(pCfg, 0, sizeof(DEVICE_CONFIG)); + + for(i = 0; i < DEV_MAX_PORT; i++) { + pCfg->port_cfg[i].port_type = UNKNOWN_PORT; + pCfg->port_cfg[i].port_protocol = UNKNOWN_PRO_TYPE; + } + + pCfg->lan_config.ip_type = UNKNOWN_IP_TYPE; +} + +static int __get_dev_port_config(config_t *pCfg, DEV_PORT_CONFIG pPortCfg[], int *pMaxItems) +{ + int i, nItems; + config_setting_t *pSetting; + + if(pPortCfg == NULL || pMaxItems == NULL || *pMaxItems <= 0) { + return -ERR_INPUTERR; + } + + pSetting = config_lookup(pCfg, "network.port"); + + if(pSetting == NULL) { + return -ERR_NOTFOUND; + } + + nItems = config_setting_length(pSetting); + + if(nItems <= 0) { + return -ERR_NOTFOUND; + } else if(nItems > *pMaxItems) { + nItems = *pMaxItems; + } + + *pMaxItems = nItems; + + for(i = 0; i < *pMaxItems; i++) { + char* pString; + int port_type, port_pro, iValue; + config_setting_t *pSubVal; + + config_setting_t *pVal = config_setting_get_elem(pSetting, i); + + if(config_setting_lookup_int(pVal, "port_type", &port_type) == CONFIG_TRUE) { + pPortCfg[i].port_type = port_type; + } + + // below this process WAN configures + if(port_type == LAN_PORT) { + continue; + } + + if(config_setting_lookup_int(pVal, "protocol", &port_pro) == CONFIG_TRUE) { + pPortCfg[i].port_protocol = port_pro; + } + + switch(port_pro){ + case STATIC_TYPE: + pSubVal = config_setting_get_member(pVal, "static"); + + if(pSubVal == NULL) { + break; + } + + if(config_setting_lookup_int(pSubVal, "ip_type", &iValue) == CONFIG_TRUE) { + pPortCfg[i].wan_conf.static_config.ip_type = iValue; + } + + if(config_setting_lookup_string(pSubVal, "ip_addr", (const char**)&pString) == CONFIG_TRUE) { + strcpy(pPortCfg[i].wan_conf.static_config.ip_addr, pString); + } + + if(config_setting_lookup_string(pSubVal, "netmask", (const char**)&pString) == CONFIG_TRUE) { + strcpy(pPortCfg[i].wan_conf.static_config.netmask, pString); + } + + if(config_setting_lookup_string(pSubVal, "gateway", (const char**)&pString) == CONFIG_TRUE) { + strcpy(pPortCfg[i].wan_conf.static_config.gateway, pString); + } + + if(config_setting_lookup_string(pSubVal, "master_dns", (const char**)&pString) == CONFIG_TRUE) { + strcpy(pPortCfg[i].wan_conf.static_config.master_dns, pString); + } + + if(config_setting_lookup_string(pSubVal, "backup_dns", (const char**)&pString) == CONFIG_TRUE) { + strcpy(pPortCfg[i].wan_conf.static_config.backup_dns, pString); + } + break; + case PPPOE_TYPE: + pSubVal = config_setting_get_member(pVal, "pppoe"); + + if(pSubVal == NULL) { + break; + } + + if(config_setting_lookup_string(pSubVal, "username", (const char**)&pString) == CONFIG_TRUE) { + strcpy(pPortCfg[i].wan_conf.pppoe_config.username, pString); + } + + if(config_setting_lookup_string(pSubVal, "password", (const char**)&pString) == CONFIG_TRUE) { + strcpy(pPortCfg[i].wan_conf.pppoe_config.password, pString); + } + break; + case DHCP_TYPE: + break; + default: + break; + } + + } + + return ERR_OK; +} + +int load_dev_config(const char *devId) +{ + int i; + config_t devCfg; + DEVICE_CONFIG devInfo; + char *pSubDir = g_ztpInfo.dev_dir; + char *pDirPath = strdup(g_ztpFilePath); + char *pDir; + char filePath[MAX_PATH]; + + memset(filePath, 0, MAX_PATH); + + pDir = strrchr(pDirPath, '/'); + + if(pDir == NULL) { + snprintf(filePath, MAX_PATH, "../%s/%s.conf", pSubDir, devId); + } else { + *pDir = 0; + snprintf(filePath, MAX_PATH, "%s/%s/%s.conf", pDirPath, pSubDir, devId); + } + + if(g_devFilePath) { + free(g_devFilePath); + g_devFilePath = strdup(filePath); + } + + LOG_EX(LOG_Debug, "Dev configure path: %s\n", filePath); + + config_init(&devCfg); + config_set_tab_width(&devCfg, 4); + + if(config_read_file(&devCfg, filePath) != CONFIG_TRUE) { + LOG_EX(LOG_Error, "Read device configre file %s error %s at %d\n", filePath, + config_error_text(&devCfg), config_error_file(&devCfg)); + + config_destroy(&devCfg); + return -ERR_READFILE; + } + + __init_dev_config(&devInfo); + + devInfo.total_ports = DEV_MAX_PORT; + __get_dev_port_config(&devCfg, devInfo.port_cfg, &devInfo.total_ports); + + devInfo.lan_config.ip_type = cfg_get_int_value(&devCfg, "network.lan_network.ip_type", IPV4_TYPE); + + strncpy(devInfo.lan_config.ip_addr, + cfg_get_string_value(&devCfg, "network.lan_network.ip_addr", ""), MAX_IP_LEN - 1); + + strncpy(devInfo.lan_config.netmask, + cfg_get_string_value(&devCfg, "network.lan_network.netmask", ""), MAX_IP_LEN - 1); + + memcpy(&g_devInfo, &devInfo, sizeof(DEVICE_CONFIG)); + + free(pDirPath); + config_destroy(&devCfg); + return ERR_OK; +} + +int load_ztp_config(const char *pPath) +{ + config_t ztpCfg; + ZTP_CONFIG ztpInfo; + + config_init(&ztpCfg); + config_set_tab_width(&ztpCfg, 4); + + memset(&ztpInfo, 0, sizeof(ZTP_CONFIG)); + + if(config_read_file(&ztpCfg, pPath) != CONFIG_TRUE) { + LOG_EX(LOG_Error, "Read ztp configre file %s error %s at %d\n", pPath, + config_error_text(&ztpCfg), config_error_file(&ztpCfg)); + + config_destroy(&ztpCfg); + return -ERR_READFILE; + } + + ztpInfo.svr_cfg.port = cfg_get_int_value(&ztpCfg, "ztp.server.port", 10000); + strncpy(ztpInfo.svr_cfg.server_url, + cfg_get_string_value(&ztpCfg, "ztp.server.domain", ""), MAX_PATH * 2 - 1); + strncpy(ztpInfo.dev_dir, + cfg_get_string_value(&ztpCfg, "ztp.dev_dir", DEVICE_ZTP_PATH), MAX_PATH - 1); + + if(g_ztpInfo.svr_cfg.port != ztpInfo.svr_cfg.port) { + LOG_EX(LOG_Info, "Port Changed: [%d] --> [%d]\n", g_ztpInfo.svr_cfg.port, ztpInfo.svr_cfg.port); + g_ztpInfo.svr_cfg.port = ztpInfo.svr_cfg.port; + } + + if(strcmp(g_ztpInfo.svr_cfg.server_url, ztpInfo.svr_cfg.server_url) != 0) { + LOG_EX(LOG_Info, "Url Changed: [%s] --> [%s]\n", g_ztpInfo.svr_cfg.server_url, + ztpInfo.svr_cfg.server_url); + + memset(g_ztpInfo.svr_cfg.server_url, 0, MAX_PATH * 2); + strncpy(g_ztpInfo.svr_cfg.server_url, ztpInfo.svr_cfg.server_url, MAX_PATH * 2 - 1); + } + + if(strcmp(g_ztpInfo.dev_dir, ztpInfo.dev_dir) != 0) { + LOG_EX(LOG_Info, "Device Changed: [%s] --> [%s]\n", g_ztpInfo.dev_dir, ztpInfo.dev_dir); + memset(g_ztpInfo.dev_dir, 0, MAX_PATH); + strncpy(g_ztpInfo.dev_dir, ztpInfo.dev_dir, MAX_PATH - 1); + } + + config_destroy(&ztpCfg); + + return ERR_OK; +} + +static void *__cfg_file_monitor(void *p) +{ + int wd; + int fd = inotify_init(); + + UNUSED(p); + + if(fd == -1) { + LOG_EX(LOG_Error, "Create file watch error: %s\n", strerror(errno)); + return NULL; + } + + wd = inotify_add_watch(fd, g_ztpFilePath, + (IN_CREATE + IN_DELETE + IN_MODIFY + IN_MOVED_FROM + IN_MOVED_TO)); + + if(wd < 0) { + LOG_EX(LOG_Error, "Watch %s error %s\n", g_ztpFilePath, strerror(errno)); + close(fd); + free(g_ztpFilePath); + return NULL; + } + + while(g_ztpFilePath && strlen(g_ztpFilePath) > 0) { + unsigned char buf[MAX_PATH]; + struct inotify_event *event = NULL; + fd_set fds; + FD_ZERO(&fds); + FD_SET(fd, &fds); + + memset(buf, 0, MAX_PATH); + + if(select(fd + 1, &fds, NULL, NULL, NULL) > 0) { + unsigned int len, index = 0; + while(((len = read(fd, &buf, MAX_PATH)) < 0) && (errno == EINTR)); //没有读取到事件。 + while(index < len) { + event = (struct inotify_event *)(buf + index); + //LOG_EX(LOG_Info, "event->mask: 0x%08x\n", event->mask); + + switch(event->mask) { + case IN_MODIFY: + case IN_MOVED_FROM: + load_ztp_config(g_ztpFilePath); + break; + + case IN_MOVED_TO: + case IN_DELETE: + case IN_CREATE: + //config_destroy(&ztpCfg); + break; + + default: + break; + } + //获取事件。 + index += sizeof(struct inotify_event) + event->len; //移动index指向下一个事件。 + } + } + } + + inotify_rm_watch(fd, wd); + + free(g_ztpFilePath); + pthread_detach(pthread_self()); + + return NULL; +} + +int init_configure(const char *pPath) +{ + memset(&g_ztpInfo, 0, sizeof(ZTP_CONFIG)); + __init_dev_config(&g_devInfo); + + if(!pPath || strlen(pPath) == 0) { + LOG_EX(LOG_Warn, "Load default ztp configure file: %s\n", DEVICE_ZTP_PATH); + pPath = DEVICE_ZTP_PATH; + } else { + LOG_EX(LOG_Debug, "Load ztp configure file: %s\n", pPath); + } + + if(access(pPath, F_OK) != 0) { + LOG_EX(LOG_Error, "Configure file %s not exists\n", pPath); + return -ERR_NOTFOUND; + } + + g_ztpFilePath = strdup(pPath); + + load_ztp_config(g_ztpFilePath); + + pthread_create(&g_monThreadId, NULL, __cfg_file_monitor, NULL); + + return ERR_OK; +} + +int cfg_get_array_int_value(config_t *pCfg, const char *pTags, int *pArray, int *pMaxItem) +{ + int i, nItems; + config_setting_t *pSetting; + + if(pTags == NULL || strlen(pTags) == 0 + || pMaxItem == NULL || *pMaxItem <= 0 || pArray == NULL) { + return -ERR_INPUTERR; + } + + pSetting = config_lookup(pCfg, pTags); + + if(pSetting == NULL) { + return -ERR_NOTFOUND; + } + + nItems = config_setting_length(pSetting); + + if(nItems > *pMaxItem) { + nItems = *pMaxItem; + } + + *pMaxItem = nItems; + + for(i = 0; i < *pMaxItem; i++) { + pArray[i] = config_setting_get_int_elem(pSetting, i); + } + + return ERR_OK; +} + +char *cfg_get_string_value(config_t *pCfg, const char *pTags, char *pDefValue) +{ + char *pValue = pDefValue; + + if(pTags == NULL || strlen(pTags) == 0) { + return pDefValue; + } + + if(config_lookup_string(pCfg, pTags, (const char **)&pValue) != CONFIG_TRUE) { + return pDefValue; + } + + return pValue; +} + +int cfg_get_int_value(config_t *pCfg, const char *pTags, int defValue) +{ + int iValue = defValue; + + if(pTags == NULL || strlen(pTags) == 0) { + return defValue; + } + + if(config_lookup_int(pCfg, pTags, &iValue) != CONFIG_TRUE) { + return defValue; + } + + return iValue; +} + +double cfg_get_float_value(config_t *pCfg, const char *pTags, double defValue) +{ + double dValue = defValue; + + if(pTags == NULL || strlen(pTags) == 0) { + return defValue; + } + + if(config_lookup_float(pCfg, pTags, &dValue) != CONFIG_TRUE) { + return defValue; + } + + return dValue; +} + +int cfg_get_bool_value(config_t *pCfg, const char *pTags, int defValue) +{ + int iValue = defValue; + + if(pTags == NULL || strlen(pTags) == 0) { + return defValue; + } + + if(config_lookup_bool(pCfg, pTags, &iValue) != CONFIG_TRUE) { + return defValue; + } + + return iValue; +} diff --git a/dev_confs/ace08484843.conf b/dev_confs/ace08484843.conf new file mode 100644 index 0000000..e0b5951 --- /dev/null +++ b/dev_confs/ace08484843.conf @@ -0,0 +1,65 @@ +version = "1.0"; + +network = +{ + port = ( + { + port_type = 1, //0: lan, 1: wan + protocol = 0, //0: dhcp, 1: static, 2: pppoe + }, + { + port_type = 1, //0: lan, 1: wan + protocol = 1, //0: dhcp, 1: static, 2: pppoe + # this section only for static ip address setting configure + static = + { + ip_type = 0; // IPv4: 0, IPv6: 1 + ip_addr = "10.0.0.123"; + netmask = "255.255.255.0"; + gateway = "10.0.0.1"; + master_dns = "1.1.1.1"; + backup_dns = "114.114.114.114"; + }; + }, + { + port_type = 1, //0: lan, 1: wan + protocol = 2, //0: dhcp, 1: static, 2: pppoe + # this section only for pppoe setting configure + pppoe = + { + username = "cmhi_user"; // for pppoe + password = "cmhi_10086"; // for pppoe + }; + }, + { + port_type = 1, //0: lan, 1: wan + protocol = 2, //0: dhcp, 1: static, 2: pppoe + # this section only for pppoe setting configure + pppoe = + { + username = "cmhi_user1"; // for pppoe + password = "cmhi_100861"; // for pppoe + }; + }, + { + port_type = 0, //0: lan, 1: wan + }, + { + port_type = 0, //0: lan, 1: wan + }, + { + port_type = 0, //0: lan, 1: wan + }, + { + port_type = 0, //0: lan, 1: wan + } + ); + + # this section for LAN network setting configure + lan_network = + { + ip_type = 0; // IPv4: 0, IPv6: 1 + ip_addr = "192.168.1.1"; + netmask = "255.255.255.0"; + }; +}; \ No newline at end of file diff --git a/http/restful.c b/http/restful.c index 4e8247c..03fc7c0 100644 --- a/http/restful.c +++ b/http/restful.c @@ -4,7 +4,6 @@ #include #include #include -#include #include #include "common.h" @@ -19,25 +18,16 @@ typedef struct { char sPath[MAX_PATH]; char sDlPath[MAX_PATH]; unsigned int reqResult[4096]; - char *pTaskUuid; unsigned int dlSize; - unsigned int lastTm; - unsigned int createTm; OnHttpResponse onRspCb; - int isCancel; CURL *pCurl; - void *pData; int errCode; } HTTP_REQ_PARAMS, *PHTTP_REQ_PARAMS; static size_t __writeDataCb(void *pData, size_t size, size_t nmemb, void *pParams) { - PHTTP_REQ_PARAMS pReq = (PHTTP_REQ_PARAMS) pParams; - int iMemSize = size * nmemb; - - if (pReq->isCancel) { - return 0; - } + PHTTP_REQ_PARAMS pReq = (PHTTP_REQ_PARAMS)pParams; + size_t iMemSize = size * nmemb; memcpy(pReq->reqResult + pReq->dlSize, pData, iMemSize); pReq->dlSize += iMemSize; @@ -47,20 +37,22 @@ static size_t __writeDataCb(void *pData, size_t size, size_t nmemb, void *pParam int http_post_request(const char *pURL, const char *pPost, OnHttpResponse onRespCb) { + int value = 1; CURL *pCurl = NULL; - CURLcode ret = 0; + int ret = 0; + struct curl_slist *pList; PHTTP_REQ_PARAMS pParams = NULL; - pParams = (PHTTP_REQ_PARAMS) malloc(sizeof(HTTP_REQ_PARAMS)); + pParams = (PHTTP_REQ_PARAMS)malloc(sizeof(HTTP_REQ_PARAMS)); - if (pParams == NULL) { + if(pParams == NULL) { LOG_EX(LOG_Error, "Malloc %u memory error\n", sizeof(HTTP_REQ_PARAMS)); return -ERR_NOMEM; } ret = curl_global_init(CURL_GLOBAL_ALL); - if (ret != CURLE_OK) { + if(ret != CURLE_OK) { LOG_EX(LOG_Error, "Init curl global error: %d\n", ret); free(pParams); return -ERR_ERR; @@ -68,7 +60,7 @@ int http_post_request(const char *pURL, const char *pPost, OnHttpResponse onResp pCurl = curl_easy_init(); - if (!pCurl) { + if(!pCurl) { LOG_EX(LOG_Error, "Init easy curl error: %d\n", ret); curl_global_cleanup(); free(pParams); @@ -78,27 +70,26 @@ int http_post_request(const char *pURL, const char *pPost, OnHttpResponse onResp memset(pParams, 0, sizeof(HTTP_REQ_PARAMS)); pParams->onRspCb = onRespCb; - pParams->pReqUrl = (char *) malloc(strlen(pURL) + 1); - //pParams->type = INET_HTTP_WEBSERVICE_POST; + pParams->pReqUrl = (char *)malloc(strlen(pURL) + 1); pParams->dlSize = 0; - //pParams->pData = pData; pParams->pCurl = pCurl; - pParams->lastTm = 0; - pParams->isCancel = FALSE; memset(pParams->pReqUrl, 0, strlen(pURL) + 1); strcpy(pParams->pReqUrl, pURL); + pList = curl_slist_append(NULL, "Content-Type:application/json;charset=UTF-8"); + curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, __writeDataCb); curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, pParams); curl_easy_setopt(pCurl, CURLOPT_PRIVATE, pParams); curl_easy_setopt(pCurl, CURLOPT_URL, pURL); - curl_easy_setopt(pCurl, CURLOPT_NOPROGRESS, 1L); + curl_easy_setopt(pCurl, CURLOPT_NOPROGRESS, value); curl_easy_setopt(pCurl, CURLOPT_USERAGENT, "libcurl-agent/1.0"); + curl_easy_setopt(pCurl, CURLOPT_HTTPHEADER, pList); - if (pPost != NULL && strlen(pPost) > 0) { + if(pPost != NULL && strlen(pPost) > 0) { curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, pPost); - curl_easy_setopt(pCurl, CURLOPT_POSTFIELDSIZE, (long) strlen(pPost)); + curl_easy_setopt(pCurl, CURLOPT_POSTFIELDSIZE, (long)strlen(pPost)); } #ifdef SKIP_PEER_VERIFICATION @@ -115,7 +106,7 @@ int http_post_request(const char *pURL, const char *pPost, OnHttpResponse onResp curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, 0L); #else curl_easy_setopt(pCurl, CURLOPT_CAINFO, SSL_CA_FILE); - curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, 1L); + curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, value); #endif #ifdef SKIP_HOSTNAME_VERIFICATION @@ -130,19 +121,20 @@ int http_post_request(const char *pURL, const char *pPost, OnHttpResponse onResp ret = curl_easy_perform(pCurl); - if (ret != CURLE_OK) { + if(ret != CURLE_OK) { LOG_EX(LOG_Error, "Http Post error: %u\n", ret); } + curl_slist_free_all(pList); curl_easy_cleanup(pCurl); curl_global_cleanup(); - if (onRespCb) { + if(onRespCb) { onRespCb(pParams->reqResult, pParams->dlSize, pParams->pReqUrl, pParams->sPath, - pParams->pTaskUuid, -pParams->errCode, pParams->pData); + NULL, -pParams->errCode, NULL); } - if (pParams->pReqUrl) { + if(pParams->pReqUrl) { free(pParams->pReqUrl); } diff --git a/include/common.h b/include/common.h index dd259c9..235e7b9 100644 --- a/include/common.h +++ b/include/common.h @@ -1,31 +1,49 @@ -// -// Created by xajhu on 2019/11/18 0018. -// - -#ifndef ZTP_COMMON_H -#define ZTP_COMMON_H - -#ifndef MAX_PATH -#define MAX_PATH (256) -#endif - -#ifndef TRUE -#define TRUE (1) -#endif - -#ifndef FALSE -#define FALSE (0) -#endif - -#define GET_FILE_SIZE(path, size) \ - do { \ - struct stat st; \ - memset(&st, 0, sizeof(struct stat)); \ - if (stat(path, &st) != 0) { \ - size = -1; \ - } else { \ - size = st.st_size; \ - } \ - } while (0) - -#endif //ZTP_COMMON_H +// +// Created by xajhu on 2019/11/18 0018. +// + +#ifndef ZTP_COMMON_H +#define ZTP_COMMON_H + +#ifndef UNUSED +#define UNUSED(x) ((void)(x)) +#endif + +#ifndef MAX_PATH +#define MAX_PATH (256) +#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 GET_FILE_SIZE(path, size) \ + do { \ + struct stat st; \ + memset(&st, 0, sizeof(struct stat)); \ + if (stat(path, &st) != 0) { \ + (size) = -1; \ + } else { \ + (size) = st.st_size; \ + } \ + } while (0) + +#endif //ZTP_COMMON_H diff --git a/include/config.h b/include/config.h index 1fdb275..988c871 100644 --- a/include/config.h +++ b/include/config.h @@ -1,30 +1,93 @@ -// -// Created by xajhu on 2019/11/18 0018. -// - -#ifndef ZTP_CLIENT_CONFIG_H -#define ZTP_CLIENT_CONFIG_H - -#include "common.h" - -#define DEVICE_ZTP_PATH ("../ztp.conf") - -typedef struct { - char server_url[MAX_PATH * 2]; - int port; -} ZTP_SERVER, *PZTP_SERVER; - -typedef struct { - ZTP_SERVER svr_cfg; - char dev_dir[MAX_PATH]; -} ZTP_CONFIG, *PZTP_CONFIG; - - -int init_configure(const char* pPath); - -int cfg_get_bool_value(const char* pTags, int defValue); -int cfg_get_int_value(const char* pTags, int defValue); -char* cfg_get_string_value(const char* pTags, char* pDefValue); -double cfg_get_float_value(const char* pTags, double defValue); - -#endif //ZTP_CLIENT_CONFIG_H +// +// Created by xajhu on 2019/11/18 0018. +// + +#ifndef ZTP_CLIENT_CONFIG_H +#define ZTP_CLIENT_CONFIG_H + +#include + +#include "common.h" + +#define DEVICE_ZTP_PATH ("../ztp.conf") +#define DEV_MAX_PORT (32) +#define MAX_IP_LEN (64) + +typedef enum { + IPV4_TYPE = 0, + IPV6_TYPE = 1, + + UNKNOWN_IP_TYPE = -1, +} IP_TYPE; + +typedef enum { + LAN_PORT = 0, + WAN_PORT = 1, + + UNKNOWN_PORT = -1, +} HARD_PORT_TYPE; + +typedef enum { + DHCP_TYPE = 0, + STATIC_TYPE = 1, + PPPOE_TYPE = 2, + + UNKNOWN_PRO_TYPE = -1, +} WAN_PRO_TYPE; + +typedef struct { + char server_url[MAX_PATH * 2]; + int port; +} ZTP_SERVER, *PZTP_SERVER; + +typedef struct { + ZTP_SERVER svr_cfg; + char dev_dir[MAX_PATH]; +} ZTP_CONFIG, *PZTP_CONFIG; + + +typedef struct { + int ip_type; + char ip_addr[MAX_IP_LEN]; + char netmask[MAX_IP_LEN]; +} DEV_LAN_CONFIG, *PDEV_LAN_CONFIG; + +typedef struct { + int ip_type; + char ip_addr[MAX_IP_LEN]; + char netmask[MAX_IP_LEN]; + char gateway[MAX_IP_LEN]; + char master_dns[MAX_IP_LEN]; + char backup_dns[MAX_IP_LEN]; +} STATIC_CONFIG, *PSTATIC_CONFIG; + +typedef struct { + char username[MAX_PATH]; + char password[MAX_PATH]; +} PPPOE_CONFIG, *PPPPOE_CONFIG; + +typedef struct { + int port_type; + int port_protocol; + union { + PPPOE_CONFIG pppoe_config; + STATIC_CONFIG static_config; + } wan_conf; +} DEV_PORT_CONFIG, *PDEV_PORT_CONFIG; + +typedef struct { + DEV_PORT_CONFIG port_cfg[DEV_MAX_PORT]; + DEV_LAN_CONFIG lan_config; + int total_ports; +} DEVICE_CONFIG, *PDEVICE_CONFIG; + +int init_configure(const char* pPath); + +int cfg_get_bool_value(config_t* pCfg, const char* pTags, int defValue); +int cfg_get_int_value(config_t* pCfg, const char* pTags, int defValue); +char* cfg_get_string_value(config_t* pCfg, const char* pTags, char* pDefValue); +double cfg_get_float_value(config_t* pCfg, const char* pTags, double defValue); +int cfg_get_array_int_value(config_t* pCfg, const char *pTags, int* pArray, int* pMaxItem); + +int load_dev_config(const char* devId); +#endif //ZTP_CLIENT_CONFIG_H diff --git a/include/err_code.h b/include/err_code.h index 4681ad2..2ce4ef9 100644 --- a/include/err_code.h +++ b/include/err_code.h @@ -1,79 +1,79 @@ -// -// Created by xajhu on 2019/11/18 0018. -// - -#ifndef ZTP_CLIENT_ERR_CODE_H -#define ZTP_CLIENT_ERR_CODE_H - -#define ERR_OK (0) -#define ERR_ERR (1) -#define ERR_UNKNOWN (2) -#define ERR_SYSERR (3) -#define ERR_NOTFOUND (4) -#define ERR_TIMEOUT (5) - -#define ERR_NULLP (6) -#define ERR_NOMEM (7) -#define ERR_CHKERR (8) -#define ERR_NOTSUPPORT (9) -#define ERR_INPUTERR (10) -#define ERR_EXIST (11) -#define ERR_FULL (12) -#define ERR_SENDERR (13) -#define ERR_NOCMID (14) -#define ERR_SRCERR (15) -#define ERR_JSONERR (16) -#define ERR_USED (17) - -#define ERR_READFILE (100) - -static inline char *err2string(int err) -{ - if(err < 0) { - err = -err; - } - - switch(err) { - case ERR_OK: return "OK"; - - case ERR_ERR: return "Error"; - - case ERR_UNKNOWN: return "Unkown"; - - case ERR_SYSERR: return "SystemError"; - - case ERR_NOTFOUND: return "NotFound"; - - case ERR_TIMEOUT: return "Timeout"; - - case ERR_NULLP: return "NullPointer" ; - - case ERR_NOMEM: return "NotEnoughMemory"; - - case ERR_CHKERR: return "CheckError"; - - case ERR_NOTSUPPORT: return "NotSupport"; - - case ERR_INPUTERR: return "InputError"; - - case ERR_EXIST: return "AlreadyExist"; - - case ERR_FULL: return "Full"; - - case ERR_SENDERR: return "SendErr"; - - case ERR_NOCMID: return "CanNotFindConfig"; - - case ERR_SRCERR: return "ConfigSourceErr"; - - case ERR_JSONERR: return "JsonFormatErr"; - - case ERR_USED: return "ItemUsed"; - - case ERR_READFILE: return "Read File Error"; - - default: return "Unknown err code"; - } -} - -#endif //ZTP_CLIENT_ERR_CODE_H +// +// Created by xajhu on 2019/11/18 0018. +// + +#ifndef ZTP_CLIENT_ERR_CODE_H +#define ZTP_CLIENT_ERR_CODE_H + +#define ERR_OK (0) +#define ERR_ERR (1) +#define ERR_UNKNOWN (2) +#define ERR_SYSERR (3) +#define ERR_NOTFOUND (4) +#define ERR_TIMEOUT (5) + +#define ERR_NULLP (6) +#define ERR_NOMEM (7) +#define ERR_CHKERR (8) +#define ERR_NOTSUPPORT (9) +#define ERR_INPUTERR (10) +#define ERR_EXIST (11) +#define ERR_FULL (12) +#define ERR_SENDERR (13) +#define ERR_NOCMID (14) +#define ERR_SRCERR (15) +#define ERR_JSONERR (16) +#define ERR_USED (17) + +#define ERR_READFILE (100) + +static inline char *err2string(int err) +{ + if(err < 0) { + err = -err; + } + + switch(err) { + case ERR_OK: return "OK"; + + case ERR_ERR: return "Error"; + + case ERR_UNKNOWN: return "Unkown"; + + case ERR_SYSERR: return "SystemError"; + + case ERR_NOTFOUND: return "NotFound"; + + case ERR_TIMEOUT: return "Timeout"; + + case ERR_NULLP: return "NullPointer" ; + + case ERR_NOMEM: return "NotEnoughMemory"; + + case ERR_CHKERR: return "CheckError"; + + case ERR_NOTSUPPORT: return "NotSupport"; + + case ERR_INPUTERR: return "InputError"; + + case ERR_EXIST: return "AlreadyExist"; + + case ERR_FULL: return "Full"; + + case ERR_SENDERR: return "SendErr"; + + case ERR_NOCMID: return "CanNotFindConfig"; + + case ERR_SRCERR: return "ConfigSourceErr"; + + case ERR_JSONERR: return "JsonFormatErr"; + + case ERR_USED: return "ItemUsed"; + + case ERR_READFILE: return "Read File Error"; + + default: return "Unknown err code"; + } +} + +#endif //ZTP_CLIENT_ERR_CODE_H diff --git a/include/json_interface.h b/include/json_interface.h index c2ed9e1..7629113 100644 --- a/include/json_interface.h +++ b/include/json_interface.h @@ -1,29 +1,29 @@ -// -// Created by xajhu on 2019/11/19 0019. -// - -#ifndef ZTP_CLIENT_JSON_INTERFACE_H -#define ZTP_CLIENT_JSON_INTERFACE_H - -typedef enum { - JE_AUTH_ZTP = 0, - - JSON_ENGINE_MAX, -} JSON_ENGINE_TYPE; - -typedef struct { - char* pESN; -} AUTH_ZTH_REQ, *PAUTH_ZTH_REQ; - -typedef struct { - char* status; - unsigned int iptype; - unsigned int ip; - char* code; -} AUTH_ZTH_RSP, *PAUTH_ZTH_RSP; - - -int Json2Struct(const char *pJsonStr, void *pData, JSON_ENGINE_TYPE type, int enBase64); -const char *Struct2Json(void *pStruct, JSON_ENGINE_TYPE type, int enBase64, int *pErr); - -#endif //ZTP_CLIENT_JSON_INTERFACE_H +// +// Created by xajhu on 2019/11/19 0019. +// + +#ifndef ZTP_CLIENT_JSON_INTERFACE_H +#define ZTP_CLIENT_JSON_INTERFACE_H + +typedef enum { + JE_AUTH_ZTP = 0, + + JSON_ENGINE_MAX, +} JSON_ENGINE_TYPE; + +typedef struct { + char* pESN; +} AUTH_ZTH_REQ, *PAUTH_ZTH_REQ; + +typedef struct { + char* status; + unsigned int iptype; + unsigned int ip; + char* code; +} AUTH_ZTH_RSP, *PAUTH_ZTH_RSP; + + +int Json2Struct(const char *pJsonStr, void *pData, JSON_ENGINE_TYPE type, int enBase64); +const char *Struct2Json(void *pStruct, JSON_ENGINE_TYPE type, int enBase64, int *pErr); + +#endif //ZTP_CLIENT_JSON_INTERFACE_H diff --git a/include/log.h b/include/log.h index 39dd150..7de46b8 100644 --- a/include/log.h +++ b/include/log.h @@ -17,28 +17,6 @@ extern "C" { #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__ diff --git a/include/restful.h b/include/restful.h index 1dcf795..b70da2b 100644 --- a/include/restful.h +++ b/include/restful.h @@ -1,12 +1,12 @@ -// -// Created by xajhu on 2019/11/18 0018. -// - -#ifndef ZTP_CLIENT_RESTFUL_H -#define ZTP_CLIENT_RESTFUL_H - -typedef void (*OnHttpResponse)(void* pData, unsigned int size, const char* pReqUrl, - const char* pDlPath, const char* pTaskUuid, int iFinished, void* pUserData); - -int http_post_request(const char *pURL, const char* pPost, OnHttpResponse onRespCb); -#endif //ZTP_CLIENT_RESTFUL_H +// +// Created by xajhu on 2019/11/18 0018. +// + +#ifndef ZTP_CLIENT_RESTFUL_H +#define ZTP_CLIENT_RESTFUL_H + +typedef void (*OnHttpResponse)(void* pData, unsigned int size, const char* pReqUrl, + const char* pDlPath, const char* pTaskUuid, int iFinished, void* pUserData); + +int http_post_request(const char *pURL, const char* pPost, OnHttpResponse onRespCb); +#endif //ZTP_CLIENT_RESTFUL_H diff --git a/json_engine/json_interface.c b/json_engine/json_interface.c index 208e61e..ca7bb00 100644 --- a/json_engine/json_interface.c +++ b/json_engine/json_interface.c @@ -1,154 +1,155 @@ -// -// Created by xajhu on 2019/11/19 0019. -// -#include -#include -#include - -#include "err_code.h" -#include "log.h" -#include "json_interface.h" - - -typedef const char *(*StructToJsonCb)(void *pStruct); -typedef int (*JsonToStructCb)(const char *pJsonStr, void **pStruct); -typedef void (*Base64Code)(void *pStruct, int enCode); - -typedef struct { - JSON_ENGINE_TYPE typeId; - StructToJsonCb s2jCb; - JsonToStructCb j2sCb; - Base64Code base64Cb; -} JSON_ENGINE, *PJSON_ENGINE; - -static const char *__ztp_auth_encode(void *pData) -{ - const char *pJsonS; - PAUTH_ZTH_REQ p = (PAUTH_ZTH_REQ)pData; - cJSON *pRoot = cJSON_CreateObject(); - - if(!p) { - return NULL; - } - - if(p->pESN && strlen(p->pESN) > 0) { - cJSON_AddStringToObject(pRoot, "ESN", p->pESN); - } - - pJsonS = cJSON_Print(pRoot); - cJSON_Delete(pRoot); - return pJsonS; -} - -static int __ztp_auth_decode(const char *pJsonS, void **pStruct) -{ - cJSON *pRoot, *pVal; - PAUTH_ZTH_RSP pData; - *pStruct = malloc(sizeof(AUTH_ZTH_RSP)); - - if(*pStruct == NULL) { - return -ERR_INPUTERR; - } - - pData = (PAUTH_ZTH_RSP) * pStruct; - pRoot = cJSON_Parse(pJsonS); - - if(!pRoot) { - return -ERR_JSONERR; - } - - memset(pData, 0, sizeof(AUTH_ZTH_RSP)); - pVal = cJSON_GetObjectItem(pRoot, "status"); - - if(cJSON_IsString(pVal)) { - pData->status = strdup(pVal->valuestring); - } - - pVal = cJSON_GetObjectItem(pRoot, "iptype"); - - if(cJSON_IsString(pVal)) { - pData->iptype = strtoul(pVal->valuestring, 0, 10); - } - - pVal = cJSON_GetObjectItem(pRoot, "ip"); - - if(cJSON_IsString(pVal)) { - pData->ip = strtoul(pVal->valuestring, 0, 10); - } - - pVal = cJSON_GetObjectItem(pRoot, "code"); - - if(cJSON_IsString(pVal)) { - pData->code = strdup(pVal->valuestring); - } - - cJSON_Delete(pRoot); - return ERR_OK; -} - - -static JSON_ENGINE g_jSonEngine[] = { - {JE_AUTH_ZTP, __ztp_auth_encode, __ztp_auth_decode, NULL}, - {JSON_ENGINE_MAX,NULL, NULL, NULL} -}; - - -int Json2Struct(const char *pJsonStr, void *pData, JSON_ENGINE_TYPE type, - int enBase64) -{ - if(pJsonStr == NULL || pData == NULL) { - return -ERR_INPUTERR; - } - - if(type < 0 || type >= JSON_ENGINE_MAX) { - return -ERR_INPUTERR; - } - - //LOG_EX(LOG_Debug, "Json:\n%s\n", pJsonStr); - - if(g_jSonEngine[type].j2sCb) { - g_jSonEngine[type].j2sCb(pJsonStr, pData); - - if(enBase64 && g_jSonEngine[type].base64Cb) { - g_jSonEngine[type].base64Cb(pData, FALSE); - } - - return ERR_OK; - } else { - return -ERR_NOTSUPPORT; - } -} - -const char *Struct2Json(void *pStruct, JSON_ENGINE_TYPE type, int enBase64, - int *pErr) -{ - if(pStruct == NULL || pErr == NULL) { - if(pErr) { - *pErr = -ERR_INPUTERR; - } - - return NULL; - } - - if(type < 0 || type >= JSON_ENGINE_MAX) { - *pErr = -ERR_INPUTERR; - return NULL; - } - - *pErr = ERR_OK; - - if(enBase64 && g_jSonEngine[type].base64Cb) { - g_jSonEngine[type].base64Cb(pStruct, TRUE); - } - - if(g_jSonEngine[type].s2jCb) { - const char *pJsongStr = g_jSonEngine[type].s2jCb(pStruct); - //LOG_EX(LOG_Debug, "Json: \n%s\n", pJsongStr); - return (pJsongStr); - } else { - return NULL; - } -} - - - +// +// Created by xajhu on 2019/11/19 0019. +// +#include +#include +#include + +#include "common.h" +#include "err_code.h" +#include "log.h" +#include "json_interface.h" + + +typedef const char *(*StructToJsonCb)(void *pStruct); +typedef int (*JsonToStructCb)(const char *pJsonStr, void **pStruct); +typedef void (*Base64Code)(void *pStruct, int enCode); + +typedef struct { + JSON_ENGINE_TYPE typeId; + StructToJsonCb s2jCb; + JsonToStructCb j2sCb; + Base64Code base64Cb; +} JSON_ENGINE; + +static const char *__ztp_auth_encode(void *pData) +{ + const char *pJsonS; + PAUTH_ZTH_REQ p = (PAUTH_ZTH_REQ)pData; + cJSON *pRoot = cJSON_CreateObject(); + + if(!p) { + return NULL; + } + + if(p->pESN && strlen(p->pESN) > 0) { + cJSON_AddStringToObject(pRoot, "ESN", p->pESN); + } + + pJsonS = cJSON_Print(pRoot); + cJSON_Delete(pRoot); + return pJsonS; +} + +static int __ztp_auth_decode(const char *pJsonS, void **pStruct) +{ + cJSON *pRoot, *pVal; + PAUTH_ZTH_RSP pData; + *pStruct = malloc(sizeof(AUTH_ZTH_RSP)); + + if(*pStruct == NULL) { + return -ERR_INPUTERR; + } + + pData = (PAUTH_ZTH_RSP) * pStruct; + pRoot = cJSON_Parse(pJsonS); + + if(!pRoot) { + return -ERR_JSONERR; + } + + memset(pData, 0, sizeof(AUTH_ZTH_RSP)); + pVal = cJSON_GetObjectItem(pRoot, "status"); + + if(cJSON_IsString(pVal)) { + pData->status = strdup(pVal->valuestring); + } + + pVal = cJSON_GetObjectItem(pRoot, "iptype"); + + if(cJSON_IsString(pVal)) { + pData->iptype = (unsigned int)strtoul(pVal->valuestring, 0, 10); + } + + pVal = cJSON_GetObjectItem(pRoot, "ip"); + + if(cJSON_IsString(pVal)) { + pData->ip = (unsigned int)strtoul(pVal->valuestring, 0, 10); + } + + pVal = cJSON_GetObjectItem(pRoot, "code"); + + if(cJSON_IsString(pVal)) { + pData->code = strdup(pVal->valuestring); + } + + cJSON_Delete(pRoot); + return ERR_OK; +} + + +static JSON_ENGINE g_jSonEngine[] = { + {JE_AUTH_ZTP, __ztp_auth_encode, __ztp_auth_decode, NULL}, + {JSON_ENGINE_MAX,NULL, NULL, NULL} +}; + + +int Json2Struct(const char *pJsonStr, void *pData, JSON_ENGINE_TYPE type, + int enBase64) +{ + if(pJsonStr == NULL || pData == NULL) { + return -ERR_INPUTERR; + } + + if(type < 0 || type >= JSON_ENGINE_MAX) { + return -ERR_INPUTERR; + } + + //LOG_EX(LOG_Debug, "Json:\n%s\n", pJsonStr); + + if(g_jSonEngine[type].j2sCb) { + g_jSonEngine[type].j2sCb(pJsonStr, pData); + + if(enBase64 && g_jSonEngine[type].base64Cb) { + g_jSonEngine[type].base64Cb(pData, FALSE); + } + + return ERR_OK; + } else { + return -ERR_NOTSUPPORT; + } +} + +const char *Struct2Json(void *pStruct, JSON_ENGINE_TYPE type, int enBase64, + int *pErr) +{ + if(pStruct == NULL || pErr == NULL) { + if(pErr) { + *pErr = -ERR_INPUTERR; + } + + return NULL; + } + + if(type < 0 || type >= JSON_ENGINE_MAX) { + *pErr = -ERR_INPUTERR; + return NULL; + } + + *pErr = ERR_OK; + + if(enBase64 && g_jSonEngine[type].base64Cb) { + g_jSonEngine[type].base64Cb(pStruct, TRUE); + } + + if(g_jSonEngine[type].s2jCb) { + const char *pJsongStr = g_jSonEngine[type].s2jCb(pStruct); + //LOG_EX(LOG_Debug, "Json: \n%s\n", pJsongStr); + return (pJsongStr); + } else { + return NULL; + } +} + + + diff --git a/log/hexdump.c b/log/hexdump.c index f6b37c0..6b0e190 100644 --- a/log/hexdump.c +++ b/log/hexdump.c @@ -6,6 +6,7 @@ #include "uthash/utstring.h" #include "log.h" +#include "common.h" static const char hex_asc[] = "0123456789abcdef"; diff --git a/log/log.c b/log/log.c index b2b678d..cc9c71d 100644 --- a/log/log.c +++ b/log/log.c @@ -30,7 +30,7 @@ typedef struct LOG_ITEM { - LOG_LEVEL level; + int level; int isPrinted; int isAddTags; struct timeval timestamp; @@ -327,7 +327,7 @@ void IHW_RunLogService(void) pthread_create(&g_logThreadId, NULL, __logOutputThread, NULL); } -static void __logTo(LOG_LEVEL level, int isAddTag, char* pMsg, int isPrint) +static void __logTo(int level, int isAddTag, char* pMsg, int isPrint) { PLOG_ITEM pLogItem; diff --git a/ztp.conf b/ztp.conf index 02b209d..5859e6f 100644 --- a/ztp.conf +++ b/ztp.conf @@ -1,12 +1,12 @@ version = "1.0"; -ztp: +ztp = { - server: + server = { domain = "cmhi.ztp.com"; port = 10082; }; - dev_dir = "/dev_confs"; // devices configure direcotry + dev_dir = "dev_confs"; // devices configure direcotry }; \ No newline at end of file diff --git a/ztp_main.c b/ztp_main.c index c9c8436..110cc5e 100644 --- a/ztp_main.c +++ b/ztp_main.c @@ -1,4 +1,3 @@ -#include #include #include @@ -10,8 +9,16 @@ #define ZTP_ESN ("ace08484843") -void __onPost(void* pData, unsigned int size, const char* pReqUrl, const char* pDlPath, const char* pTaskUuid, int iFinished, void* pUserData) +void __onPost(void* pData, unsigned int size, const char* pReqUrl, + const char* pDlPath, const char* pTaskUuid, int iFinished, void* pUserData) { + UNUSED(size); + UNUSED(pReqUrl); + UNUSED(pDlPath); + UNUSED(pTaskUuid); + UNUSED(iFinished); + UNUSED(pUserData); + LOG_EX(LOG_Info, "Post Result: %s\n", (char*)pData); } @@ -42,9 +49,11 @@ int main(int argc, char** argv) free((void*)pJson); } - while(TRUE){ - usleep(1000); - } + load_dev_config(ZTP_ESN); + +// while(ret == ERR_OK){ +// usleep(1000); +// } IHW_WaitFinishLogout();