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

Committer:
nyatla
Date:
Sat Jun 04 06:51:04 2016 +0000
Revision:
114:f6aef16d4e60
Parent:
48:00d211aac2ec
include????????; ???????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nyatla 5:6d67db3875cb 1 /*********************************************************************************
nyatla 5:6d67db3875cb 2 * PROJECT: MiMic
nyatla 5:6d67db3875cb 3 * --------------------------------------------------------------------------------
nyatla 5:6d67db3875cb 4 *
nyatla 5:6d67db3875cb 5 * This file is part of MiMic
nyatla 5:6d67db3875cb 6 * Copyright (C)2011 Ryo Iizuka
nyatla 5:6d67db3875cb 7 *
nyatla 5:6d67db3875cb 8 * MiMic is free software: you can redistribute it and/or modify
nyatla 5:6d67db3875cb 9 * it under the terms of the GNU Lesser General Public License as published
nyatla 5:6d67db3875cb 10 * by the Free Software Foundation, either version 3 of the License, or
nyatla 5:6d67db3875cb 11 * (at your option) any later version.
nyatla 5:6d67db3875cb 12 *
nyatla 5:6d67db3875cb 13 * This program is distributed in the hope that it will be useful,
nyatla 5:6d67db3875cb 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nyatla 5:6d67db3875cb 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nyatla 5:6d67db3875cb 16 * GNU General Public License for more details.
nyatla 5:6d67db3875cb 17 *
nyatla 5:6d67db3875cb 18 * You should have received a copy of the GNU Lesser General Public License
nyatla 5:6d67db3875cb 19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
nyatla 5:6d67db3875cb 20 *
nyatla 5:6d67db3875cb 21 * For further information please contact.
nyatla 5:6d67db3875cb 22 * http://nyatla.jp/
nyatla 5:6d67db3875cb 23 * <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>
nyatla 5:6d67db3875cb 24 *
nyatla 5:6d67db3875cb 25 *********************************************************************************/
nyatla 5:6d67db3875cb 26 #include "NyLPC_cModUrl.h"
nyatla 5:6d67db3875cb 27 #include "NyLPC_stdlib.h"
nyatla 5:6d67db3875cb 28 #include "NyLPC_http.h"
nyatla 14:4618692ec239 29 #include "../NyLPC_cHttpdUtils.h"
nyatla 5:6d67db3875cb 30 #include "../NyLPC_cHttpdConnection_protected.h"
nyatla 5:6d67db3875cb 31
nyatla 5:6d67db3875cb 32 typedef struct TcHeaderParser
nyatla 5:6d67db3875cb 33 {
nyatla 5:6d67db3875cb 34 NyLPC_TcHttpBasicHeaderParser_t super;
nyatla 5:6d67db3875cb 35 char* url_buf;
nyatla 7:2b33a8d84eb3 36 NyLPC_TInt16 length_of_buf;
nyatla 7:2b33a8d84eb3 37 NyLPC_TInt16 length_of_url;
nyatla 19:33b9ba0859ee 38 NyLPC_TInt16 reason;
nyatla 48:00d211aac2ec 39 NyLPC_TUInt8 skip;
nyatla 48:00d211aac2ec 40 NyLPC_TUInt8 mode;
nyatla 5:6d67db3875cb 41 }TcHeaderParser_t;
nyatla 5:6d67db3875cb 42
nyatla 48:00d211aac2ec 43
nyatla 19:33b9ba0859ee 44
nyatla 5:6d67db3875cb 45 static NyLPC_TBool NyLPC_cModUrl_urlHandler(NyLPC_TcHttpBasicHeaderParser_t* i_inst,NyLPC_TChar i_c,struct NyLPC_THttpBasicHeader* o_out)
nyatla 5:6d67db3875cb 46 {
nyatla 5:6d67db3875cb 47 TcHeaderParser_t* inst=(TcHeaderParser_t*)i_inst;
nyatla 19:33b9ba0859ee 48 if(inst->skip){
nyatla 19:33b9ba0859ee 49 //SKIPが有効な場合
nyatla 19:33b9ba0859ee 50 if(inst->length_of_url<0){
nyatla 19:33b9ba0859ee 51 inst->length_of_url++;
nyatla 19:33b9ba0859ee 52 if(inst->length_of_url==0){
nyatla 19:33b9ba0859ee 53 inst->skip=NyLPC_TUInt8_FALSE;
nyatla 19:33b9ba0859ee 54 }
nyatla 19:33b9ba0859ee 55 }
nyatla 7:2b33a8d84eb3 56 return NyLPC_TBool_TRUE;
nyatla 7:2b33a8d84eb3 57 }
nyatla 19:33b9ba0859ee 58 if((inst->mode & NyLPC_cModUrl_ParseMode_PATH_ONLY)==NyLPC_cModUrl_ParseMode_PATH_ONLY){
nyatla 19:33b9ba0859ee 59 if(strchr("?#",i_c)){
nyatla 19:33b9ba0859ee 60 inst->url_buf[inst->length_of_url]='\0';
nyatla 19:33b9ba0859ee 61 inst->skip=NyLPC_TUInt8_TRUE;
nyatla 19:33b9ba0859ee 62 return NyLPC_TBool_TRUE;//Terminate
nyatla 19:33b9ba0859ee 63 }
nyatla 19:33b9ba0859ee 64 }
nyatla 5:6d67db3875cb 65 inst->url_buf[inst->length_of_url]=i_c;
nyatla 5:6d67db3875cb 66 if(i_c=='\0'){
nyatla 5:6d67db3875cb 67 return NyLPC_TBool_TRUE;//Terminate
nyatla 5:6d67db3875cb 68 }
nyatla 19:33b9ba0859ee 69 inst->length_of_url++;
nyatla 5:6d67db3875cb 70 if(inst->length_of_url==inst->length_of_buf){
nyatla 19:33b9ba0859ee 71 inst->reason=414;
nyatla 5:6d67db3875cb 72 return NyLPC_TBool_FALSE;//長すぎる。
nyatla 5:6d67db3875cb 73 }
nyatla 19:33b9ba0859ee 74
nyatla 5:6d67db3875cb 75 return NyLPC_TBool_TRUE;
nyatla 5:6d67db3875cb 76 }
nyatla 5:6d67db3875cb 77 /**
nyatla 5:6d67db3875cb 78 * デフォルトハンドラ
nyatla 5:6d67db3875cb 79 */
nyatla 5:6d67db3875cb 80 static const struct NyLPC_TcHttpBasicHeaderParser_Handler _handler=
nyatla 5:6d67db3875cb 81 {
nyatla 48:00d211aac2ec 82 NULL,
nyatla 5:6d67db3875cb 83 NyLPC_cModUrl_urlHandler
nyatla 5:6d67db3875cb 84 };
nyatla 5:6d67db3875cb 85
nyatla 5:6d67db3875cb 86
nyatla 5:6d67db3875cb 87
nyatla 5:6d67db3875cb 88 void NyLPC_cModUrl_initialize(NyLPC_TcModUrl_t* i_inst)
nyatla 5:6d67db3875cb 89 {
nyatla 41:2389bd6b6f74 90 NyLPC_cHttpBodyParser_initialize(&i_inst->_body_parser);
nyatla 5:6d67db3875cb 91 }
nyatla 5:6d67db3875cb 92 void NyLPC_cModUrl_finalize(NyLPC_TcModUrl_t* i_inst)
nyatla 5:6d67db3875cb 93 {
nyatla 41:2389bd6b6f74 94 NyLPC_cHttpBodyParser_finalize(&i_inst->_body_parser);
nyatla 5:6d67db3875cb 95 }
nyatla 5:6d67db3875cb 96
nyatla 5:6d67db3875cb 97 const struct NyLPC_THttpBasicHeader* NyLPC_cModUrl_getHeader(const NyLPC_TcModUrl_t* i_inst)
nyatla 5:6d67db3875cb 98 {
nyatla 5:6d67db3875cb 99 return &(i_inst->_header);
nyatla 5:6d67db3875cb 100 }
nyatla 5:6d67db3875cb 101
nyatla 5:6d67db3875cb 102 /**
nyatla 5:6d67db3875cb 103 * Methodタイプを返します。
nyatla 5:6d67db3875cb 104 */
nyatla 5:6d67db3875cb 105 NyLPC_THttpMethodType NyLPC_cModUrl_getMethod(const NyLPC_TcModUrl_t* i_inst)
nyatla 5:6d67db3875cb 106 {
nyatla 5:6d67db3875cb 107 return i_inst->_header.startline.req.method;
nyatla 5:6d67db3875cb 108 }
nyatla 5:6d67db3875cb 109
nyatla 5:6d67db3875cb 110
nyatla 5:6d67db3875cb 111
nyatla 19:33b9ba0859ee 112 NyLPC_TBool NyLPC_cModUrl_execute2(NyLPC_TcModUrl_t* i_inst,NyLPC_TcHttpdConnection_t* i_connection,char* o_url_buf,NyLPC_TInt16 i_length_buf,NyLPC_TInt16 i_pass_prefix_len,NyLPC_cModUrl_ParseMode i_mode)
nyatla 5:6d67db3875cb 113 {
nyatla 5:6d67db3875cb 114 TcHeaderParser_t parser;
nyatla 7:2b33a8d84eb3 115 NyLPC_Assert(i_length_buf>0);
nyatla 7:2b33a8d84eb3 116 NyLPC_Assert(i_pass_prefix_len>=0);
nyatla 19:33b9ba0859ee 117 //コネクションのステータスチェック
nyatla 5:6d67db3875cb 118 if(!NyLPC_cHttpdConnection_getReqStatus(i_connection)==NyLPC_cHttpdConnection_ReqStatus_REQPARSE)
nyatla 5:6d67db3875cb 119 {
nyatla 5:6d67db3875cb 120 NyLPC_OnErrorGoto(Error1);
nyatla 5:6d67db3875cb 121 }
nyatla 5:6d67db3875cb 122 //リクエストParse済へ遷移
nyatla 5:6d67db3875cb 123 NyLPC_cHttpdConnection_setReqStatusParsed(i_connection);
nyatla 5:6d67db3875cb 124
nyatla 5:6d67db3875cb 125 NyLPC_cHttpBasicHeaderParser_initialize(&parser.super,&_handler);
nyatla 5:6d67db3875cb 126 parser.length_of_buf=i_length_buf;
nyatla 7:2b33a8d84eb3 127 parser.length_of_url=-i_pass_prefix_len;//無視するPrefix長
nyatla 19:33b9ba0859ee 128 parser.skip=(parser.length_of_url<0)?NyLPC_TUInt8_TRUE:NyLPC_TUInt8_FALSE;//スキップの初期値の設定
nyatla 5:6d67db3875cb 129 parser.url_buf=o_url_buf;
nyatla 43:a182f2b5ff41 130 parser.url_buf[0]='\0';//URL長<=prefix長に備えてNULLターミネイト
nyatla 19:33b9ba0859ee 131 parser.reason=400;
nyatla 19:33b9ba0859ee 132 parser.mode=i_mode;
nyatla 5:6d67db3875cb 133 //プリフェッチしたデータを流す
nyatla 5:6d67db3875cb 134 NyLPC_cHttpBasicHeaderParser_parseInit(&parser.super,&(i_inst->_header));
nyatla 5:6d67db3875cb 135 NyLPC_cHttpdConnection_pushPrefetchInfo(i_connection,&parser.super,&(i_inst->_header));
nyatla 5:6d67db3875cb 136 //後続をストリームから取り込む
nyatla 5:6d67db3875cb 137 if(!NyLPC_cHttpBasicHeaderParser_parseStream(&parser.super,NyLPC_cHttpdConnection_refStream(i_connection),&(i_inst->_header))){
nyatla 5:6d67db3875cb 138 NyLPC_OnErrorGoto(Error2);
nyatla 5:6d67db3875cb 139 }
nyatla 5:6d67db3875cb 140 if(!NyLPC_cHttpBasicHeaderParser_parseFinish(&parser.super,&(i_inst->_header))){
nyatla 5:6d67db3875cb 141 NyLPC_OnErrorGoto(Error2);
nyatla 5:6d67db3875cb 142 }
nyatla 43:a182f2b5ff41 143 //@todo http/1.1 && POSTの場合はcontinueを送る。
nyatla 41:2389bd6b6f74 144 if(i_inst->_header.startline.req.version==NyLPC_THttpVersion_11){
nyatla 41:2389bd6b6f74 145 if(i_inst->_header.startline.req.method==NyLPC_THttpMethodType_POST)
nyatla 41:2389bd6b6f74 146 {
nyatla 41:2389bd6b6f74 147 NyLPC_cHttpdConnection_send100Continue(i_connection);
nyatla 41:2389bd6b6f74 148 }
nyatla 5:6d67db3875cb 149 }
nyatla 43:a182f2b5ff41 150 //Request::ConnectionがClose設定,又はHTTP1.1では無い場合,CLOSE
nyatla 43:a182f2b5ff41 151 if(i_inst->_header.connection==NyLPC_THttpMessgeHeader_Connection_CLOSE || i_inst->_header.startline.req.version!=NyLPC_THttpVersion_11)
nyatla 43:a182f2b5ff41 152 {
nyatla 43:a182f2b5ff41 153 NyLPC_cHttpdConnection_setConnectionMode(i_connection,NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE);
nyatla 43:a182f2b5ff41 154 }
nyatla 43:a182f2b5ff41 155
nyatla 5:6d67db3875cb 156 NyLPC_cHttpBasicHeaderParser_finalize(&parser);
nyatla 41:2389bd6b6f74 157 //BodyParserの初期化
nyatla 41:2389bd6b6f74 158 if(NyLPC_cHttpBodyParser_getState(&i_inst->_body_parser)!=NyLPC_TcHttpBasicBodyParser_ST_NULL){
nyatla 41:2389bd6b6f74 159 NyLPC_cHttpBodyParser_parseFinish(&i_inst->_body_parser);
nyatla 41:2389bd6b6f74 160 }
nyatla 41:2389bd6b6f74 161 NyLPC_cHttpBodyParser_parseInit(&i_inst->_body_parser,&i_inst->_header);
nyatla 5:6d67db3875cb 162 return NyLPC_TBool_TRUE;
nyatla 5:6d67db3875cb 163 Error2:
nyatla 5:6d67db3875cb 164 //400Error
nyatla 19:33b9ba0859ee 165 NyLPC_cHttpdUtils_sendErrorResponse(i_connection,parser.reason);
nyatla 5:6d67db3875cb 166 NyLPC_cHttpBasicHeaderParser_finalize(&parser);
nyatla 5:6d67db3875cb 167 Error1:
nyatla 5:6d67db3875cb 168 return NyLPC_TBool_FALSE;
nyatla 5:6d67db3875cb 169 }
nyatla 5:6d67db3875cb 170
nyatla 5:6d67db3875cb 171
nyatla 5:6d67db3875cb 172
nyatla 41:2389bd6b6f74 173
nyatla 41:2389bd6b6f74 174
nyatla 41:2389bd6b6f74 175 NyLPC_TInt16 NyLPC_cModUrl_readBody(NyLPC_TcModUrl_t* i_inst,NyLPC_TcHttpdConnection_t* i_connection,void* i_buf,NyLPC_TInt16 i_buf_size)
nyatla 41:2389bd6b6f74 176 {
nyatla 41:2389bd6b6f74 177 NyLPC_TInt16 l;
nyatla 41:2389bd6b6f74 178 //リクエストは解析済であること
nyatla 41:2389bd6b6f74 179 if(!NyLPC_cHttpdConnection_getReqStatus(i_connection)==NyLPC_cHttpdConnection_ReqStatus_END)
nyatla 41:2389bd6b6f74 180 {
nyatla 41:2389bd6b6f74 181 return -1;
nyatla 41:2389bd6b6f74 182 }
nyatla 41:2389bd6b6f74 183 if(!NyLPC_cHttpBodyParser_parseStream(&i_inst->_body_parser,NyLPC_cHttpdConnection_refStream(i_connection),i_buf,i_buf_size,&l)){
nyatla 41:2389bd6b6f74 184 return -1;
nyatla 41:2389bd6b6f74 185 }
nyatla 41:2389bd6b6f74 186 return l;
nyatla 41:2389bd6b6f74 187 }
nyatla 41:2389bd6b6f74 188