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

mbed/NetConfig.cpp

Committer:
nyatla
Date:
2016-06-04
Revision:
114:f6aef16d4e60
Parent:
109:18f12ac01097

File content as of revision 114:f6aef16d4e60:

#include "NetConfig.h"
#include "NyLPC_flash.h"
#include "NyLPC_netif.h"
#include "utils/PlatformInfo.h"
#include <stdio.h>
#include <stdlib.h>
#include "mbed.h"

/**
 * Default UPnP Description
 */
const static char* UPNP_DEVICE_TYPE ="urn:schemas-upnp-org:device:Basic:1";
const static char* UPNP_DEFAULT_FRENDLY_NAME="MiMic Device";
const static char* UPNP_DEFAULT_MANUFACTUR_NAME="nyatla.jp";
const static char* UPNP_DEFAULT_MANUFACTUR_URL="http://nyatla.jp/";
const static char* UPNP_DEFAULT_MODEL_NAME="MiMic UPnP device.";
const static char* STR_EMPTY="";
const static char* UPNP_DEFAULT_PRESENTATION_URL="/";
const static char* UPNP_DEFAULT_ICON_MIMETYPE="image/png";
const static char* UPNP_DEFAULT_ICON_URL="./icon.png";


/*
setUPnPFriendlyName
setUPnPManufactur(name,url,description)
setUPnPModel(name,number,url)
setUPnPPresentationUrl(name,number,url)
*/


//default mdns records
const static char* MDNS_SRV_HTTP="_http._tcp\0";
//const static char* MDNS_NAME="LPC176x(MiMic)\0";

extern "C" void mbed_mac_address(char *s);


static void overrideMacAddrIfmbed(NyLPC_TcNetConfig& v)
{
    mbed_mac_address((char*)(v.super.eth_mac.addr));
    //update default hostname
    strcpy(v.hostname,"MiMic");
    NyLPC_uitoa2(NyLPC_TcIPv4Config_getEtherMac000120203(&(v.super)),v.hostname+ 5,16,8);
    NyLPC_uitoa2((NyLPC_TcIPv4Config_getEtherMac0405xxxx(&(v.super))>>16)&0xffff,v.hostname+13,16,4);
    v.services.flags=NyLPC_TcNetConfig_SERVICE_FLAG_MDNS|NyLPC_TcNetConfig_SERVICE_FLAG_UPNP;
}

static void updateOnchipConfig(NyLPC_TcNetConfig& v)
{
    struct NyLPC_TMiMicConfigulation cfg_image;
    //パラメータ→ROMイメージ変換
    cfg_image.fast_boot=0xffffffff;
    cfg_image.mac_00_01_02_03=NyLPC_TcIPv4Config_getEtherMac000120203(&(v.super));
    cfg_image.mac_04_05_xx_xx=NyLPC_TcIPv4Config_getEtherMac0405xxxx(&(v.super));
    cfg_image.ipv4_addr_net  =NyLPC_ntohl(v.super.ip_addr.v);
    cfg_image.ipv4_mask_net  =NyLPC_ntohl(v.super.netmask.v);
    cfg_image.ipv4_drut_net  =NyLPC_ntohl(v.super.dr_addr.v);
    //additional information
    cfg_image.ipv4_flags=v.tcp_mode;
    cfg_image.http_port=v.services.http_port;
    cfg_image.srv_flags=v.services.flags;
    strcpy(cfg_image.hostname,v.hostname);
    //FreeRTOSの停止
    NyLPC_cIsr_enterCritical();
    //Flashへの書き込み
    NyLPC_cMiMicConfiglation_updateConfigulation(&cfg_image);
    //FreeRTOSの復帰
    NyLPC_cIsr_exitCritical();
}


namespace MiMic
{
NetConfig::NetConfig(bool i_is_factory_default)
{
    NyLPC_cNetConfig_initialize(&(this->_inst),i_is_factory_default);
    this->_ref_custom_dns_record=NULL;
    this->_ref_custom_upnp_desc=NULL;

    //check mbed
    if(PlatformInfo::getPlatformType()==PlatformInfo::PF_MBED) {
        //mbedチップからMACアドレスを取得
        if(!NyLPC_cMiMicConfiglation_hasUserConfigulation()) {
            //is 1st read?
            //mbed override
            overrideMacAddrIfmbed((this->_inst));
            //save
            updateOnchipConfig((this->_inst));
        } else {
            //2nd read
            if(i_is_factory_default) {
                //mbed override
                overrideMacAddrIfmbed((this->_inst));
            } else {
                //nothing to do
            }
        }
    }

    //updateUUID
    this->setUPnPUdn(0xe29f7100,0x4ba2,0x01e0,0);
    this->_upnp_desc.device_type=UPNP_DEVICE_TYPE;
    this->_upnp_desc.frendly_name=UPNP_DEFAULT_FRENDLY_NAME;
    this->_upnp_desc.manufacturer=UPNP_DEFAULT_MANUFACTUR_NAME;
    this->_upnp_desc.manufacturer_url=UPNP_DEFAULT_MANUFACTUR_URL;
    this->_upnp_desc.model_descriprion=STR_EMPTY;
    this->_upnp_desc.model_name=UPNP_DEFAULT_MODEL_NAME;
    this->_upnp_desc.model_number=NULL;
    this->_upnp_desc.model_url=NULL;
    this->_upnp_desc.serial_number=STR_EMPTY;
    this->_upnp_desc.udn=this->_udn;//pointer
    this->_upnp_desc.upc=NULL;
    this->_upnp_icon.width=32;
    this->_upnp_icon.height=32;
    this->_upnp_icon.depth=8;
    this->_upnp_icon.mimetype=UPNP_DEFAULT_ICON_MIMETYPE;
    this->_upnp_icon.url=UPNP_DEFAULT_ICON_URL;
    this->_upnp_desc.icons=&this->_upnp_icon;//pointer
    this->_upnp_desc.presentation_url=UPNP_DEFAULT_PRESENTATION_URL;
    this->_upnp_desc.number_of_devices=0;
    this->_upnp_desc.number_of_service=0;
    this->_upnp_desc.number_of_icon=1;
    this->_upnp_desc.devices=NULL;
    this->_upnp_desc.services=NULL;
    //mdns
    this->_dns_record.name=UPNP_DEFAULT_FRENDLY_NAME;
    this->_dns_record.a=this->_inst.hostname;//pointer
    this->_dns_record.num_of_srv=1;
    this->_srv_record.protocol=MDNS_SRV_HTTP;
    this->_srv_record.port=this->_inst.services.http_port;
    this->_dns_record.srv=&(this->_srv_record);

}

NetConfig::~NetConfig()
{
    NyLPC_cNetConfig_finalize(&(this->_inst));
}

const struct NyLPC_TUPnPDevDescDevice* NetConfig::refUPnPDevDesc()const
{
    return this->_ref_custom_upnp_desc!=NULL?this->_ref_custom_upnp_desc:&this->_upnp_desc;
}
/** internal dns record*/
const struct NyLPC_TDnsRecord* NetConfig::refMdnsRecord()const
{
    return this->_ref_custom_dns_record!=NULL?this->_ref_custom_dns_record:&this->_dns_record;
}


void NetConfig::setZeroconf(bool v)
{
    this->_inst.tcp_mode=(v?NyLPC_TcNetConfig_IPV4_FLAG_MODE_APIPA:NyLPC_TcNetConfig_IPV4_FLAG_MODE_MANUAL);
}
/**
  * Set IPv4 ip address to instance.
  */
void NetConfig::setIpAddr(unsigned char ip1,unsigned char ip2,unsigned char ip3,unsigned char ip4)
{
    NyLPC_TIPv4Addr_set(&(this->_inst.super.ip_addr),ip1,ip2,ip3,ip4);
}

void NetConfig::setIpAddr(const IpAddr& i_addr)
{
    this->_inst.super.ip_addr=i_addr.addr.v4;
}

/**
 * Set IPv4 network mask value to instance.
 */
void NetConfig::setNetMask(unsigned char ip1,unsigned char ip2,unsigned char ip3,unsigned char ip4)
{
    NyLPC_TIPv4Addr_set(&(this->_inst.super.netmask),ip1,ip2,ip3,ip4);
}
void NetConfig::setNetMask(const IpAddr& i_mask)
{
    this->_inst.super.netmask=i_mask.addr.v4;
}

/**
 * Set IPv4 default gateway address to instance.
 */
void NetConfig::setGateway(unsigned char ip1,unsigned char ip2,unsigned char ip3,unsigned char ip4)
{
    NyLPC_TIPv4Addr_set(&(this->_inst.super.dr_addr),ip1,ip2,ip3,ip4);
}
void NetConfig::setGateway(const IpAddr& i_addr)
{
    this->_inst.super.dr_addr=i_addr.addr.v4;
}

/**
 * Set ethernet mac address to instance.
 */
void NetConfig::setEmac(unsigned char a1,unsigned char a2,unsigned char a3,unsigned char a4,unsigned char a5,unsigned char a6)
{
    NyLPC_TEthAddr_set(&(this->_inst.super.eth_mac),a1,a2,a3,a4,a5,a6);
    //update only node field
    for(NyLPC_TInt16 i=0;i<6;i++){
        NyLPC_uitoa2(this->_inst.super.eth_mac.addr[i],&this->_udn[5+24+i*2],16,2);
    }
    
}
void NetConfig::setSrvHttpPort(unsigned short port)
{
    this->_srv_record.port=this->_inst.services.http_port=port;
}
void NetConfig::setSrvMdns(bool i_enable)
{
    if(i_enable) {
        this->_inst.services.flags|=NyLPC_TcNetConfig_SERVICE_FLAG_MDNS;
    } else {
        this->_inst.services.flags&=(~NyLPC_TcNetConfig_SERVICE_FLAG_MDNS);
    }
}
void NetConfig::setSrvUPnP(bool i_enable)
{
    if(i_enable) {
        this->_inst.services.flags|=NyLPC_TcNetConfig_SERVICE_FLAG_UPNP;
    } else {
        this->_inst.services.flags&=(~NyLPC_TcNetConfig_SERVICE_FLAG_UPNP);
    }
}

void NetConfig::setHostName(const char* i_hostname)
{
    this->setHostName(i_hostname,strlen(i_hostname));
}
void NetConfig::setHostName(const char* i_hostname,int len)
{
    int l=(len>(NyLPC_TcNetConfig_HOSTNAME_LEN-1))?NyLPC_TcNetConfig_HOSTNAME_LEN-1:len;
    memcpy(this->_inst.hostname,i_hostname,l);
    *(this->_inst.hostname+l)='\0';
}
const char* NetConfig::getHostName()
{
    return this->_inst.hostname;
}

void NetConfig::setFriendlyName(const char* i_name)
{
    this->_dns_record.name=this->_upnp_desc.frendly_name=(i_name==NULL?STR_EMPTY:i_name);
}
void NetConfig::setUPnPManufactur(const char* i_name,const char* i_url)
{
    this->_upnp_desc.manufacturer=i_name==NULL?STR_EMPTY:i_name;
    this->_upnp_desc.manufacturer_url=i_name==NULL?STR_EMPTY:i_url;
}
void NetConfig::setUPnPModel(const char* i_name,const char* i_number,const char* i_url,const char* i_description)
{
    this->_upnp_desc.model_descriprion=i_description==NULL?STR_EMPTY:i_description;
    this->_upnp_desc.model_name=i_name==NULL?STR_EMPTY:i_name;
    this->_upnp_desc.model_number=i_number;
    this->_upnp_desc.model_url=i_url;
}
void NetConfig::setUPnPSerialNumber(const char* i_number)
{
    this->_upnp_desc.serial_number=i_number;
}
void NetConfig::setUPnPUdn(unsigned long i_time_l,unsigned short i_time_m,unsigned short i_time_h,unsigned short i_sq)
{
    NyLPC_TcUuid_t uuid;
    NyLPC_cUuid_initialize(&uuid);
    NyLPC_cUuid_setTimeBase(&uuid,i_time_l,(i_time_h<<16)|i_time_m,i_sq,&(this->_inst.super.eth_mac));
    strcpy(this->_udn,"uuid:");
    NyLPC_cUuid_toString(&uuid,this->_udn+5);
    NyLPC_cUuid_finalize(&uuid);
}
void NetConfig::setUPnPIcon(unsigned short i_width,unsigned short i_height,unsigned short i_depth,const char* i_mimetype,const char* i_url)
{
    this->_upnp_icon.width=i_width;
    this->_upnp_icon.height=i_height;
    this->_upnp_icon.depth=i_depth;
    this->_upnp_icon.mimetype=i_mimetype;
    this->_upnp_icon.url=i_url;    
}

void NetConfig::setUPnPPresentationURL(const char* i_url)
{
    this->_upnp_desc.presentation_url=i_url;
}
void NetConfig::setCustomUPnPDescription(const struct NyLPC_TUPnPDevDescDevice* i_ref_description)
{
    this->_ref_custom_upnp_desc=i_ref_description;
}
void NetConfig::setCustomMdnsRecord(const struct NyLPC_TDnsRecord* i_ref_record)
{
    this->_ref_custom_dns_record=i_ref_record;
}

bool NetConfig::loadFromFile(const char* i_file)
{
#define NUMBER_OF_NAME 8
    const static char* tbl[]= {
        "macaddr",  //0
        "ipaddr",   //1
        "netmask",  //2
        "gateway",  //3
        "srv_http_port",//4
        "srv_mdns",//5
        "srv_upnp"  //6
        "host",//7
    };
    char tmp[32];
    union {
        unsigned char u8[6];
        NyLPC_TUInt32 u32;
    } v;
    const char* p;//pointer to read
    const char* key;
    const char* t;
    int l;
    FILE* fp = fopen(i_file,"r");
    if(fp==NULL) {
        return false;
    }
    //read from values
    while(fgets(tmp,31,fp)) {
        p=NyLPC_cFormatTextReader_seekSpace(tmp)+tmp;//skip space
        l=NyLPC_cFormatTextReader_readWord(p,&key);
        for(int i=0; i<NUMBER_OF_NAME; i++) {
            if(l>=0 && NyLPC_strnicmp(key,tbl[i],l)==0) {
                p+=l;//skip keyname
                p=NyLPC_cFormatTextReader_seekSpace(p)+p;//skip space
                if(*p!='=') {
                    break;//check equal
                }
                p++;
                //skip space
                p=NyLPC_cFormatTextReader_seekSpace(p)+p;//skip space
                switch(i) {
                    case 0://macaddr
                        if(NyLPC_cFormatTextReader_readMacAddr(p,v.u8)!=0) {
                            this->setEmac(v.u8[0],v.u8[1],v.u8[2],v.u8[3],v.u8[4],v.u8[5]);
                        }
                        break;
                    case 1://ipaddr
                        if(NyLPC_cFormatTextReader_readIpAddr(p,v.u8)!=0) {
                            this->setIpAddr(v.u8[0],v.u8[1],v.u8[2],v.u8[3]);
                            this->setZeroconf(false);
                        } else {
                            if(NyLPC_cFormatTextReader_readWord(p,&t)==4) {
                                if(NyLPC_strnicmp(t,"auto",4)==0) {
                                    this->setZeroconf(true);
                                }
                            }
                        }
                        break;
                    case 2:
                        if(NyLPC_cFormatTextReader_readIpAddr(p,v.u8)!=0) {
                            this->setNetMask(v.u8[0],v.u8[1],v.u8[2],v.u8[3]);
                        }
                        break;
                    case 3:
                        if(NyLPC_cFormatTextReader_readIpAddr(p,v.u8)!=0) {
                            this->setGateway(v.u8[0],v.u8[1],v.u8[2],v.u8[3]);
                        }
                        break;
                    case 4:
                        if(NyLPC_cFormatTextReader_readUInt(p,&(v.u32))!=0) {
                            this->setSrvHttpPort((unsigned short)v.u32);
                        }
                        break;
                    case 5:
                        l=NyLPC_cFormatTextReader_readWord(p,&t);
                        if((*t=='y' || *t=='Y')) {
                            this->setSrvMdns(true);
                        } else if((*t=='n' || *t=='N')) {
                            this->setSrvMdns(false);
                        }
                        break;
                    case 6:
                        l=NyLPC_cFormatTextReader_readWord(p,&t);
                        if((*t=='y' || *t=='Y')) {
                            this->setSrvUPnP(true);
                        } else if((*t=='n' || *t=='N')) {
                            this->setSrvUPnP(false);
                        }
                        break;
                    case 7:
                        l=NyLPC_cFormatTextReader_readWord(p,&t);
                        if(l>1) {
                            this->setHostName(t,l);
                        }
                        break;
                    default:
                        break;
                }
            }
        }
    }
    fclose(fp);
    return true;
}
};