#ifndef ETHERLIB_TIMER_H #define ETHERLIB_TIMER_H #include #include /** * A single point in time */ typedef struct { uint32_t s; ///< Seconds uint32_t us; ///< Microseconds } TimePoint; /** * Convert TimePoint to microseconds. * @param t time * @return time in microseconds */ int64_t time_to_us(const TimePoint * t); /** * Convert microseconds to TimePoint. * @param t pointer to TimePoint structure * @param us time in microseconds */ void time_from_us(TimePoint * t, int64_t us); /** * Add microseconds to TimePoint. * @param t pointer to TimePoint structure * @param us time in microseconds */ void time_add_us(TimePoint * t, int64_t us); #define TIMER_SCHED_FAILED (-1) struct Timer_; /** * Alarm user data. */ typedef struct { void * ptr; uint32_t u; } AlarmUserData; typedef void (*TimerAlarmCb)(struct Timer_ * timer, AlarmUserData user); /** * Alarm assignment. */ typedef struct { uint32_t id; ///< Timing ID 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); /** * Schedule new alarm. * @param tmr pointer to Timer instance * @param t alarm time * @param cb alarm callback function * @param params user-defined params passed to callback function * @return ID of new schedule OR TIMER_SCHED_FAILED */ uint32_t timer_sched(Timer * tmr, const TimePoint * t, TimerAlarmCb cb, AlarmUserData params); /** * Schedule new alarm by specifying time relatively from the current time point. * @param tmr pointer to Timer instance * @param us relative alarm time * @param cb alarm callback function * @param params user-defined params passed to callback function * @return ID of new schedule OR TIMER_SCHED_FAILED */ uint32_t timer_sched_rel(Timer * tmr, int64_t us, TimerAlarmCb cb, AlarmUserData params); /** * Unschedule alarm. * @param tmr pointer to Timer instance * @param id schedule ID returned by timer_sched() or timer_sched_rel() */ void timer_unsched(Timer * tmr, uint32_t id); /** * Set time. * @param tmr pointer to Timer instance * @param t time */ void timer_set_time(Timer * tmr, const TimePoint * t); /** * Get current time in microseconds. * @param tmr pointer to Timer instance * @return current time in microseconds */ int64_t timer_get_time_us(const Timer * tmr); /** * Get current time. * @param tmr pointer to Timer instance * @return current time */ TimePoint timer_get_time(const Timer * tmr); /** * Advance timer. * @param tmr pointer to Timer instance * @param us time advancement in microseconds */ void timer_tick(Timer * tmr, int64_t us); /** * Print Timer report. * @param tmr pointer to Timer instance */ void timer_report(const Timer * tmr); #endif //ETHERLIB_TIMER_H