Suga koubou / Mbed 2 deprecated HTTPD_sample

Dependencies:   EthernetInterface HTTPD mbed-rtos mbed mbed-rpc

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "EthernetInterface.h"
00004 #include "mbed_rpc.h"
00005 #include "HTTPD.h"
00006 
00007 EthernetInterface *eth;
00008 HTTPD *httpd;
00009 
00010 Serial pc(USBTX, USBRX);
00011 LocalFileSystem local("local");
00012 DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4);
00013 
00014 void callback_cgi (int id) {
00015     int i, n;
00016     char buf[256];
00017 
00018     strcpy(buf, httpd->getFilename(id));
00019     strcat(buf, "\r\n");
00020     strcat(buf, httpd->getQueryString(id));
00021     strcat(buf, "\r\n");
00022     n = strlen(buf);
00023 
00024     i = httpd->receive(id, &buf[n], sizeof(buf) - n);
00025     if (i < 0) return;
00026     i += n;
00027     buf[i] = 0;
00028 
00029     printf("CGI %d %s\r\n", id, buf);
00030     httpd->send(id, buf, i, "Content-Type: text/plain\r\n");
00031 }
00032 
00033 void callback_ws (int id) {
00034     int i;
00035     char buf[256];
00036 
00037     i = httpd->receive(id, buf, sizeof(buf));
00038     if (i < 0) return;
00039     buf[i] = 0;
00040 
00041     printf("WS %d %s\r\n", id, buf);
00042     httpd->sendWebsocket(id, buf, i);
00043 }
00044 
00045 void callback_rpc (int id) {
00046     char buf[40], outbuf[40];
00047 
00048     strcpy(buf, "/");
00049     httpd->urldecode(httpd->getFilename(id), &buf[1], sizeof(buf) - 2);
00050     RPC::call(buf, outbuf);
00051 
00052     printf("RPC id %d '%s' '%s'\r\n", id, buf, outbuf);
00053     httpd->send(id, outbuf, strlen(outbuf), "Content-Type: text/plain\r\n");
00054 }
00055 
00056 int main () {
00057 
00058     pc.baud(115200);
00059     printf("HTTP Server...\r\n");
00060 
00061     eth = new EthernetInterface;
00062 //    eth->init(); //Use DHCP
00063     eth->init("192.168.1.2", "255.255.255.0", "192.168.1.1");
00064     if (eth->connect()) return -1;
00065     printf("IP Address is %s\r\n", eth->getIPAddress());
00066 
00067 //    RPC::add_rpc_class<RpcAnalogIn>();
00068 //    RPC::add_rpc_class<RpcAnalogOut>();
00069     RPC::add_rpc_class<RpcDigitalIn>();
00070     RPC::add_rpc_class<RpcDigitalOut>();
00071     RPC::add_rpc_class<RpcDigitalInOut>();
00072     RPC::add_rpc_class<RpcPwmOut>();
00073 
00074     httpd = new HTTPD;
00075     httpd->attach("/cgi-bin/", &callback_cgi);
00076     httpd->attach("/ws/", &callback_ws);
00077     httpd->attach("/rpc/", &callback_rpc);
00078     httpd->attach("/", "/local/");
00079     httpd->start(80);
00080     printf("httpd ready\r\n");
00081     led1 = 1;
00082 
00083     for (;;) {
00084     }
00085 }