Mod aaa-12 完成读取ZTP配置文件基本功能
RCA: SOL: 修改人:huangxin 检视人:huangxin
This commit is contained in:
parent
bcb15ba6ef
commit
134cf51f89
|
@ -1,14 +1,13 @@
|
|||
//
|
||||
// Created by xajhu on 2019/11/18 0018.
|
||||
//
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <libconfig.h>
|
||||
#include <pthread.h>
|
||||
#include <sys/inotify.h>
|
||||
#include <errno.h>
|
||||
#include <config.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "log.h"
|
||||
|
@ -16,29 +15,209 @@
|
|||
#include "config.h"
|
||||
|
||||
static char *g_ztpFilePath = NULL;
|
||||
static config_t g_ztpCfg;
|
||||
static char *g_devFilePath = NULL;
|
||||
static pthread_t g_monThreadId;
|
||||
static ZTP_CONFIG g_ztpInfo;
|
||||
static DEVICE_CONFIG g_devInfo;
|
||||
|
||||
int reload_ztp_config(const char *pPath)
|
||||
static void __init_dev_config(PDEVICE_CONFIG pCfg)
|
||||
{
|
||||
ZTP_CONFIG ztpInfo;
|
||||
int i;
|
||||
memset(pCfg, 0, sizeof(DEVICE_CONFIG));
|
||||
|
||||
memset(&ztpInfo, 0, sizeof(ZTP_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;
|
||||
}
|
||||
|
||||
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));
|
||||
pCfg->lan_config.ip_type = UNKNOWN_IP_TYPE;
|
||||
}
|
||||
|
||||
config_destroy(&g_ztpCfg);
|
||||
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;
|
||||
}
|
||||
|
||||
ztpInfo.svr_cfg.port = cfg_get_int_value("ztp.server.port", 10000);
|
||||
__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("ztp.server.domain", ""), MAX_PATH * 2 - 1);
|
||||
cfg_get_string_value(&ztpCfg, "ztp.server.domain", ""), MAX_PATH * 2 - 1);
|
||||
strncpy(ztpInfo.dev_dir,
|
||||
cfg_get_string_value("ztp.dev_dir", DEVICE_ZTP_PATH), MAX_PATH - 1);
|
||||
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);
|
||||
|
@ -59,6 +238,8 @@ int reload_ztp_config(const char *pPath)
|
|||
strncpy(g_ztpInfo.dev_dir, ztpInfo.dev_dir, MAX_PATH - 1);
|
||||
}
|
||||
|
||||
config_destroy(&ztpCfg);
|
||||
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
|
@ -67,6 +248,8 @@ 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;
|
||||
|
@ -93,7 +276,7 @@ static void *__cfg_file_monitor(void *p)
|
|||
|
||||
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(((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);
|
||||
|
@ -101,13 +284,16 @@ static void *__cfg_file_monitor(void *p)
|
|||
switch(event->mask) {
|
||||
case IN_MODIFY:
|
||||
case IN_MOVED_FROM:
|
||||
reload_ztp_config(g_ztpFilePath);
|
||||
load_ztp_config(g_ztpFilePath);
|
||||
break;
|
||||
|
||||
case IN_MOVED_TO:
|
||||
case IN_DELETE:
|
||||
case IN_CREATE:
|
||||
config_destroy(&g_ztpCfg);
|
||||
//config_destroy(&ztpCfg);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
//获取事件。
|
||||
|
@ -120,11 +306,14 @@ static void *__cfg_file_monitor(void *p)
|
|||
|
||||
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);
|
||||
|
@ -138,18 +327,47 @@ int init_configure(const char *pPath)
|
|||
return -ERR_NOTFOUND;
|
||||
}
|
||||
|
||||
config_init(&g_ztpCfg);
|
||||
config_set_tab_width(&g_ztpCfg, 4u);
|
||||
g_ztpFilePath = strdup(pPath);
|
||||
|
||||
reload_ztp_config(g_ztpFilePath);
|
||||
load_ztp_config(g_ztpFilePath);
|
||||
|
||||
pthread_create(&g_monThreadId, NULL, __cfg_file_monitor, &g_ztpCfg);
|
||||
pthread_create(&g_monThreadId, NULL, __cfg_file_monitor, NULL);
|
||||
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
char *cfg_get_string_value(const char *pTags, char *pDefValue)
|
||||
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;
|
||||
|
||||
|
@ -157,14 +375,14 @@ char *cfg_get_string_value(const char *pTags, char *pDefValue)
|
|||
return pDefValue;
|
||||
}
|
||||
|
||||
if(config_lookup_string(&g_ztpCfg, pTags, (const char **)&pValue) != CONFIG_TRUE) {
|
||||
if(config_lookup_string(pCfg, pTags, (const char **)&pValue) != CONFIG_TRUE) {
|
||||
return pDefValue;
|
||||
}
|
||||
|
||||
return pValue;
|
||||
}
|
||||
|
||||
int cfg_get_int_value(const char *pTags, int defValue)
|
||||
int cfg_get_int_value(config_t *pCfg, const char *pTags, int defValue)
|
||||
{
|
||||
int iValue = defValue;
|
||||
|
||||
|
@ -172,14 +390,14 @@ int cfg_get_int_value(const char *pTags, int defValue)
|
|||
return defValue;
|
||||
}
|
||||
|
||||
if(config_lookup_int(&g_ztpCfg, pTags, &iValue) != CONFIG_TRUE) {
|
||||
if(config_lookup_int(pCfg, pTags, &iValue) != CONFIG_TRUE) {
|
||||
return defValue;
|
||||
}
|
||||
|
||||
return iValue;
|
||||
}
|
||||
|
||||
double cfg_get_float_value(const char *pTags, double defValue)
|
||||
double cfg_get_float_value(config_t *pCfg, const char *pTags, double defValue)
|
||||
{
|
||||
double dValue = defValue;
|
||||
|
||||
|
@ -187,14 +405,14 @@ double cfg_get_float_value(const char *pTags, double defValue)
|
|||
return defValue;
|
||||
}
|
||||
|
||||
if(config_lookup_float(&g_ztpCfg, pTags, &dValue) != CONFIG_TRUE) {
|
||||
if(config_lookup_float(pCfg, pTags, &dValue) != CONFIG_TRUE) {
|
||||
return defValue;
|
||||
}
|
||||
|
||||
return dValue;
|
||||
}
|
||||
|
||||
int cfg_get_bool_value(const char *pTags, int defValue)
|
||||
int cfg_get_bool_value(config_t *pCfg, const char *pTags, int defValue)
|
||||
{
|
||||
int iValue = defValue;
|
||||
|
||||
|
@ -202,7 +420,7 @@ int cfg_get_bool_value(const char *pTags, int defValue)
|
|||
return defValue;
|
||||
}
|
||||
|
||||
if(config_lookup_bool(&g_ztpCfg, pTags, &iValue) != CONFIG_TRUE) {
|
||||
if(config_lookup_bool(pCfg, pTags, &iValue) != CONFIG_TRUE) {
|
||||
return defValue;
|
||||
}
|
||||
|
||||
|
|
|
@ -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";
|
||||
};
|
||||
};
|
|
@ -4,7 +4,6 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <curl/curl.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
size_t iMemSize = size * nmemb;
|
||||
|
||||
memcpy(pReq->reqResult + pReq->dlSize, pData, iMemSize);
|
||||
pReq->dlSize += iMemSize;
|
||||
|
@ -47,8 +37,10 @@ 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));
|
||||
|
@ -79,22 +71,21 @@ int http_post_request(const char *pURL, const char *pPost, OnHttpResponse onResp
|
|||
|
||||
pParams->onRspCb = onRespCb;
|
||||
pParams->pReqUrl = (char *)malloc(strlen(pURL) + 1);
|
||||
//pParams->type = INET_HTTP_WEBSERVICE_POST;
|
||||
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) {
|
||||
curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, pPost);
|
||||
|
@ -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
|
||||
|
@ -134,12 +125,13 @@ int http_post_request(const char *pURL, const char *pPost, OnHttpResponse onResp
|
|||
LOG_EX(LOG_Error, "Http Post error: %u\n", ret);
|
||||
}
|
||||
|
||||
curl_slist_free_all(pList);
|
||||
curl_easy_cleanup(pCurl);
|
||||
curl_global_cleanup();
|
||||
|
||||
if(onRespCb) {
|
||||
onRespCb(pParams->reqResult, pParams->dlSize, pParams->pReqUrl, pParams->sPath,
|
||||
pParams->pTaskUuid, -pParams->errCode, pParams->pData);
|
||||
NULL, -pParams->errCode, NULL);
|
||||
}
|
||||
|
||||
if(pParams->pReqUrl) {
|
||||
|
|
|
@ -5,6 +5,10 @@
|
|||
#ifndef ZTP_COMMON_H
|
||||
#define ZTP_COMMON_H
|
||||
|
||||
#ifndef UNUSED
|
||||
#define UNUSED(x) ((void)(x))
|
||||
#endif
|
||||
|
||||
#ifndef MAX_PATH
|
||||
#define MAX_PATH (256)
|
||||
#endif
|
||||
|
@ -17,14 +21,28 @@
|
|||
#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; \
|
||||
(size) = -1; \
|
||||
} else { \
|
||||
size = st.st_size; \
|
||||
(size) = st.st_size; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
|
|
|
@ -5,9 +5,35 @@
|
|||
#ifndef ZTP_CLIENT_CONFIG_H
|
||||
#define ZTP_CLIENT_CONFIG_H
|
||||
|
||||
#include <libconfig.h>
|
||||
|
||||
#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];
|
||||
|
@ -20,11 +46,48 @@ typedef struct {
|
|||
} 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(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);
|
||||
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
|
||||
|
|
|
@ -17,28 +17,6 @@ extern "C" {
|
|||
#include <linux/string.h>
|
||||
#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__
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
#include <string.h>
|
||||
#include <cjson/cJSON.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "err_code.h"
|
||||
#include "log.h"
|
||||
#include "json_interface.h"
|
||||
|
@ -19,7 +20,7 @@ typedef struct {
|
|||
StructToJsonCb s2jCb;
|
||||
JsonToStructCb j2sCb;
|
||||
Base64Code base64Cb;
|
||||
} JSON_ENGINE, *PJSON_ENGINE;
|
||||
} JSON_ENGINE;
|
||||
|
||||
static const char *__ztp_auth_encode(void *pData)
|
||||
{
|
||||
|
@ -67,13 +68,13 @@ static int __ztp_auth_decode(const char *pJsonS, void **pStruct)
|
|||
pVal = cJSON_GetObjectItem(pRoot, "iptype");
|
||||
|
||||
if(cJSON_IsString(pVal)) {
|
||||
pData->iptype = strtoul(pVal->valuestring, 0, 10);
|
||||
pData->iptype = (unsigned int)strtoul(pVal->valuestring, 0, 10);
|
||||
}
|
||||
|
||||
pVal = cJSON_GetObjectItem(pRoot, "ip");
|
||||
|
||||
if(cJSON_IsString(pVal)) {
|
||||
pData->ip = strtoul(pVal->valuestring, 0, 10);
|
||||
pData->ip = (unsigned int)strtoul(pVal->valuestring, 0, 10);
|
||||
}
|
||||
|
||||
pVal = cJSON_GetObjectItem(pRoot, "code");
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#include "uthash/utstring.h"
|
||||
|
||||
#include "log.h"
|
||||
#include "common.h"
|
||||
|
||||
static const char hex_asc[] = "0123456789abcdef";
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
6
ztp.conf
6
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
|
||||
};
|
19
ztp_main.c
19
ztp_main.c
|
@ -1,4 +1,3 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
@ -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();
|
||||
|
||||
|
|
Loading…
Reference in New Issue