ban4jp - / Mbed 2 deprecated TinyHTTPServer_WIZ820io

Dependencies:   Tiny-HTTPD WIZ820ioInterface mbed-rpc mbed

Fork of HTTPD_sample by Suga koubou

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "WIZ820ioInterface.h"
00003 #include "mbed_rpc.h"
00004 #include "HTTPD.h"
00005 
00006 SPI *spi;
00007 WIZ820ioInterface *eth;
00008 HTTPD *httpd;
00009 
00010 #if defined(TARGET_LPC1768)
00011 Serial pc(USBTX, USBRX);
00012 LocalFileSystem local("local");
00013 DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4);
00014 
00015 #elif defined(TARGET_LPC1114)
00016 Serial pc(dp16,dp15);
00017 
00018 #endif
00019 
00020 const char* const index_page =
00021     "<html>"
00022     "<body>"
00023     "<h1>Tiny HTTP Server</h1>"
00024     "<h2>RPC</h2>"
00025     "<a href=\"/rpc/DigitalOut/new%20LED1%20myled\">/rpc/DigitalOut/new LED1 myled</a><br><br>"
00026     "<a href=\"/rpc/myled/write%200\">/rpc/myled/write 0</a><br><br>"
00027     "<a href=\"/rpc/myled/write%201\">/rpc/myled/write 1</a><br><br>"
00028     "</body>"
00029     "</html>";
00030 
00031 void callback_static (int id) {
00032     char buf[256];
00033 
00034     strcpy(buf, httpd->getFilename(id));
00035     printf("static %d %s\r\n", id, buf);
00036 
00037     if (strcmp(buf, "") == 0 || strcmp(buf, "index.html") == 0) {
00038         httpd->send(id, index_page, strlen(index_page), "Content-Type: text/html\r\n");
00039     } else {
00040         httpd->httpdError(id, 404);
00041     }
00042 }
00043 
00044 void callback_cgi (int id) {
00045     int i, n;
00046     char buf[256];
00047 
00048     strcpy(buf, httpd->getFilename(id));
00049     strcat(buf, "\r\n");
00050     strcat(buf, httpd->getQueryString(id));
00051     strcat(buf, "\r\n");
00052     n = strlen(buf);
00053 
00054     i = httpd->receive(id, &buf[n], sizeof(buf) - n);
00055     if (i < 0) return;
00056     i += n;
00057     buf[i] = 0;
00058 
00059     printf("CGI %d %s\r\n", id, buf);
00060     httpd->send(id, buf, i, "Content-Type: text/plain\r\n");
00061 }
00062 
00063 void callback_ws (int id) {
00064     int i;
00065     char buf[256];
00066 
00067     i = httpd->receive(id, buf, sizeof(buf));
00068     if (i < 0) return;
00069     buf[i] = 0;
00070 
00071     printf("WS %d %s\r\n", id, buf);
00072     httpd->sendWebsocket(id, buf, i);
00073 }
00074 
00075 void callback_rpc (int id) {
00076     char buf[40], outbuf[40];
00077 
00078     strcpy(buf, "/");
00079     httpd->urldecode(httpd->getFilename(id), &buf[1], sizeof(buf) - 2);
00080     RPC::call(buf, outbuf);
00081 
00082     printf("RPC id %d '%s' '%s'\r\n", id, buf, outbuf);
00083     httpd->send(id, outbuf, strlen(outbuf), "Content-Type: text/plain\r\n");
00084 }
00085 
00086 int main () {
00087     int ret;
00088 
00089     //pc.baud(115200);
00090     pc.baud(9600);
00091     printf("HTTP Server...\r\n");
00092 
00093 #if defined(TARGET_LPC1768)
00094     spi = new SPI(p5, p6, p7); // mosi, miso, sclk
00095     spi->frequency(4000000); // 4MHz
00096     eth = new WIZ820ioInterface(spi, p21, p22); // spi, cs, reset
00097 #elif defined(TARGET_LPC1114)
00098     spi = new SPI(dp2, dp1, dp6); // mosi, miso, sclk
00099     spi->frequency(4000000); // 4MHz
00100     eth = new WIZ820ioInterface(spi, dp25, dp26); // spi, cs, reset
00101 #endif
00102 
00103     eth->init(); //Use DHCP
00104 //    eth->init("192.168.1.2", "255.255.255.0", "192.168.1.1");
00105 
00106     ret = eth->connect();
00107     if (ret) {
00108         printf("connect error %d", ret);
00109         return -1;
00110     }
00111     printf("IP Address is %s\r\n", eth->getIPAddress());
00112 
00113 //    RPC::add_rpc_class<RpcAnalogIn>();
00114 //    RPC::add_rpc_class<RpcAnalogOut>();
00115     RPC::add_rpc_class<RpcDigitalIn>();
00116     RPC::add_rpc_class<RpcDigitalOut>();
00117     RPC::add_rpc_class<RpcDigitalInOut>();
00118     RPC::add_rpc_class<RpcPwmOut>();
00119 
00120     httpd = new HTTPD;
00121     httpd->attach("/cgi-bin/", &callback_cgi);
00122     httpd->attach("/ws/", &callback_ws);
00123     httpd->attach("/rpc/", &callback_rpc);
00124 #if defined(TARGET_LPC1768)
00125     httpd->attach("/local/", "/local/");
00126 #endif
00127     httpd->attach("/", &callback_static);
00128     httpd->start(80);
00129     printf("httpd ready\r\n");
00130 #if defined(TARGET_LPC1768)
00131     led1 = 1;
00132 #endif
00133 
00134     for (;;) {
00135         httpd->poll();
00136         
00137         //printf("poll-loop\r\n");
00138     }
00139 }