Ignore lookup errors on sync request unless all lookups fail (#2298)

This commit is contained in:
Philip Gladstone 2018-03-21 02:02:25 -04:00 committed by Marcel Stör
parent 96601b6894
commit c7006dfb27
2 changed files with 21 additions and 10 deletions

View File

@ -386,14 +386,13 @@ static void sntp_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
if (ipaddr == NULL) if (ipaddr == NULL)
{ {
sntp_dbg("DNS Fail!\n"); sntp_dbg("DNS Fail!\n");
task_post_low(tasknumber, (uint32_t) name);
} }
else else
{ {
serverp[server_count] = *ipaddr; serverp[server_count] = *ipaddr;
server_count++; server_count++;
task_post_low(tasknumber, SNTP_DOLOOKUPS_ID);
} }
task_post_low(tasknumber, SNTP_DOLOOKUPS_ID);
} }
@ -621,15 +620,26 @@ static void sntp_dolookups (lua_State *L) {
// at the end, start the lookups. If we have already looked everything up, // at the end, start the lookups. If we have already looked everything up,
// then move straight to sending the packets. // then move straight to sending the packets.
if (state->list_ref == LUA_NOREF) { if (state->list_ref == LUA_NOREF) {
sntp_dosend(L); sntp_dosend();
return; return;
} }
lua_rawgeti(L, LUA_REGISTRYINDEX, state->list_ref); lua_rawgeti(L, LUA_REGISTRYINDEX, state->list_ref);
while (1) { while (1) {
int l;
if (lua_objlen(L, -1) <= state->lookup_pos) { if (lua_objlen(L, -1) <= state->lookup_pos) {
// We reached the end // We reached the end
sntp_dosend(L); if (server_count == 0) {
// Oh dear -- no valid entries -- generate an error
// This means that all the arguments are invalid. Just pick the first
lua_rawgeti(L, -1, 1);
const char *hostname = luaL_checklstring(L, -1, &l);
handle_error(L, NTP_DNS_ERR, hostname);
lua_pop(L, 1);
} else {
sntp_dosend();
}
break; break;
} }
@ -637,8 +647,6 @@ static void sntp_dolookups (lua_State *L) {
lua_rawgeti(L, -1, state->lookup_pos); lua_rawgeti(L, -1, state->lookup_pos);
int l;
const char *hostname = luaL_checklstring(L, -1, &l); const char *hostname = luaL_checklstring(L, -1, &l);
lua_pop(L, 1); lua_pop(L, 1);
@ -806,7 +814,7 @@ static int sntp_sync (lua_State *L)
goto good_ret; goto good_ret;
} }
sntp_dosend (L); sntp_dosend ();
good_ret: good_ret:
if (!lua_isnoneornil(L, 4)) { if (!lua_isnoneornil(L, 4)) {
@ -837,10 +845,8 @@ static void sntp_task(os_param_t param, uint8_t prio)
sntp_handle_result(L); sntp_handle_result(L);
} else if (param == SNTP_DOLOOKUPS_ID) { } else if (param == SNTP_DOLOOKUPS_ID) {
sntp_dolookups(L); sntp_dolookups(L);
} else if (param >= 0 && param <= NTP_MAX_ERR_ID) {
handle_error(L, param, NULL);
} else { } else {
handle_error(L, NTP_DNS_ERR, (const char *) param); handle_error(L, param, NULL);
} }
} }

View File

@ -16,6 +16,11 @@ Attempts to obtain time synchronization.
For best results you may want to to call this periodically in order to compensate for internal clock drift. As stated in the [rtctime](rtctime.md) module documentation it's advisable to sync time after deep sleep and it's necessary to sync after module reset (add it to [`init.lua`](../upload.md#initlua) after WiFi initialization). For best results you may want to to call this periodically in order to compensate for internal clock drift. As stated in the [rtctime](rtctime.md) module documentation it's advisable to sync time after deep sleep and it's necessary to sync after module reset (add it to [`init.lua`](../upload.md#initlua) after WiFi initialization).
Note that either a single server can be provided as an argument (name or address), or a list (table) of servers can be provided. Note that either a single server can be provided as an argument (name or address), or a list (table) of servers can be provided.
If *all* of the supplied host names/addresses are invalid, then the error callback will be called with argument type 1. Otherwise, if
there is at least one valid name/address, then then sync will be performed.
If any sync operation fails (maybe the device is disconnected from the internet), then all the names will be looked up again.
#### Syntax #### Syntax
`sntp.sync([server_ip], [callback], [errcallback], [autorepeat])` `sntp.sync([server_ip], [callback], [errcallback], [autorepeat])`
`sntp.sync({ server1, server2, .. }, [callback], [errcallback], [autorepeat])` `sntp.sync({ server1, server2, .. }, [callback], [errcallback], [autorepeat])`