添加AT^PPPDEBUGSET动态修改打印等级指令
author孙春阳 <snickersun@163.com>
Wed, 17 Sep 2025 15:20:21 +0000 (23:20 +0800)
committer孙春阳 <snickersun@163.com>
Wed, 17 Sep 2025 15:20:21 +0000 (23:20 +0800)
code/sdk/zhihui/ppp/CMakeLists.txt
code/sdk/zhihui/ppp/doc/dynamic_debug_usage.md [new file with mode: 0644]
code/sdk/zhihui/ppp/example/ppp_at_cmd.c
code/sdk/zhihui/ppp/include/ppp_debug.h
code/sdk/zhihui/ppp/include/ppp_debug_manager.h [new file with mode: 0644]
code/sdk/zhihui/ppp/src/ppp_debug_manager.c [new file with mode: 0644]

index 41539d6..739a9f9 100644 (file)
@@ -19,6 +19,7 @@ set(SOURCES
     src/ppp_lwip.c
     src/ppp_lwip_thread.c
     src/ppp_debug.c
+    src/ppp_debug_manager.c
     src/ppp_power_ctl.c
     src/ppp_cat1_api.c
     src/ppp_stub_functions.c
diff --git a/code/sdk/zhihui/ppp/doc/dynamic_debug_usage.md b/code/sdk/zhihui/ppp/doc/dynamic_debug_usage.md
new file mode 100644 (file)
index 0000000..0519ecb
--- /dev/null
@@ -0,0 +1 @@
+ 
\ No newline at end of file
index 3ceb0ff..eb178ea 100644 (file)
@@ -12,6 +12,7 @@
 #include "ppp_types.h"
 #include "ppp_api.h"
 #include "ppp_at_handler.h"
+#include "ppp_debug_manager.h"
 #include "ppp_example.h"
 
 
@@ -1591,6 +1592,128 @@ static errcode_t at_ppp_test_uart_performance(uint8_t *para, uint32_t paraLen)
     return ERRCODE_SUCC;
 }
 
+/**
+ * @brief AT^PPPDEBUGSET - 设置PPP调试级别
+ * @param para 参数: 调试级别 (0=NONE, 1=ERROR, 2=WARN, 3=INFO, 4=DEBUG) 或级别名称
+ * @param paraLen 参数长度
+ */
+static errcode_t at_ppp_debug_set_level(uint8_t *para, uint32_t paraLen)
+{
+    if (!para || paraLen == 0) {
+        PRINT("[PPP_ERR]PPP DEBUG SET: Missing debug level parameter\r\n");
+        PRINT("[PPP_INFO]Usage: AT^PPPDEBUGSET=<level>\r\n");
+        PRINT("[PPP_INFO]Levels: 0=NONE, 1=ERROR, 2=WARN, 3=INFO, 4=DEBUG\r\n");
+        PRINT("[PPP_INFO]Or use names: NONE, ERROR, WARN, INFO, DEBUG\r\n");
+        return ERRCODE_FAIL;
+    }
+    
+    // 将参数转换为字符串
+    char level_str[16] = {0};
+    uint32_t copy_len = (paraLen < sizeof(level_str) - 1) ? paraLen : sizeof(level_str) - 1;
+    memcpy(level_str, para, copy_len);
+    level_str[copy_len] = '\0';
+    
+    PRINT("[PPP_INFO]PPP DEBUG SET: Setting debug level to '%s'...\r\n", level_str);
+    
+    ppp_error_t ret = PPP_ERR_OK;
+    
+    // 尝试按数字设置
+    if (level_str[0] >= '0' && level_str[0] <= '4' && level_str[1] == '\0') {
+        int level = level_str[0] - '0';
+        ret = ppp_debug_set_level(level);
+        if (ret == PPP_ERR_OK) {
+            PRINT("[PPP_INFO]PPP DEBUG SET: Debug level set to %d (%s)\r\n", 
+                  level, ppp_debug_get_level_name(level));
+        }
+    } else {
+        // 尝试按名称设置
+        ret = ppp_debug_set_level_by_name(level_str);
+        if (ret == PPP_ERR_OK) {
+            int current_level = ppp_debug_get_level();
+            PRINT("[PPP_INFO]PPP DEBUG SET: Debug level set to %s (%d)\r\n", 
+                  ppp_debug_get_level_name(current_level), current_level);
+        }
+    }
+    
+    if (ret != PPP_ERR_OK) {
+        PRINT("[PPP_ERR]PPP DEBUG SET: Invalid debug level '%s'\r\n", level_str);
+        PRINT("[PPP_INFO]Valid levels: 0=NONE, 1=ERROR, 2=WARN, 3=INFO, 4=DEBUG\r\n");
+        return ERRCODE_FAIL;
+    }
+    
+    return ERRCODE_SUCC;
+}
+
+/**
+ * @brief AT^PPPDEBUGGET - 获取当前PPP调试级别
+ */
+static errcode_t at_ppp_debug_get_level(uint8_t *para, uint32_t paraLen)
+{
+    (void)para;
+    (void)paraLen;
+    
+    int current_level = ppp_debug_get_level();
+    const char *level_name = ppp_debug_get_level_name(current_level);
+    
+    PRINT("[PPP_INFO]PPP DEBUG GET: Current debug level is %d (%s)\r\n", 
+          current_level, level_name);
+    
+    // 显示各级别的含义
+    PRINT("[PPP_INFO]Debug levels:\r\n");
+    PRINT("[PPP_INFO]  0=NONE  - No debug output\r\n");
+    PRINT("[PPP_INFO]  1=ERROR - Error messages only\r\n");
+    PRINT("[PPP_INFO]  2=WARN  - Warnings and errors\r\n");
+    PRINT("[PPP_INFO]  3=INFO  - Info, warnings and errors\r\n");
+    PRINT("[PPP_INFO]  4=DEBUG - All debug messages\r\n");
+    
+    return ERRCODE_SUCC;
+}
+#include "ppp_debug.h"
+/**
+ * @brief AT^PPPDEBUGTEST - 测试各级别调试输出
+ */
+static errcode_t at_ppp_debug_test_output(uint8_t *para, uint32_t paraLen)
+{
+    (void)para;
+    (void)paraLen;
+    
+    int current_level = ppp_debug_get_level();
+    
+    PRINT("[PPP_INFO]PPP DEBUG TEST: Testing debug output at level %d (%s)\r\n", 
+          current_level, ppp_debug_get_level_name(current_level));
+    
+    // 测试各级别输出
+    PPP_DEBUG_ERROR("This is an ERROR message (level 1)");
+    PPP_DEBUG_WARN("This is a WARN message (level 2)");
+    PPP_DEBUG_INFO("This is an INFO message (level 3)");
+    PPP_DEBUG_DEBUG("This is a DEBUG message (level 4)");
+    PPP_DEBUG_CRITICAL("This is a CRITICAL message (always shown)");
+    
+    PRINT("[PPP_INFO]PPP DEBUG TEST: Test completed\r\n");
+    
+    return ERRCODE_SUCC;
+}
+
+/**
+ * @brief AT^PPPDEBUGDEFAULT - 重置调试级别为默认值
+ */
+static errcode_t at_ppp_debug_reset_level(uint8_t *para, uint32_t paraLen)
+{
+    (void)para;
+    (void)paraLen;
+    
+    ppp_error_t ret = ppp_debug_reset_level();
+    if (ret == PPP_ERR_OK) {
+        int current_level = ppp_debug_get_level();
+        PRINT("[PPP_INFO]PPP DEBUG RESET: Debug level reset to default %d (%s)\r\n", 
+              current_level, ppp_debug_get_level_name(current_level));
+        return ERRCODE_SUCC;
+    } else {
+        PRINT("[PPP_ERR]PPP DEBUG RESET: Failed to reset debug level\r\n");
+        return ERRCODE_FAIL;
+    }
+}
+
 static const at_cmd_table_t g_ppp_cmd[] = {
     { (char *)"AT^CAT1POWER",       cat1_power, (char *)"1" },
     { (char *)"AT^CAT1GETSIGNAL",   at_cat1_getsignal, (char *)"1" },
@@ -1642,6 +1765,10 @@ static const at_cmd_table_t g_ppp_cmd[] = {
     { (char *)"AT^WSMULTISTATUS",   at_ws_multi_test_status, (char *)" " },
 #endif
     { (char *)"AT^PPPSIGNALINFO",   at_ppp_get_signal_info, (char *)" " },
+    { (char *)"AT^PPPDEBUGSET",     at_ppp_debug_set_level, (char *)"level" },
+    { (char *)"AT^PPPDEBUGGET",     at_ppp_debug_get_level, (char *)" " },
+    { (char *)"AT^PPPDEBUGTEST",    at_ppp_debug_test_output, (char *)" " },
+    { (char *)"AT^PPPDEBUGDEFAULT",  at_ppp_debug_reset_level, (char *)" " },
 };
 
 at_cmd_table_t *get_ppp_cmd_table(uint32_t *cmd_count)
index 5971ea3..ecf7b4a 100644 (file)
@@ -18,37 +18,40 @@ extern "C" {
 
 #if PPP_DEBUG_ENABLE
 
+/* 前向声明动态调试级别函数 */
+extern int ppp_debug_get_level(void);
+
 #define PPP_DEBUG_ERROR(fmt, ...) \
     do { \
-        if (PPP_DEBUG_LEVEL >= PPP_DEBUG_LEVEL_ERROR) { \
+        if (ppp_debug_get_level() >= PPP_DEBUG_LEVEL_ERROR) { \
             PRINT("[PPP ERROR] " fmt "\r\n", ##__VA_ARGS__); \
         } \
     } while(0)
 
 #define PPP_DEBUG_WARN(fmt, ...) \
     do { \
-        if (PPP_DEBUG_LEVEL >= PPP_DEBUG_LEVEL_WARN) { \
+        if (ppp_debug_get_level() >= PPP_DEBUG_LEVEL_WARN) { \
             PRINT("[PPP WARN] " fmt "\r\n", ##__VA_ARGS__); \
         } \
     } while(0)
 
 #define PPP_DEBUG_INFO(fmt, ...) \
     do { \
-        if (PPP_DEBUG_LEVEL >= PPP_DEBUG_LEVEL_INFO) { \
+        if (ppp_debug_get_level() >= PPP_DEBUG_LEVEL_INFO) { \
             PRINT("[PPP INFO] " fmt "\r\n", ##__VA_ARGS__); \
         } \
     } while(0)
 
 #define PPP_DEBUG_DEBUG(fmt, ...) \
     do { \
-        if (PPP_DEBUG_LEVEL >= PPP_DEBUG_LEVEL_DEBUG) { \
+        if (ppp_debug_get_level() >= PPP_DEBUG_LEVEL_DEBUG) { \
             PRINT("[PPP DEBUG] " fmt "\r\n", ##__VA_ARGS__); \
         } \
     } while(0)
 
 #define PPP_DEBUG_HEXDUMP(data, len) \
     do { \
-        if (PPP_DEBUG_LEVEL >= PPP_DEBUG_LEVEL_DEBUG) { \
+        if (ppp_debug_get_level() >= PPP_DEBUG_LEVEL_DEBUG) { \
             ppp_debug_hexdump(data, len); \
         } \
     } while(0)
@@ -56,7 +59,7 @@ extern "C" {
 /* 兼容性宏 - 用于旧代码 */
 #define PPP_DEBUG_PRINT(fmt, ...) \
     do { \
-    if (PPP_DEBUG_LEVEL >= PPP_DEBUG_LEVEL_INFO) { \
+    if (ppp_debug_get_level() >= PPP_DEBUG_LEVEL_INFO) { \
         print_str(fmt, ##__VA_ARGS__); \
     } \
     } while(0)  
diff --git a/code/sdk/zhihui/ppp/include/ppp_debug_manager.h b/code/sdk/zhihui/ppp/include/ppp_debug_manager.h
new file mode 100644 (file)
index 0000000..a92f872
--- /dev/null
@@ -0,0 +1,61 @@
+/**
+ * @file ppp_debug_manager.h
+ * @brief PPP调试级别动态管理模块头文件
+ */
+
+#ifndef PPP_DEBUG_MANAGER_H
+#define PPP_DEBUG_MANAGER_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdint.h>
+#include <stdbool.h>
+#include "ppp_types.h"
+
+/**
+ * @brief 获取当前动态调试级别
+ * @return 当前调试级别
+ */
+int ppp_debug_get_level(void);
+
+/**
+ * @brief 设置动态调试级别
+ * @param level 调试级别 (0-4)
+ * @return PPP_ERR_OK-成功,其他-失败
+ */
+ppp_error_t ppp_debug_set_level(int level);
+
+/**
+ * @brief 获取调试级别名称
+ * @param level 调试级别
+ * @return 级别名称字符串
+ */
+const char* ppp_debug_get_level_name(int level);
+
+/**
+ * @brief 根据名称设置调试级别
+ * @param level_name 级别名称 ("NONE", "ERROR", "WARN", "INFO", "DEBUG")
+ * @return PPP_ERR_OK-成功,其他-失败
+ */
+ppp_error_t ppp_debug_set_level_by_name(const char* level_name);
+
+/**
+ * @brief 重置调试级别为默认值
+ * @return PPP_ERR_OK-成功
+ */
+ppp_error_t ppp_debug_reset_level(void);
+
+/**
+ * @brief 检查指定级别是否启用
+ * @param level 要检查的级别
+ * @return true-启用,false-未启用
+ */
+bool ppp_debug_is_level_enabled(int level);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* PPP_DEBUG_MANAGER_H */ 
\ No newline at end of file
diff --git a/code/sdk/zhihui/ppp/src/ppp_debug_manager.c b/code/sdk/zhihui/ppp/src/ppp_debug_manager.c
new file mode 100644 (file)
index 0000000..afbc2f1
--- /dev/null
@@ -0,0 +1,101 @@
+/**
+ * @file ppp_debug_manager.c
+ * @brief PPP调试级别动态管理模块
+ * 
+ * 提供运行时动态调整PPP调试输出级别的功能
+ */
+
+#include "ppp_debug_manager.h"
+#include "ppp_debug.h"
+#include "ppp_config.h"
+#include <string.h>
+
+/* 全局调试级别变量 */
+static volatile int g_ppp_dynamic_debug_level = PPP_DEBUG_LEVEL;
+
+/**
+ * @brief 获取当前动态调试级别
+ * @return 当前调试级别
+ */
+int ppp_debug_get_level(void)
+{
+    return g_ppp_dynamic_debug_level;
+}
+
+/**
+ * @brief 设置动态调试级别
+ * @param level 调试级别 (0-4)
+ * @return PPP_ERR_OK-成功,其他-失败
+ */
+ppp_error_t ppp_debug_set_level(int level)
+{
+    if (level < PPP_DEBUG_LEVEL_NONE || level > PPP_DEBUG_LEVEL_DEBUG) {
+        return PPP_ERR_INVALID_PARAM;
+    }
+    
+    g_ppp_dynamic_debug_level = level;
+    return PPP_ERR_OK;
+}
+
+/**
+ * @brief 获取调试级别名称
+ * @param level 调试级别
+ * @return 级别名称字符串
+ */
+const char* ppp_debug_get_level_name(int level)
+{
+    switch (level) {
+        case PPP_DEBUG_LEVEL_NONE:  return "NONE";
+        case PPP_DEBUG_LEVEL_ERROR: return "ERROR";
+        case PPP_DEBUG_LEVEL_WARN:  return "WARN";
+        case PPP_DEBUG_LEVEL_INFO:  return "INFO";
+        case PPP_DEBUG_LEVEL_DEBUG: return "DEBUG";
+        default: return "UNKNOWN";
+    }
+}
+
+/**
+ * @brief 根据名称设置调试级别
+ * @param level_name 级别名称 ("NONE", "ERROR", "WARN", "INFO", "DEBUG")
+ * @return PPP_ERR_OK-成功,其他-失败
+ */
+ppp_error_t ppp_debug_set_level_by_name(const char* level_name)
+{
+    if (!level_name) {
+        return PPP_ERR_INVALID_PARAM;
+    }
+    
+    if (strcasecmp(level_name, "NONE") == 0) {
+        return ppp_debug_set_level(PPP_DEBUG_LEVEL_NONE);
+    } else if (strcasecmp(level_name, "ERROR") == 0) {
+        return ppp_debug_set_level(PPP_DEBUG_LEVEL_ERROR);
+    } else if (strcasecmp(level_name, "WARN") == 0) {
+        return ppp_debug_set_level(PPP_DEBUG_LEVEL_WARN);
+    } else if (strcasecmp(level_name, "INFO") == 0) {
+        return ppp_debug_set_level(PPP_DEBUG_LEVEL_INFO);
+    } else if (strcasecmp(level_name, "DEBUG") == 0) {
+        return ppp_debug_set_level(PPP_DEBUG_LEVEL_DEBUG);
+    } else {
+        return PPP_ERR_INVALID_PARAM;
+    }
+}
+
+/**
+ * @brief 重置调试级别为默认值
+ * @return PPP_ERR_OK-成功
+ */
+ppp_error_t ppp_debug_reset_level(void)
+{
+    g_ppp_dynamic_debug_level = PPP_DEBUG_LEVEL;
+    return PPP_ERR_OK;
+}
+
+/**
+ * @brief 检查指定级别是否启用
+ * @param level 要检查的级别
+ * @return true-启用,false-未启用
+ */
+bool ppp_debug_is_level_enabled(int level)
+{
+    return g_ppp_dynamic_debug_level >= level;
+} 
\ No newline at end of file