Contiki 2.5
contiki-inga-main.c
1 /*
2  * Copyright (c) 2006, Technical University of Munich
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  * This file is part of the Contiki operating system.
30  *
31  * @(#)$$
32  */
33 #define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
34 
35 #define ANNOUNCE_BOOT 1 //adds about 600 bytes to program size
36 #if ANNOUNCE_BOOT
37 #define PRINTA(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
38 #else
39 #define PRINTA(...)
40 #endif
41 
42 #define DEBUG 0
43 #if DEBUG
44 #define PRINTD(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
45 #else
46 #define PRINTD(...)
47 #endif
48 
49 /* Track interrupt flow through mac, rdc and radio driver */
50 #if DEBUGFLOWSIZE
51 uint8_t debugflowsize,debugflow[DEBUGFLOWSIZE];
52 #define DEBUGFLOW(c) if (debugflowsize<(DEBUGFLOWSIZE-1)) debugflow[debugflowsize++]=c
53 #else
54 #define DEBUGFLOW(c)
55 #endif
56 
57 #include <avr/pgmspace.h>
58 #include <avr/fuse.h>
59 #include <avr/eeprom.h>
60 #include <stdio.h>
61 #include <string.h>
62 #include <dev/watchdog.h>
63 #include "lib/sensors.h"
64 #include "dev/button-sensor.h"
66 
67 #include "loader/symbols-def.h"
68 #include "loader/symtab.h"
69 
70 #if RF230BB //radio driver using contiki core mac
71 #include "radio/rf230bb/rf230bb.h"
72 #include "net/mac/frame802154.h"
73 #include "net/mac/framer-802154.h"
74 #include "net/sicslowpan.h"
75 
76 #else //radio driver using Atmel/Cisco 802.15.4'ish MAC
77 #include <stdbool.h>
78 #include "mac.h"
79 #include "sicslowmac.h"
80 #include "sicslowpan.h"
81 #include "ieee-15-4-manager.h"
82 #endif /*RF230BB*/
83 
84 #include "contiki.h"
85 #include "contiki-net.h"
86 #include "contiki-lib.h"
87 #include "node-id.h"
88 
89 #include "dev/rs232.h"
90 #include "dev/serial-line.h"
91 #include "dev/slip.h"
92 
93 #if AVR_WEBSERVER
94 #include "httpd-fs.h"
95 #include "httpd-cgi.h"
96 #endif
97 
98 #ifdef COFFEE_FILES
99 #include "cfs/cfs.h"
100 #include "cfs/cfs-coffee.h"
101 #endif
102 
103 #if UIP_CONF_ROUTER&&0
104 #include "net/routing/rimeroute.h"
105 #include "net/rime/rime-udp.h"
106 #endif
107 
108 #include "net/rime.h"
109 #include "sys/sprofiling.h"
110 #include "sys/profiling.h"
111 
112 /* Get periodic prints from idle loop, from clock seconds or rtimer interrupts */
113 /* Use of rtimer will conflict with other rtimer interrupts such as contikimac radio cycling */
114 /* STAMPS will print ENERGEST outputs if that is enabled. */
115 #define PERIODICPRINTS 1
116 #if PERIODICPRINTS
117 //#define PINGS 64
118 #define ROUTES 600
119 #define STAMPS 60
120 #define STACKMONITOR 600
121 uint32_t clocktime;
122 #define TESTRTIMER 0
123 #if TESTRTIMER
124 uint8_t rtimerflag=1;
125 struct rtimer rt;
126 void rtimercycle(void) {rtimerflag=1;}
127 #endif
128 #endif
129 
130 #if WITH_NODE_ID
131 uint16_t node_id;
132 #endif
133 
134 /*-------------------------------------------------------------------------*/
135 /*----------------------Configuration of the .elf file---------------------*/
136 #if 1
137 /* The proper way to set the signature is */
138 #include <avr/signature.h>
139 #else
140 /* Older avr-gcc's may not define the needed SIGNATURE bytes. Do it manually if you get an error */
141 typedef struct {const unsigned char B2;const unsigned char B1;const unsigned char B0;} __signature_t;
142 #define SIGNATURE __signature_t __signature __attribute__((section (".signature")))
143 SIGNATURE = {
144  .B2 = 0x05,//SIGNATURE_2, //ATMEGA1284p
145  .B1 = 0x97,//SIGNATURE_1, //128KB flash
146  .B0 = 0x1E,//SIGNATURE_0, //Atmel
147 };
148 #endif
149 
150 /* JTAG, SPI enabled, Internal RC osc, Boot flash size 4K, 6CK+65msec delay, brownout disabled */
151 FUSES ={.low = 0xe2, .high = 0x99, .extended = 0xff,};
152 
153 /* Put the default settings into program flash memory */
154 /* Webserver builds can set some defaults in httpd-fsdata.c via makefsdata.h */
155 #if AVR_WEBSERVER
156 extern uint8_t default_mac_address[8];
157 extern uint8_t default_server_name[16];
158 extern uint8_t default_domain_name[30];
159 #else
160 #ifdef MAC_ADDRESS
161 const uint8_t default_mac_address[8] PROGMEM = MAC_ADDRESS;
162 #else
163 const uint8_t default_mac_address[8] PROGMEM = {0x02, 0x11, 0x22, 0xff, 0xfe, 0x33, 0x44, 0x55};
164 #endif
165 #ifdef SERVER_NAME
166 const uint8_t default_server_name[16] PROGMEM = SERVER_NAME;
167 #else
168 const uint8_t default_server_name[16] PROGMEM = "Raven_webserver";
169 #endif
170 #ifdef DOMAIN_NAME
171 const uint8_t default_domain_name[30] PROGMEM = DOMAIN_NAME
172 #else
173 const uint8_t default_domain_name[30] PROGMEM = "localhost";
174 #endif
175 #endif /* AVR_WEBSERVER */
176 
177 #ifdef NODE_ID
178 const uint16_t default_nodeid PROGMEM = NODEID;
179 #else
180 const uint16_t default_nodeid PROGMEM = 0;
181 #endif
182 #ifdef CHANNEL_802_15_4
183 const uint8_t default_channel PROGMEM = CHANNEL_802_15_4;
184 #else
185 const uint8_t default_channel PROGMEM = 26;
186 #endif
187 #ifdef IEEE802154_PANID
188 const uint16_t default_panid PROGMEM = IEEE802154_PANID;
189 #else
190 const uint16_t default_panid PROGMEM = 0xABCD;
191 #endif
192 #ifdef IEEE802154_PANADDR
193 const uint16_t default_panaddr PROGMEM = IEEE802154_PANID;
194 #else
195 const uint16_t default_panaddr PROGMEM = 0;
196 #endif
197 #ifdef RF230_MAX_TX_POWER
198 const uint8_t default_txpower PROGMEM = RF230_MAX_TX_POWER;
199 #else
200 const uint8_t default_txpower PROGMEM = 0;
201 #endif
202 
203 /* Get a pseudo random number using the ADC */
204 static uint8_t
205 rng_get_uint8(void) {
206 uint8_t i,j;
207  ADCSRA=1<<ADEN; //Enable ADC, not free running, interrupt disabled, fastest clock
208  for (i=0;i<4;i++) {
209  ADMUX = 0; //toggle reference to increase noise
210  ADMUX =0x1E; //Select AREF as reference, measure 1.1 volt bandgap reference.
211  ADCSRA|=1<<ADSC; //Start conversion
212  while (ADCSRA&(1<<ADSC)); //Wait till done
213  j = (j<<2) + ADC;
214  }
215  ADCSRA=0; //Disable ADC
216  PRINTD("rng issues %d\n",j);
217  return j;
218 }
219 
220 #if CONTIKI_CONF_RANDOM_MAC
221 static void
222 generate_new_eui64(uint8_t eui64[8]) {
223  eui64[0] = 0x02;
224  eui64[1] = rng_get_uint8();
225  eui64[2] = rng_get_uint8();
226  eui64[3] = 0xFF;
227  eui64[4] = 0xFE;
228  eui64[5] = rng_get_uint8();
229  eui64[6] = rng_get_uint8();
230  eui64[7] = rng_get_uint8();
231 }
232 #endif
233 
234 #if !CONTIKI_CONF_SETTINGS_MANAGER
235 /****************************No settings manager*****************************/
236 /* If not using the settings manager, put the default values into EEMEM
237  * These can be manually changed and kept over program reflash.
238  * The channel and bit complement are used to check EEMEM integrity,
239  * If corrupt all values will be rewritten with the default flash values.
240  * To make this work, get the channel before anything else.
241  */
242 #if AVR_WEBSERVER
243 extern uint8_t eemem_mac_address[8]; //These are defined in httpd-fsdata.c via makefsdata.h
244 extern uint8_t eemem_server_name[16];
245 extern uint8_t eemem_domain_name[30];
246 #else
247 #ifdef MAC_ADDRESS
248 uint8_t eemem_mac_address[8] EEMEM = MAC_ADDRESS;
249 #else
250 uint8_t eemem_mac_address[8] EEMEM = {0x02, 0x11, 0x22, 0xff, 0xfe, 0x33, 0x44, 0x55};
251 #endif
252 #ifdef SERVER_NAME
253 uint8_t eemem_server_name[16] EEMEM = SERVER_NAME;
254 #else
255 uint8_t eemem_server_name[16] EEMEM = "Raven_webserver";
256 #endif
257 #ifdef DOMAIN_NAME
258 uint8_t eemem_domain_name[30] EEMEM = DOMAIN_NAME
259 #else
260 uint8_t eemem_domain_name[30] EEMEM = "localhost";
261 #endif
262 #endif /*AVR_WEBSERVER */
263 
264 #ifdef NODE_ID
265 uint16_t eemem_nodeid EEMEM = NODEID;
266 #else
267 uint16_t eemem_nodeid EEMEM = 0;
268 #endif
269 #ifdef CHANNEL_802_15_4
270 uint8_t eemem_channel[2] EEMEM = {CHANNEL_802_15_4, ~CHANNEL_802_15_4};
271 #else
272 uint8_t eemem_channel[2] EMEM = {26, ~26};
273 #endif
274 #ifdef IEEE802154_PANID
275 uint16_t eemem_panid EEMEM = IEEE802154_PANID;
276 #else
277 uint16_t eemem_panid EEMEM = 0xABCD;
278 #endif
279 #ifdef IEEE802154_PANADDR
280 uint16_t eemem_panaddr EEMEM = IEEE802154_PANADDR;
281 #else
282 uint16_t eemem_panaddr EEMEM = 0;
283 #endif
284 #ifdef RF230_MAX_TX_POWER
285 uint8_t eemem_txpower EEMEM = RF230_MAX_TX_POWER;
286 #else
287 uint8_t eemem_txpower EEMEM = 0;
288 #endif
289 
290 static uint8_t
291 get_channel_from_eeprom() {
292  uint8_t x[2];
293  *(uint16_t *)x = eeprom_read_word ((uint16_t *)&eemem_channel);
294 /* Don't return an invalid channel number */
295  if( (x[0]<11) || (x[0] > 26)) x[1]=x[0];
296 /* Do exclusive or test on the two values read */
297  if((uint8_t)x[0]!=(uint8_t)~x[1]) {//~x[1] can promote comparison to 16 bit
298 /* Verification fails, rewrite everything */
299  uint8_t i,buffer[32];
300  PRINTD("EEPROM is corrupt, rewriting with defaults.\n");
301 #if CONTIKI_CONF_RANDOM_MAC
302  PRINTA("Generating random MAC address.\n");
303  generate_new_eui64(&buffer);
304 #else
305  for (i=0;i<sizeof(default_mac_address);i++) buffer[i] = pgm_read_byte_near(default_mac_address+i);
306 #endif
307  cli();
308  eeprom_write_block(&buffer, &eemem_mac_address, sizeof(eemem_mac_address));
309  for (i=0;i<sizeof(default_server_name);i++) buffer[i] = pgm_read_byte_near(default_server_name+i);
310  eeprom_write_block(&buffer, &eemem_server_name, sizeof(eemem_server_name));
311  for (i=0;i<sizeof(default_domain_name);i++) buffer[i] = pgm_read_byte_near(default_domain_name+i);
312  eeprom_write_block(&buffer, &eemem_domain_name, sizeof(eemem_domain_name));
313  eeprom_write_word(&eemem_panid , pgm_read_word_near(&default_panid));
314  eeprom_write_word(&eemem_panaddr, pgm_read_word_near(&default_panaddr));
315  eeprom_write_byte(&eemem_txpower, pgm_read_byte_near(&default_txpower));
316  eeprom_write_word(&eemem_nodeid, pgm_read_word_near(&default_nodeid));
317  x[0] = pgm_read_byte_near(&default_channel);
318  x[1]= ~x[0];
319  eeprom_write_word((uint16_t *)&eemem_channel, *(uint16_t *)x);
320  sei();
321  }
322 /* Always returns a valid channel */
323  return x[0];
324 }
325 static bool
326 get_eui64_from_eeprom(uint8_t macptr[sizeof(rimeaddr_t)]) {
327  cli();
328  eeprom_read_block ((void *)macptr, &eemem_mac_address, sizeof(rimeaddr_t));
329  sei();
330  return macptr[0]!=0xFF;
331 }
332 /**
333  * Getting the PANID from EEPROM cannot work, because the 802154framer always uses the
334  * IEEE802154_PANID define for outgoing frames
335  */
336 static uint16_t
337 get_panid_from_eeprom(void) {
338  return IEEE802154_PANID;
339 }
340 static uint16_t
341 get_panaddr_from_eeprom(void) {
342  return eeprom_read_word (&eemem_panaddr);
343 }
344 static uint8_t
345 get_txpower_from_eeprom(void)
346 {
347  return eeprom_read_byte(&eemem_txpower);
348 }
349 
350 #else /* !CONTIKI_CONF_SETTINGS_MANAGER */
351 /******************************Settings manager******************************/
352 #include "settings.h"
353 /* Disable the settings add routines to reduce eeprom wear during testing */
354 #if 0
355 #define settings_add(...) 0
356 #define settings_add_uint8(...) 0
357 #define settings_add_uint16(...) 0
358 #endif
359 
360 #if AVR_WEBSERVER
361 extern uint8_t eemem_mac_address[8]; //These are defined in httpd-fsdata.c via makefsdata.h
362 extern uint8_t eemem_server_name[16];
363 extern uint8_t eemem_domain_name[30];
364 #endif
365 
366 static uint8_t
367 get_channel_from_eeprom() {
368  uint8_t x;
369 #if PRESERVE_CHANNEL
370  size_t size = 1;
371  if (settings_get(SETTINGS_KEY_CHANNEL, 0,(unsigned char*)&x, &size) == SETTINGS_STATUS_OK) {
372  if ((x<11) || (x>26)) {
373  PRINTF("Unusual RF channel %u in EEPROM\n",x);
374  }
375  PRINTD("<=Get RF channel %u.\n",x);
376  } else {
377  x = pgm_read_byte_near(&default_channel);
378  if (settings_add_uint8(SETTINGS_KEY_CHANNEL,x ) == SETTINGS_STATUS_OK) {
379  PRINTD("->Set EEPROM RF channel to %d.\n",x);
380  }
381  }
382 #else
383  x=CHANNEL_802_15_4;
384 #endif
385  return x;
386 }
387 static bool
388 get_eui64_from_eeprom(uint8_t macptr[8]) {
389  size_t size = sizeof(rimeaddr_t);
390  if(settings_get(SETTINGS_KEY_EUI64, 0, (unsigned char*)macptr, &size) == SETTINGS_STATUS_OK) {
391  PRINTD("<=Get MAC address.\n");
392  return true;
393  }
394 #if CONTIKI_CONF_RANDOM_MAC
395  PRINTD("--Generating random MAC address.\n");
396  generate_new_eui64(macptr);
397 #else
398  {uint8_t i;for (i=0;i<8;i++) macptr[i] = pgm_read_byte_near(default_mac_address+i);}
399 #endif
400  if (settings_add(SETTINGS_KEY_EUI64,(unsigned char*)macptr,8)) {
401  PRINTD("->Set EEPROM MAC address.\n");
402  }
403  return true;
404 }
405 /**
406  * Getting the PANID from EEPROM cannot work, because the 802154framer always uses the
407  * IEEE802154_PANID define for outgoing frames
408  */
409 static uint16_t
410 get_panid_from_eeprom(void) {
411  return IEEE802154_PANID;
412 }
413 static uint16_t
414 get_panaddr_from_eeprom(void) {
415  uint16_t x;
416  size_t size = 2;
417  if (settings_get(SETTINGS_KEY_PAN_ADDR, 0,(unsigned char*)&x, &size) == SETTINGS_STATUS_OK) {
418  PRINTD("<-Get PAN address of %04x.\n",x);
419  } else {
420  x=pgm_read_word_near(&default_panaddr);
421  if (settings_add_uint16(SETTINGS_KEY_PAN_ADDR,x)==SETTINGS_STATUS_OK) {
422  PRINTD("->Set EEPROM PAN address to %04x.\n",x);
423  }
424  }
425  return x;
426 }
427 static uint8_t
428 get_txpower_from_eeprom(void) {
429  uint8_t x;
430  size_t size = 1;
431  if (settings_get(SETTINGS_KEY_TXPOWER, 0,(unsigned char*)&x, &size) == SETTINGS_STATUS_OK) {
432  PRINTD("<-Get tx power of %d. (0=max)\n",x);
433  } else {
434  x=pgm_read_byte_near(&default_txpower);
435  if (settings_add_uint8(SETTINGS_KEY_TXPOWER,x)==SETTINGS_STATUS_OK) {
436  PRINTD("->Set EEPROM tx power of %d. (0=max)\n",x);
437  }
438  }
439  return x;
440 }
441 #endif /* CONTIKI_CONF_SETTINGS_MANAGER */
442 
443 /*-------------------------Low level initialization------------------------*/
444 /*------Done in a subroutine to keep main routine stack usage small--------*/
445 void initialize(void)
446 {
447  uint8_t reason;
448  extern void *watchdog_return_addr;
449 
450  /* Save the address where the watchdog occurred */
451  void *wdt_addr = watchdog_return_addr;
452 
453  /* Save the reset reason for later */
454  reason = MCUSR;
455  MCUSR = 0;
456 
457  watchdog_init();
458  watchdog_start();
459 
460  /* Second rs232 port for debugging */
461  rs232_init(RS232_PORT_0, USART_BAUD_19200,USART_PARITY_NONE | USART_STOP_BITS_1 | USART_DATA_BITS_8);
462  /* Redirect stdout to second port */
463  rs232_redirect_stdout(RS232_PORT_0);
464  clock_init();
465 
466  /* Print the stack trace if we're here without a "proper" reset */
467  if (!(reason & (_BV(BORF) | _BV(EXTRF) | _BV(PORF))))
468  profiling_stack_trace();
469 
470 #if STACKMONITOR
471  /* Simple stack pointer highwater monitor. Checks for magic numbers in the main
472  * loop. In conjuction with PERIODICPRINTS, never-used stack will be printed
473  * every STACKMONITOR seconds.
474  */
475 {
476 extern uint16_t __bss_end;
477 uint16_t p=(uint16_t)&__bss_end;
478  do {
479  *(uint16_t *)p = 0x4242;
480  p+=10;
481  } while (p<SP-10); //don't overwrite our own stack
482 }
483 #endif
484 
485 /* Get a random (or probably different) seed for the 802.15.4 packet sequence number.
486  * Some layers will ignore duplicates found in a history (e.g. Contikimac)
487  * causing the initial packets to be ignored after a short-cycle restart.
488  */
489  random_init(rng_get_uint8());
490 
491 #define CONF_CALIBRATE_OSCCAL 0
492 #if CONF_CALIBRATE_OSCCAL
493 void calibrate_rc_osc_32k();
494 {
495 extern uint8_t osccal_calibrated;
496 uint8_t i;
497  PRINTD("\nBefore calibration OSCCAL=%x\n",OSCCAL);
498  for (i=0;i<10;i++) {
500  PRINTD("Calibrated=%x\n",osccal_calibrated);
501 //#include <util/delay_basic.h>
502 //#define delay_us( us ) ( _delay_loop_2(1+(us*F_CPU)/4000000UL) )
503 // delay_us(50000);
504  }
505  clock_init();
506 }
507 #endif
508 
509  PRINTA("\n*******Booting %s*******\nReset reason: ",CONTIKI_VERSION_STRING);
510  /* Print out reset reason */
511  if (reason & _BV(JTRF))
512  PRINTA("JTAG ");
513  if (reason & _BV(WDRF))
514  PRINTA("Watchdog ");
515  if (reason & _BV(BORF))
516  PRINTA("Brown-out ");
517  if (reason & _BV(EXTRF))
518  PRINTA("External ");
519  if (reason & _BV(PORF))
520  PRINTA("Power-on ");
521  PRINTA("\n");
522  if (reason & _BV(WDRF))
523  PRINTA("Watchdog possibly occured at address %p\n", wdt_addr);
524 
525  /* Flash initialization */
526  at45db_init();
527 
528 /* rtimers needed for radio cycling */
529  rtimer_init();
530 
531  /* Initialize process subsystem */
532  process_init();
533 
534  /* etimers must be started before ctimer_init */
535  process_start(&etimer_process, NULL);
536 
537  profiling_init();
538  sprofiling_init();
539 
540 #if RF230BB
541 
542  ctimer_init();
543  /* Start radio and radio receive process */
544  NETSTACK_RADIO.init();
545 
546  /* Set addresses BEFORE starting tcpip process */
547 
548  rimeaddr_t addr;
549 // memset(&addr, 0, sizeof(rimeaddr_t));
550  get_eui64_from_eeprom(addr.u8);
551 
552 #if UIP_CONF_IPV6
553  // with IPv6
554 #if WITH_NODE_ID
555  // IPv6 + Node_ID
556  node_id=get_panaddr_from_eeprom();
557  addr.u8[1]=node_id&0xff;
558  addr.u8[0]=(node_id&0xff00)>>8;
559  PRINTA("Node ID from eeprom: %X\n",node_id);
560  //uint16_t inv_node_id=((node_id&0xff00)>>8)+((node_id&0xff)<<8); // chance order of bytes for rf23x
561 
562  memcpy(&uip_lladdr.addr, &addr.u8, sizeof(rimeaddr_t));
563  rimeaddr_set_node_addr(&addr);
564  rf230_set_pan_addr(
565  get_panid_from_eeprom(),
566  node_id,
567  (uint8_t *)&addr.u8
568  );
569 #else /* WITH_NODE_ID */
570  // IPv6 + no Node_ID
571 #error "IPv6 without Node_ID currently not implemented"
572 #endif /* WITH_NODE_ID */
573 
574 #else /* UIP_CONF_IPV6 */
575  // No IPv6 (Rime, DTN, etc.)
576 #if WITH_NODE_ID
577  // No IPv6 + Node_ID
578  node_id=get_panaddr_from_eeprom();
579  addr.u8[1]=node_id&0xff;
580  addr.u8[0]=(node_id&0xff00)>>8;
581  PRINTA("Node ID from eeprom: %X\n",node_id);
582  //uint16_t inv_node_id=((node_id&0xff00)>>8)+((node_id&0xff)<<8); // chance order of bytes for rf23x
583 
584  rimeaddr_set_node_addr(&addr);
585  rf230_set_pan_addr(
586  get_panid_from_eeprom(),
587  node_id,
588  NULL
589  );
590 #else /* WITH_NODE_ID */
591  // No IPv6 + no Node_ID
592  rimeaddr_set_node_addr(&addr);
593  rf230_set_pan_addr(
594  get_panid_from_eeprom(),
595  get_panaddr_from_eeprom(),
596  (uint8_t *)&addr.u8
597  );
598 #endif /* WITH_NODE_ID */
599 #endif /* UIP_CONF_IPV6 */
600  rf230_set_channel(get_channel_from_eeprom());
601  rf230_set_txpower(get_txpower_from_eeprom());
602 
603 
604 #if UIP_CONF_IPV6
605  PRINTA("MAC address %x:%x:%x:%x:%x:%x:%x:%x\n\r",addr.u8[0],addr.u8[1],addr.u8[2],addr.u8[3],addr.u8[4],addr.u8[5],addr.u8[6],addr.u8[7]);
606 #else
607  PRINTA("MAC address ");
608  uint8_t i;
609  for (i=sizeof(rimeaddr_t); i>0; i--){
610  PRINTA("%x:",addr.u8[i-1]);
611  }
612  PRINTA("\n");
613 #endif
614 
615 #if WITH_NODE_ID
616  PRINTF("node ID %u\n",node_id);
617 #endif
618  /* Initialize stack protocols */
619  queuebuf_init();
620  NETSTACK_RDC.init();
621  NETSTACK_MAC.init();
622  NETSTACK_NETWORK.init();
623 
624 #if ANNOUNCE_BOOT
625  PRINTA("%s %s, channel %u power %u",NETSTACK_MAC.name, NETSTACK_RDC.name,rf230_get_channel(),rf230_get_txpower());
626  if (NETSTACK_RDC.channel_check_interval) {//function pointer is zero for sicslowmac
627  unsigned short tmp;
628  tmp=CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval == 0 ? 1:\
629  NETSTACK_RDC.channel_check_interval());
630  if (tmp<65535) PRINTA(", check rate %u Hz",tmp);
631  }
632  PRINTA("\n");
633 
634 #if UIP_CONF_IPV6_RPL
635  PRINTA("RPL Enabled\n");
636 #endif
637 #if UIP_CONF_ROUTER
638  PRINTA("Routing Enabled\n");
639 #endif
640 
641 #endif /* ANNOUNCE_BOOT */
642 
643 // rime_init(rime_udp_init(NULL));
644 // uip_router_register(&rimeroute);
645 
646  process_start(&tcpip_process, NULL);
647 
648 #else /* !RF230BB */
649 /* Original RF230 combined mac/radio driver */
650 /* mac process must be started before tcpip process! */
651  process_start(&mac_process, NULL);
652  process_start(&tcpip_process, NULL);
653 #endif /* RF230BB */
654 
655 #ifdef RAVEN_LCD_INTERFACE
656  process_start(&raven_lcd_process, NULL);
657 #endif
658 
659  /* Autostart other processes */
660  autostart_start(autostart_processes);
661 
662  //Give ourselves a prefix
663  // init_net();
664 
665  /*---If using coffee file system create initial web content if necessary---*/
666 #if COFFEE_FILES
667  int fa = cfs_open( "/index.html", CFS_READ);
668  if (fa<0) { //Make some default web content
669  PRINTA("No index.html file found, creating upload.html!\n");
670  PRINTA("Formatting FLASH file system for coffee...");
672  PRINTA("Done!\n");
673  fa = cfs_open( "/index.html", CFS_WRITE);
674  int r = cfs_write(fa, &"It works!", 9);
675  if (r<0) PRINTA("Can''t create /index.html!\n");
676  cfs_close(fa);
677 // fa = cfs_open("upload.html"), CFW_WRITE);
678 // <html><body><form action="upload.html" enctype="multipart/form-data" method="post"><input name="userfile" type="file" size="50" /><input value="Upload" type="submit" /></form></body></html>
679  }else{
680  PRINTF("index.html found\n");
681  }
682 #endif /* COFFEE_FILES */
683 
684 /* Add addresses for testing */
685 #if 0
686 {
687  uip_ip6addr_t ipaddr;
688  uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
689  uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF);
690 // uip_ds6_prefix_add(&ipaddr,64,0);
691 }
692 #endif
693 
694 /*--------------------------Announce the configuration---------------------*/
695 #if ANNOUNCE_BOOT
696 {
697 #if AVR_WEBSERVER
698  uint8_t i;
699  char buf[80];
700  unsigned int size;
701 
702  for (i=0;i<UIP_DS6_ADDR_NB;i++) {
703  if (uip_ds6_if.addr_list[i].isused) {
704  httpd_cgi_sprint_ip6(uip_ds6_if.addr_list[i].ipaddr,buf);
705  PRINTA("IPv6 Address: %s\n",buf);
706  }
707  }
708  cli();
709  eeprom_read_block (buf,eemem_server_name, sizeof(eemem_server_name));
710  sei();
711  buf[sizeof(eemem_server_name)]=0;
712  PRINTA("%s",buf);
713  cli();
714  eeprom_read_block (buf,eemem_domain_name, sizeof(eemem_domain_name));
715  sei();
716  buf[sizeof(eemem_domain_name)]=0;
717  size=httpd_fs_get_size();
718 #ifndef COFFEE_FILES
719  PRINTA(".%s online with fixed %u byte web content\n",buf,size);
720 #elif COFFEE_FILES==1
721  PRINTA(".%s online with static %u byte EEPROM file system\n",buf,size);
722 #elif COFFEE_FILES==2
723  PRINTA(".%s online with dynamic %u KB EEPROM file system\n",buf,size>>10);
724 #elif COFFEE_FILES==3
725  PRINTA(".%s online with static %u byte program memory file system\n",buf,size);
726 #elif COFFEE_FILES==4
727  PRINTA(".%s online with dynamic %u KB program memory file system\n",buf,size>>10);
728 #endif /* COFFEE_FILES */
729 
730 #else
731  PRINTA("Online\n");
732 #endif /* AVR_WEBSERVER */
733 
734 #endif /* ANNOUNCE_BOOT */
735 }
736 }
737 
738 #if ROUTES && UIP_CONF_IPV6
739 static void
740 ipaddr_add(const uip_ipaddr_t *addr)
741 {
742  uint16_t a;
743  int8_t i, f;
744  for(i = 0, f = 0; i < sizeof(uip_ipaddr_t); i += 2) {
745  a = (addr->u8[i] << 8) + addr->u8[i + 1];
746  if(a == 0 && f >= 0) {
747  if(f++ == 0) PRINTF("::");
748  } else {
749  if(f > 0) {
750  f = -1;
751  } else if(i > 0) {
752  PRINTF(":");
753  }
754  PRINTF("%x",a);
755  }
756  }
757 }
758 #endif
759 
760 /*-------------------------------------------------------------------------*/
761 /*------------------------- Main Scheduler loop----------------------------*/
762 /*-------------------------------------------------------------------------*/
763 SENSORS(&button_sensor);
764 int
765 main(void)
766 {
767  initialize();
768  process_start(&sensors_process, NULL);
769 
770  profiling_start();
771 
772  while(1) {
773  process_run();
774  watchdog_periodic();
775 
776 #if 0
777 /* Various entry points for debugging in the AVR Studio simulator.
778  * Set as next statement and step into the routine.
779  */
780  NETSTACK_RADIO.send(packetbuf_hdrptr(), 42);
781  process_poll(&rf230_process);
782  packetbuf_clear();
783  len = rf230_read(packetbuf_dataptr(), PACKETBUF_SIZE);
785  NETSTACK_RDC.input();
786 #endif
787 
788 #if 0
789 /* Clock.c can trigger a periodic PLL calibration in the RF230BB driver.
790  * This can show when that happens.
791  */
792  extern uint8_t rf230_calibrated;
793  if (rf230_calibrated) {
794  PRINTD("\nRF230 calibrated!\n");
795  rf230_calibrated=0;
796  }
797 #endif
798 
799 #if DEBUGFLOWSIZE
800  if (debugflowsize) {
801  debugflow[debugflowsize]=0;
802  PRINTF("%s",debugflow);
803  debugflowsize=0;
804  }
805 #endif
806 
807  watchdog_periodic();
808 
809 #if PERIODICPRINTS
810 #if TESTRTIMER
811 /* Timeout can be increased up to 8 seconds maximum.
812  * A one second cycle is convenient for triggering the various debug printouts.
813  * The triggers are staggered to avoid printing everything at once.
814  */
815  if (rtimerflag) {
816  rtimer_set(&rt, RTIMER_NOW()+ RTIMER_ARCH_SECOND*1UL, 1,(void *) rtimercycle, NULL);
817  rtimerflag=0;
818 #else
819  if (clocktime!=clock_seconds()) {
820  clocktime=clock_seconds();
821 #endif
822 
823 #if STAMPS
824 if ((clocktime%STAMPS)==0) {
825 #if ENERGEST_CONF_ON
826 #include "lib/print-stats.h"
827  print_stats();
828 #elif RADIOSTATS
829 extern volatile unsigned long radioontime;
830  PRINTF("%u(%u)s\n",clocktime,radioontime);
831 #else
832  PRINTF("%us\n",clocktime);
833 #endif
834 
835 }
836 #endif
837 #if TESTRTIMER
838  clocktime+=1;
839 #endif
840 
841 #if PINGS && UIP_CONF_IPV6
842 extern void raven_ping6(void);
843 if ((clocktime%PINGS)==1) {
844  PRINTF("**Ping\n");
845  raven_ping6();
846 }
847 #endif
848 
849 #if ROUTES && UIP_CONF_IPV6
850 if ((clocktime%ROUTES)==2) {
851 
854 extern uip_ds6_netif_t uip_ds6_if;
855 
856  uint8_t i,j;
857  PRINTF("\nAddresses [%u max]\n",UIP_DS6_ADDR_NB);
858  for (i=0;i<UIP_DS6_ADDR_NB;i++) {
859  if (uip_ds6_if.addr_list[i].isused) {
860  ipaddr_add(&uip_ds6_if.addr_list[i].ipaddr);
861  PRINTF("\n");
862  }
863  }
864  PRINTF("\nNeighbors [%u max]\n",UIP_DS6_NBR_NB);
865  for(i = 0,j=1; i < UIP_DS6_NBR_NB; i++) {
866  if(uip_ds6_nbr_cache[i].isused) {
867  ipaddr_add(&uip_ds6_nbr_cache[i].ipaddr);
868  PRINTF("\n");
869  j=0;
870  }
871  }
872  if (j) PRINTF(" <none>");
873  PRINTF("\nRoutes [%u max]\n",UIP_DS6_ROUTE_NB);
874  for(i = 0,j=1; i < UIP_DS6_ROUTE_NB; i++) {
875  if(uip_ds6_routing_table[i].isused) {
876  ipaddr_add(&uip_ds6_routing_table[i].ipaddr);
877  PRINTF("/%u (via ", uip_ds6_routing_table[i].length);
878  ipaddr_add(&uip_ds6_routing_table[i].nexthop);
879  // if(uip_ds6_routing_table[i].state.lifetime < 600) {
880  PRINTF(") %lus\n", uip_ds6_routing_table[i].state.lifetime);
881  // } else {
882  // PRINTF(")\n");
883  // }
884  j=0;
885  }
886  }
887  if (j) PRINTF(" <none>");
888  PRINTF("\n---------\n");
889 }
890 #endif
891 
892 #if STACKMONITOR
893 if ((clocktime%STACKMONITOR)==3) {
894  extern uint16_t __bss_end;
895  uint16_t p=(uint16_t)&__bss_end;
896  do {
897  if (*(uint16_t *)p != 0x4242) {
898  PRINTF("Never-used stack > %d bytes\n",p-(uint16_t)&__bss_end);
899  break;
900  }
901  p+=10;
902  } while (p<RAMEND-10);
903 }
904 #endif
905 
906  }
907 #endif /* PERIODICPRINTS */
908 
909 #if RF230BB&&0
910 extern uint8_t rf230processflag;
911  if (rf230processflag) {
912  PRINTF("rf230p%d",rf230processflag);
913  rf230processflag=0;
914  }
915 #endif
916 
917 #if RF230BB&&0
918 extern uint8_t rf230_interrupt_flag;
919  if (rf230_interrupt_flag) {
920  // if (rf230_interrupt_flag!=11) {
921  PRINTF("**RI%u",rf230_interrupt_flag);
922  // }
923  rf230_interrupt_flag=0;
924  }
925 #endif
926  }
927  return 0;
928 }
929 
930 /*---------------------------------------------------------------------------*/
931 
932 void log_message(char *m1, char *m2)
933 {
934  PRINTF("%s%s\n", m1, m2);
935 }