diff --git a/debug_print/debug_print.c b/debug_print/debug_print.c index aba3875..513af07 100644 --- a/debug_print/debug_print.c +++ b/debug_print/debug_print.c @@ -60,11 +60,13 @@ int debug_print(const char *__restrict format, ...) va_end(valist); printf("%s", buffer); + /* char opTime[16]={0}; char logFileName[32]={0}; getDayStr(opTime,16); sprintf(logFileName,"Log.%s",opTime); buffer_to_file(logFileName,buffer,strlen(buffer),"a"); + */ return ret; } #endif diff --git a/main.c b/main.c index a39f497..6f56897 100644 --- a/main.c +++ b/main.c @@ -59,7 +59,7 @@ int fd; int UPCASE=0; int count_value=0; int getPayloadTime=120*1000;//usecond -char softwareVersion[16]="1.1.12"; +char softwareVersion[16]="1.1.17"; char stationsn[16]="TJ251372224247";//TJ250995217957 char productid[8]="10045"; char appKey[32]="fdhQmhqhvbL1cf1K9mUqt"; @@ -107,6 +107,12 @@ int searchTimeNew=0; int lightsnNum=0; int mqtt_port=1883; +static time_t last_2323_time = 0; // 记录上次收到0x2323的时间 +static bool waiting_for_4646 = false; // 是否在等待0x4646 + +// 函数声明 +void *thread_timeout_check(void *arg); + int getLedOtaVersion(char *filename); int getApOtaVersion(char *filename,char *modename); void hmacsha1(char *key,char* data,char *signbase64,int signbase64_len); @@ -367,24 +373,84 @@ void otaAp(){ } } -void enableWatchDog(){ - system("echo 1 > /sys/class/gpio/export");//watchdog enable pin SGM820 - system("echo 112 > /sys/class/gpio/export");//feed watchdog pin 112 - system("echo out > /sys/class/gpio/gpio1/direction"); - system("echo out > /sys/class/gpio/gpio112/direction"); - system("echo 1 > /sys/class/gpio/gpio1/value"); +void enableWatchDog() { + int fd; + char path[64]; + char value[2]; + + // Enable watchdog pin SGM820 + fd = open("/sys/class/gpio/export", O_WRONLY); + if (fd < 0) { + LOG_I("Failed to open gpio export: %s\n", strerror(errno)); + return; + } + write(fd, "1", 1); + close(fd); + + // Enable feed watchdog pin + fd = open("/sys/class/gpio/export", O_WRONLY); + if (fd < 0) { + LOG_I("Failed to open gpio export: %s\n", strerror(errno)); + return; + } + write(fd, "112", 3); + close(fd); + + // Set gpio1 direction + fd = open("/sys/class/gpio/gpio1/direction", O_WRONLY); + if (fd < 0) { + LOG_I("Failed to open gpio1 direction: %s\n", strerror(errno)); + return; + } + write(fd, "out", 3); + close(fd); + + // Set gpio112 direction + fd = open("/sys/class/gpio/gpio112/direction", O_WRONLY); + if (fd < 0) { + LOG_I("Failed to open gpio112 direction: %s\n", strerror(errno)); + return; + } + write(fd, "out", 3); + close(fd); + + // Set gpio1 value + fd = open("/sys/class/gpio/gpio1/value", O_WRONLY); + if (fd < 0) { + LOG_I("Failed to open gpio1 value: %s\n", strerror(errno)); + return; + } + write(fd, "1", 1); + close(fd); + LOG_I("enable watchdog\n"); - system("echo 1 > /sys/class/gpio/gpio112/value"); + + // Set gpio112 value + fd = open("/sys/class/gpio/gpio112/value", O_WRONLY); + if (fd < 0) { + LOG_I("Failed to open gpio112 value: %s\n", strerror(errno)); + return; + } + write(fd, "1", 1); + close(fd); } -void feedWatchDog(){ - while(1){ - //LOG_I("feed watchdog\n"); - system("echo 0 > /sys/class/gpio/gpio112/value"); +void feedWatchDog() { + int fd = open("/sys/class/gpio/gpio112/value", O_WRONLY); + if (fd < 0) { + LOG_I("can not open watchdog gpio112\n"); + return; + } + + while(1) { + write(fd, "0", 1); usleep(100*1000); - system("echo 1 > /sys/class/gpio/gpio112/value"); + write(fd, "1", 1); sleep(1); } + + close(fd); + return; } void rebootSystem(void){ @@ -394,7 +460,6 @@ void rebootSystem(void){ 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){ isTimeToReboot=true; LOG_I("prepare to reboot\n"); @@ -402,7 +467,7 @@ void rebootSystem(void){ } sleep(1); } - + system("sync"); system("reboot"); } @@ -655,7 +720,6 @@ int readQrcode() 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); } @@ -1305,11 +1369,15 @@ void *thread_uart_recv_ack(void *arg){ prctl(PR_SET_NAME, "uart_ack"); 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){ + // 记录收到0x2323的时间,并设置等待0x4646标志 + time(&last_2323_time); + waiting_for_4646 = true; if(isLightOn){ char *flash=mqtt_parm.msg_flash; char *light=mqtt_parm.msg_lightDuration; @@ -1464,8 +1532,12 @@ void *thread_uart_recv_ack(void *arg){ isLabelUp=false; } }else if(parm_ack==0x4646){ + last_2323_time = 0; // 重置时间 + waiting_for_4646 = false; // 重置等待状态 isSendComEnd=true; }else if(parm_ack==0x4f4f){ + last_2323_time = 0; // 重置时间 + waiting_for_4646 = false; // 重置等待状态 isSendComEnd=true; if(isOtaEnable){ otaCount++; @@ -1477,12 +1549,18 @@ void *thread_uart_recv_ack(void *arg){ } } }else if(parm_ack==0x5252){ + last_2323_time = 0; // 重置时间 + waiting_for_4646 = false; // 重置等待状态 uart_data_receive_version(&uartSend); isSendComEnd=true; isAPOtaSuccess=true; }else if(parm_ack==0x5454){ + last_2323_time = 0; // 重置时间 + waiting_for_4646 = false; // 重置等待状态 isSendComEnd=true; }else if(parm_ack==0x4848){ + last_2323_time = 0; // 重置时间 + waiting_for_4646 = false; // 重置等待状态 isAPBack=true; } } @@ -1781,8 +1859,7 @@ void *thread_mqtt_recv(void *arg){ system("reboot"); }else if(strcmp(mqtt_parm.msg_type,"1014")==0){ LOG_I("1014 update fw\n"); - system("rm -fr ota"); - system("mkdir ota"); + isSendComEnd=false; 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)); @@ -1797,14 +1874,16 @@ void *thread_mqtt_recv(void *arg){ 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 /root/ota/tx_ota.zip %s",mqtt_parm.msg_zipPath); + sprintf(otaCmd,"curl -o /userdata/tx_ota.zip %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(15); - system("unzip ota/tx_ota.zip"); - system("mv tx_ota/* ota"); - system("rm -fr tx_ota"); - system("mv ota/start.sh /root"); + system("unzip /userdata/tx_ota.zip"); + //system("mv ../tx_ota/* /userdata/ota"); + //system("rm -fr /userdata/ota/tx_ota"); + system("rm -fr /userdata/ota"); + system("mv /userdata/tx_ota /userdata/ota"); + system("mv /userdata/ota/ustart.sh /userdata"); system("sync"); sleep(1); system("reboot"); @@ -2255,21 +2334,68 @@ int main(int argc, char *argv[]) int getTimeCount=0; char *readresult=NULL; char networktype[32]={0}; + pthread_t timeout_thread; // 添加超时检测线程变量 + char syscmd[256]={0}; LOG_I("version:%s\n",softwareVersion); + #if 1 system("insmod /system/lib/modules/wk2xxx_spi.ko"); - //system("busybox udhcpc -i eth0"); + snprintf(syscmd, sizeof(syscmd), "ls Log.*|xargs rm -fr"); + system(syscmd); + + // 设置时区为 Asia/Shanghai,直接修改 /etc/localtime 软链接 + unlink("/etc/localtime"); + symlink("/usr/share/zoneinfo/Asia/Shanghai", "/etc/localtime"); + + // 导出 GPIO 63 + { + int fd = open("/sys/class/gpio/export", O_WRONLY); + if (fd >= 0) { + write(fd, "63", 2); + close(fd); + } + } + // 设置 GPIO 63 方向为 in + { + int fd = open("/sys/class/gpio/gpio63/direction", O_WRONLY); + if (fd >= 0) { + write(fd, "in", 2); + close(fd); + } + } + // 导出 GPIO 113 + { + int fd = open("/sys/class/gpio/export", O_WRONLY); + if (fd >= 0) { + write(fd, "113", 3); + close(fd); + } + } + // 设置 GPIO 113 方向为 out + { + int fd = open("/sys/class/gpio/gpio113/direction", O_WRONLY); + if (fd >= 0) { + write(fd, "out", 3); + close(fd); + } + } + #endif + #if 0 + system("insmod /system/lib/modules/wk2xxx_spi.ko"); + system("busybox udhcpc -i eth0"); system("timedatectl set-timezone Asia/Shanghai"); system("echo 63 > /sys/class/gpio/export");//reset key system("echo in > /sys/class/gpio/gpio63/direction"); system("echo 113 > /sys/class/gpio/export");//pin 113 yellow system("echo out > /sys/class/gpio/gpio113/direction"); + #endif uart_open(&uartSend,"/dev/ttyS0");//GT 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");//DT uart_init(&uartRecvData,115200,8,1,'N',0); uart_open(&uartRecvBack,"/dev/ttysWK0");//HT uart_init(&uartRecvBack,115200,8,1,'N',0); + #if 0 struct dma_config cfg = { .buf = malloc(256), @@ -2286,25 +2412,25 @@ int main(int argc, char *argv[]) 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); } #endif + +#if 0 ret = pthread_create(&pt_reboot,NULL,thread_reboot,NULL); if(ret!=0){ LOG_I("pthread_create reboot fail\n"); - system("reboot"); }else{ LOG_I("pthread_create reboot success\n"); pthread_detach(pt_reboot); } +#endif 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"); @@ -2313,7 +2439,6 @@ int main(int argc, char *argv[]) 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"); @@ -2322,7 +2447,6 @@ int main(int argc, char *argv[]) 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"); @@ -2331,7 +2455,6 @@ int main(int argc, char *argv[]) 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"); @@ -2340,7 +2463,6 @@ int main(int argc, char *argv[]) ret = pthread_create(&pt_tagsearch,NULL,thread_tag_search_send,NULL); if(ret!=0){ LOG_I("pthread_create tag search send fail\n"); - system("reboot"); }else{ LOG_I("pthread_create tag search success\n"); pthread_detach(pt_tagsearch); @@ -2349,12 +2471,12 @@ int main(int argc, char *argv[]) ret = pthread_create(&pt_removeduplicatetag,NULL,thread_remove_duplicate_tag,NULL); if(ret!=0){ LOG_I("pthread_create remove duplicate tag send fail\n"); - system("reboot"); }else{ LOG_I("pthread_create remove duplicate tag success\n"); pthread_detach(pt_removeduplicatetag); } +#if 0 ret = pthread_create(&pt_removelog,NULL,thread_removelog,NULL); if(ret!=0){ LOG_I("pthread_create removelog fail\n"); @@ -2363,11 +2485,20 @@ int main(int argc, char *argv[]) LOG_I("pthread_create removelog success\n"); pthread_detach(pt_removelog); } +#endif + + // 创建超时检测线程 + ret = pthread_create(&timeout_thread, NULL, thread_timeout_check, NULL); + if(ret!=0){ + LOG_I("pthread_create timeout_check fail\n"); + }else{ + LOG_I("pthread_create timeout_check success\n"); + pthread_detach(timeout_thread); + } ret = pthread_create(&pt_ota,NULL,thread_ota,NULL); if(ret!=0){ LOG_I("pthread_create ota fail\n"); - system("reboot"); }else{ LOG_I("pthread_create ota success\n"); pthread_detach(pt_ota); @@ -2376,7 +2507,6 @@ int main(int argc, char *argv[]) ret = pthread_create(&pt_doota,NULL,thread_do_ota,NULL); if(ret!=0){ LOG_I("pthread_create doota fail\n"); - system("reboot"); }else{ LOG_I("pthread_create doota success\n"); pthread_detach(pt_doota); @@ -2439,7 +2569,6 @@ int main(int argc, char *argv[]) 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"); @@ -2461,6 +2590,31 @@ int main(int argc, char *argv[]) } } #endif + return 0; } +// 超时检测线程 +void *thread_timeout_check(void *arg) { + prctl(PR_SET_NAME, "timeout_check"); + + while(1) { + if(last_2323_time != 0 && waiting_for_4646) { // 只有在等待0x4646时才检查超时 + time_t current_time; + time(¤t_time); + if(difftime(current_time, last_2323_time) >= 10.0) { // 超过10秒 + LOG_I("thread_timeout_check: Timeout after receiving 0x2323, no 0x4646 received, setting isSendComEnd to 1\n"); + isSendComEnd = 1; + last_2323_time = 0; // 重置时间 + waiting_for_4646 = false; // 重置等待状态 + isLightEnable = 1; // 重置点亮使能标志 + isLightOn = false; // 重置点亮状态 + isLightOnByRule = false; // 重置规则点亮状态 + isLabelUp = false; // 重置标签更新状态 + } + } + usleep(100*1000); // 每100ms检查一次 + } + return NULL; +} + diff --git a/mqtt_utils/mqtt_utils.c b/mqtt_utils/mqtt_utils.c index 9075837..12d5b44 100644 --- a/mqtt_utils/mqtt_utils.c +++ b/mqtt_utils/mqtt_utils.c @@ -192,7 +192,13 @@ json_error: } void mqtt_net_failure(int *failure_times){ - system("echo 0 > /sys/class/gpio/gpio113/value"); + int fd = open("/sys/class/gpio/gpio113/value", O_WRONLY); + if (fd < 0) { + LOG_I("Failed to open gpio113 value: %s\n", strerror(errno)); + } else { + write(fd, "0", 1); + close(fd); + } if(failure_times != NULL){ (*failure_times)++; LOG_I("mqtt net failure_times = %d\n", *failure_times);