修复程序coredump,以及串口接收不到数据的问题,取消不必要的代码

This commit is contained in:
zzh 2026-01-08 13:47:55 +08:00
parent 89ad85c7cb
commit 8146508cb4
3 changed files with 145 additions and 129 deletions

206
main.c
View File

@ -20,7 +20,7 @@ jt_led_or_group_package_t tags;
jt_led_or_group_package_t led_ctrl;
// 灯光命令队列结构体 - 解决竞态条件问题
#define MAX_LIGHT_COMMANDS 32
#define MAX_LIGHT_COMMANDS 64
typedef struct {
char deviceName[16]; // 设备名称12位十六进制
@ -157,7 +157,7 @@ int fd;
int UPCASE=0;
int count_value=0;
int getPayloadTime=100*1000;//usecond
char softwareVersion[16]="2.1.27";
char softwareVersion[16]="2.1.28";
//char stationsn[16]="d126ei4lj4cc00";//TJ250995217957
//char productid[8]="10045";
//char appSecret[64]="s3izIliw0CF48Pcsi16rjOmoFRf5WEt8";
@ -650,8 +650,15 @@ void report_lightbar_logout(uint16_t tagCodeHead, uint32_t tagCode) {
LOG_I("Lightbar logout report - topic: %s\n", topic);
LOG_I("Lightbar logout report - payload: %s\n", payload);
// 发送MQTT消息
mqtt_utils_publish(&mqtt_config, topic, 0, payload, strlen(payload));
// 【修复】检查MQTT连接状态避免在断开连接时发送导致内存累积
if (isMqttConnected) {
int result = mqtt_utils_publish(&mqtt_config, topic, 0, payload, strlen(payload));
if (result != MQTTASYNC_SUCCESS) {
LOG_I("Failed to publish logout message: %d\n", result);
}
} else {
LOG_I("MQTT not connected, skipping logout report\n");
}
}
/*================================================================================*/
@ -692,7 +699,7 @@ void update_lightbar_heartbeat(uint16_t tagCodeHead, uint32_t tagCode) {
lightbarHeartbeat[lightbarHeartbeatCount].lastHeartbeat = now;
lightbarHeartbeat[lightbarHeartbeatCount].isOnline = true;
lightbarHeartbeatCount++;
//LOG_I("New lightbar %04X%08X registered, total: %d\n", tagCodeHead, tagCode, lightbarHeartbeatCount);
LOG_I("New lightbar %04X%08X registered, total: %d\n", tagCodeHead, tagCode, lightbarHeartbeatCount);
// 只有MQTT连接后才上报登录
if (isMqttConnected) {
report_lightbar_login(tagCodeHead, tagCode);
@ -1049,8 +1056,12 @@ void removeLog(){
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(savedtime != NULL) {
saved=atol(savedtime);
LOG_I("now=%ld,saved=%ld,nowtime=%s,savedtime=%s\n",now,saved,nowtime,savedtime);
free(savedtime);
savedtime = NULL;
}
if(now-saved>=(60*60*24*5)){
//if(now-saved>=10){
buffer_to_file("logTime",nowtime,strlen(nowtime),"wb");
@ -1426,7 +1437,7 @@ int readQrcode()
ret = pthread_create(&pt_handleqrcode,NULL,actHandleQrcode,input_value_copy);
if(ret!=0){
LOG_I("pthread_create handleqrcode fail\n");
system("reboot");
//system("reboot");
}else{
pthread_detach(pt_handleqrcode);
}
@ -1643,52 +1654,6 @@ int readQrcode()
error:
return 1;
}
#if 0
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);
}
#endif
void hmacsha1_hex(char *key, char* data, char *signhex, int signhex_len) {
unsigned char result[256] = {0};
@ -1968,6 +1933,8 @@ void checkOtaKey(void){
update_app();
}
}
free(readresult);
readresult = NULL;
}
usleep(100*1000);
}
@ -1986,7 +1953,7 @@ void update_app(void){
system("rm -fr jd_ota");
system("mv ota/start.sh /root");
sleep(1);
system("reboot");
//system("reboot");
}
/*================================================================================*/
void mqtt_init(){
@ -2411,10 +2378,18 @@ void *thread_uart_recv_data(void *arg){
uint16_t reserve;
uint32_t lableParm;
while(1){
uart_data_receive_data_back(&uartRecvData,&parmAck,&tagCodeHead,&tagCode,&tagSignal,&totalLen,&tagFeature,
int ret = uart_data_receive_data_back(&uartRecvData,&parmAck,&tagCodeHead,&tagCode,&tagSignal,&totalLen,&tagFeature,
&count,&batteryV,&version,&ledCtrl,&signCode,&reserve,&lableParm);
LOG_I("recv_data:%04x,%04x,tag:%08x,%02x,%02x,%02x,%02x,battery:%02x,%04x,%02x,%04x,reserve:%04x,%08x\n",
parmAck,tagCodeHead,tagCode,tagSignal,totalLen,tagFeature,count,batteryV,version,ledCtrl,signCode,reserve,lableParm);
if(ret < 0) {
usleep(100000); // 失败时延迟100ms
continue;
}
// 只在有效数据时才打印tag不为0
if(tagCode != 0) {
LOG_I("recv_data:%04x,%04x,tag:%08x,%02x,%02x,%02x,%02x,battery:%02x,%04x,%02x,%04x,reserve:%04x,%08x\n",
parmAck,tagCodeHead,tagCode,tagSignal,totalLen,tagFeature,count,batteryV,version,ledCtrl,signCode,reserve,lableParm);
}
// 检查是否有待确认的亮灯任务如果tag匹配则上报亮灯成功
report_light_success(tagCodeHead, tagCode);
@ -2438,17 +2413,28 @@ void *thread_uart_recv_back(void *arg){
uint16_t signCode;
uint16_t reserve;
uint32_t lableParm;
//LOG_I("thread_uart_recv_back\n");
while(1){
uart_data_receive_data_back(&uartRecvBack,&parmAck,&tagCodeHead,&tagCode,&tagSignal,&totalLen,&tagFeature,
//LOG_I("Before uart_data_receive_data_back call\n");
int ret = uart_data_receive_data_back(&uartRecvBack,&parmAck,&tagCodeHead,&tagCode,&tagSignal,&totalLen,&tagFeature,
&count,&batteryV,&version,&ledCtrl,&signCode,&reserve,&lableParm);
if(ret < 0) {
//LOG_I("uart_data_receive_data_back failed with ret=%d, sleeping 100ms\n", ret);
usleep(100000); // 失败时延迟100ms
continue;
}
//LOG_I("After uart_data_receive_data_back call, ret: %d\n", parmAck);
// 打印除心跳外的所有接收数据
if(tagFeature != 0xFF) {
// 只在有效数据且不是心跳时才打印
if(tagCode != 0 && tagFeature != 0xFF) {
LOG_I("recv_back:%04x,%04x,tag:%08x,%02x,%02x,%02x,%02x,battery:%02x,%04x,%02x,%04x,reserve:%04x,%08x\n",
parmAck,tagCodeHead,tagCode,tagSignal,totalLen,tagFeature,count,batteryV,version,ledCtrl,signCode,reserve,lableParm);
}
PutDataIntoQueue(tagCode,batteryV,reserve);
//LOG_I("About to call PutDataIntoQueue\n");
//PutDataIntoQueue(tagCode,batteryV,reserve);
//LOG_I("PutDataIntoQueue returned\n");
if(tagFeature==0xFF){
//LOG_I("heart beat from lightbar %06X\n", tagCode);
update_lightbar_heartbeat(tagCodeHead, tagCode);
@ -2807,8 +2793,7 @@ void *thread_mqtt_recv(void *arg){
LOG_I("Using device from queue: %s\n", g_mqtt_deviceName);
}
} else if(GetDataFromMQueue(payload)==0) {
// 回退到原来的版本(兼容性)
LOG_I("Using legacy queue without device name\n");
//LOG_I("Using legacy queue without device name\n");
} else {
Sleep(100);
continue;
@ -2842,7 +2827,7 @@ void *thread_mqtt_recv(void *arg){
// 解析params中的各个字段
char params_str[1024] = {0};
char params_json[1024] = {0}; // 【修复】移到外面,扩大作用域
char params_json[1024] = {0}; // 【修复】移到外面
char color_json[512] = {0}; // 提前声明color_json
int params_ret = get_string_from_json_string_by_key(payload, "params", params_str, sizeof(params_str));
LOG_I("get_string_from_json_string_by_key return: %d\n", params_ret);
@ -3212,9 +3197,6 @@ void *thread_mqtt_recv(void *arg){
isBindTag = false;
}
}
// 发送回复消息(如果需要)
// functions_reply(mqtt_parm.msg_taskId, mqtt_parm.msg_messageId);
}
}
usleep(getPayloadTime);
@ -3239,17 +3221,43 @@ void getLocalIp(char *local_ip){
FILE *fp;
char buffer[64]={0};
fp=popen("ifconfig eth0 | grep 'inet' | awk '{print $2}' | cut -d':' -f2","r");
fgets(buffer,sizeof(buffer),fp);
if(fp == NULL) {
LOG_I("getLocalIp: popen failed\n");
strcpy(local_ip, "127.0.0.1");
return;
}
if(fgets(buffer,sizeof(buffer),fp) == NULL) {
LOG_I("getLocalIp: fgets failed\n");
strcpy(local_ip, "127.0.0.1");
pclose(fp);
return;
}
pclose(fp);
//LOG_I("buffer:%s\n",buffer);
memcpy(local_ip,buffer,strlen(buffer)-1);
// 去掉换行符
buffer[strcspn(buffer, "\n")] = 0;
strcpy(local_ip, buffer);
}
void getLocalMac(char *local_mac){
FILE *fp;
char buffer[64]={0};
fp=popen("ifconfig eth0 | grep 'ether' | awk '{print $2}' | tr -d ':'","r");
fgets(buffer,sizeof(buffer),fp);
if(fp == NULL) {
LOG_I("getLocalMac: popen failed\n");
strcpy(local_mac, "00:00:00:00:00:00");
return;
}
if(fgets(buffer,sizeof(buffer),fp) == NULL) {
LOG_I("getLocalMac: fgets failed\n");
strcpy(local_mac, "00:00:00:00:00:00");
pclose(fp);
return;
}
pclose(fp);
//LOG_I("buffer:%s\n",buffer);
memcpy(local_mac,buffer,strlen(buffer)-1);
// 去掉换行符
buffer[strcspn(buffer, "\n")] = 0;
strcpy(local_mac, buffer);
}
void saveStartUpTime(){
@ -3271,6 +3279,8 @@ void calculateStartUpTime(char *startTime){
gettimeofday(&tv, NULL);
LOG_I("nowUpTime:%ld\n",tv.tv_sec);
snprintf(startTime,11,"%ld",tv.tv_sec-atoi(result));
free(result);
result = NULL;
}
}
@ -3329,17 +3339,17 @@ int main(int argc, char *argv[])
uart_open(&uartRecvBack,"/dev/ttysWK0");
uart_init(&uartRecvBack,115200,8,1,'N',0);
//doCommand_help(0, NULL);
system("echo 113 > /sys/class/gpio/export");//pin 113 yellow
system("echo out > /sys/class/gpio/gpio113/direction");
system("echo 63 > /sys/class/gpio/export");//pin 63 key
system("echo in > /sys/class/gpio/gpio63/direction");
#if 1
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);
@ -3348,7 +3358,6 @@ int main(int argc, char *argv[])
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");
@ -3357,7 +3366,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");
@ -3366,7 +3374,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");
@ -3375,12 +3382,12 @@ 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");
}
#if 0
ret = pthread_create(&pt_removeduplicatetag,NULL,thread_remove_duplicate_tag,NULL);
if(ret!=0){
LOG_I("pthread_create remove duplicate tag send fail\n");
@ -3397,7 +3404,7 @@ int main(int argc, char *argv[])
LOG_I("pthread_create removelog success\n");
pthread_detach(pt_removelog);
}
#if 0
ret = pthread_create(&pt_readqr,NULL,thread_readqr,NULL);
if(ret!=0){
LOG_I("pthread_create readqr fail\n");
@ -3406,7 +3413,7 @@ int main(int argc, char *argv[])
LOG_I("pthread_create readqr success\n");
pthread_detach(pt_readqr);
}
#endif
ret = pthread_create(&pt_reporttag,NULL,thread_reporttag,NULL);
if(ret!=0){
LOG_I("pthread_create reporttag fail\n");
@ -3428,12 +3435,14 @@ int main(int argc, char *argv[])
ret = pthread_create(&pt_station_heartbeat,NULL,thread_station_heartbeat,NULL);
if(ret!=0){
LOG_I("pthread_create station_heartbeat fail\n");
system("reboot");
//system("reboot");
}else{
LOG_I("pthread_create station_heartbeat success\n");
pthread_detach(pt_station_heartbeat);
}
#endif
#if 1
ret = pthread_create(&pt_heartbeat_check,NULL,thread_heartbeat_check,NULL);
if(ret!=0){
LOG_I("pthread_create heartbeat_check fail\n");
@ -3441,6 +3450,7 @@ int main(int argc, char *argv[])
LOG_I("pthread_create heartbeat_check success\n");
pthread_detach(pt_heartbeat_check);
}
#endif
#if 0
ret = pthread_create(&pt_simulate_light,NULL,thread_simulate_light,NULL);
@ -3458,9 +3468,7 @@ int main(int argc, char *argv[])
LOG_I("pthread_create all_light success\n");
pthread_detach(pt_all_light);
}
#endif
#if 0
readresult=file_to_buffer("mqttRawPassword",&len);
if(readresult!=NULL){
strncpy(mqttRawPassword,readresult,len);
@ -3504,7 +3512,8 @@ int main(int argc, char *argv[])
}
strncpy(stationsn,readresult,len);
trim_whitespace(stationsn); // 清理空白字符
readresult=NULL;
free(readresult);
readresult = NULL;
LOG_I("saved stationsn:%s\n",stationsn);
// 读取productid配置
@ -3513,7 +3522,8 @@ int main(int argc, char *argv[])
memset(productid, 0, sizeof(productid));
strncpy(productid,readresult,len);
trim_whitespace(productid); // 清理空白字符
readresult=NULL;
free(readresult);
readresult = NULL;
LOG_I("saved productid:%s\n",productid);
} else {
LOG_I("use default productid:%s\n",productid);
@ -3525,7 +3535,8 @@ int main(int argc, char *argv[])
memset(appSecret, 0, sizeof(appSecret));
strncpy(appSecret,readresult,len);
trim_whitespace(appSecret); // 清理空白字符
readresult=NULL;
free(readresult);
readresult = NULL;
LOG_I("saved appSecret:%s\n",appSecret);
} else {
LOG_I("use default appSecret:%s\n",appSecret);
@ -3537,34 +3548,18 @@ int main(int argc, char *argv[])
memset(hostDomain, 0, sizeof(hostDomain));
strncpy(hostDomain,readresult,len);
trim_whitespace(hostDomain); // 清理空白字符
readresult=NULL;
free(readresult);
readresult = NULL;
LOG_I("saved hostDomain:%s\n",hostDomain);
} else {
LOG_I("use default hostDomain:%s\n",hostDomain);
}
#if 0
isSendComEnd=false;
isLightOnByGroup=false;
isLightOnById=false;
isLightOn=true;
isBindTag=false;
lightbars_size=5;
changecolor=4;
changesound=1;
groupno=1;
//uart_data_send_head(&uartSend,'B',5,0,lightbars_size);//bind groupno
uart_data_send_head(&uartSend,'P',5,10/5,lightbars_size);//lighton by tag
//uart_data_send_head(&uartSend,'*',5,5/5,0);//lighton by id
//uart_data_send_head(&uartSend,'&',5,5/5,1);//lighton by groupno
//uart_data_send_head(&uartSend,'R',5,30/5,1);//stop broadcast by groupno
//isStopBroadcast=true;
#endif
#if 1
while(1){
if((ping("8.8.8.8") == 0)||(ping("8.8.4.4") == 0)){
LOG_I("net ok\n");
while(!timeNew()){
//LOG_I("sleep\n");
getTimeCount++;
if(getTimeCount==100){
break;
@ -3575,7 +3570,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");

View File

@ -3421,13 +3421,23 @@ int uart_data_receive_version(uart_utils_t *uart){
int uart_data_receive_data_back(uart_utils_t *uart,uint16_t *parmAck,uint16_t *tagCodeHead,uint32_t *tagCode,uint8_t *tagSignal,
uint8_t *totalLen,uint8_t *tagFeature,uint8_t *count,uint8_t *batteryV,uint16_t *version,uint8_t *ledCtrl,
uint16_t *signCode,uint16_t *reserve,uint32_t *lableParm){
//LOG_I("ENTER: uart_data_receive_data_back\n");
int ret = 0;
jt_data_back_package_t jt_data_back_package;
uint8_t *buf = (uint8_t *)&jt_data_back_package;
int pkg_size = sizeof(jt_data_back_package_t);
//LOG_I("DEBUG: uart_data_receive_data_back starting, uart_fd=%d, buffer size: %d bytes\n", uart->uart_fd, pkg_size);
ret = uart_read_until_time(uart->uart_fd,(char *)buf, pkg_size, 1000, 50);
//LOG_I("DEBUG: uart_read_until_time returned %d (expected %d)\n", ret, pkg_size);
if(ret != pkg_size){
if(ret == 0) {
// 超时,不打印错误日志
return -1;
}
//LOG_I("uart_data_receive_data_back failed - ret=%d, expected=%d\n", ret, pkg_size);
return -1;
}

View File

@ -2,6 +2,7 @@
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <errno.h>
#include "read_utils.h"
#define PRINT_TIME_TAG
#define DBG_TAG "read_utils"
@ -20,15 +21,20 @@ int read_data_until_time(int fd, char *buffer, int len, int timeout_first, int t
int i;
memset(buffer, 0, len);
tv.tv_sec = 0;
tv.tv_usec = timeout_first*1000;
for(i=0; i<len; i++){
FD_ZERO(&fds);
FD_SET(fd, &fds);
//ret = select(FD_SETSIZE, &fds, NULL, NULL, &tv);
ret = select(FD_SETSIZE, &fds, NULL, NULL, 0);
// 每次都要重新设置超时时间,因为 select 会修改 tv
tv.tv_sec = 0;
tv.tv_usec = (i == 0) ? timeout_first*1000 : timeout_interval*1000;
// 确保超时时间正确设置
if(tv.tv_usec >= 1000000) {
tv.tv_sec = tv.tv_usec / 1000000;
tv.tv_usec = tv.tv_usec % 1000000;
}
ret = select(fd + 1, &fds, NULL, NULL, &tv);
if(ret < 0){
LOG_I("%s:seclect error\n",__func__);
LOG_I("%s:select error, errno=%d (%s), fd=%d\n",__func__, errno, strerror(errno), fd);
i = -1;
break;
}else if(ret > 0){
@ -45,8 +51,6 @@ int read_data_until_time(int fd, char *buffer, int len, int timeout_first, int t
buffer[i] = c;
}
}
tv.tv_sec = 0;
tv.tv_usec = timeout_interval*1000;
}else{
//LOG_I("read time out\n");
break;
@ -68,14 +72,19 @@ int read_data_until_char(int fd, char *buffer, int len, char until, int timeout_
memset(buffer, 0, len);
for(i=0; i<len; i++){
tv.tv_sec = 0;
tv.tv_usec = timeout_first*1000;
FD_ZERO(&fds);
FD_SET(fd, &fds);
//ret = select(FD_SETSIZE, &fds, NULL, NULL, &tv);
ret = select(FD_SETSIZE, &fds, NULL, NULL, 0);
// 每次都要重新设置超时时间,因为 select 会修改 tv
tv.tv_sec = 0;
tv.tv_usec = (i == 0) ? timeout_first*1000 : timeout_interval*1000;
// 确保超时时间正确设置
if(tv.tv_usec >= 1000000) {
tv.tv_sec = tv.tv_usec / 1000000;
tv.tv_usec = tv.tv_usec % 1000000;
}
ret = select(fd + 1, &fds, NULL, NULL, &tv);
if(ret < 0){
LOG_I("%s:seclect error\n",__func__);
LOG_I("%s:select error, errno=%d (%s), fd=%d\n",__func__, errno, strerror(errno), fd);
i = -1;
break;
}else if(ret > 0){
@ -93,15 +102,13 @@ int read_data_until_char(int fd, char *buffer, int len, char until, int timeout_
LOG_D("i= %d\n", i);
LOG_D("\t,[%#x], <%c>\n", c, c);
#endif
tv.tv_sec = 0;
tv.tv_usec = timeout_interval*1000;
if(c == until){
i++;
break;
}
}
}
}else{
if(c == until){
i++;
break;
}
//LOG_I("read time out\n");
}
}
@ -121,14 +128,19 @@ int read_data_until_str(int fd, char *buffer, int len, char *until, int until_le
memset(buffer, 0, len);
for(i=0; i<len; i++){
tv.tv_sec = 0;
tv.tv_usec = timeout_first*1000;
FD_ZERO(&fds);
FD_SET(fd, &fds);
//ret = select(FD_SETSIZE, &fds, NULL, NULL, &tv);
ret = select(FD_SETSIZE, &fds, NULL, NULL, 0);
// 每次都要重新设置超时时间,因为 select 会修改 tv
tv.tv_sec = 0;
tv.tv_usec = (i == 0) ? timeout_first*1000 : timeout_interval*1000;
// 确保超时时间正确设置
if(tv.tv_usec >= 1000000) {
tv.tv_sec = tv.tv_usec / 1000000;
tv.tv_usec = tv.tv_usec % 1000000;
}
ret = select(fd + 1, &fds, NULL, NULL, &tv);
if(ret < 0){
LOG_I("%s:seclect error\n",__func__);
LOG_I("%s:select error, errno=%d (%s), fd=%d\n",__func__, errno, strerror(errno), fd);
i = -1;
break;
}else if(ret > 0){