From 4a3d760c06ba4f575f8ee45f399152606c36347e Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Sun, 17 Jul 2022 12:52:18 +0100 Subject: [PATCH] Add timer ID attribute, for safe cleanup in the scripted environment --- mongoose.c | 3 ++- mongoose.h | 6 ++++-- src/net.c | 1 + src/net.h | 5 +++-- src/timer.c | 2 +- src/timer.h | 1 + 6 files changed, 12 insertions(+), 6 deletions(-) diff --git a/mongoose.c b/mongoose.c index 595576fc..6c69d381 100644 --- a/mongoose.c +++ b/mongoose.c @@ -3511,6 +3511,7 @@ struct mg_timer *mg_timer_add(struct mg_mgr *mgr, uint64_t milliseconds, unsigned flags, void (*fn)(void *), void *arg) { struct mg_timer *t = (struct mg_timer *) calloc(1, sizeof(*t)); mg_timer_init(&mgr->timers, t, milliseconds, flags, fn, arg); + t->id = mgr->timerid++; return t; } @@ -4847,7 +4848,7 @@ char *mg_remove_double_dots(char *s) { void mg_timer_init(struct mg_timer **head, struct mg_timer *t, uint64_t ms, unsigned flags, void (*fn)(void *), void *arg) { - struct mg_timer tmp = {ms, 0U, 0U, flags, fn, arg, *head}; + struct mg_timer tmp = {0U, ms, 0U, 0U, flags, fn, arg, *head}; *t = tmp; *head = t; } diff --git a/mongoose.h b/mongoose.h index b8d6b5bc..056c69f8 100644 --- a/mongoose.h +++ b/mongoose.h @@ -795,6 +795,7 @@ void mg_log_set_fn(void (*logfunc)(unsigned char ch)); struct mg_timer { + unsigned long id; // Timer ID uint64_t period_ms; // Timer period in milliseconds uint64_t prev_ms; // Timestamp of a previous poll uint64_t expire; // Expiration timestamp in milliseconds @@ -1001,12 +1002,13 @@ struct mg_mgr { int dnstimeout; // DNS resolve timeout in milliseconds bool use_dns6; // Use DNS6 server by default, see #1532 unsigned long nextid; // Next connection ID + unsigned long timerid; // Next timer ID void *userdata; // Arbitrary user data pointer uint16_t mqtt_id; // MQTT IDs for pub/sub void *active_dns_requests; // DNS requests in progress struct mg_timer *timers; // Active timers - void *priv; // Used by the experimental stack - size_t extraconnsize; // Used by the experimental stack + void *priv; // Used by the MIP stack + size_t extraconnsize; // Used by the MIP stack #if MG_ARCH == MG_ARCH_FREERTOS_TCP SocketSet_t ss; // NOTE(lsm): referenced from socket struct #endif diff --git a/src/net.c b/src/net.c index 15ee4f9a..7fed145c 100644 --- a/src/net.c +++ b/src/net.c @@ -224,6 +224,7 @@ struct mg_timer *mg_timer_add(struct mg_mgr *mgr, uint64_t milliseconds, unsigned flags, void (*fn)(void *), void *arg) { struct mg_timer *t = (struct mg_timer *) calloc(1, sizeof(*t)); mg_timer_init(&mgr->timers, t, milliseconds, flags, fn, arg); + t->id = mgr->timerid++; return t; } diff --git a/src/net.h b/src/net.h index b0cb80eb..357ff11a 100644 --- a/src/net.h +++ b/src/net.h @@ -25,12 +25,13 @@ struct mg_mgr { int dnstimeout; // DNS resolve timeout in milliseconds bool use_dns6; // Use DNS6 server by default, see #1532 unsigned long nextid; // Next connection ID + unsigned long timerid; // Next timer ID void *userdata; // Arbitrary user data pointer uint16_t mqtt_id; // MQTT IDs for pub/sub void *active_dns_requests; // DNS requests in progress struct mg_timer *timers; // Active timers - void *priv; // Used by the experimental stack - size_t extraconnsize; // Used by the experimental stack + void *priv; // Used by the MIP stack + size_t extraconnsize; // Used by the MIP stack #if MG_ARCH == MG_ARCH_FREERTOS_TCP SocketSet_t ss; // NOTE(lsm): referenced from socket struct #endif diff --git a/src/timer.c b/src/timer.c index 10567aca..ec3f7990 100644 --- a/src/timer.c +++ b/src/timer.c @@ -8,7 +8,7 @@ void mg_timer_init(struct mg_timer **head, struct mg_timer *t, uint64_t ms, unsigned flags, void (*fn)(void *), void *arg) { - struct mg_timer tmp = {ms, 0U, 0U, flags, fn, arg, *head}; + struct mg_timer tmp = {0U, ms, 0U, 0U, flags, fn, arg, *head}; *t = tmp; *head = t; } diff --git a/src/timer.h b/src/timer.h index a72955fd..794496c8 100644 --- a/src/timer.h +++ b/src/timer.h @@ -3,6 +3,7 @@ #include "arch.h" struct mg_timer { + unsigned long id; // Timer ID uint64_t period_ms; // Timer period in milliseconds uint64_t prev_ms; // Timestamp of a previous poll uint64_t expire; // Expiration timestamp in milliseconds