From 0b20b8cb603acf811b8335f04d570d722fd78b1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wiesner=20Andr=C3=A1s?= Date: Sun, 25 Dec 2022 11:03:01 +0100 Subject: [PATCH] timer aded --- arp_cache.c | 2 +- timer.c | 22 ++++++++++++++++++++++ timer.h | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 timer.c create mode 100644 timer.h diff --git a/arp_cache.c b/arp_cache.c index 596745b..f08da10 100644 --- a/arp_cache.c +++ b/arp_cache.c @@ -76,7 +76,7 @@ const ArpEntry *arpc_get_ask(ArpCache *arpc, ip4_addr ip) { uint32_t attemptN = 0; while (((entry = arpc_get(arpc, ip)) == NULL) && (attemptN < ETHLIB_ARP_RETRY_COUNT)) { arpc_ask(arpc, ip); - ETHLIB_SLEEP(20); + ETHLIB_SLEEP_MS(20); //attemptN++; } diff --git a/timer.c b/timer.c new file mode 100644 index 0000000..28262c8 --- /dev/null +++ b/timer.c @@ -0,0 +1,22 @@ +// +// Created by epagris on 2022.12.21.. +// + +#include "timer.h" +#include "dynmem.h" +#include "utils.h" + +Timer *timer_new(uint32_t maxSched) { + uint32_t tmrHdrSize = sizeof(TimePoint) + sizeof(AlarmAssignment *) + 2 * sizeof(uint32_t); + Timer * tmr = (Timer *) dynmem_alloc(tmrHdrSize + maxSched * sizeof(AlarmAssignment)); + ASSERT_NULL(tmr); + + tmr->maxSched = maxSched; + tmr->nSched = 0; + tmr->nextAlarm = NULL; + tmr->time.s = 0; + tmr->time.ns = 0; + memset(tmr->alarms, 0, maxSched * sizeof(AlarmAssignment)); + + return tmr; +} diff --git a/timer.h b/timer.h new file mode 100644 index 0000000..e609b73 --- /dev/null +++ b/timer.h @@ -0,0 +1,50 @@ +#ifndef ETHERLIB_TEST_TIMER_H +#define ETHERLIB_TEST_TIMER_H + +#include + +/** + * A single point in time + */ +typedef struct { + uint32_t s; ///< Seconds + uint32_t ns; ///< Nanoseconds +} TimePoint; + + +struct Timer_; + +typedef union { + void * ptr; + uint32_t u; +} AlarmUserData; + +typedef void (*TimerAlarmCb)(struct Timer_ * timer, AlarmUserData user); + +typedef struct { + TimePoint time; ///< Alarm time + TimerAlarmCb cb; ///< Pointer to callback function + AlarmUserData params; ///< User data passed to callback function +} AlarmAssignment; + +/** + * Timer class + */ +typedef struct Timer_ { + TimePoint time; ///< Absolute time + AlarmAssignment * nextAlarm; ///< Nearest alarm + uint32_t maxSched; ///< Maximum number of scheduled alarms + uint32_t nSched; ///< Number of scheduled alarms + AlarmAssignment alarms[]; ///< Alarm assigment pool +} Timer; + +/** + * Create new timer + * @param maxSched number of maximum alarms to be scheduled + * @return pointer to new instance OR NULL on error + */ +Timer * timer_new(uint32_t maxSched); + + + +#endif //ETHERLIB_TEST_TIMER_H