#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);
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