diff --git a/debug_print/debug_print.c b/debug_print/debug_print.c index 144ae5d..178a312 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 1f3268b..4e8cb02 100644 --- a/main.c +++ b/main.c @@ -348,24 +348,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){ @@ -375,7 +435,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"); @@ -383,7 +442,7 @@ void rebootSystem(void){ } sleep(1); } - + system("sync"); system("reboot"); } @@ -1765,7 +1824,7 @@ void *thread_mqtt_recv(void *arg){ sleep(15); system("unzip ota/tx_ota.zip"); system("mv tx_ota/* ota"); - system("rm -fr tx_ota"); + system("rm -fr tx_ota"); system("mv ota/start.sh /root"); system("sync"); sleep(1); @@ -2093,19 +2152,62 @@ int main(int argc, char *argv[]) pthread_t timeout_thread; // 添加超时检测线程变量 LOG_I("version:%s\n",softwareVersion); + #if 1 system("insmod /system/lib/modules/wk2xxx_spi.ko"); - //system("busybox udhcpc -i eth0"); + + // 设置时区为 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), 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);