| 34 | } |
| 35 | |
| 36 | void NtpClient::requestTime() |
| 37 | { |
| 38 | debug_d("NtpClient::requestTime()"); |
| 39 | |
| 40 | // Schedule a retry in anticipation of failure |
| 41 | startTimer(NTP_CONNECTION_TIMEOUT_MS); |
| 42 | |
| 43 | if(!WifiStation.isConnected()) { |
| 44 | debugf("NtpClient waiting for connection..."); |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | ip_addr_t resolvedIp; |
| 49 | int result = dns_gethostbyname( |
| 50 | server.c_str(), &resolvedIp, |
| 51 | [](const char*, LWIP_IP_ADDR_T* ip, void* arg) { |
| 52 | // We do a new request since the last one was never done. |
| 53 | if(ip) |
| 54 | reinterpret_cast<NtpClient*>(arg)->internalRequestTime(*ip); |
| 55 | }, |
| 56 | this); |
| 57 | |
| 58 | debug_d("dns_gethostbyname() returned %d", result); |
| 59 | |
| 60 | switch(result) { |
| 61 | case ERR_OK: |
| 62 | // Documentation says this will be the result if the string is already |
| 63 | // an ip address in dotted decimal form or if the host is found in dns cache. |
| 64 | // however I'm not sure if the dns cache is working since this never seems to |
| 65 | // be called for a host lookup other than an ip address. |
| 66 | // Doesn't really matter since the lookup will be fast anyways, the host |
| 67 | // is most likely found in the dns cache of the next node the query is sent to. |
| 68 | internalRequestTime(resolvedIp); |
| 69 | break; |
| 70 | case ERR_INPROGRESS: |
| 71 | // currently finding ip, internalRequestTime() will be called when its found. |
| 72 | debug_d("DNS IP lookup in progress..."); |
| 73 | break; |
| 74 | default: |
| 75 | debug_d("DNS lookup error occurred."); |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void NtpClient::internalRequestTime(IpAddress serverIp) |
| 81 | { |
no test coverage detected