增加连接外网dns的接口
authorCaisking <Casiking@zhihuishixun.com>
Sat, 27 Sep 2025 09:39:50 +0000 (17:39 +0800)
committerCaisking <Casiking@zhihuishixun.com>
Sat, 27 Sep 2025 09:39:50 +0000 (17:39 +0800)
code/sdk/zhihui/leiniao/imsl_ln/ln_api.c
code/sdk/zhihui/leiniao/imsl_ln/ln_api.h

index 010d26f..2297ea1 100644 (file)
 #include "audio_stream_player.h"
 #include "custom_utils.h"
 #include "ai_media.h"
+/* for DNS resolution */
+#include "lwip/netdb.h"
+#include "lwip/sockets.h"
+#include "lwip/inet.h"
 
 // 声明播放器消息处理函数
 extern void ln_story_player_simple_handle_message(int cmd, char *data, uint32_t len);
@@ -1705,4 +1709,91 @@ errcode_t uapi_ln_http_server_init(void)
 
     return ERRCODE_SUCC;
 }
+
+/* ============================ 网络连接检测函数 ============================ */
+
+/**
+ * @brief 检测DNS解析是否正常(判断是否有外网连接)
+ * @param hostname 要解析的主机名
+ * @return 1: 解析成功(有外网) 0: 解析失败(无外网) -1: 参数错误
+ */
+int uapi_ln_check_dns_resolution(const char *hostname)
+{
+    if (hostname == NULL || strlen(hostname) == 0) {
+        printf("[LN_NET] Error: hostname is NULL or empty\n");
+        return -1;
+    }
+    
+    printf("[LN_NET] Checking DNS resolution for: %s\n", hostname);
+    
+    struct hostent *host_entry = NULL;
+    
+    // 使用lwip_gethostbyname进行DNS解析
+    host_entry = lwip_gethostbyname(hostname);
+    
+    if (host_entry == NULL) {
+        printf("[LN_NET] DNS resolution failed for: %s\n", hostname);
+        return 0; // 解析失败,无外网
+    }
+    
+    printf("[LN_NET] DNS resolution successful for: %s\n", hostname);
+    printf("[LN_NET] Host name: %s\n", host_entry->h_name);
+    
+    // 打印解析到的IP地址
+    if (host_entry->h_addr_list && host_entry->h_addr_list[0]) {
+        struct in_addr addr;
+        memcpy(&addr, host_entry->h_addr_list[0], sizeof(addr));
+        printf("[LN_NET] IP address: %s\n", inet_ntoa(addr));
+    }
+    
+    return 1; // 解析成功,有外网
+}
+
+/**
+ * @brief 检测是否有外网连接
+ * 通过尝试解析多个公共DNS服务器来判断网络连接状态
+ * @return 1: 有外网连接 0: 无外网连接 -1: 检测失败
+ */
+int uapi_ln_check_internet_connection(void)
+{
+    printf("[LN_NET] Checking internet connection...\n");
+    
+    // 定义要测试的公共DNS服务器列表
+    const char *test_hosts[] = {
+        "223.5.5.5",         // 阿里DNS
+        "183.2.172.17",     // 百度
+    };
+    
+    int test_count = sizeof(test_hosts) / sizeof(test_hosts[0]);
+    int success_count = 0;
+    
+    for (int i = 0; i < test_count; i++) {
+        printf("[LN_NET] Testing host %d/%d: %s\n", i + 1, test_count, test_hosts[i]);
+        
+        int result = uapi_ln_check_dns_resolution(test_hosts[i]);
+        if (result == 1) {
+            success_count++;
+            printf("[LN_NET] Host %s resolved successfully\n", test_hosts[i]);
+        } else if (result == 0) {
+            printf("[LN_NET] Host %s resolution failed\n", test_hosts[i]);
+        } else {
+            printf("[LN_NET] Host %s test error\n", test_hosts[i]);
+        }
+        
+        // 如果已经有成功的解析,可以提前返回
+        if (success_count > 0) {
+            break;
+        }
+    }
+    
+    if (success_count > 0) {
+        printf("[LN_NET] Internet connection detected (%d/%d hosts resolved)\n", 
+               success_count, test_count);
+        return 1; // 有外网连接
+    } else {
+        printf("[LN_NET] No internet connection detected (0/%d hosts resolved)\n", test_count);
+        return 0; // 无外网连接
+    }
+}
+
 /* ============================ 队列相关代码 End ============================ */
\ No newline at end of file
index 11195b4..98a3dd6 100644 (file)
@@ -216,6 +216,10 @@ const char *uapi_ln_get_user_id(void);
 /*文件下载函数:从URL下载到指定路径*/
 int uapi_ln_download_file(const char *url, const char *save_path);
 
+/*网络连接检测函数*/
+int uapi_ln_check_internet_connection(void);
+int uapi_ln_check_dns_resolution(const char *hostname);
+
 /* ============================== 共享全局状态(跨文件) ============================== */
 extern char *g_device_token;
 extern int g_ln_last_update_type;