ProtoCore v0.0.1
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
ntp_service.cpp File Reference
#include "ntp_service.h"
#include "shared_primitives/http_date.h"
#include <time.h>

Go to the source code of this file.

Functions

void pc_ntp_set_test_epoch (time_t epoch)
 
bool pc_ntp_begin (const char *tz, const char *server1, const char *server2)
 Start the SNTP client.
 
bool pc_ntp_synced ()
 True once a plausible wall-clock time has been obtained from SNTP.
 
time_t pc_ntp_epoch ()
 Current Unix epoch seconds, or 0 if not yet synced (or disabled).
 
size_t pc_ntp_http_date (char *out, size_t out_cap)
 Format the current time as an RFC 7231 IMF-fixdate (HTTP Date).
 
uint32_t pc_ntp_time_source (void)
 NTP as a time source for the multi-source registry (services/timing_position/time_source).
 

Function Documentation

◆ pc_ntp_set_test_epoch()

void pc_ntp_set_test_epoch ( time_t  epoch)

Definition at line 58 of file ntp_service.cpp.

◆ pc_ntp_begin()

bool pc_ntp_begin ( const char *  tz = nullptr,
const char *  server1 = "pool.ntp.org",
const char *  server2 = "time.nist.gov" 
)

Start the SNTP client.

Returns immediately; the first sync arrives asynchronously (poll pc_ntp_synced()). Call once after the WiFi link is up.

Parameters
tzPOSIX TZ string (e.g. "UTC0", "EST5EDT,M3.2.0,M11.1.0"). Pass nullptr for UTC.
server1Primary NTP server (default "pool.ntp.org").
server2Secondary NTP server (default "time.nist.gov").
Returns
true if the client was started; false if disabled at compile time.

Definition at line 63 of file ntp_service.cpp.

◆ pc_ntp_synced()

bool pc_ntp_synced ( )

True once a plausible wall-clock time has been obtained from SNTP.

Checks that the system clock has advanced past 2021-01-01, which only happens after a successful sync (the RTC starts at the epoch on a cold boot).

Definition at line 70 of file ntp_service.cpp.

◆ pc_ntp_epoch()

time_t pc_ntp_epoch ( )

Current Unix epoch seconds, or 0 if not yet synced (or disabled).

Definition at line 74 of file ntp_service.cpp.

Referenced by pc_ntp_http_date(), and pc_ntp_time_source().

◆ pc_ntp_http_date()

size_t pc_ntp_http_date ( char *  out,
size_t  out_cap 
)

Format the current time as an RFC 7231 IMF-fixdate (HTTP Date).

Writes e.g. "Sun, 06 Nov 1994 08:49:37 GMT" into out. Always GMT.

Parameters
outDestination buffer (>= 30 bytes recommended).
out_capCapacity of out.
Returns
Number of characters written (excluding the null), or 0 if time is not yet available / disabled.

Definition at line 78 of file ntp_service.cpp.

References pc_http_date(), and pc_ntp_epoch().

◆ pc_ntp_time_source()

uint32_t pc_ntp_time_source ( void  )

NTP as a time source for the multi-source registry (services/timing_position/time_source).

Register with pc_time_source_add("ntp", priority, pc_ntp_time_source) so the aggregated pc_time_now() (and thus the HTTP Date header when PC_ENABLE_TIME_SOURCE is set) can be fed by NTP alongside an RTC / GPS. Returns the current epoch, or 0 when not synced.

Definition at line 88 of file ntp_service.cpp.

References pc_ntp_epoch().