Skip to content

File syn_sntp.c

FileList > net > syn_sntp.c

Go to the source code of this file

SNTP client implementation — RFC 4330 subset.

  • #include "../port/syn_port_system.h"
  • #include "../util/syn_assert.h"
  • #include "../util/syn_pack.h"
  • #include "syn_sntp.h"
  • #include <stdio.h>
  • #include <string.h>

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
SYN_Status sntp_parse_packet (SYN_SNTP * sntp, const uint8_t * pkt, size_t len)
Parse an SNTP response packet and extract the transmit timestamp.
SYN_Status sntp_send_request (const SYN_SNTP * sntp, SYN_Socket sock)
Build and send an SNTP request on an already-open socket.
SYN_Status sntp_try_recv (SYN_SNTP * sntp, SYN_Socket sock)
Non-blocking receive: try to read an NTP response.

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 sntp_parse_packet

Parse an SNTP response packet and extract the transmit timestamp.

static SYN_Status sntp_parse_packet (
    SYN_SNTP * sntp,
    const uint8_t * pkt,
    size_t len
) 

Parameters:

  • sntp SNTP client instance.
  • pkt Raw UDP payload.
  • len Payload length in bytes.

Returns:

SYN_OK on success, SYN_BUSY if packet too short, SYN_ERROR on invalid response.


function sntp_send_request

Build and send an SNTP request on an already-open socket.

static SYN_Status sntp_send_request (
    const SYN_SNTP * sntp,
    SYN_Socket sock
) 

Parameters:

  • sntp SNTP context (for server address).
  • sock Open UDP socket.

Returns:

SYN_OK if sent, SYN_ERROR on failure.


function sntp_try_recv

Non-blocking receive: try to read an NTP response.

static SYN_Status sntp_try_recv (
    SYN_SNTP * sntp,
    SYN_Socket sock
) 

Calls recvfrom with timeout_ms = 0 (non-blocking). If a valid response is available, parses it and sets sntp->synced = true.

Parameters:

  • sntp SNTP context (updated on success).
  • sock Open UDP socket.

Returns:

SYN_OK if synced, SYN_BUSY if no data yet, SYN_ERROR on bad response.



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