refactor mstimer callback handling (#400)

Co-authored-by: Steve Karg <skarg@users.sourceforge.net>
This commit is contained in:
Steve Karg
2023-02-18 10:02:13 -06:00
committed by GitHub
parent 343d9f893d
commit 261a506eb4
5 changed files with 112 additions and 220 deletions
+54
View File
@@ -22,6 +22,60 @@
#include <stdint.h>
#include "bacnet/basic/sys/mstimer.h"
/* callback data head of list */
static struct mstimer_callback_data_t *Callback_Head;
/**
* Handles an interrupt from a hardware millisecond timer
*/
void mstimer_callback_handler(void)
{
struct mstimer_callback_data_t *cb;
cb = (struct mstimer_callback_data_t *)Callback_Head;
while (cb) {
if (mstimer_expired(&cb->timer)) {
cb->callback();
if (mstimer_interval(&cb->timer) > 0) {
mstimer_reset(&cb->timer);
}
}
cb = cb->next;
}
}
/**
* Configures and enables a repeating callback function
*
* @param new_cb - pointer to #mstimer_callback_data_t
* @param callback - pointer to a #timer_callback_function function
* @param milliseconds - how often to call the function
*/
void mstimer_callback(struct mstimer_callback_data_t *new_cb,
mstimer_callback_function callback,
unsigned long milliseconds)
{
struct mstimer_callback_data_t *cb;
if (new_cb) {
new_cb->callback = callback;
mstimer_set(&new_cb->timer, milliseconds);
}
if (Callback_Head) {
cb = (struct mstimer_callback_data_t *)Callback_Head;
while (cb) {
if (!cb->next) {
cb->next = new_cb;
break;
} else {
cb = cb->next;
}
}
} else {
Callback_Head = new_cb;
}
}
/**
* @brief Set a timer for a time sometime in the future
*
+8 -4
View File
@@ -35,8 +35,9 @@ struct mstimer {
unsigned long interval;
};
/* optional callback function form */
typedef void (*mstimer_callback_function) (void);
/* callback data structure */
/* optional callback data structure */
struct mstimer_callback_data_t;
struct mstimer_callback_data_t {
struct mstimer timer;
@@ -62,15 +63,18 @@ BACNET_STACK_EXPORT
unsigned long mstimer_elapsed(struct mstimer *t);
BACNET_STACK_EXPORT
unsigned long mstimer_interval(struct mstimer *t);
/* HAL implementation */
BACNET_STACK_EXPORT
unsigned long mstimer_now(void);
/* optional callback timer support for embedded systems */
BACNET_STACK_EXPORT
void mstimer_callback(
struct mstimer_callback_data_t *cb,
mstimer_callback_function callback,
unsigned long milliseconds);
BACNET_STACK_EXPORT
void mstimer_callback_handler(void);
/* HAL implementation */
BACNET_STACK_EXPORT
unsigned long mstimer_now(void);
BACNET_STACK_EXPORT
void mstimer_init(void);
#ifdef __cplusplus