⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 natevttimer.c

📁 VXWORKS NAT 部分源代码3 有兴趣朋友可以参考下
💻 C
📖 第 1 页 / 共 2 页
字号:
/* natEvtTimer.c - WIND NET NAT timer facility API */

/* Copyright 2004 Wind River Systems, Inc. */

/* @format.tab-size 4, @format.use-tabs true, @format.new-line lf */

/*
modification history
--------------------
01b,11feb04,myz  allow passing 0 internal 
01a,05dec03,myz  created
*/

/* 
DESCRIPTION

This library supplies a timer facility that WIND NET NAT uses internally 
to prune expired translation entries.  However, it is also available for 
external use.  If you are writing your own ALG to work with WIND NET NAT, 
you will find this timer facility easier to use than a generic timer 
facility. To add events to the timer facility, call natTimerEvtCreate() 
or natOneShotTimerEvtCreate().  Each event you add to the queue remains 
dormant until you call natEvtActivate() for that event.  To remove a 
timer event from the queue, call natEvtRemove().
*/

/* ANSI Headers */

#include <stdio.h>
#include <string.h>

#include "dllLib.h"
#include "taskLib.h"
#include "msgQLib.h"
#include "sysLib.h"
#include "tickLib.h"

/* local defines */

#define TYPE_TIMER_EVENT            0x20
#define TYPE_ACTIVATE_EVENT         0x25
#define TYPE_REMOVE_EVENT           0x30
#define TYPE_SHUTDOWN_EVENT         0x35

#define OPTION_ONE_SHOT             0x1
#define OPTION_PERIODIC_TIMER       0x2
#define OPTION_STAY_UNTIL_REMOVED   0x4

#define TIMER_START                 0x200
#define TIMER_STOP                  0x210

#define NAT_TASK_PRIORITY           70
#define NAT_TASK_STACK              10000
#define NAT_TASK_OPTION             0

#define NAT_MESSAGE_NUM_MAX         100

/* event dispatcher descriptor */

typedef struct {
    DL_LIST  timerEvtList;    /* a list of timer events */
    MSG_Q_ID msgQId;          /* the message Q to receive the events */
    int      msgQTimeOutTicks;
    int      taskId;
    ULONG    lastTick; /* the last system tick when task is running*/
    } EVT_DISPATCHER_DESC;

/* event node */

typedef struct EVT_NODE_S {
    DL_NODE node;
    int msgId;
    } EVT_NODE;

typedef struct EVT_NODE_S SHUTDOWN_EVENT_DESC;

typedef struct {
    EVT_NODE evtNode;     /* event node */
    int state;            /* the running state, start or stopped */
    ULONG duration;       /* periodic time interval, a constant */
    ULONG expireInterval; /* current time to expire */
    FUNCPTR pEvtFunc;     /* the event function associated with this event */
    void *  evtFuncContext; /* the event function's argument  */ 
    int     option;
    } TIMER_EVENT_DESC;

typedef struct {
    EVT_NODE evtNode;
    void * eventId;
    } ACTIVATE_EVENT_DESC,REMOVE_EVENT_DESC;

LOCAL EVT_DISPATCHER_DESC natDispatcher;
LOCAL int timerInitCount = 0;

/* local functions */

LOCAL void * timerEvtCreate (EVT_DISPATCHER_DESC *,ULONG,FUNCPTR,void *,int);
LOCAL void dispatchTask (EVT_DISPATCHER_DESC *);
LOCAL void timerUpdateAndEvtFuncExec (EVT_DISPATCHER_DESC *);
LOCAL BOOL nodeSearch (DL_LIST *, DL_NODE *);
LOCAL void freeList (DL_LIST *);


/*****************************************************************************
*
* natTimerInit - initialize the NAT timer 
*
* This function initializes the message queue and task that comprise 
* the NAT timer.  
*
* RETURNS: OK, upon success; or ERROR, upon failure.
*
* NOMANUAL
*
*/

STATUS natTimerInit (void)
    {

    if (timerInitCount)
        {
        timerInitCount++;
        return OK;
        }

    dllInit(&natDispatcher.timerEvtList);

    if ((natDispatcher.msgQId = msgQCreate(NAT_MESSAGE_NUM_MAX,
                                          sizeof(void *),
                                          MSG_Q_FIFO)) == NULL)
        {
        printf("Fail to create the message Q\n");
        return ERROR;
        }

    /* set the clock rate(resolution) 500ms by default */

    natDispatcher.msgQTimeOutTicks = (sysClkRateGet() * 500)/1000;

    natDispatcher.taskId = taskSpawn("tNAT",NAT_TASK_PRIORITY,
                                      NAT_TASK_OPTION,
                                      NAT_TASK_STACK,
                                      (FUNCPTR)dispatchTask,
                                      (int)&natDispatcher,
                                      0,
                                      0,0,0,0,0,0,0,0);

    if (natDispatcher.taskId == ERROR)
	return ERROR;
    
    timerInitCount = 1; 
    return OK;
    }

/******************************************************************************
*
* natOneShotTimerEvtCreate - create a one-shot timer event
*
* This routine is similar to natTimerEvtCreate(), but it sets up a one-time 
* event instead of a recurring event.  This event remains dormant in the 
* queue until you call natEvtActivate() for the event and thus start the clock 
* on the event.  After the <interval> has expired, the function at <pFunc> 
* executes in the context of the 'tNAT' task.  When control returns from 
* the function at <pFunc>, the event is automatically deleted from the 
* message queue and does not repeat.   
* 
* RETURNS: 'eventId', upon success; or NULL, upon failure.  
* 
* Save the 'eventId' for use in the natEvtActivate() call you must make to 
* activate the event. 
*
*
*/

void * natOneShotTimerEvtCreate
    (
    ULONG   interval,  /* the expire time interval */
    FUNCPTR pFunc,     /* event function called when the time expired */
    void *  pArg       /* event function's argument */
    )
    {
    if (!timerInitCount)
        return NULL;

    return timerEvtCreate(&natDispatcher,interval,pFunc,pArg,OPTION_ONE_SHOT);
    }

/******************************************************************************
*
* natTimerEvtCreate - create a repeating timer event
*
* This routine creates a timer event and stores it in an internal message 
* queue processed by 'tNAT' task. After you activate the event (see the 
* netEvtActivate() reference entry), the registered function, <pFunc> executes 
* in the context of the 'tNAT' task every <interval> milliseconds.  
*
* RETURNS: NULL, upon failure; or 'eventId', upon success.  
* 
* Save the 'eventId' for use in the natEvtActivate() call you must make to 
* activate the event. 
*
*/

void * natTimerEvtCreate
    (
    ULONG   interval,  /* the expire time interval, in msec */
    FUNCPTR pFunc,     /* event function called when the time expired */
    void *  pArg       /* event function's argument */
    )
    {
    if (!timerInitCount)
        return NULL;

    return timerEvtCreate(&natDispatcher,interval,pFunc,pArg,
			  OPTION_PERIODIC_TIMER);
    }

/******************************************************************************
*
* timerEvtCreate - perform the timer event creation
*
* Internal implementation of natTimerEvtCreate(). 
*
* NOMANUAL
* 
*/

LOCAL void * timerEvtCreate
    (
    EVT_DISPATCHER_DESC * pDesc,   /* event dispatcher descriptor */
    ULONG   interval,
    FUNCPTR pFunc,
    void *  pArg,       /* event function's argument */
    int     option
    )
    {
    TIMER_EVENT_DESC * pEvt;

    if ((pEvt = (TIMER_EVENT_DESC *)malloc(sizeof(TIMER_EVENT_DESC))) == NULL)
        {
        printf("Fail to allocate the memory for NAT timer event\n");
        return NULL;
        }

    pEvt->evtNode.msgId    = TYPE_TIMER_EVENT;
    pEvt->pEvtFunc         = pFunc;
    pEvt->evtFuncContext   = pArg;
    pEvt->option           = option;
    pEvt->state            = TIMER_STOP;

    /* convert to vxWorks ticks */

    pEvt->duration = (interval * sysClkRateGet())/1000;

    pEvt->expireInterval = pEvt->duration;

    if (msgQSend(pDesc->msgQId,(char *)&pEvt,sizeof(void *),
        sysClkRateGet() >> 2, /*wait for 0.5 sec, not to hang forever in case */
        MSG_PRI_NORMAL) != OK)
   {
        free((void *)pEvt);
        return NULL;
    }

    return ((void *)pEvt);
}

/******************************************************************************
*
* natEvtActivate - activate an event
*
* Call this routine to activate an event already registered with the NAT 
* timer facility. To register a recurring event, call natTimerEvtCreate().  
* To register a one-time event, call natOneShotTimerEvtCreate(). The call 
* back functions registered with these events execute in the context of 
* the 'tNAT' task. 
*
* RETURNS: OK, upon success; or ERROR, upon failure.
*
*/

STATUS natEvtActivate
    (
    void * eventId /* From a successful event creation. */ 
    )
    {
    ACTIVATE_EVENT_DESC * pEvt;

    if (!timerInitCount)
        return ERROR;

    if ((pEvt = (ACTIVATE_EVENT_DESC *)malloc(sizeof(ACTIVATE_EVENT_DESC))) 
	== NULL)
        {
        printf("Fail to allocate the memory for activation event\n");
        return ERROR;
        }

    pEvt->evtNode.msgId    = TYPE_ACTIVATE_EVENT;
    pEvt->eventId          = eventId;

    if (msgQSend(natDispatcher.msgQId,(char *)&pEvt,sizeof(void *),
        sysClkRateGet() >> 2, /*wait for 0.5 sec, not to hang forever in case */
        MSG_PRI_NORMAL) != OK)
        {
        free((void *)pEvt);
        return ERROR;
        }

    return (OK);
    }

/******************************************************************************
*
* natEvtRemove - remove an event from the NAT message queue
*
* Call this routine to remove an event from the message queue processed by 
* the 'tNAT' task.  After calling this function, the event ID is no longer 
* valid.
*
* RETURNS: OK, upon success; or ERROR, upon failure.
*
*/

STATUS natEvtRemove
    (
    void * eventId /* From a successful event creation. */ 
    )
{
	REMOVE_EVENT_DESC * pEvt;

	if (!timerInitCount)
		return ERROR;

	if ((pEvt = (REMOVE_EVENT_DESC *)malloc(sizeof(REMOVE_EVENT_DESC)))== NULL)
	{
		printf("Fail to allocate the memory for removal event\n");
		return ERROR;
	}

	pEvt->evtNode.msgId    = TYPE_REMOVE_EVENT;
	pEvt->eventId          = eventId;

	if (msgQSend(natDispatcher.msgQId,(char *)&pEvt,sizeof(void *),
	sysClkRateGet() >> 2, /*wait for 0.5 sec, not to hang forever in case */
	MSG_PRI_NORMAL) != OK)
	{
		free((void *)pEvt);
		return ERROR;
	}

	return (OK);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -