This is Webservice SDK for mbed. LPCXpresso1769/LPC1768/FRDM-K64F/LPC4088

Dependents:   MbedFileServer_1768MiniDK2 RedWireBridge IssueDebug_gcc MiMicRemoteMCU-for-Mbed ... more

libMiMic(MiMic library for mbed)は、WebService機能を提供するSDKです。 mbedでWebAPIに対応したネットワークデバイスを簡単に作ることが出来ます。

libMiMicはMiMic projectで開発しています。MiMic projectについてはこちらをご覧ください。 http://nyatla.jp/mimic/wp/

構成

libMiMicはmbedRTOS上で動作し、ユーザアプリケーションにAPIを提供します。コアAPIはC言語で記述されていますが、使用頻度の高いものについてはmbed向けのC++APIが準備されています。

/media/uploads/nyatla/libmimic-sdk.png

※libMiMicはmbedの標準イーサネットドライバをしようしていません。

標準イーサネットドライバと同時に使用することはできません。

  • MiMicIP - IPv4スタックです。レテンシとメモリ消費量を抑えたuipベースのライブラリです。
  • ARP/ICMP/UDP/TCP - 基礎的なソケットAPIを提供します。APIは独自です。
  • HTTP/1.1 Server - HTTP/1.1に対応したサーバです。マルチセッション・Chunked・持続性接続に対応しています。
  • HTTP Modules - HTTP/1.1の機能モジュールです。以下のモジュールがあります。
    • ROM file provider - ROMに格納したファイルイメージを公開します。
    • File system provider - mbedファイルシステムを公開します。
    • Onchip configuration - プログラムフラッシュを利用して設定を保存します。
    • MiMicVM processor - RPCリクエスト(MiMicVM)を処理します。
    • FileUpload - ファイルアップロードを受け取ります。
    • URL decoder - HTTPリクエストを解析します。
    • UPnP handler -UPnPメッセージを処理します。
    • WebSocket - Websocketサーバです。
  • mDNS - マルチキャストDNSサービスです。
  • UPnP - UPnP/1.0の機能を提供します。UPnP handlerと協調して動作します。(現在はデバイス探索(SSDP)・デスクリプション(Description)のみ実装してあります。)
  • DHCP/APIPA - ゼロコンフィギュレーション用のモジュールです。
  • HTTP/1.1 Client
  • mbed C++ class library - mbed向けのC++CPIです。C言語のものより簡単です。

対応機種

  • mbed(mbed LPC1768)
  • LPCXpresso1769

プログラム

Import programMiMicRemoteMCU-for-Mbed

MiMic RemoteMCU for mbed. This program provides MCU control API over REST API. It can control MCU from Javascript,PHP or any HTTP rest client directly. And, The application has self development environment.

Import programMbedFileServer

The program publishes files at local directory and SD filesystem. It is a full-fledged webServer somewhat.

サンプル

Import programMiMicSimpleHttpd

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

Import programUPnPBasicDevice

Simplest UPnP basic device example. This program to run UPnP basic device on the mbed.

Import programWebSocketSample

MiMicSDK Websocket module sample program.

Import programHttpClientSamlpe

A http client sample program.

Import programTcpSocketClientSamlpe

MiMicSDK Tcp client socket sample program.

Import programUdpSocketSamlpe

Udp socket sample program. This program will send back the received packet.

チュートリアル

English

libMiMic(MiMic library for mbed) is SDK which provides Webservice functions. It can be created networking device easily using mbed.

See more MiMic information, See MiMic project website. http://nyatla.jp/mimic/wp/

Structure

libMiMic run on mbed RTOS and provides networking API to user application. This library has C++ class API for the mbed, and low-level C language API.

/media/uploads/nyatla/libmimic-sdk.png

For WebService general, it can be written in a simple C + + API.

libMiMic does not have the standard Ethernet driver of mbed. It is not possible that will be used with the standard Ethernet driver.

  • MiMicIP - IPv4 protocol stack. This is based uip which is reduced memory and latency.
  • ARP / ICMP / UDP / TCP - Those are provide basic IP protocols.
  • HTTP/1.1 Server - The Http server compatible HTTP/1.1. It supports multi-session, chunked transport, persistent connection.
  • HTTP Modules - There are addon-module for HTTP server. The following modules.
    • ROM file module - Publish the file images in ROM.
    • File system module - Publish thefiles in mbed file system.
    • Onchip configuration module - To save the network settings to the program flash via REST.
    • MiMicVM module - To handle the (MiMicVM) RPC request.
    • FileUpload module - Accept a file via HTTP POST.
    • URL dedoce module - A versatility URL decoder.
    • UPnP handle module - To handle UPnP messages.
    • UPnP - This provides UPnP/1.0 device functions. It works together with UPnP handler.
    • Websocket - websocket (version13) server
  • mDNS Service - DNS-SD protocol server.
  • UPnP - This provides UPnP/1.0 device functions which works with UPnP handler. (You have been implemented (SSDP) ? description only (Description) device search now.) It is a module zero configuration for - DHCP / APIPA. mbed C + + class library - C of mbed for + + is the CPI. It is simple than that of the C language.
  • DHCP/APIPA - It support zero-cpnfigulation.
  • mbed C++ class library. Almost APIs for Web applications are available.
  • HTTP/1.1 Client

Supported target

  • mbed(mbed LPC1768)
  • LPCXpresso1769

Application

Import programMiMicRemoteMCU-for-Mbed

MiMic RemoteMCU for mbed. This program provides MCU control API over REST API. It can control MCU from Javascript,PHP or any HTTP rest client directly. And, The application has self development environment.

Import programMbedFileServer

The program publishes files at local directory and SD filesystem. It is a full-fledged webServer somewhat.

Sample

Import programMiMicSimpleHttpd

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

Import programUPnPBasicDevice

Simplest UPnP basic device example. This program to run UPnP basic device on the mbed.

Import programWebSocketSample

MiMicSDK Websocket module sample program.

Import programHttpClientSamlpe

A http client sample program.

Import programTcpSocketClientSamlpe

MiMicSDK Tcp client socket sample program.

Import programUdpSocketSamlpe

Udp socket sample program. This program will send back the received packet.

Tutorial

Files at this revision

API Documentation at this revision

Comitter:
nyatla
Date:
Thu Jul 04 11:05:56 2013 +0000
Parent:
41:2389bd6b6f74
Child:
43:a182f2b5ff41
Commit message:
MiMic core r271;

Changed in this revision

core/NyLPC_cMiMicEnv.h Show annotated file Show diff for this revision Revisions of this file
core/http/NyLPC_cUrlEncode.c Show annotated file Show diff for this revision Revisions of this file
core/http/NyLPC_cUrlEncode.h Show annotated file Show diff for this revision Revisions of this file
core/net/httpd/NyLPC_cHttpd.h Show annotated file Show diff for this revision Revisions of this file
core/net/httpd/mod/NyLPC_cModFileIoBaseClass.c Show annotated file Show diff for this revision Revisions of this file
mbed/ModFileIo.cpp Show annotated file Show diff for this revision Revisions of this file
mbed/ModFileIo.h Show annotated file Show diff for this revision Revisions of this file
mbed/mimic.h Show annotated file Show diff for this revision Revisions of this file
--- a/core/NyLPC_cMiMicEnv.h	Sun Jun 30 14:17:42 2013 +0000
+++ b/core/NyLPC_cMiMicEnv.h	Thu Jul 04 11:05:56 2013 +0000
@@ -13,7 +13,7 @@
 #endif /* __cplusplus */
 
 
-#define NyLPC_cMiMicEnv_VERSION "MiMic/1.4.14"
+#define NyLPC_cMiMicEnv_VERSION "MiMic/1.4.20"
 
 
 #ifdef __cplusplus
--- a/core/http/NyLPC_cUrlEncode.c	Sun Jun 30 14:17:42 2013 +0000
+++ b/core/http/NyLPC_cUrlEncode.c	Thu Jul 04 11:05:56 2013 +0000
@@ -9,6 +9,7 @@
     return NyLPC_TBool_TRUE;
 }
 
+
 NyLPC_TcUrlEncode_ST NyLPC_cUrlEncode_decode(NyLPC_TcUrlEncode_t* i_inst,NyLPC_TChar c,NyLPC_TChar* out)
 {
     int t;
--- a/core/http/NyLPC_cUrlEncode.h	Sun Jun 30 14:17:42 2013 +0000
+++ b/core/http/NyLPC_cUrlEncode.h	Thu Jul 04 11:05:56 2013 +0000
@@ -31,6 +31,7 @@
 
 #define NyLPC_cUrlEncode_finalize(i_inst)
 
+#define NyLPC_cUrlEncode_reset(i_inst) (i_inst)->_len=0
 
 
 NyLPC_TcUrlEncode_ST NyLPC_cUrlEncode_decode(NyLPC_TcUrlEncode_t* i_inst,NyLPC_TChar c,NyLPC_TChar* out);
--- a/core/net/httpd/NyLPC_cHttpd.h	Sun Jun 30 14:17:42 2013 +0000
+++ b/core/net/httpd/NyLPC_cHttpd.h	Thu Jul 04 11:05:56 2013 +0000
@@ -17,7 +17,7 @@
 
 
 
-#define NyLPC_cNet_NUMBER_OF_THREAD 2
+#define NyLPC_cNet_NUMBER_OF_THREAD 3
 typedef void (*NyLPC_TcHttpd_onRequest)(NyLPC_TcHttpdConnection_t* i_inst);
 
 /**
--- a/core/net/httpd/mod/NyLPC_cModFileIoBaseClass.c	Sun Jun 30 14:17:42 2013 +0000
+++ b/core/net/httpd/mod/NyLPC_cModFileIoBaseClass.c	Thu Jul 04 11:05:56 2013 +0000
@@ -38,6 +38,7 @@
 struct TModFileIoHeader
 {
     struct NyLPC_THttpBasicHeader super;
+    NyLPC_TcUrlEncode_t urlencode;
     NyLPC_TUInt8 _content_id;
     //解析用
     NyLPC_TUInt8 _qery_name_id;
@@ -51,6 +52,7 @@
 
 
 
+
 static NyLPC_TBool messageHandler(NyLPC_TcHttpBasicHeaderParser_t* i_inst,const NyLPC_TChar* i_name,NyLPC_TChar i_c,struct NyLPC_THttpBasicHeader* o_out)
 {
     (void)i_inst;
@@ -95,6 +97,7 @@
 static NyLPC_TBool urlHandler(NyLPC_TcHttpBasicHeaderParser_t* i_inst,NyLPC_TChar i_c,struct NyLPC_THttpBasicHeader* o_out)
 {
     struct TModFileIoHeader* out=(struct TModFileIoHeader*)o_out;
+    NyLPC_TChar c;
     //読み飛ばし
     if(out->_prefix_len<0){
         out->_prefix_len++;
@@ -141,6 +144,7 @@
                 //クエリ値がある場合
                 switch(out->_qery_name_id){
                 case QNAME_ID_NAME:
+                    NyLPC_cUrlEncode_reset(&out->urlencode);
                     out->_astate=ST_PARSE_QUERY_VALUE_NAME;
                     break;
                 default:
@@ -159,7 +163,16 @@
             return NyLPC_TBool_TRUE;
         case ST_PARSE_QUERY_VALUE_NAME:
             if(i_c!='\0' && i_c!='&'){
-                if(!NyLPC_cStr_put(&(out->_tstr),i_c)){
+                //URLデコードしながら蓄積
+                switch(NyLPC_cUrlEncode_decode(&out->urlencode,i_c,&c)){
+                case NyLPC_TcUrlEncode_ST_NEXT:
+                    break;
+                case NyLPC_TcUrlEncode_ST_DONE:
+                    if(!NyLPC_cStr_put(&(out->_tstr),c)){
+                        NyLPC_OnErrorGoto(ERROR);
+                    }
+                    break;
+                default:
                     NyLPC_OnErrorGoto(ERROR);
                 }
                 return NyLPC_TBool_TRUE;
@@ -238,6 +251,7 @@
         single_header._prefix_len=-((NyLPC_TInt16)strlen(i_inst->super._ref_root_path)+2);
         single_header._astate=ST_PARSE_PATH;
         single_header.fname[0]='\0';
+        NyLPC_cUrlEncode_initialize(&single_header.urlencode);
         NyLPC_cStr_initialize(&single_header._tstr,single_header._tstr_buf,STRBUF_MAX);
 
         NyLPC_cHttpBasicHeaderParser_initialize(&sh.parser,&handler);
@@ -254,8 +268,10 @@
             NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500);
             NyLPC_OnErrorGoto(Error1);
         }
-        //HeaderParserはここで破棄
+        //HeaderParserはここで破棄(URLEncode,cSTRも)
         NyLPC_cHttpBasicHeaderParser_finalize(&sh.parser);
+        NyLPC_cUrlEncode_finalize(&single_header.urlencode);
+        NyLPC_cStr_finalize(&single_header._tstr);
     }
     //Connection Modeの設定 1.1 && !closeの場合はCONTINUE
     if(single_header.super.connection!=NyLPC_THttpMessgeHeader_Connection_CLOSE && single_header.super.startline.req.version==NyLPC_THttpVersion_11)
@@ -271,8 +287,9 @@
         {
         case CONTENT_ID_UPLOAD:
             //ファイル名とBodyParserを通知
-            if(method_type==NyLPC_THttpMethodType_POST || method_type==NyLPC_THttpMethodType_HEAD)
+            if(method_type==NyLPC_THttpMethodType_POST)
             {
+                NyLPC_cHttpdConnection_send100Continue(i_connection);
                 NyLPC_cHttpBodyParser_initialize(&sh.body_parser);
                 NyLPC_cHttpBodyParser_parseInit(&sh.body_parser,&single_header.super);
                 //ハンドラ内ではparseStreamのみ実行
@@ -314,20 +331,19 @@
             NyLPC_OnErrorGoto(Error2);
         }
     }
-    NyLPC_cStr_finalize(&single_header._tstr);
 //占有解除
     NyLPC_cHttpdConnection_unlock(i_connection);
     return NyLPC_TBool_TRUE;
 Error2_405:
     NyLPC_cHttpdUtils_sendErrorResponse(i_connection,405);
 Error2:
-    NyLPC_cStr_finalize(&single_header._tstr);
     //VM排他ロックの解除
     NyLPC_cHttpdConnection_unlock(i_connection);
     return NyLPC_TBool_FALSE;
 Error1:
     NyLPC_cHttpBasicHeaderParser_finalize(&parser);
     NyLPC_cStr_finalize(&single_header._tstr);
+    NyLPC_cUrlEncode_finalize(&single_header.urlencode);
     //VM排他ロックの解除
     NyLPC_cHttpdConnection_unlock(i_connection);
     return NyLPC_TBool_FALSE;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed/ModFileIo.cpp	Thu Jul 04 11:05:56 2013 +0000
@@ -0,0 +1,128 @@
+#include <ModFileIo.h>
+#include <stdio.h>
+#include "mbed.h"
+using namespace MiMic;
+/*
+ * FileIO class
+ */
+typedef struct TcModFileIoDumy
+{
+    NyLPC_TcModFileIoBaseClass_t super;
+}TcFileIoDumy_t;
+
+const static char* STR_TEXT_HTML="text/html";
+#define SIZE_OF_FBUF 256
+
+
+
+static NyLPC_TBool upload_handler(NyLPC_TcHttpdConnection_t* i_connection,const NyLPC_TChar* i_fname,NyLPC_TcHttpBodyParser_t* i_body_parser)
+{
+    char* buf=Httpd::_shared_buf;// This handler called with lock!
+
+    //save to new file
+    FILE *fp;
+    fp = fopen(i_fname, "w");
+    if(fp==NULL){
+        NyLPC_cHttpdConnection_sendResponseHeader2(i_connection,500,STR_TEXT_HTML,0,NULL);
+        return NyLPC_TBool_FALSE;
+    }
+    NyLPC_TInt16 l;
+    if(NyLPC_cHttpBodyParser_parseStream(i_body_parser,NyLPC_cHttpdConnection_refStream(i_connection),buf,SIZE_OF_FBUF,&l)){
+        while(l>0){
+            fwrite(buf,1,l,fp);            
+            if(!NyLPC_cHttpBodyParser_parseStream(i_body_parser,NyLPC_cHttpdConnection_refStream(i_connection),buf,SIZE_OF_FBUF,&l)){
+                NyLPC_OnErrorGoto(Error_FILE);
+            }
+        }
+    }
+    fclose(fp);    
+    //write response
+    NyLPC_cHttpdConnection_sendResponseHeader2(i_connection,200,STR_TEXT_HTML,0,NULL);
+    return NyLPC_TBool_TRUE;
+Error_FILE:
+    fclose(fp);
+    NyLPC_cHttpdConnection_sendResponseHeader2(i_connection,500,STR_TEXT_HTML,0,NULL);
+    return NyLPC_TBool_FALSE;
+}
+
+static NyLPC_TBool create_handler(NyLPC_TcHttpdConnection_t* i_connection,const NyLPC_TChar* i_fname)
+{
+    FILE *fp; 
+    fp = fopen(i_fname, "w");
+    if(fp!=NULL){
+        fclose(fp);
+        NyLPC_cHttpdConnection_sendResponseHeader2(i_connection,200,STR_TEXT_HTML,0,NULL);
+        return NyLPC_TBool_TRUE;
+    }
+    NyLPC_cHttpdConnection_sendResponseHeader2(i_connection,500,STR_TEXT_HTML,0,NULL);
+    return NyLPC_TBool_FALSE;
+}
+
+static NyLPC_TBool remove_handler(NyLPC_TcHttpdConnection_t* i_connection,const NyLPC_TChar* i_fname)
+{
+    if(remove(i_fname)==0){
+        NyLPC_cHttpdConnection_sendResponseHeader2(i_connection,200,STR_TEXT_HTML,0,NULL);
+        return NyLPC_TBool_TRUE;
+    }
+    //500ERROR
+    NyLPC_cHttpdConnection_sendResponseHeader2(i_connection,500,STR_TEXT_HTML,0,NULL);
+    return NyLPC_TBool_FALSE;
+}
+
+
+
+static void cModFileIoDumy_initialize(TcFileIoDumy_t* i_inst,const char* i_root_path)
+{
+    NyLPC_cModFileIoBaseClass_initialize(&(i_inst->super),i_root_path);
+    i_inst->super._abstruct_function.upload=upload_handler;
+    i_inst->super._abstruct_function.create=create_handler;
+    i_inst->super._abstruct_function.remove=remove_handler;
+}
+#define cModFileIoDumy_finalize(i_inst) NyLPC_cModFileIoBaseClass_finalize(&(i_inst)->super)
+#define cModFileIoDumy_canHandle(i_inst,i_connection) NyLPC_cModFileIoBaseClass_canHandle(&(i_inst)->super,i_connection)
+#define cModFileIoDumy_execute(i_inst,i_connection) NyLPC_cModFileIoBaseClass_execute(&(i_inst)->super,i_connection)
+
+
+
+
+
+   
+   
+namespace MiMic
+{  
+   
+   
+    ModFileIo::ModFileIo(const char* i_path):ModBaseClass(i_path)
+    {
+    }
+    ModFileIo::ModFileIo()
+    {
+    }
+    ModFileIo::~ModFileIo()
+    {
+    }
+    void ModFileIo::setParam(const char* i_path)
+    {
+        ModBaseClass::setParam(i_path);
+    }
+    bool ModFileIo::execute(HttpdConnection& i_connection)
+    {
+        //check parametor
+        if(this->_path==NULL){
+            return false;
+        }
+        TcModFileIoDumy mod;
+        cModFileIoDumy_initialize(&mod,this->_path);
+        if(cModFileIoDumy_canHandle(&mod,i_connection._ref_inst)){
+            cModFileIoDumy_execute(&mod,i_connection._ref_inst);
+            cModFileIoDumy_finalize(&mod);
+            return true;
+        }
+        cModFileIoDumy_finalize(&mod);
+        return false;
+    }   
+   
+   
+    
+
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed/ModFileIo.h	Thu Jul 04 11:05:56 2013 +0000
@@ -0,0 +1,29 @@
+#include "NyLPC_net.h"
+#include "ModBaseClass.h"
+#include "HttpdConnection.h"
+#include "Httpd.h"
+
+namespace MiMic
+{
+    class HttpdConnection;
+
+    /**
+     * This class is httpd module.
+     * The class provides 3 services.
+     * <ul>
+     * <li>create a blank file.</li>
+     * <li>remove a file.</li>
+     * <li>update a file.</li>
+     * </ul>
+     */
+    class ModFileIo:ModBaseClass
+    {
+    public:
+
+        ModFileIo(const char* i_path);
+        ModFileIo();
+        virtual ~ModFileIo();
+        void setParam(const char* i_path);
+        bool execute(HttpdConnection& i_connection);
+    };
+}
\ No newline at end of file
--- a/mbed/mimic.h	Sun Jun 30 14:17:42 2013 +0000
+++ b/mbed/mimic.h	Thu Jul 04 11:05:56 2013 +0000
@@ -13,6 +13,7 @@
 #include "ModRemoteMcu.h"
 #include "ModMiMicSetting.h"
 #include "ModLocalFileSystem.h"
+#include "ModFileIo.h"
 #include "LocalFileSystem2.h"
 
 using namespace MiMic;
\ No newline at end of file