diff --git a/mongoose.c b/mongoose.c index b1d5ef58..6ca6da3c 100644 --- a/mongoose.c +++ b/mongoose.c @@ -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, unsigned flags, void (*fn)(void *), void *arg) { 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 #ifndef MG_MAX_TLSOID_DEPTH @@ -29935,67 +29997,6 @@ bool mg_path_is_sane(const struct mg_str path) { 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 uint16_t mg_ntohs(uint16_t net) { return MG_LOAD_BE16(&net); diff --git a/mongoose.h b/mongoose.h index 7e940d5f..a1ecca8b 100644 --- a/mongoose.h +++ b/mongoose.h @@ -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 { uint64_t period_ms; // Timer period 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_vprintf(struct mg_connection *c, int op, const char *fmt, 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; // 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(). 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. // `url` defaults to "udp://time.google.com:123" when NULL. // On success the internal boot timestamp is updated so that mg_now() returns diff --git a/src/drivers/at_cmd.c b/src/drivers/at_cmd.c index 90c765e1..060a3433 100644 --- a/src/drivers/at_cmd.c +++ b/src/drivers/at_cmd.c @@ -1,6 +1,6 @@ #include "net_builtin.h" #include "log.h" -#include "sntp.h" +#include "timer.h" #if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_ATCMD) && MG_ENABLE_DRIVER_ATCMD diff --git a/src/mdash.c b/src/mdash.c index 05b0cb85..a4102471 100644 --- a/src/mdash.c +++ b/src/mdash.c @@ -8,7 +8,7 @@ #include "ota.h" #include "printf.h" #include "rpc.h" -#include "sntp.h" +#include "timer.h" #include "tls.h" #include "url.h" #include "ws.h" diff --git a/src/sntp.h b/src/sntp.h index 37661dff..3cabcc95 100644 --- a/src/sntp.h +++ b/src/sntp.h @@ -1,17 +1,9 @@ -// Time / SNTP functions +// SNTP functions #pragma once #include "net.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); +#include "timer.h" // Boot timestamp in ms since Epoch. 0 until the first successful SNTP sync; // 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(). 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. // `url` defaults to "udp://time.google.com:123" when NULL. // On success the internal boot timestamp is updated so that mg_now() returns diff --git a/src/timer.c b/src/timer.c index 6649c651..b9d1c7d9 100644 --- a/src/timer.c +++ b/src/timer.c @@ -2,6 +2,67 @@ #include "timer.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, unsigned flags, void (*fn)(void *), void *arg) { t->period_ms = ms, t->expire = 0; diff --git a/src/timer.h b/src/timer.h index 6df74de2..d574a554 100644 --- a/src/timer.h +++ b/src/timer.h @@ -2,6 +2,33 @@ #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 { uint64_t period_ms; // Timer period in milliseconds uint64_t expire; // Expiration timestamp in milliseconds diff --git a/src/tls_builtin.c b/src/tls_builtin.c index 4369ae54..203015f5 100644 --- a/src/tls_builtin.c +++ b/src/tls_builtin.c @@ -2,6 +2,7 @@ #include "config.h" #include "printf.h" #include "sha256.h" +#include "sntp.h" #include "tls.h" #include "tls_aes128.h" #include "tls_chacha20.h" diff --git a/src/util.c b/src/util.c index d8767412..aede841b 100644 --- a/src/util.c +++ b/src/util.c @@ -197,67 +197,6 @@ bool mg_path_is_sane(const struct mg_str path) { 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 uint16_t mg_ntohs(uint16_t net) { return MG_LOAD_BE16(&net); diff --git a/src/util.h b/src/util.h index 4815d5d3..81d08258 100644 --- a/src/util.h +++ b/src/util.h @@ -3,8 +3,8 @@ #include "arch.h" #include "config.h" #include "net.h" -#include "sntp.h" #include "str.h" +#include "timer.h" #if MG_ENABLE_ASSERT #include