📄 nettimer.c
字号:
/*****************************************************************************
* nettimer.c - Timer Services program file.
*
* Copyright (c) 1998 by Global Election Systems Inc.
*
* The authors hereby grant permission to use, copy, modify, distribute,
* and license this software and its documentation for any purpose, provided
* that existing copyright notices are retained in all copies and that this
* notice and the following disclaimer are included verbatim in any
* distributions. No written agreement, license, or royalty fee is required
* for any of the authorized uses.
*
* THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
* REVISION HISTORY
*
* 98-01-23 Guy Lancaster <lancasterg@acm.org>, Global Election Systems Inc.
* Original.
* 2001-05-18 Mads Christiansen <mads@mogi.dk>, Partner Voxtream
* Added support for running uC/IP in a single proces and on ethernet.
*****************************************************************************/
#include <string.h>
#include "netconf.h"
#include "net.h"
#include "netbuf.h"
#include "nettimer.h"
//#include <string.h>
#include <stdio.h>
#include "netdebug.h"
/*************************/
/*** LOCAL DEFINITIONS ***/
/*************************/
#define TIMER_STACK_SIZE NETSTACK /* Timers are used for network protocols. */
struct timer *Timers;
/***********************************/
/*** PUBLIC FUNCTION DEFINITIONS ***/
/***********************************/
long readclock()
{
long rval;
vDisableInterrupt();
rval = currentclock;
vEnableInterrupt();
return rval;
}
/*
* timeoutJiffy - Set a timer for a timeout in Jiffy time.
* A Jiffy is a system clock tick. The timer will time out at the
* specified system time.
* RETURNS: Zero if OK, otherwise an error code.
*/
INT timeoutJiffy
(
Timer *timerHdr, /* Pointer to timer record. */
ULONG timeout, /* The timeout in Jiffy time. */
void (* timerHandler)(void *), /* The timer handler function. */
void *timerArg /* Arg passed to handler. */
)
{
// ID id;
register struct timer *tnext;
struct timer *tprev = NULL;
#ifdef OS_DEPENDENT
// u_int8 err; 已经修改
#endif
INT st = 0;
/* Validate parameters. */
if (!timerHdr || !timerHandler)
st = -1;
else {
vDisableDispatch();
if (timerHdr->state == TIMER_RUN)
timerClear(timerHdr);
timerHdr->flag = (WORD)0xface;
timerHdr->expiration = (DWORD)timeout;
timerHdr->func = timerHandler;
timerHdr->arg = timerArg;
// timerHdr->taskid = id;
timerHdr->mode |= ALARM_MODE;
timerHdr->state = TIMER_RUN;
for (tnext = Timers; tnext != NULL; tprev=tnext,tnext = tnext->next) {
if((tnext->expiration >= timerHdr->expiration))
break;
}
if(tprev == NULL)
Timers = timerHdr; /* Put at beginning */
else
tprev->next = timerHdr;
timerHdr->next = tnext;
vEnableDispatch();
}
return st;
}
/*
* timerJiffys - Set a timer in Jiffys. A Jiffy is a system clock
* tick. A delay of zero will invoke the timer handler on the next
* Jiffy interrupt.
* RETURNS: Zero if OK, otherwise an error code.
*/
INT timerJiffys
(
Timer *timerHdr, /* Pointer to timer record. */
ULONG timerDelay, /* The delay value in Jiffys. */
void (* timerHandler)(void *), /* The timer handler function. */
void *timerArg /* Arg passed to handler. */
)
{
INT st = 0;
/* Validate parameters. */
if (timerDelay > MAXJIFFYDELAY)
st = -1;
else
st = timeoutJiffy(timerHdr, timerDelay + OSTimeGet(), timerHandler, timerArg);
return st;
}
/*
* timerSeconds - Set a timer in seconds. A delay of zero will
* invoke the timer handler on the next Jiffy interrupt.
* RETURNS: Zero if OK, otherwise an error code.
*/
INT timerSeconds
(
Timer *timerHdr, /* Pointer to timer record. */
ULONG timerDelay, /* The delay value in seconds. */
void (* timerHandler)(void *), /* The timer handler function. */
void *timerArg /* Arg passed to handler. */
)
{
return timeoutJiffy(
timerHdr,
OSTimeGet() + timerDelay * TICKSPERSEC,
timerHandler,
timerArg);
}
/*
* timerTempSeconds - Get a temporary timer from the free list and set
* it in seconds. Note that you don't get a handle on the timer record.
* The record will be automatically returned to the free list when either
* it expires or it is cancelled.
* RETURNS: Zero if OK, otherwise an error code.
*/
INT timerTempSeconds
(
ULONG timerDelay, /* The delay value in seconds. */
void (* timerHandler)(void *), /* The timer handler function. */
void *timerArg /* Arg passed to handler. */
)
{
INT st;
Timer* curTimer;
st = SysCreateTimer((PDWORD)&curTimer, (DWORD)timerDelay*1000, timerHandler, timerArg, ALARM_MODE|AUTO_CLEAR_MODE);
st = st | SysStartTimer((DWORD)curTimer);
return st;
}
/*
* timerClear() - Clear the given timer.
*/
int16 timerClear
(
Timer *timerHdr /* Pointer to timer record. */
)
{
register struct timer *t;
struct timer *tlast = NULL;
if (timerHdr == NULL)
return TIMER_ERROR;
if (timerHdr->flag != 0xface)
return TIMER_ERROR;
if (timerHdr->state == TIMER_RUN) {
/* Enter Critical Section */
vDisableDispatch();
for(t = Timers;t != NULL;tlast = t,t = t->next)
if (t == timerHdr)
break;
if(t == NULL)
return TIMER_ERROR; /* Should probably panic here */
/* Delete from active timer list */
if(tlast != NULL)
tlast->next = t->next;
else
Timers = t->next;
timerHdr->next = NULL;
/* Leave Critical Section */
vEnableDispatch();
}
return 0;
}
/*
* timerCancel - Clear the first matching timer for the given function
* pointer and argument.
*/
void timerCancel(
void (* timerHandler)(void *), /* The timer handler function. */
void *timerArg /* Arg passed to handler. */
)
{
Timer *curTimer;
vDisableDispatch();
for (curTimer = Timers;
curTimer != NULL
&& (curTimer->func != timerHandler
|| curTimer->arg != timerArg);
curTimer = curTimer->next
);
vEnableDispatch();
if (curTimer != NULL) timerClear(curTimer);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -