54 #define DEBUG 0 //Making this 1 will slightly alter command timings
56 #define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
60 #define DEBUGSERIAL 0 //Making this 1 will significantly alter command timings
63 #include "contiki-lib.h"
64 #include "contiki-net.h"
67 #include "webserver-nogui.h"
68 #include "httpd-cgi.h"
71 #include "raven-lcd.h"
75 #include <avr/pgmspace.h>
76 #include <avr/eeprom.h>
77 #include <avr/sleep.h>
78 #include <dev/watchdog.h>
80 static uint8_t count = 0;
82 uip_ipaddr_t dest_addr;
84 #define MAX_CMD_LEN 20
86 uint8_t frame[MAX_CMD_LEN];
93 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
94 #define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len])
95 #define PING6_DATALEN 16
97 void rs232_send(uint8_t port,
unsigned char c);
105 #define PING_GOOGLE 0
115 }
else if (seqno==2) {
117 }
else if (seqno==3) {
134 count, PING6_DATALEN);
151 send_frame(uint8_t cmd, uint8_t len, uint8_t *payload)
162 char serial_char_received;
175 void micro_sleep(uint8_t howlong)
177 uint8_t saved_sreg = SREG, saved_howlong = howlong;
178 #if AVR_CONF_USE32KCRYSTAL
180 uint8_t savedTCNT2=TCNT2, savedTCCR2A=TCCR2A, savedTCCR2B = TCCR2B, savedOCR2A = OCR2A;
186 set_sleep_mode(SLEEP_MODE_PWR_SAVE);
187 if (howlong==0) howlong=3;
191 TCCR2B =((1<<CS22)|(1<<CS21)|(1<<CS20));
206 while(ASSR & (1 << TCN2UB));
207 TIMSK2 |= (1 << OCIE2A);
212 serial_char_received=0;
220 if (saved_howlong)
break;
222 if (serial_char_received)
break;
227 #if AVR_CONF_USE32KCRYSTAL
231 TCCR2A = savedTCCR2A;
232 TCCR2B = savedTCCR2B;
237 TIMSK2 &= ~(1 << OCIE2A);
242 #if !AVR_CONF_USE32KCRYSTAL
246 ISR(TIMER2_COMPA_vect)
259 raven_gui_loop(process_event_t ev, process_data_t data)
261 uint8_t i,activeconnections,radio_state;
265 printf_P(PSTR(
"Buffer [%d]="),serialcount);
268 printf_P(PSTR(
" %d"),dbuf[i]);
272 if(ev == tcpip_icmp6_event)
switch(*((uint8_t *)data)) {
284 send_frame(REPORT_PING_BEEP, 0, 0);
288 send_frame(REPORT_PING, 1, &seqno);
295 PRINTF(
"\nCommand %d length %d done %d",cmd.cmd,cmd.len,cmd.done);
301 seqno = cmd.frame[0];
307 web_set_temp((
char *)cmd.frame);
313 web_set_voltage((
char *)cmd.frame);
318 if (cmd.frame[0]==0) {
327 if((uip_conns[i].tcpstateflags & UIP_TS_MASK) != UIP_CLOSED) activeconnections++;
329 if (activeconnections) {
330 PRINTF(
"\nWaiting for %d connections",activeconnections);
335 radio_state = NETSTACK_RADIO.off();
336 PRINTF (
"\nsleep %d radio state %d...",cmd.frame[0],radio_state);
339 PRINTF(
"\nSleeping...");
340 micro_sleep(cmd.frame[0]);
342 radio_state = NETSTACK_RADIO.on();
343 if (radio_state > 0) {
346 PRINTF(
"Radio wake error %d\n",radio_state);
351 send_frame(REPORT_WAKE, 0, 0);
371 int raven_lcd_serial_input(
unsigned char ch)
375 if (ch) serial_char_received++;
377 if (serialcount<25) dbuf[serialcount]=ch;
406 if (cmd.ndx >= (MAX_CMD_LEN+3)) {
413 if (cmd.ndx >= cmd.len+3){
431 cmd.frame[cmd.ndx - 3] = ch;
442 raven_lcd_show_text(
char *text) {
443 uint8_t textlen=strlen(text)+1;
444 if (textlen > MAX_CMD_LEN) textlen=MAX_CMD_LEN;
445 send_frame(REPORT_TEXT_MSG, textlen, (uint8_t *) text);
450 lcd_show_servername(
void) {
453 extern uint8_t eemem_server_name[16];
455 char buf[
sizeof(eemem_server_name)+1];
456 eeprom_read_block (buf,eemem_server_name,
sizeof(eemem_server_name));
457 buf[
sizeof(eemem_server_name)]=0;
458 raven_lcd_show_text(buf);
462 PROCESS(raven_lcd_process,
"Raven LCD interface process");
469 lcd_show_servername();
473 if(icmp6_new(
NULL) == 0) {
478 raven_gui_loop(ev, data);
#define UIP_CONNS
The maximum number of simultaneously open TCP connections.
uip_len
The length of the packet in the uip_buf buffer.
void tcpip_ipv6_output(void)
This function does address resolution and then calls tcpip_output.
#define PROCESS_BEGIN()
Define the beginning of a process.
void watchdog_start(void)
Starts the WDT in watchdog mode if enabled by user configuration, maximum interval.
#define ICMP6_ECHO_REPLY
Echo reply.
#define NULL
The null pointer.
#define SOF_CHAR
Start-of-frame character.
int process_post(struct process *p, process_event_t ev, process_data_t data)
Post an asynchronous event.
void clock_adjust_ticks(clock_time_t howmany)
Adjust the system current clock time.
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
#define UIP_IP_BUF
Pointer to IP header.
#define PROCESS_END()
Define the end of a process.
void watchdog_stop(void)
In watchdog mode, the WDT can not be stopped.
uint16_t uip_icmp6chksum(void)
Calculate the ICMP checksum of the packet in uip_buf.
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
#define ICMP6_ECHO_REQUEST
Echo request.
#define PROCESS(name, strname)
Declare a process.
void uip_ds6_select_src(uip_ipaddr_t *src, uip_ipaddr_t *dst)
Source address selection, see RFC 3484.
#define UIP_ICMP_BUF
Pointer to ICMP header.
#define PROCESS_YIELD()
Yield the currently running process.
#define EOF_CHAR
End-of-frame character.
#define uip_ip6addr(addr, addr0, addr1, addr2, addr3, addr4, addr5, addr6, addr7)
Construct an IPv6 address from eight 16-bit words.
void rs232_send(uint8_t port, unsigned char c)
Print a character on RS232.
#define UIP_ICMP6_ECHO_REQUEST_LEN
Echo Request constant part length.
#define CLOCK_SECOND
A second, measured in system clock time.