mirror of
https://github.com/oopuuu/zTC1.git
synced 2025-12-12 21:18:13 +08:00
解决没网获取时间崩溃
This commit is contained in:
@@ -318,8 +318,9 @@ void mqtt_client_thread(mico_thread_arg_t arg)
|
||||
|
||||
rc = NewNetwork(&n, MQTT_SERVER, MQTT_SERVER_PORT, ssl_settings);
|
||||
if (rc == MQTT_SUCCESS) break;
|
||||
mqtt_log("ERROR: MQTT network connection err=%d, reconnect after 3s...", rc);
|
||||
|
||||
//暂时去掉这个日志
|
||||
//mqtt_log("ERROR: MQTT network connection err=%d, reconnect after 3s...", rc);
|
||||
}
|
||||
mqtt_log("MQTT network connection success!");
|
||||
|
||||
|
||||
@@ -20,62 +20,48 @@ OSStatus user_sntp_get_time()
|
||||
char ** pptr = NULL;
|
||||
struct in_addr ipp;
|
||||
|
||||
// mico_rtc_time_t rtc_time;
|
||||
|
||||
hostent_content = gethostbyname("pool.ntp.org");
|
||||
pptr = hostent_content->h_addr_list;
|
||||
ipp.s_addr = 0xd248912c;
|
||||
err = sntp_get_time(&ipp, ¤t_time);
|
||||
if (err != kNoErr)
|
||||
{
|
||||
os_log("sntp_get_time err = %d.", err);
|
||||
ipp.s_addr = *(uint32_t *) (*pptr);
|
||||
err = sntp_get_time(&ipp, ¤t_time);
|
||||
}
|
||||
if (err != kNoErr)
|
||||
{
|
||||
os_log("sntp_get_time0 err = %d.", err);
|
||||
hostent_content = gethostbyname("cn.ntp.org.cn");
|
||||
pptr = hostent_content->h_addr_list;
|
||||
ipp.s_addr = *(uint32_t *) (*pptr);
|
||||
err = sntp_get_time(&ipp, ¤t_time);
|
||||
}
|
||||
if (err != kNoErr)
|
||||
{
|
||||
os_log("sntp_get_time1 err = %d.", err);
|
||||
hostent_content = gethostbyname("cn.pool.ntp.org");
|
||||
pptr = hostent_content->h_addr_list;
|
||||
ipp.s_addr = *(uint32_t *) (*pptr);
|
||||
err = sntp_get_time(&ipp, ¤t_time);
|
||||
}
|
||||
if (err != kNoErr)
|
||||
{
|
||||
os_log("sntp_get_time2 err = %d.", err);
|
||||
hostent_content = gethostbyname("s1a.time.edu.cn");
|
||||
pptr = hostent_content->h_addr_list;
|
||||
ipp.s_addr = *(uint32_t *) (*pptr);
|
||||
err = sntp_get_time(&ipp, ¤t_time);
|
||||
}
|
||||
if (err != kNoErr)
|
||||
{
|
||||
os_log("sntp_get_time3 err = %d.", err);
|
||||
hostent_content = gethostbyname("ntp.sjtu.edu.cn");
|
||||
pptr = hostent_content->h_addr_list;
|
||||
ipp.s_addr = *(uint32_t *) (*pptr);
|
||||
err = sntp_get_time(&ipp, ¤t_time);
|
||||
}
|
||||
|
||||
if (err != kNoErr)
|
||||
{
|
||||
int ntp_count = 5;
|
||||
char* ntp_hosts[5] = {
|
||||
"pool.ntp.org",
|
||||
"cn.ntp.org.cn",
|
||||
"cn.pool.ntp.org",
|
||||
"s1a.time.edu.cn",
|
||||
"ntp.sjtu.edu.cn",
|
||||
};
|
||||
|
||||
int i = 0;
|
||||
for (; i < ntp_count; i++)
|
||||
{
|
||||
hostent_content = gethostbyname(ntp_hosts[i]);
|
||||
if (hostent_content == NULL)
|
||||
{
|
||||
os_log("gethostbyname(%s)", ntp_hosts[i]);
|
||||
continue;
|
||||
}
|
||||
pptr = hostent_content->h_addr_list;
|
||||
ipp.s_addr = *(uint32_t *)(*pptr);
|
||||
err = sntp_get_time(&ipp, ¤t_time);
|
||||
if (err == kNoErr)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (err != kNoErr)
|
||||
{
|
||||
os_log("sntp_get_time4 err[%d]", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
mico_utc_time_ms_t utc_time_ms = (uint64_t)current_time.seconds * (uint64_t)1000
|
||||
+ (current_time.microseconds / 1000);
|
||||
mico_time_set_utc_time_ms(&utc_time_ms);
|
||||
}
|
||||
else
|
||||
{
|
||||
os_log("sntp_get_time4 err = %d.", err);
|
||||
return err;
|
||||
}
|
||||
return kNoErr;
|
||||
}
|
||||
|
||||
|
||||
@@ -76,7 +76,7 @@ void WifiScanCallback(ScanResult_adv* scan_ret, void* arg)
|
||||
int i = 0;
|
||||
wifi_ret = malloc(sizeof(char)*count * (32 + 2) + 50);
|
||||
char* ssids = malloc(sizeof(char)*count * 32);
|
||||
char* secs = malloc(sizeof(char)*count * 2);
|
||||
char* secs = malloc(sizeof(char)*count * 2 + 1);
|
||||
char* tmp1 = ssids;
|
||||
char* tmp2 = secs;
|
||||
for (; i < count; i++)
|
||||
@@ -85,7 +85,7 @@ void WifiScanCallback(ScanResult_adv* scan_ret, void* arg)
|
||||
if (strstr(ssid, "'") || strstr(ssid, "\"")) continue;
|
||||
sprintf(tmp1, "'%s',", ssid);
|
||||
tmp1 += (strlen(ssid) + 3);
|
||||
sprintf(tmp2, "%d,", scan_ret->ApList[i].security);
|
||||
sprintf(tmp2, "%d,", scan_ret->ApList[i].security%10);
|
||||
tmp2 += 2;
|
||||
}
|
||||
*(--tmp1) = 0;
|
||||
|
||||
Reference in New Issue
Block a user