ztp/ztp_main.c

101 lines
2.2 KiB
C
Raw Normal View History

#include <stdlib.h>
#include <unistd.h>
#include "log.h"
#include "ztp_config.h"
#include "restful.h"
#include "json_interface.h"
#include "err_code.h"
#define ZTP_ESN ("ace08484843")
#define ZTP_VERSION ("0.1")
void __onPost(void *pData, unsigned int size, const char *pReqUrl,
const char *pDlPath, const char *pTaskUuid, int iFinished, void *pUserData)
{
int ret = ERR_OK;
PAUTH_ZTH_RSP pZTPRsp;
UNUSED(size);
UNUSED(pReqUrl);
UNUSED(pDlPath);
UNUSED(pTaskUuid);
UNUSED(iFinished);
UNUSED(pUserData);
LOG_EX(LOG_Info, "Post URL: [%s]\n", pReqUrl);
if(pUserData && strlen(pUserData) > 0) {
LOG_EX(LOG_Info, "Post Data: [%s]\n", pUserData);
}
LOG_EX(LOG_Info, "Post Result: [%s]\n", (char *)pData);
ret = Json2Struct(pData, &pZTPRsp, JE_AUTH_ZTP, FALSE);
if(ret == ERR_OK && pZTPRsp) {
if(strcasecmp(pZTPRsp->status, "SUCCESS") == 0) {
LOG_EX(LOG_Debug, "Request OK\n");
} else {
LOG_EX(LOG_Error, "Request Error\n");
}
} else {
LOG_EX(LOG_Error, "Decode JSON error: %d\n", ret);
}
if(pZTPRsp) {
if(pZTPRsp->status) {
free(pZTPRsp->status);
}
if(pZTPRsp->code) {
free(pZTPRsp->code);
}
if(pZTPRsp->ip) {
free(pZTPRsp->ip);
}
free(pZTPRsp);
}
}
int main(int argc, char **argv)
{
int ret = ERR_OK;
const char *pJson;
AUTH_ZTH_REQ ztpReq;
IHW_InitLOG("ZTP", NULL, TRUE);
APP_BUILD_INFO("ZTP", ZTP_VERSION);
init_configure(DEVICE_ZTP_PATH);
memset(&ztpReq, 0, sizeof(AUTH_ZTH_REQ));
ztpReq.pESN = "tt21";//ZTP_ESN;
pJson = Struct2Json(&ztpReq, JE_AUTH_ZTP, FALSE, &ret);
if(pJson && ret == ERR_OK) {
//http_post_request_async("https://xajhuang.com:3006/post", pJson, __onPost);
http_post_request_async("http://172.28.73.43:8088/device/esn", pJson, __onPost);
} else {
LOG_EX(LOG_Error, "Create JSON error: %d\n", ret);
}
if(pJson) {
free((void *)pJson);
}
load_dev_config(ZTP_ESN);
#ifdef MONITOR_ZTP_CFG_FILE
while(ret == ERR_OK) {
usleep(1000);
}
#endif
IHW_WaitFinishLogout();
return 0;
}