Contiki 2.5
config.c
1 /*
2  * Copyright (c) 2007, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * Author: Oliver Schmidt <ol.sc@web.de>
32  *
33  * $Id: config.c,v 1.7 2010/09/29 21:48:54 oliverschmidt Exp $
34  */
35 
36 #include <stdlib.h>
37 #include <string.h>
38 
39 #include "contiki-net.h"
40 #include "cfs/cfs.h"
41 #include "sys/log.h"
42 #include "lib/error.h"
43 #include "net/ethernet-drv.h"
44 
45 /*-----------------------------------------------------------------------------------*/
46 #if LOG_CONF_ENABLED
47 static char * CC_FASTCALL
48 ipaddrtoa(uip_ipaddr_t *ipaddr, char *buffer)
49 {
50  char *ptr = buffer;
51  u8_t i;
52 
53  for(i = 0; i < 4; ++i) {
54  *ptr = '.';
55  utoa(ipaddr->u8[i], ++ptr, 10);
56  ptr += strlen(ptr);
57  }
58 
59  return buffer + 1;
60 }
61 #endif /* LOG_CONF_ENABLED */
62 /*-----------------------------------------------------------------------------------*/
63 struct ethernet_config * CC_FASTCALL
64 config_read(char *filename)
65 {
66  static struct {
67  uip_ipaddr_t hostaddr;
68  uip_ipaddr_t netmask;
69  uip_ipaddr_t draddr;
70  uip_ipaddr_t resolvaddr;
71  struct ethernet_config ethernetcfg;
72  } config;
73  int file;
74 
75  file = cfs_open(filename, CFS_READ);
76  if(file < 0) {
77  log_message(filename, ": File not found");
78  error_exit();
79  }
80 
81  if(cfs_read(file, &config, sizeof(config)) < sizeof(config)
82  - sizeof(config.ethernetcfg.name)) {
83  log_message(filename, ": No config file");
84  error_exit();
85  }
86 
87  cfs_close(file);
88 
89  log_message("IP Address: ", ipaddrtoa(&config.hostaddr, uip_buf));
90  log_message("Subnet Mask: ", ipaddrtoa(&config.netmask, uip_buf));
91  log_message("Def. Router: ", ipaddrtoa(&config.draddr, uip_buf));
92  log_message("DNS Server: ", ipaddrtoa(&config.resolvaddr, uip_buf));
93 
94 #ifndef ETHERNET
95  log_message("Eth. Driver: ", config.ethernetcfg.name);
96 #else /* !ETHERNET */
97  #define _stringize(arg) #arg
98  #define stringize(arg) _stringize(arg)
99  log_message("Eth. Driver: ", stringize(ETHERNET));
100  #undef _stringize
101  #undef stringize
102 #endif /* !ETHERNET */
103  log_message("Driver Port: $", utoa(config.ethernetcfg.addr, uip_buf, 16));
104 
105  uip_sethostaddr(&config.hostaddr);
106  uip_setnetmask(&config.netmask);
107  uip_setdraddr(&config.draddr);
108 #if WITH_DNS
109  resolv_conf(&config.resolvaddr);
110 #endif /* WITH_DNS */
111 
112  return &config.ethernetcfg;
113 }
114 /*-----------------------------------------------------------------------------------*/