diff --git a/nwatch.c b/nwatch.c index 3203270e..b7ded80f 100644 --- a/nwatch.c +++ b/nwatch.c @@ -65,10 +65,13 @@ typedef struct __netwatchtimer { struct timeval tv; /* time when event is due */ pNWCallback func; /* function to call */ void* cntx; /* abstract context to pass to callback */ - long int tick; /* millisecond repeat rate */ + int msec; /* millisecond delay time */ + int tick; /* millisecond repeat rate */ long int vrfy; /* integrity check */ } NWTimer; +static pNWTimer activeTimer = NULL; + /* * \brief private function to insert an entry into the sorted timer queue. * @@ -173,6 +176,7 @@ int NetWatchRegisterTimer(pNWTimer* handle, int mSec, pNew->tv.tv_sec ++; pNew->tv.tv_usec -= 1000000; } + pNew->msec = mSec; pNew->tick = 0; pNew->func = callback; pNew->cntx = context; @@ -196,16 +200,33 @@ int NetWatchRegisterTimerPeriodic(pNWTimer* handle, int mSecInitial, int mSecPer return 0; } +pNWTimer NetWatchGetActiveTimer(void) +{ + return activeTimer; +} + +int NetWatchGetTimerInitial(pNWTimer handle) +{ + if (handle == NULL || (handle->vrfy != NWMAGIC && handle->vrfy != ~NWMAGIC)) + return 0; + return handle->msec; +} + +int NetWatchGetTimerDelay(pNWTimer handle) +{ + return NetWatchGetTimerInitial(handle); +} + int NetWatchGetTimerPeriod(pNWTimer handle) { - if (handle == NULL || handle->vrfy != NWMAGIC) + if (handle == NULL || (handle->vrfy != NWMAGIC && handle->vrfy != ~NWMAGIC)) return 0; return handle->tick; } int NetWatchSetTimerPeriod(pNWTimer handle, int mSecPeriod) { - if (handle == NULL || handle->vrfy != NWMAGIC) + if (handle == NULL || (handle->vrfy != NWMAGIC && handle->vrfy != ~NWMAGIC)) return 0; handle->tick = mSecPeriod; return 1; @@ -214,7 +235,7 @@ int NetWatchSetTimerPeriod(pNWTimer handle, int mSecPeriod) int NetWatchRemoveTimer(pNWTimer handle) { pNetWatch self = instance; - if (!self || self->lMagic != NWMAGIC)\ + if (!self || self->lMagic != NWMAGIC) return 0; if (handle == NULL || handle->vrfy != NWMAGIC) return 0; @@ -447,7 +468,11 @@ int NetWatchTask (void* pData) break; } NetWatchTimerRemQue(self, pNew); + activeTimer = pNew; + activeTimer->vrfy = ~NWMAGIC; iStatus = pNew->func(pNew->cntx, 0); + activeTimer->vrfy = 0; + activeTimer = NULL; /* * If this is a recurrent timer and the function * indicates to keep it going, put it back in diff --git a/nwatch.h b/nwatch.h index fbaeda14..4d5a0de4 100644 --- a/nwatch.h +++ b/nwatch.h @@ -52,6 +52,10 @@ int NetWatchRegisterTimer(pNWTimer* handle, int mSec, int NetWatchRegisterTimerPeriodic(pNWTimer* handle, int mSecInitial, int mSecPeriod, pNWCallback callback, void* context); +pNWTimer NetWatchGetActiveTimer(void); + +int NetWatchGetTimerDelay(pNWTimer handle); +int NetWatchGetTimerInitial(pNWTimer handle); int NetWatchGetTimerPeriod(pNWTimer handle); int NetWatchSetTimerPeriod(pNWTimer handle, int mSecPeriod); /**