Contiki-Inga 3.x
contiki-wismote-main.c
1 /*
2  * Copyright (c) 2011, 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  */
30 
31 #include "contiki.h"
32 #include <stdio.h>
33 #include <string.h>
34 
35 #include "dev/cc2520/cc2520.h"
36 //#include "dev/ds2411.h"
37 #include "dev/leds.h"
38 #include "dev/serial-line.h"
39 #include "dev/slip.h"
40 #include "dev/uart1.h"
41 #include "dev/watchdog.h"
42 #include "dev/xmem.h"
43 #include "lib/random.h"
44 #include "net/netstack.h"
45 #include "net/mac/frame802154.h"
46 
47 #if WITH_UIP6
48 #include "net/ipv6/uip-ds6.h"
49 #endif /* WITH_UIP6 */
50 
51 #include "net/rime/rime.h"
52 
53 #include "sys/node-id.h"
54 #include "sys/autostart.h"
55 
56 #if UIP_CONF_ROUTER
57 
58 #ifndef UIP_ROUTER_MODULE
59 #ifdef UIP_CONF_ROUTER_MODULE
60 #define UIP_ROUTER_MODULE UIP_CONF_ROUTER_MODULE
61 #else /* UIP_CONF_ROUTER_MODULE */
62 #define UIP_ROUTER_MODULE rimeroute
63 #endif /* UIP_CONF_ROUTER_MODULE */
64 #endif /* UIP_ROUTER_MODULE */
65 
66 extern const struct uip_router UIP_ROUTER_MODULE;
67 #endif /* UIP_CONF_ROUTER */
68 
69 #ifndef WITH_UIP
70 #define WITH_UIP 0
71 #endif
72 
73 #if WITH_UIP
74 #include "net/ip/uip.h"
75 #include "net/ipv4/uip-fw.h"
76 #include "net/uip-fw-drv.h"
77 #include "net/ipv4/uip-over-mesh.h"
78 static struct uip_fw_netif slipif =
79  {UIP_FW_NETIF(192,168,1,2, 255,255,255,255, slip_send)};
80 static struct uip_fw_netif meshif =
81  {UIP_FW_NETIF(172,16,0,0, 255,255,0,0, uip_over_mesh_send)};
82 
83 #endif /* WITH_UIP */
84 
85 #define UIP_OVER_MESH_CHANNEL 8
86 #if WITH_UIP
87 static uint8_t is_gateway;
88 #endif /* WITH_UIP */
89 
90 #ifdef EXPERIMENT_SETUP
91 #include "experiment-setup.h"
92 #endif
93 
94 void init_platform(void);
95 
96 /*---------------------------------------------------------------------------*/
97 #if 0
98 int
99 force_float_inclusion()
100 {
101  extern int __fixsfsi;
102  extern int __floatsisf;
103  extern int __mulsf3;
104  extern int __subsf3;
105 
106  return __fixsfsi + __floatsisf + __mulsf3 + __subsf3;
107 }
108 #endif
109 /*---------------------------------------------------------------------------*/
110 void uip_log(char *msg) { puts(msg); }
111 /*---------------------------------------------------------------------------*/
112 #ifndef RF_CHANNEL
113 #define RF_CHANNEL 26
114 #endif
115 /*---------------------------------------------------------------------------*/
116 #if 0
117 void
118 force_inclusion(int d1, int d2)
119 {
120  snprintf(NULL, 0, "%d", d1 % d2);
121 }
122 #endif
123 /*---------------------------------------------------------------------------*/
124 #ifndef NODE_ID
125 #define NODE_ID 0x03
126 #endif /* NODE_ID */
127 static void
128 set_rime_addr(void)
129 {
130  linkaddr_t n_addr;
131  int i;
132 
133  memset(&n_addr, 0, sizeof(linkaddr_t));
134 
135  // Set node address
136 #if UIP_CONF_IPV6
137  //memcpy(addr.u8, ds2411_id, sizeof(addr.u8));
138  n_addr.u8[7] = node_id & 0xff;
139  n_addr.u8[6] = node_id >> 8;
140 #else
141  /* if(node_id == 0) {
142  for(i = 0; i < sizeof(linkaddr_t); ++i) {
143  addr.u8[i] = ds2411_id[7 - i];
144  }
145  } else {
146  addr.u8[0] = node_id & 0xff;
147  addr.u8[1] = node_id >> 8;
148  }*/
149  n_addr.u8[0] = node_id & 0xff;
150  n_addr.u8[1] = node_id >> 8;
151 #endif
152 
153  linkaddr_set_node_addr(&n_addr);
154  printf("Rime started with address ");
155  for(i = 0; i < sizeof(n_addr.u8) - 1; i++) {
156  printf("%d.", n_addr.u8[i]);
157  }
158  printf("%d\n", n_addr.u8[i]);
159 }
160 /*---------------------------------------------------------------------------*/
161 #if !PROCESS_CONF_NO_PROCESS_NAMES
162 static void
163 print_processes(struct process * const processes[])
164 {
165  /* const struct process * const * p = processes;*/
166  printf("Starting");
167  while(*processes != NULL) {
168  printf(" '%s'", (*processes)->name);
169  processes++;
170  }
171  putchar('\n');
172 }
173 #endif /* !PROCESS_CONF_NO_PROCESS_NAMES */
174 /*--------------------------------------------------------------------------*/
175 #if WITH_UIP
176 static void
177 set_gateway(void)
178 {
179  if(!is_gateway) {
180  leds_on(LEDS_RED);
181  //printf("%d.%d: making myself the IP network gateway.\n\n",
182  // linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1]);
183  //printf("IPv4 address of the gateway: %d.%d.%d.%d\n\n",
184  // uip_ipaddr_to_quad(&uip_hostaddr));
185  uip_over_mesh_set_gateway(&linkaddr_node_addr);
186  uip_over_mesh_make_announced_gateway();
187  is_gateway = 1;
188  }
189 }
190 #endif /* WITH_UIP */
191 /*---------------------------------------------------------------------------*/
192 int
193 main(int argc, char **argv)
194 {
195  /*
196  * Initalize hardware.
197  */
198 
199  msp430_cpu_init();
200  clock_init();
201  leds_init();
202 
203  leds_on(LEDS_RED);
204 
205  clock_wait(2);
206 
207  uart1_init(115200); /* Must come before first printf */
208 
209 #if WITH_UIP
210  slip_arch_init(115200);
211 #endif /* WITH_UIP */
212 
213  clock_wait(1);
214 
215  leds_on(LEDS_GREEN);
216  //ds2411_init();
217 
218  /* XXX hack: Fix it so that the 802.15.4 MAC address is compatible
219  with an Ethernet MAC address - byte 0 (byte 2 in the DS ID)
220  cannot be odd. */
221  //ds2411_id[2] &= 0xfe;
222 
223  leds_on(LEDS_BLUE);
224  //xmem_init();
225 
226  leds_off(LEDS_RED);
227  rtimer_init();
228  /*
229  * Hardware initialization done!
230  */
231 
232  node_id = NODE_ID;
233 
234  /* Restore node id if such has been stored in external mem */
235  //node_id_restore();
236 
237  /* for setting "hardcoded" IEEE 802.15.4 MAC addresses */
238 #ifdef IEEE_802154_MAC_ADDRESS
239  {
240  uint8_t ieee[] = IEEE_802154_MAC_ADDRESS;
241  //memcpy(ds2411_id, ieee, sizeof(uip_lladdr.addr));
242  //ds2411_id[7] = node_id & 0xff;
243  }
244 #endif
245 
246  //random_init(ds2411_id[0] + node_id);
247 
248  leds_off(LEDS_BLUE);
249  /*
250  * Initialize Contiki and our processes.
251  */
252  process_init();
253  process_start(&etimer_process, NULL);
254 
255  ctimer_init();
256 
257  init_platform();
258 
259  set_rime_addr();
260 
261  cc2520_init();
262  {
263  uint8_t longaddr[8];
264  uint16_t shortaddr;
265 
266  shortaddr = (linkaddr_node_addr.u8[0] << 8) +
267  linkaddr_node_addr.u8[1];
268  memset(longaddr, 0, sizeof(longaddr));
269  linkaddr_copy((linkaddr_t *)&longaddr, &linkaddr_node_addr);
270 
271  printf("MAC %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x ",
272  longaddr[0], longaddr[1], longaddr[2], longaddr[3],
273  longaddr[4], longaddr[5], longaddr[6], longaddr[7]);
274 
275  cc2520_set_pan_addr(IEEE802154_PANID, shortaddr, longaddr);
276  }
277  cc2520_set_channel(RF_CHANNEL);
278 
279  printf(CONTIKI_VERSION_STRING " started. ");
280  if(node_id > 0) {
281  printf("Node id is set to %u.\n", node_id);
282  } else {
283  printf("Node id is not set.\n");
284  }
285 
286 #if WITH_UIP6
287  /* memcpy(&uip_lladdr.addr, ds2411_id, sizeof(uip_lladdr.addr)); */
288  memcpy(&uip_lladdr.addr, linkaddr_node_addr.u8,
289  UIP_LLADDR_LEN > LINKADDR_SIZE ? LINKADDR_SIZE : UIP_LLADDR_LEN);
290 
291  /* Setup nullmac-like MAC for 802.15.4 */
292 /* sicslowpan_init(sicslowmac_init(&cc2520_driver)); */
293 /* printf(" %s channel %u\n", sicslowmac_driver.name, RF_CHANNEL); */
294 
295  /* Setup X-MAC for 802.15.4 */
296  queuebuf_init();
297  NETSTACK_RDC.init();
298  NETSTACK_MAC.init();
299  NETSTACK_NETWORK.init();
300 
301  printf("%s %s, channel check rate %lu Hz, radio channel %u\n",
302  NETSTACK_MAC.name, NETSTACK_RDC.name,
303  CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1:
304  NETSTACK_RDC.channel_check_interval()),
305  RF_CHANNEL);
306 
307  process_start(&tcpip_process, NULL);
308 
309  printf("Tentative link-local IPv6 address ");
310  {
311  uip_ds6_addr_t *lladdr;
312  int i;
313  lladdr = uip_ds6_get_link_local(-1);
314  for(i = 0; i < 7; ++i) {
315  printf("%02x%02x:", lladdr->ipaddr.u8[i * 2],
316  lladdr->ipaddr.u8[i * 2 + 1]);
317  }
318  printf("%02x%02x\n", lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]);
319  }
320 
321  if(!UIP_CONF_IPV6_RPL) {
322  uip_ipaddr_t ipaddr;
323  int i;
324  uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
325  uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
326  uip_ds6_addr_add(&ipaddr, 0, ADDR_TENTATIVE);
327  printf("Tentative global IPv6 address ");
328  for(i = 0; i < 7; ++i) {
329  printf("%02x%02x:",
330  ipaddr.u8[i * 2], ipaddr.u8[i * 2 + 1]);
331  }
332  printf("%02x%02x\n",
333  ipaddr.u8[7 * 2], ipaddr.u8[7 * 2 + 1]);
334  }
335 
336 #else /* WITH_UIP6 */
337 
338  NETSTACK_RDC.init();
339  NETSTACK_MAC.init();
340  NETSTACK_NETWORK.init();
341 
342  printf("%s %s, channel check rate %lu Hz, radio channel %u\n",
343  NETSTACK_MAC.name, NETSTACK_RDC.name,
344  CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0? 1:
345  NETSTACK_RDC.channel_check_interval()),
346  RF_CHANNEL);
347 #endif /* WITH_UIP6 */
348 
349 #if !WITH_UIP && !WITH_UIP6
350  uart1_set_input(serial_line_input_byte);
351  serial_line_init();
352 #endif
353 
354  leds_off(LEDS_GREEN);
355 
356 #if TIMESYNCH_CONF_ENABLED
357  timesynch_init();
359 #endif /* TIMESYNCH_CONF_ENABLED */
360 
361 #if WITH_UIP
362  process_start(&tcpip_process, NULL);
363  process_start(&uip_fw_process, NULL); /* Start IP output */
364  process_start(&slip_process, NULL);
365 
366  slip_set_input_callback(set_gateway);
367 
368  {
369  uip_ipaddr_t hostaddr, netmask;
370 
371  uip_init();
372 
373  uip_ipaddr(&hostaddr, 172,16,
375  uip_ipaddr(&netmask, 255,255,0,0);
376  uip_ipaddr_copy(&meshif.ipaddr, &hostaddr);
377 
378  uip_sethostaddr(&hostaddr);
379  uip_setnetmask(&netmask);
380  uip_over_mesh_set_net(&hostaddr, &netmask);
381  /* uip_fw_register(&slipif);*/
382  uip_over_mesh_set_gateway_netif(&slipif);
383  uip_fw_default(&meshif);
384  uip_over_mesh_init(UIP_OVER_MESH_CHANNEL);
385  printf("uIP started with IP address %d.%d.%d.%d\n",
386  uip_ipaddr_to_quad(&hostaddr));
387  }
388 #endif /* WITH_UIP */
389 
390  energest_init();
391  ENERGEST_ON(ENERGEST_TYPE_CPU);
392 
393  watchdog_start();
394  /* Stop the watchdog */
395  watchdog_stop();
396 
397 #if !PROCESS_CONF_NO_PROCESS_NAMES
398  print_processes(autostart_processes);
399 #else /* !PROCESS_CONF_NO_PROCESS_NAMES */
400  putchar('\n'); /* include putchar() */
401 #endif /* !PROCESS_CONF_NO_PROCESS_NAMES */
402  autostart_start(autostart_processes);
403 
404  /*
405  * This is the scheduler loop.
406  */
407  while(1) {
408 
409  int r;
410  do {
411  /* Reset watchdog. */
413  r = process_run();
414  } while(r > 0);
415 
416  /*
417  * Idle processing.
418  */
419  int s = splhigh(); /* Disable interrupts. */
420  /* uart1_active is for avoiding LPM3 when still sending or receiving */
421  if(process_nevents() != 0 || uart1_active()) {
422  splx(s); /* Re-enable interrupts. */
423  } else {
424  static unsigned long irq_energest = 0;
425 
426  /* Re-enable interrupts and go to sleep atomically. */
427  ENERGEST_OFF(ENERGEST_TYPE_CPU);
428  ENERGEST_ON(ENERGEST_TYPE_LPM);
429  /* We only want to measure the processing done in IRQs when we
430  are asleep, so we discard the processing time done when we
431  were awake. */
432  energest_type_set(ENERGEST_TYPE_IRQ, irq_energest);
433  watchdog_stop();
434  _BIS_SR(GIE | SCG0 | SCG1 | CPUOFF); /* LPM3 sleep. This
435  statement will block
436  until the CPU is
437  woken up by an
438  interrupt that sets
439  the wake up flag. */
440 
441  /* We get the current processing time for interrupts that was
442  done during the LPM and store it for next time around. */
443  dint();
444  irq_energest = energest_type_time(ENERGEST_TYPE_IRQ);
445  eint();
446  watchdog_start();
447  ENERGEST_OFF(ENERGEST_TYPE_LPM);
448  ENERGEST_ON(ENERGEST_TYPE_CPU);
449  }
450  }
451 }
452 /*---------------------------------------------------------------------------*/
453 #if LOG_CONF_ENABLED
454 void
455 log_message(char *m1, char *m2)
456 {
457  printf("%s%s\n", m1, m2);
458 }
459 #endif /* LOG_CONF_ENABLED */