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