nvparms.c

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

C
1,211
字号
         e = get_file_parms(nvfilename, nv_formats, &line);
      }
      else
         Printu_Net("Can't create sample file either.\n");
   }
   return(e);
}




/* FUNCTION: getnet()
 *
 * get_nvif_nets() - get number of next interface to set up
 * 
 * Networks in the .nv file can be indexed by ones based numbering, zero 
 * based numbering, or names. On the first net we figure out which, then 
 * enforce the system on the remaining nets.
 *
 * PARAM1: char * parm
 * PARAM2: struct nvparm_info * nvinfo_ptr
 *
 * RETURNS: 0 if OK, else -1
 */

static enum { 
   ZERO_BASED, ONES_BASED, NAME_BASED 
} iftype = ZERO_BASED;     /* default... */

int
get_nvif_nets(struct nvparm_info * nvinfo_ptr, char * parm)
{
   int   newnet;

   if(netidx == -1)  /* first net in nv file */
   {
      /* see if net parameter is name or number */
      if((*parm >= '0') && (*parm <= '9'))
      {
         /* see if first number is zero or one. */
         if(*parm == '0')
            iftype = ZERO_BASED;
         else if(*parm == '1')
            iftype = ONES_BASED;
         else
         {
            Printu_Net("First Net number must be 0 or 1");
            return -1;
         }
      }
      else
         iftype = NAME_BASED;
   }

   switch(iftype)
   {
   case ONES_BASED:
      newnet = atoi(parm);
      if (newnet < 1 || newnet > MAXNETS)
      {
         Printu_Net("Net numbers must be 1-%d", MAXNETS);
         return -1;
      }
      newnet--;      /* convert to zero based C array index */
      goto check_nextnet;
   case ZERO_BASED:
      newnet = atoi(parm);
      if (newnet < 0 || newnet >= MAXNETS)
      {
         Printu_Net("Net numbers must be 0-%d", MAXNETS - 1 );
         return -1;
      }
check_nextnet:
      if (newnet != (netidx + 1))
      {
         Printu_Net("next net should be %d", netidx + 1);
         return -1;
      }
      netidx = newnet;
      /* set ones based index number as textual name */

      *(((struct ifinfo *)(nvinfo_ptr->nvdata) + netidx)->name) = \
      (char)('1' + netidx);   

      break;
   case NAME_BASED:
      if(isdigit(*parm))
      {
         Printu_Net("Net must be all single digits or all names");
         Printu_Net("which don't start with a digit\n");
         return -1;
      }
      if(strlen(parm) >= IF_NAMELEN)
      {
         Printu_Net("interface names must be shorter than %d chars", IF_NAMELEN);
         return -1;
      }
      netidx++;   /* bump index to next ifs entry */
      /* copy in name */
      strncpy(((struct ifinfo *)(nvinfo_ptr->nvdata) + netidx)->name, parm, \
              IF_NAMELEN - 1);
      break;
   }
   return 0;   /* OK return */
}

/* IP address options */


/* FUNCTION: get_nvipdec_nets()
 *
 * getip - get Internet ip address for the NETS array
 *
 * 
 * PARAM1: char * parm
 *
 * RETURNS: 
 */

int 
get_nvipdec_nets(struct nvparm_info * nvinfo_ptr, char * parm)
{
   char *   cp;
   unsigned subnet;     /* dummy for passing to parse_ipad() */

   switch(nvinfo_ptr->nvtype)
   {
   case NVIPDEC_NETS:
      cp = parse_ipad((ip_addr *)&(((struct ifinfo *)(nvinfo_ptr->nvdata) + \
                      netidx)->ipaddr), &subnet, parm);
      break;
   case NVSBDEC_NETS:
      cp = parse_ipad((ip_addr *)&(((struct ifinfo *)(nvinfo_ptr->nvdata) + \
                      netidx)->subnet), &subnet, parm);
      break;
   case NVGTDEC_NETS:
      cp = parse_ipad((ip_addr *)&(((struct ifinfo *)(nvinfo_ptr->nvdata) + \
                      netidx)->gateway), &subnet, parm);
      break;
   default:   /* Bad programming ? */
      dprintf("get_nv_value: unknown nvtype in nvparm_format %d\n", \
               nvinfo_ptr->nvtype );
      return -1;
   }

   if (cp)
   {
      Printu_Net(IPerr, line, cp);
      return -1;
   }
   return 0;
}


/* FUNCTION: get_nvbool_nets()
 *
 * Parse boolean parameters for the NETS Array.
 *
 * Right now we only use this to check if the DHCP client
 * is enabled on the iface. Can be extended for other Nets
 * boolean variables.
 * 
 * PARAM1: char* parm
 *
 * RETURNS:
 */

int
get_nvbool_nets(struct nvparm_info * nvinfo_ptr, char* parm)
{
   nv_bool(parm, &(((struct ifinfo *)(nvinfo_ptr->nvdata) + \
                   netidx)->client_dhcp));
   return 0;
}

#ifdef DNS_CLIENT

/* FUNCTION: get_nvdnssrv()
 * 
 * PARAM1: struct nvparm_info * nvinfo_ptr
 * PARAM2: char * parm
 *
 * RETURNS: 
 */

int 
get_nvdnssrv(struct nvparm_info * nvinfo_ptr, char * parm)
{
   char *   cp;
   unsigned subnet;     /* dummy for passing to parse_ipad() */
   int   svr_num;

   svr_num = atoi(parm);
   if (svr_num < 1 || svr_num > MAXDNSSERVERS)
   {
      Printu_Net("Error in line %d; DNS server number must be 1-%d\n",
       line, MAXDNSSERVERS);
      return -1;
   }
   parm += 4;  /* point to IP address field */

   cp = parse_ipad((ip_addr *)nvinfo_ptr->nvdata + (svr_num - 1), \
                   &subnet, parm);

   if (cp)
   {
      Printu_Net(IPerr, line, cp);
      return -1;
   }
   return 0;
}
#endif   /* DNS_CLIENT */

/* FUNCTION: get_nvbool()
 *
 * 
 * PARAM1: char* parm
 *
 * RETURNS: 
 */
int
get_nvbool(struct nvparm_info * nvinfo_ptr, char* parm)
{
   nv_bool(parm, (int *)(nvinfo_ptr->nvdata));
   return 0;
}

/* FUNCTION: get_nvint()
 * 
 * PARAM1: char* parm
 *
 * RETURNS: 
 */

int
get_nvint(struct nvparm_info * nvinfo_ptr, char* parm)
{
   *(int *)nvinfo_ptr->nvdata = (int)atoi(parm);
   return 0;
}


/* FUNCTION: get_nvunshort()
 * 
 * PARAM1: char* parm
 *
 * RETURNS: 
 */

int
get_nvunshort(struct nvparm_info * nvinfo_ptr, char* parm)
{
   *(unshort *)nvinfo_ptr->nvdata = (unshort)atoi(parm);
   return 0;
}

/* FUNCTION: get_nvstring()
 * 
 * PARAM1: char* parm
 *
 * RETURNS: 
 */

int
get_nvstring(struct nvparm_info * nvinfo_ptr, char* parm)
{
   strncpy((char *)(nvinfo_ptr->nvdata), parm, nvinfo_ptr->nvbound);
   return 0;
}


/* FUNCTION: get_nvipdec()
 *
 * get_nvipdec - get Internet ip address
 *
 * 
 * PARAM1: char * parm
 *
 * RETURNS: 
 */
int 
get_nvipdec(struct nvparm_info * nvinfo_ptr, char * parm)
{
   char *   cp;
   unsigned subnet;   /* dummy for passing to parse_ipad() */

   cp = parse_ipad((ip_addr *)&(nvinfo_ptr->nvdata), &subnet, parm);
   if (cp)
   {
      Printu_Net(IPerr, line, cp);
      return -1;
   }
   return 0;
}


/* FUNCTION: set_nv_params()
 * 
 * PARAM1: void * pio
 *
 * RETURNS: 
 */

int
set_nv_params(void * pio)
{
   int i;
   NV_FILE * fp;        /* file with nvram data */
   struct nvparm_format * curr_format = nv_formats;

   fp = nv_fopen(nvfilename, "w");
   if (!fp)
   {
      ns_printf(pio, "Unable to open NV Parameters file \"%s\"\n", nvfilename);
      return -1;
   }

   while (curr_format)
   {
      curr_format->setfunc(fp);
      curr_format = curr_format->next_format;
   }

   nv_fclose(fp);

#ifdef HT_SYNCDEV
   if (nv_writeflash)   /* make sure pointer is set */
      nv_writeflash();  /* call optional per-port flash write routine */
#endif

   return 0;
}

/* FUNCTION: inet_nvset()
 * 
 * PARAM1: NV_FILE * fp
 *
 * RETURNS: Silent return of 0 for OK
 */
int inet_nvset(NV_FILE * fp)
{
int j;
struct l2b ip;  /* structure for IP address conversions */
int iface;      /* nets[] index */

   /* fill in nvparms from set active values */

   /* IP addressing parameters section: */

   for (iface = 0; iface < MAXNETS; iface++)
   {
      j = 0;    /* inet_nvformats element currently being witten */
      nv_fprintf(fp, inet_nvformats[j++].pattern, iface);

      ip.ip.iplong = inet_nvparms.ifs[iface].ipaddr;
      nv_fprintf(fp, inet_nvformats[j++].pattern,  ip.ip.ipchar[0], 
                 ip.ip.ipchar[1], ip.ip.ipchar[2], ip.ip.ipchar[3]);

      ip.ip.iplong = inet_nvparms.ifs[iface].subnet;
      nv_fprintf(fp, inet_nvformats[j++].pattern,  ip.ip.ipchar[0], 
                 ip.ip.ipchar[1], ip.ip.ipchar[2], ip.ip.ipchar[3]);

      ip.ip.iplong = inet_nvparms.ifs[iface].gateway;
      nv_fprintf(fp, inet_nvformats[j++].pattern,  ip.ip.ipchar[0], 
                 ip.ip.ipchar[1], ip.ip.ipchar[2], ip.ip.ipchar[3]);

      nv_fprintf(fp, inet_nvformats[j++].pattern, 
                 (inet_nvparms.ifs[iface].client_dhcp)?"YES":"NO");
   }

#ifdef DNS_CLIENT
   {
      int   i;
      for (i = 0; i < MAXDNSSERVERS; i++)
      {
         ip.ip.iplong = inet_nvparms.dns_servers[i];
         nv_fprintf(fp, inet_nvformats[j++].pattern,  ip.ip.ipchar[0], 
                    ip.ip.ipchar[1], ip.ip.ipchar[2], ip.ip.ipchar[3]);
      }
   }

#ifdef DNS_CLIENT_UPDT
   nv_fprintf(fp, inet_nvformats[j++].pattern, inet_nvparms.dns_zone_name);
#endif   /* DNS_CLIENT_UPDT */

#endif   /* DNS_CLIENT */

   return 0;
}

#ifdef USE_COMPORT
/* FUNCTION: comport_nvset()
 * 
 * PARAM1: NV_FILE * fp
 *
 * RETURNS: Silent return of 0 for OK
 */
int comport_nvset(NV_FILE * fp)
{
int i = 0;

   if (comport_nvparms.comport != '1'  && comport_nvparms.comport !='2' )
      comport_nvparms.comport='1';

⌨️ 快捷键说明

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