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

📄 config.c

📁 ATmega103、ATmega128做的开发板web server源码
💻 C
字号:
/******************************************************************
* config.c: Reads configuration from file server.ini, if not file is
* 			present, default configuration is read
* 
* Copyright (c) 2001 Atmel Corporation.
* All Rights Reserved.
*
* You are autorized to use, copy and distribute this software only at 
* a single site (the term "site" meaning a single company location). 
* This copyright notice must be included in any copy, modification 
* or portion of this software merged into another program.
* 
* This software is licenced solely for use with Atmel AVR micro 
* controller family. The software may not be modified to execute on 
* any other microcontroller architectures
*
* This software is provided "as is"; Without warranties either express
* or implied, including any warranty regarding merchantability, 
* fitness for a particular purpose or noninfringement. 
*
* In no event shall Atmel or its suppliers be liable for any special, 
* indirect,incidential or concequential damages resulting from the 
* use or inability to use this software.
*
* Revision history:
*
* January 17, 2001:   Version 1.0   Created by JB
* July 13, 2001		  Version 1.2     JB
* 					- Changed to IAR compiler V2.25
*					- Renamed flash file functions to avoid conflict with 
*					  standard file I/O names	
*					- Bug fixes in HTTP
*					- Speed optimization in TCP 
*
*******************************************************************/

#include "config.h"
#include "ffile.h"
#include "main.h"
#include <string.h>
#include <stdio.h>
#include "ethernet.h"

extern unsigned char dhcpEnable;
extern config_type ID;

unsigned char config(void)
{
  char buffer[CONFIG_BUFFER_SIZE];
  
  /*get the MAC-address*/
  if ( getOption("System\0","MAC0",buffer) )
  {
    sscanf(buffer,"%x",&ID.MAC0);
  }
  else
  {
    return 0;                       //error, not found  
  }
  if ( getOption("System\0","MAC1",buffer) )
  {
    sscanf(buffer,"%x",&ID.MAC1);
  }
  else
  {
    return 0;                       //error, not found  
  }
  
  if ( getOption("System\0","MAC2",buffer) )
  {
    sscanf(buffer,"%x",&ID.MAC2);
  }
  else
  {
    return 0;                       //error, not found  
  }
  
  if ( getOption("System\0","DHCP",buffer) )
  {
    if ( !strncmp(buffer,"1",1) )
    {
      dhcpEnable=1;
    }
    else
    {
      dhcpEnable=0;
    }
  }
  else
  {
    return 0;                       //error, not found  
  }
  
  /*if dhcp is not to be used, get the static IP-address*/
  if ( !dhcpEnable )                   
  {
    if ( getOption("System\0","IP0",buffer) )
    {
      sscanf(buffer,"%x",&ID.IP0);
    }
    else
    {
      return 0;                       //error, not found  
    } 
    
    if ( getOption("System\0","IP1",buffer) )
    {
      sscanf(buffer,"%x",&ID.IP1);
    }
    else
    {
      return 0;                       //error, not found  
    }
      
  }
  return 1;
}

void defaultConfig(void)
{
  ID.MAC0=CONFIG_DEFAULT_MAC0;
  ID.MAC1=CONFIG_DEFAULT_MAC1;
  ID.MAC2=CONFIG_DEFAULT_MAC2;

  if ( !CONFIG_DEFAULT_DHCP )
  {
    dhcpEnable=0;
    ID.IP0=CONFIG_DEFAULT_IP0;
    ID.IP1=CONFIG_DEFAULT_IP1;  
  }
  else
  {
    dhcpEnable=1;
  }
}

/*getOption searches for an option in server.ini, which should be formated
  as follows:
  
  [header1]
  name1=value1
  name2=value2
  
  [header2]
  .
  .
  .
*/
unsigned char getOption(char * header, char * name, char * targetBuffer)
{
  char buffer[CONFIG_OPTION_BUFFER_SIZE];
  char * searchValue;
  unsigned char read;
  FFILE * server = ffopen("server.ini",'r');
    
  if ( server==NULL )                           //check if server.ini was opened
  {
    return 0;
  }
 
  /*search for header*/
  while ( !ffeof(server) )
  {
    read=ffreadln(server,buffer,CONFIG_OPTION_BUFFER_SIZE);
    buffer[read]='\0';
    if (buffer[0]=='[')
    {
      searchValue=strtok(&buffer[1],"]");
      if (strlen(searchValue)==strlen(header))
      {
        if (!strncmp(header,searchValue,strlen(header)))
        {
          break;                              //header found
        }
      }
    }
  }
  
  /*if the header is not found, eof will be reached and the 
    second while loop's conditon will be false, 0 will be
    returned*/
  
  /*search for the name*/
  while ( !ffeof(server) )
  { 
    read=ffreadln(server,buffer,CONFIG_OPTION_BUFFER_SIZE);
    buffer[read]='\0';
    searchValue=strtok(buffer,"= \t");   
    if (buffer[0]=='['){                       //the start of next header field, stop search
      break; 
    }
    if ( strlen(searchValue)==strlen(name) )
    {
      if ( !strncmp(searchValue,name,strlen(name)) )//name found
      {
        searchValue=strtok(NULL," =\r\n\t");  //extract the value
        strcpy(targetBuffer,searchValue);
        ffclose(server);
        return 1;
      }
    }
  }
  ffclose(server);
  return 0;                                 //not found
}

⌨️ 快捷键说明

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