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