| 150 | } |
| 151 | |
| 152 | bool HalClock::syncFromNTP() { |
| 153 | if (!_available) return false; |
| 154 | |
| 155 | if (WiFi.status() != WL_CONNECTED) { |
| 156 | LOG_ERR("CLK", "WiFi not connected, cannot sync NTP"); |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | LOG_INF("CLK", "Starting NTP sync..."); |
| 161 | configTzTime("UTC0", "pool.ntp.org", "time.nist.gov"); |
| 162 | |
| 163 | // Wait for SNTP sync to complete (up to 5 seconds) |
| 164 | constexpr int maxAttempts = 50; |
| 165 | for (int i = 0; i < maxAttempts; i++) { |
| 166 | if (sntp_get_sync_status() == SNTP_SYNC_STATUS_COMPLETED) { |
| 167 | time_t now = time(nullptr); |
| 168 | struct tm timeinfo; |
| 169 | gmtime_r(&now, &timeinfo); |
| 170 | |
| 171 | if (writeTimeToRTC(timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec)) { |
| 172 | LOG_INF("CLK", "RTC set to %02d:%02d:%02d UTC", timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec); |
| 173 | return true; |
| 174 | } |
| 175 | return false; |
| 176 | } |
| 177 | delay(100); |
| 178 | } |
| 179 | |
| 180 | LOG_ERR("CLK", "NTP sync timed out"); |
| 181 | return false; |
| 182 | } |
no outgoing calls
no test coverage detected