From: 孙春阳 Date: Mon, 22 Dec 2025 11:55:53 +0000 (+0800) Subject: 补充提交at指令 X-Git-Url: http://git.snicker.com.cn/css/html/static/gitweb.css?a=commitdiff_plain;h=854e51c3e431c62326389361f08760740927bb99;p=mirrors%2FFD230.git 补充提交at指令 --- diff --git a/code/sdk/zhihui/ppp/src/ppp_api.c b/code/sdk/zhihui/ppp/src/ppp_api.c index ebff75625..dbdaffbf0 100644 --- a/code/sdk/zhihui/ppp/src/ppp_api.c +++ b/code/sdk/zhihui/ppp/src/ppp_api.c @@ -2819,5 +2819,208 @@ errcode_t ppp_get_network_signal_info(void) PPP_DEBUG_CRITICAL("=== End Signal Summary ==="); + return ERRCODE_SUCC; +} + +/** + * @brief 进入休眠模式 + * + * 流程: + * 1. 发送 "+++" 退出PPP模式 + * 2. 发送 "AT+uartppptopsm" 命令 + * 3. 发送 "AT+NWENPWRSAVE=1" 允许进入休眠 + * 4. 发送 "ATO" 返回PPP模式 + * 5. 进入休眠状态 + */ +errcode_t ppp_api_enter_sleep_mode(void) +{ + PPP_DEBUG_CRITICAL("=== Entering Sleep Mode ==="); + + errcode_t ret = ERRCODE_SUCC; + bool need_resume = false; + bool need_deinit_at = false; + + // 1. 检查当前模式,如果在PPP模式,先切换到AT模式 + ppp_serial_mode_t current_mode = ppp_serial_get_mode(); + if (current_mode == PPP_SERIAL_MODE_PPP_DATA) { + PPP_DEBUG_INFO("Currently in PPP mode, switching to AT mode..."); + if (ppp_ppp_mode_pause() != ERRCODE_SUCC) { + PPP_DEBUG_ERROR("Failed to pause PPP mode"); + return ERRCODE_FAIL; + } + need_resume = true; + } else { + PPP_DEBUG_INFO("Already in AT mode"); + } + + // 2. 初始化AT handler + PPP_DEBUG_INFO("Initializing AT handler for sleep mode commands..."); + ppp_error_t at_ret = ppp_at_handler_init(SIO_UART_BUS, g_ppp_config.baudrate, true); + if (at_ret != PPP_ERR_OK) { + PPP_DEBUG_ERROR("Failed to initialize AT handler: %d", at_ret); + if (need_resume) { + ppp_ppp_mode_resume(); + } + return ERRCODE_FAIL; + } + need_deinit_at = true; + + // 3. 发送 "AT+uartppptopsm" 命令 + PPP_DEBUG_INFO("Sending AT+uartppptopsm command..."); + char response[128] = {0}; + uint32_t response_len = 0; + ret = ppp_execute_at_command("AT+uartppptopsm", response, sizeof(response), &response_len, 3000); + if (ret != ERRCODE_SUCC) { + PPP_DEBUG_ERROR("Failed to send AT+uartppptopsm command"); + if (need_deinit_at) { + ppp_at_handler_deinit(); + } + if (need_resume) { + ppp_ppp_mode_resume(); + } + return ERRCODE_FAIL; + } + PPP_DEBUG_INFO("AT+uartppptopsm response: %s", response); + + // 4. 发送 "AT+NWENPWRSAVE=1" 允许进入休眠 + PPP_DEBUG_INFO("Sending AT+NWENPWRSAVE=1 command..."); + memset_s(response, sizeof(response), 0, sizeof(response)); + response_len = 0; + ret = ppp_execute_at_command("AT+NWENPWRSAVE=1", response, sizeof(response), &response_len, 3000); + if (ret != ERRCODE_SUCC) { + PPP_DEBUG_ERROR("Failed to send AT+NWENPWRSAVE=1 command"); + if (need_deinit_at) { + ppp_at_handler_deinit(); + } + if (need_resume) { + ppp_ppp_mode_resume(); + } + return ERRCODE_FAIL; + } + PPP_DEBUG_INFO("AT+NWENPWRSAVE=1 response: %s", response); + + // 5. 如果之前切换了模式,现在恢复PPP模式 + if (need_resume) { + // 先去初始化 AT handler + if (need_deinit_at) { + PPP_DEBUG_INFO("Deinitializing AT handler..."); + ppp_at_handler_deinit(); + need_deinit_at = false; + } + + // 然后恢复 PPP 模式 + PPP_DEBUG_INFO("Resuming PPP mode before entering sleep..."); + ret = ppp_ppp_mode_resume(); + if (ret != ERRCODE_SUCC) { + PPP_DEBUG_ERROR("Failed to resume PPP mode: %d", ret); + return ERRCODE_FAIL; + } + } else { + // 如果一直在AT模式,去初始化AT handler + if (need_deinit_at) { + PPP_DEBUG_INFO("Deinitializing AT handler..."); + ppp_at_handler_deinit(); + } + } + + // 6. 进入休眠状态(调用电源管理接口) + PPP_DEBUG_CRITICAL("Entering sleep state..."); + // 这里可以调用电源管理接口,让系统进入休眠 + // 例如:cat1_api_remove_cat1_vote() 或类似的接口 + + PPP_DEBUG_CRITICAL("=== Sleep Mode Entered Successfully ==="); + + return ERRCODE_SUCC; +} + +/** + * @brief 退出休眠模式 + * + * 流程: + * 1. 如果不在AT模式,先切换到AT模式 + * 2. 发送 "AT+NWENPWRSAVE=0" 禁止休眠 + * 3. 如果之前切换了模式,恢复PPP模式 + */ +errcode_t ppp_api_exit_sleep_mode(void) +{ + PPP_DEBUG_CRITICAL("=== Exiting Sleep Mode ==="); + + errcode_t ret = ERRCODE_SUCC; + bool need_resume = false; + bool need_deinit_at = false; + + // 1. 退出休眠状态(调用电源管理接口) + PPP_DEBUG_INFO("Exiting sleep state..."); + // 这里可以调用电源管理接口,唤醒系统 + // 例如:cat1_api_add_cat1_vote() 或类似的接口 + + // 2. 检查当前模式,如果在PPP模式,先切换到AT模式 + ppp_serial_mode_t current_mode = ppp_serial_get_mode(); + if (current_mode == PPP_SERIAL_MODE_PPP_DATA) { + PPP_DEBUG_INFO("Currently in PPP mode, switching to AT mode..."); + if (ppp_ppp_mode_pause() != ERRCODE_SUCC) { + PPP_DEBUG_ERROR("Failed to pause PPP mode"); + return ERRCODE_FAIL; + } + need_resume = true; + } else { + PPP_DEBUG_INFO("Already in AT mode"); + } + + // 3. 初始化AT handler + PPP_DEBUG_INFO("Initializing AT handler for exit sleep mode command..."); + ppp_error_t at_ret = ppp_at_handler_init(SIO_UART_BUS, g_ppp_config.baudrate, true); + if (at_ret != PPP_ERR_OK) { + PPP_DEBUG_ERROR("Failed to initialize AT handler: %d", at_ret); + if (need_resume) { + ppp_ppp_mode_resume(); + } + return ERRCODE_FAIL; + } + need_deinit_at = true; + + // 4. 发送 "AT+NWENPWRSAVE=0" 禁止休眠 + PPP_DEBUG_INFO("Sending AT+NWENPWRSAVE=0 command..."); + char response[128] = {0}; + uint32_t response_len = 0; + ret = ppp_execute_at_command("AT+NWENPWRSAVE=0", response, sizeof(response), &response_len, 3000); + if (ret != ERRCODE_SUCC) { + PPP_DEBUG_ERROR("Failed to send AT+NWENPWRSAVE=0 command"); + if (need_deinit_at) { + ppp_at_handler_deinit(); + } + if (need_resume) { + ppp_ppp_mode_resume(); + } + return ERRCODE_FAIL; + } + PPP_DEBUG_INFO("AT+NWENPWRSAVE=0 response: %s", response); + + // 5. 如果之前切换了模式,恢复PPP模式 + if (need_resume) { + // 先去初始化 AT handler + if (need_deinit_at) { + PPP_DEBUG_INFO("Deinitializing AT handler..."); + ppp_at_handler_deinit(); + need_deinit_at = false; + } + + // 然后恢复 PPP 模式 + PPP_DEBUG_INFO("Resuming PPP mode after exiting sleep..."); + ret = ppp_ppp_mode_resume(); + if (ret != ERRCODE_SUCC) { + PPP_DEBUG_ERROR("Failed to resume PPP mode: %d", ret); + return ERRCODE_FAIL; + } + } else { + // 如果一直在AT模式,去初始化AT handler + if (need_deinit_at) { + PPP_DEBUG_INFO("Deinitializing AT handler..."); + ppp_at_handler_deinit(); + } + } + + PPP_DEBUG_CRITICAL("=== Sleep Mode Exited Successfully ==="); + return ERRCODE_SUCC; } \ No newline at end of file