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:
85:416f8bbbdf54
include????????; ???????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nyatla 72:c118a7aa37a3 1 #include "ModJsonRpc.h"
nyatla 72:c118a7aa37a3 2 #include "../net/httpd/mod/NyLPC_cModWebSocket_protected.h"
nyatla 72:c118a7aa37a3 3 #include "HttpdConnection.h"
nyatla 72:c118a7aa37a3 4
nyatla 72:c118a7aa37a3 5 namespace MiMic
nyatla 72:c118a7aa37a3 6 {
nyatla 85:416f8bbbdf54 7 #define NUM_OF_OBJECTS 16
nyatla 76:b375b3d750d8 8 ModJsonRpc::BasicRpcObject** newObjectArray()
nyatla 73:8c7dd6fd462e 9 {
nyatla 76:b375b3d750d8 10 ModJsonRpc::BasicRpcObject** r=new ModJsonRpc::BasicRpcObject*[NUM_OF_OBJECTS];
nyatla 76:b375b3d750d8 11 memset(r,0,sizeof(ModJsonRpc::BasicRpcObject*)*NUM_OF_OBJECTS);
nyatla 73:8c7dd6fd462e 12 return r;
nyatla 73:8c7dd6fd462e 13 }
nyatla 76:b375b3d750d8 14 void deleteObjectArray(ModJsonRpc::BasicRpcObject** v)
nyatla 73:8c7dd6fd462e 15 {
nyatla 73:8c7dd6fd462e 16 for(int i=0;i<NUM_OF_OBJECTS;i++){
nyatla 73:8c7dd6fd462e 17 if(v[i]!=NULL){
nyatla 73:8c7dd6fd462e 18 delete v[i];
nyatla 73:8c7dd6fd462e 19 }
nyatla 73:8c7dd6fd462e 20 }
nyatla 76:b375b3d750d8 21 delete[] v;
nyatla 73:8c7dd6fd462e 22 }
nyatla 72:c118a7aa37a3 23
nyatla 72:c118a7aa37a3 24 ModJsonRpc::ModJsonRpc(const char* i_path,const struct NyLPC_TJsonRpcClassDef** i_rpc_table):ModBaseClass(i_path)
nyatla 72:c118a7aa37a3 25 {
nyatla 72:c118a7aa37a3 26 this->_mod=NULL;
nyatla 72:c118a7aa37a3 27 this->_rpc_table=i_rpc_table;
nyatla 72:c118a7aa37a3 28 }
nyatla 72:c118a7aa37a3 29 ModJsonRpc::ModJsonRpc()
nyatla 72:c118a7aa37a3 30 {
nyatla 72:c118a7aa37a3 31 this->_mod=NULL;
nyatla 72:c118a7aa37a3 32 }
nyatla 72:c118a7aa37a3 33 ModJsonRpc::~ModJsonRpc()
nyatla 72:c118a7aa37a3 34 {
nyatla 72:c118a7aa37a3 35 if(this->_mod!=NULL){
nyatla 85:416f8bbbdf54 36 NyLPC_cModJsonRpc_finalize(&(this->_mod->super));
nyatla 73:8c7dd6fd462e 37 free(this->_mod);
nyatla 73:8c7dd6fd462e 38 deleteObjectArray(this->_objects);
nyatla 72:c118a7aa37a3 39 this->_mod=NULL;
nyatla 72:c118a7aa37a3 40 }
nyatla 72:c118a7aa37a3 41 }
nyatla 72:c118a7aa37a3 42 void ModJsonRpc::setParam(const char* i_path,const struct NyLPC_TJsonRpcClassDef** i_rpc_table)
nyatla 72:c118a7aa37a3 43 {
nyatla 72:c118a7aa37a3 44 ModBaseClass::setParam(i_path);
nyatla 72:c118a7aa37a3 45 this->_rpc_table=i_rpc_table;
nyatla 72:c118a7aa37a3 46 }
nyatla 81:e5e4f2264d24 47 bool ModJsonRpc::isStarted(){
nyatla 81:e5e4f2264d24 48 return this->_mod!=NULL;
nyatla 81:e5e4f2264d24 49 }
nyatla 72:c118a7aa37a3 50 bool ModJsonRpc::execute(HttpdConnection& i_connection)
nyatla 72:c118a7aa37a3 51 {
nyatla 81:e5e4f2264d24 52 i_connection.lockHttpd();
nyatla 72:c118a7aa37a3 53 if(this->_mod!=NULL){
nyatla 81:e5e4f2264d24 54 i_connection.unlockHttpd();
nyatla 72:c118a7aa37a3 55 return false;
nyatla 72:c118a7aa37a3 56 }
nyatla 85:416f8bbbdf54 57 this->_mod=(TcJsonRpcEx_t*)malloc(sizeof(TcJsonRpcEx_t));
nyatla 81:e5e4f2264d24 58 i_connection.unlockHttpd();
nyatla 72:c118a7aa37a3 59 if(this->_mod==NULL){
nyatla 72:c118a7aa37a3 60 return false;
nyatla 72:c118a7aa37a3 61 }
nyatla 85:416f8bbbdf54 62 this->_mod->cppmod_ptr=this;
nyatla 81:e5e4f2264d24 63
nyatla 72:c118a7aa37a3 64 //initialize websocket
nyatla 85:416f8bbbdf54 65 NyLPC_cModJsonRpc_initialize(&(this->_mod->super),this->_path,this->_rpc_table);
nyatla 85:416f8bbbdf54 66 if(NyLPC_cModJsonRpc_canHandle(&(this->_mod->super),i_connection._ref_inst)){
nyatla 85:416f8bbbdf54 67 if(NyLPC_cModJsonRpc_execute(&(this->_mod->super),i_connection._ref_inst)){
nyatla 73:8c7dd6fd462e 68 //initialize object array
nyatla 73:8c7dd6fd462e 69 this->_objects=newObjectArray();
nyatla 73:8c7dd6fd462e 70 return true;
nyatla 73:8c7dd6fd462e 71 }
nyatla 72:c118a7aa37a3 72 }
nyatla 85:416f8bbbdf54 73 NyLPC_cModJsonRpc_finalize(&(this->_mod->super));
nyatla 72:c118a7aa37a3 74 free(this->_mod);
nyatla 81:e5e4f2264d24 75 i_connection.lockHttpd();
nyatla 72:c118a7aa37a3 76 this->_mod=NULL;
nyatla 81:e5e4f2264d24 77 i_connection.unlockHttpd();
nyatla 73:8c7dd6fd462e 78 return false;
nyatla 72:c118a7aa37a3 79 }
nyatla 72:c118a7aa37a3 80
nyatla 75:2a18df0ef111 81 void ModJsonRpc::dispatchRpc()
nyatla 72:c118a7aa37a3 82 {
nyatla 72:c118a7aa37a3 83 const union NyLPC_TJsonRpcParserResult* rpc_result;
nyatla 73:8c7dd6fd462e 84 if(this->_mod==NULL){
nyatla 73:8c7dd6fd462e 85 return;
nyatla 73:8c7dd6fd462e 86 }
nyatla 72:c118a7aa37a3 87 for(;;){
nyatla 85:416f8bbbdf54 88 if(!NyLPC_cModJsonRpc_processRpcMessage(&(this->_mod->super))){
nyatla 72:c118a7aa37a3 89 break;
nyatla 72:c118a7aa37a3 90 }
nyatla 72:c118a7aa37a3 91 //メッセージ取得を試行
nyatla 85:416f8bbbdf54 92 rpc_result=NyLPC_cModJsonRpc_getMessage(&(this->_mod->super));
nyatla 72:c118a7aa37a3 93 if(rpc_result==NULL){
nyatla 72:c118a7aa37a3 94 //nothing
nyatla 72:c118a7aa37a3 95 continue;
nyatla 72:c118a7aa37a3 96 }
nyatla 72:c118a7aa37a3 97 if(NyLPC_TJsonRpcParserResult_hasMethodHandler(rpc_result)){
nyatla 85:416f8bbbdf54 98 if(NyLPC_TJsonRpcParserResult_callMethodHandler(rpc_result,this->_mod)){
nyatla 72:c118a7aa37a3 99 continue;
nyatla 72:c118a7aa37a3 100 }else{
nyatla 72:c118a7aa37a3 101 //function failed.
nyatla 72:c118a7aa37a3 102 break;
nyatla 72:c118a7aa37a3 103 }
nyatla 72:c118a7aa37a3 104 }else{
nyatla 72:c118a7aa37a3 105 //no handler
nyatla 72:c118a7aa37a3 106 break;
nyatla 72:c118a7aa37a3 107 }
nyatla 72:c118a7aa37a3 108 }
nyatla 85:416f8bbbdf54 109 NyLPC_cModJsonRpc_close(&(this->_mod->super),1000);
nyatla 85:416f8bbbdf54 110 NyLPC_cModJsonRpc_finalize(&(this->_mod->super));
nyatla 73:8c7dd6fd462e 111 deleteObjectArray(this->_objects);
nyatla 73:8c7dd6fd462e 112 free(this->_mod);
nyatla 73:8c7dd6fd462e 113 this->_mod=NULL;
nyatla 72:c118a7aa37a3 114 return;
nyatla 72:c118a7aa37a3 115 }
nyatla 85:416f8bbbdf54 116 bool ModJsonRpc::putResult(unsigned int i_id)
nyatla 85:416f8bbbdf54 117 {
nyatla 85:416f8bbbdf54 118 return this->putResult(i_id,"");
nyatla 85:416f8bbbdf54 119 }
nyatla 72:c118a7aa37a3 120 bool ModJsonRpc::putResult(unsigned int i_id,const char* i_params_fmt,...)
nyatla 72:c118a7aa37a3 121 {
nyatla 72:c118a7aa37a3 122 bool ret;
nyatla 72:c118a7aa37a3 123 va_list a;
nyatla 72:c118a7aa37a3 124 va_start(a,i_params_fmt);
nyatla 85:416f8bbbdf54 125 ret=NyLPC_cModJsonRpc_putResultV(&(this->_mod->super),i_id,i_params_fmt,a)?true:false;
nyatla 72:c118a7aa37a3 126 va_end(a);
nyatla 72:c118a7aa37a3 127 return ret;
nyatla 72:c118a7aa37a3 128 }
nyatla 72:c118a7aa37a3 129 bool ModJsonRpc::putError(unsigned int i_id,int i_code)
nyatla 72:c118a7aa37a3 130 {
nyatla 85:416f8bbbdf54 131 return NyLPC_cModJsonRpc_putError(&(this->_mod->super),i_id,i_code)?true:false;
nyatla 72:c118a7aa37a3 132 }
nyatla 72:c118a7aa37a3 133
nyatla 76:b375b3d750d8 134 int ModJsonRpc::addObject(BasicRpcObject* i_object)
nyatla 72:c118a7aa37a3 135 {
nyatla 72:c118a7aa37a3 136 for(int i=0;i<NUM_OF_OBJECTS;i++){
nyatla 72:c118a7aa37a3 137 if(this->_objects[i]==NULL){
nyatla 72:c118a7aa37a3 138 this->_objects[i]=i_object;
nyatla 72:c118a7aa37a3 139 return i;
nyatla 72:c118a7aa37a3 140 }
nyatla 72:c118a7aa37a3 141 }
nyatla 72:c118a7aa37a3 142 return -1;
nyatla 72:c118a7aa37a3 143 }
nyatla 85:416f8bbbdf54 144 bool ModJsonRpc::removeObject(int i_id)
nyatla 85:416f8bbbdf54 145 {
nyatla 85:416f8bbbdf54 146 if(0<=i_id && i_id<NUM_OF_OBJECTS){
nyatla 85:416f8bbbdf54 147 if(this->_objects[i_id]!=NULL){
nyatla 85:416f8bbbdf54 148 delete this->_objects[i_id];
nyatla 85:416f8bbbdf54 149 this->_objects[i_id]=NULL;
nyatla 85:416f8bbbdf54 150 return true;
nyatla 85:416f8bbbdf54 151 }
nyatla 85:416f8bbbdf54 152 }
nyatla 85:416f8bbbdf54 153 return false;
nyatla 85:416f8bbbdf54 154 }
nyatla 72:c118a7aa37a3 155 void* ModJsonRpc::getObject(int i_oid)
nyatla 72:c118a7aa37a3 156 {
nyatla 76:b375b3d750d8 157 return this->_objects[i_oid]->obj;
nyatla 76:b375b3d750d8 158 }
nyatla 72:c118a7aa37a3 159
nyatla 76:b375b3d750d8 160 }