Contiki 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 NETSTACK_CONF_WITH_IPV6
48 #include "net/ipv6/uip-ds6.h"
49 #endif /* NETSTACK_CONF_WITH_IPV6 */
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 NETSTACK_CONF_WITH_IPV4
70 #define NETSTACK_CONF_WITH_IPV4 0
71 #endif
72 
73 #if NETSTACK_CONF_WITH_IPV4
74 #include "net/ip/uip.h"
75 #include "net/ipv4/uip-fw.h"
76 #include "net/ipv4/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 /* NETSTACK_CONF_WITH_IPV4 */
84 
85 #define UIP_OVER_MESH_CHANNEL 8
86 #if NETSTACK_CONF_WITH_IPV4
87 static uint8_t is_gateway;
88 #endif /* NETSTACK_CONF_WITH_IPV4 */
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 NETSTACK_CONF_WITH_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 NETSTACK_CONF_WITH_IPV4
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 /* NETSTACK_CONF_WITH_IPV4 */
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 NETSTACK_CONF_WITH_IPV4
210  slip_arch_init(115200);
211 #endif /* NETSTACK_CONF_WITH_IPV4 */
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  /* Restore node id if such has been stored in external mem */
233  node_id_restore();
234 
235  if(!node_id) {
236  node_id = NODE_ID;
237  }
238 
239  /* for setting "hardcoded" IEEE 802.15.4 MAC addresses */
240 #ifdef IEEE_802154_MAC_ADDRESS
241  {
242  uint8_t ieee[] = IEEE_802154_MAC_ADDRESS;
243  //memcpy(ds2411_id, ieee, sizeof(uip_lladdr.addr));
244  //ds2411_id[7] = node_id & 0xff;
245  }
246 #endif
247 
248  //random_init(ds2411_id[0] + node_id);
249 
250  leds_off(LEDS_BLUE);
251  /*
252  * Initialize Contiki and our processes.
253  */
254  process_init();
255  process_start(&etimer_process, NULL);
256 
257  ctimer_init();
258 
259  init_platform();
260 
261  set_rime_addr();
262  random_init(linkaddr_node_addr.u8[LINKADDR_SIZE-2] + linkaddr_node_addr.u8[LINKADDR_SIZE-1]);
263 
264  cc2520_init();
265  {
266  uint8_t longaddr[8];
267  uint16_t shortaddr;
268 
269  shortaddr = (linkaddr_node_addr.u8[0] << 8) +
270  linkaddr_node_addr.u8[1];
271  memset(longaddr, 0, sizeof(longaddr));
272  linkaddr_copy((linkaddr_t *)&longaddr, &linkaddr_node_addr);
273 
274  printf("MAC %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x ",
275  longaddr[0], longaddr[1], longaddr[2], longaddr[3],
276  longaddr[4], longaddr[5], longaddr[6], longaddr[7]);
277 
278  cc2520_set_pan_addr(IEEE802154_PANID, shortaddr, longaddr);
279  }
280  cc2520_set_channel(RF_CHANNEL);
281 
282  printf(CONTIKI_VERSION_STRING " started. ");
283  if(node_id > 0) {
284  printf("Node id is set to %u.\n", node_id);
285  } else {
286  printf("Node id is not set.\n");
287  }
288 
289 #if NETSTACK_CONF_WITH_IPV6
290  /* memcpy(&uip_lladdr.addr, ds2411_id, sizeof(uip_lladdr.addr)); */
291  memcpy(&uip_lladdr.addr, linkaddr_node_addr.u8,
292  UIP_LLADDR_LEN > LINKADDR_SIZE ? LINKADDR_SIZE : UIP_LLADDR_LEN);
293 
294  /* Setup nullmac-like MAC for 802.15.4 */
295 /* sicslowpan_init(sicslowmac_init(&cc2520_driver)); */
296 /* printf(" %s channel %u\n", sicslowmac_driver.name, RF_CHANNEL); */
297 
298  /* Setup X-MAC for 802.15.4 */
299  queuebuf_init();
300  NETSTACK_RDC.init();
301  NETSTACK_MAC.init();
302  NETSTACK_NETWORK.init();
303 
304  printf("%s %s, channel check rate %lu Hz, radio channel %u\n",
305  NETSTACK_MAC.name, NETSTACK_RDC.name,
306  CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1:
307  NETSTACK_RDC.channel_check_interval()),
308  RF_CHANNEL);
309 
310  process_start(&tcpip_process, NULL);
311 
312  printf("Tentative link-local IPv6 address ");
313  {
314  uip_ds6_addr_t *lladdr;
315  int i;
316  lladdr = uip_ds6_get_link_local(-1);
317  for(i = 0; i < 7; ++i) {
318  printf("%02x%02x:", lladdr->ipaddr.u8[i * 2],
319  lladdr->ipaddr.u8[i * 2 + 1]);
320  }
321  printf("%02x%02x\n", lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]);
322  }
323 
324  if(!UIP_CONF_IPV6_RPL) {
325  uip_ipaddr_t ipaddr;
326  int i;
327  uip_ip6addr(&ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0);
328  uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
329  uip_ds6_addr_add(&ipaddr, 0, ADDR_TENTATIVE);
330  printf("Tentative global IPv6 address ");
331  for(i = 0; i < 7; ++i) {
332  printf("%02x%02x:",
333  ipaddr.u8[i * 2], ipaddr.u8[i * 2 + 1]);
334  }
335  printf("%02x%02x\n",
336  ipaddr.u8[7 * 2], ipaddr.u8[7 * 2 + 1]);
337  }
338 
339 #else /* NETSTACK_CONF_WITH_IPV6 */
340 
341  NETSTACK_RDC.init();
342  NETSTACK_MAC.init();
343  NETSTACK_NETWORK.init();
344 
345  printf("%s %s, channel check rate %lu Hz, radio channel %u\n",
346  NETSTACK_MAC.name, NETSTACK_RDC.name,
347  CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0? 1:
348  NETSTACK_RDC.channel_check_interval()),
349  RF_CHANNEL);
350 #endif /* NETSTACK_CONF_WITH_IPV6 */
351 
352 #if !NETSTACK_CONF_WITH_IPV4 && !NETSTACK_CONF_WITH_IPV6
353  uart1_set_input(serial_line_input_byte);
354  serial_line_init();
355 #endif
356 
357  leds_off(LEDS_GREEN);
358 
359 #if TIMESYNCH_CONF_ENABLED
360  timesynch_init();
362 #endif /* TIMESYNCH_CONF_ENABLED */
363 
364 #if NETSTACK_CONF_WITH_IPV4
365  process_start(&tcpip_process, NULL);
366  process_start(&uip_fw_process, NULL); /* Start IP output */
367  process_start(&slip_process, NULL);
368 
369  slip_set_input_callback(set_gateway);
370 
371  {
372  uip_ipaddr_t hostaddr, netmask;
373 
374  uip_init();
375 
376  uip_ipaddr(&hostaddr, 172,16,
378  uip_ipaddr(&netmask, 255,255,0,0);
379  uip_ipaddr_copy(&meshif.ipaddr, &hostaddr);
380 
381  uip_sethostaddr(&hostaddr);
382  uip_setnetmask(&netmask);
383  uip_over_mesh_set_net(&hostaddr, &netmask);
384  /* uip_fw_register(&slipif);*/
385  uip_over_mesh_set_gateway_netif(&slipif);
386  uip_fw_default(&meshif);
387  uip_over_mesh_init(UIP_OVER_MESH_CHANNEL);
388  printf("uIP started with IP address %d.%d.%d.%d\n",
389  uip_ipaddr_to_quad(&hostaddr));
390  }
391 #endif /* NETSTACK_CONF_WITH_IPV4 */
392 
393  energest_init();
394  ENERGEST_ON(ENERGEST_TYPE_CPU);
395 
396  watchdog_start();
397  /* Stop the watchdog */
398  watchdog_stop();
399 
400 #if !PROCESS_CONF_NO_PROCESS_NAMES
401  print_processes(autostart_processes);
402 #else /* !PROCESS_CONF_NO_PROCESS_NAMES */
403  putchar('\n'); /* include putchar() */
404 #endif /* !PROCESS_CONF_NO_PROCESS_NAMES */
405  autostart_start(autostart_processes);
406 
407  /*
408  * This is the scheduler loop.
409  */
410  while(1) {
411 
412  int r;
413  do {
414  /* Reset watchdog. */
416  r = process_run();
417  } while(r > 0);
418 
419  /*
420  * Idle processing.
421  */
422  int s = splhigh(); /* Disable interrupts. */
423  /* uart1_active is for avoiding LPM3 when still sending or receiving */
424  if(process_nevents() != 0 || uart1_active()) {
425  splx(s); /* Re-enable interrupts. */
426  } else {
427  static unsigned long irq_energest = 0;
428 
429  /* Re-enable interrupts and go to sleep atomically. */
430  ENERGEST_SWITCH(ENERGEST_TYPE_CPU, ENERGEST_TYPE_LPM);
431  /* We only want to measure the processing done in IRQs when we
432  are asleep, so we discard the processing time done when we
433  were awake. */
434  energest_type_set(ENERGEST_TYPE_IRQ, irq_energest);
435  watchdog_stop();
436  _BIS_SR(GIE | SCG0 | SCG1 | CPUOFF); /* LPM3 sleep. This
437  statement will block
438  until the CPU is
439  woken up by an
440  interrupt that sets
441  the wake up flag. */
442 
443  /* We get the current processing time for interrupts that was
444  done during the LPM and store it for next time around. */
445  dint();
446  irq_energest = energest_type_time(ENERGEST_TYPE_IRQ);
447  eint();
448  watchdog_start();
449  ENERGEST_SWITCH(ENERGEST_TYPE_LPM, ENERGEST_TYPE_CPU);
450  }
451  }
452 }
453 /*---------------------------------------------------------------------------*/
454 #if LOG_CONF_ENABLED
455 void
456 log_message(char *m1, char *m2)
457 {
458  printf("%s%s\n", m1, m2);
459 }
460 #endif /* LOG_CONF_ENABLED */
void uip_log(char *msg)
Print out a uIP log message.
#define uip_sethostaddr(addr)
Set the IP address of this host.
Definition: uip.h:192
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
Definition: uip-nd6.c:129
#define uip_ipaddr(addr, addr0, addr1, addr2, addr3)
Construct an IP address from four bytes.
Definition: uip.h:956
void timesynch_set_authority_level(int level)
Set the authority level of the current time.
uint8_t slip_send(void)
Send an IP packet from the uIP buffer with SLIP.
Definition: slip.c:193
Header file for IPv6-related data structures.
void uart1_init(unsigned long ubr)
Initalize the RS232 port.
Definition: uart1.c:143
uip_ipaddr_t ipaddr
The IP address of this interface.
Definition: uip-fw.h:57
#define UIP_LLADDR_LEN
Link layer address length.
Definition: uip.h:151
void random_init(unsigned short seed)
Seed the cc2538 random number generator.
Definition: random.c:41
void watchdog_stop(void)
Stops the WDT such that it won't timeout and cause MCU reset.
Definition: watchdog.c:58
Header file for tunnelling uIP over Rime mesh
void clock_init(void)
Initialize the clock library.
Definition: clock.c:76
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1027
int process_run(void)
Run the system once - call poll handlers and process one event.
Definition: process.c:302
Unicast address structure.
Definition: uip-ds6.h:202
void slip_arch_init(unsigned long ubr)
Initalize the RS232 port and the SLIP driver.
Definition: slip-arch.c:56
void slip_set_input_callback(void(*c)(void))
Set a function to be called when there is activity on the SLIP interface; used for detecting if a nod...
Definition: slip.c:143
uip_ipaddr_t netmask
The netmask of the interface.
Definition: uip-fw.h:58
Header file for the Rime stack
#define uip_setnetmask(addr)
Set the netmask.
Definition: uip.h:236
#define NULL
The null pointer.
#define uip_ipaddr_to_quad(a)
Convert an IP address to four bytes separated by commas.
Definition: uip.h:928
802.15.4 frame creation and parsing functions
uIP packet forwarding header file.
void linkaddr_set_node_addr(linkaddr_t *t)
Set the address of the current node.
Definition: linkaddr.c:72
#define UIP_FW_NETIF(ip1, ip2, ip3, ip4, nm1, nm2, nm3, nm4, outputfunc)
Instantiating macro for a uIP network interface.
Definition: uip-fw.h:80
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition: watchdog.c:64
#define ADDR_TENTATIVE
Possible states for the an address (RFC 4862)
Definition: uip-ds6.h:153
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
#define uip_ip6addr(addr, addr0, addr1, addr2, addr3, addr4, addr5, addr6, addr7)
Construct an IPv6 address from eight 16-bit words.
Definition: uip.h:970
Header file for the uIP TCP/IP stack.
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
uip_ds6_addr_t * uip_ds6_addr_add(uip_ipaddr_t *ipaddr, unsigned long vlifetime, uint8_t type)
Add a unicast address to the interface.
Definition: uip-ds6.c:326
Header file for module for automatically starting and exiting a list of processes.
void clock_wait(clock_time_t t)
Wait for a given number of ticks.
Definition: clock.c:162
void timesynch_init(void)
Initialize the timesynch module.
void rtimer_init(void)
Initialize the real-time scheduler.
Definition: rtimer.c:61
Generic serial I/O process header filer.
CCIF uip_lladdr_t uip_lladdr
Host L2 address.
Definition: uip.c:118
void uip_init(void)
uIP initialization function.
Definition: uip.c:363
int process_nevents(void)
Number of events waiting to be processed.
Definition: process.c:316
void process_init(void)
Initialize the process module.
Definition: process.c:208
void ctimer_init(void)
Initialize the callback timer library.
Definition: ctimer.c:91
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a Rime address.
Definition: linkaddr.c:60
linkaddr_t linkaddr_node_addr
The Rime address of the node.
Definition: linkaddr.c:48
int serial_line_input_byte(unsigned char c)
Get one byte of input from the serial driver.
Definition: serial-line.c:60
void watchdog_start(void)
Starts the WDT in watchdog mode if enabled by user configuration, maximum interval.
Definition: watchdog.c:49
Include file for the Contiki low-layer network stack (NETSTACK)
void uip_ds6_set_addr_iid(uip_ipaddr_t *ipaddr, uip_lladdr_t *lladdr)
set the last 64 bits of an IP address based on the MAC address
Definition: uip-ds6.c:542
void uip_fw_default(struct uip_fw_netif *netif)
Register a default network interface.
Definition: uip-fw.c:515
Representation of a uIP network interface.
Definition: uip-fw.h:54