This is a simplest HTTP server made ​​of libMiMic. It will echo back a request path.

Dependencies:   libMiMic mbed-rtos mbed NySDFileSystem

最も簡単なhttpdです。 クライアントから送信されてきたURLをHtmlにして返します。

ModUrlモジュールのサンプルでもあります。

This program is simple httpserver.

This is tutorial for ModUrl module.

Files at this revision

API Documentation at this revision

Comitter:
nyatla
Date:
Thu May 16 16:07:17 2013 +0000
Parent:
4:4dc18f23661a
Child:
6:bcf3fe4d0ba1
Commit message:
initial version

Changed in this revision

libMiMic.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
registers.lib Show annotated file Show diff for this revision Revisions of this file
--- a/libMiMic.lib	Tue May 14 13:14:42 2013 +0000
+++ b/libMiMic.lib	Thu May 16 16:07:17 2013 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/nyatla/code/libMiMic/#00b8dee1538f
+http://mbed.org/users/nyatla/code/libMiMic/#f02596009a02
--- a/main.cpp	Tue May 14 13:14:42 2013 +0000
+++ b/main.cpp	Thu May 16 16:07:17 2013 +0000
@@ -1,37 +1,106 @@
 #include "mimic.h"
-
+#include "mbed.h"
+#include "InOut.h"
+LocalFileSystem2 lf("local");
 
 
-/**
- * MiMic RemoteMCU httpd.<br/>
- * <p>Service list</p>
- * <pre>
- * /rom/ - romfs
- * /setup/ - MiMic configulation REST API.
- * /local/ - mbed LocalFileSystem
- * /mvm/   - MiMicVM REST API
- * </pre>
- */
+class RedIoPin: public InOut
+{
+private:
+    int _v;
+public:
+    RedIoPin(char pin):InOut(pin,0)
+    {
+        //pull down
+        this->mode(1);//pull down
+        this->_v=0;
+        this->write(0);
+    }
+    /**
+     * set RedStoneValue RedStone value
+     * @return
+     * red stone value.
+     */
+    bool setRedValue(int v)
+    {
+        //read real pin status,direction=in
+        this->setDirection(false);
+        if(this->read()!=0)
+        {   //real pin==1:out=1,in=0
+            this->_v=1;
+            this->write(0);
+        }else{
+            //real pin==0: direction=out,out=redstone value
+            this->setDirection(true);
+            int  vt=v!=0?1:0;
+            this->write(vt);
+            this->_v=vt;
+        }
+        return this->_v;
+    }
+};
+class RedIo
+{
+public:
+    RedIoPin* io[30];
+    RedIo()
+    {
+        const char d[]={LED1,LED2,LED3,LED4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
+        for(int i=0;i<30;i++){
+            this->io[i]=new RedIoPin(d[i]);
+        }
+    }
+    virtual ~RedIo()
+    {
+        for(int i=0;i<30;i++){
+            delete this->io[i];
+        }
+    }
+    unsigned int update(unsigned int v)
+    {
+        unsigned int ret=0;
+        for(int i=0;i<30;i++){
+            ret=ret<<1;
+            ret=ret | (this->io[29-i]->setRedValue((v>>(29-i))&0x00000001)?1:0);
+        }
+        return ret;
+    }
+};
 
 class MiMicRemoteMcu:public MiMic::Httpd
 {
 private:
     ModUrl modurl; //basic URL parser
+    RedIo rsio;
 public:
     MiMicRemoteMcu():Httpd(80)
     {
     }
     virtual void onRequest(HttpdConnection& i_connection)
     {
-        char url[32];
+        char url[64];
         int method;
+        
         //call ModUrl module.
-        if(this->modurl.execute(i_connection,url,32,&method)){
-            //send 200 OK and requested URL
-            i_connection.sendHeader(200,"text/html",NULL);
-            i_connection.sendBodyF("<html><body>Your Request path is %s.</body></html>",url);
+        if(!this->modurl.execute(i_connection,url,64,&method)){
+            i_connection.sendHeader(400,"text/html",NULL);
+            i_connection.sendBodyF("<html><body>Bad Request.</body></html>",url);
             return;
         }
+        UrlReader r(url);
+        if(!r.isPathEqual("/rsb/")){
+            i_connection.sendHeader(403,"text/html",NULL);
+            i_connection.sendBodyF("<html><body>Path must be '/rsb/?p=[:unsigned int:]'</body></html>",url);
+            return;
+        }
+        unsigned int rsv;
+        if(!r.getQueryUInt("p",rsv)){
+            i_connection.sendHeader(400,"text/html",NULL);
+            i_connection.sendBodyF("<html><body>p val must be unsigned int</body></html>",url);
+            return;
+        }
+        i_connection.sendHeader(200,"text/html",NULL);
+        i_connection.sendBodyF("%u",rsio.update(rsv));
         return;
     }
 };
@@ -39,6 +108,9 @@
 int main()
 {
     NetConfig cfg; //create network configulation
+    //try to override setting by local file.
+    cfg.loadFromFile("/local/mimic.cfg");
+
     Net net(cfg);  //create a net instance.
     MiMicRemoteMcu httpd; //create a httpd instance.
     httpd.loop();  //start httpd loop.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/registers.lib	Thu May 16 16:07:17 2013 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/elevatorguy/code/registers/#276fb0fe230c