AP05/main.c

1681 lines
66 KiB
C
Raw Permalink Normal View History

2025-04-06 06:41:47 +00:00
#include "main.h"
#define PRINT_TIME_TAG
#define DBG_TAG "main"
#define DBG_LVL DBG_INFO
#include "debug_print.h"
pthread_t pt_uart_send;
pthread_t pt_uart_recv_ack;
pthread_t pt_uart_recv_data;
pthread_t pt_uart_recv_back;
pthread_t pt_mqtt_recv;
pthread_t pt_readqr;
pthread_t pt_mqtt;
pthread_t pt_removelog;
pthread_t pt_watchdog;
pthread_t pt_tagsearch;
pthread_t pt_removeduplicatetag;
uart_utils_t uartSend = {0};
uart_utils_t uartRecvData = {0};
uart_utils_t uartRecvBack = {0};
mqtt_parm_t mqtt_parm={0};
mqtt_utils_t mqtt_config;
struct input_event buff;
bool newappDownloaded=false;
bool isLightOn=false;
bool isLightOnByRule=false;
bool isLabelUp=false;
bool isSearchLabel=false;
bool isSearchLabelOn=false;
bool isLightEnable=true;
bool isSendComEnd=true;
jt_only_tag_t onlyTags[200]={0};
int tagCount=0;
int fd;
int UPCASE=0;
int count_value=0;
int getPayloadTime=120*1000;//usecond
2025-04-18 13:58:27 +00:00
char softwareVersion[16]="1.1.9";
2025-04-06 06:41:47 +00:00
char stationsn[16]="TJ251372224247";//TJ250995217957
char productid[8]="10045";
char appKey[32]="fdhQmhqhvbL1cf1K9mUqt";
char appSecret[64]="s3izIliw0CF48Pcsi16rjOmoFRf5WEt8";
char mqttRawPassword[64]="";
char input_value[1024]={0};//94b4e39caf1d45ee9a071d701cc037b4
char input_value_copy[1024]={0};
char *getDevRegisterStatusUrl="https://gaea-zt-express.com/device/modifyRegisterInfo";
char *logUploadUrl="http://pda.pupumall.net:1180/open/api/device/log/upload";
char *getDevRawPasswordUrl="https://gaea.zt-express.com/device/modifyRegisterInfo";
char *hostDomain="tx-mqtt.zt-express.com";
char lightsn1[9]={0};
char lightsn2[9]={0};
char lightsn3[9]={0};
char lightsn4[9]={0};
char lightsn5[9]={0};
int searchTimeOut=0;
int searchTimeOld=0;
int searchTimeNew=0;
int lightsnNum=0;
int mqtt_port=1883;
/*================================================================================*/
void doCommand_help(int argc, char *argv[])
{
printf("************************************************************\r\n"\
"all commands are case insensitive\r\n"\
"help print this message\r\n"\
"SND [01-60] set sound S01-S60\r\n"\
"************************************************************\r\n");
}
void doCommand(int argc, char *argv[])
{
//sendData(argv[1],argv[0]);
}
__command_initialize("RON", doCommand);
__command_initialize("help",doCommand_help);
/*================================================================================*/
void do_removelog(void)
{
LOG_I("%s\n",__func__);
FILE *fp;
char buffer[5];
char syscmd[32];
int logcount=0;
fp=popen("ls Log.*|wc -l", "r");
fgets(buffer,sizeof(buffer),fp);
logcount=atoi(buffer);
LOG_I("logcount:%d\n",logcount);
if(logcount>=15){
sprintf(syscmd,"ls Log.*|head -%d|xargs rm -fr",logcount-7);
LOG_I("%s\n",syscmd);
system(syscmd);
}else{
LOG_I("logcount less than 15,not remove\n");
}
}
void removeLog(){
int len=0;
char *savedtime=NULL;
char nowtime[16]={0};
long now=0;
long saved=0;
while(1){
now=getCurrentTime();
sprintf(nowtime,"%ld",now);
savedtime=file_to_buffer("logTime",&len);
if(savedtime==NULL){
buffer_to_file("logTime",nowtime,strlen(nowtime),"wb");
savedtime=file_to_buffer("logTime",&len);
}
saved=atol(savedtime);
LOG_I("now=%ld,saved=%ld,nowtime=%s,savedtime=%s\n",now,saved,nowtime,savedtime);
if(now-saved>=(60*60*24*30)){
//if(now-saved>=10){
buffer_to_file("logTime",nowtime,strlen(nowtime),"wb");
do_removelog();
}
sleep(60*60*10);
}
}
void enableWatchDog(){
system("echo 1 > /sys/class/gpio/export");//watchdog enable pin SGM820
system("echo 112 > /sys/class/gpio/export");//feed watchdog pin 113 yellow
system("echo 113 > /sys/class/gpio/export");//feed watchdog pin 113 yellow
system("echo out > /sys/class/gpio/gpio1/direction");
system("echo out > /sys/class/gpio/gpio112/direction");
system("echo out > /sys/class/gpio/gpio113/direction");
system("echo 1 > /sys/class/gpio/gpio1/value");
LOG_I("enable watchdog\n");
system("echo 1 > /sys/class/gpio/gpio112/value");
}
void feedWatchDog(){
while(1){
//LOG_I("feed watchdog\n");
system("echo 0 > /sys/class/gpio/gpio112/value");
usleep(100*1000);
system("echo 1 > /sys/class/gpio/gpio112/value");
sleep(1);
}
}
void rebootSystem(void){
while(!newappDownloaded){
if(!newappDownloaded){
struct tm *result;
time_t timep;
time (&timep);
result = localtime(&timep);
//printf("%d:%d:%d\n",result->tm_hour, result->tm_min, result->tm_sec);
if(result->tm_hour==0 && result->tm_min==0 && result->tm_sec==10){
newappDownloaded=true;
LOG_I("prepare to reboot\n");
}
}
sleep(1);
}
system("reboot");
}
int readQrcode()
{
fd = open("/dev/input/event2", O_RDONLY);
if (fd < 0)
{
LOG_I("can not open scanner input event2!\n");
goto error;
}
while (1)
{
while (read(fd, &buff, sizeof(struct input_event)) == 0)
{
;
}
if (buff.type == EV_KEY){
if(buff.value==0){
#if 0
LOG_I("type:%d code:%d value:%d\n", buff.type, buff.code, buff.value);
#else
switch(buff.code){
case 2:
if(UPCASE==1){
input_value[count_value]='!';
}else{
input_value[count_value]='1';
}
UPCASE=0;
count_value++;
break;
case 3:
if(UPCASE==1){
input_value[count_value]='@';
}else{
input_value[count_value]='2';
}
UPCASE=0;
count_value++;
break;
case 4:
if(UPCASE==1){
input_value[count_value]='#';
}else{
input_value[count_value]='3';
}
UPCASE=0;
count_value++;
break;
case 5:
if(UPCASE==1){
input_value[count_value]='$';
}else{
input_value[count_value]='4';
}
UPCASE=0;
count_value++;
break;
case 6:
if(UPCASE==1){
input_value[count_value]='%';
}else{
input_value[count_value]='5';
}
UPCASE=0;
count_value++;
break;
case 7:
if(UPCASE==1){
input_value[count_value]='^';
}else{
input_value[count_value]='6';
}
UPCASE=0;
count_value++;
break;
case 8:
if(UPCASE==1){
input_value[count_value]='&';
}else{
input_value[count_value]='7';
}
UPCASE=0;
count_value++;
break;
case 9:
if(UPCASE==1){
input_value[count_value]='*';
}else{
input_value[count_value]='8';
}
UPCASE=0;
count_value++;
break;
case 10:
if(UPCASE==1){
input_value[count_value]='(';
}else{
input_value[count_value]='9';
}
UPCASE=0;
count_value++;
break;
case 11:
if(UPCASE==1){
input_value[count_value]=')';
}else{
input_value[count_value]='0';
}
UPCASE=0;
count_value++;
break;
case 12:
if(UPCASE==1){
input_value[count_value]='_';
}else{
input_value[count_value]='-';
}
UPCASE=0;
count_value++;
break;
case 13:
if(UPCASE==1){
input_value[count_value]='+';
}else{
input_value[count_value]='=';
}
UPCASE=0;
count_value++;
break;
case 16:
if(UPCASE==1){
input_value[count_value]='Q';
}else{
input_value[count_value]='q';
}
UPCASE=0;
count_value++;
break;
case 17:
if(UPCASE==1){
input_value[count_value]='W';
}else{
input_value[count_value]='w';
}
UPCASE=0;
count_value++;
break;
case 18:
if(UPCASE==1){
input_value[count_value]='E';
}else{
input_value[count_value]='e';
}
UPCASE=0;
count_value++;
break;
case 19:
if(UPCASE==1){
input_value[count_value]='R';
}else{
input_value[count_value]='r';
}
UPCASE=0;
count_value++;
break;
case 20:
if(UPCASE==1){
input_value[count_value]='T';
}else{
2025-04-09 04:40:51 +00:00
input_value[count_value]='T';
2025-04-06 06:41:47 +00:00
}
UPCASE=0;
count_value++;
break;
case 21:
if(UPCASE==1){
input_value[count_value]='Y';
}else{
input_value[count_value]='y';
}
UPCASE=0;
count_value++;
break;
case 22:
if(UPCASE==1){
input_value[count_value]='U';
}else{
input_value[count_value]='u';
}
UPCASE=0;
count_value++;
break;
case 23:
if(UPCASE==1){
input_value[count_value]='I';
}else{
input_value[count_value]='i';
}
UPCASE=0;
count_value++;
break;
case 24:
if(UPCASE==1){
input_value[count_value]='O';
}else{
input_value[count_value]='o';
}
UPCASE=0;
count_value++;
break;
case 25:
if(UPCASE==1){
input_value[count_value]='P';
}else{
input_value[count_value]='p';
}
UPCASE=0;
count_value++;
break;
case 26:
if(UPCASE==1){
input_value[count_value]='{';
}else{
input_value[count_value]='[';
}
UPCASE=0;
count_value++;
break;
case 27:
if(UPCASE==1){
input_value[count_value]='}';
}else{
input_value[count_value]=']';
}
UPCASE=0;
count_value++;
break;
case 28:
input_value[count_value]='\0';
//LOG_I("%s\n",input_value);
memset(input_value_copy,0,1024);
memcpy(input_value_copy,input_value,1024);
memset(input_value,0,1024);
count_value=0;
int ret=-1;
pthread_t pt_handleqrcode;
ret = pthread_create(&pt_handleqrcode,NULL,actHandleQrcode,input_value_copy);
if(ret!=0){
LOG_I("pthread_create handleqrcode fail\n");
system("reboot");
}else{
pthread_detach(pt_handleqrcode);
}
break;
case 30:
if(UPCASE==1){
input_value[count_value]='A';
}else{
input_value[count_value]='a';
}
UPCASE=0;
count_value++;
break;
case 31:
if(UPCASE==1){
input_value[count_value]='S';
}else{
input_value[count_value]='s';
}
UPCASE=0;
count_value++;
break;
case 32:
if(UPCASE==1){
input_value[count_value]='D';
}else{
input_value[count_value]='d';
}
UPCASE=0;
count_value++;
break;
case 33:
if(UPCASE==1){
input_value[count_value]='F';
}else{
input_value[count_value]='f';
}
UPCASE=0;
count_value++;
break;
case 34:
if(UPCASE==1){
input_value[count_value]='G';
}else{
input_value[count_value]='g';
}
UPCASE=0;
count_value++;
break;
case 35:
if(UPCASE==1){
input_value[count_value]='H';
}else{
input_value[count_value]='h';
}
UPCASE=0;
count_value++;
break;
case 36:
if(UPCASE==1){
input_value[count_value]='J';
}else{
input_value[count_value]='j';
}
UPCASE=0;
count_value++;
break;
case 37:
if(UPCASE==1){
input_value[count_value]='K';
}else{
input_value[count_value]='k';
}
UPCASE=0;
count_value++;
break;
case 38:
if(UPCASE==1){
input_value[count_value]='L';
}else{
input_value[count_value]='l';
}
UPCASE=0;
count_value++;
break;
case 39:
if(UPCASE==1){
input_value[count_value]=':';
}else{
input_value[count_value]=';';
}
UPCASE=0;
count_value++;
break;
case 40:
if(UPCASE==1){
input_value[count_value]='"';
}else{
input_value[count_value]='\'';
}
UPCASE=0;
count_value++;
break;
case 42:
UPCASE=1;
break;
case 43:
if(UPCASE==1){
input_value[count_value]='|';
}else{
input_value[count_value]='\\';
}
UPCASE=0;
count_value++;
break;
case 44:
if(UPCASE==1){
input_value[count_value]='Z';
}else{
input_value[count_value]='z';
}
UPCASE=0;
count_value++;
break;
case 45:
if(UPCASE==1){
input_value[count_value]='X';
}else{
input_value[count_value]='x';
}
UPCASE=0;
count_value++;
break;
case 46:
if(UPCASE==1){
input_value[count_value]='C';
}else{
input_value[count_value]='c';
}
UPCASE=0;
count_value++;
break;
case 47:
if(UPCASE==1){
input_value[count_value]='V';
}else{
input_value[count_value]='v';
}
UPCASE=0;
count_value++;
break;
case 48:
if(UPCASE==1){
input_value[count_value]='B';
}else{
input_value[count_value]='b';
}
UPCASE=0;
count_value++;
break;
case 49:
if(UPCASE==1){
input_value[count_value]='N';
}else{
input_value[count_value]='n';
}
UPCASE=0;
count_value++;
break;
case 50:
if(UPCASE==1){
input_value[count_value]='M';
}else{
input_value[count_value]='m';
}
UPCASE=0;
count_value++;
break;
case 51:
if(UPCASE==1){
input_value[count_value]='<';
}else{
input_value[count_value]=',';
}
UPCASE=0;
count_value++;
break;
case 52:
if(UPCASE==1){
input_value[count_value]='>';
}else{
input_value[count_value]='.';
}
UPCASE=0;
count_value++;
break;
case 53:
if(UPCASE==1){
input_value[count_value]='?';
}else{
input_value[count_value]='/';
}
UPCASE=0;
count_value++;
break;
}
#endif
}
}
}
close(fd);
return 0;
error:
return 1;
}
void writeWpa(char *filename,char *ssid,char *psw){
LOG_I("%s\n",__func__);
buffer_to_file(filename,"# WPA-PSK/TKIP\n",strlen("# WPA-PSK/TKIP\n"),"wb");
buffer_to_file(filename,"ap_scan=1\n",strlen("ap_scan=1\n"),"a");
buffer_to_file(filename,"ctrl_interface=/var/run/wpa_supplicant\n",strlen("ctrl_interface=/var/run/wpa_supplicant\n"),"a");
buffer_to_file(filename,"network={\n",strlen("network={\n"),"a");
buffer_to_file(filename,"ssid=\"",strlen("ssid=\""),"a");
buffer_to_file(filename,ssid,strlen(ssid),"a");
buffer_to_file(filename,"\"",strlen("\""),"a");
buffer_to_file(filename,"\n",strlen("\n"),"a");
buffer_to_file(filename,"key_mgmt=WPA-PSK\n",strlen("key_mgmt=WPA-PSK\n"),"a");
buffer_to_file(filename,"proto=WPA RSN\n",strlen("proto=WPA RSN\n"),"a");
buffer_to_file(filename,"pairwise=CCMP TKIP\n",strlen("pairwise=CCMP TKIP\n"),"a");
buffer_to_file(filename,"group=CCMP TKIP\n",strlen("group=CCMP TKIP\n"),"a");
buffer_to_file(filename,"psk=\"",strlen("psk=\""),"a");
buffer_to_file(filename,psw,strlen(psw),"a");
buffer_to_file(filename,"\"",strlen("\""),"a");
buffer_to_file(filename,"\n",strlen("\n"),"a");
buffer_to_file(filename,"}\n",strlen("}\n"),"a");
buffer_to_file(filename,"\n",strlen("\n"),"a");
}
void writeWlan(char *filename,char *netway,char *gate,char *ip){
LOG_I("%s\n",__func__);
buffer_to_file(filename,"METHOD=",strlen("METHOD="),"wb");
buffer_to_file(filename,netway,strlen(netway),"a");
buffer_to_file(filename,"\n",strlen("\n"),"a");
if(strcmp(netway,"STATIC")==0){
buffer_to_file(filename,"IPADDR=",strlen("IPADDR="),"a");
buffer_to_file(filename,ip,strlen(ip),"a");
buffer_to_file(filename,"\n",strlen("\n"),"a");
buffer_to_file(filename,"NETMASK=255.255.255.0\n",strlen("NETMASK=255.255.255.0\n"),"a");
buffer_to_file(filename,"GATEWAY=",strlen("GATEWAY="),"a");
buffer_to_file(filename,gate,strlen(gate),"a");
buffer_to_file(filename,"\n",strlen("\n"),"a");
}
}
void removeFile(char *filename){
char cmd[32]={0};
sprintf(cmd,"rm %s",filename);
//LOG_I("%s\n",cmd);
system(cmd);
}
void *actHandleQrcode(void *parm){
char *str=(char *)parm;
LOG_I("scan:%s\n",str);
if(strstr(str,"{")!=NULL){
char deviceSecret[64]={0};
char deviceCode[64]={0};
char netWorkType[64]={0};
char netWorkSsid[64]={0};
char netWorkPassword[64]={0};
get_string_from_json_string_by_key_unescape(str, "deviceSecret",deviceSecret,64);
LOG_I("deviceSecret:%s\n",deviceSecret);
get_string_from_json_string_by_key_unescape(str, "deviceCode",deviceCode,64);
LOG_I("deviceCode:%s\n",deviceCode);
if(strcmp(deviceSecret,"")!=0 && strcmp(deviceCode,"")!=0){
buffer_to_file("deviceSecret",deviceSecret,strlen(deviceSecret),"wb");
buffer_to_file("savedDevSn",deviceCode,strlen(deviceCode),"wb");
}
get_string_from_json_string_by_key_unescape(str, "netWorkType",netWorkType,64);
LOG_I("netWorkType:%s\n",netWorkType);
if(strcmp(netWorkType,"net_wifi")==0){
buffer_to_file("nettype","net_wifi",strlen("net_wifi"),"wb");
get_string_from_json_string_by_key_unescape(str, "netWorkSsid",netWorkSsid,64);
LOG_I("netWorkSsid:%s\n",netWorkSsid);
get_string_from_json_string_by_key_unescape(str, "netWorkPassword",netWorkPassword,64);
LOG_I("netWorkPassword:%s\n",netWorkPassword);
if(strcmp(netWorkSsid,"")!=0 && strcmp(netWorkPassword,"")!=0){
writeWpa("/etc/wpa_supplicant.conf",netWorkSsid,netWorkPassword);
sleep(1);
system("reboot");
}else{
}
}else if(strcmp(netWorkType,"net_wired")==0){
buffer_to_file("nettype","net_wired",strlen("net_wired"),"wb");
LOG_I("use wired\n");
sleep(1);
system("reboot");
}else if(strcmp(netWorkType,"net_mobile")==0){
buffer_to_file("nettype","net_mobile",strlen("net_mobile"),"wb");
LOG_I("use mobile\n");
sleep(1);
system("reboot");
}
}else{
if(strstr(str,"TJ")!=NULL && strlen(str)==14){
buffer_to_file("savedDevSn",str,strlen(str),"wb");
}
}
pthread_exit(NULL);
}
char *file_to_buffer(const char *pathname, unsigned int *size){
FILE *fp = NULL;
char *temp = NULL;
fp = fopen(pathname, "rb");
if(fp==NULL){
//LOG_I("open %s error\n", pathname);
}else{
fseek(fp,0,2);
*size = ftell(fp);
rewind(fp);
temp = (char *)malloc(*size);
fread(temp, 1, *size, fp);
fclose(fp);
}
return temp;
}
/*================================================================================*/
void showShellInfo(char *command){
FILE *fp;
char buffer[512]={0};
fp=popen(command, "r");
fread(buffer,1,sizeof(buffer),fp);
LOG_I("\n%s",buffer);
}
void getTimeStr(char *buffer,int len){
time_t timep;
struct tm *p;
time (&timep);
p=localtime(&timep);
strftime (buffer,len,"%Y-%m-%d %H:%M:%S",p);
}
long getCurrentTime(){
struct timeval begin;
gettimeofday(&begin,NULL);
long long beginTime = (long long)begin.tv_sec * 1000 + (long long)begin.tv_usec / 1000;
long last=beginTime/1000;
return last;
}
void myrand(char *randnum,int len){
int i;
srand((unsigned)time(0));
for(i=0;i<=len-1;i++)
randnum[i]=rand()%10 +'0';
randnum[len]='\0';
}
void removeSpaces(char *str) {
int i = 0, j = 0;
while (str[i] != '\0') {
if (str[i] != ' ') {
str[j++] = str[i];
}
i++;
}
str[j] = '\0';
}
void getDevRawPassword(char *devsn){
LOG_I("devsn:%s\n",devsn);
json_object *root = NULL;
const char *payload = NULL;
char send_request[256]={0};
char signbase64[256]={0};
char signtmp[256]={0};
char jsondata[256] = {0};
char tmpdata[256] = {0};
//char tmpdata1[64] = {0};
char buffer[512];
FILE *fp;
root = json_object_new_object();
if(root == NULL){
LOG_I("json_object_new_object error\n");
}else{
json_object_object_add(root, "deviceId",json_object_new_string(devsn));
json_object_object_add(root, "oldPassword",json_object_new_string("12345"));
payload = json_object_to_json_string(root);
removeSpaces(payload);
strcpy(signtmp,payload);
strcat(signtmp,appSecret);
//LOG_I("signtmp:%s\n",signtmp);
get_base64_md5_from_string(signbase64, sizeof(signbase64), signtmp, strlen(signtmp));
//LOG_I("signbase64:%s\n",signbase64);
sprintf(send_request,"curl -X POST -H 'x-appKey:%s' -H 'x-datadigest:%s' -H 'Content-Type: application/json' -d '%s' %s",
appKey,signbase64,payload,getDevRawPasswordUrl);
LOG_I("send_request:%s\n",send_request);
fp=popen(send_request,"r");
fgets(buffer,sizeof(buffer),fp);
//get_string_from_json_string_by_key_unescape(buffer, "message", tmpdata1, sizeof(tmpdata1));
//LOG_I("message: %s\n",tmpdata1);
get_string_from_json_string_by_key_unescape(buffer, "result", tmpdata, sizeof(tmpdata));
LOG_I("result: %s\n",tmpdata);
snprintf(jsondata, sizeof(jsondata), "{%s}", tmpdata);
//LOG_I("jsondata:%s\n",jsondata);
get_string_from_json_string_by_key_unescape(jsondata, "password", mqttRawPassword, sizeof(mqttRawPassword));
LOG_I("save mqttRawPassword:%s\n",mqttRawPassword);
if(strcmp(mqttRawPassword,"")!=0){
buffer_to_file("mqttRawPassword",mqttRawPassword,strlen(mqttRawPassword),"wb");
}
}
}
/*================================================================================*/
void mqtt_init(){
int ret=0;
char username[32] = {0};
char password[32] = {0};
char clientid[256] = {0};
//char clientid[256] = "TJ250995217957|signmethod=hmacsha1|timestamp=1741416329331";
char hostip[16]={0};
//char protocol[64]="mqtt";
//char RawPassword[64]="94b4e39caf1d45ee9a071d701cc037b4";
struct timeval tv;
//MQTTASYNC_TRACE_MAXIMUM
//MQTTASYNC_TRACE_MEDIUM
//MQTTASYNC_TRACE_MINIMUM
//MQTTASYNC_TRACE_PROTOCOL
//MQTTASYNC_TRACE_ERROR
//MQTTASYNC_TRACE_SEVERE
//MQTTASYNC_TRACE_FATAL
//mqtt_config.tracelevel=get_trace_level_by_name("MQTTASYNC_TRACE_PROTOCOL");
gettimeofday(&tv, NULL);
snprintf(clientid,sizeof(clientid),"%s|signmethod=hmacsha1|timestamp=%ld",stationsn,tv.tv_sec*1000+tv.tv_usec);
LOG_I("clientid:%s,mqttRawPassword:%s\n",clientid,mqttRawPassword);
hmacsha1(mqttRawPassword,clientid,password,sizeof(password));
mqtt_config.tracelevel=MQTTASYNC_TRACE_PROTOCOL;
mqtt_config.retain=0;
mqtt_config.port=mqtt_port;
mqtt_config.keepalive=30;
//memset(mqtt_config.protocol,0,sizeof(mqtt_config.protocol));
//memcpy(mqtt_config.protocol,protocol,sizeof(protocol));
memset(mqtt_config.clientid,0,sizeof(mqtt_config.clientid));
memcpy(mqtt_config.clientid,clientid,sizeof(clientid));
snprintf(username, sizeof(username), "%s", stationsn);
LOG_I("username:%s,password:%s\n",username,password);
memset(mqtt_config.productcode, 0, sizeof(mqtt_config.productcode));
memcpy(mqtt_config.productcode, productid, strlen(productid));
memset(mqtt_config.username, 0, sizeof(mqtt_config.username));
memcpy(mqtt_config.username, username, strlen(username));
memset(mqtt_config.password, 0, sizeof(mqtt_config.password));
memcpy(mqtt_config.password, password, strlen(password));
get_ip_by_domain(hostDomain,hostip,16);
memset(mqtt_config.host, 0, sizeof(mqtt_config.host));
memcpy(mqtt_config.host, hostip, strlen(hostip));
mqtt_config.MQTTVersion = 4;
/* will options */
mqtt_config.will_topic = NULL;
mqtt_config.will_payload = NULL;
mqtt_config.will_qos = 2;
mqtt_config.will_retain = 0;
/* TLS options */
mqtt_config.insecure = 0;
mqtt_config.capath = NULL;
mqtt_config.cert = NULL;
mqtt_config.cafile = NULL;
mqtt_config.key = NULL;
mqtt_config.keypass = NULL;
mqtt_config.ciphers = NULL;
ret = sem_init(&mqtt_config.sem_connect, 0, 0);
if (ret != 0)
{
LOG_I("sem_connect init error\n");
goto error;
}
ret = mqtt_utils_init(&mqtt_config);
if (ret < 0)
{
LOG_I("mqtt_utils_init error\n");
ret = -1;
goto error;
}
sem_wait(&mqtt_config.sem_connect);
ret = 0;
error:
return;
}
/*================================================================================*/
void *thread_mqtt(void *arg){
mqtt_init();
}
void *thread_removelog(void *arg){
removeLog();
}
void *thread_feed_watchdog(void *arg){
feedWatchDog();
}
void *thread_readqr(void *arg){
readQrcode();
}
void *thread_tag_search_send(void *arg){
while(1){
if(isSearchLabelOn){
isSearchLabel=true;
uart_data_send_head_search(&uartSend,5);
sleep(10);
searchTimeNew=getCurrentTime();
LOG_I("searchTimeNew:%d\n",searchTimeNew);
if((searchTimeNew-searchTimeOld)>=searchTimeOut){
isSearchLabelOn=false;
search_tag_report();
}
}
}
}
//char *tag_1="00A04CCE";
//char *tag_2="00A04C0E";
//char *tag_3="00A04C0F";
//char *tag_4="00A04C11";
//char *tag_5="00A04CD0";
void *thread_uart_recv_ack(void *arg){
uint16_t parm_ack;
int ret=0;
while(1){
ret=uart_data_receive_ack(&uartSend,&parm_ack);
if(ret>0){
LOG_I("ack:%x\n",parm_ack);
if(parm_ack==0x2323){
if(isLightOn){
char *flash=mqtt_parm.msg_flash;
char *light=mqtt_parm.msg_lightDuration;
uint32_t tag1=strtol(lightsn1,NULL,16);
uint32_t tag2=strtol(lightsn2,NULL,16);
uint32_t tag3=strtol(lightsn3,NULL,16);
uint32_t tag4=strtol(lightsn4,NULL,16);
uint32_t tag5=strtol(lightsn5,NULL,16);
uint8_t changecolor=0;
//8灭 1红 2黄 3蓝 4绿 5青 6白 7紫 xitu
//0灭 1蓝 2绿 3青 4红 5紫 6黄 7白 yitaile
if(strcmp(mqtt_parm.msg_color,"1")==0){
changecolor=4;
}else if(strcmp(mqtt_parm.msg_color,"2")==0){
changecolor=6;
}else if(strcmp(mqtt_parm.msg_color,"3")==0){
changecolor=1;
}else if(strcmp(mqtt_parm.msg_color,"4")==0){
changecolor=2;
}else if(strcmp(mqtt_parm.msg_color,"5")==0){
changecolor=3;
}else if(strcmp(mqtt_parm.msg_color,"6")==0){
changecolor=7;
}else if(strcmp(mqtt_parm.msg_color,"7")==0){
changecolor=5;
}else if(strcmp(mqtt_parm.msg_color,"8")==0){
changecolor=0;
}
jt_ledctrl_package_t led_ctrl={
.s.color=changecolor,
.s.sound=atoi(mqtt_parm.msg_sound),
.s.flash=atoi(mqtt_parm.msg_flash),
};
uart_data_send_lighton(&uartSend,led_ctrl.ch,atoi(flash),atoi(light),tag1,tag2,tag3,tag4,tag5,lightsnNum);
isLightOn=false;
}
if(isSearchLabel){
uart_data_send_search(&uartSend,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF);
isSearchLabel=false;
}
if(isLightOnByRule){
#if 0
uint16_t label2=strtol("FF01",NULL,16)&0xFFFF;
uint32_t label3=strtol("663855c0",NULL,16);
uint16_t flash_i=strtol("1",NULL,16)&0xFFFF;
uint16_t light_d=strtol("5",NULL,16)&0xFFFF;
jt_ledctrl_package_t led_ctrl={
.s.color=1,
.s.sound=1,
.s.flash=1,
};
uart_data_send_light_rule(&uartSend,atoi("03"),label2,label3,
atoi("0"), atoi("0"),atoi("0"),
led_ctrl.ch,
flash_i,light_d);
#else
uint16_t label2=strtol(mqtt_parm.msg_label2,NULL,16)&0xFFFF;
uint32_t label3=strtol(mqtt_parm.msg_label3,NULL,16);
uint16_t flash_i=strtol(mqtt_parm.msg_flashInterval,NULL,16)&0xFFFF;
uint16_t light_d=strtol(mqtt_parm.msg_lightDuration,NULL,16)&0xFFFF;
uint8_t changecolor=0;
//8灭 1红 2黄 3蓝 4绿 5青 6白 7紫 xitu
//0灭 1蓝 2绿 3青 4红 5紫 6黄 7白 yitaile
if(strcmp(mqtt_parm.msg_color,"1")==0){
changecolor=4;
}else if(strcmp(mqtt_parm.msg_color,"2")==0){
changecolor=6;
}else if(strcmp(mqtt_parm.msg_color,"3")==0){
changecolor=1;
}else if(strcmp(mqtt_parm.msg_color,"4")==0){
changecolor=2;
}else if(strcmp(mqtt_parm.msg_color,"5")==0){
changecolor=3;
}else if(strcmp(mqtt_parm.msg_color,"6")==0){
changecolor=7;
}else if(strcmp(mqtt_parm.msg_color,"7")==0){
changecolor=5;
}else if(strcmp(mqtt_parm.msg_color,"8")==0){
changecolor=0;
}
jt_ledctrl_package_t led_ctrl={
.s.color=changecolor,
.s.sound=atoi(mqtt_parm.msg_sound),
.s.flash=atoi(mqtt_parm.msg_flash),
};
uart_data_send_light_rule(&uartSend,atoi(mqtt_parm.msg_label1),label2,label3,
atoi(mqtt_parm.msg_label1Rule), atoi(mqtt_parm.msg_label2Rule),atoi(mqtt_parm.msg_label3Rule),
led_ctrl.ch,
flash_i,light_d);
#endif
isLightOnByRule=false;
}
if(isLabelUp){
#if 1
uint32_t tag=strtol(mqtt_parm.msg_sn,NULL,16);
uint16_t label2=strtol(mqtt_parm.msg_label2,NULL,16)&0xFFFF;
uint32_t label3=strtol(mqtt_parm.msg_label3,NULL,16);
uart_data_send_lable(&uartSend,tag,atoi(mqtt_parm.msg_label1),label2,label3,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
1);
#else
uint32_t tag=strtol("00A04C0F",NULL,16);
uint16_t label2=strtol("FF01",NULL,16)&0xFFFF;
uint32_t label3=strtol("663855c0",NULL,16);
uart_data_send_lable(&uartSend,tag,atoi("03"),label2,label3,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
1);
#endif
isLabelUp=false;
}
}else if(parm_ack==0x4646){
isSendComEnd=true;
}
}
sleep(1);
}
}
void *thread_uart_recv_data(void *arg){
uint16_t parmAck;
uint32_t tagCode;
uint8_t tagSignal;
uint8_t totalLen;
uint8_t tagFeature;
uint8_t count;
uint16_t batteryV;
uint16_t version;
uint8_t ledCtrl;
uint8_t lable1;
uint16_t lable2;
uint32_t lable3;
while(1){
uart_data_receive_data_back(&uartRecvData,&parmAck,&tagCode,&tagSignal,&totalLen,&tagFeature,
&count,&batteryV,&version,&ledCtrl,&lable1,&lable2,&lable3);
//LOG_I("recv_data:%04x,%08x,%02x,%02x,%02x,%02x,%04x,%04x,%02x,%02x,%04x,%08x\n",parmAck,tagCode,tagSignal,totalLen,
// tagFeature,count,batteryV,version,ledCtrl,lable1,lable2,lable3);
//sleep(2);
}
}
void *thread_uart_recv_back(void *arg){
uint16_t parmAck;
uint32_t tagCode;
uint8_t tagSignal;
uint8_t totalLen;
uint8_t tagFeature;
uint8_t count;
uint16_t batteryV;
uint16_t version;
uint8_t ledCtrl;
uint8_t lable1;
uint16_t lable2;
uint32_t lable3;
while(1){
uart_data_receive_data_back(&uartRecvBack,&parmAck,&tagCode,&tagSignal,&totalLen,&tagFeature,
&count,&batteryV,&version,&ledCtrl,&lable1,&lable2,&lable3);
//LOG_I("recv_back:%04x,%08x,%02x,%02x,%02x,%02x,%04x,%04x,%02x,%02x,%04x,%08x\n",parmAck,tagCode,tagSignal,totalLen,
//tagFeature,count,batteryV,version,ledCtrl,lable1,lable2,lable3);
if(tagFeature==0x83){
LOG_I("search tag:%08x,battery:%04x\n",tagCode,batteryV);
PutDataIntoQueue(tagCode,batteryV);
}else if(tagFeature==0x82){
LOG_I("light down tag:%08x,battery:%04x\n",tagCode,batteryV);
light_off_tag_report(tagCode,batteryV);
}else if(tagFeature==0x81){
LOG_I("key down tag:%08x,battery:%04x\n",tagCode,batteryV);
}
}
}
void light_off_tag_report(uint32_t name,uint16_t batteryv){
json_object *lights = NULL;
lights=json_object_new_array();
char tagName[9]={0};
char battery[5]={0};
sprintf(tagName,"%08X",name);
sprintf(battery,"%d",batteryv);
json_object *light = json_object_new_object();
json_object_object_add(light, "sn", json_object_new_string(tagName));
json_object_object_add(light, "battery", json_object_new_string(battery));
json_object_array_add(lights,light);
char myid[32]={0};
myrand(myid,19);
mqtt_server_light_status_report(stationsn,myid,"2",lights);
}
void search_tag_report(){
json_object *lights = NULL;
lights=json_object_new_array();
for(int i=0;i<tagCount;i++){
LOG_I("onlyTags:%08X,%04x\n",onlyTags[i].name,onlyTags[i].battery);
char tagName[9]={0};
char battery[5]={0};
sprintf(tagName,"%08X",onlyTags[i].name);
sprintf(battery,"%d",onlyTags[i].battery);
json_object *light = json_object_new_object();
json_object_object_add(light, "sn", json_object_new_string(tagName));
json_object_object_add(light, "battery", json_object_new_string(battery));
json_object_array_add(lights,light);
}
LOG_I("tagCount:%d\n",tagCount);
mqtt_server_light_status_report(stationsn,mqtt_parm.msg_messageId,"1",lights);
mqtt_service_reply(stationsn,mqtt_parm.msg_messageId,"ok",1,productid);
tagCount=0;
memset(onlyTags,0,sizeof(onlyTags));
}
void addOnlyTag(uint32_t tagname,uint16_t battery){
int i=0;
if(tagCount==0){
jt_only_tag_t jt_only_tag = {
.name=tagname,
.battery=battery,
};
onlyTags[tagCount]=jt_only_tag;
tagCount++;
}else{
for(i=0;i<tagCount;i++){
if(tagname==onlyTags[i].name){
break;
}
}
if(i==tagCount){
jt_only_tag_t jt_only_tag = {
.name=tagname,
.battery=battery,
};
onlyTags[tagCount]=jt_only_tag;
tagCount++;
}
}
}
void *thread_remove_duplicate_tag(void *arg){
uint32_t tagname=0;
uint16_t battery=0;
while(1){
if(GetDataFromQueue(&tagname,&battery)==0){
//LOG_I("%08x,%04x\n",tagname,battery);
addOnlyTag(tagname,battery);
}
}
}
void *thread_mqtt_recv(void *arg){
char payload[1024]={0};
char msg_body_value[1024] ={0};
char msg_body[1024] = {0};
char msg_data_value[1024] = {0};
char msg_data[1024] = {0};
char msg_labelconfig_value[1024] = {0};
char msg_labelconfig[1024] = {0};
char msg_lightsn_value[128] = {0};
char msg_lightsn[128] = {0};
while(1){
if(isSendComEnd){
if(GetDataFromMQueue(payload)==0){
//getPayloadTime=120*1000;
LOG_I("payload:%s\n",payload);
get_string_from_json_string_by_key_unescape(payload,"messageId",mqtt_parm.msg_messageId,sizeof(mqtt_parm.msg_messageId));
LOG_I("messageId:%s\n",mqtt_parm.msg_messageId);
if(isSearchLabelOn){
mqtt_service_reply(stationsn,mqtt_parm.msg_messageId,"search busy",0,productid);
}else{
get_string_from_json_string_by_key_unescape(payload,"deviceId",mqtt_parm.msg_deviceId,sizeof(mqtt_parm.msg_deviceId));
LOG_I("deviceId:%s\n",mqtt_parm.msg_deviceId);
get_int_from_json_string_by_key(payload,"timestamp",&mqtt_parm.msg_timestamp);
LOG_I("timestamp:%d\n",mqtt_parm.msg_timestamp);
get_string_from_json_string_by_key_unescape(payload, "msg", msg_body_value, sizeof(msg_body_value));
snprintf(msg_body, sizeof(msg_body), "%s", msg_body_value);
LOG_I("msg_body:%s\n",msg_body);
get_string_from_json_string_by_key_unescape(msg_body, "msgType", mqtt_parm.msg_type, sizeof(mqtt_parm.msg_type));
LOG_I("msgType:%s\n",mqtt_parm.msg_type);
get_string_from_json_string_by_key_unescape(msg_body, "data", msg_data_value, sizeof(msg_data_value));
snprintf(msg_data, sizeof(msg_data), "{%s}", msg_data_value);
LOG_I("msg_data:%s\n",msg_data);
if(strcmp(mqtt_parm.msg_type,"1001")==0){
LOG_I("1001 dev disable\n");
isLightEnable=false;
buffer_to_file("LightEnable","disable",strlen("disable"),"wb");
mqtt_service_reply(stationsn,mqtt_parm.msg_messageId,"ok",1,productid);
}else if(strcmp(mqtt_parm.msg_type,"1002")==0){
LOG_I("1002 dev enable\n");
isLightEnable=true;
buffer_to_file("LightEnable","enable",strlen("enable"),"wb");
mqtt_service_reply(stationsn,mqtt_parm.msg_messageId,"ok",1,productid);
}else if(strcmp(mqtt_parm.msg_type,"1005")==0){
LOG_I("1005 dev reboot\n");
mqtt_service_reply(stationsn,mqtt_parm.msg_messageId,"ok",1,productid);
system("reboot");
}else if(strcmp(mqtt_parm.msg_type,"1014")==0){
LOG_I("1014 update fw\n");
get_int_from_json_string_by_key(msg_data, "needWifi", &mqtt_parm.msg_needWifi);
LOG_I("needWifi:%d\n",mqtt_parm.msg_needWifi);
get_string_from_json_string_by_key_unescape(msg_data, "zipPath", mqtt_parm.msg_zipPath, sizeof(mqtt_parm.msg_zipPath));
LOG_I("zipPath:%s\n",mqtt_parm.msg_zipPath);
get_int_from_json_string_by_key(msg_data, "installType", &mqtt_parm.msg_installType);
LOG_I("installType:%d\n",mqtt_parm.msg_installType);
get_int_from_json_string_by_key(msg_data, "immediately", &mqtt_parm.msg_immediately);
LOG_I("immediately:%d\n",mqtt_parm.msg_immediately);
get_int_from_json_string_by_key(msg_data, "installTime", &mqtt_parm.msg_installTime);
LOG_I("installTime:%d\n",mqtt_parm.msg_installTime);
get_string_from_json_string_by_key_unescape(msg_data, "version", mqtt_parm.msg_version, sizeof(mqtt_parm.msg_version));
LOG_I("version:%s\n",mqtt_parm.msg_version);
mqtt_service_reply(stationsn,mqtt_parm.msg_messageId,"ok",1,productid);
char otaCmd[256]={0};
sprintf(otaCmd,"curl -o /home/linaro/tx_server %s",mqtt_parm.msg_zipPath);
//sprintf(otaCmd,"curl -o /root/tx_server https://fscdn.zto.com/cloudm/iot-device-package/7d609af2165b4d14ae318f17659b2fbf.bin");
system(otaCmd);
sleep(8);
system("reboot");
}else if(strcmp(mqtt_parm.msg_type,"5003")==0){
LOG_I("5003 need report station info\n");
updateStationInfo(mqtt_parm.msg_messageId);
}else if(strcmp(mqtt_parm.msg_type,"5005")==0){
LOG_I("5005 cloud search light\n");
isSendComEnd=false;
isLightOn=false;
isLightOnByRule=false;
isLabelUp=false;
isSearchLabel=true;
get_string_from_json_string_by_key_unescape(msg_data, "scene", mqtt_parm.msg_scene, sizeof(mqtt_parm.msg_scene));
LOG_I("scene:%s\n",mqtt_parm.msg_scene);
get_string_from_json_string_by_key_unescape(msg_data, "timeout", mqtt_parm.msg_timeout, sizeof(mqtt_parm.msg_timeout));
LOG_I("timeout:%s\n",mqtt_parm.msg_timeout);
searchTimeOut=atoi(mqtt_parm.msg_timeout);
LOG_I("searchTimeOut:%d\n",searchTimeOut);
searchTimeOld=getCurrentTime();
LOG_I("searchTimeOld:%d\n",searchTimeOld);
isSearchLabelOn=true;
}else if(strcmp(mqtt_parm.msg_type,"3015")==0){
LOG_I("3015 light on\n");
get_string_from_json_string_by_key_unescape(msg_data, "scene", mqtt_parm.msg_scene, sizeof(mqtt_parm.msg_scene));
LOG_I("scene:%s\n",mqtt_parm.msg_scene);
get_string_from_json_string_by_key_unescape(msg_data, "lights", mqtt_parm.msg_lights, sizeof(mqtt_parm.msg_lights));
LOG_I("lights:%s\n",mqtt_parm.msg_lights);
get_size_from_json_string_arry_by_key(msg_data, "lights", &mqtt_parm.msg_opNumber);
LOG_I("lights size = %d\n", mqtt_parm.msg_opNumber);
lightsnNum=mqtt_parm.msg_opNumber;
for(int i = 0; i < mqtt_parm.msg_opNumber; i++){
get_string_from_json_string_arry_by_key(msg_data,"lights",msg_lightsn_value,128,i);
snprintf(msg_lightsn, sizeof(msg_lightsn), "{%s}", msg_lightsn_value);
get_string_from_json_string_by_key_unescape(msg_lightsn, "sn", mqtt_parm.msg_sn,sizeof(mqtt_parm.msg_sn));
LOG_I("sn:%s\n",mqtt_parm.msg_sn);
if(i==0){
memset(lightsn1,0,9);
memcpy(lightsn1,mqtt_parm.msg_sn,strlen(mqtt_parm.msg_sn));
}else if(i==1){
memset(lightsn2,0,9);
memcpy(lightsn2,mqtt_parm.msg_sn,strlen(mqtt_parm.msg_sn));
}else if(i==2){
memset(lightsn3,0,9);
memcpy(lightsn3,mqtt_parm.msg_sn,strlen(mqtt_parm.msg_sn));
}else if(i==3){
memset(lightsn4,0,9);
memcpy(lightsn4,mqtt_parm.msg_sn,strlen(mqtt_parm.msg_sn));
}else if(i==4){
memset(lightsn5,0,9);
memcpy(lightsn5,mqtt_parm.msg_sn,strlen(mqtt_parm.msg_sn));
}
}
get_string_from_json_string_by_key_unescape(msg_data, "color", mqtt_parm.msg_color, sizeof(mqtt_parm.msg_color));
LOG_I("color:%s\n",mqtt_parm.msg_color);
get_string_from_json_string_by_key_unescape(msg_data, "sound", mqtt_parm.msg_sound, sizeof(mqtt_parm.msg_sound));
LOG_I("sound:%s\n",mqtt_parm.msg_sound);
get_string_from_json_string_by_key_unescape(msg_data, "flash", mqtt_parm.msg_flash, sizeof(mqtt_parm.msg_flash));
LOG_I("flash:%s\n",mqtt_parm.msg_flash);
get_string_from_json_string_by_key_unescape(msg_data, "flashInterval", mqtt_parm.msg_flashInterval,
sizeof(mqtt_parm.msg_flashInterval));
LOG_I("flashInterval:%s\n",mqtt_parm.msg_flashInterval);
get_string_from_json_string_by_key_unescape(msg_data, "lightDuration", mqtt_parm.msg_lightDuration,
sizeof(mqtt_parm.msg_lightDuration));
LOG_I("lightDuration:%s\n",mqtt_parm.msg_lightDuration);
if(isLightEnable && (mqtt_parm.msg_opNumber>0)){
mqtt_service_reply(stationsn,mqtt_parm.msg_messageId,"ok",1,productid);
isSendComEnd=false;
isLightOn=true;
isLightOnByRule=false;
isLabelUp=false;
isSearchLabel=false;
uart_data_send_head_lighton(&uartSend,5,lightsnNum);
}else{
mqtt_service_reply(stationsn,mqtt_parm.msg_messageId,"light disabled or sn is null",0,productid);
}
}else if(strcmp(mqtt_parm.msg_type,"3022")==0){
LOG_I("3022 updata lable\n");
get_string_from_json_string_by_key_unescape(msg_data, "label1", mqtt_parm.msg_label1, sizeof(mqtt_parm.msg_label1));
LOG_I("label1:%s\n",mqtt_parm.msg_label1);
get_string_from_json_string_by_key_unescape(msg_data, "label2", mqtt_parm.msg_label2, sizeof(mqtt_parm.msg_label2));
LOG_I("label2:%s\n",mqtt_parm.msg_label2);
get_string_from_json_string_by_key_unescape(msg_data, "label3", mqtt_parm.msg_label3, sizeof(mqtt_parm.msg_label3));
LOG_I("label3:%s\n",mqtt_parm.msg_label3);
get_string_from_json_string_by_key_unescape(msg_data, "sn", mqtt_parm.msg_sn, sizeof(mqtt_parm.msg_sn));
LOG_I("sn:%s\n",mqtt_parm.msg_sn);
if(strcmp(mqtt_parm.msg_sn,"")!=0){
mqtt_service_reply(stationsn,mqtt_parm.msg_messageId,"ok",1,productid);
isSendComEnd=false;
isLightOn=false;
isLightOnByRule=false;
isLabelUp=true;
isSearchLabel=false;
uart_data_send_head_lableup(&uartSend,5,1);
}else{
mqtt_service_reply(stationsn,mqtt_parm.msg_messageId,"sn is empty",0,productid);
}
}else if(strcmp(mqtt_parm.msg_type,"3023")==0){
LOG_I("3023 light on by rule\n");
get_string_from_json_string_by_key_unescape(msg_data, "labelConfig", msg_labelconfig_value, sizeof(msg_labelconfig_value));
snprintf(msg_labelconfig, sizeof(msg_labelconfig), "{%s}", msg_labelconfig_value);
LOG_I("msg_labelconfig:%s\n",msg_labelconfig);
get_string_from_json_string_by_key_unescape(msg_labelconfig, "label1", mqtt_parm.msg_label1, sizeof(mqtt_parm.msg_label1));
LOG_I("label1:%s\n",mqtt_parm.msg_label1);
get_string_from_json_string_by_key_unescape(msg_labelconfig, "label2", mqtt_parm.msg_label2, sizeof(mqtt_parm.msg_label2));
LOG_I("label2:%s\n",mqtt_parm.msg_label2);
get_string_from_json_string_by_key_unescape(msg_labelconfig, "label3", mqtt_parm.msg_label3, sizeof(mqtt_parm.msg_label3));
LOG_I("label3:%s\n",mqtt_parm.msg_label3);
get_string_from_json_string_by_key_unescape(msg_labelconfig, "label1Rule", mqtt_parm.msg_label1Rule,
sizeof(mqtt_parm.msg_label1Rule));
LOG_I("label1Rule:%s\n",mqtt_parm.msg_label1Rule);
get_string_from_json_string_by_key_unescape(msg_labelconfig, "label2Rule", mqtt_parm.msg_label2Rule,
sizeof(mqtt_parm.msg_label2Rule));
LOG_I("label2Rule:%s\n",mqtt_parm.msg_label2Rule);
get_string_from_json_string_by_key_unescape(msg_labelconfig, "label3Rule", mqtt_parm.msg_label3Rule,
sizeof(mqtt_parm.msg_label3Rule));
LOG_I("label3Rule:%s\n",mqtt_parm.msg_label3Rule);
get_string_from_json_string_by_key_unescape(msg_data, "color", mqtt_parm.msg_color, sizeof(mqtt_parm.msg_color));
LOG_I("color:%s\n",mqtt_parm.msg_color);
get_string_from_json_string_by_key_unescape(msg_data, "sound", mqtt_parm.msg_sound, sizeof(mqtt_parm.msg_sound));
LOG_I("sound:%s\n",mqtt_parm.msg_sound);
get_string_from_json_string_by_key_unescape(msg_data, "flash", mqtt_parm.msg_flash, sizeof(mqtt_parm.msg_flash));
LOG_I("flash:%s\n",mqtt_parm.msg_flash);
get_string_from_json_string_by_key_unescape(msg_data, "flashInterval", mqtt_parm.msg_flashInterval,
sizeof(mqtt_parm.msg_flashInterval));
LOG_I("flashInterval:%s\n",mqtt_parm.msg_flashInterval);
get_string_from_json_string_by_key_unescape(msg_data, "lightDuration", mqtt_parm.msg_lightDuration,
sizeof(mqtt_parm.msg_lightDuration));
LOG_I("lightDuration:%s\n",mqtt_parm.msg_lightDuration);
if(strcmp(msg_labelconfig_value,"")!=0){
mqtt_service_reply(stationsn,mqtt_parm.msg_messageId,"ok",1,productid);
isSendComEnd=false;
isLightOn=false;
isLightOnByRule=true;
isLabelUp=false;
isSearchLabel=false;
uart_data_send_head_lightonrule(&uartSend,5);
}else{
mqtt_service_reply(stationsn,mqtt_parm.msg_messageId,"labelconfig is empty",0,productid);
}
}
}
}
usleep(getPayloadTime);
}else{
usleep(getPayloadTime);
}
}
}
/*================================================================================*/
void hmacsha1(char *key,char* data,char *signbase64,int signbase64_len){
char result[256]={0};
size_t len=sizeof(result);
//LOG_I("key_len:%d,data_len:%d\n",strlen(key),strlen(data));
hmac_sha1(key,strlen(key),data,strlen(data),result,&len);
//LOG_I("result_len:%d\n",len);
int t_i;
//for(t_i=0;t_i<len;t_i++)
// printf("%x ",result[t_i]);
Base64_encode( signbase64, signbase64_len, result,strlen(result));
//printf("last:%s\n",signbase64);
}
void getLocalIp(char *local_ip){
char hostname[32]={0};
2025-04-09 04:40:51 +00:00
FILE *fp;
char buffer[64]={0};
//get_ip_by_domain(hostname, local_ip, 32);
fp=popen("ifconfig eth0 | grep 'inet' | awk '{print $2}' | cut -d':' -f2","r");
fgets(buffer,sizeof(buffer),fp);
LOG_I("buffer:%s\n",buffer);
memcpy(local_ip,buffer,strlen(buffer)-1);
2025-04-06 06:41:47 +00:00
}
void saveStartUpTime(){
system("rm ./startUpTime");
struct timeval tv;
char startUpTime[32] = {0};
gettimeofday(&tv, NULL);
snprintf(startUpTime,sizeof(startUpTime),"%ld",tv.tv_sec);
buffer_to_file("startUpTime",startUpTime,strlen(startUpTime),"wb");
}
void calculateStartUpTime(char *startTime){
char *result=NULL;
struct timeval tv;
int len=0;
result=file_to_buffer("startUpTime",&len);
if(result!=NULL){
LOG_I("startUpTime:%s\n",result);
gettimeofday(&tv, NULL);
LOG_I("nowUpTime:%ld\n",tv.tv_sec);
snprintf(startTime,11,"%ld",tv.tv_sec-atoi(result));
}
}
void updateStationInfo(char *msg_id){
char local_ip[32] = {0};
char startTime[11]={0};
getLocalIp(local_ip);
LOG_I("local_ip:%s\n",local_ip);
calculateStartUpTime(startTime);
LOG_I("Time:%s\n",startTime);
mqtt_server_station_status_report(msg_id,productid,stationsn,local_ip,softwareVersion,"1",startTime);
}
2025-04-18 13:58:27 +00:00
bool timeNew(){
struct timeval tv;
gettimeofday(&tv, NULL);
long gettime=tv.tv_sec*1000+tv.tv_usec;
LOG_I("gettime:%ld\n",gettime);
if(gettime<1744000000000){
return false;
}
return true;
}
2025-04-06 06:41:47 +00:00
//==========================================================================================
int main(int argc, char *argv[])
{
int ret = -1;
uint32_t can_id;
char recv_data[8]={0};
char command_buffer[256] = "";
int len=0;
2025-04-18 13:58:27 +00:00
int getTimeCount=0;
2025-04-06 06:41:47 +00:00
char *readresult=NULL;
char networktype[32]={0};
LOG_I("version:%s\n",softwareVersion);
system("insmod /system/lib/modules/wk2xxx_spi.ko");
2025-04-18 13:58:27 +00:00
//system("busybox udhcpc -i eth0");
2025-04-06 06:41:47 +00:00
system("timedatectl set-timezone Asia/Shanghai");
uart_open(&uartSend,"/dev/ttyS0");//U12 ttyS0,U14 ttyS4,U21 ttysWK0 U13 ttysWK1 U15 ttysWK2 U22 ttysWK3 U20 ttyS1
uart_init(&uartSend,115200,8,1,'N',0);
uart_open(&uartRecvData,"/dev/ttyS4");
uart_init(&uartRecvData,115200,8,1,'N',0);
uart_open(&uartRecvBack,"/dev/ttysWK0");
uart_init(&uartRecvBack,115200,8,1,'N',0);
//doCommand_help(0, NULL);
//sleep(5);
2025-04-18 13:58:27 +00:00
#if 1
2025-04-06 06:41:47 +00:00
enableWatchDog();
ret = pthread_create(&pt_watchdog,NULL,thread_feed_watchdog,NULL);
if(ret!=0){
LOG_I("pthread_create watchdog fail\n");
system("reboot");
}else{
LOG_I("pthread_create watchdog success\n");
pthread_detach(pt_watchdog);
}
2025-04-18 13:58:27 +00:00
#endif
2025-04-06 06:41:47 +00:00
ret = pthread_create(&pt_uart_recv_ack,NULL,thread_uart_recv_ack,NULL);
if(ret!=0){
LOG_I("pthread_create uart_recv_ack fail\n");
system("reboot");
}else{
pthread_detach(pt_uart_recv_ack);
LOG_I("pthread_create uart_recv_ack success\n");
}
ret = pthread_create(&pt_uart_recv_data,NULL,thread_uart_recv_data,NULL);
if(ret!=0){
LOG_I("pthread_create uart_recv_data fail\n");
system("reboot");
}else{
pthread_detach(pt_uart_recv_data);
LOG_I("pthread_create uart_recv_data success\n");
}
ret = pthread_create(&pt_uart_recv_back,NULL,thread_uart_recv_back,NULL);
if(ret!=0){
LOG_I("pthread_create uart_recv_back fail\n");
system("reboot");
}else{
pthread_detach(pt_uart_recv_back);
LOG_I("pthread_create uart_recv_back success\n");
}
ret = pthread_create(&pt_mqtt_recv,NULL,thread_mqtt_recv,NULL);
if(ret!=0){
LOG_I("pthread_create mqtt_recv fail\n");
system("reboot");
}else{
pthread_detach(pt_mqtt_recv);
LOG_I("pthread_create mqtt_recv success\n");
}
ret = pthread_create(&pt_tagsearch,NULL,thread_tag_search_send,NULL);
if(ret!=0){
LOG_I("pthread_create tag search send fail\n");
}else{
LOG_I("pthread_create tag search success\n");
pthread_detach(pt_tagsearch);
}
ret = pthread_create(&pt_removeduplicatetag,NULL,thread_remove_duplicate_tag,NULL);
if(ret!=0){
LOG_I("pthread_create remove duplicate tag send fail\n");
}else{
LOG_I("pthread_create remove duplicate tag success\n");
pthread_detach(pt_removeduplicatetag);
}
ret = pthread_create(&pt_removelog,NULL,thread_removelog,NULL);
if(ret!=0){
LOG_I("pthread_create removelog fail\n");
system("reboot");
}else{
LOG_I("pthread_create removelog success\n");
pthread_detach(pt_removelog);
}
ret = pthread_create(&pt_readqr,NULL,thread_readqr,NULL);
if(ret!=0){
LOG_I("pthread_create readqr fail\n");
system("reboot");
}else{
LOG_I("pthread_create readqr success\n");
pthread_detach(pt_readqr);
}
readresult=file_to_buffer("savedDevSn",&len);
while(readresult==NULL){
readresult=file_to_buffer("savedDevSn",&len);
sleep(5);
LOG_I("please scan sn\n");
}
strncpy(stationsn,readresult,len);
readresult=NULL;
LOG_I("saved stationsn:%s\n",stationsn);
readresult=file_to_buffer("mqttRawPassword",&len);
if(readresult!=NULL){
strncpy(mqttRawPassword,readresult,len);
readresult=NULL;
LOG_I("saved mqttRawPassword:%s\n",mqttRawPassword);
}else{
2025-04-09 04:40:51 +00:00
if((ping("8.8.8.8") == 0)||(ping("8.8.4.4") == 0)){
getDevRawPassword(stationsn);
}else{
LOG_I("getDevRawPassword net not ready\n");
sleep(3);
}
2025-04-06 06:41:47 +00:00
}
readresult=file_to_buffer("LightEnable",&len);
if(readresult!=NULL){
if(strcmp(readresult,"enable")==0){
isLightEnable=true;
}else{
isLightEnable=false;
}
readresult=NULL;
}
while(1){
if((ping("8.8.8.8") == 0)||(ping("8.8.4.4") == 0)){
2025-04-18 13:58:27 +00:00
LOG_I("net ok\n");
while(!timeNew()){
//LOG_I("sleep\n");
getTimeCount++;
if(getTimeCount==100){
break;
}
sleep(1);
}
saveStartUpTime();
2025-04-06 06:41:47 +00:00
ret = pthread_create(&pt_mqtt,NULL,thread_mqtt,NULL);
if(ret!=0){
LOG_I("pthread_create mqtt fail\n");
system("reboot");
}else{
pthread_detach(pt_mqtt);
LOG_I("pthread_create mqtt success\n");
}
break;
}else{
LOG_I("net not ready\n");
sleep(3);
}
}
#if 1
while(1)
{
fgets(command_buffer, sizeof(command_buffer), stdin);
if(execute_command(command_buffer, (command_t *)&__start_command, (&__stop_command - &__start_command)/2) < 0)
{
LOG_I("unsupport command!\r\n");
}
}
#endif
return 0;
}