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:
57:bc4330dfa62f
Parent:
43:a182f2b5ff41
Child:
58:03b89038b21a
--- a/core/uip/NyLPC_cTcpSocket.c	Sat Aug 10 02:52:22 2013 +0000
+++ b/core/uip/NyLPC_cTcpSocket.c	Fri Sep 13 06:38:16 2013 +0000
@@ -31,6 +31,29 @@
 static NyLPC_TUInt32 iss32=3939;
 #define SIZE_OF_IPv4_TCPIP_HEADER 40
 
+/**
+ * TCPのRTOの最大値。
+ * ms単位である。
+ * defaultは64SEC
+ */
+#define UIP_IP_RTO_MAX_RTO 64000
+/**
+ * TCPのRTOの初期値。
+ * ms単位である。
+ * 伝送路の特性に合わせて調整すること。
+ */
+#define UIP_TCP_RTO_INITIAL 3000
+
+/**
+ * CONNECTION時のRTO
+ */
+#define UIP_TCP_RTO_CONNECTION_INITIAL 200
+
+/**
+ * 下限値
+ */
+#define UIP_TCP_RTO_MINIMUM 100
+
 
 /**
  * for Debug
@@ -44,11 +67,10 @@
     #define DEBUG_RTO_LOG(i_inst)
 #endif
 
-
 //#define lockResource(i_inst) NyLPC_cMutex_lock(&((i_inst)->_smutex))
 //#define unlockResource(i_inst) NyLPC_cMutex_unlock(&((i_inst)->_smutex))
-#define lockResource(i_inst) NyLPC_cMutex_lock(((i_inst)->_smutex))
-#define unlockResource(i_inst) NyLPC_cMutex_unlock(((i_inst)->_smutex))
+#define lockResource(i_inst) NyLPC_cMutex_lock(NyLPC_cIPv4_getSockMutex(((i_inst)->_super._parent_ipv4)))
+#define unlockResource(i_inst) NyLPC_cMutex_unlock(NyLPC_cIPv4_getSockMutex(((i_inst)->_super._parent_ipv4)))
 
 static void sendRst(NyLPC_TcTcpSocket_t* i_inst);
 
@@ -225,8 +247,8 @@
         break;
     }
     NyLPC_cStopwatch_finalize(&sw);
-    if(new_rto<UIP_IP_RTO_MINIMUM){
-        new_rto=UIP_IP_RTO_MINIMUM;
+    if(new_rto<UIP_TCP_RTO_MINIMUM){
+        new_rto=UIP_TCP_RTO_MINIMUM;
     }
     i_inst->uip_connr.current_rto32=new_rto;
 }
@@ -312,7 +334,7 @@
 
     NyLPC_cFifoBuffer_initialize(&(i_inst->rxbuf),i_rbuf,i_rbuf_len);
     //  NyLPC_AbortIfNot(NyLPC_cMutex_initialize(&(i_inst->_smutex)));//個別Mutex
-    i_inst->_smutex=NyLPC_cIPv4_getSockMutex(&(srv->_tcpv4));//共有Mutex
+//  i_inst->_smutex=NyLPC_cIPv4_getSockMutex(&(srv->_tcpv4));//共有Mutex
     i_inst->tcpstateflags=UIP_CLOSED;
     i_inst->txbuf.rp=i_inst->txbuf.wp=0;
     for(i=0;i<NyLPC_TcTcpSocket_NUMBER_OF_TXQ;i++){
@@ -356,7 +378,7 @@
     {
         //localipとdefault_mmsは別枠で設定
         /* Fill in the necessary fields for the new connection. */
-        i_inst->uip_connr.current_rto32 = UIP_IP_RTOP_INITIAL;
+        i_inst->uip_connr.current_rto32 = UIP_TCP_RTO_INITIAL;
         i_inst->uip_connr.lport = i_lport;
         i_inst->uip_connr.rport = i_lq->rport;
         i_inst->uip_connr.ripaddr=i_lq->srcaddr;
@@ -364,20 +386,17 @@
         /* rcv_nxt should be the seqno from the incoming packet + 1. */
         i_inst->uip_connr.rcv_nxt32= i_lq->rcv_nxt32;
         //MSSの設定
-        i_inst->uip_connr.peer_mss=i_inst->uip_connr.default_mss;
-        if(i_lq->mss!=0){
-            i_inst->uip_connr.peer_mss=i_lq->mss;
-        }
+        i_inst->uip_connr.peer_mss=(i_lq->mss!=0)?i_lq->mss:i_inst->uip_connr.default_mss;
         i_inst->uip_connr.peer_win=0;
         NyLPC_cFifoBuffer_clear(&(i_inst->rxbuf));
+        //ここでステータスがかわる。
+        i_inst->tcpstateflags = UIP_SYN_RCVD;
         //前回のデータが残っていた場合の保険
         if(i_inst->txbuf.rp!=i_inst->txbuf.wp){
             resetTxQWithUnlock(i_inst);
         }else{
             unlockResource(i_inst);
         }
-        //ここでステータスがかわる。
-        i_inst->tcpstateflags = UIP_SYN_RCVD;
         return NyLPC_TBool_TRUE;
     }
     unlockResource(i_inst);
@@ -496,7 +515,7 @@
         s=i_len;
     }
     //ACK番号の計算
-    next_ack=i_inst->uip_connr.snd_nxt32+s+((i_tcpf&(TCP_FIN|TCP_SYN))!=0x00?1:0);
+    next_ack=i_inst->uip_connr.snd_nxt32+s+(((i_tcpf&(TCP_FIN|TCP_SYN))!=0x00)?1:0);
     txq->rto32=i_inst->uip_connr.current_rto32;
     txq->tick_of_sent=NyLPC_cStopwatch_now();
 
@@ -561,6 +580,7 @@
         //エラー:ドロップする。
         return NyLPC_TBool_FALSE;
     }
+
     return NyLPC_TBool_TRUE;
 }
 
@@ -571,6 +591,77 @@
  * Public function
  */
 
+NyLPC_TBool NyLPC_cTcpSocket_connect(NyLPC_TcTcpSocket_t* i_inst,struct NyLPC_TIPv4Addr* i_addr,NyLPC_TUInt16 i_peer_port,NyLPC_TUInt32 i_wait_in_msec)
+{
+    volatile NyLPC_TUInt8 f;
+    NyLPC_TUInt32 sq;
+    NyLPC_TcStopwatch_t sw;
+    NyLPC_TUInt16 lport;
+    lockResource(i_inst);
+    //ソケットが無効であること。
+    if(i_inst->tcpstateflags!=UIP_CLOSED)
+    {
+        NyLPC_OnErrorGoto(Error);
+    }
+    //ポート番号の取得(lockResourceが他のソケットと共有なので、重複ポートの割当は起こりえない。でもちょっと注意して)
+    lport=NyLPC_htons(NyLPC_cIPv4_getNewPortNumber(i_inst->_super._parent_ipv4));
+    if(lport==0){
+        NyLPC_OnErrorGoto(Error);
+    }
+    //connectの為の準備
+
+    //localipとdefault_mmsは別枠で設定
+    /* Fill in the necessary fields for the new connection. */
+    i_inst->uip_connr.current_rto32 = UIP_TCP_RTO_CONNECTION_INITIAL;//RTOを短くしてARP発行時の再接続短縮を期待する。
+    i_inst->uip_connr.lport = lport;
+    i_inst->uip_connr.rport = NyLPC_htons(i_peer_port);
+    i_inst->uip_connr.ripaddr=*i_addr;
+    i_inst->uip_connr.snd_nxt32=iss32;//should be random
+    /* rcv_nxt should be the seqno from the incoming packet + 1. */
+    i_inst->uip_connr.rcv_nxt32=0;
+    //MSSの設定
+    i_inst->uip_connr.peer_mss=i_inst->uip_connr.default_mss;
+    i_inst->uip_connr.peer_win=1;//periodicの再送信を期待するために相手のWindowサイズは1と仮定する。
+    NyLPC_cFifoBuffer_clear(&(i_inst->rxbuf));
+    //ここでステータスがかわる。
+    i_inst->tcpstateflags = UIP_SYN_SENT;
+    //前回のデータが残っていた場合の保険
+    if(i_inst->txbuf.rp!=i_inst->txbuf.wp){
+        resetTxQWithUnlock(i_inst);
+    }else{
+        unlockResource(i_inst);
+    }
+
+    NyLPC_cStopwatch_initialize(&sw);
+
+    NyLPC_cStopwatch_startExpire(&sw,i_wait_in_msec);
+    if(sendWithRetransmit(i_inst,TCP_SYN,NULL,0,&sw,&sq)==0){
+        //ちょっと待つ。
+        NyLPC_cThread_yield();
+        //キューにあるTXが消えるのを待つ。
+        if(waitForTxRemove(i_inst,sq,&sw)){
+            //ACK受信に成功して、TXが消失
+            NyLPC_cStopwatch_finalize(&sw);
+            return NyLPC_TBool_TRUE;
+        }
+    }
+    //ロックして、強制的なステータス遷移
+    lockResource(i_inst);
+    f=i_inst->tcpstateflags;
+    if(f!=UIP_CLOSED){
+        //もし、強制CLOSE遷移であれば、RSTも送信。
+        i_inst->tcpstateflags=UIP_CLOSED;
+        unlockResource(i_inst);
+        sendRst(i_inst);
+    }else{
+        unlockResource(i_inst);
+    }
+    return NyLPC_TBool_FALSE;
+Error:
+    unlockResource(i_inst);
+    return NyLPC_TBool_FALSE;
+}
+
 /**
  * この関数は、UIP_SYN_RCVDステータスのソケットを、ESTABLISHEDへ遷移させます。
  * cTcpListener_listen関数を通過したインスタンスに実行してください。
@@ -612,12 +703,12 @@
     lockResource(i_inst);
     f=i_inst->tcpstateflags;
     if(f!=UIP_CLOSED){
+        //もし、強制CLOSE遷移であれば、RSTも送信。
         i_inst->tcpstateflags=UIP_CLOSED;
-    }
-    unlockResource(i_inst);
-    //もし、強制CLOSE遷移であれば、RSTも送信。
-    if(f!=UIP_CLOSED){
+        unlockResource(i_inst);
         sendRst(i_inst);
+    }else{
+        unlockResource(i_inst);
     }
     return NyLPC_TBool_FALSE;
 }
@@ -861,7 +952,7 @@
     if(i_len<1){
         return 0;
     }
-    hint=(i_len>32767)?32767:0;
+    hint=(i_len>32767)?32767:i_len;
     buf=NyLPC_cTcpSocket_allocSendBuf(i_inst,hint,&s,i_wait_in_msec);
     if(buf==NULL){
         return -1;
@@ -967,12 +1058,12 @@
     lockResource(i_inst);
     f=i_inst->tcpstateflags;
     if(f!=UIP_CLOSED){
+        //もし、強制CLOSE遷移であれば、RSTも送信。
         i_inst->tcpstateflags=UIP_CLOSED;
-    }
-    unlockResource(i_inst);
-    //もし、強制CLOSE遷移であれば、RSTも送信。
-    if(f!=UIP_CLOSED){
+        unlockResource(i_inst);
         sendRst(i_inst);
+    }else{
+        unlockResource(i_inst);
     }
     NyLPC_cStopwatch_finalize(&sw);
     return;
@@ -1149,8 +1240,8 @@
                 }
             }
         }
-        //MSSとWNDの更新
-        i_inst->uip_connr.peer_mss = i_inst->uip_connr.default_mss;
+//      //MSSとWNDの更新
+//      i_inst->uip_connr.peer_mss = i_inst->uip_connr.default_mss;//@bug じゃないのこれ。peer_mss勝手に上書きしたらダメだろ
         //どちらにしろ、ACK送信
         if(is_new_packet && (in_tcpflag & TCP_FIN)){
             //FINがあるときは、ステータスをCLOSE_WAITへセットして、ACKを返す。
@@ -1200,10 +1291,21 @@
         break;
     case UIP_CLOSED:
         //何もできない。何もしない。
-        break;//goto DROP;
+        break;
     case UIP_TIME_WAIT:
         //最終ACKを送り続ける。
         break;
+    case UIP_SYN_SENT:
+        //connect関数実行中しか起動しないステータス
+        if(num_of_noack==0){
+            i_inst->tcpstateflags=UIP_ESTABLISHED;
+            i_inst->uip_connr.rcv_nxt32=NyLPC_ntohl(o_ipp->payload.tcp->seqno32)+1;
+        }else{
+            //それ以外のパケットはドロップする。
+            break;//goto DROP;
+        }
+        //ACKを送る。
+        break;
     default:
         goto DROP;
     }