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

Revision:
69:8c5f220441f5
Parent:
68:f7def7eb5504
Child:
79:baa21f8763cf
--- a/core/uip/NyLPC_cUipService.c	Thu May 22 12:59:50 2014 +0000
+++ b/core/uip/NyLPC_cUipService.c	Thu May 29 14:29:15 2014 +0000
@@ -106,9 +106,10 @@
 
 
 static NyLPC_TBool sendIPv4Tx(struct NyLPC_TTxBufferHeader* i_eth_buf);
-static void copyAndSendIPv4Tx(const struct TEthPacket* i_buf);
+
 //static void sendArpReqest(const struct TEthPacket* i_eth_packet);
-static void sendRawEthFrame(void* i_buf,NyLPC_TUInt16 i_len);
+static void sendRawEthFrameNL(void* i_buf,NyLPC_TUInt16 i_len);
+static void releaseTxBufNL(void* i_buf);
 
 /**メッセージなし*/
 #define TTaskMessage_MSG_NULL    0x0000
@@ -256,9 +257,6 @@
             NyLPC_cIPv4Arp_initialize(&(inst->_arp),inst->_ref_config);
             NyLPC_cStopwatch_startExpire(&(inst->_arp_sw),1);//1度ARPを起動するため。
             NyLPC_cStopwatch_startExpire(&(inst->_periodic_sw),PERIODIC_TIMER);
-            //InBuffer初期化
-            inst->stx.h.is_lock=NyLPC_TUInt8_FALSE;
-            inst->stx.h.ref=0;
             //EtherNETデバイス初期化
             while(!NyLPC_iEthernetDevice_start(inst->_ethif,&(inst->_ref_config->eth_mac),ethernet_handler,inst));
             NyLPC_TUInt16_setBit(inst->_status,NyLPC_TcUipService_STATUSBIT_IS_RUNNING);
@@ -277,7 +275,7 @@
     NyLPC_TUInt16 rx_len,tx_len;
     struct TEthPacket* ethbuf;
     NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst;
-    NyLPC_TBool r;
+    void* r;
     (void)pvParameters;
     for( ;; )
     {
@@ -304,27 +302,28 @@
                     //ARPテーブルの更新
                     //uip_arp_ipin(&(ethbuf->header),ethbuf->data.ipv4.srcipaddr);
                     NyLPC_cIPv4Arp_incomingIp(&inst->_arp,&(ethbuf->header),ethbuf->data.ipv4.srcipaddr);
-                    //Ethernet Device UnLock(パケット解析の為に一時的な解除)
+                    //Ethernet Device UnLock(NyLPC_cIPv4_rxがallocをコールする可能性があるので一時的にロック解除)
                     NyLPC_cMutex_unlock(&(inst->_mutex));
                     //IPパケットの処理
                     r=NyLPC_cIPv4_rx(&(inst->_tcpv4),&(ethbuf->data.ipv4),rx_len);
+                    if(r!=NULL){
+                        //IPパケットとして送信
+                        NyLPC_cUipService_sendIPv4Tx(r);
+                    }
                     //ロックの復帰
                     NyLPC_cMutex_lock(&(inst->_mutex));
-                    if(!r){
-                        //応答データは存在しない。
-                        break;
+                    if(r!=NULL){
+                        releaseTxBufNL(r);
                     }
-                    //IPパケットをTxバッファに転写して送信
-                    copyAndSendIPv4Tx(ethbuf);
-                    ethbuf=NULL;
                     break;
                 case NyLPC_HTONS(NyLPC_TEthernetIIHeader_TYPE_ARP):
-                    //if(uip_arp_arpin(&(ethbuf->data.arp),rx_len)){
-                    if(NyLPC_cIPv4Arp_incomingArp(&inst->_arp,&(ethbuf->data.arp),rx_len)){
-                        tx_len=NyLPC_TEthernetIIHeader_setArpTx(&(ethbuf->header),&(inst->_ref_config->eth_mac));
-                    }
-                    if(tx_len>0){
-                        sendRawEthFrame(ethbuf,tx_len);
+                    //Ethernet Device UnLock(NyLPC_cIPv4_rxがallocをコールする可能性があるので一時的にロック解除)
+                    NyLPC_cMutex_unlock(&(inst->_mutex));
+                    r=NyLPC_cIPv4Arp_rx(&inst->_arp,&(ethbuf->data.arp),rx_len,&tx_len);
+                    NyLPC_cMutex_lock(&(inst->_mutex));
+                    if(r!=NULL){
+                        sendRawEthFrameNL(r,tx_len);
+                        releaseTxBufNL(r);
                     }
                     break;
                 case NyLPC_HTONS(NyLPC_TEthernetIIHeader_TYPE_IPV6):
@@ -377,22 +376,26 @@
 
 /**
  * IPv4パケットのpeerIPを問い合わせるARPパケットを送信します。
+ * allocを中でコールするから要UNLOCK状態
  */
-static void sendArpReqest(const struct NyLPC_TIPv4Addr* i_addr)
+void NyLPC_cUipService_sendArpRequest(const struct NyLPC_TIPv4Addr* i_addr)
 {
     NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst;
+    struct NyLPC_TTxBufferHeader* p;
     NyLPC_TUInt16 tx_len;
     struct TEthPacket* ethbuf;
-    //ACK送信用の自己バッファが空くまで待つ
-    while(inst->stx.h.is_lock){
-        NyLPC_iEthernetDevice_processTx(inst->_ethif);
-    }
+    //システムTxBufを得る
+    ethbuf=(struct TEthPacket*)NyLPC_cUipService_allocSysTxBuf();
     //ARPパケットを作る。
-    ethbuf=(struct TEthPacket*)(inst->stx.buf);
     NyLPC_TArpHeader_setArpRequest(&(ethbuf->data.arp),inst->_ref_config->ip_addr,&(inst->_ref_config->eth_mac),i_addr);
     tx_len=NyLPC_TEthernetIIHeader_setArpTx(&(ethbuf->header),&(inst->_ref_config->eth_mac));
     //送信
-    NyLPC_iEthernetDevice_sendTxEthFrame(inst->_ethif,&(inst->stx.h),tx_len);
+    p=((struct NyLPC_TTxBufferHeader*)(((struct NyLPC_TEthernetIIHeader*)ethbuf)-1))-1;
+
+    NyLPC_cMutex_lock(&(inst->_mutex));
+    NyLPC_iEthernetDevice_sendTxEthFrame(inst->_ethif,p,tx_len);
+    NyLPC_iEthernetDevice_releaseTxBuf(inst->_ethif,p);
+    NyLPC_cMutex_unlock(&(inst->_mutex));
 }
 
 
@@ -410,30 +413,18 @@
     NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst;
     struct NyLPC_TTxBufferHeader* p=((struct NyLPC_TTxBufferHeader*)(((struct NyLPC_TEthernetIIHeader*)i_eth_payload)-1))-1;
     NyLPC_cMutex_lock(&(inst->_mutex));
-
     //IPパケットの送信を試行
-    if(!sendIPv4Tx(p)){
-        //ARPリクエストを代わりに送信
-        sendArpReqest(&((struct NyLPC_TIPv4Header*)i_eth_payload)->destipaddr);
+    if(sendIPv4Tx(p)){
+        NyLPC_cMutex_unlock(&(inst->_mutex));
+        return;
     }
     NyLPC_cMutex_unlock(&(inst->_mutex));
+    //ARPリクエストを代わりに送信
+    NyLPC_cUipService_sendArpRequest(&((struct NyLPC_TIPv4Header*)i_eth_payload)->destipaddr);
     return;
 }
 
 
-
-/**
- * 指定したIPアドレスに対してARPRequestを送信します。
- */
-void NyLPC_cUipService_sendArpRequest(const struct NyLPC_TIPv4Addr* i_addr)
-{
-    NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst;
-    NyLPC_cMutex_lock(&(inst->_mutex));
-    sendArpReqest(i_addr);
-    NyLPC_cMutex_unlock(&(inst->_mutex));
-    return;
-}
-
 /**
  * ARPテーブルに指定したIPがあるかを返します。
  */
@@ -443,21 +434,32 @@
     return NyLPC_cIPv4Arp_IPv4toEthAddr(&inst->_arp,*i_addr)!=NULL;
 }
 
+/**
+ * システム用の送信ペイロードを返します。
+ * 関数は必ず成功します。
+ */
+void* NyLPC_cUipService_allocSysTxBuf(void)
+{
+    NyLPC_TUInt16 s;
+    NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst;
+    struct NyLPC_TTxBufferHeader* ethbuf;
+    //排他処理をして、メモリを取得する。SYSTEMメモリはEthernetドライバの解放待ちのみなのでまとめてLOCKしておk
+    NyLPC_cMutex_lock(&(inst->_mutex));
+    for(;;){
+        ethbuf=(struct NyLPC_TTxBufferHeader*)NyLPC_iEthernetDevice_allocTxBuf(inst->_ethif,NyLPC_TcEthernetMM_HINT_CTRL_PACKET,&s);
+        if(ethbuf==NULL){
+            NyLPC_cThread_yield();
+            continue;
+        }
+        break;
+    }
+    NyLPC_cMutex_unlock(&(inst->_mutex));
+    //イーサネットバッファのアドレスを計算
+    return &(((struct TEthPacket*)(ethbuf+1))->data);
+}
 
 
 
-/**
- * 送信ペイロードメモリを返します。
- * この関数は、リエントラントを許容します。
- * @param i_hint
- * 取得したいメモリサイズを指定します。(このサイズは、イーサネットヘッダのサイズを含みません。)
- * このサイズよりも小さなサイズが割り当てられることがあります。
- * @param o_size
- * イーサネットヘッダを除いたペイロード部分の長さ
- * @return
- * 成功:IPペイロードのためのメモリブロックを返します。/失敗:NULL
- * 返されるメモリはブロックの[TEthPacket][payload]の構造で、[payload]のアドレスが返されます。
- */
 void* NyLPC_cUipService_allocTxBuf(NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_size)
 {
     NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst;
@@ -497,11 +499,29 @@
 /**********
  * イーサネットHWのコントロール関数
  */
-
+/**
+ * "IPv4パケットを格納した"イーサフレームを送信します。
+ * コール前に、必ずロックしてから呼び出してください。
+ *//*
+static void copyAndSendIPv4Tx(const struct TEthPacket* i_buf)
+{
+    NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst;
+    NyLPC_TUInt16 s;
+    //送信する。
+    s=NyLPC_htons(i_buf->data.ipv4.len16)+sizeof(struct NyLPC_TEthernetIIHeader);
+    memcpy(inst->stx.buf,i_buf,s);
+    if(!sendIPv4Tx(&(inst->stx.h))){
+        //失敗した場合はARPリクエストに変換して再送
+//@todo unchecked PASS!
+        sendArpReqest(&i_buf->data.ipv4.destipaddr);
+    }
+    return;
+}*/
 /**
  * "IPv4パケットを格納した"イーサフレームを送信します。
  * コール前に、必ずロックしてから呼び出してください。
  */
+/*
 static void copyAndSendIPv4Tx(const struct TEthPacket* i_buf)
 {
     NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst;
@@ -520,35 +540,37 @@
     }
     return;
 }
-
+*/
 
 
 /**
- * uipタスクが所有するTXバッファを使用してデータを送信します。
- * この関数は、i_bufをコピーします。
+ * イーサネットフレームを送信します。
+ * この関数はiptaskで実行される関数からのみ使用てください。
  * @i_buf
  * イーサネットフレームを格納したメモリです。
  * @i_len
  * イーサネットペイロードのサイズです。
  */
-static void sendRawEthFrame(void* i_buf,NyLPC_TUInt16 i_len)
+static void sendRawEthFrameNL(void* i_buf,NyLPC_TUInt16 i_len)
 {
-    NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst;
-
-    //ACK送信用の自己バッファが空くまで待つ
-    while(inst->stx.h.is_lock){
-        NyLPC_iEthernetDevice_processTx(inst->_ethif);
-    }
-    //64バイトを超えるとかありえない。
-    if(i_len+sizeof(struct NyLPC_TEthernetIIHeader)>NyLPC_TcUipService_SIZE_OF_REPLY_BUF){
-        return;
-    }
-    //送信する。
-    memcpy(inst->stx.buf,i_buf,i_len);
-    NyLPC_iEthernetDevice_sendTxEthFrame(inst->_ethif,&(inst->stx.h),i_len);
+    NyLPC_iEthernetDevice_sendTxEthFrame(
+        _NyLPC_TcUipService_inst->_ethif,
+        ((struct NyLPC_TTxBufferHeader*)(((struct NyLPC_TEthernetIIHeader*)i_buf)-1))-1,
+        i_len);
     return;
 }
-
+/**
+ * ロック状態で使用できるreleaseTxBuf。
+ * この関数はiptaskで実行される関数からのみ使用してください。
+ */
+static void releaseTxBufNL(void* i_buf)
+{
+    //ペイロードの位置から、メモリブロックを再生。
+    NyLPC_iEthernetDevice_releaseTxBuf(
+        _NyLPC_TcUipService_inst->_ethif,
+        ((struct NyLPC_TTxBufferHeader*)(((struct NyLPC_TEthernetIIHeader*)i_buf)-1))-1);
+    return;
+}
 /**
  * マルチキャスとアドレスへ変換する。
  */