ProjetER3GR2BLegrosAbbs
html.cpp@1:ba608856b208, 2015-08-12 (annotated)
- Committer:
- superphil06
- Date:
- Wed Aug 12 15:53:49 2015 +0000
- Revision:
- 1:ba608856b208
- Parent:
- 0:539b0fc9d536
- Child:
- 2:889c56a58376
including thread and user call back
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
superphil06 | 0:539b0fc9d536 | 1 | /**************************************************************************** |
superphil06 | 0:539b0fc9d536 | 2 | * |
superphil06 | 0:539b0fc9d536 | 3 | * (C) 2000 by BECK IPC GmbH |
superphil06 | 0:539b0fc9d536 | 4 | * |
superphil06 | 0:539b0fc9d536 | 5 | * BECK IPC GmbH |
superphil06 | 0:539b0fc9d536 | 6 | * Garbenheimerstr. 38 |
superphil06 | 0:539b0fc9d536 | 7 | * D-35578 Wetzlar |
superphil06 | 0:539b0fc9d536 | 8 | * |
superphil06 | 0:539b0fc9d536 | 9 | * Phone : (49)-6441-905-240 |
superphil06 | 0:539b0fc9d536 | 10 | * Fax : (49)-6441-905-245 |
superphil06 | 0:539b0fc9d536 | 11 | * |
superphil06 | 0:539b0fc9d536 | 12 | * --------------------------------------------------------------------------- |
superphil06 | 0:539b0fc9d536 | 13 | * Module : HTML.C |
superphil06 | 0:539b0fc9d536 | 14 | * Function : contains functions to operate between HTML files and SC12 |
superphil06 | 0:539b0fc9d536 | 15 | * Cgi Interface. It read Html files, copy it into memory |
superphil06 | 0:539b0fc9d536 | 16 | * block and save positions of '^VARDEF[x]' entry in var_field |
superphil06 | 0:539b0fc9d536 | 17 | * struct to insert dynamtic code int the static file. |
superphil06 | 0:539b0fc9d536 | 18 | |
superphil06 | 0:539b0fc9d536 | 19 | By Example: |
superphil06 | 0:539b0fc9d536 | 20 | On the flash exists the following htm file (a:\exp.htm): |
superphil06 | 0:539b0fc9d536 | 21 | _______________________________________________________ |
superphil06 | 0:539b0fc9d536 | 22 | |
superphil06 | 0:539b0fc9d536 | 23 | <HTML><HEAD><TITLE>Example Page</TITLE></HEAD> |
superphil06 | 0:539b0fc9d536 | 24 | <BODY> |
superphil06 | 0:539b0fc9d536 | 25 | The first Dynamic code/text ^VARDEF[0] = 32 <BR> |
superphil06 | 0:539b0fc9d536 | 26 | And now the Dynamic code/text ^VARDEF[1] = 3 <BR> |
superphil06 | 0:539b0fc9d536 | 27 | </BODY> |
superphil06 | 0:539b0fc9d536 | 28 | </HTML> |
superphil06 | 0:539b0fc9d536 | 29 | _______________________________________________________ |
superphil06 | 0:539b0fc9d536 | 30 | |
superphil06 | 0:539b0fc9d536 | 31 | now you have to call the function in the following way: |
superphil06 | 0:539b0fc9d536 | 32 | |
superphil06 | 0:539b0fc9d536 | 33 | |
superphil06 | 0:539b0fc9d536 | 34 | |
superphil06 | 0:539b0fc9d536 | 35 | |
superphil06 | 0:539b0fc9d536 | 36 | |
superphil06 | 0:539b0fc9d536 | 37 | |
superphil06 | 0:539b0fc9d536 | 38 | #include "html.h" |
superphil06 | 0:539b0fc9d536 | 39 | |
superphil06 | 0:539b0fc9d536 | 40 | vard_field_t exp_vardef[2]; // size "2": 2 var defs in htm file |
superphil06 | 0:539b0fc9d536 | 41 | char exp_page[1024]; // html code buffer (1024 bytes) |
superphil06 | 0:539b0fc9d536 | 42 | |
superphil06 | 0:539b0fc9d536 | 43 | .... |
superphil06 | 0:539b0fc9d536 | 44 | .... |
superphil06 | 0:539b0fc9d536 | 45 | .. |
superphil06 | 0:539b0fc9d536 | 46 | |
superphil06 | 0:539b0fc9d536 | 47 | int main(void) |
superphil06 | 0:539b0fc9d536 | 48 | { |
superphil06 | 0:539b0fc9d536 | 49 | char tmpBuffer[1024]; |
superphil06 | 0:539b0fc9d536 | 50 | ... |
superphil06 | 0:539b0fc9d536 | 51 | |
superphil06 | 0:539b0fc9d536 | 52 | |
superphil06 | 0:539b0fc9d536 | 53 | |
superphil06 | 0:539b0fc9d536 | 54 | |
superphil06 | 0:539b0fc9d536 | 55 | exp_page = Gen_HtmlCode_From_File( "a:\exp.htm", &exp_vardef, 2 ); |
superphil06 | 0:539b0fc9d536 | 56 | |
superphil06 | 0:539b0fc9d536 | 57 | // know, if exp != Null the page is in the memory block. |
superphil06 | 0:539b0fc9d536 | 58 | // the Vardef field are filled with blank spaces (ASCII 32). |
superphil06 | 0:539b0fc9d536 | 59 | // The exp_vardef struct points to this vardefs fields. |
superphil06 | 0:539b0fc9d536 | 60 | // So you can access to this empty fields in the |
superphil06 | 0:539b0fc9d536 | 61 | // following way (normaly the filling of the Vardefs will |
superphil06 | 0:539b0fc9d536 | 62 | // be done in the CGI function): |
superphil06 | 0:539b0fc9d536 | 63 | |
superphil06 | 0:539b0fc9d536 | 64 | // 1. text: |
superphil06 | 0:539b0fc9d536 | 65 | strcpy( tmpBuffer, "I am the First text" ); |
superphil06 | 0:539b0fc9d536 | 66 | FILL_STR( tmpBuffer, exp_vardef[0].length ); // this will |
superphil06 | 0:539b0fc9d536 | 67 | // to fill the not used characters of |
superphil06 | 0:539b0fc9d536 | 68 | // the string with blank spaces |
superphil06 | 0:539b0fc9d536 | 69 | memcpy( exp_vardef[0].ptr, tmpBufferm, exp_vardef[0].length ); |
superphil06 | 0:539b0fc9d536 | 70 | |
superphil06 | 0:539b0fc9d536 | 71 | |
superphil06 | 0:539b0fc9d536 | 72 | // 2. text: |
superphil06 | 0:539b0fc9d536 | 73 | strcpy( tmpBuffer, "I am the First text" ); |
superphil06 | 0:539b0fc9d536 | 74 | FILL_STR( tmpBuffer, exp_vardef[0].length ); // this will |
superphil06 | 0:539b0fc9d536 | 75 | // to fill the not used characters of |
superphil06 | 0:539b0fc9d536 | 76 | // the string with blank spaces |
superphil06 | 0:539b0fc9d536 | 77 | memcpy( exp_vardef[0].ptr, tmpBufferm, exp_vardef[0].length ); |
superphil06 | 0:539b0fc9d536 | 78 | |
superphil06 | 0:539b0fc9d536 | 79 | ..... |
superphil06 | 0:539b0fc9d536 | 80 | .. |
superphil06 | 0:539b0fc9d536 | 81 | return; |
superphil06 | 0:539b0fc9d536 | 82 | |
superphil06 | 0:539b0fc9d536 | 83 | } |
superphil06 | 0:539b0fc9d536 | 84 | |
superphil06 | 0:539b0fc9d536 | 85 | * |
superphil06 | 0:539b0fc9d536 | 86 | * Author : Stoidner |
superphil06 | 0:539b0fc9d536 | 87 | * Date : 30.08.00 |
superphil06 | 0:539b0fc9d536 | 88 | * Version : V1.00 |
superphil06 | 0:539b0fc9d536 | 89 | * --------------------------------------------------------------------------- |
superphil06 | 0:539b0fc9d536 | 90 | * History : |
superphil06 | 0:539b0fc9d536 | 91 | * |
superphil06 | 0:539b0fc9d536 | 92 | * Vx.yy Author Changes |
superphil06 | 0:539b0fc9d536 | 93 | * |
superphil06 | 0:539b0fc9d536 | 94 | * V1.00 cs Create |
superphil06 | 0:539b0fc9d536 | 95 | **************************************************************/ |
superphil06 | 0:539b0fc9d536 | 96 | |
superphil06 | 0:539b0fc9d536 | 97 | |
superphil06 | 0:539b0fc9d536 | 98 | //******************* |
superphil06 | 0:539b0fc9d536 | 99 | //* standard includes |
superphil06 | 0:539b0fc9d536 | 100 | |
superphil06 | 0:539b0fc9d536 | 101 | #include "mbed.h" |
superphil06 | 0:539b0fc9d536 | 102 | #include <stdio.h> |
superphil06 | 0:539b0fc9d536 | 103 | #include <stdlib.h> |
superphil06 | 0:539b0fc9d536 | 104 | #include <string.h> |
superphil06 | 1:ba608856b208 | 105 | #include "EthernetInterface.h" |
superphil06 | 1:ba608856b208 | 106 | #include <stdlib.h> |
superphil06 | 1:ba608856b208 | 107 | #include <string.h> |
superphil06 | 0:539b0fc9d536 | 108 | |
superphil06 | 0:539b0fc9d536 | 109 | //******************** |
superphil06 | 0:539b0fc9d536 | 110 | //* specified includes |
superphil06 | 0:539b0fc9d536 | 111 | #include "html.h" |
superphil06 | 1:ba608856b208 | 112 | |
superphil06 | 1:ba608856b208 | 113 | #include "rtos.h" |
superphil06 | 0:539b0fc9d536 | 114 | |
superphil06 | 0:539b0fc9d536 | 115 | |
superphil06 | 1:ba608856b208 | 116 | #define _HTML_DEBUG |
superphil06 | 1:ba608856b208 | 117 | #define _IP_DEBUG |
superphil06 | 1:ba608856b208 | 118 | #define _TCP_DEBUG |
superphil06 | 1:ba608856b208 | 119 | #define _HTTP_DEBUG |
superphil06 | 1:ba608856b208 | 120 | # define _HTML_DEBUG |
superphil06 | 1:ba608856b208 | 121 | |
superphil06 | 1:ba608856b208 | 122 | extern var_field_t tab_balise[10]; //une balise est présente dans le squelette |
superphil06 | 1:ba608856b208 | 123 | |
superphil06 | 1:ba608856b208 | 124 | |
superphil06 | 1:ba608856b208 | 125 | EthernetInterface eth; // ethernet layer management object |
superphil06 | 1:ba608856b208 | 126 | TCPSocketServer svr; // |
superphil06 | 1:ba608856b208 | 127 | bool serverIsListened = false;// flag to indicate server is listening |
superphil06 | 1:ba608856b208 | 128 | #define PORT 80 // tcp port for socket listeningt |
superphil06 | 1:ba608856b208 | 129 | TCPSocketConnection client;// socket to identify client side |
superphil06 | 1:ba608856b208 | 130 | bool clientIsConnected = false;// flag that indicate if a web client is avalaible |
superphil06 | 1:ba608856b208 | 131 | |
superphil06 | 1:ba608856b208 | 132 | |
superphil06 | 1:ba608856b208 | 133 | char *html_page; // pointer to memory area of Html Page |
superphil06 | 1:ba608856b208 | 134 | /*************** locals prototypes ***************/ |
superphil06 | 0:539b0fc9d536 | 135 | |
superphil06 | 0:539b0fc9d536 | 136 | |
superphil06 | 0:539b0fc9d536 | 137 | |
superphil06 | 0:539b0fc9d536 | 138 | |
superphil06 | 0:539b0fc9d536 | 139 | |
superphil06 | 0:539b0fc9d536 | 140 | |
superphil06 | 1:ba608856b208 | 141 | //char * Gen_HtmlCode_From_File( char *Path, var_field_t *var_field, int max_vardef ) |
superphil06 | 1:ba608856b208 | 142 | char * Gen_HtmlCode_From_File( char *Path, var_field_t *var_field, int max_vardef ) |
superphil06 | 0:539b0fc9d536 | 143 | {LocalFileSystem local("local"); |
superphil06 | 0:539b0fc9d536 | 144 | FILE *in; // File Handle |
superphil06 | 0:539b0fc9d536 | 145 | //fpos_t filepos; // file position counter |
superphil06 | 0:539b0fc9d536 | 146 | long filepos; // file position counter |
superphil06 | 0:539b0fc9d536 | 147 | int data; // read data from file |
superphil06 | 0:539b0fc9d536 | 148 | char value_str[40]; // to search for '^VARDEF[x]' entrys |
superphil06 | 0:539b0fc9d536 | 149 | int i=0; // VarDef Counter ( '^VARDEF[i]' ) |
superphil06 | 0:539b0fc9d536 | 150 | long mem_pos = -1; // actually mem offset |
superphil06 | 1:ba608856b208 | 151 | //char *html_page; // pointer to memory area of Html Page |
superphil06 | 0:539b0fc9d536 | 152 | |
superphil06 | 0:539b0fc9d536 | 153 | if ((in = fopen(Path, "rt")) == NULL) |
superphil06 | 0:539b0fc9d536 | 154 | { |
superphil06 | 0:539b0fc9d536 | 155 | printf("\r\nError Open File: -%s-", Path); |
superphil06 | 0:539b0fc9d536 | 156 | return(NULL); |
superphil06 | 0:539b0fc9d536 | 157 | } |
superphil06 | 0:539b0fc9d536 | 158 | |
superphil06 | 1:ba608856b208 | 159 | #ifdef _HTML_DEBUG |
superphil06 | 0:539b0fc9d536 | 160 | printf("\r\nOpen File.. \"%s\"", Path); |
superphil06 | 0:539b0fc9d536 | 161 | #endif |
superphil06 | 0:539b0fc9d536 | 162 | |
superphil06 | 0:539b0fc9d536 | 163 | // checking Html Page size and filling var_field struct |
superphil06 | 0:539b0fc9d536 | 164 | while (!feof(in)) |
superphil06 | 0:539b0fc9d536 | 165 | { |
superphil06 | 0:539b0fc9d536 | 166 | // serach for Commands |
superphil06 | 0:539b0fc9d536 | 167 | mem_pos++; |
superphil06 | 0:539b0fc9d536 | 168 | |
superphil06 | 0:539b0fc9d536 | 169 | // check for unexpected page size |
superphil06 | 0:539b0fc9d536 | 170 | if (mem_pos >= 0xFFFF) |
superphil06 | 0:539b0fc9d536 | 171 | { |
superphil06 | 0:539b0fc9d536 | 172 | fclose(in); |
superphil06 | 0:539b0fc9d536 | 173 | return(NULL); |
superphil06 | 0:539b0fc9d536 | 174 | } |
superphil06 | 0:539b0fc9d536 | 175 | |
superphil06 | 0:539b0fc9d536 | 176 | if ( fgetc(in) == CMD_HTML_SIGN ) |
superphil06 | 0:539b0fc9d536 | 177 | { |
superphil06 | 0:539b0fc9d536 | 178 | //fgetpos(in, &filepos); |
superphil06 | 0:539b0fc9d536 | 179 | filepos= ftell ( in );// replace fgetpos call |
superphil06 | 0:539b0fc9d536 | 180 | |
superphil06 | 0:539b0fc9d536 | 181 | |
superphil06 | 0:539b0fc9d536 | 182 | if ( fscanf(in , "VARDEF[ %d ] = %s", &i, &value_str ) ) |
superphil06 | 0:539b0fc9d536 | 183 | { |
superphil06 | 1:ba608856b208 | 184 | #ifdef _HTML_DEBUG |
superphil06 | 0:539b0fc9d536 | 185 | printf("\r\nFound and save entry.."); |
superphil06 | 0:539b0fc9d536 | 186 | #endif |
superphil06 | 0:539b0fc9d536 | 187 | mem_pos += atoi(value_str)-1; |
superphil06 | 0:539b0fc9d536 | 188 | } |
superphil06 | 0:539b0fc9d536 | 189 | else fseek( in, filepos, SEEK_SET ); |
superphil06 | 0:539b0fc9d536 | 190 | } |
superphil06 | 0:539b0fc9d536 | 191 | |
superphil06 | 0:539b0fc9d536 | 192 | } // while (!feof |
superphil06 | 0:539b0fc9d536 | 193 | |
superphil06 | 0:539b0fc9d536 | 194 | // allocate memory |
superphil06 | 0:539b0fc9d536 | 195 | html_page = (char *) malloc( (int) mem_pos+1 ); |
superphil06 | 0:539b0fc9d536 | 196 | |
superphil06 | 0:539b0fc9d536 | 197 | // check if alloc was successful |
superphil06 | 0:539b0fc9d536 | 198 | if (html_page == NULL) return(NULL); |
superphil06 | 0:539b0fc9d536 | 199 | |
superphil06 | 0:539b0fc9d536 | 200 | // jump to beginning of html file |
superphil06 | 0:539b0fc9d536 | 201 | fseek( in, 0, SEEK_SET); |
superphil06 | 0:539b0fc9d536 | 202 | |
superphil06 | 0:539b0fc9d536 | 203 | // now loading website into memory |
superphil06 | 0:539b0fc9d536 | 204 | mem_pos = 0; |
superphil06 | 1:ba608856b208 | 205 | #ifdef _HTML_DEBUG |
superphil06 | 0:539b0fc9d536 | 206 | printf("\r\nReading Page"); |
superphil06 | 0:539b0fc9d536 | 207 | #endif |
superphil06 | 0:539b0fc9d536 | 208 | while (!feof(in)) |
superphil06 | 0:539b0fc9d536 | 209 | { |
superphil06 | 0:539b0fc9d536 | 210 | data = fgetc(in); |
superphil06 | 0:539b0fc9d536 | 211 | |
superphil06 | 0:539b0fc9d536 | 212 | // serach for Commands |
superphil06 | 0:539b0fc9d536 | 213 | if ( data == CMD_HTML_SIGN ) |
superphil06 | 0:539b0fc9d536 | 214 | { |
superphil06 | 0:539b0fc9d536 | 215 | // fgetpos(in, &filepos); |
superphil06 | 0:539b0fc9d536 | 216 | filepos =ftell ( in );// replace fgetpos call |
superphil06 | 0:539b0fc9d536 | 217 | |
superphil06 | 0:539b0fc9d536 | 218 | if ( fscanf(in , "VARDEF[ %d ] = %s", &i, &value_str ) ) |
superphil06 | 0:539b0fc9d536 | 219 | { |
superphil06 | 0:539b0fc9d536 | 220 | if (var_field) |
superphil06 | 0:539b0fc9d536 | 221 | { |
superphil06 | 0:539b0fc9d536 | 222 | // save VarDef |
superphil06 | 0:539b0fc9d536 | 223 | |
superphil06 | 0:539b0fc9d536 | 224 | // check if vardef index is valid |
superphil06 | 0:539b0fc9d536 | 225 | if ( i>max_vardef ) { free(html_page); return(NULL); }; |
superphil06 | 0:539b0fc9d536 | 226 | //var_field[i].length = atoi(value_str); |
superphil06 | 0:539b0fc9d536 | 227 | var_field[i].length=strlen(value_str); |
superphil06 | 0:539b0fc9d536 | 228 | var_field[i].ptr = &html_page[(int)mem_pos]; |
superphil06 | 0:539b0fc9d536 | 229 | } |
superphil06 | 0:539b0fc9d536 | 230 | |
superphil06 | 0:539b0fc9d536 | 231 | // insert free characters |
superphil06 | 0:539b0fc9d536 | 232 | // for (i=0; i<=atoi(value_str)-1; i++) |
superphil06 | 0:539b0fc9d536 | 233 | for(i=0;i<=strlen(value_str);i++) |
superphil06 | 0:539b0fc9d536 | 234 | { |
superphil06 | 0:539b0fc9d536 | 235 | html_page[(int)mem_pos] = (char) 32; |
superphil06 | 0:539b0fc9d536 | 236 | mem_pos++; |
superphil06 | 0:539b0fc9d536 | 237 | } |
superphil06 | 0:539b0fc9d536 | 238 | |
superphil06 | 0:539b0fc9d536 | 239 | } |
superphil06 | 0:539b0fc9d536 | 240 | else |
superphil06 | 0:539b0fc9d536 | 241 | { |
superphil06 | 0:539b0fc9d536 | 242 | // set data pointer back |
superphil06 | 0:539b0fc9d536 | 243 | fseek( in, (long) filepos, SEEK_SET); |
superphil06 | 0:539b0fc9d536 | 244 | |
superphil06 | 0:539b0fc9d536 | 245 | // copy data into memory |
superphil06 | 0:539b0fc9d536 | 246 | html_page[(int)mem_pos] = (char)data; |
superphil06 | 0:539b0fc9d536 | 247 | mem_pos++; |
superphil06 | 0:539b0fc9d536 | 248 | } |
superphil06 | 0:539b0fc9d536 | 249 | |
superphil06 | 0:539b0fc9d536 | 250 | } |
superphil06 | 0:539b0fc9d536 | 251 | else |
superphil06 | 0:539b0fc9d536 | 252 | { |
superphil06 | 0:539b0fc9d536 | 253 | // copy data into memory |
superphil06 | 0:539b0fc9d536 | 254 | html_page[(int)mem_pos] = (char)data; |
superphil06 | 0:539b0fc9d536 | 255 | mem_pos++; |
superphil06 | 0:539b0fc9d536 | 256 | } |
superphil06 | 0:539b0fc9d536 | 257 | |
superphil06 | 0:539b0fc9d536 | 258 | } // while (!feof |
superphil06 | 0:539b0fc9d536 | 259 | |
superphil06 | 0:539b0fc9d536 | 260 | // terminate string |
superphil06 | 0:539b0fc9d536 | 261 | html_page[(int)mem_pos-1] = (char)'\0'; |
superphil06 | 0:539b0fc9d536 | 262 | |
superphil06 | 0:539b0fc9d536 | 263 | |
superphil06 | 0:539b0fc9d536 | 264 | |
superphil06 | 0:539b0fc9d536 | 265 | // close HTML Page |
superphil06 | 0:539b0fc9d536 | 266 | fclose(in); |
superphil06 | 0:539b0fc9d536 | 267 | |
superphil06 | 1:ba608856b208 | 268 | #ifdef _HTML_DEBUG |
superphil06 | 0:539b0fc9d536 | 269 | printf("\r\nDone."); |
superphil06 | 0:539b0fc9d536 | 270 | #endif |
superphil06 | 0:539b0fc9d536 | 271 | |
superphil06 | 0:539b0fc9d536 | 272 | |
superphil06 | 0:539b0fc9d536 | 273 | return(html_page); |
superphil06 | 0:539b0fc9d536 | 274 | } |
superphil06 | 0:539b0fc9d536 | 275 | //simple function thatr load file from disk and return pointer |
superphil06 | 0:539b0fc9d536 | 276 | |
superphil06 | 0:539b0fc9d536 | 277 | char *load_HtmlCode_From_File( char *Path,long * size) |
superphil06 | 0:539b0fc9d536 | 278 | {LocalFileSystem local("local"); |
superphil06 | 0:539b0fc9d536 | 279 | FILE *in; // File Handle |
superphil06 | 0:539b0fc9d536 | 280 | int data; // read data from file |
superphil06 | 0:539b0fc9d536 | 281 | long mem_pos = -1; // actually mem offset |
superphil06 | 0:539b0fc9d536 | 282 | char *html_page; // pointer to memory area of Html Page |
superphil06 | 0:539b0fc9d536 | 283 | if ((in = fopen(Path, "rt")) == NULL) |
superphil06 | 0:539b0fc9d536 | 284 | { printf("\r\nError Open File: -%s-", Path); //open file from disk |
superphil06 | 0:539b0fc9d536 | 285 | return(NULL); |
superphil06 | 0:539b0fc9d536 | 286 | } |
superphil06 | 0:539b0fc9d536 | 287 | |
superphil06 | 0:539b0fc9d536 | 288 | while (!feof(in)) // checking Html Page size |
superphil06 | 0:539b0fc9d536 | 289 | { |
superphil06 | 0:539b0fc9d536 | 290 | mem_pos++; |
superphil06 | 0:539b0fc9d536 | 291 | if ( (fgetc(in) == EOF)||(mem_pos==0xFFFF) ) |
superphil06 | 0:539b0fc9d536 | 292 | {break;} |
superphil06 | 0:539b0fc9d536 | 293 | }// while (!feof ) |
superphil06 | 0:539b0fc9d536 | 294 | html_page = (char *) malloc( (int) mem_pos+1 );// allocate memory |
superphil06 | 0:539b0fc9d536 | 295 | |
superphil06 | 0:539b0fc9d536 | 296 | // check if alloc was successful |
superphil06 | 0:539b0fc9d536 | 297 | if (html_page == NULL) return(NULL); |
superphil06 | 0:539b0fc9d536 | 298 | fseek( in, 0, SEEK_SET);// jump to beginning of html file |
superphil06 | 0:539b0fc9d536 | 299 | |
superphil06 | 0:539b0fc9d536 | 300 | mem_pos = 0; // now loading website into memory |
superphil06 | 0:539b0fc9d536 | 301 | while (!feof(in)) |
superphil06 | 0:539b0fc9d536 | 302 | { data = fgetc(in); |
superphil06 | 0:539b0fc9d536 | 303 | html_page[(int)mem_pos] = (char)data; |
superphil06 | 0:539b0fc9d536 | 304 | mem_pos++; |
superphil06 | 0:539b0fc9d536 | 305 | } // while (!feof |
superphil06 | 0:539b0fc9d536 | 306 | |
superphil06 | 0:539b0fc9d536 | 307 | html_page[(int)mem_pos-1] = (char)'\0'; // terminate string |
superphil06 | 0:539b0fc9d536 | 308 | *size=mem_pos-1; //return size of file |
superphil06 | 0:539b0fc9d536 | 309 | fclose(in); // close HTML Page |
superphil06 | 0:539b0fc9d536 | 310 | return(html_page); |
superphil06 | 0:539b0fc9d536 | 311 | } |
superphil06 | 0:539b0fc9d536 | 312 | |
superphil06 | 0:539b0fc9d536 | 313 | void Html_Patch ( var_field_t *pTab_Balise,int index, char * pChaine ){ |
superphil06 | 0:539b0fc9d536 | 314 | |
superphil06 | 0:539b0fc9d536 | 315 | FILL_STR( pChaine,pTab_Balise[index].length ); |
superphil06 | 0:539b0fc9d536 | 316 | memcpy( pTab_Balise[index].ptr, pChaine, pTab_Balise[index].length ); |
superphil06 | 0:539b0fc9d536 | 317 | } |
superphil06 | 1:ba608856b208 | 318 | |
superphil06 | 1:ba608856b208 | 319 | void (*pfPtr) (void); |
superphil06 | 1:ba608856b208 | 320 | |
superphil06 | 1:ba608856b208 | 321 | |
superphil06 | 1:ba608856b208 | 322 | |
superphil06 | 1:ba608856b208 | 323 | int Init_Web_Server(void (*fPtr)(void) )// fptr pointer to a void interrupt CGI function |
superphil06 | 1:ba608856b208 | 324 | {pfPtr=fPtr;// affectation du pointeur public |
superphil06 | 1:ba608856b208 | 325 | |
superphil06 | 1:ba608856b208 | 326 | //setup ethernet interface |
superphil06 | 1:ba608856b208 | 327 | eth.init(); //Use DHCP |
superphil06 | 1:ba608856b208 | 328 | eth.connect(); |
superphil06 | 1:ba608856b208 | 329 | #ifdef _IP_DEBUG |
superphil06 | 1:ba608856b208 | 330 | printf("IP Address is %s\n\r", eth.getIPAddress());// display server ip address |
superphil06 | 1:ba608856b208 | 331 | #endif |
superphil06 | 1:ba608856b208 | 332 | // Set the callbacks for Listening |
superphil06 | 1:ba608856b208 | 333 | //srv.setOnEvent(&onListeningTCPSocketEvent); // create a call back fonction associated with listning socket |
superphil06 | 1:ba608856b208 | 334 | //setup srv tcp socket |
superphil06 | 1:ba608856b208 | 335 | if(svr.bind(PORT)< 0) { |
superphil06 | 1:ba608856b208 | 336 | #ifdef _IP_DEBUG |
superphil06 | 1:ba608856b208 | 337 | printf("tcp server bind failed.\n\r"); |
superphil06 | 1:ba608856b208 | 338 | #endif |
superphil06 | 1:ba608856b208 | 339 | return -1; |
superphil06 | 1:ba608856b208 | 340 | } else { |
superphil06 | 1:ba608856b208 | 341 | #ifdef _IP_DEBUG |
superphil06 | 1:ba608856b208 | 342 | printf("tcp server bind successed.\n\r"); |
superphil06 | 1:ba608856b208 | 343 | #endif |
superphil06 | 1:ba608856b208 | 344 | serverIsListened = true; |
superphil06 | 1:ba608856b208 | 345 | } |
superphil06 | 1:ba608856b208 | 346 | |
superphil06 | 1:ba608856b208 | 347 | if(svr.listen(1) < 0) { |
superphil06 | 1:ba608856b208 | 348 | #ifdef _IP_DEBUG |
superphil06 | 1:ba608856b208 | 349 | printf("tcp server listen failed.\n\r"); |
superphil06 | 1:ba608856b208 | 350 | #endif |
superphil06 | 1:ba608856b208 | 351 | return -1; |
superphil06 | 1:ba608856b208 | 352 | } else { |
superphil06 | 1:ba608856b208 | 353 | #ifdef _IP_DEBUG |
superphil06 | 1:ba608856b208 | 354 | printf("tcp server is listening...\n\r"); |
superphil06 | 1:ba608856b208 | 355 | #endif |
superphil06 | 1:ba608856b208 | 356 | |
superphil06 | 1:ba608856b208 | 357 | return 0; |
superphil06 | 1:ba608856b208 | 358 | } |
superphil06 | 1:ba608856b208 | 359 | |
superphil06 | 1:ba608856b208 | 360 | } |
superphil06 | 1:ba608856b208 | 361 | |
superphil06 | 1:ba608856b208 | 362 | |
superphil06 | 1:ba608856b208 | 363 | //************* used to close srv socket and web server thread *********************** |
superphil06 | 1:ba608856b208 | 364 | int DeInit_Web_Server(void) |
superphil06 | 1:ba608856b208 | 365 | {int iError; |
superphil06 | 1:ba608856b208 | 366 | serverIsListened=false;// |
superphil06 | 1:ba608856b208 | 367 | iError=svr.close (true);// close main server socket |
superphil06 | 1:ba608856b208 | 368 | return iError; |
superphil06 | 1:ba608856b208 | 369 | |
superphil06 | 1:ba608856b208 | 370 | } |
superphil06 | 1:ba608856b208 | 371 | |
superphil06 | 1:ba608856b208 | 372 | |
superphil06 | 1:ba608856b208 | 373 | |
superphil06 | 1:ba608856b208 | 374 | |
superphil06 | 1:ba608856b208 | 375 | |
superphil06 | 1:ba608856b208 | 376 | |
superphil06 | 1:ba608856b208 | 377 | |
superphil06 | 1:ba608856b208 | 378 | /***************** thread section **************/ |
superphil06 | 1:ba608856b208 | 379 | |
superphil06 | 1:ba608856b208 | 380 | |
superphil06 | 1:ba608856b208 | 381 | void Web_Server_Thread(void const *args) |
superphil06 | 1:ba608856b208 | 382 | { |
superphil06 | 1:ba608856b208 | 383 | |
superphil06 | 1:ba608856b208 | 384 | printf("web server thread start.. \n\r"); |
superphil06 | 1:ba608856b208 | 385 | |
superphil06 | 1:ba608856b208 | 386 | while ( serverIsListened) { |
superphil06 | 1:ba608856b208 | 387 | //blocking mode(never timeout) |
superphil06 | 1:ba608856b208 | 388 | if(svr.accept(client)<0) { |
superphil06 | 1:ba608856b208 | 389 | #ifdef _TCP_DEBUG |
superphil06 | 1:ba608856b208 | 390 | printf("failed to accept connection.\n\r"); |
superphil06 | 1:ba608856b208 | 391 | #endif |
superphil06 | 1:ba608856b208 | 392 | } else { |
superphil06 | 1:ba608856b208 | 393 | #ifdef _TCP_DEBUG |
superphil06 | 1:ba608856b208 | 394 | printf("connection success!\n\rIP: %s\n\r",client.get_address()); |
superphil06 | 1:ba608856b208 | 395 | #endif |
superphil06 | 1:ba608856b208 | 396 | clientIsConnected = true; |
superphil06 | 1:ba608856b208 | 397 | // Setup the new socket events |
superphil06 | 1:ba608856b208 | 398 | // client.setOnEvent(&onConnectedTCPSocketEvent); |
superphil06 | 1:ba608856b208 | 399 | // We can find out from where the connection is coming by looking at the |
superphil06 | 1:ba608856b208 | 400 | // Host parameter of the accept() method |
superphil06 | 1:ba608856b208 | 401 | |
superphil06 | 1:ba608856b208 | 402 | while(clientIsConnected) { |
superphil06 | 1:ba608856b208 | 403 | char buffer[1024] = {}; |
superphil06 | 1:ba608856b208 | 404 | |
superphil06 | 1:ba608856b208 | 405 | switch(client.receive(buffer, 1023)) { |
superphil06 | 1:ba608856b208 | 406 | case 0: |
superphil06 | 1:ba608856b208 | 407 | #ifdef _TCP_DEBUG |
superphil06 | 1:ba608856b208 | 408 | printf("recieved buffer is empty.\n\r"); |
superphil06 | 1:ba608856b208 | 409 | #endif |
superphil06 | 1:ba608856b208 | 410 | clientIsConnected = false; |
superphil06 | 1:ba608856b208 | 411 | break; |
superphil06 | 1:ba608856b208 | 412 | case -1: |
superphil06 | 1:ba608856b208 | 413 | #ifdef _TCP_DEBUG |
superphil06 | 1:ba608856b208 | 414 | printf("failed to read data from client.\n\r"); |
superphil06 | 1:ba608856b208 | 415 | #endif |
superphil06 | 1:ba608856b208 | 416 | clientIsConnected = false; |
superphil06 | 1:ba608856b208 | 417 | break; |
superphil06 | 1:ba608856b208 | 418 | default: |
superphil06 | 1:ba608856b208 | 419 | #ifdef _HTTP_DEBUG |
superphil06 | 1:ba608856b208 | 420 | printf("Recieved Data: %d\n\r\n\r%.*s\n\r",strlen(buffer),strlen(buffer),buffer); |
superphil06 | 1:ba608856b208 | 421 | #endif |
superphil06 | 1:ba608856b208 | 422 | if(buffer[0] == 'G' && buffer[1] == 'E' && buffer[2] == 'T' ) { |
superphil06 | 1:ba608856b208 | 423 | #ifdef _HTTP_DEBUG |
superphil06 | 1:ba608856b208 | 424 | printf("GET request incomming.\n\r"); |
superphil06 | 1:ba608856b208 | 425 | #endif |
superphil06 | 1:ba608856b208 | 426 | //setup http response header & data |
superphil06 | 1:ba608856b208 | 427 | /************* patch all dynamic data in html response page ************/ |
superphil06 | 1:ba608856b208 | 428 | //giCounter++; |
superphil06 | 1:ba608856b208 | 429 | |
superphil06 | 1:ba608856b208 | 430 | (*pfPtr)();// call to external CGI function located in main |
superphil06 | 1:ba608856b208 | 431 | |
superphil06 | 1:ba608856b208 | 432 | /* char ma_chaine4[20]={};// needed to form html response |
superphil06 | 1:ba608856b208 | 433 | sprintf (ma_chaine4,"%d",(int)15);// convert speed as ascii string |
superphil06 | 1:ba608856b208 | 434 | Html_Patch (tab_balise,0,ma_chaine4);// patch first label with dyn.string |
superphil06 | 1:ba608856b208 | 435 | |
superphil06 | 1:ba608856b208 | 436 | sprintf (ma_chaine4,"%d",(int)16);// convert count as ascii string |
superphil06 | 1:ba608856b208 | 437 | Html_Patch (tab_balise,1,ma_chaine4);// patch first label with dyn.string |
superphil06 | 1:ba608856b208 | 438 | |
superphil06 | 1:ba608856b208 | 439 | sprintf (ma_chaine4,"%d",17);// convert count as ascii string |
superphil06 | 1:ba608856b208 | 440 | Html_Patch (tab_balise,2,ma_chaine4);// patch first label with dyn.string |
superphil06 | 1:ba608856b208 | 441 | |
superphil06 | 1:ba608856b208 | 442 | sprintf (ma_chaine4,"%d",18);// convert count as ascii string |
superphil06 | 1:ba608856b208 | 443 | Html_Patch (tab_balise,3,ma_chaine4);// patch first label with dyn.string |
superphil06 | 1:ba608856b208 | 444 | |
superphil06 | 1:ba608856b208 | 445 | sprintf (ma_chaine4,"%d",19);// convert count as ascii string |
superphil06 | 1:ba608856b208 | 446 | Html_Patch (tab_balise,4,ma_chaine4);// patch first label with dyn.string |
superphil06 | 1:ba608856b208 | 447 | |
superphil06 | 1:ba608856b208 | 448 | */ |
superphil06 | 1:ba608856b208 | 449 | |
superphil06 | 1:ba608856b208 | 450 | |
superphil06 | 1:ba608856b208 | 451 | |
superphil06 | 1:ba608856b208 | 452 | |
superphil06 | 1:ba608856b208 | 453 | char echoHeader[256] = {}; |
superphil06 | 1:ba608856b208 | 454 | sprintf(echoHeader,"HTTP/1.1 200 OK\n\rContent-Length: %d\n\rContent-Type: text\n\rConnection: Close\n\r",strlen(html_page)); |
superphil06 | 1:ba608856b208 | 455 | client.send(echoHeader,strlen(echoHeader)); |
superphil06 | 1:ba608856b208 | 456 | #ifdef _HTTP_DEBUG |
superphil06 | 1:ba608856b208 | 457 | printf("%s",echoHeader);// debut http header sent |
superphil06 | 1:ba608856b208 | 458 | #endif |
superphil06 | 1:ba608856b208 | 459 | //client.send(buffer,strlen(buffer));// realise echo request to client |
superphil06 | 1:ba608856b208 | 460 | |
superphil06 | 1:ba608856b208 | 461 | |
superphil06 | 1:ba608856b208 | 462 | client.send(html_page,strlen(html_page));// realise echo request to client |
superphil06 | 1:ba608856b208 | 463 | #ifdef _HTTP_DEBUG |
superphil06 | 1:ba608856b208 | 464 | printf("%s",html_page); |
superphil06 | 1:ba608856b208 | 465 | #endif |
superphil06 | 1:ba608856b208 | 466 | |
superphil06 | 1:ba608856b208 | 467 | clientIsConnected = false;// close connection with this client at end of http response |
superphil06 | 1:ba608856b208 | 468 | #ifdef _HTTP_DEBUG |
superphil06 | 1:ba608856b208 | 469 | printf("web page sent.\n\r"); |
superphil06 | 1:ba608856b208 | 470 | #endif |
superphil06 | 1:ba608856b208 | 471 | } |
superphil06 | 1:ba608856b208 | 472 | break; |
superphil06 | 1:ba608856b208 | 473 | }// end case |
superphil06 | 1:ba608856b208 | 474 | //Thread::wait(10);// sleep for 10 ms |
superphil06 | 1:ba608856b208 | 475 | }// end while Client is Connected |
superphil06 | 1:ba608856b208 | 476 | #ifdef _HTTP_DEBUG |
superphil06 | 1:ba608856b208 | 477 | printf("close connection.\n\rtcp server is listening...\n\r"); |
superphil06 | 1:ba608856b208 | 478 | #endif |
superphil06 | 1:ba608856b208 | 479 | client.close(); |
superphil06 | 1:ba608856b208 | 480 | |
superphil06 | 1:ba608856b208 | 481 | |
superphil06 | 1:ba608856b208 | 482 | } |
superphil06 | 1:ba608856b208 | 483 | Thread::wait(10);// sleep for 10 ms |
superphil06 | 1:ba608856b208 | 484 | } // end while ServerIs listening |
superphil06 | 1:ba608856b208 | 485 | } |
superphil06 | 1:ba608856b208 | 486 | |
superphil06 | 1:ba608856b208 | 487 | |
superphil06 | 1:ba608856b208 | 488 | |
superphil06 | 1:ba608856b208 | 489 | |
superphil06 | 1:ba608856b208 | 490 | |
superphil06 | 1:ba608856b208 | 491 | |
superphil06 | 1:ba608856b208 | 492 | |
superphil06 | 1:ba608856b208 | 493 | |
superphil06 | 1:ba608856b208 | 494 |