[core] Implement POSIX gettimeofday() and settimeofday()
This commit is contained in:
@@ -77,6 +77,11 @@
|
|||||||
#define LT_UART_DEFAULT_SERIAL LT_UART_DEFAULT_PORT
|
#define LT_UART_DEFAULT_SERIAL LT_UART_DEFAULT_PORT
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Misc options
|
||||||
|
#ifndef LT_USE_TIME
|
||||||
|
#define LT_USE_TIME 0
|
||||||
|
#endif
|
||||||
|
|
||||||
// Per-module logging output - applies to all loglevels
|
// Per-module logging output - applies to all loglevels
|
||||||
#ifndef LT_DEBUG_ALL
|
#ifndef LT_DEBUG_ALL
|
||||||
#define LT_DEBUG_ALL 0
|
#define LT_DEBUG_ALL 0
|
||||||
|
|||||||
@@ -41,6 +41,12 @@ void runPeriodicTasks() {
|
|||||||
periodicTasks[0] = millis();
|
periodicTasks[0] = millis();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
#if LT_USE_TIME
|
||||||
|
if (millis() - periodicTasks[1] > 10000) {
|
||||||
|
gettimeofday(NULL, NULL);
|
||||||
|
periodicTasks[1] = millis();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
/* Copyright (c) Kuba Szczodrzyński 2022-05-16. */
|
/* Copyright (c) Kuba Szczodrzyński 2022-05-16. */
|
||||||
|
|
||||||
|
#include <sys/time.h>
|
||||||
|
|
||||||
extern char *strdup(const char *);
|
extern char *strdup(const char *);
|
||||||
extern int strcasecmp(const char *s1, const char *s2);
|
extern int strcasecmp(const char *s1, const char *s2);
|
||||||
extern int strncasecmp(const char *s1, const char *s2, size_t n);
|
extern int strncasecmp(const char *s1, const char *s2, size_t n);
|
||||||
|
|||||||
50
arduino/libretuya/posix/time.c
Normal file
50
arduino/libretuya/posix/time.c
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
/* Copyright (c) Kuba Szczodrzyński 2022-09-03. */
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
static uint32_t reset_epoch = 0; // epoch corresponding to millis() == 0
|
||||||
|
static uint32_t reset_millis = 0; // millis() when epoch reset was performed
|
||||||
|
|
||||||
|
int __wrap_gettimeofday(struct timeval *tv, void *tz) {
|
||||||
|
if (millis() < reset_millis) {
|
||||||
|
// the clock overflowed
|
||||||
|
reset_epoch += UINT32_MAX / 1000;
|
||||||
|
reset_millis = millis();
|
||||||
|
}
|
||||||
|
if (!tv) {
|
||||||
|
errno = EINVAL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
unsigned long m = millis();
|
||||||
|
tv->tv_sec = reset_epoch + (m / 1000);
|
||||||
|
tv->tv_usec = (m % 1000) * 1000;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int __wrap_settimeofday(const struct timeval *tv, const struct timezone *tz) {
|
||||||
|
if (!tv) {
|
||||||
|
errno = EINVAL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
unsigned long m = millis();
|
||||||
|
reset_epoch = tv->tv_sec - (m / 1000);
|
||||||
|
reset_millis = m;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int gettimeofday(struct timeval *tv, void *tz) {
|
||||||
|
return __wrap_gettimeofday(tv, tz);
|
||||||
|
}
|
||||||
|
|
||||||
|
int settimeofday(const struct timeval *tv, const struct timezone *tz) {
|
||||||
|
return __wrap_settimeofday(tv, tz);
|
||||||
|
}
|
||||||
|
|
||||||
|
int _gettimeofday(struct timeval *tv, void *tz) {
|
||||||
|
return __wrap_gettimeofday(tv, tz);
|
||||||
|
}
|
||||||
|
|
||||||
|
int _settimeofday(const struct timeval *tv, const struct timezone *tz) {
|
||||||
|
return __wrap_settimeofday(tv, tz);
|
||||||
|
}
|
||||||
@@ -30,6 +30,9 @@ env.Append(
|
|||||||
# wrappers from port/printf/
|
# wrappers from port/printf/
|
||||||
"-Wl,-wrap,putchar",
|
"-Wl,-wrap,putchar",
|
||||||
"-Wl,-wrap,puts",
|
"-Wl,-wrap,puts",
|
||||||
|
# wrappers from posix/time.c
|
||||||
|
"-Wl,-wrap,gettimeofday",
|
||||||
|
"-Wl,-wrap,settimeofday",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
# Arduino core uses __libc_init_array
|
# Arduino core uses __libc_init_array
|
||||||
|
|||||||
@@ -107,3 +107,7 @@ The meaning of most flags is as follows:
|
|||||||
- `LT_ARD_HAS_MD5` - MD5 library implemented, `MD5Impl.h` available
|
- `LT_ARD_HAS_MD5` - MD5 library implemented, `MD5Impl.h` available
|
||||||
- `LT_ARD_HAS_WIFI` - WiFi library implemented, `WiFiData.h` available
|
- `LT_ARD_HAS_WIFI` - WiFi library implemented, `WiFiData.h` available
|
||||||
- `LT_HEAP_FUNC` - function name used to get available heap size (for `LT_HEAP_I()`)
|
- `LT_HEAP_FUNC` - function name used to get available heap size (for `LT_HEAP_I()`)
|
||||||
|
|
||||||
|
### Misc options
|
||||||
|
|
||||||
|
- `LT_USE_TIME` - enables implementation of `gettimeofday()` and `settimeofday()`; checks for `millis()` overflows periodically
|
||||||
|
|||||||
Reference in New Issue
Block a user