gateway.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  * @(#)$Id: gateway.c,v 1.4 2010/10/19 18:29:05 adamdunkels Exp $
00030  */
00031 
00032 /*
00033  * Example gateway configuration with two IP interfaces, one SLIP over
00034  * USB and one over 802.11.4 radio.
00035  *
00036  * The IP address is hardcoded to 172.16.0.1 and the 172.16/16 network
00037  * is on the radio interface.
00038  *
00039  * The default route is over the SLIP interface.
00040  *
00041  * On the SLIP host run a standard SLIP implementation or the tunslip
00042  * program that can also view debug printfs, example:
00043  *
00044  * sliphost# ./tunslip 172.16.0.1 255.255.0.0
00045  *
00046  *
00047  * This kernel has no application code but new modules can be uploaded
00048  * using the codeprop program, example:
00049  *
00050  * datan$ ./codeprop 172.16.0.1 loadable_prg.ko
00051  * File successfully sent (1116 bytes)
00052  * Reply: ok
00053  *
00054  */
00055 
00056 #include <stdio.h>
00057 
00058 #include <avr/eeprom.h>
00059 
00060 #include "contiki.h"
00061 
00062 /* Also IP output. */
00063 #include "net/uip-fw-drv.h"
00064 #include "net/uaodv.h"
00065 #include "dev/slip.h"
00066 #include "dev/cc2420.h"
00067 
00068 #include "dev/leds.h"
00069 #include "dev/xmem.h"
00070 
00071 #include "codeprop/codeprop.h"
00072 
00073 /* We have two IP interfaces. */
00074 struct uip_fw_netif cc2420if =
00075 {UIP_FW_NETIF(172,16,0,1, 255,255,0,0, cc2420_send_uaodv)};
00076 
00077 static struct uip_fw_netif slipif =
00078 {UIP_FW_NETIF(0,0,0,0, 255,255,255,255, slip_send)};
00079 
00080 /* Radio stuff in network byte order. */
00081 static u16_t panId = UIP_HTONS(0x2024);
00082 
00083 #ifndef RF_CHANNEL
00084 #define RF_CHANNEL              15
00085 #endif
00086 
00087 unsigned char ds2411_id[8] = { 0x00, 0x12, 0x4b, 0x00, 0x00, 0x00, 0x09, 0x04};
00088 
00089 static void
00090 ds2411_init(void)
00091 {
00092   u8_t i, t;
00093   eeprom_read_block(ds2411_id, 0, 8);
00094   for (i = 0; i < 4; i++) {
00095     t = ds2411_id[i];
00096     ds2411_id[i] = ds2411_id[7 - i];
00097     ds2411_id[7 - i] = t;
00098   }
00099 }
00100 
00101 int
00102 main(int argc, char **argv)
00103 {
00104   /*
00105    * Initalize hardware.
00106    */
00107   cpu_init();
00108   clock_init();
00109   leds_init();
00110   leds_toggle(LEDS_ALL);
00111   slip_arch_init(BAUD2UBR(38400)); /* Must come before first printf */
00112   printf("Starting %s "
00113          "($Id: gateway.c,v 1.4 2010/10/19 18:29:05 adamdunkels Exp $)\n", __FILE__);
00114   ds2411_init();
00115   cc2420_init();
00116   xmem_init();
00117   clock_wait(CLOCK_SECOND/10);
00118   leds_toggle(LEDS_ALL);
00119   /*
00120    * Hardware initialization done!
00121    */
00122   
00123   printf("MAC %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x CHANNEL %d\n",
00124          ds2411_id[0], ds2411_id[1], ds2411_id[2], ds2411_id[3],
00125          ds2411_id[4], ds2411_id[5], ds2411_id[6], ds2411_id[7],
00126          RF_CHANNEL);
00127 
00128   uip_ipaddr_copy(&uip_hostaddr, &cc2420if.ipaddr);
00129   uip_ipaddr_copy(&uip_netmask, &cc2420if.netmask);
00130   printf("IP %d.%d.%d.%d netmask %d.%d.%d.%d\n",
00131          uip_ipaddr_to_quad(&uip_hostaddr),
00132          uip_ipaddr_to_quad(&uip_netmask));
00133   cc2420_set_chan_pan_addr(RF_CHANNEL, panId, uip_hostaddr.u16[1], ds2411_id);
00134 
00135   /*
00136    * Initialize Contiki and our processes.
00137    */
00138   process_init();
00139   process_start(&etimer_process, NULL);
00140 
00141   /* Configure IP stack. */
00142   uip_init();
00143   uip_fw_default(&slipif);      /* Point2point, no default router. */
00144   uip_fw_register(&cc2420if);
00145   tcpip_set_forwarding(1);
00146 
00147   /* Start IP stack. */
00148   process_start(&tcpip_process, NULL);
00149   process_start(&uip_fw_process, NULL); /* Start IP output */
00150   process_start(&slip_process, NULL);
00151   process_start(&cc2420_process, NULL);
00152   cc2420_on();
00153   process_start(&uaodv_process, NULL);
00154 
00155   //process_start(&tcp_loader_process, NULL);
00156 
00157   /*
00158    * This is the scheduler loop.
00159    */
00160   printf("process_run()...\n");
00161   while (1) {
00162     do {
00163       /* Reset watchdog. */
00164     } while(process_run() > 0);
00165     /* Idle! */
00166   }
00167 
00168   return 0;
00169 }

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