Delta / Mbed 2 deprecated NNN40_APmodeToSTAmodeByHTTPServer

Dependencies:   WIFI_API_32kRAM mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Formatter.cpp Source File

Formatter.cpp

00001 #include "Formatter.h"
00002 #include "mbed.h"
00003 #include "RPCObjectManager.h"
00004 #include "EthernetInterface.h"
00005 
00006 const char *SIMPLE_HTML_CODE = "\
00007 <!DOCTYPE html>\
00008 <html>\
00009 <head>\
00010 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\
00011 <title>TCP Server</title>\
00012 </head>\
00013  <body>";
00014 
00015 
00016 const char* INTERACTIVE_HTML_CODE_1 = "\
00017 <!DOCTYPE html> \
00018 <html>\
00019 <head>\
00020 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\
00021 <title>TCP Server</title>\
00022 <script type=\"text/javascript\">\
00023 var ip = \"%s\";\
00024 function submitCreateForm()\
00025 {\
00026 var list = document.getElementById(\"type\");\
00027 var type = list.options[list.selectedIndex].value;\
00028 var name = document.getElementById(\"name\").value;\
00029 if(name === \"\") \
00030 return;\
00031 var arg = document.getElementById(\"arg\").value;\
00032 var url;\
00033 if(arg === \"\") url = \"http://\" + ip + type + \"new?name=\" + name;\
00034 else url = \"http://\" + ip + type + \"new?arg=\" + arg + \"&name=\" + name;\
00035 location.href= url;\
00036 }\
00037 function submitCallFuncForm()\
00038 {\
00039 var command = document.getElementById(\"command\").value;\
00040 if(command === \"\") \
00041 return; \
00042 var tmp = command.split(\' \');\
00043 var url = tmp[0];\
00044 if(tmp.length > 1)\
00045 url += \"?\";\
00046 for(var i = 1; i < tmp.length; ++i)\
00047 {\
00048 url += \"arg\" + i + \"=\" + tmp[i];\
00049 if(i+1 < tmp.length)\
00050 url += \"&\";\
00051 }\
00052 location.href = url;\
00053 }\
00054 </script>\
00055 </head> \
00056 <body>";
00057 
00058 const char* INTERACTIVE_HTML_CODE_2 = "<h3>Create Object :</h3>\
00059 <form>\
00060 Type: <select id=\"type\">\
00061 <option value=\"/DigitalOut/\">DigitalOut</option>\
00062 <option value=\"/DigitalIn/\">DigitalIn</option>\
00063 <option value=\"/DigitalInOut/\">DigitalInOut</option>\
00064 <option value=\"/PwmOut/\">PwmOut</option>\
00065 <option value=\"/Timer/\">Timer</option>\
00066 </select><br>\
00067 name: <input type=\"text\" id=\"name\"><br>\
00068 arg(optional): <input type=\"text\" id=\"arg\">\
00069 <p><input type=\"button\" value=\"Create\" onclick=\"javascript:submitCreateForm();\"></p>\
00070 </form> \
00071  \
00072 <h3>Call a function :</h3>\
00073 <p>Enter an RPC command.</p>\
00074 <form>\
00075 Command: <input type= \"text\" id=\"command\" maxlength=127><br>\
00076 <p><input type=\"button\" value=\"Send\" onclick=\"javascript:submitCallFuncForm();\"></p><br>\
00077 </form>\
00078 </body> \
00079 </html>";
00080 
00081 const char* DELRA_WIFI_SETTING_HTML_CODE_0 = "<!DOCTYPE html> \
00082 <html>\
00083 <head>\
00084 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\
00085 <title>NNN40 Wifi Module Setting</title>\
00086 <style>\
00087 #background {\
00088 background: -moz-linear-gradient(top,#ffffff,#0099FF);\
00089 border-width: 3px;\
00090 border-style: ridge;\
00091 width: 500px;\
00092 height: 700px;\
00093 border-color: #000055;\
00094 padding: 20px\
00095 }#title {color: #444444;}#input {margin: 10px;}\
00096 </style><script type=\"application/javascript\">\
00097 function reset() {\
00098 document.getElementById(\"defaultchs\").checked = true;\
00099 document.getElementById(\"ssid\").value = \"\";\
00100 document.getElementById(\"password\").value = \"\";\
00101 }function send() {var ip = \"192.168.2.1/WebSetting/\";\
00102 var ssid = document.getElementById(\"ssid\").value;\
00103 var pwd = document.getElementById(\"password\").value;\
00104 location.href = \"http://\"+ip+ssid +\"/\"+pwd;}\  
00105 </script></head><body>";
00106 
00107         const char* DELRA_WIFI_SETTING_HTML_CODE_1 = "<div id=\"background\">\
00108 <div align=\"center\">\
00109 <img src=\"https://upload.wikimedia.org/wikipedia/commons/8/8c/DELTA_Electronics_Logo.png\" width=\"354\" height=\"109\";>\
00110 </div><h1 id=\"title\"align=\"center\">WIFI SETTING</h1>\
00111 <HR align=\"center\" width=\"90%\"><form id=\"input\" method=\"get\">\
00112 <font size=\"6\" class=\"font\">Security:</font>\
00113 <br><input id=\"defaultchs\" type=\"radio\" name=\"security\" value=\"NONE\" checked>\
00114 NONE<input type=\"radio\" name=\"security\" value=\"WEP\">\
00115 WEP<input type=\"radio\" name=\"security\" value=\"WPA\">\
00116 WPA<input type=\"radio\" name=\"security\" value=\"WPA2\">\
00117 WPA2<br> <br><font size=\"6\" class=\"font\">SSID:</font>\
00118 <br><input id=\"ssid\" size=\"20\"  type=\"text\" name=\"ssid\">\
00119 <br><br><font size=\"6\" class=\"font\">Password:</font>\
00120 <br><input type=\"text\" id=\"password\" name=\"pwd\">\
00121 <div align=\"center\"><input class=\"font\" type=\"button\" style=\"width: 160px; height: 80px; margin: 50px 40px 0px 0px; font-size: 25px\" value=\"Confirm\" onclick=\"send()\" />\
00122 <input class=\"font\" type=\"button\" style=\"width: 160px; height: 80px; margin: 50px 40px 0px 0px; font-size: 25px\" value=\"Reset\" onclick=\"reset()\" />\
00123 </div></form></div></body></html>" ;
00124 
00125 static char chunk[1024];
00126 
00127 Formatter::Formatter(int nb):
00128     currentChunk(0),
00129     nbChunk(nb)
00130 {
00131 }
00132 
00133 char* Formatter::get_page(char *reply)
00134 {
00135     chunk[0] = '\0';
00136 
00137     if(currentChunk < nbChunk) {
00138         get_chunk(currentChunk, reply);
00139         currentChunk++;
00140     } else
00141         currentChunk = 0;
00142 
00143     return chunk;
00144 }
00145 
00146 void Formatter::get_chunk(const int c, char *reply)
00147 {
00148     strcat(chunk, reply);
00149 }
00150 
00151 SimpleHTMLFormatter::SimpleHTMLFormatter():
00152     Formatter()
00153 {
00154 }
00155 
00156 void SimpleHTMLFormatter::get_chunk(const int c, char* reply)
00157 {
00158     strcat(chunk, SIMPLE_HTML_CODE);
00159 
00160     if(reply != NULL && strlen(reply) != 0) {
00161         strcat(chunk, "RPC reply : ");
00162         strcat(chunk, reply);
00163     }
00164 
00165     if(!RPCObjectManager::instance().is_empty()) {
00166         strcat(chunk, "<ul>");
00167         for(std::list<char*>::iterator itor = RPCObjectManager::instance().begin();
00168                 itor != RPCObjectManager::instance().end();
00169                 ++itor) {
00170             strcat(chunk, "<li>");
00171             strcat(chunk, *itor);
00172             strcat(chunk, "</li>");
00173         }
00174         strcat(chunk, "</ul>");
00175     }
00176     strcat(chunk, "</body></html>");
00177 }
00178 
00179 InteractiveHTMLFormatter::InteractiveHTMLFormatter():
00180     Formatter(3)
00181 {
00182 }
00183 
00184 void InteractiveHTMLFormatter::get_chunk(const int c, char *reply)
00185 {
00186     if(c == 0)
00187         sprintf(chunk, INTERACTIVE_HTML_CODE_1, EthernetInterface::getIPAddress());
00188 
00189     else if(c == 1) {
00190         if(reply != NULL && strlen(reply) != 0) {
00191             strcat(chunk, "RPC reply : ");
00192             strcat(chunk, reply);
00193         }
00194         if(!RPCObjectManager::instance().is_empty()) {
00195             strcat(chunk, "<p>Objects created :</p>");
00196 
00197             strcat(chunk, "<ul>");
00198             for(std::list<char*>::iterator itor = RPCObjectManager::instance().begin();
00199                     itor != RPCObjectManager::instance().end();
00200                     ++itor) {
00201                 strcat(chunk, "<li>");
00202                 strcat(chunk, *itor);
00203                 strcat(chunk, " (<a href=\"http://");
00204                 strcat(chunk, EthernetInterface::getIPAddress());
00205                 strcat(chunk, "/");
00206                 strcat(chunk, *itor);
00207                 strcat(chunk, "/delete\">delete</a>)");
00208                 strcat(chunk, "</li>");
00209             }
00210             strcat(chunk, "</ul>");
00211         }
00212         strcat(chunk, " ");
00213     } else if(c == 2)
00214         strcat(chunk, INTERACTIVE_HTML_CODE_2);
00215 }
00216 
00217 DeltaWifiSettingHTMLFormatter::DeltaWifiSettingHTMLFormatter():
00218     Formatter(2)
00219 {
00220 }
00221 void DeltaWifiSettingHTMLFormatter::get_chunk(const int c, char *reply)
00222 {
00223     if(c == 0)
00224         strcat(chunk, DELRA_WIFI_SETTING_HTML_CODE_0);
00225 
00226 
00227     else if(c == 1) {
00228         strcat(chunk,DELRA_WIFI_SETTING_HTML_CODE_1);
00229 
00230     }
00231 }