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