优化低功耗,把所有的wakeup_cat1放到 串口发送里面,并在读取中断来了添加投票,idle的时候去除投票。 2525/1
author孙春阳 <snickersun@163.com>
Wed, 15 Oct 2025 09:59:36 +0000 (17:59 +0800)
committer孙春阳 <snickersun@163.com>
Wed, 15 Oct 2025 09:59:36 +0000 (17:59 +0800)
27 files changed:
code/sdk/open_source/openharmony/third_party/lwip/lwip_adapter/liteos/src/arch/sio.c
code/sdk/open_source/openharmony/third_party/lwip/lwip_v2.1.3/src/apps/sntp/sntp.c
code/sdk/zhihui/4g/cat1Service/Service/cat1_ctl.c
code/sdk/zhihui/4g/cat1Service/Service/cat1_server.c
code/sdk/zhihui/4g/cat1Service/Service/cat1_tcpServer.c
code/sdk/zhihui/4g/cat1Service/Service/sntp.c
code/sdk/zhihui/4gTest/cat1_at_cmd.c
code/sdk/zhihui/leiniao/imsl_ln/ln_api.c
code/sdk/zhihui/leiniao/imsl_ln/ln_api_device_info.c
code/sdk/zhihui/leiniao/imsl_ln/ln_api_emoji.c
code/sdk/zhihui/leiniao/imsl_ln/ln_api_ota.c
code/sdk/zhihui/leiniao/imsl_ln/ln_api_speech_chat.c
code/sdk/zhihui/leiniao/imsl_ln/ln_api_story_choose.c
code/sdk/zhihui/leiniao/imsl_ln/ln_api_story_exit.c
code/sdk/zhihui/leiniao/imsl_ln/ln_api_story_finish.c
code/sdk/zhihui/leiniao/imsl_ln/ln_api_story_random.c
code/sdk/zhihui/leiniao/imsl_ln/ln_api_timeline_activities.c
code/sdk/zhihui/leiniao/imsl_ln/ln_api_token.c
code/sdk/zhihui/leiniao/imsl_ln/ln_api_websocket.c
code/sdk/zhihui/ppp/example/ppp_at_cmd.c
code/sdk/zhihui/ppp/include/ppp_config.h
code/sdk/zhihui/ppp/src/ppp_api.c
code/sdk/zhihui/ppp/src/ppp_interrupt.c
code/sdk/zhihui/ppp/src/ppp_power_ctl.c
code/sdk/zhihui/ppp/src/ppp_serial.c
code/sdk/zhihui/services/gnss/src/gnss_http_server.c
code/sdk/zhihui/websocket/os_ws/haisi_websocket.c

index ace61d0..99a65b7 100644 (file)
 #define SIO_IRQ_PROCESS_TASK_PRIO  24    // 处理线程优先级
 #define SIO_IRQ_PROCESS_STACK_SIZE 4096  // 处理线程栈大小
 
+/* CAT1 vote 管理配置(与PPP保持一致) */
+#ifdef SUPPORT_PPP_SERVICE_INTERRUPT
+#define SIO_CAT1_VOTE_IDLE_MS      3000  // 3秒空闲后移除CAT1 vote
+#endif
+
 /* 乒乓缓冲区状态 */
 typedef enum {
     SIO_BUFFER_STATE_EMPTY = 0,      // 空闲,可以写入
@@ -159,6 +164,16 @@ static sio_stats_t g_sio_stats = {0};
 /* 数据处理回调函数指针 - 用于将数据传递给SLIP */
 static void (*g_sio_data_callback)(const uint8_t *data, uint32_t len) = NULL;
 
+#ifdef SUPPORT_PPP_SERVICE_INTERRUPT
+/* CAT1 vote 状态管理(与PPP保持一致) */
+static volatile bool g_sio_cat1_vote_added = false;
+static volatile uint32_t g_sio_last_rx_time_ms = 0;  // 最后接收数据时间
+
+/* 外部函数声明(CAT1 API) */
+extern void cat1_api_add_cat1_vote(void);
+extern void cat1_api_remove_cat1_vote(void);
+#endif
+
 /* 内部函数声明 */
 static uint32_t sio_get_current_time_ms(void);
 static errcode_t sio_flush_buffer(uint8_t buffer_index);
@@ -310,6 +325,21 @@ static void sio_timeout_timer_callback(unsigned long arg)
     // ⚠️ 更新全局时间戳(供ISR使用,ISR中不能调用时间函数)
     g_sio_timer_timestamp_ms = current_time;
     
+#ifdef SUPPORT_PPP_SERVICE_INTERRUPT
+    // ✅ CAT1 vote 管理:检测 idle 状态,超过阈值时移除 vote
+    if (g_sio_cat1_vote_added) {
+        if (g_sio_last_rx_time_ms > 0) {
+            uint32_t idle_time = current_time - g_sio_last_rx_time_ms;
+            if (idle_time >= SIO_CAT1_VOTE_IDLE_MS) {
+                // 空闲时间超过阈值,移除 vote 允许 CAT1 休眠
+                cat1_api_remove_cat1_vote();
+                g_sio_cat1_vote_added = false;
+                printf("[SIO_CAT1_VOTE] Removed vote after %ums idle\n", idle_time);
+            }
+        }
+    }
+#endif
+    
     if (g_sio_ping_pong_buffers == NULL) {
         goto restart_timer;
     }
@@ -371,6 +401,17 @@ static void sio_uart_rx_interrupt_handler(const void *buffer, uint16_t length, b
     g_sio_stats.total_interrupts++;
     g_sio_stats.total_bytes_received += length;
     
+#ifdef SUPPORT_PPP_SERVICE_INTERRUPT
+    // ✅ 更新最后接收时间(用于空闲检测)
+    g_sio_last_rx_time_ms = g_sio_timer_timestamp_ms;
+    
+    // ✅ CAT1 vote 管理:接收数据时添加 vote(保持 CAT1 唤醒)
+    if (!g_sio_cat1_vote_added) {
+        cat1_api_add_cat1_vote();
+        g_sio_cat1_vote_added = true;
+    }
+#endif
+    
     uint8_t current_idx = g_sio_current_buffer_index;
     sio_ping_pong_buffer_t *current_buffer = &g_sio_ping_pong_buffers[current_idx];
     
@@ -559,6 +600,12 @@ errcode_t sio_interrupt_enable(void (*data_callback)(const uint8_t *data, uint32
     }
     g_sio_current_buffer_index = 0;
     
+#ifdef SUPPORT_PPP_SERVICE_INTERRUPT
+    // 初始化 CAT1 vote 状态
+    g_sio_cat1_vote_added = false;
+    g_sio_last_rx_time_ms = 0;
+#endif
+    
     printf("[SIO] Allocated %d x %d bytes = %.2f KB total\n", 
            SIO_PING_PONG_BUFFER_COUNT, SIO_PING_PONG_BUFFER_SIZE,
            (SIO_PING_PONG_BUFFER_COUNT * SIO_PING_PONG_BUFFER_SIZE) / 1024.0);
@@ -710,6 +757,16 @@ errcode_t sio_interrupt_disable(void)
     // 5. 清零统计
     memset(&g_sio_stats, 0, sizeof(sio_stats_t));
     
+#ifdef SUPPORT_PPP_SERVICE_INTERRUPT
+    // 6. 清理 CAT1 vote(如果之前添加过)
+    if (g_sio_cat1_vote_added) {
+        cat1_api_remove_cat1_vote();
+        g_sio_cat1_vote_added = false;
+        printf("[SIO] CAT1 vote removed during disable\n");
+    }
+    g_sio_last_rx_time_ms = 0;
+#endif
+    
     g_sio_interrupt_enabled = false;
     g_sio_irq_process_task = 0;
     g_sio_data_callback = NULL;
@@ -726,6 +783,14 @@ void sio_print_stats(void)
 {
     printf("=== SIO Interrupt Statistics (Ping-Pong Mode) ===\n");
     printf("Interrupt Status: %s\n", g_sio_interrupt_enabled ? "ENABLED" : "DISABLED");
+#ifdef SUPPORT_PPP_SERVICE_INTERRUPT
+    printf("CAT1 Vote Status: %s\n", 
+           g_sio_cat1_vote_added ? "ADDED (awake)" : "REMOVED (sleep)");
+    if (g_sio_last_rx_time_ms > 0) {
+        uint32_t idle_time = sio_get_current_time_ms() - g_sio_last_rx_time_ms;
+        printf("RX Idle Time: %u ms\n", idle_time);
+    }
+#endif
     printf("Ping-Pong Buffers: %d x %d bytes = %d KB total\n", 
            SIO_PING_PONG_BUFFER_COUNT, SIO_PING_PONG_BUFFER_SIZE,
            (SIO_PING_PONG_BUFFER_COUNT * SIO_PING_PONG_BUFFER_SIZE) / 1024);
index 5b58a15..f05155b 100644 (file)
@@ -615,7 +615,6 @@ sntp_request(void *arg)
       LWIP_DEBUGF(SNTP_DEBUG_STATE, ("ble connected, skip sntp_request.\n"));
       return;
   }
-  uapi_cat1_wakeup_cat1(1);
   /* initialize SNTP server address */
 #if SNTP_SERVER_DNS
   if (sntp_servers[sntp_current_server].name) {
@@ -626,7 +625,6 @@ sntp_request(void *arg)
     if (err == ERR_INPROGRESS) {
       /* DNS request sent, wait for sntp_dns_found being called */
       LWIP_DEBUGF(SNTP_DEBUG_STATE, ("sntp_request: Waiting for server address to be resolved.\n"));
-      uapi_cat1_wakeup_cat1(0);
       return;
     } else if (err == ERR_OK) {
       sntp_servers[sntp_current_server].addr = sntp_server_address;
@@ -648,7 +646,6 @@ sntp_request(void *arg)
     sys_untimeout(sntp_try_next_server, NULL);
     sys_timeout((u32_t)SNTP_RETRY_TIMEOUT, sntp_try_next_server, NULL);
   }
-  uapi_cat1_wakeup_cat1(0);
 }
 
 /**
index 3e869b9..3993f19 100644 (file)
@@ -290,9 +290,7 @@ void cat1_ring_isr_callback(pin_t pin, uintptr_t param)
     cat1_api_add_cat1_vote();
     //中断会在此添加休眠投票,在收到cat1的消息后,会移除投票进入休眠。
     //之前的5秒后移除投票延迟太久,与5秒更新一次信号强度
-    #ifdef USE_CAT1_SLEEP_TIMER
-        restart_sleep_cat1_timer();
-    #endif
+
 }
 
 static uint8_t cat1_keepon_wakeup = 0;
index 5bf66d1..532200d 100644 (file)
@@ -420,7 +420,6 @@ void udp_send_msg_toCat1(uint8_t cmd,char *content)
         return ;
     }
 
-    cat1_api_wakeup_cat1(1);
     // uart_port_register_irq(UART_BUS_0);
     osal_irq_enable(UART_1_IRQN);
 
@@ -436,7 +435,6 @@ void udp_send_msg_toCat1(uint8_t cmd,char *content)
 
      
     }
-     cat1_api_wakeup_cat1(0);
 }
 
 #define BOOTMSG_HEADER_STR "BOOTUP"
index 2c8a957..f98f608 100644 (file)
@@ -209,7 +209,6 @@ void ble_tcp_send_msg_toCat1(uint8_t mode,uint8_t cmd,char *content)
         cat1_print_err("Error cat1 poweroff tcp skipp send data\n");
         return ;
     }
-    cat1_api_wakeup_cat1(1);
     // uart_port_register_irq(UART_BUS_0);
     osal_irq_enable(UART_1_IRQN);
 
@@ -223,7 +222,6 @@ void ble_tcp_send_msg_toCat1(uint8_t mode,uint8_t cmd,char *content)
         
         udp_send_response(station_tx_buffer,mode,cmd, content);
     }
-    cat1_api_wakeup_cat1(0);
 }
 
 
index 67f8185..6c16d48 100644 (file)
@@ -615,7 +615,6 @@ sntp_request(void *arg)
   //     LWIP_DEBUGF(SNTP_DEBUG_STATE, ("ble connected, skip sntp_request.\n"));
   //     return;
   // }
-  cat1_api_wakeup_cat1(1);
   /* initialize SNTP server address */
 #if SNTP_SERVER_DNS
   if (sntp_servers[sntp_current_server].name) {
@@ -626,7 +625,6 @@ sntp_request(void *arg)
     if (err == ERR_INPROGRESS) {
       /* DNS request sent, wait for sntp_dns_found being called */
       LWIP_DEBUGF(SNTP_DEBUG_STATE, ("sntp_request: Waiting for server address to be resolved.\n"));
-      cat1_api_wakeup_cat1(0);
       return;
     } else if (err == ERR_OK) {
       sntp_servers[sntp_current_server].addr = sntp_server_address;
@@ -648,7 +646,6 @@ sntp_request(void *arg)
     sys_untimeout(sntp_try_next_server, NULL);
     sys_timeout((u32_t)SNTP_RETRY_TIMEOUT, sntp_try_next_server, NULL);
   }
-  cat1_api_wakeup_cat1(0);
 }
 
 /**
index 0bac7e0..e766f43 100644 (file)
@@ -199,12 +199,10 @@ static uint32_t cat1_wakeup(uint8_t *para, uint32_t paraLen, char *strCommonCmd)
     if(enabled == 0)
     {
         cat1_api_keep_wakeup(0);
-        cat1_api_wakeup_cat1(0);
     }
     else 
     {
         cat1_api_keep_wakeup(1);
-        cat1_api_wakeup_cat1(1);
     }
     cat1_print_info("CAT1TEST ATCMD  keepwakeup cat1  enabled = %d\n",enabled);
 
@@ -222,12 +220,8 @@ static uint32_t cat1_ping(uint8_t *para, uint32_t paraLen, char *strCommonCmd)
         cat1_print_err("CAT1TEST ATCMD Convert Command Failed\n");
         return ERRCODE_FAIL;
     }
-    cat1_api_wakeup_cat1(1);
-    cat1_api_keep_wakeup(1);
     u32_t sret= OsShellPing( argc, (const char *)argv[0]);
     cat1_print_info("CAT1TEST ATCMD  ping   ret = 0x%X\n",sret);
-    cat1_api_keep_wakeup(0);
-    cat1_api_wakeup_cat1(0);
     return ERRCODE_SUCC;
 }
 static uint32_t cat1_ifconfig(uint8_t *para, uint32_t paraLen, char *strCommonCmd)
@@ -321,8 +315,6 @@ errcode_t http_single_download_start( uint8_t *server, uint16_t port,uint8_t *ur
 
     //通讯开始,唤醒cat1
     cat1_api_add_cat1_vote();
-    cat1_api_wakeup_cat1(1);
-    cat1_api_keep_wakeup(1);
     cat1_api_disable_cat1_cmd(1);
     //httpc_connection_t settings = {0};
 
@@ -361,8 +353,6 @@ errcode_t http_single_download_stop(void)
     //通讯结束,休眠cat1
     g_http_download_started = 0;
     cat1_api_remove_cat1_vote();
-    cat1_api_keep_wakeup(0);
-    cat1_api_wakeup_cat1(0);
     cat1_api_disable_cat1_cmd(0);
     return ERRCODE_SUCC;
 }
index 2be4e97..5b6fd36 100644 (file)
@@ -967,8 +967,6 @@ static uint32_t ln_download_mp3_file_advanced(const char *audio_url, const char
 #ifdef SUPPORT_CAT1_SERVICE
     /* CAT1 网络保活处理 */
     cat1_api_add_cat1_vote();
-    cat1_api_wakeup_cat1(1);
-    cat1_api_keep_wakeup(1);
     cat1_api_disable_cat1_cmd(0);
     LOS_Msleep(300);
 #endif
index 2768a9e..a2449f3 100644 (file)
@@ -269,8 +269,6 @@ uint32_t ln_device_get_character_http(const char *device_id,
 #ifdef SUPPORT_CAT1_SERVICE
     /* CAT1 保活 */
     cat1_api_add_cat1_vote();
-    cat1_api_wakeup_cat1(1);
-    cat1_api_keep_wakeup(1);
     cat1_api_disable_cat1_cmd(0);
     LOS_Msleep(200);
 #endif
@@ -340,8 +338,6 @@ uint32_t ln_device_get_user_id_http(const char *device_id,
 #ifdef SUPPORT_CAT1_SERVICE
     /* CAT1 保活 */
     cat1_api_add_cat1_vote();
-    cat1_api_wakeup_cat1(1);
-    cat1_api_keep_wakeup(1);
     cat1_api_disable_cat1_cmd(0);
     LOS_Msleep(200);
 #endif
@@ -447,8 +443,6 @@ uint32_t ln_device_get_chat_stamina_http(const char *device_id, const char *devi
 
 #ifdef SUPPORT_CAT1_SERVICE
     cat1_api_add_cat1_vote();
-    cat1_api_wakeup_cat1(1);
-    cat1_api_keep_wakeup(1);
     cat1_api_disable_cat1_cmd(0);
     LOS_Msleep(200);
 #endif
index d00c8bd..6b63916 100644 (file)
@@ -809,8 +809,6 @@ static uint32_t ln_emoji_report_status_http(const char *device_id,
     // CAT1 保活
 #ifdef SUPPORT_CAT1_SERVICE
     cat1_api_add_cat1_vote();
-    cat1_api_wakeup_cat1(1);
-    cat1_api_keep_wakeup(1);
     cat1_api_disable_cat1_cmd(0);
     LOS_Msleep(200);
 #endif
@@ -935,8 +933,6 @@ static uint32_t ln_emoji_report_all_http(const char *device_id,
 
 #ifdef SUPPORT_CAT1_SERVICE
     cat1_api_add_cat1_vote();
-    cat1_api_wakeup_cat1(1);
-    cat1_api_keep_wakeup(1);
     cat1_api_disable_cat1_cmd(0);
     LOS_Msleep(200);
 #endif
index be8b0fc..2d3467e 100644 (file)
@@ -241,8 +241,6 @@ uint32_t ln_ota_get_task_http(const char *device_id,
 #ifdef SUPPORT_CAT1_SERVICE
     /* CAT1 保活 */
     cat1_api_add_cat1_vote();
-    cat1_api_wakeup_cat1(1);
-    cat1_api_keep_wakeup(1);
     cat1_api_disable_cat1_cmd(0);
     LOS_Msleep(200);
 #endif
@@ -361,8 +359,6 @@ static int ln_download_file_to_path(const char *full_url, const char *save_path,
 #ifdef SUPPORT_CAT1_SERVICE
     /* CAT1 网络保活处理 */
     cat1_api_add_cat1_vote();
-    cat1_api_wakeup_cat1(1);
-    cat1_api_keep_wakeup(1);
     cat1_api_disable_cat1_cmd(0);
     LOS_Msleep(500); // 等待模块就绪
 #endif
index 8c03ac2..a7d9829 100644 (file)
@@ -146,8 +146,6 @@ uint32_t ln_speech_chat_stream_http(const char *device_id,
 #ifdef SUPPORT_CAT1_SERVICE
     /* CAT1 保活 */
     cat1_api_add_cat1_vote();
-    cat1_api_wakeup_cat1(1);
-    cat1_api_keep_wakeup(1);
     cat1_api_disable_cat1_cmd(0);
     LOS_Msleep(500);
 #endif
index 3efd7c0..98c3a6b 100644 (file)
@@ -143,8 +143,6 @@ uint32_t ln_story_choose_http(const char *device_id,
 #ifdef SUPPORT_CAT1_SERVICE
     /* CAT1 网络保活处理 */
     cat1_api_add_cat1_vote();
-    cat1_api_wakeup_cat1(1);
-    cat1_api_keep_wakeup(1);
     cat1_api_disable_cat1_cmd(0);
     LOS_Msleep(500);
 #endif
index 6175f53..483e203 100644 (file)
@@ -9,8 +9,6 @@ uint32_t ln_story_exit_inquiry_http(const char *device_id,
 #ifdef SUPPORT_CAT1_SERVICE
     /* CAT1 保活 */
     cat1_api_add_cat1_vote();
-    cat1_api_wakeup_cat1(1);
-    cat1_api_keep_wakeup(1);
     cat1_api_disable_cat1_cmd(0);
     LOS_Msleep(200);
 #endif
index 5a89616..7c9a79c 100644 (file)
@@ -10,8 +10,6 @@ uint32_t ln_story_finish_http(const char *device_id,
 #ifdef SUPPORT_CAT1_SERVICE
     /* CAT1 保活 */
     cat1_api_add_cat1_vote();
-    cat1_api_wakeup_cat1(1);
-    cat1_api_keep_wakeup(1);
     cat1_api_disable_cat1_cmd(0);
     LOS_Msleep(200);
 #endif
index 218e4e7..b90588d 100644 (file)
@@ -32,8 +32,6 @@ uint32_t ln_story_random_http(const char *device_id,
 #ifdef SUPPORT_CAT1_SERVICE
     /* CAT1 网络保活处理 */
     cat1_api_add_cat1_vote();
-    cat1_api_wakeup_cat1(1);
-    cat1_api_keep_wakeup(1);
     cat1_api_disable_cat1_cmd(0);
     LOS_Msleep(500);
 #endif
index 11780f5..c360ada 100644 (file)
@@ -61,8 +61,6 @@ uint32_t ln_timeline_get_status_http(const char *device_id,
 
 #ifdef SUPPORT_CAT1_SERVICE
        cat1_api_add_cat1_vote();
-       cat1_api_wakeup_cat1(1);
-       cat1_api_keep_wakeup(1);
        cat1_api_disable_cat1_cmd(0);
        LOS_Msleep(200);
 #endif
index fcdf234..2e8be61 100644 (file)
@@ -286,8 +286,6 @@ uint32_t ln_get_device_token_http(const char *device_id,
 #ifdef SUPPORT_CAT1_SERVICE
     /* 4. 保活CAT1,准备网络 */
     cat1_api_add_cat1_vote();
-    cat1_api_wakeup_cat1(1);
-    cat1_api_keep_wakeup(1);
     cat1_api_disable_cat1_cmd(0);
     LOS_Msleep(500);
 #endif
index 2ad5e74..78675ef 100644 (file)
@@ -872,8 +872,6 @@ int ln_ws_start(ws_message_callback_t_ln callback, uint32_t audio_timeout_ticks)
     // 注册音频数据写入回调
     AI_RegisterWriteAudioDataCallback(ln_ws_audio_write_callback);
 
-    cat1_api_keep_wakeup(1);
-    cat1_api_wakeup_cat1(1);
 
     return 0;
 }
index 50f867d..19fc869 100644 (file)
@@ -166,13 +166,13 @@ static uint32_t cat1_wakeup(uint8_t *para, uint32_t paraLen, char *strCommonCmd)
     int enabled = atoi((char*)argv[0]);
     if(enabled == 0)
     {
-        // cat1_api_keep_wakeup(0);
-        // cat1_api_wakeup_cat1(0);
+        cat1_api_keep_wakeup(0);
+        cat1_api_wakeup_cat1(0);
     }
     else 
     {
-        // cat1_api_keep_wakeup(1);
-        // cat1_api_wakeup_cat1(1);
+        cat1_api_keep_wakeup(1);
+        cat1_api_wakeup_cat1(1);
     }
     ppp_print_info("CAT1TEST ATCMD  keepwakeup cat1  enabled = %d\n",enabled);
 
@@ -190,12 +190,8 @@ static uint32_t cat1_ping(uint8_t *para, uint32_t paraLen, char *strCommonCmd)
         ppp_print_err("CAT1TEST ATCMD Convert Command Failed\n");
         return ERRCODE_FAIL;
     }
-    // cat1_api_wakeup_cat1(1);
-    // cat1_api_keep_wakeup(1);
     u32_t sret= OsShellPing( argc, (const char *)argv[0]);
     ppp_print_info("CAT1TEST ATCMD  ping   ret = 0x%X\n",sret);
-    // cat1_api_keep_wakeup(0);
-    // cat1_api_wakeup_cat1(0);
     return ERRCODE_SUCC;
 }
 static uint32_t cat1_ifconfig(uint8_t *para, uint32_t paraLen, char *strCommonCmd)
index a5ad45e..bf881bd 100644 (file)
@@ -9,6 +9,7 @@ extern "C" {
 #include <stdbool.h>
 
 /* PPP配置参数 - UART配置统一使用SIO (在lwipopts_default.h中定义) */
+#define PPP_LOW_POWER_MODE          1
 
 /* SIO设备号配置 - 与SLIP区分开 */
 #define PPP_IFNUM                   0                   // PPP使用的SIO设备号,与SLIP_IFNUM(0)区分
index ebff756..4921348 100644 (file)
@@ -91,13 +91,10 @@ errcode_t ppp_api_power_control(bool is_open)
 
         // 唤醒4G模块
         PPP_DEBUG_PRINT("Waking up 4G module...\n");
-        cat1_api_wakeup_cat1(true);
        
     } else {
         PPP_DEBUG_PRINT("Sleeping 4G module power...\n");
         // 唤醒4G模块
-        cat1_api_wakeup_cat1(false);
-        osal_msleep(500);
         // 启用4G电源
         PPP_DEBUG_PRINT("Disabling 4G module power...\n");
         cat1_api_enable_cat1(false);
index 4f62eff..fcd6fc4 100644 (file)
@@ -130,10 +130,22 @@ static ppp_data_state_t g_ppp_data_state = {0};
 #define PPP_SAFE_SWITCH_IDLE_MS     5000    // 5秒空闲才安全切换
 #define PPP_LIGHT_THRESHOLD_BPS     100     // 100 bps以下为轻量传输
 
+#ifdef SUPPORT_PPP_SERVICE_INTERRUPT
+#define PPP_CAT1_VOTE_IDLE_MS       3000    // 3秒空闲后移除CAT1 vote
+/* CAT1 vote 状态管理 */
+static volatile bool g_ppp_cat1_vote_added = false;  // CAT1 vote 状态标志
+#endif
+
 /* 外部函数声明(在ppp_lwip.c中实现) */
 extern ppp_pcb* ppp_lwip_get_pcb(void);
 extern bool ppp_lwip_is_initialized(void);
 
+#ifdef SUPPORT_PPP_SERVICE_INTERRUPT
+/* 外部函数声明(CAT1 API) */
+extern void cat1_api_add_cat1_vote(void);
+extern void cat1_api_remove_cat1_vote(void);
+#endif
+
 /* 内部函数声明 */
 static uint32_t ppp_get_current_time_ms(void);
 static ppp_error_t ppp_flush_buffer(uint8_t buffer_index);
@@ -403,6 +415,21 @@ static void ppp_timeout_timer_callback(unsigned long arg)
         last_rate_update_time = current_time;
     }
     
+#ifdef SUPPORT_PPP_SERVICE_INTERRUPT
+    // ✅ CAT1 vote 管理:检测 idle 状态,超过阈值时移除 vote
+    if (g_ppp_cat1_vote_added) {
+        if (g_ppp_data_state.last_rx_time_ms > 0) {
+            uint32_t idle_time = current_time - g_ppp_data_state.last_rx_time_ms;
+            if (idle_time >= PPP_CAT1_VOTE_IDLE_MS) {
+                // 空闲时间超过阈值,移除 vote 允许 CAT1 休眠
+                cat1_api_remove_cat1_vote();
+                g_ppp_cat1_vote_added = false;
+                PPP_DEBUG_INFO("[CAT1_VOTE] Removed vote after %ums idle", idle_time);
+            }
+        }
+    }
+#endif
+    
     uint8_t current_idx = g_current_buffer_index;  // 读取一次,避免重复读取
     
     // ⚠️ 检查内存是否已分配
@@ -546,6 +573,14 @@ void _ppp_uart_rx_interrupt_handler(const void *buffer, uint16_t length, bool er
     g_ppp_data_state.last_rx_time_ms = g_timer_timestamp_ms;
     g_ppp_data_state.rx_bytes_this_second += length;
     
+#ifdef SUPPORT_PPP_SERVICE_INTERRUPT
+    // ✅ CAT1 vote 管理:接收数据时添加 vote(保持 CAT1 唤醒)
+    if (!g_ppp_cat1_vote_added) {
+        cat1_api_add_cat1_vote();
+        g_ppp_cat1_vote_added = true;
+    }
+#endif
+    
     // 获取当前buffer
     uint8_t current_idx = g_current_buffer_index;
     ppp_ping_pong_buffer_t *current_buffer = &g_ping_pong_buffers[current_idx];
@@ -710,6 +745,11 @@ ppp_error_t ppp_interrupt_enable(void)
     }
     g_current_buffer_index = 0;
     
+#ifdef SUPPORT_PPP_SERVICE_INTERRUPT
+    // 2.1 初始化 CAT1 vote 状态
+    g_ppp_cat1_vote_added = false;
+#endif
+    
     PPP_DEBUG_INFO("Ping-pong buffers initialized: %d x %d bytes = %d KB total", 
                   PPP_PING_PONG_BUFFER_COUNT, PPP_PING_PONG_BUFFER_SIZE,
                   (PPP_PING_PONG_BUFFER_COUNT * PPP_PING_PONG_BUFFER_SIZE) / 1024);
@@ -957,6 +997,16 @@ ppp_error_t ppp_interrupt_disable(void)
     memset(&g_ppp_interrupt_stats, 0, sizeof(ppp_interrupt_stats_t));
     memset(&g_pingpong_stats, 0, sizeof(ppp_pingpong_stats_t));
     memset(&g_uart_error_stats, 0, sizeof(ppp_uart_error_stats_t));
+    memset(&g_ppp_data_state, 0, sizeof(ppp_data_state_t));
+    
+#ifdef SUPPORT_PPP_SERVICE_INTERRUPT
+    // 9. 清理 CAT1 vote(如果之前添加过)
+    if (g_ppp_cat1_vote_added) {
+        cat1_api_remove_cat1_vote();
+        g_ppp_cat1_vote_added = false;
+        PPP_DEBUG_INFO("CAT1 vote removed during PPP interrupt disable");
+    }
+#endif
     
     g_ppp_interrupt_enabled = false;
     g_ppp_irq_process_task = 0;
@@ -1288,6 +1338,10 @@ void ppp_interrupt_print_data_state(void)
                    ppp_lwip_is_connected() ? "YES" : "NO ");
     PPP_DEBUG_WARN("║ RX Idle Time:   %u ms                 ║", idle_ms);
     PPP_DEBUG_WARN("║ RX Rate:        %u bytes/sec          ║", g_ppp_data_state.rx_rate_bps);
+#ifdef SUPPORT_PPP_SERVICE_INTERRUPT
+    PPP_DEBUG_WARN("║ CAT1 Vote:      %s                    ║", 
+                   g_ppp_cat1_vote_added ? "ADDED (awake)" : "REMOVED (sleep)");
+#endif
     PPP_DEBUG_WARN("║ Safe for +++:   %s                    ║", 
                    ppp_interrupt_is_safe_for_command_switch() ? "YES ✓" : "NO  ✗");
     PPP_DEBUG_WARN("╠═══════════════════════════════════════╣");
index 99b1f33..231ab10 100644 (file)
@@ -416,14 +416,11 @@ void ppp_ring_isr_callback(pin_t pin, uintptr_t param)
     gpio_level_t val = uapi_gpio_get_val(pin);
     if (val == GPIO_LEVEL_HIGH) {
         PPP_DEBUG_PRINT("PPP: Cat1 ring interrupt on pin %d high \n", pin);
-        #ifdef PPP_LOW_POWER_MODE
-        ppp_api_wakeup_cat1(true);
-        #endif
+        cat1_api_add_cat1_vote();
     } else {
         PPP_DEBUG_PRINT("PPP: Cat1 ring interrupt on pin %d low\n", pin);
-        #ifdef PPP_LOW_POWER_MODE
-        ppp_api_wakeup_cat1(false);
-        #endif
+        //idle后 remove
+        // cat1_api_remove_cat1_vote();
     }
     uapi_gpio_clear_interrupt(pin);
 }
index 84ee6f1..3305c37 100644 (file)
@@ -342,7 +342,7 @@ ppp_error_t ppp_serial_write(const uint8_t *data, uint32_t length)
     }
 
     PPP_DEBUG_DEBUG("=== PPP Serial Write Start ===");
-    #ifdef PPP_LOW_POWER_MODE
+    #if defined(PPP_LOW_POWER_MODE) && (PPP_LOW_POWER_MODE == 1)
     ppp_api_wakeup_cat1(true);
     osal_msleep(1);
     #endif
@@ -424,7 +424,7 @@ ppp_error_t ppp_serial_write(const uint8_t *data, uint32_t length)
                    result == PPP_ERR_OK ? "SUCCESS" : "FAILED", result);
     PPP_DEBUG_INFO("Statistics: tx_bytes=%u, tx_errors=%u", g_serial_ctx.tx_bytes, g_serial_ctx.tx_errors);
     PPP_DEBUG_INFO("=== PPP Serial Write End ===");
-    #ifdef PPP_LOW_POWER_MODE
+    #if defined(PPP_LOW_POWER_MODE) && (PPP_LOW_POWER_MODE == 1)
     ppp_api_wakeup_cat1(false);
     #endif
 
@@ -439,6 +439,10 @@ ppp_error_t ppp_serial_read(uint8_t *buffer, uint32_t buffer_size, uint32_t *byt
     if (!g_serial_ctx.initialized || !buffer || !bytes_read || buffer_size == 0) {
         return PPP_ERR_INVALID_PARAM;
     }
+    #if defined(PPP_LOW_POWER_MODE) && (PPP_LOW_POWER_MODE == 1)
+    ppp_api_wakeup_cat1(true);
+    osal_msleep(1);
+    #endif
 
     *bytes_read = 0;
     ppp_error_t result = PPP_ERR_OK;
@@ -523,7 +527,8 @@ ppp_error_t ppp_serial_read(uint8_t *buffer, uint32_t buffer_size, uint32_t *byt
                         // 读取错误
                         if (total_read == 0) {
                             PPP_DEBUG_ERROR("PPP UART Read: Error %d", read_result);
-                            return PPP_ERR_UART;
+                            result = PPP_ERR_UART;
+                            break;
                         }
                         break;
                     }
@@ -533,9 +538,11 @@ ppp_error_t ppp_serial_read(uint8_t *buffer, uint32_t buffer_size, uint32_t *byt
                 
                 if (total_read > 0) {
                     PPP_DEBUG_DEBUG("PPP optimized read: %u bytes in %u batches", total_read, batch_count);
-                    return PPP_ERR_OK;
+                    result = PPP_ERR_OK;
+                    break;
                 } else {
-                    return PPP_ERR_OK; // 无数据但无错误
+                    result = PPP_ERR_OK;
+                    break; // 无数据但无错误
                 }
             }
             break;
@@ -571,6 +578,10 @@ ppp_error_t ppp_serial_read(uint8_t *buffer, uint32_t buffer_size, uint32_t *byt
             break;
     }
 
+    #if defined(PPP_LOW_POWER_MODE) && (PPP_LOW_POWER_MODE == 1)
+    ppp_api_wakeup_cat1(false);
+    osal_msleep(1);
+    #endif
     return result;
 }
 
index d485686..c65a3f7 100644 (file)
@@ -178,8 +178,6 @@ static void agnss_download_result_handler(const char *filename, HTTPC_RESULT res
             // HTTP请求失败,清理CAT1资源
 #ifdef SUPPORT_CAT1_SERVICE
             cat1_api_remove_cat1_vote();
-            cat1_api_keep_wakeup(0);
-            cat1_api_wakeup_cat1(0);
             cat1_api_disable_cat1_cmd(0);
 #endif
         }
@@ -1392,8 +1390,6 @@ static void agnss_process_complete_response(void)
 
     #ifdef SUPPORT_CAT1_SERVICE
         cat1_api_remove_cat1_vote();
-        cat1_api_keep_wakeup(0);
-        cat1_api_wakeup_cat1(0);
         cat1_api_disable_cat1_cmd(0);
     #endif
         
@@ -1546,8 +1542,6 @@ int download_agnss_files(void)
     //通讯开始,唤醒cat1
 #ifdef SUPPORT_CAT1_SERVICE
     cat1_api_add_cat1_vote();
-    cat1_api_wakeup_cat1(1);
-    cat1_api_keep_wakeup(1);
     cat1_api_disable_cat1_cmd(0);  // 修改:允许CAT1命令,确保DNS查询可用
 #endif
     // 等待CAT1就绪
@@ -1651,8 +1645,6 @@ int download_agnss_files(void)
 
 #ifdef SUPPORT_CAT1_SERVICE
         cat1_api_remove_cat1_vote();
-        cat1_api_keep_wakeup(0);
-        cat1_api_wakeup_cat1(0);
         cat1_api_disable_cat1_cmd(0);
 #endif
 
@@ -2368,8 +2360,6 @@ int gnss_start_agnss_download_simple(void)
         // 下载取消,清理CAT1资源
 #ifdef SUPPORT_CAT1_SERVICE
         cat1_api_remove_cat1_vote();
-        cat1_api_keep_wakeup(0);
-        cat1_api_wakeup_cat1(0);
         cat1_api_disable_cat1_cmd(0);
 #endif
         return -1;
@@ -2384,8 +2374,6 @@ int gnss_start_agnss_download_simple(void)
             // 下载取消,清理CAT1资源
 #ifdef SUPPORT_CAT1_SERVICE
             cat1_api_remove_cat1_vote();
-            cat1_api_keep_wakeup(0);
-            cat1_api_wakeup_cat1(0);
             cat1_api_disable_cat1_cmd(0);
 #endif
             return -1;
@@ -2470,8 +2458,6 @@ errcode_t uapi_gnss_http_server_deinit(void)
     // 清理CAT1资源
 #ifdef SUPPORT_CAT1_SERVICE
     cat1_api_remove_cat1_vote();
-    cat1_api_keep_wakeup(0);
-    cat1_api_wakeup_cat1(0);
     cat1_api_disable_cat1_cmd(0);
     PRINT("CAT1 resources cleaned up\r\n");
 #endif
index 0530aef..17baf75 100644 (file)
@@ -625,8 +625,6 @@ int uapi_ws_start(const ws_call_backs_t *callbacks)
         };
         (void)uapi_create_osThread_custom_with_args(&main_task);
     }
-    cat1_api_keep_wakeup(1);
-    cat1_api_wakeup_cat1(1);
 
     return 0;
 }