Fonctions communes aux programmes Nucleo Web ENC28J60...

Dependents:   Nucleo_Web_ENC28J60 Nucleo_Web_ENC28J60_ADC

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Fct_Web.h Source File

Fct_Web.h

00001 const uint16_t   MY_PORT = 80;      // for HTTP connection
00002 EthernetServer   myServer = EthernetServer(MY_PORT);
00003 
00004 string       httpHeader;     // HTTP header
00005 string       httpContent;    // HTTP content
00006 
00007 string& page(uint8_t status); // Declaration de la fonction page
00008 
00009 // analyse the url given
00010 // return values: -1 invalid password
00011 //                -2 no command given but password valid
00012 //                -3 just refresh page
00013 //                 0 switch off
00014 //                 1 switch on
00015 //
00016 //                The string passed to this function will look like this:
00017 //                GET /password HTTP/1.....
00018 //                GET /password/ HTTP/1.....
00019 //                GET /password/?sw=1 HTTP/1.....
00020 //                GET /password/?sw=0 HTTP/1.....
00021 
00022 int8_t analyse_get_url(string& str)
00023 {
00024     if(str.substr(5, PASSWORD.size()) != PASSWORD)
00025         return(-1);
00026 
00027     uint8_t pos = 5 + PASSWORD.size();
00028 
00029     if(str.substr(pos, 1) == " ") return(-2);
00030 
00031     if(str.substr(pos, 1) != "/") return(-1);
00032 
00033     pos++;
00034 
00035     string cmd(str.substr(pos, 5));
00036 
00037     if(cmd == "?sw=0")  return(OFF);
00038 
00039     if(cmd == "?sw=1")  return(ON);
00040 
00041     return(-3);
00042 }
00043 
00044 
00045     
00046 string& moved_perm(uint8_t flag)
00047 {
00048     if(flag == 1)
00049         httpContent = "/" +  PASSWORD + "/";
00050     else
00051         httpContent = "";
00052  
00053     httpContent += "<h1>301 Moved Permanently ";
00054     httpContent += str_moved_perm;
00055     httpContent += "</h1>\r\n";
00056     
00057     return (httpContent);
00058 }
00059 
00060 string& page_toggle_switch(uint8_t status)
00061 {
00062     //-------------
00063     httpContent = str_DOCTYPE;
00064     httpContent += "<HTML><HEAD>\r\n";
00065     httpContent += "<title>WEB Server Nucleo F411RE - ENC28J60 - Password Page</title>\r\n";
00066     httpContent += "</HEAD><BODY><center>\r\n";
00067     httpContent += "<h2>WEB Server Nucleo F411RE - ENC28J60 - Password Page</h2>\r\n<p>";
00068     httpContent += str_image_Password_Folder;
00069     httpContent += "<p>\r\n";
00070     
00071     if(status == 1)
00072     {
00073         httpContent += "<hr><pre>\r\n  <font color=#00FF00>ON</font>";
00074         httpContent += " <a href=\"./?sw=0\">[switch off]</a>";
00075         httpContent += str_ampoule_OFF;
00076         httpContent += "\r\n";
00077     }
00078     else
00079     {
00080         httpContent += "<hr><pre>\r\n  <font color=#FF0000>OFF</font>";
00081         httpContent += " <a href=\"./?sw=1\">[switch on]</a>";
00082         httpContent += str_ampoule_ON;
00083         httpContent += "\r\n";
00084     }
00085 
00086     httpContent += "  <a href=\".\">[refresh status]</a>\r\n";
00087     httpContent += "</pre>\r\n<hr>\r\n";
00088     
00089     httpContent += "</center></BODY></HTML>";
00090     //-----------
00091     //wait(1);
00092     return httpContent;
00093 }
00094 
00095 void http_send(EthernetClient& client, string& header, string& content)
00096 {
00097     char content_length[5] = {};
00098 
00099     header += "\r\nContent-Type: text/html\r\n";
00100     header += "Content-Length: ";
00101     sprintf(content_length, "%d", content.length());
00102     header += string(content_length) + "\r\n";
00103     header += "Pragma: no-cache\r\n";
00104     header += "Connection: About to close\r\n";
00105     header += "\r\n";
00106     string webpage = header + content;
00107     client.write((uint8_t*)webpage.c_str(),webpage.length());
00108 }
00109 
00110 void HTTP_LOOP(void)
00111 {
00112 //-----------------      
00113     UIPEthernet.begin(MY_MAC,MY_IP);
00114     myServer.begin();
00115    //----BEGIN WHILE----------- 
00116   while(1)
00117     {
00118 
00119      EthernetClient client = myServer.available();
00120         if(client)
00121         {
00122             size_t size = client.available();
00123             if(size > 0)
00124             {
00125                 uint8_t* buf = (uint8_t*)malloc(size);
00126                 size = client.read(buf, size);
00127                 string received((char*)buf);
00128                 free(buf);
00129                 if(received.substr(0, 3) != "GET")
00130                 {
00131                     // head, post or other method
00132                     // for possible status codes see:
00133                     // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
00134                     httpHeader = HTTP_OK;
00135                     httpContent = "<h1>200 OK</h1>";
00136                     http_send(client, httpHeader, httpContent);
00137                     continue;
00138                 }
00139 
00140                 if(received.substr(0, 6) == "GET / ")
00141                 {
00142                     httpHeader = HTTP_OK;
00143                     // httpContent = "<p>Usage: http://host_or_ip/password</p>\r\n";
00144                     // http_send(client, httpHeader, httpContent);
00145                     
00146                     http_send(client, httpHeader, page(sw));
00147                     continue;
00148                 }
00149 
00150                 int cmd = analyse_get_url(received);
00151 
00152                 if(cmd == -2)
00153                 {
00154                     // redirect to the right base url
00155                     httpHeader = MOVED_PERM;
00156                     http_send(client, httpHeader, moved_perm(1));
00157                     continue;
00158                 }
00159 
00160                 if(cmd == -1)
00161                 {
00162                     httpHeader = UNAUTHORIZED;
00163                     httpContent = "<h1>401 Unauthorized ";
00164                     httpContent = str_Unauthorized;
00165                     httpContent += "</h1>\r\n";
00166                     
00167                     http_send(client, httpHeader, httpContent);
00168                     continue;
00169                 }
00170 
00171                 if(cmd == 1)
00172                 { 
00173                  sw = 1;     // switch on
00174                 }
00175 
00176                 if(cmd == 0)
00177                 {
00178                  sw = 0;    // switch off
00179                 }
00180 
00181                 httpHeader = HTTP_OK;
00182                 http_send(client, httpHeader, page_toggle_switch(sw));
00183             }
00184         }  
00185 
00186     }
00187     //----END WHILE-----------    
00188 }