Fonctions communes aux programmes Nucleo Web ENC28J60...

Dependents:   Nucleo_Web_ENC28J60 Nucleo_Web_ENC28J60_ADC

Committer:
Fo170
Date:
Wed Aug 19 09:49:11 2015 +0000
Revision:
0:36baf2ffa8eb
Child:
1:b52ad028c502
Fonctions communes aux programmes Nucleo Web ENC28J60...

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Fo170 0:36baf2ffa8eb 1 const uint16_t MY_PORT = 80; // for HTTP connection
Fo170 0:36baf2ffa8eb 2 EthernetServer myServer = EthernetServer(MY_PORT);
Fo170 0:36baf2ffa8eb 3
Fo170 0:36baf2ffa8eb 4 string httpHeader; // HTTP header
Fo170 0:36baf2ffa8eb 5 string httpContent; // HTTP content
Fo170 0:36baf2ffa8eb 6
Fo170 0:36baf2ffa8eb 7 string& page(uint8_t status); // Declaration de la fonction page
Fo170 0:36baf2ffa8eb 8
Fo170 0:36baf2ffa8eb 9 // analyse the url given
Fo170 0:36baf2ffa8eb 10 // return values: -1 invalid password
Fo170 0:36baf2ffa8eb 11 // -2 no command given but password valid
Fo170 0:36baf2ffa8eb 12 // -3 just refresh page
Fo170 0:36baf2ffa8eb 13 // 0 switch off
Fo170 0:36baf2ffa8eb 14 // 1 switch on
Fo170 0:36baf2ffa8eb 15 //
Fo170 0:36baf2ffa8eb 16 // The string passed to this function will look like this:
Fo170 0:36baf2ffa8eb 17 // GET /password HTTP/1.....
Fo170 0:36baf2ffa8eb 18 // GET /password/ HTTP/1.....
Fo170 0:36baf2ffa8eb 19 // GET /password/?sw=1 HTTP/1.....
Fo170 0:36baf2ffa8eb 20 // GET /password/?sw=0 HTTP/1.....
Fo170 0:36baf2ffa8eb 21
Fo170 0:36baf2ffa8eb 22 int8_t analyse_get_url(string& str)
Fo170 0:36baf2ffa8eb 23 {
Fo170 0:36baf2ffa8eb 24 if(str.substr(5, PASSWORD.size()) != PASSWORD)
Fo170 0:36baf2ffa8eb 25 return(-1);
Fo170 0:36baf2ffa8eb 26
Fo170 0:36baf2ffa8eb 27 uint8_t pos = 5 + PASSWORD.size();
Fo170 0:36baf2ffa8eb 28
Fo170 0:36baf2ffa8eb 29 if(str.substr(pos, 1) == " ") return(-2);
Fo170 0:36baf2ffa8eb 30
Fo170 0:36baf2ffa8eb 31 if(str.substr(pos, 1) != "/") return(-1);
Fo170 0:36baf2ffa8eb 32
Fo170 0:36baf2ffa8eb 33 pos++;
Fo170 0:36baf2ffa8eb 34
Fo170 0:36baf2ffa8eb 35 string cmd(str.substr(pos, 5));
Fo170 0:36baf2ffa8eb 36
Fo170 0:36baf2ffa8eb 37 if(cmd == "?sw=0") return(OFF);
Fo170 0:36baf2ffa8eb 38
Fo170 0:36baf2ffa8eb 39 if(cmd == "?sw=1") return(ON);
Fo170 0:36baf2ffa8eb 40
Fo170 0:36baf2ffa8eb 41 return(-3);
Fo170 0:36baf2ffa8eb 42 }
Fo170 0:36baf2ffa8eb 43
Fo170 0:36baf2ffa8eb 44
Fo170 0:36baf2ffa8eb 45
Fo170 0:36baf2ffa8eb 46 string& moved_perm(uint8_t flag)
Fo170 0:36baf2ffa8eb 47 {
Fo170 0:36baf2ffa8eb 48 if(flag == 1)
Fo170 0:36baf2ffa8eb 49 httpContent = "/" + PASSWORD + "/";
Fo170 0:36baf2ffa8eb 50 else
Fo170 0:36baf2ffa8eb 51 httpContent = "";
Fo170 0:36baf2ffa8eb 52 /*
Fo170 0:36baf2ffa8eb 53 httpContent += "<h1>301 Moved Permanently ";
Fo170 0:36baf2ffa8eb 54 httpContent += __image_301_Moved_Permanently__;
Fo170 0:36baf2ffa8eb 55 httpContent += "</h1>\r\n";
Fo170 0:36baf2ffa8eb 56 */
Fo170 0:36baf2ffa8eb 57 httpContent += str_moved_perm;
Fo170 0:36baf2ffa8eb 58
Fo170 0:36baf2ffa8eb 59 return (httpContent);
Fo170 0:36baf2ffa8eb 60 }
Fo170 0:36baf2ffa8eb 61
Fo170 0:36baf2ffa8eb 62 string& page_toggle_switch(uint8_t status)
Fo170 0:36baf2ffa8eb 63 {
Fo170 0:36baf2ffa8eb 64 //-------------
Fo170 0:36baf2ffa8eb 65 httpContent = str_DOCTYPE;
Fo170 0:36baf2ffa8eb 66 httpContent += "<HTML><HEAD>\r\n";
Fo170 0:36baf2ffa8eb 67 httpContent += "<title>WEB Server Nucleo F411RE - ENC28J60 - Password Page</title>\r\n";
Fo170 0:36baf2ffa8eb 68 httpContent += "</HEAD><BODY>\r\n";
Fo170 0:36baf2ffa8eb 69 httpContent += "<center><h2>WEB Server Nucleo F411RE - ENC28J60 - Password Page</h2></center>\r\n<p>";
Fo170 0:36baf2ffa8eb 70 httpContent += str_image_Password_Folder;
Fo170 0:36baf2ffa8eb 71 httpContent += "<p>\r\n";
Fo170 0:36baf2ffa8eb 72
Fo170 0:36baf2ffa8eb 73 if(status == 1)
Fo170 0:36baf2ffa8eb 74 {
Fo170 0:36baf2ffa8eb 75 httpContent += "<hr><pre>\r\n <font color=#00FF00>ON</font>";
Fo170 0:36baf2ffa8eb 76 httpContent += " <a href=\"./?sw=0\">[switch off]</a>\r\n";
Fo170 0:36baf2ffa8eb 77 }
Fo170 0:36baf2ffa8eb 78 else
Fo170 0:36baf2ffa8eb 79 {
Fo170 0:36baf2ffa8eb 80 httpContent += "<hr><pre>\r\n <font color=#FF0000>OFF</font>";
Fo170 0:36baf2ffa8eb 81 httpContent += " <a href=\"./?sw=1\">[switch on]</a>\r\n";
Fo170 0:36baf2ffa8eb 82 }
Fo170 0:36baf2ffa8eb 83
Fo170 0:36baf2ffa8eb 84 httpContent += " <a href=\".\">[refresh status]</a>\r\n";
Fo170 0:36baf2ffa8eb 85 httpContent += "</pre>\r\n<hr>\r\n";
Fo170 0:36baf2ffa8eb 86
Fo170 0:36baf2ffa8eb 87 httpContent += "</BODY></HTML>";
Fo170 0:36baf2ffa8eb 88 //-----------
Fo170 0:36baf2ffa8eb 89 //wait(1);
Fo170 0:36baf2ffa8eb 90 return httpContent;
Fo170 0:36baf2ffa8eb 91 }
Fo170 0:36baf2ffa8eb 92
Fo170 0:36baf2ffa8eb 93 void http_send(EthernetClient& client, string& header, string& content)
Fo170 0:36baf2ffa8eb 94 {
Fo170 0:36baf2ffa8eb 95 char content_length[5] = {};
Fo170 0:36baf2ffa8eb 96
Fo170 0:36baf2ffa8eb 97 header += "\r\nContent-Type: text/html\r\n";
Fo170 0:36baf2ffa8eb 98 header += "Content-Length: ";
Fo170 0:36baf2ffa8eb 99 sprintf(content_length, "%d", content.length());
Fo170 0:36baf2ffa8eb 100 header += string(content_length) + "\r\n";
Fo170 0:36baf2ffa8eb 101 header += "Pragma: no-cache\r\n";
Fo170 0:36baf2ffa8eb 102 header += "Connection: About to close\r\n";
Fo170 0:36baf2ffa8eb 103 header += "\r\n";
Fo170 0:36baf2ffa8eb 104 string webpage = header + content;
Fo170 0:36baf2ffa8eb 105 client.write((uint8_t*)webpage.c_str(),webpage.length());
Fo170 0:36baf2ffa8eb 106 }
Fo170 0:36baf2ffa8eb 107
Fo170 0:36baf2ffa8eb 108 void HTTP_LOOP(void)
Fo170 0:36baf2ffa8eb 109 {
Fo170 0:36baf2ffa8eb 110 //-----------------
Fo170 0:36baf2ffa8eb 111 UIPEthernet.begin(MY_MAC,MY_IP);
Fo170 0:36baf2ffa8eb 112 myServer.begin();
Fo170 0:36baf2ffa8eb 113 //----BEGIN WHILE-----------
Fo170 0:36baf2ffa8eb 114 while(1)
Fo170 0:36baf2ffa8eb 115 {
Fo170 0:36baf2ffa8eb 116
Fo170 0:36baf2ffa8eb 117 EthernetClient client = myServer.available();
Fo170 0:36baf2ffa8eb 118 if(client)
Fo170 0:36baf2ffa8eb 119 {
Fo170 0:36baf2ffa8eb 120 size_t size = client.available();
Fo170 0:36baf2ffa8eb 121 if(size > 0)
Fo170 0:36baf2ffa8eb 122 {
Fo170 0:36baf2ffa8eb 123 uint8_t* buf = (uint8_t*)malloc(size);
Fo170 0:36baf2ffa8eb 124 size = client.read(buf, size);
Fo170 0:36baf2ffa8eb 125 string received((char*)buf);
Fo170 0:36baf2ffa8eb 126 free(buf);
Fo170 0:36baf2ffa8eb 127 if(received.substr(0, 3) != "GET")
Fo170 0:36baf2ffa8eb 128 {
Fo170 0:36baf2ffa8eb 129 // head, post or other method
Fo170 0:36baf2ffa8eb 130 // for possible status codes see:
Fo170 0:36baf2ffa8eb 131 // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
Fo170 0:36baf2ffa8eb 132 httpHeader = HTTP_OK;
Fo170 0:36baf2ffa8eb 133 httpContent = "<h1>200 OK</h1>";
Fo170 0:36baf2ffa8eb 134 http_send(client, httpHeader, httpContent);
Fo170 0:36baf2ffa8eb 135 continue;
Fo170 0:36baf2ffa8eb 136 }
Fo170 0:36baf2ffa8eb 137
Fo170 0:36baf2ffa8eb 138 if(received.substr(0, 6) == "GET / ")
Fo170 0:36baf2ffa8eb 139 {
Fo170 0:36baf2ffa8eb 140 httpHeader = HTTP_OK;
Fo170 0:36baf2ffa8eb 141 // httpContent = "<p>Usage: http://host_or_ip/password</p>\r\n";
Fo170 0:36baf2ffa8eb 142 // http_send(client, httpHeader, httpContent);
Fo170 0:36baf2ffa8eb 143
Fo170 0:36baf2ffa8eb 144 http_send(client, httpHeader, page(sw));
Fo170 0:36baf2ffa8eb 145 continue;
Fo170 0:36baf2ffa8eb 146 }
Fo170 0:36baf2ffa8eb 147
Fo170 0:36baf2ffa8eb 148 int cmd = analyse_get_url(received);
Fo170 0:36baf2ffa8eb 149
Fo170 0:36baf2ffa8eb 150 if(cmd == -2)
Fo170 0:36baf2ffa8eb 151 {
Fo170 0:36baf2ffa8eb 152 // redirect to the right base url
Fo170 0:36baf2ffa8eb 153 httpHeader = MOVED_PERM;
Fo170 0:36baf2ffa8eb 154 http_send(client, httpHeader, moved_perm(1));
Fo170 0:36baf2ffa8eb 155 continue;
Fo170 0:36baf2ffa8eb 156 }
Fo170 0:36baf2ffa8eb 157
Fo170 0:36baf2ffa8eb 158 if(cmd == -1)
Fo170 0:36baf2ffa8eb 159 {
Fo170 0:36baf2ffa8eb 160 httpHeader = UNAUTHORIZED;
Fo170 0:36baf2ffa8eb 161 // httpContent = "<h1>401 Unauthorized ";
Fo170 0:36baf2ffa8eb 162 // httpContent += __image_401_Unauthorized__;
Fo170 0:36baf2ffa8eb 163 // httpContent += "</h1>\r\n";
Fo170 0:36baf2ffa8eb 164
Fo170 0:36baf2ffa8eb 165 httpContent = str_Unauthorized;
Fo170 0:36baf2ffa8eb 166 http_send(client, httpHeader, httpContent);
Fo170 0:36baf2ffa8eb 167 continue;
Fo170 0:36baf2ffa8eb 168 }
Fo170 0:36baf2ffa8eb 169
Fo170 0:36baf2ffa8eb 170 if(cmd == 1)
Fo170 0:36baf2ffa8eb 171 {
Fo170 0:36baf2ffa8eb 172 sw = 1; // switch on
Fo170 0:36baf2ffa8eb 173 }
Fo170 0:36baf2ffa8eb 174
Fo170 0:36baf2ffa8eb 175 if(cmd == 0)
Fo170 0:36baf2ffa8eb 176 {
Fo170 0:36baf2ffa8eb 177 sw = 0; // switch off
Fo170 0:36baf2ffa8eb 178 }
Fo170 0:36baf2ffa8eb 179
Fo170 0:36baf2ffa8eb 180 httpHeader = HTTP_OK;
Fo170 0:36baf2ffa8eb 181 http_send(client, httpHeader, page_toggle_switch(sw));
Fo170 0:36baf2ffa8eb 182 }
Fo170 0:36baf2ffa8eb 183 }
Fo170 0:36baf2ffa8eb 184
Fo170 0:36baf2ffa8eb 185 }
Fo170 0:36baf2ffa8eb 186 //----END WHILE-----------
Fo170 0:36baf2ffa8eb 187 }