fix: improve field diagnostics and mirror logs to USB CDC

This commit is contained in:
chenjintao
2026-10-20 14:46:08 +08:00
parent 8bff1f569e
commit 07094d4a4e
20 changed files with 1036 additions and 200 deletions
@@ -16,6 +16,18 @@
#include "netdb.h"
#include "app_inc.h"
/* Temporary field-debug switch: set to 1 to restore GNSS text/NMEA logs.
* The quiet function deliberately evaluates arguments (some close the GNSS). */
#ifndef BANBAN_GNSS_LOG_ENABLED
#define BANBAN_GNSS_LOG_ENABLED 0
#endif
#if BANBAN_GNSS_LOG_ENABLED
#define GNSS_LOG printf
#else
static int gnss_log_quiet(const char *format, ...) { (void)format; return 0; }
#define GNSS_LOG gnss_log_quiet
#endif
// 新增宏定义(原代码中未定义)
#define GNSS_FAST_MODE_HOLD_TIME (5 * 60 * 1000) // FAST模式持续5分钟后切SLOW
@@ -213,7 +225,7 @@ void quec_gnss_callback(char *data, int len)
// 打印原始 NMEA 数据
for (i = 0; i < len; i++) {
printf("%c", data[i]);
GNSS_LOG("%c", data[i]);
}
// 在 data 中定位 GGA 句子的起止位置
@@ -267,7 +279,7 @@ void quec_gnss_callback(char *data, int len)
g_gps_location.is_valid = (fix_status >= 1) ? 1 : 0;
} else {
g_gps_location.is_valid = 0;
printf("\n[GNSS Parse failed] FS=%d, %s\n",
GNSS_LOG("\n[GNSS Parse failed] FS=%d, %s\n",
fix_status,
(fix_status == 0) ? "No Fix (Invalid)" : "Format error");
}
@@ -321,7 +333,7 @@ void quec_gnss_callback(char *data, int len)
void set_gnss_mode(gnss_mode_t mode)
{
g_gnss_mode = mode;
printf("[GNSSMode] Set to %s Mode\n",
GNSS_LOG("[GNSSMode] Set to %s Mode\n",
(mode == GNSS_MODE_FAST) ? "Fast (30s)" : "Slow (5min)");
}
@@ -353,11 +365,11 @@ gps_location_info_t* get_gps_location_info(void)
void print_gps_location_info(void)
{
if (g_gps_location.is_valid) {
printf("[GPSinfo] Latitude: %.8f, Longitude: %.8f\n",
GNSS_LOG("[GPSinfo] Latitude: %.8f, Longitude: %.8f\n",
g_gps_location.latitude, g_gps_location.longitude);
printf("[GPSinfo] rawNMEA: %s\n", g_gps_location.raw_nmea);
GNSS_LOG("[GPSinfo] rawNMEA: %s\n", g_gps_location.raw_nmea);
} else {
printf("[GPSinfo] No valid position data\n");
GNSS_LOG("[GPSinfo] No valid position data\n");
}
}
@@ -395,26 +407,26 @@ static void quec_gnss_test(void * argv)
/* ========== FAST MODE ========== */
if (fast_mode_start_time == 0) {
fast_mode_start_time = hal_os_up_time_ms();
printf("[GNSS] Enter FAST mode\n");
GNSS_LOG("[GNSS] Enter FAST mode\n");
}
if (!gnss_is_open) {
gnss_is_open = 1;
app_set_app_operation(1);
ql_gnss_open(quec_gnss_callback);
printf("[GNSS] FAST: GPS OPEN\n");
GNSS_LOG("[GNSS] FAST: GPS OPEN\n");
}
// 5分钟超时切SLOW
if (hal_os_up_time_ms() - fast_mode_start_time >= GNSS_FAST_MODE_HOLD_TIME) {
printf("[GNSS] FAST 5min timeout -> SLOW\n");
GNSS_LOG("[GNSS] FAST 5min timeout -> SLOW\n");
g_gnss_mode = GNSS_MODE_SLOW;
fast_mode_start_time = 0;
slow_mode_start_time = 0;
slow_next_wake_time = 0;
gnss_is_open = 0;
printf("[GNSS] SLOW: GPS CLOSED ret:%d\n", ql_gnss_close());
GNSS_LOG("[GNSS] SLOW: GPS CLOSED ret:%d\n", ql_gnss_close());
}
}
else
@@ -426,12 +438,12 @@ static void quec_gnss_test(void * argv)
if (slow_mode_start_time == 0) {
slow_mode_start_time = now;
slow_next_wake_time = now; // 立即尝试第一次定位
printf("[GNSS] Enter SLOW mode (fixed 15min interval)\n");
GNSS_LOG("[GNSS] Enter SLOW mode (fixed 15min interval)\n");
}
// 检查是否到达唤醒时间
if (now < slow_next_wake_time) {
printf("[GNSS] SLOW: next wake in %d s\n",
GNSS_LOG("[GNSS] SLOW: next wake in %d s\n",
(slow_next_wake_time - now) / 1000);
}
else if (!gnss_is_open) {
@@ -444,7 +456,7 @@ static void quec_gnss_test(void * argv)
app_set_app_operation(1);
ql_gnss_open(quec_gnss_callback);
printf("[GNSS] SLOW: GPS OPEN, search start\n");
GNSS_LOG("[GNSS] SLOW: GPS OPEN, search start\n");
/* ---- 第一阶段:60秒卫星检测 ---- */
int sat_found = 0;
@@ -454,7 +466,7 @@ static void quec_gnss_test(void * argv)
elapsed++;
if (g_gnss_sat_count > 1) {
sat_found = 1;
printf("[GNSS] SLOW: Sat found! count=%d, after %ds\n",
GNSS_LOG("[GNSS] SLOW: Sat found! count=%d, after %ds\n",
g_gnss_sat_count, elapsed);
break;
}
@@ -462,10 +474,10 @@ static void quec_gnss_test(void * argv)
if (!sat_found) {
// 60秒无卫星 → 立即关闭,等待15分钟
printf("[GNSS] SLOW: No satellite in 30s, stop ret:%d.\n", ql_gnss_close());
GNSS_LOG("[GNSS] SLOW: No satellite in 30s, stop ret:%d.\n", ql_gnss_close());
gnss_is_open = 0;
slow_next_wake_time = search_start_time + (GNSS_SEARCH_INTERVAL * 1000);
printf("[GNSS] SLOW: GPS CLOSED, next search at +%ds\n", GNSS_SEARCH_INTERVAL);
GNSS_LOG("[GNSS] SLOW: GPS CLOSED, next search at +%ds\n", GNSS_SEARCH_INTERVAL);
continue;
}
@@ -478,17 +490,17 @@ static void quec_gnss_test(void * argv)
// 1.5分钟时刻检查卫星数
int sat_count_now = g_gnss_sat_count;
printf("[GNSS] SLOW: 1min check, satellite count = %d\n", sat_count_now);
GNSS_LOG("[GNSS] SLOW: 1min check, satellite count = %d\n", sat_count_now);
if (sat_count_now < GNSS_SAT_MIN_COUNT) {
printf("[GNSS] SLOW: Sat count < %d, stop search ret:%d.\n", GNSS_SAT_MIN_COUNT,ql_gnss_close());
GNSS_LOG("[GNSS] SLOW: Sat count < %d, stop search ret:%d.\n", GNSS_SAT_MIN_COUNT,ql_gnss_close());
gnss_is_open = 0;
slow_next_wake_time = search_start_time + (GNSS_SEARCH_INTERVAL * 1000);
printf("[GNSS] SLOW: GPS CLOSED, next search at +%ds\n", GNSS_SEARCH_INTERVAL);
GNSS_LOG("[GNSS] SLOW: GPS CLOSED, next search at +%ds\n", GNSS_SEARCH_INTERVAL);
continue;
}
/* ---- 第三阶段:卫星足够,继续定位,但总时间不超过3分钟 ---- */
printf("[GNSS] SLOW: Sat count OK, continue for fix (max %ds total)\n",
GNSS_LOG("[GNSS] SLOW: Sat count OK, continue for fix (max %ds total)\n",
GNSS_MAX_SEARCH_TIME);
// 继续等待直到总时间达到 GNSS_MAX_SEARCH_TIME (180秒)
@@ -498,28 +510,28 @@ static void quec_gnss_test(void * argv)
// 如果获得有效定位,打印提示(但继续等待至3分钟)
if (g_gps_location.is_valid) {
printf("[GNSS] SLOW: Fix obtained at %ds!\n", elapsed);
GNSS_LOG("[GNSS] SLOW: Fix obtained at %ds!\n", elapsed);
print_gps_location_info();
// 若希望在定位成功后立即关闭,可在此 break,但此处按“最长3分钟”处理
}
}
// 3分钟时间到,关闭GPS
printf("[GNSS] SLOW: Max search time reached, stop.\n");
GNSS_LOG("[GNSS] SLOW: Max search time reached, stop.\n");
if (g_gps_location.is_valid) {
printf("[GNSS] SLOW: Final valid position:\n");
GNSS_LOG("[GNSS] SLOW: Final valid position:\n");
print_gps_location_info();
} else {
printf("[GNSS] SLOW: No valid fix within 3min.\n");
GNSS_LOG("[GNSS] SLOW: No valid fix within 3min.\n");
}
printf("[GNSS] SLOW: GPS CLOSED ret:%d",ql_gnss_close());
GNSS_LOG("[GNSS] SLOW: GPS CLOSED ret:%d",ql_gnss_close());
gnss_is_open = 0;
g_gps_location.is_valid = 0; // 重置状态
// 设置下次唤醒时间为 15分钟 后
slow_next_wake_time = search_start_time + (GNSS_SEARCH_INTERVAL * 1000);
printf("[GNSS] SLOW: GPS CLOSED, next search at +%ds\n", GNSS_SEARCH_INTERVAL);
GNSS_LOG("[GNSS] SLOW: GPS CLOSED, next search at +%ds\n", GNSS_SEARCH_INTERVAL);
}
// else: GPS正在运行中(理论上不会到这里)
}