diff --git a/platform/avr-raven/apps/raven-webserver/httpd-cgi.c b/platform/avr-raven/apps/raven-webserver/httpd-cgi.c index e6d21d81a..2c00c20c9 100644 --- a/platform/avr-raven/apps/raven-webserver/httpd-cgi.c +++ b/platform/avr-raven/apps/raven-webserver/httpd-cgi.c @@ -28,7 +28,7 @@ * * This file is part of the uIP TCP/IP stack. * - * $Id: httpd-cgi.c,v 1.2 2009/06/19 17:11:28 dak664 Exp $ + * $Id: httpd-cgi.c,v 1.3 2009/07/23 16:16:07 dak664 Exp $ * */ @@ -50,11 +50,18 @@ #include "httpd-cgi.h" #include "httpd-fs.h" #include "httpd-fsdata.h" - //#include "lib/petsciiconv.h" +#define petsciiconv_toascii(...) #include "sensors.h" +#define DEBUGLOGIC 0 //see httpd.c +#if DEBUGLOGIC +#define uip_mss(...) 512 +#define uip_appdata TCPBUF +extern char TCPBUF[512]; +#endif + static struct httpd_cgi_call *calls = NULL; /*cgi function names*/ @@ -133,25 +140,30 @@ generate_file_stats(void *arg) { char tmp[20]; struct httpd_fsdata_file_noconst *f,fram; - u16_t i,numprinted; + u16_t i; + unsigned short numprinted; - /* Transfer arg from flash file to RAM */ - memcpy_P_trim(tmp, (char *)arg); + /* Transfer arg from whichever flash that contains the html file to RAM */ + httpd_fs_cpy(&tmp, (char *)arg, 20); /* Count for this page, with common page footer */ if (tmp[0]=='.') { - return snprintf_P((char *)uip_appdata, uip_mss(), + numprinted=snprintf_P((char *)uip_appdata, uip_mss(), PSTR("



This page has been sent %u times"), httpd_fs_open(thisfilename, 0)); /* Count for all files */ /* Note buffer will overflow if there are too many files! */ } else if (tmp[0]=='*') { i=0;numprinted=0; - for(f = (struct httpd_fsdata_file_noconst *)httpd_get_root(); + for(f = (struct httpd_fsdata_file_noconst *)httpd_fs_get_root(); f != NULL; f = (struct httpd_fsdata_file_noconst *)fram.next) { + + /* Get the linked list file entry into RAM from program flash memory*/ memcpy_P(&fram,f,sizeof(fram)); - memcpy_P(&tmp,fram.name,sizeof(tmp)); + + /* Get the file name from whatever flash memory it is in */ + httpd_fs_cpy(&tmp, fram.name, sizeof(tmp)); numprinted+=snprintf_P((char *)uip_appdata + numprinted, uip_mss() - numprinted, #if HTTPD_FS_STATISTICS==1 PSTR("%s%d"),tmp,tmp,f->count); @@ -159,12 +171,15 @@ generate_file_stats(void *arg) PSTR("%s%d"),tmp,tmp,httpd_filecount[i++]); #endif } - return numprinted; /* Count for specified file */ } else { - return snprintf_P((char *)uip_appdata, uip_mss(), PSTR("%5u"), httpd_fs_open(tmp, 0)); + numprinted=snprintf_P((char *)uip_appdata, uip_mss(), PSTR("%5u"), httpd_fs_open(tmp, 0)); } +#if DEBUGLOGIC + return 0; +#endif + return numprinted; } /*---------------------------------------------------------------------------*/ static @@ -174,7 +189,8 @@ PT_THREAD(file_stats(struct httpd_state *s, char *ptr)) PSOCK_BEGIN(&s->sout); thisfilename=&s->filename[0]; //temporary way to pass filename to generate_file_stats - PSOCK_GENERATOR_SEND(&s->sout, generate_file_stats, (void *) (strchr_P(ptr, ' ') + 1)); + + PSOCK_GENERATOR_SEND(&s->sout, generate_file_stats, (void *) ptr); PSOCK_END(&s->sout); } @@ -229,7 +245,7 @@ make_processes(void *p) char name[40],tstate[20]; strncpy(name, ((struct process *)p)->name, 40); -//petsciiconv_toascii(name, 40); + petsciiconv_toascii(name, 40); strcpy_P(tstate,states[9 + ((struct process *)p)->state]); return snprintf_P((char *)uip_appdata, uip_mss(), PSTR("%p%s%p%s\r\n"), diff --git a/platform/avr-raven/apps/raven-webserver/httpd-fs.c b/platform/avr-raven/apps/raven-webserver/httpd-fs.c index d0c856508..eec8edd1a 100644 --- a/platform/avr-raven/apps/raven-webserver/httpd-fs.c +++ b/platform/avr-raven/apps/raven-webserver/httpd-fs.c @@ -30,14 +30,13 @@ * * Author: Adam Dunkels * - * $Id: httpd-fs.c,v 1.2 2009/06/19 17:11:28 dak664 Exp $ + * $Id: httpd-fs.c,v 1.3 2009/07/23 16:16:07 dak664 Exp $ */ #include "contiki-net.h" #include "httpd.h" #include "httpd-fs.h" #include "httpd-fsdata.h" - #include "httpd-fsdata.c" #if HTTPD_FS_STATISTICS==2 @@ -46,11 +45,18 @@ u16_t httpd_filecount[HTTPD_FS_NUMFILES]; /*-----------------------------------------------------------------------------------*/ void * -httpd_get_root() +httpd_fs_get_root() { return (void *)HTTPD_FS_ROOT; } /*-----------------------------------------------------------------------------------*/ +u16_t +httpd_fs_get_size() +{ + return HTTPD_FS_SIZE; +} +/*-----------------------------------------------------------------------------------*/ + u16_t httpd_fs_open(const char *name, struct httpd_fs_file *file) { @@ -62,24 +68,28 @@ httpd_fs_open(const char *name, struct httpd_fs_file *file) for(f = (struct httpd_fsdata_file_noconst *)HTTPD_FS_ROOT; f != NULL; f = (struct httpd_fsdata_file_noconst *)fram.next) { + + /*Get the linked list entry into ram from program flash */ memcpy_P(&fram,f,sizeof(fram)); - if(strcmp_P(name, fram.name) == 0) { + + /*Compare name passed in RAM with name in whatever flash the file is in */ + if(httpd_fs_strcmp((char *)name, fram.name) == 0) { if (file) { file->data = fram.data; file->len = fram.len; -#if HTTPD_FS_STATISTICS==1 +#if HTTPD_FS_STATISTICS==1 //increment count in linked list field if it is in RAM f->count++; } return f->count; } ++i -#elif HTTPD_FS_STATISTICS==2 +#elif HTTPD_FS_STATISTICS==2 //increment count in RAM array when linked list is in flash ++httpd_filecount[i]; } return httpd_filecount[i]; } ++i; -#else +#else //no file statistics } return 1; } diff --git a/platform/avr-raven/apps/raven-webserver/httpd-fs.h b/platform/avr-raven/apps/raven-webserver/httpd-fs.h index f6d91525e..a40971b99 100644 --- a/platform/avr-raven/apps/raven-webserver/httpd-fs.h +++ b/platform/avr-raven/apps/raven-webserver/httpd-fs.h @@ -30,7 +30,7 @@ * * Author: Adam Dunkels * - * $Id: httpd-fs.h,v 1.2 2009/06/19 17:11:28 dak664 Exp $ + * $Id: httpd-fs.h,v 1.3 2009/07/23 16:16:07 dak664 Exp $ */ #ifndef __HTTPD_FS_H__ #define __HTTPD_FS_H__ @@ -45,23 +45,43 @@ extern u16_t httpd_filecount[]; #endif /* HTTPD_FS_STATISTICS */ #include +#if COFFEE_FILES +#include "cfs-coffee-arch.h" +#include +#endif struct httpd_fs_file { char *data; int len; }; - -/* file must be allocated by caller and will be filled in - by the function. If NULL, just file stats are returned.*/ +/* Initialize the file system and set statistics to zero */ +void httpd_fs_init(void); +/* Returns root of http files in program flash */ +void* httpd_fs_get_root(); +/* Returns size of http files in any flash */ +u16_t httpd_fs_get_size(); +/* Open a file in any flash and return statistics if enabled. + If file is allocated by caller it will be filled in. + If NULL, just file stats are returned. + */ u16_t httpd_fs_open(const char *name, struct httpd_fs_file *file); -/* Returns root of http pages in flash */ -void * httpd_get_root(); +#if COFFEE_FILES +/* Coffee file system can be static or dynamic. If static, new files + can not be created and rewrites of an existing file can not be + made beyond the initial allocation. + */ +#define httpd_fs_cpy avr_httpd_fs_cpy +#define httpd_fs_getchar avr_httpd_fs_getchar +#define httpd_fs_strcmp avr_httpd_fs_strcmp +#define httpd_fs_strchr avr_httpd_fs_strchr -void httpd_fs_init(void); - -#define httpd_fs_cpy memcpy_P -#define httpd_fs_strchr strchr_P -#define httpd_fs_getchar(x) pgm_read_byte(x) +#else +/* These will fail if the web content is above 64K in program flash */ +#define httpd_fs_cpy memcpy_P +#define httpd_fs_strcmp strcmp_P +#define httpd_fs_strchr strchr_P +#define httpd_fs_getchar(x) pgm_read_byte(x) +#endif #endif /* __HTTPD_FS_H__ */ diff --git a/platform/avr-raven/apps/raven-webserver/httpd.c b/platform/avr-raven/apps/raven-webserver/httpd.c index e65938fb2..1fea963e8 100644 --- a/platform/avr-raven/apps/raven-webserver/httpd.c +++ b/platform/avr-raven/apps/raven-webserver/httpd.c @@ -30,7 +30,7 @@ * * Author: Adam Dunkels * - * $Id: httpd.c,v 1.2 2009/06/19 17:11:28 dak664 Exp $ + * $Id: httpd.c,v 1.3 2009/07/23 16:16:07 dak664 Exp $ */ #include @@ -40,14 +40,43 @@ #include "webserver.h" #include "httpd-fs.h" #include "httpd-cgi.h" -//#include "lib/petsciiconv.h" -//#include "http-strings.h" - #include "httpd.h" +//#include "lib/petsciiconv.h" +#define petsciiconv_topetscii(...) +//#include "http-strings.h" +#if COFFEE_FILES +#include "cfs-coffee-arch.h" +#endif /* COFFEE_FILES */ + +/* DEBUGLOGIC is a convenient way to debug in a simulator without a tcp/ip connection. + * Break the program in the process loop and step from the entry in httpd_appcall. + * The input file is forced to /index.html and the output directed to TCPBUF. + * If cgi's are invoked define it in httpd-cgi.c as well. + * psock_generator_send in /core/net/psock.c must also be modified as follows: + * ... + * // Wait until all data is sent and acknowledged. + * if (!s->sendlen) break; //<---add this line + * PT_YIELD_UNTIL(&s->psockpt, uip_acked() || uip_rexmit()); + * ... + */ +#define DEBUGLOGIC 0 +#define DEBUG 0 +#if DEBUGLOGIC +struct httpd_state *sg; +#define uip_mss(...) 512 +#define uip_appdata TCPBUF +char TCPBUF[512]; +#endif +#if DEBUG +#include +#define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args) +#else +#define PRINTF(...) +#endif #ifndef WEBSERVER_CONF_CGI_CONNS #define CONNS 4 -#else /* WEBSERVER_CONF_CGI_CONNS */ +#else #define CONNS WEBSERVER_CONF_CGI_CONNS #endif /* WEBSERVER_CONF_CGI_CONNS */ @@ -57,6 +86,8 @@ //#define SEND_STRING(s, str) PSOCK_SEND(s, (uint8_t *)str, (unsigned int)strlen(str)) MEMB(conns, struct httpd_state, CONNS); +/* MAX_SCRIPT_NAME_LENGTH should be at least be the maximum file name length+2 for %!: includes */ +#define MAX_SCRIPT_NAME_LENGTH 20 #define ISO_tab 0x09 #define ISO_nl 0x0a #define ISO_cr 0x0d @@ -78,10 +109,12 @@ generate(void *state) } else { s->len = s->file.len; } - httpd_fs_cpy(uip_appdata, s->file.data, s->len); - +#if DEBUGLOGIC + return 0; +#else return s->len; +#endif } /*---------------------------------------------------------------------------*/ static @@ -89,12 +122,12 @@ PT_THREAD(send_file(struct httpd_state *s)) { PSOCK_BEGIN(&s->sout); - do { + do { PSOCK_GENERATOR_SEND(&s->sout, generate, s); - s->file.len -= s->len; + s->file.len -= s->len; s->file.data += s->len; } while(s->file.len > 0); - + PSOCK_END(&s->sout); } /*---------------------------------------------------------------------------*/ @@ -102,15 +135,15 @@ static PT_THREAD(send_part_of_file(struct httpd_state *s)) { PSOCK_BEGIN(&s->sout); - + static int oldfilelen, oldlen; static char * olddata; - + //Store stuff that gets clobbered... oldfilelen = s->file.len; oldlen = s->len; olddata = s->file.data; - + //How much to send s->file.len = s->len; @@ -119,7 +152,7 @@ PT_THREAD(send_part_of_file(struct httpd_state *s)) s->file.len -= s->len; s->file.data += s->len; } while(s->file.len > 0); - + s->len = oldlen; s->file.len = oldfilelen; s->file.data = olddata; @@ -131,11 +164,11 @@ static void next_scriptstate(struct httpd_state *s) { char *p; - +/* Skip over any script parameters to the beginning of the next line */ if((p = (char *)httpd_fs_strchr(s->scriptptr, ISO_nl)) != NULL) { p += 1; s->scriptlen -= (unsigned short)(p - s->scriptptr); - s->scriptptr = p; + s->scriptptr = p; } else { s->scriptlen = 0; } @@ -147,58 +180,67 @@ next_scriptstate(struct httpd_state *s) } /*---------------------------------------------------------------------------*/ -void -memcpy_P_trim(char *toram, char *fromflash) +char * +get_scriptname(char *dest, char *fromfile) { - uint8_t i; - for (i=0;i<19;) { - toram[i]=pgm_read_byte_near(fromflash++); - if (toram[i]==ISO_tab) {if (i) break; else continue;} //skip leading tabs - if (toram[i]==ISO_space) {if (i) break; else continue;} //skip leading spaces - if (toram[i]==ISO_nl) break; //nl is preferred delimiter - if (toram[i]==ISO_cr) break; //some editors insert cr - if (toram[i]==0) break; //files are terminated with null + uint8_t i=0,skip=1; + /* Extract a file or cgi name, trim leading spaces and replace termination with zero */ + /* Returns number of characters processed up to the next non-tab or space */ + do { + dest[i]=httpd_fs_getchar(fromfile++); + if (dest[i]==ISO_colon) {if (!skip) break;} //allow leading colons + else if (dest[i]==ISO_tab ) {if (skip) continue;else break;}//skip leading tabs + else if (dest[i]==ISO_space) {if (skip) continue;else break;}//skip leading spaces + else if (dest[i]==ISO_nl ) break; //nl is preferred delimiter + else if (dest[i]==ISO_cr ) break; //some editors insert cr + else if (dest[i]==0 ) break; //files are terminated with null + else skip=0; i++; - } - toram[i]=0; - - } + } while (i<(MAX_SCRIPT_NAME_LENGTH+1)); + fromfile--; + while ((dest[i]==ISO_space) || (dest[i]==ISO_tab)) dest[i]=httpd_fs_getchar(++fromfile); + dest[i]=0; + return (fromfile); +} /*---------------------------------------------------------------------------*/ -static char filenamebuf[25],*pptr;//See below! + static PT_THREAD(handle_script(struct httpd_state *s)) { -// char *ptr; //one of these gets whomped unless in globals -// char filenamebuf[25]; - + /* Note script includes will attach a leading : to the filename and a trailing zero */ + static char scriptname[MAX_SCRIPT_NAME_LENGTH+1],*pptr; + static uint16_t filelength; + PT_BEGIN(&s->scriptpt); + filelength=s->file.len; while(s->file.len > 0) { + /* Sanity check */ + if (s->file.len > filelength) break; - /* Check if we should start executing a script. */ + /* Check if we should start executing a script, flagged by %! */ if(httpd_fs_getchar(s->file.data) == ISO_percent && httpd_fs_getchar(s->file.data + 1) == ISO_bang) { - s->scriptptr = s->file.data + 3; - s->scriptlen = s->file.len - 3; - memcpy_P_trim(filenamebuf,s->scriptptr); - if(httpd_fs_getchar(s->scriptptr - 1) == ISO_colon) { - httpd_fs_open(filenamebuf, &s->file); - PT_WAIT_THREAD(&s->scriptpt, send_file(s)); - } else { - PT_WAIT_THREAD(&s->scriptpt, - httpd_cgi(filenamebuf)(s, s->scriptptr)); - } - next_scriptstate(s); + /* Extract name, if starts with colon include file else call cgi */ + s->scriptptr=get_scriptname(scriptname,s->file.data+2); + s->scriptlen=s->file.len-(s->scriptptr-s->file.data); + PRINTF("httpd: Handle script named %s\n",scriptname); + if(scriptname[0] == ISO_colon) { + if (httpd_fs_open(&scriptname[1], &s->file)) { + PT_WAIT_THREAD(&s->scriptpt, send_file(s)); + } + } else { + PT_WAIT_THREAD(&s->scriptpt,httpd_cgi(scriptname)(s, s->scriptptr)); + } + next_scriptstate(s); - /* The script is over, so we reset the pointers and continue - sending the rest of the file. */ + /* Reset the pointers and continue sending the current file. */ s->file.data = s->scriptptr; - s->file.len = s->scriptlen; + s->file.len = s->scriptlen; } else { - /* See if we find the start of script marker in the block of HTML - to be sent. */ + /* Send file up to the next potential script */ if(s->file.len > uip_mss()) { s->len = uip_mss(); } else { @@ -210,15 +252,17 @@ PT_THREAD(handle_script(struct httpd_state *s)) } else { pptr = (char *) httpd_fs_strchr(s->file.data, ISO_percent); } + if(pptr != NULL && pptr != s->file.data) { s->len = (int)(pptr - s->file.data); if(s->len >= uip_mss()) { s->len = uip_mss(); } } + PRINTF("httpd: Sending %u bytes from 0x%04x\n",s->file.len,s->file.data); PT_WAIT_THREAD(&s->scriptpt, send_part_of_file(s)); s->file.data += s->len; - s->file.len -= s->len; + s->file.len -= s->len; } } @@ -233,7 +277,11 @@ generate_status_P(void *pstr) memcpy_P(uip_appdata+9, pstr, slen); slen+=9; memcpy_P(uip_appdata+slen, PSTR("\r\nServer: Contiki/2.0 http://www.sics.se/contiki/\r\nConnection: close\r\n"), 70); +#if DEBUGLOGIC + return 0; +#else return slen+70; +#endif } /*---------------------------------------------------------------------------*/ static unsigned short @@ -244,7 +292,11 @@ generate_header_P(void *pstr) memcpy_P(uip_appdata+14, pstr, slen); slen+=14; memcpy_P(uip_appdata+slen,PSTR("\r\n\r\n"),4); +#if DEBUGLOGIC + return 0; +#else return slen+4; +#endif } /*---------------------------------------------------------------------------*/ @@ -296,7 +348,9 @@ PT_THREAD(handle_output(struct httpd_state *s)) char *ptr; PT_BEGIN(&s->outputpt); -// strcpy(s->filename,"/index.html"); //for debugging +#if DEBUGLOGIC + strcpy(s->filename,"/index.html"); +#endif if(!httpd_fs_open(s->filename, &s->file)) { strcpy_P(s->filename, PSTR("/404.html")); httpd_fs_open(s->filename, &s->file); @@ -320,7 +374,8 @@ char http_get[4] PROGMEM ="GET "; char http_ref[8] PROGMEM ="Referer:"; static PT_THREAD(handle_input(struct httpd_state *s)) -{ +{ + PSOCK_BEGIN(&s->sin); PSOCK_READTO(&s->sin, ISO_space); @@ -350,7 +405,7 @@ PT_THREAD(handle_input(struct httpd_state *s)) if(strncmp_P(s->inputbuf, http_ref, 8) == 0) { s->inputbuf[PSOCK_DATALEN(&s->sin) - 2] = 0; -// petsciiconv_topetscii(s->inputbuf, PSOCK_DATALEN(&s->sin) - 2); + petsciiconv_topetscii(s->inputbuf, PSOCK_DATALEN(&s->sin) - 2); webserver_log(s->inputbuf); } } @@ -360,17 +415,25 @@ PT_THREAD(handle_input(struct httpd_state *s)) static void handle_connection(struct httpd_state *s) { +#if DEBUGLOGIC + handle_output(s); +#else handle_input(s); if(s->state == STATE_OUTPUT) { handle_output(s); } +#endif } /*---------------------------------------------------------------------------*/ void httpd_appcall(void *state) { +#if DEBUGLOGIC + struct httpd_state *s; //Enter here for debugging with output directed to TCPBUF + s = sg = (struct httpd_state *)memb_alloc(&conns); + if (1) { +#else struct httpd_state *s = (struct httpd_state *)state; - if(uip_closed() || uip_aborted() || uip_timedout()) { if(s != NULL) { memb_free(&conns, s); @@ -381,6 +444,7 @@ httpd_appcall(void *state) uip_abort(); return; } +#endif tcp_markconn(uip_conn, s); PSOCK_INIT(&s->sin, (uint8_t *)s->inputbuf, sizeof(s->inputbuf) - 1); PSOCK_INIT(&s->sout, (uint8_t *)s->inputbuf, sizeof(s->inputbuf) - 1); @@ -393,8 +457,8 @@ httpd_appcall(void *state) if(uip_poll()) { ++s->timer; if(s->timer >= 20) { - uip_abort(); - memb_free(&conns, s); + uip_abort(); + memb_free(&conns, s); } } else { s->timer = 0; diff --git a/platform/avr-raven/apps/raven-webserver/httpd.h b/platform/avr-raven/apps/raven-webserver/httpd.h index b34814d26..6ddd7d4b2 100644 --- a/platform/avr-raven/apps/raven-webserver/httpd.h +++ b/platform/avr-raven/apps/raven-webserver/httpd.h @@ -28,7 +28,7 @@ * * This file is part of the uIP TCP/IP stack. * - * $Id: httpd.h,v 1.2 2009/06/19 17:11:28 dak664 Exp $ + * $Id: httpd.h,v 1.3 2009/07/23 16:16:07 dak664 Exp $ * */ @@ -59,6 +59,5 @@ struct httpd_state { void httpd_init(void); void httpd_appcall(void *state); -void memcpy_P_trim(char *toram, char *fromflash); #endif /* __HTTPD_H__ */ diff --git a/platform/avr-raven/cfs-coffee-arch.c b/platform/avr-raven/cfs-coffee-arch.c index 28d208cec..b4642a405 100644 --- a/platform/avr-raven/cfs-coffee-arch.c +++ b/platform/avr-raven/cfs-coffee-arch.c @@ -42,6 +42,7 @@ #include #include #include +#include #include "cfs-coffee-arch.h" @@ -53,7 +54,7 @@ #define PRINTF(...) #endif -#define TESTCOFFEE 1 +#define TESTCOFFEE 0 #if TESTCOFFEE #include "cfs/cfs.h" @@ -61,7 +62,6 @@ #include "lib/crc16.h" #include "lib/random.h" #include -#include #define FAIL(x) error = (x); goto end; @@ -325,6 +325,7 @@ void avr_flash_read(CFS_CONF_OFFSET_TYPE addr, uint8_t *buf, CFS_CONF_OFFSET_TYPE size) { uint32_t addr32=COFFEE_START+addr; + uint16_t isize=size; #if DEBUG unsigned char *bufo=(unsigned char *)buf; uint8_t i; @@ -332,7 +333,7 @@ avr_flash_read(CFS_CONF_OFFSET_TYPE addr, uint8_t *buf, CFS_CONF_OFFSET_TYPE siz PRINTF("r0x%04x(%u) ",w,size); #endif #ifndef FLASH_WORD_READS - for (;size>0;size--) { + for (;isize>0;isize--) { #if FLASH_COMPLEMENT_DATA *buf++=~(uint8_t)pgm_read_byte_far(addr32++); #else @@ -341,25 +342,24 @@ avr_flash_read(CFS_CONF_OFFSET_TYPE addr, uint8_t *buf, CFS_CONF_OFFSET_TYPE siz } #else /* 130 bytes more PROGMEM, but faster */ - if (size&0x01) { //handle first odd byte + if (isize&0x01) { //handle first odd byte #if FLASH_COMPLEMENT_DATA *buf++=~(uint8_t)pgm_read_byte_far(addr32++); #else *buf++=(uint8_t)pgm_read_byte_far(addr32++); #endif /*FLASH_COMPLEMENT_DATA*/ - size--; + isize--; } - for (;size>1;size-=2) {//read words from flash + for (;isize>1;isize-=2) {//read words from flash #if FLASH_COMPLEMENT_DATA *(uint16_t *)buf=~(uint16_t)pgm_read_word_far(addr32); #else *(uint16_t *)buf=(uint16_t)pgm_read_word_far(addr32); #endif /*FLASH_COMPLEMENT_DATA*/ - buf+=2; addr32+=2; } - if (size) { //handle last odd byte + if (isize) { //handle last odd byte #if FLASH_COMPLEMENT_DATA *buf++=~(uint8_t)pgm_read_byte_far(addr32); #else @@ -370,7 +370,8 @@ avr_flash_read(CFS_CONF_OFFSET_TYPE addr, uint8_t *buf, CFS_CONF_OFFSET_TYPE siz #if DEBUG>1 PRINTF("\nbuf="); - for (i=0;i<16;i++) PRINTF("%2x ",*bufo++); +// PRINTF("%s",bufo); +// for (i=0;i<16;i++) PRINTF("%2x ",*bufo++); #endif } /*---------------------------------------------------------------------------*/ @@ -392,7 +393,7 @@ avr_flash_erase(coffee_page_t sector) * Note this routine will be reentered during the test! */ if ((sector+i)==COFFEE_PAGES-1) { - unsigned int j=COFFEE_START>>1,k=(COFFEE_START+COFFEE_SIZE)>>1,l=COFFEE_SIZE/1024UL; + int j=(int)(COFFEE_START>>1),k=(int)((COFFEE_START>>1)+(COFFEE_SIZE>>1)),l=(int)(COFFEE_SIZE/1024UL); printf_P(PSTR("\nTesting coffee filesystem [0x%08x -> 0x%08x (%uKb)] ..."),j,k,l); int r= coffee_file_test(); if (r<0) { @@ -401,8 +402,49 @@ avr_flash_erase(coffee_page_t sector) printf_P(PSTR("Passed! :-)\n")); } } -#endif +#endif /* TESTCOFFEE */ } + +/*httpd-fs routines + getchar is straigtforward. + strcmp only needs to handle file names for fs_open. Note filename in buf will not be zero terminated + if it fills the coffee name field, so a pseudo strcmp is done here. + strchr searches for script starts so must handle arbitrarily large strings + */ +char avr_httpd_fs_getchar(char *addr) { + char r; + avr_flash_read((CFS_CONF_OFFSET_TYPE) addr, (uint8_t*) &r, 1); + return r; +} +int avr_httpd_fs_strcmp (char *ram, char *addr) { + uint8_t i,*in,buf[32]; + avr_flash_read((CFS_CONF_OFFSET_TYPE)addr, buf, sizeof(buf)); +//return strcmp(ram, (char *)buf); + in=(uint8_t *)ram; + for (i=0;i<32;i++) { + if (buf[i]==0) return(0); + if (buf[i]!=*in) break; + in++; + } +/* A proper strcmp would return a + or minus number based on the last comparison*/ +//if (buf[i]>*in) return(i); else return(-i); + return(i); +} +char * avr_httpd_fs_strchr (char *addr, int character) { + char buf[129],*pptr; + buf[128]=character; + while (1) { + avr_flash_read((CFS_CONF_OFFSET_TYPE)addr, (uint8_t *) buf, 128); + pptr=strchr(buf, character); + if (pptr!=&buf[128]) { + if (pptr==0) return 0; + return (addr+(pptr-buf)); + } + addr+=128; + } + +} + /*---------------------------------------------------------------------------*/ /* * Transfer buf[size] from RAM to flash, starting at addr. @@ -415,7 +457,7 @@ void avr_flash_write(CFS_CONF_OFFSET_TYPE addr, uint8_t *buf, CFS_CONF_OFFSET_TYPE size) { uint32_t addr32; - uint16_t w,startpage; + uint16_t w; uint8_t bb,ba,sreg; /* Disable interrupts, make sure no eeprom write in progress */ @@ -426,18 +468,22 @@ avr_flash_write(CFS_CONF_OFFSET_TYPE addr, uint8_t *buf, CFS_CONF_OFFSET_TYPE si /* Calculate the starting address of the first flash page being modified (will be on a page boundary) and the number of unaltered bytes before and after the data to be written. */ - - startpage=addr/COFFEE_PAGE_SIZE; +#if 0 //this is 8 bytes longer + uint16_t startpage=addr/COFFEE_PAGE_SIZE; addr32=COFFEE_START+startpage*COFFEE_PAGE_SIZE; +#else + addr32=COFFEE_START+(addr&~(COFFEE_PAGE_SIZE-1)); +#endif bb=addr&0xff; ba=2*SPM_PAGESIZE-((addr+size)&0xff); #if DEBUG + uint16_t startpage=addr/COFFEE_PAGE_SIZE; w=addr32>>1; //Show progmem word address for debug if (buf) { PRINTF("w0x%04x %u %u %u",w,size,bb,ba); } else { - PRINTF("e0x%04x %u ",w,thepage); + PRINTF("e0x%04x %u ",w,startpage); } #endif diff --git a/platform/avr-raven/cfs-coffee-arch.h b/platform/avr-raven/cfs-coffee-arch.h index e9ac9046c..853c60784 100644 --- a/platform/avr-raven/cfs-coffee-arch.h +++ b/platform/avr-raven/cfs-coffee-arch.h @@ -44,19 +44,42 @@ #include "contiki-conf.h" -//Currently you may choose just one for the coffee file sytem -//#define COFFEE_AVR_EEPROM //use eeprom for file system -#define COFFEE_AVR_FLASH //use flash for file system +//Currently you may choose just one of the following for the coffee file sytem +//A static file sysstem allows file rewrites but no extensions or new files +//This allows a static linked list to index into the file system +#if COFFEE_FILES==1 //1=eeprom for static file system +#define COFFEE_AVR_EEPROM 1 +#define COFFEE_STATIC 1 +#elif COFFEE_FILES==2 //2=eeprom for full file system +#define COFFEE_AVR_EEPROM 1 +#elif COFFEE_FILES==2 //3=program flash for static file system +#define COFFEE_AVR_FLASH 1 +#define COFFEE_STATIC 1 +#else //4=program flash with full file system +#define COFFEE_AVR_FLASH 1 +#endif #ifdef COFFEE_AVR_EEPROM #include "dev/eeprom.h" //1284p EEPROM has 512 pages of 8 bytes each = 4KB -#define COFFEE_SECTOR_SIZE 64UL +#if COFFEE_ADDRESS==DEFAULT //Make can pass starting address with COFFEE_ADDRESS=0xnnnnnnnn +#undef COFFEE_ADDRESS +#ifdef CFS_EEPROM_CONF_OFFSET //Else use the platform default +#define COFFEE_ADDRESS CFS_EEPROM_CONF_OFFSET +#else //Use zero if no default defined +#define COFFEE_ADDRESS 0 +#endif +#endif + +/* Byte page size, starting address, and size of the file system */ #define COFFEE_PAGE_SIZE 16UL -#define COFFEE_START CFS_EEPROM_CONF_OFFSET +#define COFFEE_START COFFEE_ADDRESS #define COFFEE_SIZE ((3 * 1024U) - COFFEE_START) +/* These must agree with the parameters passed to makefsdata */ +#define COFFEE_SECTOR_SIZE (COFFEE_PAGE_SIZE*4) #define COFFEE_NAME_LENGTH 16 +/* These are used internally by the coffee file system */ #define COFFEE_MAX_OPEN_FILES 4 #define COFFEE_FD_SET_SIZE 8 #define COFFEE_LOG_TABLE_LIMIT 16 @@ -81,30 +104,45 @@ void avr_eeprom_erase(uint16_t sector); #ifdef COFFEE_AVR_FLASH /* 1284p PROGMEM has 512 pages of 256 bytes each = 128KB * Writing to the last 32 NRRW pages will halt the CPU. - * Take care not to overwrite the .bootloader section... */ -#define COFFEE_PAGE_SIZE 256UL //Byte per program flash page (256 for 1284p -#define COFFEE_PAGE_START 65536UL/COFFEE_PAGE_SIZE //Starting page at 64KB for webserver -#define COFFEE_PAGES 512UL-COFFEE_PAGE_START-32UL //Number of pages to use (reserve NWWR pages) -#define COFFEE_START (COFFEE_PAGE_START)*COFFEE_PAGE_SIZE //Starting address of the file system, on a page boundary -#define FLASH_WORD_READS 1 //1=Faster, but 130 bytes more PROGMEM -#define FLASH_COMPLEMENT_DATA 0 //1=Slower reads, but no page writes after erase and 18 bytes less PROGMEM + * Take care not to overwrite the .bootloader section... + */ -#define COFFEE_SIZE (COFFEE_PAGES)*COFFEE_PAGE_SIZE //Bytes in filesystem -#define COFFEE_SECTOR_SIZE (COFFEE_PAGE_SIZE*1) //Each page a sector -#define COFFEE_NAME_LENGTH 16 //processes.shtml is longest file name +/* Byte page size, starting address on page boundary, and size of the file system */ +#define COFFEE_PAGE_SIZE (2*SPM_PAGESIZE) +#ifndef COFFEE_ADDRESS //Make can pass starting address with COFFEE_ADDRESS=0xnnnnnnnn, default is 64KB for webserver +#define COFFEE_ADDRESS 0x10000 +#endif +#define COFFEE_PAGES (512-(COFFEE_ADDRESS/COFFEE_PAGE_SIZE)-32) +#define COFFEE_START (COFFEE_ADDRESS & ~(COFFEE_PAGE_SIZE-1)) +//#define COFFEE_START (COFFEE_PAGE_SIZE*COFFEE_PAGES) +#define COFFEE_SIZE (COFFEE_PAGES*COFFEE_PAGE_SIZE) + +/* These must agree with the parameters passed to makefsdata */ +#define COFFEE_SECTOR_SIZE (COFFEE_PAGE_SIZE*1) +#define COFFEE_NAME_LENGTH 16 + +/* These are used internally by the AVR flash read routines */ +/* Word reads are faster but take 130 bytes more PROGMEM */ +#define FLASH_WORD_READS 1 +/* 1=Slower reads, but no page writes after erase and 18 bytes less PROGMEM. Best for dynamic file system */ +#define FLASH_COMPLEMENT_DATA 0 + +/* These are used internally by the coffee file system */ +/* Micro logs are not needed for single page sectors */ #define COFFEE_MAX_OPEN_FILES 4 -#define COFFEE_FD_SET_SIZE 8 //Size of file descriptor +#define COFFEE_FD_SET_SIZE 8 #define COFFEE_LOG_TABLE_LIMIT 16 #define COFFEE_DIR_CACHE_ENTRIES 1 -#define COFFEE_DYN_SIZE (COFFEE_PAGE_SIZE*1) //Allocation block size -#define COFFEE_MICRO_LOGS 0 //1 to enable, 0 best for single page sectors -#define COFFEE_LOG_SIZE 128 //Microlog size, when used +#define COFFEE_DYN_SIZE (COFFEE_PAGE_SIZE*1) +#define COFFEE_MICRO_LOGS 0 +#define COFFEE_LOG_SIZE 128 /* coffee_page_t is used for page and sector numbering * uint8_t can handle 511 pages. * cfs_offset_t is used for full byte addresses * If CFS_CONF_OFFSET_TYPE is not defined it defaults to int. - * uint16_t can handle up to a 65535 byte file system. */ + * uint16_t can handle up to a 65535 byte file system. + */ #define coffee_page_t uint8_t #define CFS_CONF_OFFSET_TYPE uint16_t @@ -121,6 +159,12 @@ void avr_flash_erase(coffee_page_t sector); void avr_flash_read (CFS_CONF_OFFSET_TYPE addr, uint8_t *buf, CFS_CONF_OFFSET_TYPE size); void avr_flash_write(CFS_CONF_OFFSET_TYPE addr, uint8_t *buf, CFS_CONF_OFFSET_TYPE size); +#define avr_httpd_fs_cpy(dest,addr,size) avr_flash_read((CFS_CONF_OFFSET_TYPE) addr, (uint8_t *)dest, (CFS_CONF_OFFSET_TYPE) size) +char avr_httpd_fs_getchar(char *addr); +char * avr_httpd_fs_strchr (char *ram, int character); +int avr_httpd_fs_strcmp (char *addr,char *ram); + + #endif /* COFFEE_AVR_FLASH */ #endif /* !COFFEE_ARCH_H */ diff --git a/platform/avr-raven/contiki-raven-main.c b/platform/avr-raven/contiki-raven-main.c index a1a875512..70eeabd4a 100644 --- a/platform/avr-raven/contiki-raven-main.c +++ b/platform/avr-raven/contiki-raven-main.c @@ -30,6 +30,16 @@ * * @(#)$$ */ +#define DEBUG 0 +#if DEBUG +#define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args) +int pingtimer1=0,pingtimer2=0; +#if RF230BB +extern int rf230_interrupt_flag; +#endif +#else +#define PRINTF(...) +#endif #include #include @@ -40,21 +50,21 @@ //#include "lib/mmem.h" #include "loader/symbols-def.h" #include "loader/symtab.h" -//#include -#ifdef RF230BB -#include "radio/rf230bb/rf230bb.h" +#ifdef RF230BB //radio driver using contiki core mac +#include "rf230bb.h" #include "net/mac/frame802154.h" #include "net/sicslowpan.h" #include "net/uip-netif.h" -#include "net/mac/sicslowmac.h" - -#else +//#include "net/mac/sicslowmac.h" +#else //radio driver using Atmel/Cisco 802.15.4'ish MAC +#include #include "mac.h" -#include "sicslowmac.h" +//#include "sicslowmac.h" #include "sicslowpan.h" #include "ieee-15-4-manager.h" #endif /*RF230BB*/ +#include "sicslowmac.h" #include "contiki.h" #include "contiki-net.h" @@ -68,7 +78,12 @@ #include "raven-lcd.h" #endif -#include "sicslowmac.h" +#include "httpd-fs.h" +#ifdef COFFEE_FILES +#include "cfs/cfs.h" +#include "cfs/cfs-coffee.h" +#endif + #if UIP_CONF_ROUTER #include "net/routing/rimeroute.h" #include "net/rime/rime-udp.h" @@ -76,43 +91,25 @@ #include "net/rime.h" //#include "node-id.h" -#define DEBUG 0 -#if DEBUG -#define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args) -#else -#define PRINTF(...) do {} while (0) -#endif - -typedef struct -{ - unsigned char B2; - unsigned char B1; - unsigned char B0; -} __signature_t; - - -/* Put the MCU signature in the .elf file */ +/*-------------------------------------------------------------------------*/ +/*----------------------Configuration of the .elf file---------------------*/ +typedef struct {unsigned char B2;unsigned char B1;unsigned char B0;} __signature_t; #define SIGNATURE __signature_t __signature __attribute__((section (".signature"))) - -SIGNATURE = -{ +SIGNATURE = { /* Older AVR-GCCs may not define the SIGNATURE_n bytes so use explicit 1284p values */ .B2 = 0x05,//SIGNATURE_2, .B1 = 0x97,//SIGNATURE_1, .B0 = 0x1E,//SIGNATURE_0, }; - -/* Set the fuses in the .elf file */ -FUSES = -{ - .low = 0xe2, - .high = 0x99, - .extended = 0xff, -}; +FUSES ={.low = 0xe2, .high = 0x99, .extended = 0xff,}; /* Put default MAC address in EEPROM */ -uint8_t mac_address[8] EEMEM = {0x02, 0x11, 0x22, 0xff, 0xfe, 0x33, 0x44, 0x55}; +//uint8_t mac_address[8] EEMEM = {0x02, 0x11, 0x22, 0xff, 0xfe, 0x33, 0x44, 0x55}; +extern uint8_t mac_address[8]; //These are defined in httpd-fsdata.c via makefsdata.h +extern uint8_t server_name[16]; +extern uint8_t domain_name[30]; +/*-----------------------Initial contiki processes--------------------------*/ #ifdef RAVEN_LCD_INTERFACE #ifdef RF230BB PROCINIT(&etimer_process, &tcpip_process, &raven_lcd_process); @@ -127,33 +124,44 @@ PROCINIT(&etimer_process, &mac_process, &tcpip_process); #endif /*RF230BB*/ #endif /*RAVEN_LCD_INTERFACE*/ -void -init_lowlevel(void) +/*-------------------------Low level initialization------------------------*/ +/*------Done in a subroutine to keep main routine stack usage small--------*/ +void initialize(void) { + char buf[80]; + unsigned int size; + //calibrate_rc_osc_32k(); //CO: Had to comment this out + #ifdef RAVEN_LCD_INTERFACE /* First rs232 port for Raven 3290 port */ - rs232_init(RS232_PORT_0, USART_BAUD_38400, - USART_PARITY_NONE | USART_STOP_BITS_1 | USART_DATA_BITS_8); - + rs232_init(RS232_PORT_0, USART_BAUD_38400,USART_PARITY_NONE | USART_STOP_BITS_1 | USART_DATA_BITS_8); /* Set input handler for 3290 port */ rs232_set_input(0,raven_lcd_serial_input); #endif /* Second rs232 port for debugging */ - rs232_init(RS232_PORT_1, USART_BAUD_57600, - USART_PARITY_NONE | USART_STOP_BITS_1 | USART_DATA_BITS_8); - + rs232_init(RS232_PORT_1, USART_BAUD_57600,USART_PARITY_NONE | USART_STOP_BITS_1 | USART_DATA_BITS_8); /* Redirect stdout to second port */ rs232_redirect_stdout(RS232_PORT_1); -} + clock_init(); + printf_P(PSTR("\n*******Booting %s*******\n"),CONTIKI_VERSION_STRING); + + /* Initialize process subsystem */ + process_init(); + #ifdef RF230BB -/* Address initialization when using the core MAC layer */ -/*---------------------------------------------------------------------------*/ -static void -set_addresses(void) { + /* Start radio and radio receive process */ + rf230_init(); + sicslowpan_init(sicslowmac_init(&rf230_driver)); +// ctimer_init(); +// sicslowpan_init(lpp_init(&rf230_driver)); +// rime_init(sicslowmac_driver.init(&rf230_driver)); +// rime_init(lpp_init(&rf230_driver)); + + /* Set addresses BEFORE starting tcpip process */ + rimeaddr_t addr; - memset(&addr, 0, sizeof(rimeaddr_t)); AVR_ENTER_CRITICAL_REGION(); eeprom_read_block ((void *)&addr.u8, &mac_address, 8); @@ -175,42 +183,9 @@ set_addresses(void) rime_init(rime_udp_init(NULL)); uip_router_register(&rimeroute); #endif + + PRINTF("Driver: %s, Channel: %u\n", sicslowmac_driver.name, rf230_get_channel()); } - -#if DEBUG -int pingtimer1=0;pingtimer2=0 -extern int rf230_interrupt_flag; -#endif -#endif /*RF230BB*/ - -int -main(void) -{ - //calibrate_rc_osc_32k(); //CO: Had to comment this out - - /* Initialize serial ports */ - init_lowlevel(); - - /* Clock */ - clock_init(); - - printf_P(PSTR("\n********BOOTING CONTIKI*********\n")); - - /* Process subsystem */ - process_init(); - -#ifdef RF230BB - /* Start radio and radio receive process */ - rf230_init(); - sicslowpan_init(sicslowmac_init(&rf230_driver)); -// ctimer_init(); -// sicslowpan_init(lpp_init(&rf230_driver)); -// rime_init(sicslowmac_driver.init(&rf230_driver)); -// rime_init(lpp_init(&rf230_driver)); - - /* Set addresses BEFORE starting tcpip process */ - set_addresses(); - PRINTF(PSTR("Driver: %s, Channel: %u\n"), sicslowmac_driver.name, rf230_get_channel()); #endif /*RF230BB*/ /* Register initial processes */ @@ -220,13 +195,52 @@ main(void) autostart_start(autostart_processes); //Give ourselves a prefix - // init_net(); + // init_net(); - printf_P(PSTR(CONTIKI_VERSION_STRING" online\n")); + /*---If using coffee file system create initial web content if necessary---*/ +#if COFFEE_FILES + int fa = cfs_open( "/index.html", CFS_READ); + if (fa<0) { //Make some default web content + printf_P(PSTR("No index.html file found, creating upload.html!\n")); + printf_P(PSTR("Formatting FLASH file system for coffee...")); + cfs_coffee_format(); + printf_P(PSTR("Done!\n")); + fa = cfs_open( "/index.html", CFS_WRITE); + int r = cfs_write(fa, &"It works!", 9); + if (r<0) printf_P(PSTR("Can''t create /index.html!\n")); + cfs_close(fa); +// fa = cfs_open("upload.html"), CFW_WRITE); +//

+ } +#endif - /* Main scheduler loop */ +/*--------------------------Announce the configuration---------------------*/ + eeprom_read_block (buf,server_name, sizeof(server_name)); + buf[sizeof(server_name)]=0; + printf_P(PSTR("%s"),buf); + eeprom_read_block (buf,domain_name, sizeof(domain_name)); + buf[sizeof(domain_name)]=0; + size=httpd_fs_get_size(); +#ifndef COFFEE_FILES + printf_P(PSTR(".%s online with fixed %u byte web content\n"),buf,size); +#elif COFFEE_FILES==1 + printf_P(PSTR(".%s online with static %u byte EEPROM file system\n"),buf,size); +#elif COFFEE_FILES==2 + printf_P(PSTR(".%s online with dynamic %u KB EEPROM file system\n"),buf,size>>10); +#elif COFFEE_FILES==3 + printf_P(PSTR(".%s online with static %u byte program memory file system\n"),buf,size); +#elif COFFEE_FILES==4 + printf_P(PSTR(".%s online with dynamic %u KB program memory file system\n"),buf,size>>10); +#endif +} +/*-------------------------------------------------------------------------*/ +/*------------------------- Main Scheduler loop----------------------------*/ +/*-------------------------------------------------------------------------*/ +int +main(void) +{ + initialize(); while(1) { - process_run(); #if DEBUG @@ -236,6 +250,8 @@ main(void) rf230_interrupt_flag=0; } } +#endif +#if PINGS if (pingtimer1++==10000) { pingtimer1=0; if (pingtimer2++==1000) { @@ -247,6 +263,5 @@ main(void) #endif } - return 0; } diff --git a/platform/avr-ravenusb/contiki-raven-main.c b/platform/avr-ravenusb/contiki-raven-main.c index 43ebe7fde..9caee9e17 100644 --- a/platform/avr-ravenusb/contiki-raven-main.c +++ b/platform/avr-ravenusb/contiki-raven-main.c @@ -65,16 +65,22 @@ #include "rndis/rndis_task.h" #include "storage/storage_task.h" -FUSES = - { - .low = 0xde, - .high = 0x99, - .extended = 0xff, - }; +/*----------------------Configuration of the .elf file---------------------*/ +typedef struct {unsigned char B2;unsigned char B1;unsigned char B0;} __signature_t; +#define SIGNATURE __signature_t __signature __attribute__((section (".signature"))) +SIGNATURE = { +/* Older AVR-GCCs may not define the SIGNATURE_n bytes so use explicit values */ + .B2 = 0x82,//SIGNATURE_2, //AT90USB128x + .B1 = 0x97,//SIGNATURE_1, //128KB flash + .B0 = 0x1E,//SIGNATURE_0, //Atmel +}; +FUSES ={.low = 0xde, .high = 0x99, .extended = 0xff,}; /* Put default MAC address in EEPROM */ uint8_t mac_address[8] EEMEM = {0x02, 0x12, 0x13, 0xff, 0xfe, 0x14, 0x15, 0x16}; - +//uint8_t EEMEM mac_address[8]; //The raven webserver uses this EEMEM allocation +//uint8_t EEMEM server_name[16]; +//uint8_t EEMEM domain_name[30]; PROCINIT(&etimer_process, &mac_process);