Fonctions communes aux programmes Nucleo Web ENC28J60...
Dependents: Nucleo_Web_ENC28J60 Nucleo_Web_ENC28J60_ADC
Fct_Web.h@2:ba0105da75cd, 2015-09-28 (annotated)
- Committer:
- Fo170
- Date:
- Mon Sep 28 22:19:50 2015 +0000
- Revision:
- 2:ba0105da75cd
- Parent:
- 1:b52ad028c502
mise a jours info carte ENC28J60 de MikroE
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:b52ad028c502 | 52 | |
Fo170 | 0:36baf2ffa8eb | 53 | httpContent += "<h1>301 Moved Permanently "; |
Fo170 | 1:b52ad028c502 | 54 | httpContent += str_moved_perm; |
Fo170 | 0:36baf2ffa8eb | 55 | httpContent += "</h1>\r\n"; |
Fo170 | 0:36baf2ffa8eb | 56 | |
Fo170 | 0:36baf2ffa8eb | 57 | return (httpContent); |
Fo170 | 0:36baf2ffa8eb | 58 | } |
Fo170 | 0:36baf2ffa8eb | 59 | |
Fo170 | 0:36baf2ffa8eb | 60 | string& page_toggle_switch(uint8_t status) |
Fo170 | 0:36baf2ffa8eb | 61 | { |
Fo170 | 0:36baf2ffa8eb | 62 | //------------- |
Fo170 | 0:36baf2ffa8eb | 63 | httpContent = str_DOCTYPE; |
Fo170 | 0:36baf2ffa8eb | 64 | httpContent += "<HTML><HEAD>\r\n"; |
Fo170 | 0:36baf2ffa8eb | 65 | httpContent += "<title>WEB Server Nucleo F411RE - ENC28J60 - Password Page</title>\r\n"; |
Fo170 | 2:ba0105da75cd | 66 | httpContent += "</HEAD><BODY><center>\r\n"; |
Fo170 | 2:ba0105da75cd | 67 | httpContent += "<h2>WEB Server Nucleo F411RE - ENC28J60 - Password Page</h2>\r\n<p>"; |
Fo170 | 0:36baf2ffa8eb | 68 | httpContent += str_image_Password_Folder; |
Fo170 | 0:36baf2ffa8eb | 69 | httpContent += "<p>\r\n"; |
Fo170 | 0:36baf2ffa8eb | 70 | |
Fo170 | 0:36baf2ffa8eb | 71 | if(status == 1) |
Fo170 | 0:36baf2ffa8eb | 72 | { |
Fo170 | 0:36baf2ffa8eb | 73 | httpContent += "<hr><pre>\r\n <font color=#00FF00>ON</font>"; |
Fo170 | 1:b52ad028c502 | 74 | httpContent += " <a href=\"./?sw=0\">[switch off]</a>"; |
Fo170 | 1:b52ad028c502 | 75 | httpContent += str_ampoule_OFF; |
Fo170 | 1:b52ad028c502 | 76 | httpContent += "\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 | 1:b52ad028c502 | 81 | httpContent += " <a href=\"./?sw=1\">[switch on]</a>"; |
Fo170 | 1:b52ad028c502 | 82 | httpContent += str_ampoule_ON; |
Fo170 | 1:b52ad028c502 | 83 | httpContent += "\r\n"; |
Fo170 | 0:36baf2ffa8eb | 84 | } |
Fo170 | 0:36baf2ffa8eb | 85 | |
Fo170 | 0:36baf2ffa8eb | 86 | httpContent += " <a href=\".\">[refresh status]</a>\r\n"; |
Fo170 | 0:36baf2ffa8eb | 87 | httpContent += "</pre>\r\n<hr>\r\n"; |
Fo170 | 0:36baf2ffa8eb | 88 | |
Fo170 | 2:ba0105da75cd | 89 | httpContent += "</center></BODY></HTML>"; |
Fo170 | 0:36baf2ffa8eb | 90 | //----------- |
Fo170 | 0:36baf2ffa8eb | 91 | //wait(1); |
Fo170 | 0:36baf2ffa8eb | 92 | return httpContent; |
Fo170 | 0:36baf2ffa8eb | 93 | } |
Fo170 | 0:36baf2ffa8eb | 94 | |
Fo170 | 0:36baf2ffa8eb | 95 | void http_send(EthernetClient& client, string& header, string& content) |
Fo170 | 0:36baf2ffa8eb | 96 | { |
Fo170 | 0:36baf2ffa8eb | 97 | char content_length[5] = {}; |
Fo170 | 0:36baf2ffa8eb | 98 | |
Fo170 | 0:36baf2ffa8eb | 99 | header += "\r\nContent-Type: text/html\r\n"; |
Fo170 | 0:36baf2ffa8eb | 100 | header += "Content-Length: "; |
Fo170 | 0:36baf2ffa8eb | 101 | sprintf(content_length, "%d", content.length()); |
Fo170 | 0:36baf2ffa8eb | 102 | header += string(content_length) + "\r\n"; |
Fo170 | 0:36baf2ffa8eb | 103 | header += "Pragma: no-cache\r\n"; |
Fo170 | 0:36baf2ffa8eb | 104 | header += "Connection: About to close\r\n"; |
Fo170 | 0:36baf2ffa8eb | 105 | header += "\r\n"; |
Fo170 | 0:36baf2ffa8eb | 106 | string webpage = header + content; |
Fo170 | 0:36baf2ffa8eb | 107 | client.write((uint8_t*)webpage.c_str(),webpage.length()); |
Fo170 | 0:36baf2ffa8eb | 108 | } |
Fo170 | 0:36baf2ffa8eb | 109 | |
Fo170 | 0:36baf2ffa8eb | 110 | void HTTP_LOOP(void) |
Fo170 | 0:36baf2ffa8eb | 111 | { |
Fo170 | 0:36baf2ffa8eb | 112 | //----------------- |
Fo170 | 0:36baf2ffa8eb | 113 | UIPEthernet.begin(MY_MAC,MY_IP); |
Fo170 | 0:36baf2ffa8eb | 114 | myServer.begin(); |
Fo170 | 0:36baf2ffa8eb | 115 | //----BEGIN WHILE----------- |
Fo170 | 0:36baf2ffa8eb | 116 | while(1) |
Fo170 | 0:36baf2ffa8eb | 117 | { |
Fo170 | 0:36baf2ffa8eb | 118 | |
Fo170 | 0:36baf2ffa8eb | 119 | EthernetClient client = myServer.available(); |
Fo170 | 0:36baf2ffa8eb | 120 | if(client) |
Fo170 | 0:36baf2ffa8eb | 121 | { |
Fo170 | 0:36baf2ffa8eb | 122 | size_t size = client.available(); |
Fo170 | 0:36baf2ffa8eb | 123 | if(size > 0) |
Fo170 | 0:36baf2ffa8eb | 124 | { |
Fo170 | 0:36baf2ffa8eb | 125 | uint8_t* buf = (uint8_t*)malloc(size); |
Fo170 | 0:36baf2ffa8eb | 126 | size = client.read(buf, size); |
Fo170 | 0:36baf2ffa8eb | 127 | string received((char*)buf); |
Fo170 | 0:36baf2ffa8eb | 128 | free(buf); |
Fo170 | 0:36baf2ffa8eb | 129 | if(received.substr(0, 3) != "GET") |
Fo170 | 0:36baf2ffa8eb | 130 | { |
Fo170 | 0:36baf2ffa8eb | 131 | // head, post or other method |
Fo170 | 0:36baf2ffa8eb | 132 | // for possible status codes see: |
Fo170 | 0:36baf2ffa8eb | 133 | // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html |
Fo170 | 0:36baf2ffa8eb | 134 | httpHeader = HTTP_OK; |
Fo170 | 0:36baf2ffa8eb | 135 | httpContent = "<h1>200 OK</h1>"; |
Fo170 | 0:36baf2ffa8eb | 136 | http_send(client, httpHeader, httpContent); |
Fo170 | 0:36baf2ffa8eb | 137 | continue; |
Fo170 | 0:36baf2ffa8eb | 138 | } |
Fo170 | 0:36baf2ffa8eb | 139 | |
Fo170 | 0:36baf2ffa8eb | 140 | if(received.substr(0, 6) == "GET / ") |
Fo170 | 0:36baf2ffa8eb | 141 | { |
Fo170 | 0:36baf2ffa8eb | 142 | httpHeader = HTTP_OK; |
Fo170 | 0:36baf2ffa8eb | 143 | // httpContent = "<p>Usage: http://host_or_ip/password</p>\r\n"; |
Fo170 | 0:36baf2ffa8eb | 144 | // http_send(client, httpHeader, httpContent); |
Fo170 | 0:36baf2ffa8eb | 145 | |
Fo170 | 0:36baf2ffa8eb | 146 | http_send(client, httpHeader, page(sw)); |
Fo170 | 0:36baf2ffa8eb | 147 | continue; |
Fo170 | 0:36baf2ffa8eb | 148 | } |
Fo170 | 0:36baf2ffa8eb | 149 | |
Fo170 | 0:36baf2ffa8eb | 150 | int cmd = analyse_get_url(received); |
Fo170 | 0:36baf2ffa8eb | 151 | |
Fo170 | 0:36baf2ffa8eb | 152 | if(cmd == -2) |
Fo170 | 0:36baf2ffa8eb | 153 | { |
Fo170 | 0:36baf2ffa8eb | 154 | // redirect to the right base url |
Fo170 | 0:36baf2ffa8eb | 155 | httpHeader = MOVED_PERM; |
Fo170 | 0:36baf2ffa8eb | 156 | http_send(client, httpHeader, moved_perm(1)); |
Fo170 | 0:36baf2ffa8eb | 157 | continue; |
Fo170 | 0:36baf2ffa8eb | 158 | } |
Fo170 | 0:36baf2ffa8eb | 159 | |
Fo170 | 0:36baf2ffa8eb | 160 | if(cmd == -1) |
Fo170 | 0:36baf2ffa8eb | 161 | { |
Fo170 | 0:36baf2ffa8eb | 162 | httpHeader = UNAUTHORIZED; |
Fo170 | 1:b52ad028c502 | 163 | httpContent = "<h1>401 Unauthorized "; |
Fo170 | 1:b52ad028c502 | 164 | httpContent = str_Unauthorized; |
Fo170 | 1:b52ad028c502 | 165 | httpContent += "</h1>\r\n"; |
Fo170 | 0:36baf2ffa8eb | 166 | |
Fo170 | 0:36baf2ffa8eb | 167 | http_send(client, httpHeader, httpContent); |
Fo170 | 0:36baf2ffa8eb | 168 | continue; |
Fo170 | 0:36baf2ffa8eb | 169 | } |
Fo170 | 0:36baf2ffa8eb | 170 | |
Fo170 | 0:36baf2ffa8eb | 171 | if(cmd == 1) |
Fo170 | 0:36baf2ffa8eb | 172 | { |
Fo170 | 0:36baf2ffa8eb | 173 | sw = 1; // switch on |
Fo170 | 0:36baf2ffa8eb | 174 | } |
Fo170 | 0:36baf2ffa8eb | 175 | |
Fo170 | 0:36baf2ffa8eb | 176 | if(cmd == 0) |
Fo170 | 0:36baf2ffa8eb | 177 | { |
Fo170 | 0:36baf2ffa8eb | 178 | sw = 0; // switch off |
Fo170 | 0:36baf2ffa8eb | 179 | } |
Fo170 | 0:36baf2ffa8eb | 180 | |
Fo170 | 0:36baf2ffa8eb | 181 | httpHeader = HTTP_OK; |
Fo170 | 0:36baf2ffa8eb | 182 | http_send(client, httpHeader, page_toggle_switch(sw)); |
Fo170 | 0:36baf2ffa8eb | 183 | } |
Fo170 | 0:36baf2ffa8eb | 184 | } |
Fo170 | 0:36baf2ffa8eb | 185 | |
Fo170 | 0:36baf2ffa8eb | 186 | } |
Fo170 | 0:36baf2ffa8eb | 187 | //----END WHILE----------- |
Fo170 | 0:36baf2ffa8eb | 188 | } |