Contiki 3.x
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  */
34 
35 #include <stdlib.h>
36 #include <string.h>
37 
38 #include "contiki-net.h"
39 #include "cfs/cfs.h"
40 #include "sys/log.h"
41 #include "lib/error.h"
42 #include "net/ethernet-drv.h"
43 
44 /*-----------------------------------------------------------------------------------*/
45 #if LOG_CONF_ENABLED
46 static char * CC_FASTCALL
47 ipaddrtoa(uip_ipaddr_t *ipaddr, char *buffer)
48 {
49  char *ptr = buffer;
50  uint8_t i;
51 
52  for(i = 0; i < 4; ++i) {
53  *ptr = '.';
54  utoa(ipaddr->u8[i], ++ptr, 10);
55  ptr += strlen(ptr);
56  }
57 
58  return buffer + 1;
59 }
60 #endif /* LOG_CONF_ENABLED */
61 /*-----------------------------------------------------------------------------------*/
62 struct ethernet_config * CC_FASTCALL
63 config_read(char *filename)
64 {
65  static struct {
66  uip_ipaddr_t hostaddr;
67  uip_ipaddr_t netmask;
68  uip_ipaddr_t draddr;
69  uip_ipaddr_t resolvaddr;
70  struct ethernet_config ethernetcfg;
71  } config;
72  int file;
73 
74  file = cfs_open(filename, CFS_READ);
75  if(file < 0) {
76  log_message(filename, ": File not found");
77  error_exit();
78  }
79 
80  if(cfs_read(file, &config, sizeof(config)) < sizeof(config)
81  - sizeof(config.ethernetcfg.name)) {
82  log_message(filename, ": No config file");
83  error_exit();
84  }
85 
86  cfs_close(file);
87 
88  log_message("IP Address: ", ipaddrtoa(&config.hostaddr, uip_buf));
89  log_message("Subnet Mask: ", ipaddrtoa(&config.netmask, uip_buf));
90  log_message("Def. Router: ", ipaddrtoa(&config.draddr, uip_buf));
91  log_message("DNS Server: ", ipaddrtoa(&config.resolvaddr, uip_buf));
92 
93 #ifndef ETHERNET
94  log_message("Eth. Driver: ", config.ethernetcfg.name);
95 #else /* !ETHERNET */
96  #define _stringize(arg) #arg
97  #define stringize(arg) _stringize(arg)
98  log_message("Eth. Driver: ", stringize(ETHERNET));
99  #undef _stringize
100  #undef stringize
101 #endif /* !ETHERNET */
102  log_message("Driver Port: $", utoa(config.ethernetcfg.addr, uip_buf, 16));
103 
104  uip_sethostaddr(&config.hostaddr);
105  uip_setnetmask(&config.netmask);
106  uip_setdraddr(&config.draddr);
107 #if WITH_DNS
108  resolv_conf(&config.resolvaddr);
109 #endif /* WITH_DNS */
110 
111  return &config.ethernetcfg;
112 }
113 /*-----------------------------------------------------------------------------------*/
#define uip_sethostaddr(addr)
Set the IP address of this host.
Definition: uip.h:195
int cfs_open(const char *name, int flags)
Open a file.
Definition: cfs-coffee.c:996
#define CFS_READ
Specify that cfs_open() should open a file for reading.
Definition: cfs.h:90
#define uip_setdraddr(addr)
Set the default router&#39;s IP address.
Definition: uip.h:227
#define CC_FASTCALL
Configure if the C compiler supports fastcall function declarations.
Definition: cc.h:77
#define uip_setnetmask(addr)
Set the netmask.
Definition: uip.h:239
void cfs_close(int fd)
Close an open file.
Definition: cfs-coffee.c:1032
CFS header file.