nvparms.c

来自「在ARM7和UC/OSII的平台上实现了GPS自动报站的功能,涉及GPS模块LE」· C语言 代码 · 共 1,211 行 · 第 1/3 页

C
1,211
字号
/*
 * FILENAME: nvparms.c
 *
 * Copyright 1998- 2002 By InterNiche Technologies Inc. All rights reserved
 *
 *
 * MODULE: MISCLIB
 *
 * ROUTINES: get_file_parms(), get_nv_value(), get_nv_params(), 
 * ROUTINES: get_nvif_nets(), get_nvipdec_nets(), get_nvbool_nets(), 
 * ROUTINES: get_nvbool(), get_nvint(), get_nvunshort(), get_nvstring(), 
 * ROUTINES: get_nvipdec(), get_nvdnssrv(), nv_bool(), set_nv_params(), 
 * ROUTINES: inet_nvset(), comport_nvset(), install_nvformat(), 
 * ROUTINES: edit_nv_params(), nv_get_sec_num(), nv_read_parse(), 
 * ROUTINES: nv_add_entry(), nv_del_entry(),  nv_del_entry_byid(), 
 * PORTABLE: yes
 */

/* Additional Copyrights: */

/* nvparms.c 
 * Portions Copyright 1996 by NetPort Software Code for a generic 
 * Non-Volatile parameters system. For the basic demo, we assume nv 
 * parms can just be written to a file. On an embedded system, this 
 * probably needs to be replaced with code to write them to nvram or 
 * flash. The following routines get/set the nvram data from a file 
 * into the single global nv_parms structure. Programmers should read 
 * NV data by calling get_nv_params() and then reading the data 
 * itrems from the structure. Writing NV data is done by setting the 
 * data in the structure and calling set_nv_params(), to ensure 
 * backup to NV storage. Most routines silently return 0 if OK, else 
 * print an error message to console and return -1. 
 */

#include "ipport.h"
#include "libport.h"

#ifdef   INCLUDE_NVPARMS   /* whole file can be ifdeffed out */

#ifndef VFS_FILES
#ifndef HT_LOCALFS
#error Must have VFS to use NV parameters system
#endif   /* HT_LOCALFS */
#endif   /* VFS_FILES  */

#include "vfsfiles.h"
#include "nvparms.h"
#include "ip.h"
#include "in_utils.h"

#include "nvfsio.h"

#ifdef DNS_CLIENT
#include "dns.h"
#endif   /* DNS_CLIENT */

char *   nvfilename  = "webport.nv";

/* the IP stack's nvparm structure */
struct inet_nvparam inet_nvparms;

#ifdef USE_COMPORT
struct comport_nvparam comport_nvparms;
#endif   /* USE_COMPORT */

void   nv_bool(char * string, int * boolptr);

/* nvram (actually, file data) get functions */
int get_nvif_nets(struct nvparm_info * nvinfo_ptr, char * parm);
int get_nvipdec_nets(struct nvparm_info * nvinfo_ptr, char * parm);
int get_nvbool_nets(struct nvparm_info * nvinfo_ptr, char* parm);
int get_nvbool(struct nvparm_info * nvinfo_ptr, char* parm);
int get_nvint(struct nvparm_info * nvinfo_ptr, char* parm);
int get_nvunshort(struct nvparm_info * nvinfo_ptr, char* parm);
int get_nvstring(struct nvparm_info * nvinfo_ptr, char* parm);
int get_nvipdec(struct nvparm_info * nvinfo_ptr, char * parm);
#ifdef DNS_CLIENT
int get_nvdnssrv(struct nvparm_info * nvinfo_ptr, char * parm);
#endif   /* DNS_CLIENT */

/* Non-volatile parameters are read from a disk file and filled into 
 * their runtime tables below. We do this with an array of "nvparm_info" 
 * structures, where each entry has text for a single parameter name, 
 * a parameter type, an optional bound, and a pointer to a variable to 
 * store the parsed data. Each Module has an array of "nvparm_info" 
 * structures which it keeps track of with the "nvparm_format" structure.
 * The "nvparm_format" structure has a pointer to the array of "nvparm_info"'s
 * a count for the size of the array, a pointer to a "set" routine to generate 
 * and initialize a default NV file, and finally a pointer to the next module's
 * "nvparm_format" structure. The get_file_parms() routine is called with the
 * name of the file to parse and a pointer to an "nvparm_format" linked list. 
 * It scans the file line by line, looks up the parameters in each module's
 * "nvparm_format" structure's "nvparm_info" array, and calls the appropriate
 * generic parse function to set the associated variable. 
 */

struct nvparm_info inet_nvformats[] = 
{
   {"   Net interface: %u\n",        NVIF_NETS,    NVBND_NOOP, \
    &inet_nvparms.ifs[0], NULL, },
   /* first four iterate for MAXNETS number of nets */
   {"IP address: %u.%u.%u.%u\n",     NVIPDEC_NETS, NVBND_NOOP, \
    &inet_nvparms.ifs[0], NULL, },
   {"subnet mask: %u.%u.%u.%u\n",    NVSBDEC_NETS, NVBND_NOOP, \
    &inet_nvparms.ifs[0], NULL, },
   {"gateway: %u.%u.%u.%u\n",        NVGTDEC_NETS, NVBND_NOOP, \
    &inet_nvparms.ifs[0], NULL, },
   {"DHCP Client: %s\n",             NVBOOL_NETS,  NVBND_NOOP, \
    &inet_nvparms.ifs[0], NULL, }, 

#ifdef DNS_CLIENT
   /* hardcode number of DNS servers, should match MAXDNSSERVERS */
   {"DNS server: 1 - %u.%u.%u.%u\n", NVDNSSRV,     NVBND_NOOP, \
    &inet_nvparms.dns_servers[0], NULL, }, 
   {"DNS server: 2 - %u.%u.%u.%u\n", NVDNSSRV,     NVBND_NOOP, \
    &inet_nvparms.dns_servers[0], NULL, }, 
   {"DNS server: 3 - %u.%u.%u.%u\n", NVDNSSRV,     NVBND_NOOP, \
    &inet_nvparms.dns_servers[0], NULL, }, 
#ifdef DNS_CLIENT_UPDT
   {"DNS zone name: - %s\n",         NVSTRING,     MAX_NVSTRING, \
    &inet_nvparms.dns_zone_name[0], NULL, },
#endif   /* DNS_CLIENT_UPDT */
#endif   /* DNS_CLIENT */
};

#define NUMINET_FORMATS  \
        (sizeof(inet_nvformats)/sizeof(struct nvparm_info))

#ifdef USE_COMPORT
/* Comport nvparms live here, since SLIP which lives in the .\net directory
 * could be using the comport nvparms.
 */
struct nvparm_info comport_nvformats[] = 
{
   {"Comm port: %c\n"    , NVINT, NVBND_NOOP, &comport_nvparms.comport, NULL, },
   {"line protocol: %s\n", NVINT, NVBND_NOOP, &comport_nvparms.LineProtocol, NULL, },
};

#define NUMCOMPORT_FORMATS  \
        (sizeof(comport_nvformats)/sizeof(struct nvparm_info))

#endif   /* USE_COMPORT */


/* per-port routine to pre-initialize new NV parameters */
void (*set_nv_defaults)(void) = NULL;

#ifdef HT_SYNCDEV
/* per-port routine to write nvfs image to flash part */
void (*nv_writeflash)(void) = NULL;
#endif

char *   IPerr = "Bad IP address format in NV file line %d\nerror: %s\n";

static int netidx = -1; /* index of nets[] to configure */

static int line;      /* line number of input file currently being processed */


char * get_nv_value(char *linebuf,struct nvparm_format *parmformat);

int inet_nvset(NV_FILE * fp);

#ifdef USE_COMPORT
int comport_nvset(NV_FILE * fp);
#endif   /* USE_COMPORT */

#ifdef USE_COMPORT
struct nvparm_format comport_format =
{
   NUMCOMPORT_FORMATS,
   &comport_nvformats[0],
   comport_nvset,
   NULL,
}; 
#endif   /* USE_COMPORT */

struct nvparm_format inet_format =
{
   NUMINET_FORMATS,
   &inet_nvformats[0],
   inet_nvset,
#ifdef USE_COMPORT
   &comport_format,
#else
   (struct nvparm_format *)NULL,
#endif   /* USE_COMPORT */
};

struct nvparm_format *nv_formats = &inet_format;


/* FUNCTION: get_file_parms()
 *
 * get_file_parms() - Open an InterNiche style parameters text file, 
 * read it a line at a time, and call the set functions in the 
 * descriptor array passed. Returns 0 if no error, else one of the 
 * ENP_ errors. 
 *
 * 
 * PARAM1: char * filename
 * PARAM2: struct nvparm_info *parminfo
 * PARAM3: int array_items
 * PARAM4: int *readline
 *
 * RETURNS: 
 */

int
get_file_parms(char * filename,       /* file to read (w/path) */
   struct nvparm_format * parmformat, /* array of parmeter descriptors */
   int * readline)                    /* count of lines read from file */
{
   char  linebuf[NV_LINELENGTH];      /* make sure you have a big stack */
   NV_FILE * fp;                      /* file with nvram data */
   int      retval = 0;               /* value to return from this function */
   char *retstr=NULL;

   fp = nv_fopen(filename, "r");
   if (!fp)
   {
      Printu_Net("Unable to open NV Parameters file \"%s\"\n", filename);
      return ENP_NOFILE;
   }

   *readline = 0;                     /* no parameters read yet */

   /* the main file reading loop */
   for ((*readline) = 1; ; (*readline)++)
   {
      if (nv_fgets(linebuf, NV_LINELENGTH, fp) != linebuf)
      {
         retval = 0;
         break;
      }

      retstr=get_nv_value(linebuf,parmformat);

      if (retstr)
      {
         Printu_Net("%s %d, file %s\n", retstr,*readline, filename);
         Printu_Net("content: %s\n", linebuf);
         Printu_Net("Try moving %s to another directory & re-run executable\n",
          filename);
         retval=ENP_PARAM;
         break;
      }
   }
   nv_fclose(fp);
   return retval;
}

/* FUNCTION: get_nv_value()
 *
 * get_nv_value() - Process the line which contains information
 * about the nvparm. General format is <str>: <value>
 * From the <str>, identify the entry in parminfo,
 * and call the set functions in the descriptor array passed. 
 *
 * 
 * PARAM1: char *linebuf - line which is to be processed
 * PARAM2: struct nvparm_info *parminfo
 * PARAM3: int array_items
 *
 * RETURNS: Returns NULL if no error, else error string.
 */

char *
get_nv_value(char *linebuf, struct nvparm_format *parmformat)   
{
   char *   colon_pos;  /* position of colon char ':' in input line */
   char *   lf_pos;     /* position of linefeed in input line */
   int i, j, err;
   struct nvparm_info * curr_infoptr;

   if (linebuf[0] == '\n' || linebuf[0] == '\r')   /* blank line */
      return NULL; /* skip this line */
   if (linebuf[0] == '#')  /* see if line is commented out */
      return NULL; /* skip this line */
   lf_pos = strchr(linebuf, '\n');     /* find possible linefeed */
   if (lf_pos)       /* if linefeed exists, null it over */
      *lf_pos = '\0';
   lf_pos = strchr(linebuf, '\r');     /* find possible carriage-return */
   if (lf_pos)       /* if carriage-return exists, null it over */
      *lf_pos = '\0';
   /* make sure line has a colon and sanity check colon position */
   colon_pos = strchr(linebuf, ':');
   if (colon_pos < &linebuf[5] || colon_pos > &linebuf[50])
   {
      return "get_nv_params: Missing or displaced colon in line";
   }

   /* search nvparm_format's to figure out which parameter it is */
   while (parmformat && parmformat->info_ptr)
   {
      curr_infoptr = parmformat->info_ptr;

      for (j = 0; j < parmformat->count; j++)
      {
         if (strncmp(linebuf, curr_infoptr->pattern, colon_pos-linebuf) == 0)
         {
            switch(curr_infoptr->nvtype)
            {
            case NVBOOL:
               err = get_nvbool(curr_infoptr, colon_pos+2);
               break;
            case NVINT:
               err = get_nvint(curr_infoptr, colon_pos+2);
               break;
            case NVUNSHORT:
               err = get_nvunshort(curr_infoptr, colon_pos+2);
               break;
            case NVSTRING:
               err = get_nvstring(curr_infoptr, colon_pos+2);
               break;
            case NVIPDEC:
               err = get_nvipdec(curr_infoptr, colon_pos+2);
               break;
#ifdef DNS_CLIENT
            case NVDNSSRV:
               err = get_nvdnssrv(curr_infoptr, colon_pos+2);
               break;
#endif
            case NVIF_NETS:
               err = get_nvif_nets(curr_infoptr, colon_pos+2);
               break;
            case NVIPDEC_NETS:
            case NVSBDEC_NETS:
            case NVGTDEC_NETS:
               err = get_nvipdec_nets(curr_infoptr, colon_pos+2);
               break;
            case NVBOOL_NETS:
               err = get_nvbool_nets(curr_infoptr, colon_pos+2);
               break;
            default:    /* proprietary nvtype or is it unknown nvtype ? */
               if (curr_infoptr->nvtype >= 100 && curr_infoptr->nvtype <=200)
               {
                  if (curr_infoptr->get_nvprop_type)
                  {
                     err = curr_infoptr->get_nvprop_type(curr_infoptr, colon_pos+2);
                  }
                  else
                  {
                     Printu_Net("Expecting pointer to proprietary nvparm type ");
                     Printu_Net("parse function.\n");
                     Printu_Net("Please check configuration of the nvparm_info ");
                     Printu_Net("for the pattern = %s\n", curr_infoptr->pattern);
                     dtrap("nvparms 0\n");
                     err = 1;
                  }
               }
               else
               {
                  dprintf("get_nv_value: unknown nvtype in nvparm_format %d\n", \
                          curr_infoptr->nvtype );
                  err = 1;
               }
               break;
            }

            if (err)
               return "error setting value in line";
            else
               return NULL;
         }
         curr_infoptr++;
      }
      parmformat = parmformat->next_format;
   }
   return "Unknown NV parameter string or format in line" ;
}

/* FUNCTION: get_nv_params()
 *
 * get_nv_params() - Read BV params from file or flash into NV 
 * streucture. This is usually called from main()/netmain() Returns: 
 * silently return 0 if OK, else print an error message to console 
 * and return -1. 
 *
 * 
 * PARAM1: 
 *
 * RETURNS: 
 */

int
get_nv_params()
{
   int   e;
   NV_FILE * fp;

   /* invoke the generic parm reader to get TCP/IP system parameters */
   e = get_file_parms(nvfilename, nv_formats, &line);
   if (e == ENP_NOFILE)
   {
      Printu_Net("Creating sample file\n");
      fp = nv_fopen(nvfilename, "w+");
      if (fp)
      {
         if (set_nv_defaults)   /* see if app wants to init defaults */
            set_nv_defaults();
         set_nv_params(NULL);   /* write defaults to new file */

         /* now try again from the flash file we just set up */

⌨️ 快捷键说明

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