Time
mg_millis()
uint64_t mg_millis(void);
Return current uptime in milliseconds.
Parameters: None
Return value: Current uptime
Usage example:
uint64_t uptime = mg_millis();
mg_now()
uint64_t mg_now(void);
Return current time in milliseconds, requires an SNTP server connection (see mg_sntp_connect())
Parameters: None
Return value: If an SNTP server connection has been configured, returns current time. Otherwise, returns current uptime just like mg_millis()
Usage example:
mg_sntp_connect(mgr&, NULL /* connect to time.google.com */, NULL, NULL);
...
uint64_t curtime = mg_now();
mg_timer_expired()
bool mg_timer_expired(uint64_t *t, uint64_t period, uint64_t now);
Parameters:
t- Pointer to a the timer valueperiod- timer intervalnow- current time
Return true if a given timer t has expired: now >= *t, false otherwise.
If the timer has expired, the t is advanced by the period.
Usage example:
uint64_t timer = 0, period = 500; // Milliseconds
for (;;) {
if (mg_timer_expired(&timer, period, mg_now())) {
MG_INFO(("Hi!")); // Print a message every 1/2 second
}
mg_mgr_poll(&mgr, 10);
}
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 useurl- Specifies remote URL,time.google.comif NULL.fn- A user event handler function, use NULL if you don't need onefn_data- an arbitrary pointer, which will be stored in the connection structure asc->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);