Contiki 2.5
gateway.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  * @(#)$Id: gateway.c,v 1.4 2010/10/19 18:29:05 adamdunkels Exp $
30  */
31 
32 /*
33  * Example gateway configuration with two IP interfaces, one SLIP over
34  * USB and one over 802.11.4 radio.
35  *
36  * The IP address is hardcoded to 172.16.0.1 and the 172.16/16 network
37  * is on the radio interface.
38  *
39  * The default route is over the SLIP interface.
40  *
41  * On the SLIP host run a standard SLIP implementation or the tunslip
42  * program that can also view debug printfs, example:
43  *
44  * sliphost# ./tunslip 172.16.0.1 255.255.0.0
45  *
46  *
47  * This kernel has no application code but new modules can be uploaded
48  * using the codeprop program, example:
49  *
50  * datan$ ./codeprop 172.16.0.1 loadable_prg.ko
51  * File successfully sent (1116 bytes)
52  * Reply: ok
53  *
54  */
55 
56 #include <stdio.h>
57 
58 #include <avr/eeprom.h>
59 
60 #include "contiki.h"
61 
62 /* Also IP output. */
63 #include "net/uip-fw-drv.h"
64 #include "net/uaodv.h"
65 #include "dev/slip.h"
66 #include "dev/cc2420.h"
67 
68 #include "dev/leds.h"
69 #include "dev/xmem.h"
70 
71 #include "codeprop/codeprop.h"
72 
73 /* We have two IP interfaces. */
74 struct uip_fw_netif cc2420if =
75 {UIP_FW_NETIF(172,16,0,1, 255,255,0,0, cc2420_send_uaodv)};
76 
77 static struct uip_fw_netif slipif =
78 {UIP_FW_NETIF(0,0,0,0, 255,255,255,255, slip_send)};
79 
80 /* Radio stuff in network byte order. */
81 static u16_t panId = UIP_HTONS(0x2024);
82 
83 #ifndef RF_CHANNEL
84 #define RF_CHANNEL 15
85 #endif
86 
87 unsigned char ds2411_id[8] = { 0x00, 0x12, 0x4b, 0x00, 0x00, 0x00, 0x09, 0x04};
88 
89 static void
90 ds2411_init(void)
91 {
92  u8_t i, t;
93  eeprom_read_block(ds2411_id, 0, 8);
94  for (i = 0; i < 4; i++) {
95  t = ds2411_id[i];
96  ds2411_id[i] = ds2411_id[7 - i];
97  ds2411_id[7 - i] = t;
98  }
99 }
100 
101 int
102 main(int argc, char **argv)
103 {
104  /*
105  * Initalize hardware.
106  */
107  cpu_init();
108  clock_init();
109  leds_init();
110  leds_toggle(LEDS_ALL);
111  slip_arch_init(BAUD2UBR(38400)); /* Must come before first printf */
112  printf("Starting %s "
113  "($Id: gateway.c,v 1.4 2010/10/19 18:29:05 adamdunkels Exp $)\n", __FILE__);
114  ds2411_init();
115  cc2420_init();
116  xmem_init();
118  leds_toggle(LEDS_ALL);
119  /*
120  * Hardware initialization done!
121  */
122 
123  printf("MAC %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x CHANNEL %d\n",
124  ds2411_id[0], ds2411_id[1], ds2411_id[2], ds2411_id[3],
125  ds2411_id[4], ds2411_id[5], ds2411_id[6], ds2411_id[7],
126  RF_CHANNEL);
127 
128  uip_ipaddr_copy(&uip_hostaddr, &cc2420if.ipaddr);
129  uip_ipaddr_copy(&uip_netmask, &cc2420if.netmask);
130  printf("IP %d.%d.%d.%d netmask %d.%d.%d.%d\n",
131  uip_ipaddr_to_quad(&uip_hostaddr),
132  uip_ipaddr_to_quad(&uip_netmask));
133  cc2420_set_chan_pan_addr(RF_CHANNEL, panId, uip_hostaddr.u16[1], ds2411_id);
134 
135  /*
136  * Initialize Contiki and our processes.
137  */
138  process_init();
139  process_start(&etimer_process, NULL);
140 
141  /* Configure IP stack. */
142  uip_init();
143  uip_fw_default(&slipif); /* Point2point, no default router. */
144  uip_fw_register(&cc2420if);
145  tcpip_set_forwarding(1);
146 
147  /* Start IP stack. */
148  process_start(&tcpip_process, NULL);
149  process_start(&uip_fw_process, NULL); /* Start IP output */
150  process_start(&slip_process, NULL);
151  process_start(&cc2420_process, NULL);
152  cc2420_on();
153  process_start(&uaodv_process, NULL);
154 
155  //process_start(&tcp_loader_process, NULL);
156 
157  /*
158  * This is the scheduler loop.
159  */
160  printf("process_run()...\n");
161  while (1) {
162  do {
163  /* Reset watchdog. */
164  } while(process_run() > 0);
165  /* Idle! */
166  }
167 
168  return 0;
169 }