Skip to content

Commit 76b8945

Browse files
authored
Merge pull request #30 from CHERIoT-Platform/hlefeuvre/fix-sntp-issues
Make SNTP connection failure (more) reliable.
2 parents 8aefa6e + 8387659 commit 76b8945

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

examples/01.SNTP/sntp.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ void __cheri_compartment("sntp_example") example()
1919
{
2020
Debug::log("Failed to update NTP time");
2121
Timeout oneSecond{MS_TO_TICKS(1000)};
22+
t = Timeout{MS_TO_TICKS(5000)};
2223
}
2324
Debug::log("Updating NTP took {} ticks", t.elapsed);
2425
t = UnlimitedTimeout;

examples/03.HTTPS/https.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ void __cheri_compartment("https_example") example()
3232
{
3333
Debug::log("Failed to update NTP time");
3434
Timeout oneSecond{MS_TO_TICKS(1000)};
35+
t = Timeout{MS_TO_TICKS(5000)};
3536
}
3637
Debug::log("Updating NTP took {} ticks", t.elapsed);
3738
t = UnlimitedTimeout;

examples/04.MQTT/mqtt.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ void __cheri_compartment("mqtt_example") example()
9696
{
9797
Debug::log("Failed to update NTP time");
9898
Timeout oneSecond{MS_TO_TICKS(1000)};
99+
t = Timeout{MS_TO_TICKS(5000)};
99100
}
100101
Debug::log("Updating NTP took {} ticks", t.elapsed);
101102
t = UnlimitedTimeout;

lib/sntp/sntp.cc

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ namespace
410410
SntpStatus_t lastStatus = SntpSuccess;
411411
do
412412
{
413+
// `timeout` is updated through `context`.
413414
status = Sntp_ReceiveTimeResponse(&context, 0);
414415
if (status != lastStatus)
415416
{
@@ -423,8 +424,21 @@ namespace
423424
thread_sleep(&t);
424425
timeout->elapse(t.elapsed);
425426
}
426-
} while (status == SntpNoResponseReceived);
427-
Debug::log("Received new time fron NTP!");
427+
} while (status == SntpNoResponseReceived &&
428+
timeout->may_block());
429+
430+
if (status != SntpSuccess)
431+
{
432+
Debug::log("Failed to receive SNTP time response: {}",
433+
status);
434+
Timeout t{UnlimitedTimeout};
435+
network_socket_close(
436+
&t, MALLOC_CAPABILITY, udpContext.socket);
437+
timeout->elapse(t.elapsed);
438+
return ntp_error_to_errno(status);
439+
}
440+
441+
Debug::log("Received new time from NTP!");
428442
}
429443
else
430444
{

0 commit comments

Comments
 (0)