Compare commits

..
1 Commits
Author SHA1 Message Date
Sergio R. Caprile 48a6b0d422 move timer stuff out of sntp/util 2026-11-30 12:41:06 -03:00
22 changed files with 184 additions and 192 deletions

No files matched your search

+62 -61
View File
@@ -16778,6 +16778,67 @@ bool mg_str_to_num(struct mg_str str, int base, void *val, size_t val_len) {
// The clock lives in mg_micros(), mg_millis() is derived from it. Either one
// can be replaced, see MG_ENABLE_CUSTOM_MICROS and MG_ENABLE_CUSTOM_MILLIS
#if MG_ENABLE_CUSTOM_MICROS == 0
uint64_t mg_micros(void) {
#if MG_ARCH == MG_ARCH_ESP32
return (uint64_t) esp_timer_get_time();
#elif MG_ENABLE_FREERTOS
return (uint64_t) xTaskGetTickCount() * portTICK_PERIOD_MS * 1000;
#elif MG_ARCH == MG_ARCH_THREADX
return (uint64_t) tx_time_get() *
(1000 /* MS per SEC */ / TX_TIMER_TICKS_PER_SECOND) * 1000;
#elif MG_ARCH == MG_ARCH_TIRTOS
return (uint64_t) Clock_getTicks() * 1000;
#elif MG_ARCH == MG_ARCH_ZEPHYR
return k_ticks_to_us_floor64((uint64_t) k_uptime_ticks());
#elif MG_ARCH == MG_ARCH_CMSIS_RTOS1
return (uint64_t) rt_time_get() * 1000;
#elif MG_ARCH == MG_ARCH_CMSIS_RTOS2
return (uint64_t) ((osKernelGetTickCount() * 1000) / osKernelGetTickFreq()) *
1000;
#elif MG_ARCH == MG_ARCH_RTTHREAD
return (uint64_t) ((rt_tick_get() * 1000) / RT_TICK_PER_SECOND) * 1000;
#elif MG_ARCH == MG_ARCH_WIN32
return (uint64_t) GetTickCount() * 1000;
#elif MG_ARCH == MG_ARCH_PICOSDK
return time_us_64();
#elif MG_ARCH == MG_ARCH_CUBE
return (uint64_t) HAL_GetTick() * 1000;
#elif MG_ARCH == MG_ARCH_UNIX && defined(__APPLE__)
// Apple CLOCK_MONOTONIC_RAW is equivalent to CLOCK_BOOTTIME on linux
// Apple CLOCK_UPTIME_RAW is equivalent to CLOCK_MONOTONIC_RAW on linux
return clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000;
#elif MG_ARCH == MG_ARCH_UNIX
struct timespec ts = {0, 0};
// See #1615 - prefer monotonic clock
#if defined(CLOCK_MONOTONIC_RAW)
// Raw hardware-based time that is not subject to NTP adjustment
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
#elif defined(CLOCK_MONOTONIC)
// Affected by the incremental adjustments performed by adjtime and NTP
clock_gettime(CLOCK_MONOTONIC, &ts);
#else
// Affected by discontinuous jumps in the system time and by the incremental
// adjustments performed by adjtime and NTP
clock_gettime(CLOCK_REALTIME, &ts);
#endif
return ((uint64_t) ts.tv_sec * 1000000 + (uint64_t) ts.tv_nsec / 1000);
#elif defined(ARDUINO)
return (uint64_t) micros();
#else
return (uint64_t) time(NULL) * 1000000;
#endif
}
#endif
#if MG_ENABLE_CUSTOM_MILLIS == 0
uint64_t mg_millis(void) {
return mg_micros() / 1000;
}
#endif
void mg_timer_init(struct mg_timer **head, struct mg_timer *t, uint64_t ms, void mg_timer_init(struct mg_timer **head, struct mg_timer *t, uint64_t ms,
unsigned flags, void (*fn)(void *), void *arg) { unsigned flags, void (*fn)(void *), void *arg) {
t->period_ms = ms, t->expire = 0; t->period_ms = ms, t->expire = 0;
@@ -17973,6 +18034,7 @@ int mg_aes_gcm_decrypt(unsigned char *output, const unsigned char *input,
#if MG_TLS == MG_TLS_BUILTIN #if MG_TLS == MG_TLS_BUILTIN
#ifndef MG_MAX_TLSOID_DEPTH #ifndef MG_MAX_TLSOID_DEPTH
@@ -29935,67 +29997,6 @@ bool mg_path_is_sane(const struct mg_str path) {
return true; return true;
} }
// The clock lives in mg_micros(), mg_millis() is derived from it. Either one
// can be replaced, see MG_ENABLE_CUSTOM_MICROS and MG_ENABLE_CUSTOM_MILLIS
#if MG_ENABLE_CUSTOM_MICROS == 0
uint64_t mg_micros(void) {
#if MG_ARCH == MG_ARCH_ESP32
return (uint64_t) esp_timer_get_time();
#elif MG_ENABLE_FREERTOS
return (uint64_t) xTaskGetTickCount() * portTICK_PERIOD_MS * 1000;
#elif MG_ARCH == MG_ARCH_THREADX
return (uint64_t) tx_time_get() *
(1000 /* MS per SEC */ / TX_TIMER_TICKS_PER_SECOND) * 1000;
#elif MG_ARCH == MG_ARCH_TIRTOS
return (uint64_t) Clock_getTicks() * 1000;
#elif MG_ARCH == MG_ARCH_ZEPHYR
return k_ticks_to_us_floor64((uint64_t) k_uptime_ticks());
#elif MG_ARCH == MG_ARCH_CMSIS_RTOS1
return (uint64_t) rt_time_get() * 1000;
#elif MG_ARCH == MG_ARCH_CMSIS_RTOS2
return (uint64_t) ((osKernelGetTickCount() * 1000) / osKernelGetTickFreq()) *
1000;
#elif MG_ARCH == MG_ARCH_RTTHREAD
return (uint64_t) ((rt_tick_get() * 1000) / RT_TICK_PER_SECOND) * 1000;
#elif MG_ARCH == MG_ARCH_WIN32
return (uint64_t) GetTickCount() * 1000;
#elif MG_ARCH == MG_ARCH_PICOSDK
return time_us_64();
#elif MG_ARCH == MG_ARCH_CUBE
return (uint64_t) HAL_GetTick() * 1000;
#elif MG_ARCH == MG_ARCH_UNIX && defined(__APPLE__)
// Apple CLOCK_MONOTONIC_RAW is equivalent to CLOCK_BOOTTIME on linux
// Apple CLOCK_UPTIME_RAW is equivalent to CLOCK_MONOTONIC_RAW on linux
return clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000;
#elif MG_ARCH == MG_ARCH_UNIX
struct timespec ts = {0, 0};
// See #1615 - prefer monotonic clock
#if defined(CLOCK_MONOTONIC_RAW)
// Raw hardware-based time that is not subject to NTP adjustment
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
#elif defined(CLOCK_MONOTONIC)
// Affected by the incremental adjustments performed by adjtime and NTP
clock_gettime(CLOCK_MONOTONIC, &ts);
#else
// Affected by discontinuous jumps in the system time and by the incremental
// adjustments performed by adjtime and NTP
clock_gettime(CLOCK_REALTIME, &ts);
#endif
return ((uint64_t) ts.tv_sec * 1000000 + (uint64_t) ts.tv_nsec / 1000);
#elif defined(ARDUINO)
return (uint64_t) micros();
#else
return (uint64_t) time(NULL) * 1000000;
#endif
}
#endif
#if MG_ENABLE_CUSTOM_MILLIS == 0
uint64_t mg_millis(void) {
return mg_micros() / 1000;
}
#endif
// network format equates big endian order // network format equates big endian order
uint16_t mg_ntohs(uint16_t net) { uint16_t mg_ntohs(uint16_t net) {
return MG_LOAD_BE16(&net); return MG_LOAD_BE16(&net);
+28 -27
View File
@@ -1807,6 +1807,33 @@ void mg_log_set_fn(mg_pfn_t fn, void *param);
// Return microseconds since boot. Never goes backwards.
// To define your own implementation, set MG_ENABLE_CUSTOM_MICROS=1
uint64_t mg_micros(void);
// Return milliseconds since boot. Never goes backwards. Calls mg_micros().
// To define your own implementation, set MG_ENABLE_CUSTOM_MILLIS=1
// Deprecated: use mg_micros() instead.
uint64_t mg_millis(void);
// Checks whether a periodic deadline has expired.
//
// Returns:
// True when the timer has fired; false otherwise.
// Example:
// uint64_t timer = 0;
// if (mg_timer_expired(&timer, 1000, mg_millis())) {
// MG_INFO(("every second"));
// }
// Full examples:
// tutorials/mqtt/mqtt-client
// Related APIs:
// mg_millis(), mg_now()
// Notes:
// Initialise *expiration to 0 before first use. On expiry, this function
// advances *expiration by period and handles time wrap-around.
bool mg_timer_expired(uint64_t *expiration, uint64_t period, uint64_t now);
struct mg_timer { struct mg_timer {
uint64_t period_ms; // Timer period in milliseconds uint64_t period_ms; // Timer period in milliseconds
uint64_t expire; // Expiration timestamp in milliseconds uint64_t expire; // Expiration timestamp in milliseconds
@@ -3475,20 +3502,12 @@ size_t mg_ws_wrap(struct mg_connection *, size_t len, int op);
size_t mg_ws_printf(struct mg_connection *c, int op, const char *fmt, ...); size_t mg_ws_printf(struct mg_connection *c, int op, const char *fmt, ...);
size_t mg_ws_vprintf(struct mg_connection *c, int op, const char *fmt, size_t mg_ws_vprintf(struct mg_connection *c, int op, const char *fmt,
va_list *); va_list *);
// Time / SNTP functions // SNTP functions
// Return microseconds since boot. Never goes backwards.
// To define your own implementation, set MG_ENABLE_CUSTOM_MICROS=1
uint64_t mg_micros(void);
// Return milliseconds since boot. Never goes backwards. Calls mg_micros().
// To define your own implementation, set MG_ENABLE_CUSTOM_MILLIS=1
// Deprecated: use mg_micros() instead.
uint64_t mg_millis(void);
// Boot timestamp in ms since Epoch. 0 until the first successful SNTP sync; // Boot timestamp in ms since Epoch. 0 until the first successful SNTP sync;
// updated automatically by mg_sntp_connect() on each successful response. // updated automatically by mg_sntp_connect() on each successful response.
@@ -3498,24 +3517,6 @@ extern uint64_t mg_boot_timestamp_ms;
// Until a successful SNTP request completes, this is identical to mg_millis(). // Until a successful SNTP request completes, this is identical to mg_millis().
uint64_t mg_now(void); uint64_t mg_now(void);
// Checks whether a periodic deadline has expired.
//
// Returns:
// True when the timer has fired; false otherwise.
// Example:
// uint64_t timer = 0;
// if (mg_timer_expired(&timer, 1000, mg_millis())) {
// MG_INFO(("every second"));
// }
// Full examples:
// tutorials/mqtt/mqtt-client
// Related APIs:
// mg_millis(), mg_now()
// Notes:
// Initialise *expiration to 0 before first use. On expiry, this function
// advances *expiration by period and handles time wrap-around.
bool mg_timer_expired(uint64_t *expiration, uint64_t period, uint64_t now);
// Connect to an SNTP server and send a time request. // Connect to an SNTP server and send a time request.
// `url` defaults to "udp://time.google.com:123" when NULL. // `url` defaults to "udp://time.google.com:123" when NULL.
// On success the internal boot timestamp is updated so that mg_now() returns // On success the internal boot timestamp is updated so that mg_now() returns
+1 -1
View File
@@ -1,6 +1,6 @@
#include "net_builtin.h" #include "net_builtin.h"
#include "log.h" #include "log.h"
#include "sntp.h" #include "timer.h"
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_ATCMD) && MG_ENABLE_DRIVER_ATCMD #if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_ATCMD) && MG_ENABLE_DRIVER_ATCMD
-1
View File
@@ -104,7 +104,6 @@ static bool mg_tcpip_driver_imxrt_init(struct mg_tcpip_if *ifp) {
ENET->MSCR = (1 << 8) | ((cr & 0x3f) << 1); // HOLDTIME 2 clks ENET->MSCR = (1 << 8) | ((cr & 0x3f) << 1); // HOLDTIME 2 clks
struct mg_phy phy = {enet_read_phy, enet_write_phy}; struct mg_phy phy = {enet_read_phy, enet_write_phy};
mg_phy_init(&phy, d->phy_addr, MG_PHY_LEDS_ACTIVE_HIGH); // MAC clocks PHY mg_phy_init(&phy, d->phy_addr, MG_PHY_LEDS_ACTIVE_HIGH); // MAC clocks PHY
mg_tcpip_call(ifp, MG_TCPIP_EV_PHY_INIT, &phy);
// Select RMII mode, 100M, keep CRC, set max rx length, disable loop // Select RMII mode, 100M, keep CRC, set max rx length, disable loop
ENET->RCR = (1518 << 16) | MG_BIT(8) | MG_BIT(2); ENET->RCR = (1518 << 16) | MG_BIT(8) | MG_BIT(2);
// ENET->RCR |= MG_BIT(3); // Receive all // ENET->RCR |= MG_BIT(3); // Receive all
-1
View File
@@ -243,7 +243,6 @@ static bool mg_tcpip_driver_netc_init(struct mg_tcpip_if *ifp) {
NETC_EMDIO->EMDIO_CFG = NETC_EMDIO->EMDIO_CFG =
((mdc_cr & 0x1ffU) << 7) | MG_BIT(2); // MDIO_CLK_DIV, MDIO_HOLD ((mdc_cr & 0x1ffU) << 7) | MG_BIT(2); // MDIO_CLK_DIV, MDIO_HOLD
mg_phy_init(&phy, d->phy_addr, MG_PHY_RGMII); mg_phy_init(&phy, d->phy_addr, MG_PHY_RGMII);
mg_tcpip_call(ifp, MG_TCPIP_EV_PHY_INIT, &phy);
// Initialize ENETC1 PSI0 and port // Initialize ENETC1 PSI0 and port
NETC_ENETC1_PCI->CMD |= MG_BIT(2) | MG_BIT(1); // BUS_MASTER_EN, MEM_ACCESS NETC_ENETC1_PCI->CMD |= MG_BIT(2) | MG_BIT(1); // BUS_MASTER_EN, MEM_ACCESS
-1
View File
@@ -164,7 +164,6 @@ static bool mg_tcpip_driver_ra_init(struct mg_tcpip_if *ifp) {
MG_DEBUG(("PHY addr: %d, smispin: %d", d->phy_addr, s_smispin)); MG_DEBUG(("PHY addr: %d, smispin: %d", d->phy_addr, s_smispin));
struct mg_phy phy = {raeth_read_phy, raeth_write_phy}; struct mg_phy phy = {raeth_read_phy, raeth_write_phy};
mg_phy_init(&phy, d->phy_addr, MG_PHY_CLOCKS_MAC); mg_phy_init(&phy, d->phy_addr, MG_PHY_CLOCKS_MAC);
mg_tcpip_call(ifp, MG_TCPIP_EV_PHY_INIT, &phy);
// Select RMII mode, // Select RMII mode,
ETHERC->ECMR = MG_BIT(2) | MG_BIT(1); // 100M, Full-duplex, CRC ETHERC->ECMR = MG_BIT(2) | MG_BIT(1); // 100M, Full-duplex, CRC
-1
View File
@@ -69,7 +69,6 @@ static bool mg_tcpip_driver_rw612_init(struct mg_tcpip_if *ifp) {
ENET->MSCR = ((d->mdc_cr & 0x3f) << 1) | ((d->mdc_holdtime & 7) << 8); ENET->MSCR = ((d->mdc_cr & 0x3f) << 1) | ((d->mdc_holdtime & 7) << 8);
struct mg_phy phy = {eth_read_phy, eth_write_phy}; struct mg_phy phy = {eth_read_phy, eth_write_phy};
mg_phy_init(&phy, d->phy_addr, 0); mg_phy_init(&phy, d->phy_addr, 0);
mg_tcpip_call(ifp, MG_TCPIP_EV_PHY_INIT, &phy);
ENET->ECR |= MG_BIT(0); // reset ETH ENET->ECR |= MG_BIT(0); // reset ETH
// initialize descriptors // initialize descriptors
-1
View File
@@ -128,7 +128,6 @@ static bool mg_tcpip_driver_same54_init(struct mg_tcpip_if *ifp) {
struct mg_phy phy = {eth_read_phy, eth_write_phy}; struct mg_phy phy = {eth_read_phy, eth_write_phy};
mg_phy_init(&phy, d->phy_addr, 0); mg_phy_init(&phy, d->phy_addr, 0);
mg_tcpip_call(ifp, MG_TCPIP_EV_PHY_INIT, &phy);
for (int i = 0; i < ETH_DESC_CNT; i++) { // Init TX descriptors for (int i = 0; i < ETH_DESC_CNT; i++) { // Init TX descriptors
s_txdesc[i][0] = (uint32_t) s_txbuf[i]; // Point to data buffer s_txdesc[i][0] = (uint32_t) s_txbuf[i]; // Point to data buffer
-1
View File
@@ -142,7 +142,6 @@ static bool mg_tcpip_driver_stm32f_init(struct mg_tcpip_if *ifp) {
ETH->MACFFR = MG_BIT(10); // Perfect filtering ETH->MACFFR = MG_BIT(10); // Perfect filtering
struct mg_phy phy = {eth_read_phy, eth_write_phy}; struct mg_phy phy = {eth_read_phy, eth_write_phy};
mg_phy_init(&phy, phy_addr, MG_PHY_CLOCKS_MAC); mg_phy_init(&phy, phy_addr, MG_PHY_CLOCKS_MAC);
mg_tcpip_call(ifp, MG_TCPIP_EV_PHY_INIT, &phy);
ETH->DMARDLAR = (uint32_t) (uintptr_t) s_rxdesc; // RX descriptors ETH->DMARDLAR = (uint32_t) (uintptr_t) s_rxdesc; // RX descriptors
ETH->DMATDLAR = (uint32_t) (uintptr_t) s_txdesc; // RX descriptors ETH->DMATDLAR = (uint32_t) (uintptr_t) s_txdesc; // RX descriptors
ETH->DMAIER = MG_BIT(6) | MG_BIT(16); // RIE, NISE ETH->DMAIER = MG_BIT(6) | MG_BIT(16); // RIE, NISE
-1
View File
@@ -196,7 +196,6 @@ static bool mg_tcpip_driver_stm32h_init(struct mg_tcpip_if *ifp) {
#endif #endif
struct mg_phy phy = {eth_read_phy, eth_write_phy}; struct mg_phy phy = {eth_read_phy, eth_write_phy};
mg_phy_init(&phy, phy_addr, phy_conf); mg_phy_init(&phy, phy_addr, phy_conf);
mg_tcpip_call(ifp, MG_TCPIP_EV_PHY_INIT, &phy);
ETH->DMACRDLAR = ETH->DMACRDLAR =
(uint32_t) (uintptr_t) s_rxdesc; // RX descriptors start address (uint32_t) (uintptr_t) s_rxdesc; // RX descriptors start address
ETH->DMACRDRLR = ETH_DESC_CNT - 1; // ring length ETH->DMACRDRLR = ETH_DESC_CNT - 1; // ring length
-1
View File
@@ -108,7 +108,6 @@ static bool mg_tcpip_driver_tms570_init(struct mg_tcpip_if *ifp) {
while (delay-- != 0) (void) 0; while (delay-- != 0) (void) 0;
struct mg_phy phy = {emac_read_phy, emac_write_phy}; struct mg_phy phy = {emac_read_phy, emac_write_phy};
mg_phy_init(&phy, d->phy_addr, MG_PHY_CLOCKS_MAC); mg_phy_init(&phy, d->phy_addr, MG_PHY_CLOCKS_MAC);
mg_tcpip_call(ifp, MG_TCPIP_EV_PHY_INIT, &phy);
uint32_t channel; uint32_t channel;
for (channel = 0; channel < 8; channel++) { for (channel = 0; channel < 8; channel++) {
EMAC->MACINDEX = channel; EMAC->MACINDEX = channel;
-1
View File
@@ -89,7 +89,6 @@ static bool mg_tcpip_driver_xmc_init(struct mg_tcpip_if *ifp) {
// init phy // init phy
struct mg_phy phy = {eth_read_phy, eth_write_phy}; struct mg_phy phy = {eth_read_phy, eth_write_phy};
mg_phy_init(&phy, d->phy_addr, MG_PHY_CLOCKS_MAC); mg_phy_init(&phy, d->phy_addr, MG_PHY_CLOCKS_MAC);
mg_tcpip_call(ifp, MG_TCPIP_EV_PHY_INIT, &phy);
// configure MAC: DO, DM, FES, TC // configure MAC: DO, DM, FES, TC
ETH0->MAC_CONFIGURATION = MG_BIT(13) | MG_BIT(11) | MG_BIT(14) | MG_BIT(24); ETH0->MAC_CONFIGURATION = MG_BIT(13) | MG_BIT(11) | MG_BIT(14) | MG_BIT(24);
-1
View File
@@ -150,7 +150,6 @@ static bool mg_tcpip_driver_xmc7_init(struct mg_tcpip_if *ifp) {
// init phy // init phy
struct mg_phy phy = {eth_read_phy, eth_write_phy}; struct mg_phy phy = {eth_read_phy, eth_write_phy};
mg_phy_init(&phy, d->phy_addr, MG_PHY_RGMII); mg_phy_init(&phy, d->phy_addr, MG_PHY_RGMII);
mg_tcpip_call(ifp, MG_TCPIP_EV_PHY_INIT, &phy);
(void) d; (void) d;
return true; return true;
+1 -1
View File
@@ -8,7 +8,7 @@
#include "ota.h" #include "ota.h"
#include "printf.h" #include "printf.h"
#include "rpc.h" #include "rpc.h"
#include "sntp.h" #include "timer.h"
#include "tls.h" #include "tls.h"
#include "url.h" #include "url.h"
#include "ws.h" #include "ws.h"
-1
View File
@@ -251,7 +251,6 @@ void mg_tcpip_call(struct mg_tcpip_if *ifp, int ev, void *ev_data) {
"TCPIP_EV_WIFI_SCAN_END", "TCPIP_EV_WIFI_CONNECT_ERR", "TCPIP_EV_WIFI_SCAN_END", "TCPIP_EV_WIFI_CONNECT_ERR",
"TCPIP_EV_DRIVER", "TCPIP_EV_STATE6_CHANGE", "TCPIP_EV_DRIVER", "TCPIP_EV_STATE6_CHANGE",
"TCPIP_EV_PHY_RX", "TCPIP_EV_PHY_TX", "TCPIP_EV_PHY_RX", "TCPIP_EV_PHY_TX",
"TCPIP_EV_PHY_INIT",
"TCPIP_EV_USER"}; "TCPIP_EV_USER"};
if (ev != MG_TCPIP_EV_TIMER_1S && ev < (int) (sizeof(names) / sizeof(names[0]))) { if (ev != MG_TCPIP_EV_TIMER_1S && ev < (int) (sizeof(names) / sizeof(names[0]))) {
MG_PROF_ADD(ifp, names[ev]); // TODO(): call MG_PROF_DUMP() MG_PROF_FREE() MG_PROF_ADD(ifp, names[ev]); // TODO(): call MG_PROF_DUMP() MG_PROF_FREE()
-1
View File
@@ -42,7 +42,6 @@ enum {
MG_TCPIP_EV_STATE6_CHANGE, // IPv6 link state changed uint8_t *(&ifp->state6) MG_TCPIP_EV_STATE6_CHANGE, // IPv6 link state changed uint8_t *(&ifp->state6)
MG_TCPIP_EV_PHY_RX, // Radio frame received struct mg_str * MG_TCPIP_EV_PHY_RX, // Radio frame received struct mg_str *
MG_TCPIP_EV_PHY_TX, // Radio frame about to be sent struct mg_str * MG_TCPIP_EV_PHY_TX, // Radio frame about to be sent struct mg_str *
MG_TCPIP_EV_PHY_INIT, // PHY initialised struct mg_phy *
MG_TCPIP_EV_USER // Starting ID for user-defined events MG_TCPIP_EV_USER // Starting ID for user-defined events
}; };
// clang-format on // clang-format on
+2 -28
View File
@@ -1,17 +1,9 @@
// Time / SNTP functions // SNTP functions
#pragma once #pragma once
#include "net.h" #include "net.h"
#include "timer.h"
// Return microseconds since boot. Never goes backwards.
// To define your own implementation, set MG_ENABLE_CUSTOM_MICROS=1
uint64_t mg_micros(void);
// Return milliseconds since boot. Never goes backwards. Calls mg_micros().
// To define your own implementation, set MG_ENABLE_CUSTOM_MILLIS=1
// Deprecated: use mg_micros() instead.
uint64_t mg_millis(void);
// Boot timestamp in ms since Epoch. 0 until the first successful SNTP sync; // Boot timestamp in ms since Epoch. 0 until the first successful SNTP sync;
// updated automatically by mg_sntp_connect() on each successful response. // updated automatically by mg_sntp_connect() on each successful response.
@@ -21,24 +13,6 @@ extern uint64_t mg_boot_timestamp_ms;
// Until a successful SNTP request completes, this is identical to mg_millis(). // Until a successful SNTP request completes, this is identical to mg_millis().
uint64_t mg_now(void); uint64_t mg_now(void);
// Checks whether a periodic deadline has expired.
//
// Returns:
// True when the timer has fired; false otherwise.
// Example:
// uint64_t timer = 0;
// if (mg_timer_expired(&timer, 1000, mg_millis())) {
// MG_INFO(("every second"));
// }
// Full examples:
// tutorials/mqtt/mqtt-client
// Related APIs:
// mg_millis(), mg_now()
// Notes:
// Initialise *expiration to 0 before first use. On expiry, this function
// advances *expiration by period and handles time wrap-around.
bool mg_timer_expired(uint64_t *expiration, uint64_t period, uint64_t now);
// Connect to an SNTP server and send a time request. // Connect to an SNTP server and send a time request.
// `url` defaults to "udp://time.google.com:123" when NULL. // `url` defaults to "udp://time.google.com:123" when NULL.
// On success the internal boot timestamp is updated so that mg_now() returns // On success the internal boot timestamp is updated so that mg_now() returns
+61
View File
@@ -2,6 +2,67 @@
#include "timer.h" #include "timer.h"
#include "util.h" #include "util.h"
// The clock lives in mg_micros(), mg_millis() is derived from it. Either one
// can be replaced, see MG_ENABLE_CUSTOM_MICROS and MG_ENABLE_CUSTOM_MILLIS
#if MG_ENABLE_CUSTOM_MICROS == 0
uint64_t mg_micros(void) {
#if MG_ARCH == MG_ARCH_ESP32
return (uint64_t) esp_timer_get_time();
#elif MG_ENABLE_FREERTOS
return (uint64_t) xTaskGetTickCount() * portTICK_PERIOD_MS * 1000;
#elif MG_ARCH == MG_ARCH_THREADX
return (uint64_t) tx_time_get() *
(1000 /* MS per SEC */ / TX_TIMER_TICKS_PER_SECOND) * 1000;
#elif MG_ARCH == MG_ARCH_TIRTOS
return (uint64_t) Clock_getTicks() * 1000;
#elif MG_ARCH == MG_ARCH_ZEPHYR
return k_ticks_to_us_floor64((uint64_t) k_uptime_ticks());
#elif MG_ARCH == MG_ARCH_CMSIS_RTOS1
return (uint64_t) rt_time_get() * 1000;
#elif MG_ARCH == MG_ARCH_CMSIS_RTOS2
return (uint64_t) ((osKernelGetTickCount() * 1000) / osKernelGetTickFreq()) *
1000;
#elif MG_ARCH == MG_ARCH_RTTHREAD
return (uint64_t) ((rt_tick_get() * 1000) / RT_TICK_PER_SECOND) * 1000;
#elif MG_ARCH == MG_ARCH_WIN32
return (uint64_t) GetTickCount() * 1000;
#elif MG_ARCH == MG_ARCH_PICOSDK
return time_us_64();
#elif MG_ARCH == MG_ARCH_CUBE
return (uint64_t) HAL_GetTick() * 1000;
#elif MG_ARCH == MG_ARCH_UNIX && defined(__APPLE__)
// Apple CLOCK_MONOTONIC_RAW is equivalent to CLOCK_BOOTTIME on linux
// Apple CLOCK_UPTIME_RAW is equivalent to CLOCK_MONOTONIC_RAW on linux
return clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000;
#elif MG_ARCH == MG_ARCH_UNIX
struct timespec ts = {0, 0};
// See #1615 - prefer monotonic clock
#if defined(CLOCK_MONOTONIC_RAW)
// Raw hardware-based time that is not subject to NTP adjustment
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
#elif defined(CLOCK_MONOTONIC)
// Affected by the incremental adjustments performed by adjtime and NTP
clock_gettime(CLOCK_MONOTONIC, &ts);
#else
// Affected by discontinuous jumps in the system time and by the incremental
// adjustments performed by adjtime and NTP
clock_gettime(CLOCK_REALTIME, &ts);
#endif
return ((uint64_t) ts.tv_sec * 1000000 + (uint64_t) ts.tv_nsec / 1000);
#elif defined(ARDUINO)
return (uint64_t) micros();
#else
return (uint64_t) time(NULL) * 1000000;
#endif
}
#endif
#if MG_ENABLE_CUSTOM_MILLIS == 0
uint64_t mg_millis(void) {
return mg_micros() / 1000;
}
#endif
void mg_timer_init(struct mg_timer **head, struct mg_timer *t, uint64_t ms, void mg_timer_init(struct mg_timer **head, struct mg_timer *t, uint64_t ms,
unsigned flags, void (*fn)(void *), void *arg) { unsigned flags, void (*fn)(void *), void *arg) {
t->period_ms = ms, t->expire = 0; t->period_ms = ms, t->expire = 0;
+27
View File
@@ -2,6 +2,33 @@
#include "arch.h" #include "arch.h"
// Return microseconds since boot. Never goes backwards.
// To define your own implementation, set MG_ENABLE_CUSTOM_MICROS=1
uint64_t mg_micros(void);
// Return milliseconds since boot. Never goes backwards. Calls mg_micros().
// To define your own implementation, set MG_ENABLE_CUSTOM_MILLIS=1
// Deprecated: use mg_micros() instead.
uint64_t mg_millis(void);
// Checks whether a periodic deadline has expired.
//
// Returns:
// True when the timer has fired; false otherwise.
// Example:
// uint64_t timer = 0;
// if (mg_timer_expired(&timer, 1000, mg_millis())) {
// MG_INFO(("every second"));
// }
// Full examples:
// tutorials/mqtt/mqtt-client
// Related APIs:
// mg_millis(), mg_now()
// Notes:
// Initialise *expiration to 0 before first use. On expiry, this function
// advances *expiration by period and handles time wrap-around.
bool mg_timer_expired(uint64_t *expiration, uint64_t period, uint64_t now);
struct mg_timer { struct mg_timer {
uint64_t period_ms; // Timer period in milliseconds uint64_t period_ms; // Timer period in milliseconds
uint64_t expire; // Expiration timestamp in milliseconds uint64_t expire; // Expiration timestamp in milliseconds
+1
View File
@@ -2,6 +2,7 @@
#include "config.h" #include "config.h"
#include "printf.h" #include "printf.h"
#include "sha256.h" #include "sha256.h"
#include "sntp.h"
#include "tls.h" #include "tls.h"
#include "tls_aes128.h" #include "tls_aes128.h"
#include "tls_chacha20.h" #include "tls_chacha20.h"
-61
View File
@@ -197,67 +197,6 @@ bool mg_path_is_sane(const struct mg_str path) {
return true; return true;
} }
// The clock lives in mg_micros(), mg_millis() is derived from it. Either one
// can be replaced, see MG_ENABLE_CUSTOM_MICROS and MG_ENABLE_CUSTOM_MILLIS
#if MG_ENABLE_CUSTOM_MICROS == 0
uint64_t mg_micros(void) {
#if MG_ARCH == MG_ARCH_ESP32
return (uint64_t) esp_timer_get_time();
#elif MG_ENABLE_FREERTOS
return (uint64_t) xTaskGetTickCount() * portTICK_PERIOD_MS * 1000;
#elif MG_ARCH == MG_ARCH_THREADX
return (uint64_t) tx_time_get() *
(1000 /* MS per SEC */ / TX_TIMER_TICKS_PER_SECOND) * 1000;
#elif MG_ARCH == MG_ARCH_TIRTOS
return (uint64_t) Clock_getTicks() * 1000;
#elif MG_ARCH == MG_ARCH_ZEPHYR
return k_ticks_to_us_floor64((uint64_t) k_uptime_ticks());
#elif MG_ARCH == MG_ARCH_CMSIS_RTOS1
return (uint64_t) rt_time_get() * 1000;
#elif MG_ARCH == MG_ARCH_CMSIS_RTOS2
return (uint64_t) ((osKernelGetTickCount() * 1000) / osKernelGetTickFreq()) *
1000;
#elif MG_ARCH == MG_ARCH_RTTHREAD
return (uint64_t) ((rt_tick_get() * 1000) / RT_TICK_PER_SECOND) * 1000;
#elif MG_ARCH == MG_ARCH_WIN32
return (uint64_t) GetTickCount() * 1000;
#elif MG_ARCH == MG_ARCH_PICOSDK
return time_us_64();
#elif MG_ARCH == MG_ARCH_CUBE
return (uint64_t) HAL_GetTick() * 1000;
#elif MG_ARCH == MG_ARCH_UNIX && defined(__APPLE__)
// Apple CLOCK_MONOTONIC_RAW is equivalent to CLOCK_BOOTTIME on linux
// Apple CLOCK_UPTIME_RAW is equivalent to CLOCK_MONOTONIC_RAW on linux
return clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000;
#elif MG_ARCH == MG_ARCH_UNIX
struct timespec ts = {0, 0};
// See #1615 - prefer monotonic clock
#if defined(CLOCK_MONOTONIC_RAW)
// Raw hardware-based time that is not subject to NTP adjustment
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
#elif defined(CLOCK_MONOTONIC)
// Affected by the incremental adjustments performed by adjtime and NTP
clock_gettime(CLOCK_MONOTONIC, &ts);
#else
// Affected by discontinuous jumps in the system time and by the incremental
// adjustments performed by adjtime and NTP
clock_gettime(CLOCK_REALTIME, &ts);
#endif
return ((uint64_t) ts.tv_sec * 1000000 + (uint64_t) ts.tv_nsec / 1000);
#elif defined(ARDUINO)
return (uint64_t) micros();
#else
return (uint64_t) time(NULL) * 1000000;
#endif
}
#endif
#if MG_ENABLE_CUSTOM_MILLIS == 0
uint64_t mg_millis(void) {
return mg_micros() / 1000;
}
#endif
// network format equates big endian order // network format equates big endian order
uint16_t mg_ntohs(uint16_t net) { uint16_t mg_ntohs(uint16_t net) {
return MG_LOAD_BE16(&net); return MG_LOAD_BE16(&net);
+1 -1
View File
@@ -3,8 +3,8 @@
#include "arch.h" #include "arch.h"
#include "config.h" #include "config.h"
#include "net.h" #include "net.h"
#include "sntp.h"
#include "str.h" #include "str.h"
#include "timer.h"
#if MG_ENABLE_ASSERT #if MG_ENABLE_ASSERT
#include <assert.h> #include <assert.h>