library ued to server dynamic web page by means of html_patch function
Dependents: scooter_mbed_etudiant scooter_ scooter_mbed_etudiant_fini_1 Oliver_Melot ... more
Revision 1:ba608856b208, committed 2015-08-12
- Comitter:
- superphil06
- Date:
- Wed Aug 12 15:53:49 2015 +0000
- Parent:
- 0:539b0fc9d536
- Commit message:
- including thread and user call back
Changed in this revision
html.cpp | Show annotated file Show diff for this revision Revisions of this file |
html.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 539b0fc9d536 -r ba608856b208 html.cpp --- a/html.cpp Tue Aug 11 15:00:40 2015 +0000 +++ b/html.cpp Wed Aug 12 15:53:49 2015 +0000 @@ -102,23 +102,44 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include "EthernetInterface.h" +#include <stdlib.h> +#include <string.h> //******************** //* specified includes #include "html.h" -#include <stdlib.h> + +#include "rtos.h" -#define HTML_DEBUG +#define _HTML_DEBUG +#define _IP_DEBUG +#define _TCP_DEBUG +#define _HTTP_DEBUG +# define _HTML_DEBUG + +extern var_field_t tab_balise[10]; //une balise est présente dans le squelette + + +EthernetInterface eth; // ethernet layer management object +TCPSocketServer svr; // +bool serverIsListened = false;// flag to indicate server is listening +#define PORT 80 // tcp port for socket listeningt +TCPSocketConnection client;// socket to identify client side +bool clientIsConnected = false;// flag that indicate if a web client is avalaible + + +char *html_page; // pointer to memory area of Html Page +/*************** locals prototypes ***************/ - - -char *Gen_HtmlCode_From_File( char *Path, var_field_t *var_field, int max_vardef ) +//char * Gen_HtmlCode_From_File( char *Path, var_field_t *var_field, int max_vardef ) +char * Gen_HtmlCode_From_File( char *Path, var_field_t *var_field, int max_vardef ) {LocalFileSystem local("local"); FILE *in; // File Handle //fpos_t filepos; // file position counter @@ -127,7 +148,7 @@ char value_str[40]; // to search for '^VARDEF[x]' entrys int i=0; // VarDef Counter ( '^VARDEF[i]' ) long mem_pos = -1; // actually mem offset - char *html_page; // pointer to memory area of Html Page + //char *html_page; // pointer to memory area of Html Page if ((in = fopen(Path, "rt")) == NULL) { @@ -135,7 +156,7 @@ return(NULL); } - #ifdef HTML_DEBUG + #ifdef _HTML_DEBUG printf("\r\nOpen File.. \"%s\"", Path); #endif @@ -160,7 +181,7 @@ if ( fscanf(in , "VARDEF[ %d ] = %s", &i, &value_str ) ) { - #ifdef HTML_DEBUG + #ifdef _HTML_DEBUG printf("\r\nFound and save entry.."); #endif mem_pos += atoi(value_str)-1; @@ -181,7 +202,7 @@ // now loading website into memory mem_pos = 0; - #ifdef HTML_DEBUG + #ifdef _HTML_DEBUG printf("\r\nReading Page"); #endif while (!feof(in)) @@ -244,7 +265,7 @@ // close HTML Page fclose(in); - #ifdef HTML_DEBUG + #ifdef _HTML_DEBUG printf("\r\nDone."); #endif @@ -294,3 +315,180 @@ FILL_STR( pChaine,pTab_Balise[index].length ); memcpy( pTab_Balise[index].ptr, pChaine, pTab_Balise[index].length ); } + +void (*pfPtr) (void); + + + +int Init_Web_Server(void (*fPtr)(void) )// fptr pointer to a void interrupt CGI function + {pfPtr=fPtr;// affectation du pointeur public + + //setup ethernet interface + eth.init(); //Use DHCP + eth.connect(); + #ifdef _IP_DEBUG + printf("IP Address is %s\n\r", eth.getIPAddress());// display server ip address + #endif + // Set the callbacks for Listening + //srv.setOnEvent(&onListeningTCPSocketEvent); // create a call back fonction associated with listning socket + //setup srv tcp socket + if(svr.bind(PORT)< 0) { + #ifdef _IP_DEBUG + printf("tcp server bind failed.\n\r"); + #endif + return -1; + } else { + #ifdef _IP_DEBUG + printf("tcp server bind successed.\n\r"); + #endif + serverIsListened = true; + } + + if(svr.listen(1) < 0) { + #ifdef _IP_DEBUG + printf("tcp server listen failed.\n\r"); + #endif + return -1; + } else { + #ifdef _IP_DEBUG + printf("tcp server is listening...\n\r"); + #endif + +return 0; + } + + } + + +//************* used to close srv socket and web server thread *********************** +int DeInit_Web_Server(void) +{int iError; + serverIsListened=false;// +iError=svr.close (true);// close main server socket +return iError; + + } + + + + + + + +/***************** thread section **************/ + + +void Web_Server_Thread(void const *args) +{ + + printf("web server thread start.. \n\r"); + + while ( serverIsListened) { + //blocking mode(never timeout) + if(svr.accept(client)<0) { + #ifdef _TCP_DEBUG + printf("failed to accept connection.\n\r"); + #endif + } else { + #ifdef _TCP_DEBUG + printf("connection success!\n\rIP: %s\n\r",client.get_address()); + #endif + clientIsConnected = true; + // Setup the new socket events + // client.setOnEvent(&onConnectedTCPSocketEvent); + // We can find out from where the connection is coming by looking at the + // Host parameter of the accept() method + + while(clientIsConnected) { + char buffer[1024] = {}; + + switch(client.receive(buffer, 1023)) { + case 0: + #ifdef _TCP_DEBUG + printf("recieved buffer is empty.\n\r"); + #endif + clientIsConnected = false; + break; + case -1: + #ifdef _TCP_DEBUG + printf("failed to read data from client.\n\r"); + #endif + clientIsConnected = false; + break; + default: + #ifdef _HTTP_DEBUG + printf("Recieved Data: %d\n\r\n\r%.*s\n\r",strlen(buffer),strlen(buffer),buffer); + #endif + if(buffer[0] == 'G' && buffer[1] == 'E' && buffer[2] == 'T' ) { + #ifdef _HTTP_DEBUG + printf("GET request incomming.\n\r"); + #endif + //setup http response header & data + /************* patch all dynamic data in html response page ************/ + //giCounter++; + + (*pfPtr)();// call to external CGI function located in main + + /* char ma_chaine4[20]={};// needed to form html response + sprintf (ma_chaine4,"%d",(int)15);// convert speed as ascii string + Html_Patch (tab_balise,0,ma_chaine4);// patch first label with dyn.string + + sprintf (ma_chaine4,"%d",(int)16);// convert count as ascii string + Html_Patch (tab_balise,1,ma_chaine4);// patch first label with dyn.string + + sprintf (ma_chaine4,"%d",17);// convert count as ascii string + Html_Patch (tab_balise,2,ma_chaine4);// patch first label with dyn.string + + sprintf (ma_chaine4,"%d",18);// convert count as ascii string + Html_Patch (tab_balise,3,ma_chaine4);// patch first label with dyn.string + + sprintf (ma_chaine4,"%d",19);// convert count as ascii string + Html_Patch (tab_balise,4,ma_chaine4);// patch first label with dyn.string + + */ + + + + + char echoHeader[256] = {}; + sprintf(echoHeader,"HTTP/1.1 200 OK\n\rContent-Length: %d\n\rContent-Type: text\n\rConnection: Close\n\r",strlen(html_page)); + client.send(echoHeader,strlen(echoHeader)); + #ifdef _HTTP_DEBUG + printf("%s",echoHeader);// debut http header sent + #endif + //client.send(buffer,strlen(buffer));// realise echo request to client + + + client.send(html_page,strlen(html_page));// realise echo request to client + #ifdef _HTTP_DEBUG + printf("%s",html_page); + #endif + + clientIsConnected = false;// close connection with this client at end of http response + #ifdef _HTTP_DEBUG + printf("web page sent.\n\r"); + #endif + } + break; + }// end case + //Thread::wait(10);// sleep for 10 ms + }// end while Client is Connected + #ifdef _HTTP_DEBUG + printf("close connection.\n\rtcp server is listening...\n\r"); + #endif + client.close(); + + + } + Thread::wait(10);// sleep for 10 ms + } // end while ServerIs listening +} + + + + + + + + + \ No newline at end of file
diff -r 539b0fc9d536 -r ba608856b208 html.h --- a/html.h Tue Aug 11 15:00:40 2015 +0000 +++ b/html.h Wed Aug 12 15:53:49 2015 +0000 @@ -50,7 +50,11 @@ //************ //* prototypes -char *Gen_HtmlCode_From_File( char *Path, var_field_t *var_field, int max_vardef ); +//char *Gen_HtmlCode_From_File( char *Path, var_field_t *var_field, int max_vardef ); +char * Gen_HtmlCode_From_File( char *Path, var_field_t *var_field, int max_vardef ); char *load_HtmlCode_From_File( char *Path,long * size); void Html_Patch ( var_field_t *pTab_Balise,int index, char * pChaine ) ; +int Init_Web_Server(void (*)(void) );// used to initialize web, ethernet and pointer to cgi function +int DeInit_Web_Server(void);// used to close main server socket and web server thread +void Web_Server_Thread(void const *args) ; #endif