00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include <stdio.h>
00042 #include <string.h>
00043 #include <avr/pgmspace.h>
00044
00045 #include "contiki.h"
00046 #include "dev/cc2420.h"
00047 #include "dev/rs232.h"
00048 #include "dev/slip.h"
00049 #include "dev/leds.h"
00050 #include "net/netstack.h"
00051 #include "net/mac/frame802154.h"
00052
00053 #include "dev/ds2401.h"
00054 #include "node-id.h"
00055
00056 #if WITH_UIP6
00057 #include "net/uip-ds6.h"
00058 #endif
00059
00060 #if WITH_UIP
00061 #include "net/uip.h"
00062 #include "net/uip-fw.h"
00063 #include "net/uip-fw-drv.h"
00064 #include "net/uip-over-mesh.h"
00065 static struct uip_fw_netif slipif =
00066 {UIP_FW_NETIF(192,168,1,2, 255,255,255,255, slip_send)};
00067 static struct uip_fw_netif meshif =
00068 {UIP_FW_NETIF(172,16,0,0, 255,255,0,0, uip_over_mesh_send)};
00069
00070 static uint8_t is_gateway;
00071
00072 #endif
00073
00074 #define UIP_OVER_MESH_CHANNEL 8
00075
00076
00077 static void
00078 set_rime_addr(void)
00079 {
00080 rimeaddr_t addr;
00081 int i;
00082
00083 memset(&addr, 0, sizeof(rimeaddr_t));
00084 #if UIP_CONF_IPV6
00085 memcpy(addr.u8, ds2401_id, sizeof(addr.u8));
00086 #else
00087 if(node_id == 0) {
00088 for(i = 0; i < sizeof(rimeaddr_t); ++i) {
00089 addr.u8[i] = ds2401_id[7 - i];
00090 }
00091 } else {
00092 addr.u8[0] = node_id & 0xff;
00093 addr.u8[1] = node_id >> 8;
00094 }
00095 #endif
00096 rimeaddr_set_node_addr(&addr);
00097 printf_P(PSTR("Rime started with address "));
00098 for(i = 0; i < sizeof(addr.u8) - 1; i++) {
00099 printf_P(PSTR("%d."), addr.u8[i]);
00100 }
00101 printf_P(PSTR("%d\n"), addr.u8[i]);
00102 }
00103
00104
00105 #if WITH_UIP
00106 static void
00107 set_gateway(void)
00108 {
00109 if(!is_gateway) {
00110 leds_on(LEDS_RED);
00111 printf_P(PSTR("%d.%d: making myself the IP network gateway.\n\n"),
00112 rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1]);
00113 printf_P(PSTR("IPv4 address of the gateway: %d.%d.%d.%d\n\n"),
00114 uip_ipaddr_to_quad(&uip_hostaddr));
00115 uip_over_mesh_set_gateway(&rimeaddr_node_addr);
00116 uip_over_mesh_make_announced_gateway();
00117 is_gateway = 1;
00118 }
00119 }
00120 #endif
00121
00122 void
00123 init_net(void)
00124 {
00125
00126 set_rime_addr();
00127 cc2420_init();
00128 {
00129 uint8_t longaddr[8];
00130 uint16_t shortaddr;
00131
00132 shortaddr = (rimeaddr_node_addr.u8[0] << 8) +
00133 rimeaddr_node_addr.u8[1];
00134 memset(longaddr, 0, sizeof(longaddr));
00135 rimeaddr_copy((rimeaddr_t *)&longaddr, &rimeaddr_node_addr);
00136 printf_P(PSTR("MAC %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n"),
00137 longaddr[0], longaddr[1], longaddr[2], longaddr[3],
00138 longaddr[4], longaddr[5], longaddr[6], longaddr[7]);
00139
00140 cc2420_set_pan_addr(IEEE802154_PANID, shortaddr, longaddr);
00141 }
00142 cc2420_set_channel(RF_CHANNEL);
00143
00144
00145 #if WITH_UIP6
00146 memcpy(&uip_lladdr.addr, ds2401_id, sizeof(uip_lladdr.addr));
00147
00148
00149
00150
00151
00152 queuebuf_init();
00153 NETSTACK_RDC.init();
00154 NETSTACK_MAC.init();
00155 NETSTACK_NETWORK.init();
00156
00157 printf_P(PSTR("%s %s, channel check rate %d Hz, radio channel %d\n"),
00158 NETSTACK_MAC.name, NETSTACK_RDC.name,
00159 CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1:
00160 NETSTACK_RDC.channel_check_interval()),
00161 RF_CHANNEL);
00162
00163 process_start(&tcpip_process, NULL);
00164
00165 printf_P(PSTR("Tentative link-local IPv6 address "));
00166 {
00167 uip_ds6_addr_t *lladdr;
00168 int i;
00169 lladdr = uip_ds6_get_link_local(-1);
00170 for(i = 0; i < 7; ++i) {
00171 printf_P(PSTR("%02x%02x:"), lladdr->ipaddr.u8[i * 2],
00172 lladdr->ipaddr.u8[i * 2 + 1]);
00173 }
00174 printf_P(PSTR("%02x%02x\n"), lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]);
00175 }
00176
00177 if(!UIP_CONF_IPV6_RPL) {
00178 uip_ipaddr_t ipaddr;
00179 int i;
00180 uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
00181 uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
00182 uip_ds6_addr_add(&ipaddr, 0, ADDR_TENTATIVE);
00183 printf_P(PSTR("Tentative global IPv6 address "));
00184 for(i = 0; i < 7; ++i) {
00185 printf_P(PSTR("%02x%02x:"),
00186 ipaddr.u8[i * 2], ipaddr.u8[i * 2 + 1]);
00187 }
00188 printf_P(PSTR("%02x%02x\n"),
00189 ipaddr.u8[7 * 2], ipaddr.u8[7 * 2 + 1]);
00190 }
00191
00192 #else
00193
00194 NETSTACK_RDC.init();
00195 NETSTACK_MAC.init();
00196 NETSTACK_NETWORK.init();
00197
00198 printf_P(PSTR("%s %s, channel check rate %d Hz, radio channel %d\n"),
00199 NETSTACK_MAC.name, NETSTACK_RDC.name,
00200 CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0? 1:
00201 NETSTACK_RDC.channel_check_interval()),
00202 RF_CHANNEL);
00203 #endif
00204
00205
00206 #if WITH_UIP
00207 uip_ipaddr_t hostaddr, netmask;
00208
00209 uip_init();
00210 uip_fw_init();
00211
00212 process_start(&tcpip_process, NULL);
00213 process_start(&slip_process, NULL);
00214 process_start(&uip_fw_process, NULL);
00215
00216 slip_set_input_callback(set_gateway);
00217
00218
00219 uip_ipaddr(&hostaddr, 172, 16, rimeaddr_node_addr.u8[0],
00220 rimeaddr_node_addr.u8[1]);
00221
00222 uip_ipaddr(&netmask, 255,255,0,0);
00223
00224 uip_ipaddr_copy(&meshif.ipaddr, &hostaddr);
00225
00226 uip_sethostaddr(&hostaddr);
00227
00228 uip_setnetmask(&netmask);
00229
00230 uip_over_mesh_set_net(&hostaddr, &netmask);
00231
00232
00233
00234 uip_over_mesh_set_gateway_netif(&slipif);
00235
00236 uip_fw_default(&meshif);
00237 uip_over_mesh_init(UIP_OVER_MESH_CHANNEL);
00238 printf_P(PSTR("uIP started with IP address %d.%d.%d.%d\n"),
00239 uip_ipaddr_to_quad(&hostaddr));
00240 #endif
00241
00242
00243
00244 }
00245