config.c

00001 /*
00002  * Copyright (c) 2007, Swedish Institute of Computer Science.
00003  * All rights reserved. 
00004  *
00005  * Redistribution and use in source and binary forms, with or without 
00006  * modification, are permitted provided that the following conditions 
00007  * are met: 
00008  * 1. Redistributions of source code must retain the above copyright 
00009  *    notice, this list of conditions and the following disclaimer. 
00010  * 2. Redistributions in binary form must reproduce the above copyright 
00011  *    notice, this list of conditions and the following disclaimer in the 
00012  *    documentation and/or other materials provided with the distribution. 
00013  * 3. Neither the name of the Institute nor the names of its contributors 
00014  *    may be used to endorse or promote products derived from this software 
00015  *    without specific prior written permission. 
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
00027  * SUCH DAMAGE. 
00028  *
00029  * This file is part of the Contiki operating system.
00030  * 
00031  * Author: Oliver Schmidt <ol.sc@web.de>
00032  *
00033  * $Id: config.c,v 1.7 2010/09/29 21:48:54 oliverschmidt Exp $
00034  */
00035 
00036 #include <stdlib.h>
00037 #include <string.h>
00038 
00039 #include "contiki-net.h"
00040 #include "cfs/cfs.h"
00041 #include "sys/log.h"
00042 #include "lib/error.h"
00043 #include "net/ethernet-drv.h"
00044 
00045 /*-----------------------------------------------------------------------------------*/
00046 #if LOG_CONF_ENABLED
00047 static char * CC_FASTCALL
00048 ipaddrtoa(uip_ipaddr_t *ipaddr, char *buffer)
00049 {
00050   char *ptr = buffer;
00051   u8_t i;
00052 
00053   for(i = 0; i < 4; ++i) {
00054     *ptr = '.';
00055     utoa(ipaddr->u8[i], ++ptr, 10);
00056     ptr += strlen(ptr);
00057   }
00058 
00059   return buffer + 1;
00060 }
00061 #endif /* LOG_CONF_ENABLED */
00062 /*-----------------------------------------------------------------------------------*/
00063 struct ethernet_config * CC_FASTCALL
00064 config_read(char *filename)
00065 {
00066   static struct {
00067     uip_ipaddr_t           hostaddr;
00068     uip_ipaddr_t           netmask;
00069     uip_ipaddr_t           draddr;
00070     uip_ipaddr_t           resolvaddr;
00071     struct ethernet_config ethernetcfg;
00072   } config;
00073   int file;
00074 
00075   file = cfs_open(filename, CFS_READ);
00076   if(file < 0) {
00077     log_message(filename, ": File not found");
00078     error_exit();
00079   }
00080 
00081   if(cfs_read(file, &config, sizeof(config)) < sizeof(config)
00082                                              - sizeof(config.ethernetcfg.name)) {
00083     log_message(filename, ": No config file");
00084     error_exit();
00085   }
00086 
00087   cfs_close(file);
00088 
00089   log_message("IP Address:  ",  ipaddrtoa(&config.hostaddr, uip_buf));
00090   log_message("Subnet Mask: ",  ipaddrtoa(&config.netmask, uip_buf));
00091   log_message("Def. Router: ",  ipaddrtoa(&config.draddr, uip_buf));
00092   log_message("DNS Server:  ",  ipaddrtoa(&config.resolvaddr, uip_buf));
00093 
00094 #ifndef ETHERNET
00095   log_message("Eth. Driver: ",  config.ethernetcfg.name);
00096 #else /* !ETHERNET */
00097   #define _stringize(arg) #arg
00098   #define  stringize(arg) _stringize(arg)
00099   log_message("Eth. Driver: ",  stringize(ETHERNET));
00100   #undef  _stringize
00101   #undef   stringize
00102 #endif /* !ETHERNET */
00103   log_message("Driver Port: $", utoa(config.ethernetcfg.addr, uip_buf, 16));
00104 
00105   uip_sethostaddr(&config.hostaddr);
00106   uip_setnetmask(&config.netmask);
00107   uip_setdraddr(&config.draddr);
00108 #if WITH_DNS
00109   resolv_conf(&config.resolvaddr);
00110 #endif /* WITH_DNS */
00111 
00112   return &config.ethernetcfg;
00113 }
00114 /*-----------------------------------------------------------------------------------*/

Generated on Mon Apr 11 14:23:33 2011 for Contiki 2.5 by  doxygen 1.6.1