www.pudn.com > Firewall_PNE_3_3.zip > usrNvIfFile.c, change:2009-03-16,size:6063b


/* usrNvIfFile.c - platform specific Non-Volatile storage interface routines */

/* Copyright 2004 Wind River Systems, Inc. */
#include "copyright_wrs.h"

/*
modification history
--------------------
01a,11feb04,myz  written
*/


/*
DESCRIPTION

This file provides the platform specific firewall NV storage interface
for a file system supporting the basic I/O described in the chapter 3 of the
VxWorks Programmer's Guide. The interface routines are fileParamsGet(),
fileParamsSet() and fileParamsClose(). These routines should be installed by
calling fwNvFuncsInstall(). The initialization routine fileParamsInit() should
be called directly and with a string as passing parameter specifying the base
directory, for exampe "/tgtsvr/" which is the base directory of the VxWorks
Target Server File System. Currently maximum four files named as prein,input,
forwd and output are expected to be saved under the base directory. 

*/

#include "vxWorks.h"
#include "stdio.h"
#include "ioLib.h"
#include "stdlib.h"
#include "string.h"

#define MAX_PARAM_FILES   10
#define MAX_BASE_DIR_LEN  100

typedef struct {
    char name[8];
    int  id;
    } NET_FILE_DESC;

LOCAL NET_FILE_DESC netFiles[MAX_PARAM_FILES];
LOCAL char fileBaseDir[100];

/******************************************************************************
*
* fileParamsGet - Retrieve the parameter block from NV storage
*
* Read the entire parameter block identified by pParamsId into the use supplied
* buffer pointed to by pParamsBuf.
*
* RETURNS: the actual length of retrieved data or 0 if fails
*/

int fileParamsGet
    (
    char * pParamsId,    /* the parameter block ID, 0 terminated less than or
                          * equal 8 byte(including 0 character) string */
    char * pParamsBuf,   /* buffer to store the parameters */
    int    len,          /* the length of the buffer */
    int    offset     /* offset from the start of the storage */
    )
    {
    int rLen = 0;
    int i;
    int fd;

    char nameBuf[MAX_BASE_DIR_LEN + 8];

    for (i = 0; i < MAX_PARAM_FILES; i++) 
        {
        if (strcmp(netFiles[i].name,pParamsId) == 0)
            {
            /* we have a match */

            break;
            }
        }

    if (i == MAX_PARAM_FILES)
        {
        /* we don't have a match, lets see if we can open it */

        sprintf(nameBuf,"%s%s",fileBaseDir,pParamsId);

        fd = open(nameBuf,O_RDWR,0);
        if (fd == ERROR)
            {
            /* no such file, exit */

	    return 0;
            }

        /* find the slot to store it */

        for (i = 0; i < MAX_PARAM_FILES; i++)
            {
            if (netFiles[i].name[0] == 0)
                {
                strcpy(netFiles[i].name,pParamsId);
                netFiles[i].id = fd;
                break;
                }
            } 

        if (i == MAX_PARAM_FILES)
            {
            printf("ERROR: the descriptor is full\n");
            return 0;
            }
        }

    /*  we have the valid file descriptor open if reaches here */

    lseek(netFiles[i].id,offset,SEEK_SET);
    rLen =  read(netFiles[i].id,pParamsBuf,len);
    return rLen;
    }

/******************************************************************************
*
* fileParamsSet - Set the parameter block to NV storage
*
* Write the entire parameter block pointed to by pParams to the NV storage
* identified by parameter block ID pParamsId.
*
* RETURNS: the actual written length  or 0 if fails
*
*/

int fileParamsSet
    (
    char * pParamsId,    /* the parameter block ID, 0 terminated less than or
                          * equal 8 byte(including 0 character) string */
    char * pParams,   /* parameter block for storing */
    int    len,       /* the length of the parameter block */
    int    offset     /* offset from the start of the storage */
    )
    {
    int wLen = 0;
    int i;
    char nameBuf[MAX_BASE_DIR_LEN + 8];
    int fd;

    for (i = 0; i < MAX_PARAM_FILES; i++) 
        {
        if (strcmp(netFiles[i].name,pParamsId) == 0)
            {
            /* we have a match */

            break;
            }
        }

    if (i == MAX_PARAM_FILES)
        {
        /* we don't have a match, lets see if we can open it */

        sprintf(nameBuf,"%s%s",fileBaseDir,pParamsId);
        fd = open(nameBuf,O_RDWR,0);
        if (fd == ERROR)
            {
            /* no such file, lets create it */

            fd = creat(nameBuf,O_RDWR);
            }

        /* find the slot to store it */

        for (i = 0; i < MAX_PARAM_FILES; i++)
            {
            if (netFiles[i].name[0] == 0)
                {
                strcpy(netFiles[i].name,pParamsId);
                netFiles[i].id = fd;
                break;
                }
            } 

        if (i == MAX_PARAM_FILES)
            {
            printf("ERROR: the descriptor is full\n");
            return 0;
            }
        }

    /*  we have the valid file descriptor open if reaches here */

    lseek(netFiles[i].id,offset,SEEK_SET);
    wLen =  write(netFiles[i].id,pParams,len);

    return wLen;
    }

/*****************************************************************************
*
* fileParamsClose - Close a file
*
*/
int fileParamsClose
    (
    char * pParamsId
    )
    {
    int i;

    for (i = 0; i < MAX_PARAM_FILES; i++)
        {
        if (strcmp(netFiles[i].name,pParamsId) == 0)
            {
            /* we have a match */

            break;
            }
        }

    if (i == MAX_PARAM_FILES)
        return ERROR;

    close(netFiles[i].id);
    bzero(netFiles[i].name,8);
    return OK;
    } 
        

/******************************************************************************
*
* fileParamsInit - Initialize the file interface for NV storage
*
*/

int fileParamsInit
    (
    char * pStr
    )
    {
    
    if (pStr != NULL)
        {
        if (strlen(pStr) > (MAX_BASE_DIR_LEN - 1))
            return ERROR;

        strcpy(fileBaseDir,pStr);
        }
    else
        return ERROR;

    return OK;
    }