/*
 * 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 <ol.sc@web.de>
 *
 * $Id: config.c,v 1.1 2007/11/27 21:42:02 oliverschmidt Exp $
 */

#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

#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;
}
/*-----------------------------------------------------------------------------------*/