Skip to content

File syn_sntp.h

FileList > net > syn_sntp.h

Go to the source code of this file

SNTP client — lightweight time synchronization over UDP. More...

  • #include "../common/syn_defs.h"
  • #include "../dsp/syn_filter.h"
  • #include "../port/syn_port_socket.h"
  • #include "../pt/syn_pt.h"
  • #include "../sched/syn_task.h"
  • #include "../util/syn_backoff.h"
  • #include <stdbool.h>

Classes

Type Name
struct SYN_SNTP
SNTP client context — caller-owned.

Public Functions

Type Name
int32_t syn_sntp_get_drift_ppm (const SYN_SNTP * sntp)
Get the calculated hardware clock drift in Parts-Per-Million (PPM).
uint32_t syn_sntp_get_epoch_ns (const SYN_SNTP * sntp)
Get current nanosecond component of the timestamp.
uint32_t syn_sntp_get_epoch_s (const SYN_SNTP * sntp)
Get current UTC epoch in seconds.
void syn_sntp_init (SYN_SNTP * sntp, const SYN_SockAddr * server, uint32_t sync_interval_s)
Initialize the SNTP client.
SYN_Status syn_sntp_query (SYN_SNTP * sntp)
Perform a single blocking NTP query.
SYN_PT_Status syn_sntp_task (SYN_PT * pt, SYN_Task * task)
Cooperative protothread task for periodic NTP sync.

Public Static Functions

Type Name
bool syn_sntp_is_synced (const SYN_SNTP * sntp)
Check if the client has successfully synced at least once.

Macros

Type Name
define SYN_SNTP_EPOCH_OFFSET 2208988800UL
define SYN_SNTP_MAX_RETRIES 3
define SYN_SNTP_PACKET_SIZE 48
define SYN_SNTP_TIMEOUT_MS 3000

Detailed Description

Implements a Simple NTP (RFC 4330) client that queries a single NTP server and maintains a local epoch clock offset. The client runs as a cooperative protothread task, periodically re-syncing.

After the first successful sync, syn_sntp_get_epoch_s() returns a real UTC epoch timestamp derived from the last NTP response plus local tick elapsed since then.

Used by the WireGuard module for TAI64N handshake timestamps.

** **

static SYN_SNTP sntp;
static SYN_SockAddr ntp_server = { .ip = {216,239,35,0}, .port = 123 };

syn_sntp_init(&sntp, &ntp_server, 3600);  // re-sync every hour

// Register as a scheduler task:
syn_task_create(&tasks[0], "sntp", syn_sntp_task, 2, &sntp);

// Later, read the time:
if (syn_sntp_is_synced(&sntp)) {
    uint32_t now = syn_sntp_get_epoch_s(&sntp);
}

Public Functions Documentation

function syn_sntp_get_drift_ppm

Get the calculated hardware clock drift in Parts-Per-Million (PPM).

int32_t syn_sntp_get_drift_ppm (
    const SYN_SNTP * sntp
) 

Calculated automatically on subsequent NTP syncs by comparing network time elapsed against local hardware timer elapsed.

Parameters:

  • sntp Client context.

Returns:

Calculated drift in PPM (+ = local clock runs fast, - = slow).


function syn_sntp_get_epoch_ns

Get current nanosecond component of the timestamp.

uint32_t syn_sntp_get_epoch_ns (
    const SYN_SNTP * sntp
) 

Derived from the tick counter sub-second remainder + NTP fractional seconds from the last sync. Approximate — not sub-ms accurate.

Parameters:

  • sntp Client context.

Returns:

Nanoseconds (0–999999999), or 0 if unsynced.


function syn_sntp_get_epoch_s

Get current UTC epoch in seconds.

uint32_t syn_sntp_get_epoch_s (
    const SYN_SNTP * sntp
) 

Computed as: last_sync_epoch + (current_tick - sync_tick) / 1000. Returns 0 if not yet synced.

Parameters:

  • sntp Client context.

Returns:

Current epoch seconds, or 0 if unsynced.


function syn_sntp_init

Initialize the SNTP client.

void syn_sntp_init (
    SYN_SNTP * sntp,
    const SYN_SockAddr * server,
    uint32_t sync_interval_s
) 

Parameters:

  • sntp Client context.
  • server NTP server address (typically port 123).
  • sync_interval_s Seconds between re-syncs (e.g. 3600 for hourly).

function syn_sntp_query

Perform a single blocking NTP query.

SYN_Status syn_sntp_query (
    SYN_SNTP * sntp
) 

Opens a UDP socket, sends a request, waits for the response, and updates the internal epoch. Closes the socket when done.

Parameters:

  • sntp Client context.

Returns:

SYN_OK on success, SYN_TIMEOUT or SYN_ERROR on failure.


function syn_sntp_task

Cooperative protothread task for periodic NTP sync.

SYN_PT_Status syn_sntp_task (
    SYN_PT * pt,
    SYN_Task * task
) 

Syncs on startup, then re-syncs every sync_interval_s seconds. Pass the SYN_SNTP context via task->user_data.

Parameters:

  • pt Protothread.
  • task Task descriptor.

Returns:

PT status (PT_WAITING, PT_YIELDED, or PT_ENDED).


Public Static Functions Documentation

function syn_sntp_is_synced

Check if the client has successfully synced at least once.

static inline bool syn_sntp_is_synced (
    const SYN_SNTP * sntp
) 

Parameters:

  • sntp Client context.

Returns:

true if synced.


Macro Definition Documentation

define SYN_SNTP_EPOCH_OFFSET

#define SYN_SNTP_EPOCH_OFFSET `2208988800UL`

NTP epoch offset: seconds between 1900-01-01 and 1970-01-01.


define SYN_SNTP_MAX_RETRIES

#define SYN_SNTP_MAX_RETRIES `3`

Maximum retry attempts per sync cycle.


define SYN_SNTP_PACKET_SIZE

#define SYN_SNTP_PACKET_SIZE `48`

NTP packet size (both request and response).


define SYN_SNTP_TIMEOUT_MS

#define SYN_SNTP_TIMEOUT_MS `3000`

Default receive timeout for NTP response (ms).



The documentation for this class was generated from the following file src/syntropic/net/syn_sntp.h