SNTP

mg_sntp_connect()

struct mg_connection *mg_sntp_connect(struct mg_mgr *mgr, const char *url,
                                      mg_event_handler_t fn, void *fn_data)

Connect to an SNTP server.

Parameters:

  • mgr - Event manager to use
  • url - Specifies remote URL, time.google.com if NULL.
  • fn - A user event handler function, use NULL if you don't need one
  • fn_data - an arbitrary pointer, which will be stored in the connection structure as c->fn_data, so the event handler can use it when called.

Return value: created connection, or NULL on error. Possible errors are: not enough memory, a NULL URL, or, in the case of our built-in TCP/IP stack, the network not being ready.

Simplest usage example: see mg_now()

Full usage example:

static void sntp_cb(struct mg_connection *c, int ev, void *ev_data) {
  if (ev == MG_EV_SNTP_TIME) {
    // Time received, the internal protocol handler updates what mg_now() returns
    uint64_t curtime = mg_now();
    // otherwise, you can process the server returned data yourself
    uint64_t epoch_millis = *(uint64_t *) ev_data;
  }
}
...
mg_sntp_connect(mgr&, NULL /* connect to time.google.com */, sntp_cb, NULL);

mg_sntp_request()

void mg_sntp_request(struct mg_connection *c)

Send time request to SNTP server

Parameters:

  • c - Connection to use

Return value: None

Usage example:

mg_sntp_request(c);