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