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:
109:18f12ac01097
include????????; ???????????

Who changed what in which revision?

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