|
|
#define LOG_TAG "mqtt"
|
|
|
#include "common.h"
|
|
|
#include "elog.h"
|
|
|
|
|
|
DEV_CFG_INFO dev_cfg;
|
|
|
|
|
|
char *get_time_in_json(void)
|
|
|
{
|
|
|
static char time_string[32];
|
|
|
char ms[8];
|
|
|
struct timeval tv;
|
|
|
struct tm *now;
|
|
|
|
|
|
gettimeofday(&tv,NULL);
|
|
|
now = localtime(&tv.tv_sec);
|
|
|
memset(time_string,0,sizeof(time_string));
|
|
|
strftime(time_string, sizeof(time_string),"%Y-%m-%dT%H:%M:%S",now);
|
|
|
snprintf(ms,sizeof(ms),".%03ld",tv.tv_usec/1000);
|
|
|
strncat(time_string,ms,strlen(ms));
|
|
|
strncat(time_string,"+0800",strlen("+0800"));
|
|
|
return time_string;
|
|
|
}
|
|
|
|
|
|
char *make_token(char *token,char len)
|
|
|
{
|
|
|
static uint32_t token_inc=0;
|
|
|
if(token!=NULL)
|
|
|
snprintf(token,len-1,"%08d",token_inc++);
|
|
|
return token;
|
|
|
}
|
|
|
|
|
|
int split(char *src,const char *separator,char **dest)
|
|
|
{
|
|
|
/*
|
|
|
src 源字符串的首地址(buf的地址)
|
|
|
separator 指定的分割字符
|
|
|
dest 接收子字符串的数组
|
|
|
num 分割后子字符串的个数
|
|
|
*/
|
|
|
char *pNext;
|
|
|
int count = 0;
|
|
|
if(src==NULL||strlen(src)==0) //如果传入的地址为空或长度为0,直接终止
|
|
|
return 0;
|
|
|
if(separator==NULL||strlen(separator)==0) //如未指定分割的字符串,直接终止
|
|
|
return 0;
|
|
|
pNext = (char *)strtok(src,separator); //必须使用(char *)进行强制类型转换(虽然不写有的编译器中不会出现指针错误)
|
|
|
while(pNext != NULL) {
|
|
|
*dest++ = pNext;
|
|
|
++count;
|
|
|
pNext = (char *)strtok(NULL,separator); //必须使用(char *)进行强制类型转换
|
|
|
}
|
|
|
return count;
|
|
|
}
|
|
|
|
|
|
//ret返回长度
|
|
|
char *read_file(char *file_name,int32_t *ret)
|
|
|
{
|
|
|
FILE *f=NULL;
|
|
|
char *content;
|
|
|
int32_t file_len;
|
|
|
if(access(file_name,R_OK)==-1)
|
|
|
{
|
|
|
log_e("access file %s failed",file_name);
|
|
|
return NULL;
|
|
|
}
|
|
|
if((f=fopen(file_name,"r"))==NULL)
|
|
|
{
|
|
|
log_e("open file %s failed",file_name);
|
|
|
return NULL;
|
|
|
}
|
|
|
fseek(f,0,SEEK_END);
|
|
|
file_len=ftell(f);
|
|
|
fseek(f,0,SEEK_SET);
|
|
|
content=(char*)malloc(file_len+1);
|
|
|
if(content == NULL)
|
|
|
{
|
|
|
log_e("malloc failed when !");
|
|
|
fclose(f);
|
|
|
return NULL;
|
|
|
}
|
|
|
memset(content,0,file_len+1);
|
|
|
fread(content,1,file_len,f);
|
|
|
if(ret!=NULL)
|
|
|
*ret=file_len;
|
|
|
fclose(f);
|
|
|
return content;
|
|
|
}
|
|
|
|
|
|
cJSON *file_to_json(char *file)
|
|
|
{
|
|
|
char *file_content;
|
|
|
cJSON *root=NULL;
|
|
|
if((file_content=read_file(file,NULL))!=NULL)
|
|
|
{
|
|
|
if((root=cJSON_Parse(file_content))!=NULL)
|
|
|
{
|
|
|
free(file_content);
|
|
|
//if(cJSON_IsInvalid(root))
|
|
|
return root;
|
|
|
//cJSON_Delete(root);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
/* code */
|
|
|
free(file_content);
|
|
|
}
|
|
|
}
|
|
|
return NULL;
|
|
|
}
|
|
|
|
|
|
int32_t read_dev_cfg(DEV_CFG_INFO *p)
|
|
|
{
|
|
|
cJSON *root;
|
|
|
cJSON *devdesc;
|
|
|
cJSON *port;
|
|
|
cJSON *item;
|
|
|
char *json_string;
|
|
|
int32_t json_len;
|
|
|
|
|
|
if((json_string=read_file(CFG_FILE,&json_len))==NULL)
|
|
|
{
|
|
|
log_e("read cfg file failed");
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
root = cJSON_Parse(json_string);//解析json缓冲区
|
|
|
if(!root)
|
|
|
{
|
|
|
log_e("DevList Invalid Json Error before:[%s]",cJSON_GetErrorPtr());
|
|
|
free(json_string);
|
|
|
return -2;
|
|
|
}
|
|
|
|
|
|
if((devdesc=cJSON_GetObjectItem(root,"devdesc"))!=NULL)
|
|
|
{
|
|
|
if((item=cJSON_GetObjectItem(devdesc,"port"))!=NULL)
|
|
|
{
|
|
|
strncpy(p->dev_desc.port,item->valuestring,sizeof(p->dev_desc.port));
|
|
|
log_i("port=%s",p->dev_desc.port);
|
|
|
}
|
|
|
if((item=cJSON_GetObjectItem(devdesc,"addr"))!=NULL)
|
|
|
{
|
|
|
strncpy(p->dev_desc.addr,item->valuestring,sizeof(p->dev_desc.addr));
|
|
|
log_i("addr=%s",p->dev_desc.addr);
|
|
|
}
|
|
|
if((item=cJSON_GetObjectItem(devdesc,"desc"))!=NULL)
|
|
|
{
|
|
|
strncpy(p->dev_desc.desc,item->valuestring,sizeof(p->dev_desc.desc));
|
|
|
log_i("desc=%s",p->dev_desc.desc);
|
|
|
}
|
|
|
if((item=cJSON_GetObjectItem(devdesc,"model"))!=NULL)
|
|
|
{
|
|
|
strncpy(p->dev_desc.model,item->valuestring,sizeof(p->dev_desc.model));
|
|
|
log_i("model=%s",p->dev_desc.model);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if((port=cJSON_GetObjectItem(root,"port"))!=NULL)
|
|
|
{
|
|
|
if((item=cJSON_GetObjectItem(port,"baud"))!=NULL)
|
|
|
{
|
|
|
strncpy(p->port_cfg.baud,item->valuestring,sizeof(p->port_cfg.baud));
|
|
|
log_i("baud=%s",p->port_cfg.baud);
|
|
|
}
|
|
|
if((item=cJSON_GetObjectItem(port,"bit"))!=NULL)
|
|
|
{
|
|
|
strncpy(p->port_cfg.bits,item->valuestring,sizeof(p->port_cfg.bits));
|
|
|
log_i("bit=%s",p->port_cfg.bits);
|
|
|
}
|
|
|
if((item=cJSON_GetObjectItem(port,"parity"))!=NULL)
|
|
|
{
|
|
|
strncpy(p->port_cfg.parity,item->valuestring,sizeof(p->port_cfg.parity));
|
|
|
log_i("parity=%s",p->port_cfg.parity);
|
|
|
}
|
|
|
}
|
|
|
free(json_string);
|
|
|
cJSON_free(root);
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
|
|
|
int32_t save_to_file(char *file,uint32_t pos,uint8_t *buf,uint32_t size)
|
|
|
{
|
|
|
FILE *f=NULL;
|
|
|
int ret;
|
|
|
if((f=fopen(file,"w"))==NULL)
|
|
|
{
|
|
|
log_e("open file %s failed",file);
|
|
|
return -1;
|
|
|
}
|
|
|
/*if(pos)
|
|
|
{
|
|
|
fseek(f,pos,SEEK_SET);
|
|
|
}*/
|
|
|
ret=fwrite(buf,1,size,f);
|
|
|
fclose(f);
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
|
|
|
int32_t load_from_file(char *file,uint32_t pos,uint8_t *buf,uint32_t size)
|
|
|
{
|
|
|
FILE *f=NULL;
|
|
|
int ret;
|
|
|
if((f=fopen(file,"r"))==NULL)
|
|
|
{
|
|
|
log_e("open file %s failed",file);
|
|
|
return -1;
|
|
|
}
|
|
|
if(pos)
|
|
|
{
|
|
|
fseek(f,pos,SEEK_SET);
|
|
|
}
|
|
|
ret=fread(buf,1,size,f);
|
|
|
fclose(f);
|
|
|
return ret;
|
|
|
}
|
|
|
|