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:
Fri Sep 27 12:47:51 2013 +0000
Revision:
58:03b89038b21a
Parent:
51:45d42af201c2
Child:
70:2ed02b798004
add classes; checked:modwebsocket; unchecked:tcpsocket,udpsocket,httpclient.;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nyatla 3:0a94993be1f6 1 #include "NetConfig.h"
nyatla 11:c82a7b2899b0 2 #include "NyLPC_uip.h"
nyatla 27:eb66b517efe6 3 #include "NyLPC_flash.h"
nyatla 11:c82a7b2899b0 4 #include "NyLPC_uip_ethernet.h"
nyatla 29:e19aab0c4156 5 #include "utils/PlatformInfo.h"
nyatla 13:a71705c5e6fd 6 #include <stdio.h>
nyatla 13:a71705c5e6fd 7 #include <stdlib.h>
nyatla 13:a71705c5e6fd 8 #include "mbed.h"
nyatla 27:eb66b517efe6 9
nyatla 48:00d211aac2ec 10 /**
nyatla 48:00d211aac2ec 11 * Default UPnP Description
nyatla 48:00d211aac2ec 12 */
nyatla 48:00d211aac2ec 13 const static char* UPNP_DEVICE_TYPE ="urn:schemas-upnp-org:device:Basic:1";
nyatla 48:00d211aac2ec 14 const static char* UPNP_DEFAULT_FRENDLY_NAME="MiMic Device(LPC176x)";
nyatla 51:45d42af201c2 15 const static char* UPNP_DEFAULT_MANUFACTUR_NAME="nyatla.jp";
nyatla 48:00d211aac2ec 16 const static char* UPNP_DEFAULT_MANUFACTUR_URL="http://nyatla.jp/";
nyatla 48:00d211aac2ec 17 const static char* UPNP_DEFAULT_MODEL_NAME="MiMic UPnP device.";
nyatla 48:00d211aac2ec 18 const static char* STR_EMPTY="";
nyatla 48:00d211aac2ec 19 const static char* UPNP_DEFAULT_PRESENTATION_URL="/";
nyatla 50:35e765380772 20 const static char* UPNP_DEFAULT_ICON_MIMETYPE="image/png";
nyatla 50:35e765380772 21 const static char* UPNP_DEFAULT_ICON_URL="./icon.png";
nyatla 48:00d211aac2ec 22
nyatla 48:00d211aac2ec 23
nyatla 48:00d211aac2ec 24 /*
nyatla 48:00d211aac2ec 25 setUPnPFriendlyName
nyatla 48:00d211aac2ec 26 setUPnPManufactur(name,url,description)
nyatla 48:00d211aac2ec 27 setUPnPModel(name,number,url)
nyatla 48:00d211aac2ec 28 setUPnPPresentationUrl(name,number,url)
nyatla 48:00d211aac2ec 29 */
nyatla 48:00d211aac2ec 30
nyatla 48:00d211aac2ec 31
nyatla 37:fc4b4fd6a649 32 //default mdns records
nyatla 37:fc4b4fd6a649 33 const static char* MDNS_SRV_HTTP="_http._tcp\0";
nyatla 48:00d211aac2ec 34 //const static char* MDNS_NAME="LPC176x(MiMic)\0";
nyatla 37:fc4b4fd6a649 35
nyatla 27:eb66b517efe6 36 extern "C" void mbed_mac_address(char *s);
nyatla 37:fc4b4fd6a649 37
nyatla 37:fc4b4fd6a649 38
nyatla 37:fc4b4fd6a649 39 static void overrideMacAddrIfmbed(NyLPC_TcNetConfig& v)
nyatla 27:eb66b517efe6 40 {
nyatla 37:fc4b4fd6a649 41 mbed_mac_address((char*)(v.super.eth_mac.addr));
nyatla 37:fc4b4fd6a649 42 //update default hostname
nyatla 37:fc4b4fd6a649 43 strcpy(v.hostname,"MiMic");
nyatla 48:00d211aac2ec 44 NyLPC_uitoa2(NyLPC_TcIPv4Config_getEtherMac000120203(&(v.super)),v.hostname+ 5,16,8);
nyatla 48:00d211aac2ec 45 NyLPC_uitoa2((NyLPC_TcIPv4Config_getEtherMac0405xxxx(&(v.super))>>16)&0xffff,v.hostname+13,16,4);
nyatla 51:45d42af201c2 46 v.services.flags=NyLPC_TcNetConfig_SERVICE_FLAG_MDNS|NyLPC_TcNetConfig_SERVICE_FLAG_UPNP;
nyatla 37:fc4b4fd6a649 47 }
nyatla 37:fc4b4fd6a649 48
nyatla 37:fc4b4fd6a649 49 static void updateOnchipConfig(NyLPC_TcNetConfig& v)
nyatla 48:00d211aac2ec 50 {
nyatla 48:00d211aac2ec 51 struct NyLPC_TMiMicConfigulation cfg_image;
nyatla 27:eb66b517efe6 52 //パラメータ→ROMイメージ変換
nyatla 27:eb66b517efe6 53 cfg_image.fast_boot=0xffffffff;
nyatla 37:fc4b4fd6a649 54 cfg_image.mac_00_01_02_03=NyLPC_TcIPv4Config_getEtherMac000120203(&(v.super));
nyatla 37:fc4b4fd6a649 55 cfg_image.mac_04_05_xx_xx=NyLPC_TcIPv4Config_getEtherMac0405xxxx(&(v.super));
nyatla 37:fc4b4fd6a649 56 cfg_image.ipv4_addr_net =NyLPC_ntohl(v.super.ip_addr.v);
nyatla 37:fc4b4fd6a649 57 cfg_image.ipv4_mask_net =NyLPC_ntohl(v.super.netmask.v);
nyatla 37:fc4b4fd6a649 58 cfg_image.ipv4_drut_net =NyLPC_ntohl(v.super.dr_addr.v);
nyatla 37:fc4b4fd6a649 59 //additional information
nyatla 37:fc4b4fd6a649 60 cfg_image.ipv4_flags=v.tcp_mode;
nyatla 37:fc4b4fd6a649 61 cfg_image.http_port=v.services.http_port;
nyatla 37:fc4b4fd6a649 62 cfg_image.srv_flags=v.services.flags;
nyatla 37:fc4b4fd6a649 63 strcpy(cfg_image.hostname,v.hostname);
nyatla 27:eb66b517efe6 64 //FreeRTOSの停止
nyatla 27:eb66b517efe6 65 NyLPC_cIsr_enterCritical();
nyatla 27:eb66b517efe6 66 //Flashへの書き込み
nyatla 27:eb66b517efe6 67 NyLPC_cMiMicConfiglation_updateConfigulation(&cfg_image);
nyatla 27:eb66b517efe6 68 //FreeRTOSの復帰
nyatla 27:eb66b517efe6 69 NyLPC_cIsr_exitCritical();
nyatla 27:eb66b517efe6 70 }
nyatla 37:fc4b4fd6a649 71
nyatla 37:fc4b4fd6a649 72
nyatla 3:0a94993be1f6 73 namespace MiMic
nyatla 3:0a94993be1f6 74 {
nyatla 48:00d211aac2ec 75 NetConfig::NetConfig(bool i_is_factory_default)
nyatla 48:00d211aac2ec 76 {
nyatla 48:00d211aac2ec 77 NyLPC_cNetConfig_initialize(&(this->_inst),i_is_factory_default);
nyatla 48:00d211aac2ec 78 this->_ref_custom_dns_record=NULL;
nyatla 48:00d211aac2ec 79 this->_ref_custom_upnp_desc=NULL;
nyatla 27:eb66b517efe6 80
nyatla 48:00d211aac2ec 81 //check mbed
nyatla 48:00d211aac2ec 82 if(PlatformInfo::getPlatformType()!=PlatformInfo::PF_MBED) {
nyatla 48:00d211aac2ec 83 return;
nyatla 3:0a94993be1f6 84 }
nyatla 37:fc4b4fd6a649 85
nyatla 48:00d211aac2ec 86 if(!NyLPC_cMiMicConfiglation_hasUserConfigulation()) {
nyatla 48:00d211aac2ec 87 //is 1st read?
nyatla 48:00d211aac2ec 88 //mbed override
nyatla 48:00d211aac2ec 89 overrideMacAddrIfmbed((this->_inst));
nyatla 48:00d211aac2ec 90 //save
nyatla 48:00d211aac2ec 91 updateOnchipConfig((this->_inst));
nyatla 48:00d211aac2ec 92 } else {
nyatla 48:00d211aac2ec 93 //2nd read
nyatla 48:00d211aac2ec 94 if(i_is_factory_default) {
nyatla 48:00d211aac2ec 95 //mbed override
nyatla 48:00d211aac2ec 96 overrideMacAddrIfmbed((this->_inst));
nyatla 48:00d211aac2ec 97 } else {
nyatla 48:00d211aac2ec 98 //nothing to do
nyatla 37:fc4b4fd6a649 99 }
nyatla 37:fc4b4fd6a649 100 }
nyatla 48:00d211aac2ec 101 //updateUUID
nyatla 50:35e765380772 102 this->setUPnPUdn(0xe29f7100,0x4ba2,0x01e0,0);
nyatla 48:00d211aac2ec 103 this->_upnp_desc.device_type=UPNP_DEVICE_TYPE;
nyatla 48:00d211aac2ec 104 this->_upnp_desc.frendly_name=UPNP_DEFAULT_FRENDLY_NAME;
nyatla 48:00d211aac2ec 105 this->_upnp_desc.manufacturer=UPNP_DEFAULT_MANUFACTUR_NAME;
nyatla 48:00d211aac2ec 106 this->_upnp_desc.manufacturer_url=UPNP_DEFAULT_MANUFACTUR_URL;
nyatla 48:00d211aac2ec 107 this->_upnp_desc.model_descriprion=STR_EMPTY;
nyatla 48:00d211aac2ec 108 this->_upnp_desc.model_name=UPNP_DEFAULT_MODEL_NAME;
nyatla 48:00d211aac2ec 109 this->_upnp_desc.model_number=NULL;
nyatla 48:00d211aac2ec 110 this->_upnp_desc.model_url=NULL;
nyatla 48:00d211aac2ec 111 this->_upnp_desc.serial_number=STR_EMPTY;
nyatla 50:35e765380772 112 this->_upnp_desc.udn=this->_udn;//pointer
nyatla 48:00d211aac2ec 113 this->_upnp_desc.upc=NULL;
nyatla 50:35e765380772 114 this->_upnp_icon.width=32;
nyatla 50:35e765380772 115 this->_upnp_icon.height=32;
nyatla 50:35e765380772 116 this->_upnp_icon.depth=8;
nyatla 50:35e765380772 117 this->_upnp_icon.mimetype=UPNP_DEFAULT_ICON_MIMETYPE;
nyatla 50:35e765380772 118 this->_upnp_icon.url=UPNP_DEFAULT_ICON_URL;
nyatla 50:35e765380772 119 this->_upnp_desc.icons=&this->_upnp_icon;//pointer
nyatla 48:00d211aac2ec 120 this->_upnp_desc.presentation_url=UPNP_DEFAULT_PRESENTATION_URL;
nyatla 48:00d211aac2ec 121 this->_upnp_desc.number_of_devices=0;
nyatla 48:00d211aac2ec 122 this->_upnp_desc.number_of_service=0;
nyatla 50:35e765380772 123 this->_upnp_desc.number_of_icon=1;
nyatla 48:00d211aac2ec 124 this->_upnp_desc.devices=NULL;
nyatla 48:00d211aac2ec 125 this->_upnp_desc.services=NULL;
nyatla 50:35e765380772 126 //mdns
nyatla 50:35e765380772 127 this->_dns_record.name=UPNP_DEFAULT_FRENDLY_NAME;
nyatla 50:35e765380772 128 this->_dns_record.a=this->_inst.hostname;//pointer
nyatla 50:35e765380772 129 this->_dns_record.num_of_srv=1;
nyatla 50:35e765380772 130 this->_srv_record.protocol=MDNS_SRV_HTTP;
nyatla 50:35e765380772 131 this->_srv_record.port=this->_inst.services.http_port;
nyatla 50:35e765380772 132 this->_dns_record.srv=&(this->_srv_record);
nyatla 50:35e765380772 133
nyatla 48:00d211aac2ec 134 }
nyatla 48:00d211aac2ec 135
nyatla 48:00d211aac2ec 136 NetConfig::~NetConfig()
nyatla 48:00d211aac2ec 137 {
nyatla 48:00d211aac2ec 138 NyLPC_cNetConfig_finalize(&(this->_inst));
nyatla 48:00d211aac2ec 139 }
nyatla 48:00d211aac2ec 140
nyatla 48:00d211aac2ec 141 const struct NyLPC_TUPnPDevDescDevice* NetConfig::refUPnPDevDesc()const
nyatla 48:00d211aac2ec 142 {
nyatla 48:00d211aac2ec 143 return this->_ref_custom_upnp_desc!=NULL?this->_ref_custom_upnp_desc:&this->_upnp_desc;
nyatla 48:00d211aac2ec 144 }
nyatla 48:00d211aac2ec 145 /** internal dns record*/
nyatla 48:00d211aac2ec 146 const struct NyLPC_TDnsRecord* NetConfig::refMdnsRecord()const
nyatla 48:00d211aac2ec 147 {
nyatla 48:00d211aac2ec 148 return this->_ref_custom_dns_record!=NULL?this->_ref_custom_dns_record:&this->_dns_record;
nyatla 48:00d211aac2ec 149 }
nyatla 48:00d211aac2ec 150
nyatla 48:00d211aac2ec 151
nyatla 48:00d211aac2ec 152 void NetConfig::setZeroconf(bool v)
nyatla 48:00d211aac2ec 153 {
nyatla 48:00d211aac2ec 154 this->_inst.tcp_mode=(v?NyLPC_TcNetConfig_IPV4_FLAG_MODE_APIPA:NyLPC_TcNetConfig_IPV4_FLAG_MODE_MANUAL);
nyatla 48:00d211aac2ec 155 }
nyatla 48:00d211aac2ec 156 /**
nyatla 48:00d211aac2ec 157 * Set IPv4 ip address to instance.
nyatla 48:00d211aac2ec 158 */
nyatla 48:00d211aac2ec 159 void NetConfig::setIpAddr(unsigned char ip1,unsigned char ip2,unsigned char ip3,unsigned char ip4)
nyatla 48:00d211aac2ec 160 {
nyatla 48:00d211aac2ec 161 NyLPC_TIPv4Addr_set(&(this->_inst.super.ip_addr),ip1,ip2,ip3,ip4);
nyatla 48:00d211aac2ec 162 }
nyatla 58:03b89038b21a 163
nyatla 58:03b89038b21a 164 void NetConfig::setIpAddr(const IpAddr& i_addr)
nyatla 58:03b89038b21a 165 {
nyatla 58:03b89038b21a 166 this->_inst.super.ip_addr=i_addr.addr.v4;
nyatla 58:03b89038b21a 167 }
nyatla 58:03b89038b21a 168
nyatla 48:00d211aac2ec 169 /**
nyatla 48:00d211aac2ec 170 * Set IPv4 network mask value to instance.
nyatla 48:00d211aac2ec 171 */
nyatla 48:00d211aac2ec 172 void NetConfig::setNetMask(unsigned char ip1,unsigned char ip2,unsigned char ip3,unsigned char ip4)
nyatla 48:00d211aac2ec 173 {
nyatla 48:00d211aac2ec 174 NyLPC_TIPv4Addr_set(&(this->_inst.super.netmask),ip1,ip2,ip3,ip4);
nyatla 48:00d211aac2ec 175 }
nyatla 58:03b89038b21a 176 void NetConfig::setNetMask(const IpAddr& i_mask)
nyatla 58:03b89038b21a 177 {
nyatla 58:03b89038b21a 178 this->_inst.super.netmask=i_mask.addr.v4;
nyatla 58:03b89038b21a 179 }
nyatla 58:03b89038b21a 180
nyatla 48:00d211aac2ec 181 /**
nyatla 48:00d211aac2ec 182 * Set IPv4 default gateway address to instance.
nyatla 48:00d211aac2ec 183 */
nyatla 48:00d211aac2ec 184 void NetConfig::setGateway(unsigned char ip1,unsigned char ip2,unsigned char ip3,unsigned char ip4)
nyatla 48:00d211aac2ec 185 {
nyatla 48:00d211aac2ec 186 NyLPC_TIPv4Addr_set(&(this->_inst.super.dr_addr),ip1,ip2,ip3,ip4);
nyatla 48:00d211aac2ec 187 }
nyatla 58:03b89038b21a 188 void NetConfig::setGateway(const IpAddr& i_addr)
nyatla 58:03b89038b21a 189 {
nyatla 58:03b89038b21a 190 this->_inst.super.dr_addr=i_addr.addr.v4;
nyatla 58:03b89038b21a 191 }
nyatla 58:03b89038b21a 192
nyatla 48:00d211aac2ec 193 /**
nyatla 48:00d211aac2ec 194 * Set ethernet mac address to instance.
nyatla 48:00d211aac2ec 195 */
nyatla 48:00d211aac2ec 196 void NetConfig::setEmac(unsigned char a1,unsigned char a2,unsigned char a3,unsigned char a4,unsigned char a5,unsigned char a6)
nyatla 48:00d211aac2ec 197 {
nyatla 48:00d211aac2ec 198 NyLPC_TEthAddr_set(&(this->_inst.super.eth_mac),a1,a2,a3,a4,a5,a6);
nyatla 48:00d211aac2ec 199 //update only node field
nyatla 48:00d211aac2ec 200 for(NyLPC_TInt16 i=0;i<6;i++){
nyatla 48:00d211aac2ec 201 NyLPC_uitoa2(this->_inst.super.eth_mac.addr[i],&this->_udn[5+24+i*2],16,2);
nyatla 37:fc4b4fd6a649 202 }
nyatla 37:fc4b4fd6a649 203
nyatla 48:00d211aac2ec 204 }
nyatla 48:00d211aac2ec 205 void NetConfig::setSrvHttpPort(unsigned short port)
nyatla 48:00d211aac2ec 206 {
nyatla 50:35e765380772 207 this->_srv_record.port=this->_inst.services.http_port=port;
nyatla 48:00d211aac2ec 208 }
nyatla 48:00d211aac2ec 209 void NetConfig::setSrvMdns(bool i_enable)
nyatla 48:00d211aac2ec 210 {
nyatla 48:00d211aac2ec 211 if(i_enable) {
nyatla 48:00d211aac2ec 212 this->_inst.services.flags|=NyLPC_TcNetConfig_SERVICE_FLAG_MDNS;
nyatla 48:00d211aac2ec 213 } else {
nyatla 48:00d211aac2ec 214 this->_inst.services.flags&=(~NyLPC_TcNetConfig_SERVICE_FLAG_MDNS);
nyatla 48:00d211aac2ec 215 }
nyatla 48:00d211aac2ec 216 }
nyatla 48:00d211aac2ec 217 void NetConfig::setSrvUPnP(bool i_enable)
nyatla 48:00d211aac2ec 218 {
nyatla 48:00d211aac2ec 219 if(i_enable) {
nyatla 48:00d211aac2ec 220 this->_inst.services.flags|=NyLPC_TcNetConfig_SERVICE_FLAG_UPNP;
nyatla 48:00d211aac2ec 221 } else {
nyatla 48:00d211aac2ec 222 this->_inst.services.flags&=(~NyLPC_TcNetConfig_SERVICE_FLAG_UPNP);
nyatla 48:00d211aac2ec 223 }
nyatla 48:00d211aac2ec 224 }
nyatla 48:00d211aac2ec 225
nyatla 48:00d211aac2ec 226 void NetConfig::setHostName(const char* i_hostname)
nyatla 48:00d211aac2ec 227 {
nyatla 48:00d211aac2ec 228 this->setHostName(i_hostname,strlen(i_hostname));
nyatla 48:00d211aac2ec 229 }
nyatla 48:00d211aac2ec 230 void NetConfig::setHostName(const char* i_hostname,int len)
nyatla 48:00d211aac2ec 231 {
nyatla 48:00d211aac2ec 232 int l=(len>(NyLPC_TcNetConfig_HOSTNAME_LEN-1))?NyLPC_TcNetConfig_HOSTNAME_LEN-1:len;
nyatla 48:00d211aac2ec 233 memcpy(this->_inst.hostname,i_hostname,l);
nyatla 48:00d211aac2ec 234 *(this->_inst.hostname+l)='\0';
nyatla 48:00d211aac2ec 235 }
nyatla 48:00d211aac2ec 236 void NetConfig::setFriendlyName(const char* i_name)
nyatla 48:00d211aac2ec 237 {
nyatla 48:00d211aac2ec 238 this->_dns_record.name=this->_upnp_desc.frendly_name=(i_name==NULL?STR_EMPTY:i_name);
nyatla 48:00d211aac2ec 239 }
nyatla 48:00d211aac2ec 240 void NetConfig::setUPnPManufactur(const char* i_name,const char* i_url)
nyatla 48:00d211aac2ec 241 {
nyatla 48:00d211aac2ec 242 this->_upnp_desc.manufacturer=i_name==NULL?STR_EMPTY:i_name;
nyatla 48:00d211aac2ec 243 this->_upnp_desc.manufacturer_url=i_name==NULL?STR_EMPTY:i_url;
nyatla 48:00d211aac2ec 244 }
nyatla 48:00d211aac2ec 245 void NetConfig::setUPnPModel(const char* i_name,const char* i_number,const char* i_url,const char* i_description)
nyatla 48:00d211aac2ec 246 {
nyatla 48:00d211aac2ec 247 this->_upnp_desc.model_descriprion=i_description==NULL?STR_EMPTY:i_description;
nyatla 48:00d211aac2ec 248 this->_upnp_desc.model_name=i_name==NULL?STR_EMPTY:i_name;
nyatla 48:00d211aac2ec 249 this->_upnp_desc.model_number=i_number;
nyatla 48:00d211aac2ec 250 this->_upnp_desc.model_url=i_url;
nyatla 48:00d211aac2ec 251 }
nyatla 48:00d211aac2ec 252 void NetConfig::setUPnPSerialNumber(const char* i_number)
nyatla 48:00d211aac2ec 253 {
nyatla 48:00d211aac2ec 254 this->_upnp_desc.serial_number=i_number;
nyatla 48:00d211aac2ec 255 }
nyatla 48:00d211aac2ec 256 void NetConfig::setUPnPUdn(unsigned long i_time_l,unsigned short i_time_m,unsigned short i_time_h,unsigned short i_sq)
nyatla 48:00d211aac2ec 257 {
nyatla 48:00d211aac2ec 258 NyLPC_TcUuid_t uuid;
nyatla 48:00d211aac2ec 259 NyLPC_cUuid_initialize(&uuid);
nyatla 48:00d211aac2ec 260 NyLPC_cUuid_setTimeBase(&uuid,i_time_l,(i_time_h<<16)|i_time_m,i_sq,&(this->_inst.super.eth_mac));
nyatla 48:00d211aac2ec 261 strcpy(this->_udn,"uuid:");
nyatla 48:00d211aac2ec 262 NyLPC_cUuid_toString(&uuid,this->_udn+5);
nyatla 49:d4334acc3ec3 263 NyLPC_cUuid_finalize(&uuid);
nyatla 48:00d211aac2ec 264 }
nyatla 50:35e765380772 265 void NetConfig::setUPnPIcon(unsigned short i_width,unsigned short i_height,unsigned short i_depth,const char* i_mimetype,const char* i_url)
nyatla 50:35e765380772 266 {
nyatla 50:35e765380772 267 this->_upnp_icon.width=i_width;
nyatla 50:35e765380772 268 this->_upnp_icon.height=i_height;
nyatla 50:35e765380772 269 this->_upnp_icon.depth=i_depth;
nyatla 50:35e765380772 270 this->_upnp_icon.mimetype=i_mimetype;
nyatla 50:35e765380772 271 this->_upnp_icon.url=i_url;
nyatla 50:35e765380772 272 }
nyatla 48:00d211aac2ec 273
nyatla 48:00d211aac2ec 274 void NetConfig::setUPnPPresentationURL(const char* i_url)
nyatla 48:00d211aac2ec 275 {
nyatla 48:00d211aac2ec 276 this->_upnp_desc.presentation_url=i_url;
nyatla 48:00d211aac2ec 277 }
nyatla 48:00d211aac2ec 278 void NetConfig::setCustomUPnPDescription(const struct NyLPC_TUPnPDevDescDevice* i_ref_description)
nyatla 48:00d211aac2ec 279 {
nyatla 48:00d211aac2ec 280 this->_ref_custom_upnp_desc=i_ref_description;
nyatla 48:00d211aac2ec 281 }
nyatla 48:00d211aac2ec 282 void NetConfig::setCustomMdnsRecord(const struct NyLPC_TDnsRecord* i_ref_record)
nyatla 48:00d211aac2ec 283 {
nyatla 48:00d211aac2ec 284 this->_ref_custom_dns_record=i_ref_record;
nyatla 48:00d211aac2ec 285 }
nyatla 48:00d211aac2ec 286
nyatla 48:00d211aac2ec 287 bool NetConfig::loadFromFile(const char* i_file)
nyatla 48:00d211aac2ec 288 {
nyatla 51:45d42af201c2 289 #define NUMBER_OF_NAME 8
nyatla 48:00d211aac2ec 290 const static char* tbl[]= {
nyatla 48:00d211aac2ec 291 "macaddr", //0
nyatla 48:00d211aac2ec 292 "ipaddr", //1
nyatla 48:00d211aac2ec 293 "netmask", //2
nyatla 48:00d211aac2ec 294 "gateway", //3
nyatla 48:00d211aac2ec 295 "srv_http_port",//4
nyatla 48:00d211aac2ec 296 "srv_mdns",//5
nyatla 51:45d42af201c2 297 "srv_upnp" //6
nyatla 51:45d42af201c2 298 "host",//7
nyatla 48:00d211aac2ec 299 };
nyatla 48:00d211aac2ec 300 char tmp[32];
nyatla 48:00d211aac2ec 301 union {
nyatla 48:00d211aac2ec 302 unsigned char u8[6];
nyatla 48:00d211aac2ec 303 NyLPC_TUInt32 u32;
nyatla 48:00d211aac2ec 304 } v;
nyatla 48:00d211aac2ec 305 const char* p;//pointer to read
nyatla 48:00d211aac2ec 306 const char* key;
nyatla 48:00d211aac2ec 307 const char* t;
nyatla 48:00d211aac2ec 308 int l;
nyatla 48:00d211aac2ec 309 FILE* fp = fopen(i_file,"r");
nyatla 48:00d211aac2ec 310 if(fp==NULL) {
nyatla 48:00d211aac2ec 311 return false;
nyatla 48:00d211aac2ec 312 }
nyatla 48:00d211aac2ec 313 //read from values
nyatla 48:00d211aac2ec 314 while(fgets(tmp,31,fp)) {
nyatla 48:00d211aac2ec 315 p=NyLPC_cFormatTextReader_seekSpace(tmp)+tmp;//skip space
nyatla 48:00d211aac2ec 316 l=NyLPC_cFormatTextReader_readWord(p,&key);
nyatla 51:45d42af201c2 317 for(int i=0; i<NUMBER_OF_NAME; i++) {
nyatla 48:00d211aac2ec 318 if(l>=0 && NyLPC_strnicmp(key,tbl[i],l)==0) {
nyatla 48:00d211aac2ec 319 p+=l;//skip keyname
nyatla 48:00d211aac2ec 320 p=NyLPC_cFormatTextReader_seekSpace(p)+p;//skip space
nyatla 48:00d211aac2ec 321 if(*p!='=') {
nyatla 48:00d211aac2ec 322 break;//check equal
nyatla 48:00d211aac2ec 323 }
nyatla 48:00d211aac2ec 324 p++;
nyatla 48:00d211aac2ec 325 //skip space
nyatla 48:00d211aac2ec 326 p=NyLPC_cFormatTextReader_seekSpace(p)+p;//skip space
nyatla 48:00d211aac2ec 327 switch(i) {
nyatla 37:fc4b4fd6a649 328 case 0://macaddr
nyatla 48:00d211aac2ec 329 if(NyLPC_cFormatTextReader_readMacAddr(p,v.u8)!=0) {
nyatla 37:fc4b4fd6a649 330 this->setEmac(v.u8[0],v.u8[1],v.u8[2],v.u8[3],v.u8[4],v.u8[5]);
nyatla 13:a71705c5e6fd 331 }
nyatla 13:a71705c5e6fd 332 break;
nyatla 37:fc4b4fd6a649 333 case 1://ipaddr
nyatla 48:00d211aac2ec 334 if(NyLPC_cFormatTextReader_readIpAddr(p,v.u8)!=0) {
nyatla 37:fc4b4fd6a649 335 this->setIpAddr(v.u8[0],v.u8[1],v.u8[2],v.u8[3]);
nyatla 37:fc4b4fd6a649 336 this->setZeroconf(false);
nyatla 48:00d211aac2ec 337 } else {
nyatla 48:00d211aac2ec 338 if(NyLPC_cFormatTextReader_readWord(p,&t)==4) {
nyatla 48:00d211aac2ec 339 if(NyLPC_strnicmp(t,"auto",4)==0) {
nyatla 37:fc4b4fd6a649 340 this->setZeroconf(true);
nyatla 37:fc4b4fd6a649 341 }
nyatla 37:fc4b4fd6a649 342 }
nyatla 13:a71705c5e6fd 343 }
nyatla 13:a71705c5e6fd 344 break;
nyatla 13:a71705c5e6fd 345 case 2:
nyatla 48:00d211aac2ec 346 if(NyLPC_cFormatTextReader_readIpAddr(p,v.u8)!=0) {
nyatla 37:fc4b4fd6a649 347 this->setNetMask(v.u8[0],v.u8[1],v.u8[2],v.u8[3]);
nyatla 13:a71705c5e6fd 348 }
nyatla 13:a71705c5e6fd 349 break;
nyatla 13:a71705c5e6fd 350 case 3:
nyatla 48:00d211aac2ec 351 if(NyLPC_cFormatTextReader_readIpAddr(p,v.u8)!=0) {
nyatla 37:fc4b4fd6a649 352 this->setGateway(v.u8[0],v.u8[1],v.u8[2],v.u8[3]);
nyatla 37:fc4b4fd6a649 353 }
nyatla 37:fc4b4fd6a649 354 break;
nyatla 37:fc4b4fd6a649 355 case 4:
nyatla 48:00d211aac2ec 356 if(NyLPC_cFormatTextReader_readUInt(p,&(v.u32))!=0) {
nyatla 37:fc4b4fd6a649 357 this->setSrvHttpPort((unsigned short)v.u32);
nyatla 37:fc4b4fd6a649 358 }
nyatla 37:fc4b4fd6a649 359 break;
nyatla 37:fc4b4fd6a649 360 case 5:
nyatla 37:fc4b4fd6a649 361 l=NyLPC_cFormatTextReader_readWord(p,&t);
nyatla 48:00d211aac2ec 362 if((*t=='y' || *t=='Y')) {
nyatla 37:fc4b4fd6a649 363 this->setSrvMdns(true);
nyatla 48:00d211aac2ec 364 } else if((*t=='n' || *t=='N')) {
nyatla 37:fc4b4fd6a649 365 this->setSrvMdns(false);
nyatla 37:fc4b4fd6a649 366 }
nyatla 37:fc4b4fd6a649 367 break;
nyatla 37:fc4b4fd6a649 368 case 6:
nyatla 37:fc4b4fd6a649 369 l=NyLPC_cFormatTextReader_readWord(p,&t);
nyatla 51:45d42af201c2 370 if((*t=='y' || *t=='Y')) {
nyatla 51:45d42af201c2 371 this->setSrvUPnP(true);
nyatla 51:45d42af201c2 372 } else if((*t=='n' || *t=='N')) {
nyatla 51:45d42af201c2 373 this->setSrvUPnP(false);
nyatla 51:45d42af201c2 374 }
nyatla 51:45d42af201c2 375 break;
nyatla 51:45d42af201c2 376 case 7:
nyatla 51:45d42af201c2 377 l=NyLPC_cFormatTextReader_readWord(p,&t);
nyatla 48:00d211aac2ec 378 if(l>1) {
nyatla 37:fc4b4fd6a649 379 this->setHostName(t,l);
nyatla 13:a71705c5e6fd 380 }
nyatla 13:a71705c5e6fd 381 break;
nyatla 13:a71705c5e6fd 382 default:
nyatla 13:a71705c5e6fd 383 break;
nyatla 13:a71705c5e6fd 384 }
nyatla 13:a71705c5e6fd 385 }
nyatla 13:a71705c5e6fd 386 }
nyatla 37:fc4b4fd6a649 387 }
nyatla 48:00d211aac2ec 388 fclose(fp);
nyatla 48:00d211aac2ec 389 return true;
nyatla 48:00d211aac2ec 390 }
nyatla 3:0a94993be1f6 391 };