ported HTTP-Server with W5500 Ethernet Shield

Dependencies:   W5500Interface mbed-rpc mbed

Fork of HTTP-Server by F B

Committer:
embeddist
Date:
Tue Apr 28 13:54:18 2015 +0000
Revision:
11:ac3569846176
Parent:
10:8b4c3d605bf0
ported HTTP-Server with W5500 Ethernet Shield

Who changed what in which revision?

UserRevisionLine numberNew contents of line
feb11 0:9e4bcb10b3e3 1 #include "Formatter.h"
feb11 0:9e4bcb10b3e3 2 #include "mbed.h"
feb11 0:9e4bcb10b3e3 3 #include "RPCObjectManager.h"
feb11 0:9e4bcb10b3e3 4 #include "EthernetInterface.h"
feb11 0:9e4bcb10b3e3 5
feb11 0:9e4bcb10b3e3 6 const char *SIMPLE_HTML_CODE = "\
feb11 0:9e4bcb10b3e3 7 <!DOCTYPE html>\
feb11 0:9e4bcb10b3e3 8 <html>\
feb11 4:624527ebc0fa 9 <head>\
feb11 4:624527ebc0fa 10 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\
feb11 4:624527ebc0fa 11 <title>TCP Server</title>\
feb11 4:624527ebc0fa 12 </head>\
feb11 4:624527ebc0fa 13 <body>";
feb11 0:9e4bcb10b3e3 14
feb11 0:9e4bcb10b3e3 15
feb11 0:9e4bcb10b3e3 16 const char* INTERACTIVE_HTML_CODE_1 = "\
feb11 4:624527ebc0fa 17 <!DOCTYPE html> \
feb11 0:9e4bcb10b3e3 18 <html>\
feb11 4:624527ebc0fa 19 <head>\
feb11 4:624527ebc0fa 20 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\
feb11 4:624527ebc0fa 21 <title>TCP Server</title>\
feb11 4:624527ebc0fa 22 <script type=\"text/javascript\">\
feb11 4:624527ebc0fa 23 var ip = \"%s\";\
feb11 4:624527ebc0fa 24 function submitCreateForm()\
feb11 4:624527ebc0fa 25 {\
feb11 4:624527ebc0fa 26 var list = document.getElementById(\"type\");\
feb11 4:624527ebc0fa 27 var type = list.options[list.selectedIndex].value;\
feb11 4:624527ebc0fa 28 var name = document.getElementById(\"name\").value;\
feb11 10:8b4c3d605bf0 29 if(name === \"\") \
feb11 10:8b4c3d605bf0 30 return;\
feb11 4:624527ebc0fa 31 var arg = document.getElementById(\"arg\").value;\
feb11 4:624527ebc0fa 32 var url;\
feb11 6:d03e189ebbfe 33 if(arg === \"\") url = \"http://\" + ip + type + \"new?name=\" + name;\
feb11 6:d03e189ebbfe 34 else url = \"http://\" + ip + type + \"new?arg=\" + arg + \"&name=\" + name;\
feb11 4:624527ebc0fa 35 location.href= url;\
feb11 4:624527ebc0fa 36 }\
feb11 4:624527ebc0fa 37 function submitCallFuncForm()\
feb11 4:624527ebc0fa 38 {\
feb11 4:624527ebc0fa 39 var command = document.getElementById(\"command\").value;\
feb11 10:8b4c3d605bf0 40 if(command === \"\") \
feb11 10:8b4c3d605bf0 41 return; \
feb11 4:624527ebc0fa 42 var tmp = command.split(\' \');\
feb11 4:624527ebc0fa 43 var url = tmp[0];\
feb11 4:624527ebc0fa 44 if(tmp.length > 1)\
feb11 4:624527ebc0fa 45 url += \"?\";\
feb11 4:624527ebc0fa 46 for(var i = 1; i < tmp.length; ++i)\
feb11 4:624527ebc0fa 47 {\
feb11 4:624527ebc0fa 48 url += \"arg\" + i + \"=\" + tmp[i];\
feb11 4:624527ebc0fa 49 if(i+1 < tmp.length)\
feb11 4:624527ebc0fa 50 url += \"&\";\
feb11 4:624527ebc0fa 51 }\
feb11 4:624527ebc0fa 52 location.href = url;\
feb11 4:624527ebc0fa 53 }\
feb11 4:624527ebc0fa 54 </script>\
feb11 4:624527ebc0fa 55 </head> \
feb11 0:9e4bcb10b3e3 56 <body>";
feb11 0:9e4bcb10b3e3 57
feb11 0:9e4bcb10b3e3 58 const char* INTERACTIVE_HTML_CODE_2 = "<h3>Create Object :</h3>\
feb11 9:a9bf63017854 59 <form>\
feb11 4:624527ebc0fa 60 Type: <select id=\"type\">\
feb11 4:624527ebc0fa 61 <option value=\"/DigitalOut/\">DigitalOut</option>\
feb11 4:624527ebc0fa 62 <option value=\"/DigitalIn/\">DigitalIn</option>\
feb11 4:624527ebc0fa 63 <option value=\"/DigitalInOut/\">DigitalInOut</option>\
feb11 4:624527ebc0fa 64 <option value=\"/PwmOut/\">PwmOut</option>\
feb11 4:624527ebc0fa 65 <option value=\"/Timer/\">Timer</option>\
feb11 4:624527ebc0fa 66 </select><br>\
feb11 4:624527ebc0fa 67 name: <input type=\"text\" id=\"name\"><br>\
feb11 4:624527ebc0fa 68 arg(optional): <input type=\"text\" id=\"arg\">\
feb11 4:624527ebc0fa 69 <p><input type=\"button\" value=\"Create\" onclick=\"javascript:submitCreateForm();\"></p>\
feb11 4:624527ebc0fa 70 </form> \
feb11 4:624527ebc0fa 71 \
feb11 4:624527ebc0fa 72 <h3>Call a function :</h3>\
feb11 4:624527ebc0fa 73 <p>Enter an RPC command.</p>\
feb11 9:a9bf63017854 74 <form>\
feb11 10:8b4c3d605bf0 75 Command: <input type= \"text\" id=\"command\" maxlength=127><br>\
feb11 9:a9bf63017854 76 <p><input type=\"button\" value=\"Send\" onclick=\"javascript:submitCallFuncForm();\"></p><br>\
feb11 4:624527ebc0fa 77 </form>\
feb11 4:624527ebc0fa 78 </body> \
feb11 0:9e4bcb10b3e3 79 </html>";
feb11 0:9e4bcb10b3e3 80
feb11 0:9e4bcb10b3e3 81 static char chunk[1024];
feb11 0:9e4bcb10b3e3 82
feb11 0:9e4bcb10b3e3 83 Formatter::Formatter(int nb):
feb11 0:9e4bcb10b3e3 84 currentChunk(0),
feb11 0:9e4bcb10b3e3 85 nbChunk(nb)
feb11 0:9e4bcb10b3e3 86 {
feb11 0:9e4bcb10b3e3 87 }
feb11 0:9e4bcb10b3e3 88
feb11 0:9e4bcb10b3e3 89 char* Formatter::get_page(char *reply)
feb11 0:9e4bcb10b3e3 90 {
feb11 0:9e4bcb10b3e3 91 chunk[0] = '\0';
feb11 0:9e4bcb10b3e3 92
feb11 0:9e4bcb10b3e3 93 if(currentChunk < nbChunk)
feb11 0:9e4bcb10b3e3 94 {
feb11 0:9e4bcb10b3e3 95 get_chunk(currentChunk, reply);
feb11 0:9e4bcb10b3e3 96 currentChunk++;
feb11 0:9e4bcb10b3e3 97 }
feb11 0:9e4bcb10b3e3 98 else
feb11 0:9e4bcb10b3e3 99 currentChunk = 0;
feb11 0:9e4bcb10b3e3 100
feb11 0:9e4bcb10b3e3 101 return chunk;
feb11 0:9e4bcb10b3e3 102 }
feb11 0:9e4bcb10b3e3 103
feb11 0:9e4bcb10b3e3 104 void Formatter::get_chunk(const int c, char *reply)
feb11 0:9e4bcb10b3e3 105 {
feb11 0:9e4bcb10b3e3 106 strcat(chunk, reply);
feb11 0:9e4bcb10b3e3 107 }
feb11 0:9e4bcb10b3e3 108
feb11 0:9e4bcb10b3e3 109 SimpleHTMLFormatter::SimpleHTMLFormatter():
feb11 0:9e4bcb10b3e3 110 Formatter()
feb11 0:9e4bcb10b3e3 111 {
feb11 0:9e4bcb10b3e3 112 }
feb11 0:9e4bcb10b3e3 113
feb11 0:9e4bcb10b3e3 114 void SimpleHTMLFormatter::get_chunk(const int c, char* reply)
feb11 0:9e4bcb10b3e3 115 {
feb11 0:9e4bcb10b3e3 116 strcat(chunk, SIMPLE_HTML_CODE);
feb11 0:9e4bcb10b3e3 117
feb11 0:9e4bcb10b3e3 118 if(reply != NULL && strlen(reply) != 0)
feb11 0:9e4bcb10b3e3 119 {
feb11 0:9e4bcb10b3e3 120 strcat(chunk, "RPC reply : ");
feb11 0:9e4bcb10b3e3 121 strcat(chunk, reply);
feb11 0:9e4bcb10b3e3 122 }
feb11 0:9e4bcb10b3e3 123
feb11 0:9e4bcb10b3e3 124 if(!RPCObjectManager::instance().is_empty())
feb11 0:9e4bcb10b3e3 125 {
feb11 0:9e4bcb10b3e3 126 strcat(chunk, "<ul>");
feb11 0:9e4bcb10b3e3 127 for(std::list<char*>::iterator itor = RPCObjectManager::instance().begin();
feb11 0:9e4bcb10b3e3 128 itor != RPCObjectManager::instance().end();
feb11 0:9e4bcb10b3e3 129 ++itor)
feb11 0:9e4bcb10b3e3 130 {
feb11 0:9e4bcb10b3e3 131 strcat(chunk, "<li>");
feb11 0:9e4bcb10b3e3 132 strcat(chunk, *itor);
feb11 0:9e4bcb10b3e3 133 strcat(chunk, "</li>");
feb11 0:9e4bcb10b3e3 134 }
feb11 0:9e4bcb10b3e3 135 strcat(chunk, "</ul>");
feb11 0:9e4bcb10b3e3 136 }
feb11 0:9e4bcb10b3e3 137
feb11 0:9e4bcb10b3e3 138 strcat(chunk, "</body></html>");
feb11 0:9e4bcb10b3e3 139 }
feb11 0:9e4bcb10b3e3 140
feb11 0:9e4bcb10b3e3 141 InteractiveHTMLFormatter::InteractiveHTMLFormatter():
feb11 0:9e4bcb10b3e3 142 Formatter(3)
feb11 0:9e4bcb10b3e3 143 {
feb11 0:9e4bcb10b3e3 144 }
embeddist 11:ac3569846176 145 extern char ipaddr_backup[17];
feb11 0:9e4bcb10b3e3 146
feb11 0:9e4bcb10b3e3 147 void InteractiveHTMLFormatter::get_chunk(const int c, char *reply)
feb11 0:9e4bcb10b3e3 148 {
feb11 0:9e4bcb10b3e3 149 if(c == 0)
embeddist 11:ac3569846176 150 {
embeddist 11:ac3569846176 151 //srintf(chunk, INTERACTIVE_HTML_CODE_1, "192.168.77.34");
embeddist 11:ac3569846176 152 sprintf(chunk, INTERACTIVE_HTML_CODE_1, ipaddr_backup);
embeddist 11:ac3569846176 153 //sprintf(chunk, INTERACTIVE_HTML_CODE_1, EthernetInterface::getIPAddress());
embeddist 11:ac3569846176 154 }
feb11 0:9e4bcb10b3e3 155 else if(c == 1)
feb11 0:9e4bcb10b3e3 156 {
feb11 0:9e4bcb10b3e3 157 if(reply != NULL && strlen(reply) != 0)
feb11 0:9e4bcb10b3e3 158 {
feb11 0:9e4bcb10b3e3 159 strcat(chunk, "RPC reply : ");
feb11 0:9e4bcb10b3e3 160 strcat(chunk, reply);
feb11 0:9e4bcb10b3e3 161 }
feb11 0:9e4bcb10b3e3 162 if(!RPCObjectManager::instance().is_empty())
feb11 0:9e4bcb10b3e3 163 {
feb11 0:9e4bcb10b3e3 164 strcat(chunk, "<p>Objects created :</p>");
feb11 0:9e4bcb10b3e3 165
feb11 0:9e4bcb10b3e3 166 strcat(chunk, "<ul>");
feb11 0:9e4bcb10b3e3 167 for(std::list<char*>::iterator itor = RPCObjectManager::instance().begin();
feb11 0:9e4bcb10b3e3 168 itor != RPCObjectManager::instance().end();
feb11 0:9e4bcb10b3e3 169 ++itor)
feb11 0:9e4bcb10b3e3 170 {
feb11 0:9e4bcb10b3e3 171 strcat(chunk, "<li>");
feb11 0:9e4bcb10b3e3 172 strcat(chunk, *itor);
feb11 0:9e4bcb10b3e3 173 strcat(chunk, " (<a href=\"http://");
embeddist 11:ac3569846176 174 //strcat(chunk, EthernetInterface::getIPAddress());
embeddist 11:ac3569846176 175 strcat(chunk, "192.168.77.34");
feb11 0:9e4bcb10b3e3 176 strcat(chunk, "/");
feb11 0:9e4bcb10b3e3 177 strcat(chunk, *itor);
feb11 0:9e4bcb10b3e3 178 strcat(chunk, "/delete\">delete</a>)");
feb11 0:9e4bcb10b3e3 179 strcat(chunk, "</li>");
feb11 0:9e4bcb10b3e3 180 }
feb11 0:9e4bcb10b3e3 181 strcat(chunk, "</ul>");
feb11 0:9e4bcb10b3e3 182 }
feb11 0:9e4bcb10b3e3 183 strcat(chunk, " ");
feb11 0:9e4bcb10b3e3 184 }
feb11 0:9e4bcb10b3e3 185 else if(c == 2)
feb11 0:9e4bcb10b3e3 186 strcat(chunk, INTERACTIVE_HTML_CODE_2);
feb11 0:9e4bcb10b3e3 187 }
feb11 0:9e4bcb10b3e3 188
feb11 0:9e4bcb10b3e3 189