From 6028fccc394ec8f37a100ea65409a755f3588e91 Mon Sep 17 00:00:00 2001 From: oliverschmidt Date: Tue, 27 Nov 2007 21:42:02 +0000 Subject: [PATCH] Added read function for a simple binary config file. --- cpu/6502/lib/config.c | 88 +++++++++++++++++++++++++++++++++++++++++++ cpu/6502/lib/config.h | 41 ++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 cpu/6502/lib/config.c create mode 100644 cpu/6502/lib/config.h diff --git a/cpu/6502/lib/config.c b/cpu/6502/lib/config.c new file mode 100644 index 000000000..8bbf9ec9f --- /dev/null +++ b/cpu/6502/lib/config.c @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2007, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * Author: Oliver Schmidt + * + * $Id: config.c,v 1.1 2007/11/27 21:42:02 oliverschmidt Exp $ + */ + +#include +#include +#include +#include +#include + +#include "contiki-net.h" +#include "lib/error.h" +#include "net/ethernet-drv.h" + +/*-----------------------------------------------------------------------------------*/ +struct ethernet_config * +config_read(char *filename) +{ + static struct { + uip_ipaddr_t hostaddr; + uip_ipaddr_t netmask; + uip_ipaddr_t draddr; + uip_ipaddr_t resolvaddr; + struct ethernet_config ethernetcfg; + } config; + int file; + + file = open(filename, O_RDONLY); + if(file < 0) { + fprintf(stderr, "%s: %s\n", filename, strerror(errno)); + error_exit(); + } + + if(read(file, &config, sizeof(config)) < sizeof(config) + - sizeof(config.ethernetcfg.name)) { + fprintf(stderr, "%s: %s\n", filename, "No Config File"); + error_exit(); + } + + close(file); + + fprintf(stderr, "IP Address: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&config.hostaddr)); + fprintf(stderr, "Subnet Mask: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&config.netmask)); + fprintf(stderr, "Def. Router: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&config.draddr)); + fprintf(stderr, "DNS Server: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&config.resolvaddr)); + + fprintf(stderr, "Eth. Driver: %s at $%X\n", config.ethernetcfg.name, + config.ethernetcfg.addr); + + uip_sethostaddr(&config.hostaddr); + uip_setnetmask(&config.netmask); + uip_setdraddr(&config.draddr); + resolv_conf(&config.resolvaddr); + + return &config.ethernetcfg; +} +/*-----------------------------------------------------------------------------------*/ diff --git a/cpu/6502/lib/config.h b/cpu/6502/lib/config.h new file mode 100644 index 000000000..ebd42cb2c --- /dev/null +++ b/cpu/6502/lib/config.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2007, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * Author: Oliver Schmidt + * + * $Id: config.h,v 1.1 2007/11/27 21:42:02 oliverschmidt Exp $ + */ + +#ifndef __CONFIG_H__ +#define __CONFIG_H__ + +struct ethernet_config *config_read(char *filename); + +#endif /* __CONFIG_H__ */