Commit 2762d018 authored by 957dd's avatar 957dd

加入了大窝船,修改了部分代码,重写了网络重连机制,多种保底

parent ffa4cc2c
......@@ -174,13 +174,89 @@ static void check_and_install_dependencies() {
}
}
// 检测是否有互联网连接(多目标探测,任意一个成功即视为有网)
// HTTP 204 保底探测:用于 ping 被屏蔽时的应用层联网判断
static int has_http_204_connection() {
// 先尝试 curl
if (is_command_available("curl")) {
int ret = system("test \"$(curl -sL --max-time 3 -o /dev/null -w '%{http_code}' https://connectivitycheck.gstatic.com/generate_204)\" = \"204\"");
if (ret == 0) {
my_zlog_debug("网络探测成功: HTTP 204 (curl, https)");
return 1;
}
// 备用 HTTP,适配部分 HTTPS 证书链异常场景
ret = system("test \"$(curl -sL --max-time 3 -o /dev/null -w '%{http_code}' http://connectivitycheck.gstatic.com/generate_204)\" = \"204\"");
if (ret == 0) {
my_zlog_debug("网络探测成功: HTTP 204 (curl, http)");
return 1;
}
}
// curl 不可用时,使用 wget 做兜底探测
if (is_command_available("wget")) {
int ret = system("wget -q --timeout=3 --spider https://connectivitycheck.gstatic.com/generate_204");
if (ret == 0) {
my_zlog_debug("网络探测成功: HTTP 204 (wget)");
return 1;
}
}
return 0;
}
// 业务接口保底探测:请求 getConfig,若返回 data 非空则视为联网可用
static int has_business_api_connection() {
const char *device_no_raw = device_id_function();
if (device_no_raw == NULL || device_no_raw[0] == '\0') {
return 0;
}
// 设备号文件可能带换行,先清理
char device_no[64];
snprintf(device_no, sizeof(device_no), "%s", device_no_raw);
device_no[strcspn(device_no, "\r\n")] = '\0';
if (device_no[0] == '\0') {
return 0;
}
if (is_command_available("curl")) {
char cmd[512];
snprintf(cmd, sizeof(cmd),
"curl -sL --max-time 3 \"https://fcrs-api.yd-ss.com/device/getConfig?deviceNo=%s\"",
device_no);
FILE *fp = popen(cmd, "r");
if (fp != NULL) {
char resp[1024];
size_t n = fread(resp, 1, sizeof(resp) - 1, fp);
resp[n] = '\0';
pclose(fp);
cJSON *json = cJSON_Parse(resp);
if (json != NULL) {
cJSON *data = cJSON_GetObjectItemCaseSensitive(json, "data");
if (data != NULL && !cJSON_IsNull(data)) {
my_zlog_debug("网络探测成功: business api");
cJSON_Delete(json);
return 1;
}
cJSON_Delete(json);
}
}
}
return 0;
}
// 检测是否有互联网连接(多目标 ping + HTTP 204 保底,任意成功即视为有网)
static int has_internet_connection() {
static const char *targets[] = {
"baidu.com", // 国内常见目标
"google.com", // 海外常见目标
"mqtt177.controlmelive.com", // 业务域名探测
"47.83.167.0", // 业务 IP 探测(不依赖 DNS)
"1.1.1.1", // Cloudflare DNS(不依赖域名解析)
"8.8.8.8" // Google DNS(不依赖域名解析)
"8.8.8.8", // Google DNS(不依赖域名解析)
};
// -c 1: 发送 1 个包, -W 2: 超时 2 秒
......@@ -192,7 +268,12 @@ static int has_internet_connection() {
return 1; // 任意一个成功,直接返回
}
}
return 0;
// ping 全失败时,尝试应用层保底探测
if (has_http_204_connection()) {
return 1;
}
return has_business_api_connection();
}
// 尝试连接 WiFi
......
No preview for this file type
......@@ -9,4 +9,4 @@ file perms = 600
millisecond = "%d(%Y-%m-%d %H:%M:%S).%ms [%V] %m%n"
[rules]
my_log.* "/home/orangepi/car/master/log/log_2026-04-03.log"; millisecond
my_log.* "/home/orangepi/car/master/log/log_2026-04-08.log"; millisecond
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment