56 #define DEBUG 0 //Making this 1 will slightly alter command timings
58 #define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
62 #define DEBUGSERIAL 0 //Making this 1 will significantly alter command timings
65 #include "contiki-lib.h"
66 #include "contiki-net.h"
67 #include "webserver-nogui.h"
68 #include "httpd-cgi.h"
69 #include "raven-lcd.h"
73 #include <avr/pgmspace.h>
74 #include <avr/eeprom.h>
75 #include <avr/sleep.h>
76 #include <dev/watchdog.h>
78 static uint8_t count = 0;
80 uip_ipaddr_t dest_addr;
82 #define MAX_CMD_LEN 20
84 uint8_t frame[MAX_CMD_LEN];
91 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
92 #define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len])
93 #define PING6_DATALEN 16
95 void rs232_send(uint8_t port,
unsigned char c);
103 #if UIP_CONF_IPV6_RPL||1
105 #define PING_GOOGLE 1
117 }
else if (seqno==2) {
119 }
else if (seqno==3) {
136 count, PING6_DATALEN);
149 #if defined(__AVR_ATmega128RFA1__)
153 send_frame(uint8_t cmd, uint8_t len, uint8_t *payload)
160 send_frame(uint8_t cmd, uint8_t len, uint8_t *payload)
172 char serial_char_received;
185 void micro_sleep(uint8_t howlong)
187 uint8_t saved_sreg = SREG, saved_howlong = howlong;
188 #if AVR_CONF_USE32KCRYSTAL
190 uint8_t savedTCNT2=TCNT2, savedTCCR2A=TCCR2A, savedTCCR2B = TCCR2B, savedOCR2A = OCR2A;
196 set_sleep_mode(SLEEP_MODE_PWR_SAVE);
197 if (howlong==0) howlong=3;
201 TCCR2B =((1<<CS22)|(1<<CS21)|(1<<CS20));
216 while(ASSR & (1 << TCN2UB));
217 TIMSK2 |= (1 << OCIE2A);
222 serial_char_received=0;
231 if (saved_howlong)
break;
232 PRINTF(
"%d",serial_char_received);
233 if (serial_char_received)
break;
238 #if AVR_CONF_USE32KCRYSTAL
242 TCCR2A = savedTCCR2A;
243 TCCR2B = savedTCCR2B;
248 TIMSK2 &= ~(1 << OCIE2A);
253 #if !AVR_CONF_USE32KCRYSTAL
257 ISR(TIMER2_COMPA_vect)
269 extern uint16_t ledtimer;
271 raven_gui_loop(process_event_t ev, process_data_t data)
273 uint8_t i,activeconnections,radio_state;
277 printf_P(PSTR(
"Buffer [%d]="),serialcount);
280 printf_P(PSTR(
" %d"),dbuf[i]);
284 if(ev == tcpip_icmp6_event)
switch(*((uint8_t *)data)) {
297 #if RF230BB_CONF_LEDONPORTE1
298 PORTE|=(1<<PE1);ledtimer=1000;
310 PRINTF(
"\nCommand %d length %d done %d",cmd.cmd,cmd.len,cmd.done);
316 seqno = cmd.frame[0];
329 if (cmd.frame[0]==0) {
337 if((uip_conns[i].tcpstateflags & UIP_TS_MASK) != UIP_CLOSED) activeconnections++;
339 if (activeconnections) {
340 PRINTF(
"\nWaiting for %d connections",activeconnections);
344 radio_state = NETSTACK_RADIO.off();
345 PRINTF (
"\nsleep %d radio state %d...",cmd.frame[0],radio_state);
348 PRINTF(
"\nSleeping...");
349 micro_sleep(cmd.frame[0]);
351 radio_state = NETSTACK_RADIO.on();
352 if (radio_state > 0) {
355 PRINTF(
"Radio wake error %d\n",radio_state);
360 send_frame(REPORT_WAKE, 0, 0);
380 int raven_lcd_serial_input(
unsigned char ch)
384 if (ch) serial_char_received++;
386 if (serialcount<25) dbuf[serialcount]=ch;
415 if (cmd.ndx >= (MAX_CMD_LEN+3)) {
422 if (cmd.ndx >= cmd.len+3){
440 cmd.frame[cmd.ndx - 3] = ch;
451 raven_lcd_show_text(
char *text) {
452 uint8_t textlen=strlen(text)+1;
453 if (textlen > MAX_CMD_LEN) textlen=MAX_CMD_LEN;
454 send_frame(REPORT_TEXT_MSG, textlen, (uint8_t *) text);
459 lcd_show_servername(
void) {
462 extern uint8_t server_name[16];
464 char buf[
sizeof(server_name)+1];
465 eeprom_read_block (buf,server_name,
sizeof(server_name));
466 buf[
sizeof(server_name)]=0;
467 raven_lcd_show_text(buf);
471 PROCESS(raven_lcd_process,
"Raven LCD interface process");
478 lcd_show_servername();
482 if(icmp6_new(
NULL) == 0) {
487 raven_gui_loop(ev, data);