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};
/* 支持的波特率列表 */
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");
}
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 = {
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); // 短暂延时后尝试下一个波特率
}
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;
}
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));
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;
}
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];
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,
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") ||
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); // 短暂延时