ppp拨号修复死机问题,打印更多信息 2232/1
author孙春阳 <snickersun@163.com>
Fri, 12 Sep 2025 01:04:15 +0000 (09:04 +0800)
committer孙春阳 <snickersun@163.com>
Fri, 12 Sep 2025 01:04:15 +0000 (09:04 +0800)
code/sdk/zhihui/ppp/src/ppp_api.c
code/sdk/zhihui/ppp/src/ppp_at_handler.c

index 9fe135d..1603f49 100644 (file)
@@ -67,9 +67,12 @@ errcode_t ppp_start_dial(void)
 
     // 1. 启动4G电源(通过兼容接口)
     cat1_api_enable_cat1(true);
+    PPP_DEBUG_PRINT("4G module power enabled\n");
 
-    // 2. 等待4G模块启动
-    osal_msleep(2000);
+    // 2. 等待4G模块启动(增加等待时间)
+    PPP_DEBUG_PRINT("Waiting for 4G module startup...\n");
+    osal_msleep(5000);  // 增加到5秒
+    PPP_DEBUG_PRINT("4G module startup wait completed\n");
 
     // 3. 初始化串口(AT命令模式)
     ppp_serial_config_t serial_config = {
index fe7fb33..12e4324 100644 (file)
@@ -20,7 +20,7 @@ static struct {
     bool auto_baudrate_mode;
     ppp_at_response_t last_response;
     osal_timer at_timer;
-    osal_mutex *at_mutex;
+    osal_mutex at_mutex;  // 改为结构体,不是指针
 } g_at_ctx = {0};
 
 /* 支持的波特率列表 */
@@ -83,10 +83,7 @@ void ppp_at_handler_deinit(void)
         g_at_ctx.at_timer.handler = NULL;
     }
 
-    if (g_at_ctx.at_mutex) {
-        osal_mutex_destroy(g_at_ctx.at_mutex);
-        g_at_ctx.at_mutex = NULL;
-    }
+    osal_mutex_destroy(&g_at_ctx.at_mutex);
 
     memset_s(&g_at_ctx, sizeof(g_at_ctx), 0, sizeof(g_at_ctx));
     PPP_DEBUG_INFO("AT handler deinitialized");
@@ -102,12 +99,24 @@ ppp_error_t ppp_at_baudrate_handshake(void)
     }
 
     PPP_DEBUG_INFO("Starting baudrate handshake");
+    
+    /* 先发送一些字符清空缓冲区 */
+    PPP_DEBUG_DEBUG("Clearing UART buffers...");
+    const char *clear_cmd = "\r\n\r\n";
+    uapi_uart_write(g_at_ctx.uart_bus, (const uint8_t *)clear_cmd, strlen(clear_cmd), 0);
+    osal_msleep(200);
+    
+    /* 清空接收缓冲区 */
+    uint8_t dummy_buffer[256];
+    while (uapi_uart_read(g_at_ctx.uart_bus, dummy_buffer, sizeof(dummy_buffer), 100) > 0) {
+        /* 继续清空 */
+    }
 
     /* 尝试自适应波特率列表 */
     for (int i = 0; i < PPP_BAUDRATE_AUTO_COUNT; i++) {
         uint32_t baudrate = g_auto_baudrates[i];
         
-        PPP_DEBUG_DEBUG("Trying baudrate: %u", baudrate);
+        PPP_DEBUG_INFO("=== Trying Baudrate %u (attempt %d/%d) ===", baudrate, i + 1, PPP_BAUDRATE_AUTO_COUNT);
         
         /* 配置串口波特率 */
         uart_attr_t uart_attr = {
@@ -121,17 +130,37 @@ ppp_error_t ppp_at_baudrate_handshake(void)
             PPP_DEBUG_WARN("Failed to set baudrate %u", baudrate);
             continue;
         }
+        
+        PPP_DEBUG_INFO("UART configured for baudrate %u", baudrate);
 
         /* 发送AT命令测试 */
         ppp_at_response_t response;
-        ppp_error_t ret = ppp_at_send_command("AT", "OK", 1000, &response);
+        
+        /* 增加重试机制 */
+        ppp_error_t ret = PPP_ERR_AT_FAILED;
+        for (int retry = 0; retry < 3; retry++) {
+            PPP_DEBUG_INFO("--- AT Test at %u baud, retry %d/3 ---", baudrate, retry + 1);
+            ret = ppp_at_send_command("AT", "OK", 3000, &response);  // 增加超时到3秒
+            
+            if (ret == PPP_ERR_OK) {
+                PPP_DEBUG_INFO("AT command successful at baudrate %u", baudrate);
+                break;
+            } else {
+                PPP_DEBUG_WARN("AT command failed at baudrate %u, retry %d (error: %d)", baudrate, retry + 1, ret);
+            }
+            
+            if (retry < 2) {
+                osal_msleep(500); // 重试间隔
+            }
+        }
         
         if (ret == PPP_ERR_OK) {
             g_at_ctx.current_baudrate = baudrate;
-            PPP_DEBUG_INFO("Baudrate handshake successful at %u", baudrate);
+            PPP_DEBUG_INFO("=== Baudrate handshake successful at %u ===", baudrate);
             return PPP_ERR_OK;
         }
         
+        PPP_DEBUG_WARN("Baudrate %u failed, trying next...", baudrate);
         osal_msleep(100); // 短暂延时后尝试下一个波特率
     }
 
@@ -190,7 +219,7 @@ ppp_error_t ppp_at_set_baudrate(uint32_t baudrate)
     g_at_ctx.current_baudrate = baudrate;
     g_at_ctx.auto_baudrate_mode = false;
     
-    PPP_DEBUG_INFO("Baudrate set to %u (fixed)", baudrate);
+    PPP_DEBUG_INFO("Baudrate set bus(%d)  to %u (fixed)",g_at_ctx.uart_bus, baudrate);
     return PPP_ERR_OK;
 }
 
@@ -204,9 +233,12 @@ ppp_error_t ppp_at_send_command(const char *command, const char *expected_respon
         return PPP_ERR_INVALID_PARAM;
     }
 
-    osal_mutex_lock(g_at_ctx.at_mutex);
+    osal_mutex_lock(&g_at_ctx.at_mutex);
 
-    PPP_DEBUG_DEBUG("Sending AT command: %s", command);
+    PPP_DEBUG_INFO("=== AT Command Send ===");
+    PPP_DEBUG_INFO("Command: %s", command);
+    PPP_DEBUG_INFO("Expected: %s", expected_response ? expected_response : "None");
+    PPP_DEBUG_INFO("Timeout: %u ms", timeout);
 
     /* 清空响应缓冲区 */
     memset_s(response, sizeof(ppp_at_response_t), 0, sizeof(ppp_at_response_t));
@@ -216,28 +248,96 @@ ppp_error_t ppp_at_send_command(const char *command, const char *expected_respon
     snprintf_s(cmd_with_crlf, sizeof(cmd_with_crlf), sizeof(cmd_with_crlf) - 1, "%s\r\n", command);
     
     uint32_t cmd_len = strlen(cmd_with_crlf);
+    
+    /* 打印发送的原始数据(十六进制) */
+    PPP_DEBUG_INFO("Sending %u bytes:", cmd_len);
+    for (uint32_t i = 0; i < cmd_len; i++) {
+        PPP_DEBUG_PRINT("%02X ", (uint8_t)cmd_with_crlf[i]);
+    }
+    PPP_DEBUG_PRINT("\n");
+    
+    /* 打印发送的字符串(可见字符) */
+    char printable_cmd[PPP_AT_CMD_MAX_LEN + 20];
+    uint32_t j = 0;
+    for (uint32_t i = 0; i < cmd_len && j < sizeof(printable_cmd) - 10; i++) {
+        if (cmd_with_crlf[i] == '\r') {
+            printable_cmd[j++] = '\\';
+            printable_cmd[j++] = 'r';
+        } else if (cmd_with_crlf[i] == '\n') {
+            printable_cmd[j++] = '\\';
+            printable_cmd[j++] = 'n';
+        } else if (cmd_with_crlf[i] >= 32 && cmd_with_crlf[i] <= 126) {
+            printable_cmd[j++] = cmd_with_crlf[i];
+        } else {
+            j += snprintf_s(printable_cmd + j, sizeof(printable_cmd) - j, sizeof(printable_cmd) - j - 1, "\\x%02X", (uint8_t)cmd_with_crlf[i]);
+        }
+    }
+    printable_cmd[j] = '\0';
+    PPP_DEBUG_INFO("Send String: %s", printable_cmd);
+    
     if (uapi_uart_write(g_at_ctx.uart_bus, (const uint8_t *)cmd_with_crlf, cmd_len, 0) != cmd_len) {
         PPP_DEBUG_ERROR("Failed to send AT command");
-        osal_mutex_unlock(g_at_ctx.at_mutex);
+        osal_mutex_unlock(&g_at_ctx.at_mutex);
         return PPP_ERR_UART;
     }
+    
+    PPP_DEBUG_INFO("AT command sent successfully bus(%d)",g_at_ctx.uart_bus);
 
     /* 等待响应 */
     ppp_error_t ret = ppp_at_wait_response(response, timeout);
     
+    /* 打印响应结果 */
+    PPP_DEBUG_INFO("=== AT Command Response ===");
+    if (ret == PPP_ERR_OK) {
+        PPP_DEBUG_INFO("Response received (%u bytes):", response->length);
+        
+        /* 打印接收的原始数据(十六进制) */
+        for (uint32_t i = 0; i < response->length; i++) {
+            PPP_DEBUG_PRINT("%02X ", (uint8_t)response->response[i]);
+            if ((i + 1) % 16 == 0) PPP_DEBUG_PRINT("\n");
+        }
+        if (response->length % 16 != 0) PPP_DEBUG_PRINT("\n");
+        
+        /* 打印接收的字符串(可见字符) */
+        char printable_resp[PPP_AT_RESPONSE_MAX_LEN + 100];
+        uint32_t j = 0;
+        for (uint32_t i = 0; i < response->length && j < sizeof(printable_resp) - 10; i++) {
+            if (response->response[i] == '\r') {
+                printable_resp[j++] = '\\';
+                printable_resp[j++] = 'r';
+            } else if (response->response[i] == '\n') {
+                printable_resp[j++] = '\\';
+                printable_resp[j++] = 'n';
+            } else if (response->response[i] >= 32 && response->response[i] <= 126) {
+                printable_resp[j++] = response->response[i];
+            } else {
+                j += snprintf_s(printable_resp + j, sizeof(printable_resp) - j, sizeof(printable_resp) - j - 1, "\\x%02X", (uint8_t)response->response[i]);
+            }
+        }
+        printable_resp[j] = '\0';
+        PPP_DEBUG_INFO("Response String: %s", printable_resp);
+        PPP_DEBUG_INFO("Raw Response: %s", response->response);
+    } else {
+        PPP_DEBUG_ERROR("No response received (error: %d)", ret);
+    }
+    
     if (ret == PPP_ERR_OK && expected_response) {
         if (!ppp_at_parse_response(response->response, expected_response)) {
-            PPP_DEBUG_WARN("Unexpected response: %s", response->response);
+            PPP_DEBUG_WARN("Response validation failed - Expected: %s", expected_response);
+            PPP_DEBUG_WARN("Response validation failed - Received: %s", response->response);
             response->success = false;
             response->error_code = PPP_ERR_AT_FAILED;
             ret = PPP_ERR_AT_FAILED;
         } else {
+            PPP_DEBUG_INFO("Response validation successful - Expected: %s", expected_response);
             response->success = true;
             response->error_code = PPP_ERR_OK;
         }
     }
+    
+    PPP_DEBUG_INFO("=== AT Command Complete (Result: %s) ===", ret == PPP_ERR_OK ? "SUCCESS" : "FAILED");
 
-    osal_mutex_unlock(g_at_ctx.at_mutex);
+    osal_mutex_unlock(&g_at_ctx.at_mutex);
     return ret;
 }
 
@@ -430,6 +530,8 @@ static ppp_error_t ppp_at_wait_response(ppp_at_response_t *response, uint32_t ti
     uint32_t start_time = (uint32_t)osal_get_jiffies();
     uint32_t bytes_received = 0;
     bool response_complete = false;
+    
+    PPP_DEBUG_INFO("Waiting bus(%d) for AT response (timeout: %u ms)...", g_at_ctx.uart_bus,timeout);
 
     while (!response_complete && (osal_get_jiffies() - start_time) < osal_msecs_to_jiffies(timeout)) {
         uint8_t buffer[256];
@@ -442,6 +544,33 @@ static ppp_error_t ppp_at_wait_response(ppp_at_response_t *response, uint32_t ti
             bytes_read = (uint32_t)ret;
             buffer[bytes_read] = '\0';
             
+            /* 打印实时接收的数据 */
+            PPP_DEBUG_INFO("Received %u bytes:", bytes_read);
+            for (uint32_t i = 0; i < bytes_read; i++) {
+                PPP_DEBUG_PRINT("%02X ", buffer[i]);
+            }
+            PPP_DEBUG_PRINT("\n");
+            
+            /* 打印可读字符 */
+            char printable_buf[512];
+            uint32_t j = 0;
+            for (uint32_t i = 0; i < bytes_read && j < sizeof(printable_buf) - 10; i++) {
+                if (buffer[i] == '\r') {
+                    printable_buf[j++] = '\\';
+                    printable_buf[j++] = 'r';
+                } else if (buffer[i] == '\n') {
+                    printable_buf[j++] = '\\';
+                    printable_buf[j++] = 'n';
+                } else if (buffer[i] >= 32 && buffer[i] <= 126) {
+                    printable_buf[j++] = buffer[i];
+                } else {
+                    j += snprintf_s(printable_buf + j, sizeof(printable_buf) - j, sizeof(printable_buf) - j - 1, "\\x%02X", buffer[i]);
+                }
+            }
+            printable_buf[j] = '\0';
+            PPP_DEBUG_INFO("Received String: %s", printable_buf);
+            PPP_DEBUG_INFO("Received Raw: %s", (char*)buffer);
+            
             /* 追加到响应缓冲区 */
             if (bytes_received + bytes_read < PPP_AT_RESPONSE_MAX_LEN - 1) {
                 memcpy_s(response->response + bytes_received, 
@@ -451,6 +580,8 @@ static ppp_error_t ppp_at_wait_response(ppp_at_response_t *response, uint32_t ti
                 response->response[bytes_received] = '\0';
                 response->length = bytes_received;
                 
+                PPP_DEBUG_DEBUG("Total received so far: %u bytes", bytes_received);
+                
                 /* 检查是否收到完整响应 */
                 if (strstr(response->response, "OK") || 
                     strstr(response->response, "ERROR") ||
@@ -458,12 +589,15 @@ static ppp_error_t ppp_at_wait_response(ppp_at_response_t *response, uint32_t ti
                     strstr(response->response, "NO CARRIER") ||
                     strstr(response->response, "BUSY") ||
                     strstr(response->response, "NO DIALTONE")) {
+                    PPP_DEBUG_INFO("Complete response detected");
                     response_complete = true;
                 }
             } else {
                 PPP_DEBUG_WARN("AT response buffer overflow");
                 break;
             }
+        } else if (ret < 0) {
+            PPP_DEBUG_DEBUG("UART read error: %d", ret);
         }
         
         osal_msleep(10); // 短暂延时