#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 char softwareVersion[16]="1.1.9"; 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{ input_value[count_value]='T'; } 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;i0)){ 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