修改GPSstop的逻辑 待验证
authorCaisking <Casiking@zhihuishixun.com>
Mon, 19 Jan 2026 02:05:23 +0000 (10:05 +0800)
committerCaisking <Casiking@zhihuishixun.com>
Mon, 19 Jan 2026 02:05:23 +0000 (10:05 +0800)
code/sdk/zhihui/services/gnss/sample/pgnss_sample/pgnss_sample.c

index 84be0ca..5cc7e97 100644 (file)
@@ -24,6 +24,7 @@
 #include "custom_utils.h"
 #include <dirent.h>
 #include <sys/stat.h>
+#include "cmsis_os2.h"
 // #include "gnss_sample.h"
 
 #define PGNSS_DATA_PATH          "/user/userdata/gnss"
@@ -61,6 +62,11 @@ static int g_is_listening_data = 0;  // 添加数据监听状态标志
 
 static char *pgnss_log = NULL;
 
+// GPS停止等待定时器相关变量
+static osTimerId_t g_stop_wait_timer = NULL;
+static int g_stop_wait_count = 0;
+static const int MAX_WAIT_COUNT = 80;  // 最多等待8秒 (80 * 100ms)
+
 // 全局变量,存储当前有效的时间戳文件夹路径
 static char *g_current_agnss_folder = NULL;
 
@@ -652,30 +658,12 @@ static void ListenGnssData(tiot_handle handle)
     cleanup_gnss_service_buffer();
 }
 
-void PGnssServerStop(void)
+/**
+ * @brief 执行GPS停止后的清理工作
+ * 这个函数在定时器回调中调用,确保非阻塞执行
+ */
+static void do_gps_stop_cleanup(void)
 {
-    printf("PGnssServerStop: Attempting to stop GPS...\r\n");
-    
-    // 检查GPS是否已经启动
-    if (g_is_pgnss_poweron == 0) {
-        printf("PGnssServerStop: GPS is not powered on, nothing to stop\r\n");
-        return;
-    }
-
-    // 等待数据监听停止(最多等待6秒)
-    int wait_count = 0;
-    const int max_wait_ms = 8000;
-    const int check_interval_ms = 100;
-    
-    while (g_is_listening_data == 0 && wait_count < (max_wait_ms / check_interval_ms)) {
-        LOS_Msleep(check_interval_ms);
-        wait_count++;
-        if (wait_count % 2 == 0) {
-            printf("PGnssServerStop: Waiting for data listening to stop... (%d/%d)\r\n", 
-                   wait_count * check_interval_ms, max_wait_ms);
-        }
-    }
-    
     // 设置停止标志
     g_pgnss_stopflag = 1;
     
@@ -696,6 +684,104 @@ void PGnssServerStop(void)
     printf("PGnssServerStop: Force stop completed\r\n");
 }
 
+/**
+ * @brief GPS停止等待定时器回调函数
+ * 每100ms检查一次数据监听状态,如果已停止或超时则执行清理
+ */
+static void gps_stop_wait_timer_callback(int data)
+{
+    g_stop_wait_count++;
+    
+    // 检查数据监听是否已停止
+    if (g_is_listening_data == 0) {
+        printf("PGnssServerStop: Data listening stopped, executing cleanup...\r\n");
+        
+        // 停止定时器
+        if (g_stop_wait_timer != NULL) {
+            osTimerStop(g_stop_wait_timer);
+            osTimerDelete(g_stop_wait_timer);
+            g_stop_wait_timer = NULL;
+        }
+        
+        // 执行清理工作
+        do_gps_stop_cleanup();
+        g_stop_wait_count = 0;
+        return;
+    }
+    
+    // 打印等待进度(每1秒打印一次)
+    if (g_stop_wait_count % 10 == 0) {
+        printf("PGnssServerStop: Waiting for data listening to stop... (%d/%d ms)\r\n", 
+               g_stop_wait_count * 100, MAX_WAIT_COUNT * 100);
+    }
+    
+    // 超时处理:如果等待时间超过最大值,强制执行清理
+    if (g_stop_wait_count >= MAX_WAIT_COUNT) {
+        printf("PGnssServerStop: Timeout reached, forcing cleanup...\r\n");
+        
+        // 停止定时器
+        if (g_stop_wait_timer != NULL) {
+            osTimerStop(g_stop_wait_timer);
+            osTimerDelete(g_stop_wait_timer);
+            g_stop_wait_timer = NULL;
+        }
+        
+        // 强制执行清理工作
+        do_gps_stop_cleanup();
+        g_stop_wait_count = 0;
+    }
+}
+
+void PGnssServerStop(void)
+{
+    printf("PGnssServerStop: Attempting to stop GPS...\r\n");
+    
+    // 检查GPS是否已经启动
+    if (g_is_pgnss_poweron == 0) {
+        printf("PGnssServerStop: GPS is not powered on, nothing to stop\r\n");
+        return;
+    }
+
+    // 如果数据监听已经停止,直接执行清理
+    if (g_is_listening_data == 0) {
+        printf("PGnssServerStop: Data listening already stopped, executing cleanup immediately\r\n");
+        do_gps_stop_cleanup();
+        return;
+    }
+    
+    // 如果定时器已存在,先停止并删除
+    if (g_stop_wait_timer != NULL) {
+        osTimerStop(g_stop_wait_timer);
+        osTimerDelete(g_stop_wait_timer);
+        g_stop_wait_timer = NULL;
+    }
+    
+    // 重置等待计数
+    g_stop_wait_count = 0;
+    
+    // 创建周期性定时器(每100ms检查一次)
+    g_stop_wait_timer = osTimerNew((osTimerFunc_t)gps_stop_wait_timer_callback, 
+                                    osTimerPeriodic, NULL, NULL);
+    if (g_stop_wait_timer == NULL) {
+        printf("PGnssServerStop: Failed to create wait timer, executing cleanup immediately\r\n");
+        do_gps_stop_cleanup();
+        return;
+    }
+    
+    // 启动定时器(100ms间隔)
+    int tick = osMs2Tick(100);
+    osStatus_t ret = osTimerStart(g_stop_wait_timer, tick);
+    if (ret != osOK) {
+        printf("PGnssServerStop: Failed to start wait timer (ret=%d), executing cleanup immediately\r\n", ret);
+        osTimerDelete(g_stop_wait_timer);
+        g_stop_wait_timer = NULL;
+        do_gps_stop_cleanup();
+        return;
+    }
+    
+    printf("PGnssServerStop: Started non-blocking wait timer, cleanup will execute when ready\r\n");
+}
+
 int getPGnssServerStat(void)
 {
     return g_is_pgnss_poweron;