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

core/net/upnp/NyLPC_cSsdpSocket.c

Committer:
nyatla
Date:
2014-10-26
Revision:
100:8d45dab6dcf6
Parent:
57:bc4330dfa62f
Child:
109:18f12ac01097

File content as of revision 100:8d45dab6dcf6:

/*********************************************************************************
 * PROJECT: MiMic
 * --------------------------------------------------------------------------------
 *
 * This file is part of MiMic
 * Copyright (C)2011 Ryo Iizuka
 *
 * MiMic is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * For further information please contact.
 *	http://nyatla.jp/
 *	<airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>
 *
 *********************************************************************************/
#include "NyLPC_cSsdpSocket.h"
#include "NyLPC_http.h"
#include "NyLPC_uipService.h"
#include "../uip/NyLPC_cUipService_protected.h"

#include <stdio.h>
#include <string.h>





#define HTTP_SP 0x20

#define PARSE_NULL  0
#define PARSE_ST	0x01
#define PARSE_MAN	0x11
#define PARSE_UNKNOWN 0xff

static const struct NyLPC_TIPv4Addr SSDP_MCAST_IPADDR=NyLPC_TIPv4Addr_pack(239,255,255,250);
static const char* STR_UPNP_ROOT_DEVICE="upnp:rootdevice";

struct TMSearchHeader
{
	struct NyLPC_THttpBasicHeader super;

	const struct NyLPC_TUPnPDeviceRecord* _ref_devices;
	/**
	 * パーサのステータス
	 */
	NyLPC_TUInt8 st;
	/**
	 * メモリ位置
	 */
	const NyLPC_TChar* _rpos;
	struct{
		const NyLPC_TChar* st_str;
		const NyLPC_TChar* man_str;
		NyLPC_TUInt16 st_len;
		NyLPC_TUInt16 man_len;
	}result;
};

//とりあえずprivate
void NyLPC_cSsdpSocket_notify(NyLPC_TcSsdpSocket_t* i_inst);


static NyLPC_TBool urlHandler(NyLPC_TcHttpBasicHeaderParser_t* i_inst,NyLPC_TChar i_c,struct NyLPC_THttpBasicHeader* o_out)
{
// *であるかを確認 未実装
	return NyLPC_TBool_TRUE;
}

#define TIMEOUT_IN_MS 100

/**
 * SERVER MessageHeaderの値
 * 40文字以内であること。
 */
#define SERVER_MESSAGE_HEADER "MiMic/1.4 UPnP/1.0 MiMicUPnP/0.2"


/**
 * MsearchResponseを格納したTxパケットをAllocする。
 * @param i_st
 * ST値
 * @param i_udn
 * DDESCのUDNの値
 * @param i_usn
 * USNのサフィックスパラメータ
 * @return
 * MsearchResponseを格納したTXメモリ
 */
static void* allocMsearchResponeTx(
	NyLPC_TcSsdpSocket_t* i_inst,
	const NyLPC_TChar* i_st,
	const NyLPC_TChar* i_udn,
	const NyLPC_TChar* i_usn,
	NyLPC_TUInt16 i_st_len,
	NyLPC_TInt16* o_len)
{
	NyLPC_TChar* obuf;
	NyLPC_TUInt16 l;
	NyLPC_TUInt16 len_usn=(NyLPC_TUInt16)((i_usn!=NULL)?strlen(i_usn):0);
	NyLPC_TUInt16 len_udn=(NyLPC_TUInt16)strlen(i_udn);
	NyLPC_TUInt16 len_location=(NyLPC_TUInt16)strlen(i_inst->location_path);

	//	//161Byte
	//	"HTTP/1.1 200 OK\r\n" 							//15+2=17
	//	"CACHE-CONTROL: max-age = nnnn\r\n" 			//29+2=31
	//	"SERVER: [:40byte:]\r\n"						//8+40+2=50
	//	"EXT: \r\n" 									//5+2 = 7
	//	"LOCATION: http://xxx.xxx.xxx.xxx:nnnnn/%s/d.xml\r\n"	//34+2=46
	//	"USN: %s%s\r\n"									//5+2=7
	//	"ST: %s\r\n\r\n"								//4+4=8
	l=166+len_location+len_usn+len_udn+i_st_len;
	obuf=NyLPC_cUdpSocket_allocSendBuf(&(i_inst->super),l,&l,TIMEOUT_IN_MS);

	if(obuf==NULL){
		return NULL;
	}
	//必要なメモリサイズを確保できた?
	if(l<161+len_location+len_usn+len_udn+i_st_len)
	{
		NyLPC_cUdpSocket_releaseSendBuf(&i_inst->super,obuf);
		return NULL;
	}
	//ワーク変数lの再初期化
	l=0;
	strcpy(obuf,
		"HTTP/1.1 200 OK\r\n"
		"CACHE-CONTROL: max-age = 300\r\n"
		"SERVER: "SERVER_MESSAGE_HEADER"\r\n"
		"EXT: \r\n"
		"LOCATION: http://");
	l+=strlen(obuf);
	//IP addr:port\r\n
	l+=NyLPC_TIPv4Addr_toString(NyLPC_cUdpSocket_getSockIP(&i_inst->super),obuf+l);
	*(obuf+l)=':';
	l+=1+NyLPC_itoa(i_inst->location_port,obuf+l+1,10);
	*(obuf+l)='/';l++;
	memcpy(obuf+l,i_inst->location_path,len_location);l+=len_location;
	memcpy(obuf+l,"/d.xml",6);l+=6;
	*(obuf+l+0)='\r';
	*(obuf+l+1)='\n';
	l+=2;
	//USN: uuid:xxx
	memcpy(obuf+l,"USN: ",5);		l+=5;
	memcpy(obuf+l,i_udn,len_udn);	l+=len_udn;	//uuid:xxx
	if(i_usn!=NULL){
		*(obuf+l+0)=':';
		*(obuf+l+1)=':';
		l+=2;
		memcpy(obuf+l,i_usn,len_usn);l+=len_usn;	//usn:xxx
	}
	*(obuf+l+0)='\r';
	*(obuf+l+1)='\n';
	l+=2;
	//ST
	memcpy(obuf+l,"ST: ",4);	l+=4;
	memcpy(obuf+l,i_st,i_st_len);l+=i_st_len;
	memcpy(obuf+l,"\r\n\r\n",4);	l+=4;
	*o_len=l;
	return obuf;
}


/**
 * MsearchResponseを格納したTxパケットをAllocする。
 * @param i_udn
 * udn
 * @param i_udn
 * DDESCのUDNの値
 * @param i_usn
 * USNのサフィックスパラメータ
 * @return
 * MsearchResponseを格納したTXメモリ
 */
static void* allocNotifyTx(
	NyLPC_TcSsdpSocket_t* i_inst,
	const NyLPC_TChar* i_udn,
	const NyLPC_TChar* i_usn,
	NyLPC_TInt16* o_len)
{
	NyLPC_TChar* obuf;
	NyLPC_TUInt16 l,l2;
	NyLPC_TUInt16 len_usn=(NyLPC_TUInt16)((i_usn!=NULL)?strlen(i_usn):0);
	NyLPC_TUInt16 len_udn=(NyLPC_TUInt16)strlen(i_udn);
	NyLPC_TUInt16 len_location=(NyLPC_TUInt16)strlen(i_inst->location_path);

	//	//193Byte
	//	"NOTIFY * HTTP/1.1\r\n" 						//15+2=17
	//	"HOST: 239.255.255.250:1900\r\n"				//26+2=28
	//	"CACHE-CONTROL: max-age = 1800\r\n" 			//29+2=31
	//	"SERVER: [:40byte:]\r\n"						//8+40+2=50
	//	"NTS: ssdp:alive\r\n"							//14+2 =17
	//	"LOCATION: http://xxx.xxx.xxx.xxx:nnnnn/%s/d.xml\r\n"//44+2=46
	//	"USN: %s%s\r\n"									//5+2=7
	//	"NT: %s\r\n\r\n"								//4+4=8
	l2=204+len_location+len_usn+len_udn+((len_usn>0)?len_usn:len_udn);
	obuf=NyLPC_cUdpSocket_allocSendBuf(&(i_inst->super),l2,&l,TIMEOUT_IN_MS);
	if(obuf==NULL){
		return NULL;
	}
	//必要なメモリサイズを確保できた?
	if(l<l2)
	{
		NyLPC_cUdpSocket_releaseSendBuf(&i_inst->super,obuf);
		return NULL;
	}
	//ワーク変数lの再初期化
	l=0;
	strcpy(obuf,
		"NOTIFY * HTTP/1.1\r\n"
		"HOST: 239.255.255.250:1900\r\n"
		"CACHE-CONTROL: max-age = 300\r\n"
		"SERVER: "SERVER_MESSAGE_HEADER"\r\n"
		"NTS: ssdp:alive\r\n"
		"LOCATION: http://");
	l+=strlen(obuf);
	//IP addr:port\r\n
	l+=NyLPC_TIPv4Addr_toString(NyLPC_cUdpSocket_getSockIP(&i_inst->super),obuf+l);
	*(obuf+l)=':';
	l+=1+NyLPC_itoa(i_inst->location_port,obuf+l+1,10);
	*(obuf+l)='/';l++;
	memcpy(obuf+l,i_inst->location_path,len_location);l+=len_location;
	memcpy(obuf+l,"/d.xml",6);l+=6;
	*(obuf+l+0)='\r';
	*(obuf+l+1)='\n';
	l+=2;
	//USN: uuid:xxx
	memcpy(obuf+l,"USN: ",5);	l+=5;
	memcpy(obuf+l,i_udn,len_udn);	l+=len_udn;	//uuid:xxx
	if(i_usn!=NULL){
		*(obuf+l+0)=':';
		*(obuf+l+1)=':';
		l+=2;
		memcpy(obuf+l,i_usn,len_usn);l+=len_usn;	//usn:xxx
	}
	*(obuf+l+0)='\r';
	*(obuf+l+1)='\n';
	l+=2;
	//NT
	memcpy(obuf+l,"NT: ",4);	l+=4;
	if(len_usn>0){
		memcpy(obuf+l,i_usn,len_usn);l+=len_usn;
	}else{
		memcpy(obuf+l,i_udn,len_udn);l+=len_udn;
	}
	memcpy(obuf+l,"\r\n\r\n",4);	l+=4;
	*o_len=l;
	return obuf;
}


static NyLPC_TBool messageHandler(NyLPC_TcHttpBasicHeaderParser_t* i_inst,const NyLPC_TChar* i_name,NyLPC_TChar i_c,struct NyLPC_THttpBasicHeader* o_out)
{
	struct TMSearchHeader* header=(struct TMSearchHeader*)o_out;
	switch(header->st)
	{
		case PARSE_NULL:
			if(NyLPC_stricmp(i_name,"ST")==0){
				//mode==ST
				header->st=PARSE_ST;
				header->result.st_str=NULL;
			}else if(NyLPC_stricmp(i_name,"MAN")==0){
				//mode=MAN
				header->st=PARSE_MAN;
				header->result.man_str=NULL;
			}else{
				header->st=PARSE_UNKNOWN;
				//無視
			}
			break;
		case PARSE_ST:
			if((header->result.st_str==NULL) && (i_c!=HTTP_SP)){
				header->result.st_str=header->_rpos;
			}
			if(i_c=='\0')
			{
				header->result.st_len=header->_rpos-header->result.st_str-1;
				header->st=PARSE_NULL;
			}
			break;
		case PARSE_MAN:
			if((header->result.man_str==NULL) && (i_c!=HTTP_SP)){
				header->result.man_str=header->_rpos;
			}
			if(i_c=='\0'){
				header->result.man_len=header->_rpos-header->result.man_str-1;
				header->st=PARSE_NULL;
			}
			break;
		case PARSE_UNKNOWN:
		default:
			if(i_c=='\0'){
				header->st=PARSE_NULL;
			}
			break;
	}
	return NyLPC_TBool_TRUE;
}

/**
 * デフォルトハンドラ
 */
static const struct NyLPC_TcHttpBasicHeaderParser_Handler handler=
{
	messageHandler,
	urlHandler
};

static NyLPC_TBool parseHeader(struct TMSearchHeader* i_out,const void* i_rx,NyLPC_TInt16 i_rx_size)
{
	NyLPC_TInt16 i;
	NyLPC_TcHttpBasicHeaderParser_t parser;
	//headerの初期化
	i_out->st=PARSE_NULL;
	i_out->result.st_str=NULL;
	i_out->result.man_str=NULL;
	NyLPC_cHttpBasicHeaderParser_initialize(&parser,&handler);
	NyLPC_cHttpBasicHeaderParser_parseInit(&parser,&(i_out->super));
	for(i=0;i<i_rx_size;i++){
		i_out->_rpos=((const char*)(i_rx))+i;
		if(NyLPC_cHttpBasicHeaderParser_parseChar(&parser,i_out->_rpos,1,&(i_out->super))<0){
			NyLPC_cHttpBasicHeaderParser_finalize(&parser);
			return NyLPC_TBool_FALSE;//ERROR
		}
	}
	NyLPC_cHttpBasicHeaderParser_parseFinish(&parser,&(i_out->super));
	NyLPC_cHttpBasicHeaderParser_finalize(&parser);
	return NyLPC_TBool_TRUE;//OK
}

static NyLPC_TBool onPacket(NyLPC_TcUdpSocket_t* i_inst,const void* i_buf,const struct NyLPC_TIPv4RxInfo* i_info)
{
	//パケット解析
	void* tx;
	struct TMSearchHeader header;
	NyLPC_TInt16 tx_len;
	NyLPC_TInt8 i,i2;
	NyLPC_TcSsdpSocket_t* sock=(NyLPC_TcSsdpSocket_t*)i_inst;
	if(!parseHeader(&header,i_buf,i_info->size)){
		NyLPC_OnErrorGoto(ERROR1);
	}
	//resultチェック
	if(header.result.man_str==NULL || header.result.st_str==NULL){
		NyLPC_OnErrorGoto(ERROR1);
	}
	//Methodチェック
	if(header.super.startline.req.method!=NyLPC_THttpMethodType_M_SEARCH){
		NyLPC_OnErrorGoto(ERROR1);
	}

	//MANチェック
	if(strncmp("\"ssdp:discover\"",header.result.man_str,15)!=0){
		NyLPC_OnErrorGoto(ERROR1);
	}
	//STによる処理分岐
	if(strncmp("ssdp:all",header.result.st_str,8)==0){
		tx=allocMsearchResponeTx(
			sock,header.result.st_str,
			sock->ref_device_record[0]->udn,STR_UPNP_ROOT_DEVICE,
			header.result.st_len,
			&tx_len);
		if(tx==NULL){
			NyLPC_OnErrorGoto(ERROR1);
		}
		if(!NyLPC_cUdpSocket_psend(i_inst,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){
			NyLPC_OnErrorGoto(ERROR2);
		}
		//全デバイスの送信
		for(i=0;i<sock->number_of_device;i++){
			tx=allocMsearchResponeTx(
				sock,header.result.st_str,
				sock->ref_device_record[i]->udn,sock->ref_device_record[i]->device_type,
				header.result.st_len,
				&tx_len);
			if(tx==NULL){
				NyLPC_OnErrorGoto(ERROR1);
			}
			if(!NyLPC_cUdpSocket_psend(i_inst,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){
				NyLPC_OnErrorGoto(ERROR2);
			}
			for(i2=0;i2<sock->ref_device_record[i]->number_of_service;i2++){
				//serviceに一致
				tx=allocMsearchResponeTx(
					sock,header.result.st_str,
					sock->ref_device_record[i]->udn,sock->ref_device_record[i]->services[i2].service_type,
					header.result.st_len,
					&tx_len);
				if(tx==NULL){
					NyLPC_OnErrorGoto(ERROR1);
				}
				if(!NyLPC_cUdpSocket_psend(i_inst,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){
					NyLPC_OnErrorGoto(ERROR2);
				}
			}
		}
	}else if(strncmp("uuid:",header.result.st_str,5)==0){
		//UDNの一致するデバイスの送信
		NyLPC_TInt16 i;
		for(i=sock->number_of_device-1;i>=0;i--){
			if(strncmp(header.result.st_str,sock->ref_device_record[i]->udn,header.result.st_len)==0){
				//UDN一致
				tx=allocMsearchResponeTx(
					sock,header.result.st_str,
					sock->ref_device_record[i]->udn,NULL,
					header.result.st_len,
					&tx_len);
				if(tx==NULL){
					NyLPC_OnErrorGoto(ERROR1);
				}
				if(!NyLPC_cUdpSocket_psend(i_inst,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){
					NyLPC_OnErrorGoto(ERROR2);
				}
				break;//送信処理終了
			}
		}
	}else if(strncmp(STR_UPNP_ROOT_DEVICE,header.result.st_str,15)==0){
		//rootDeviceはSTR_UPNP_ROOT_DEVICE
		tx=allocMsearchResponeTx(
			sock,header.result.st_str,
			sock->ref_device_record[0]->udn,STR_UPNP_ROOT_DEVICE,
			header.result.st_len,
			&tx_len);
		if(tx==NULL){
			NyLPC_OnErrorGoto(ERROR1);
		}
		if(!NyLPC_cUdpSocket_psend(i_inst,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){
			NyLPC_OnErrorGoto(ERROR2);
		}
	}else if(strncmp("urn:",header.result.st_str,4)==0){
		for(i=0;i<sock->number_of_device;i++){
			//urn一致チェック
			if(strncmp(sock->ref_device_record[i]->device_type,header.result.st_str,header.result.st_len)==0){
				//deviceに一致
				tx=allocMsearchResponeTx(
					sock,header.result.st_str,
					sock->ref_device_record[i]->udn,sock->ref_device_record[i]->device_type,
					header.result.st_len,
					&tx_len);
				if(tx==NULL){
					NyLPC_OnErrorGoto(ERROR1);
				}
				if(!NyLPC_cUdpSocket_psend(i_inst,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){
					NyLPC_OnErrorGoto(ERROR2);
				}
				continue;
			}
			for(i2=0;i2<sock->ref_device_record[i]->number_of_service;i2++){
				if(strncmp(sock->ref_device_record[i]->services[i2].service_type,header.result.st_str,header.result.st_len)==0){
					//serviceに一致
					tx=allocMsearchResponeTx(
						sock,header.result.st_str,
						sock->ref_device_record[i]->udn,sock->ref_device_record[i]->services[i2].service_type,
						header.result.st_len,
						&tx_len);
					if(tx==NULL){
						NyLPC_OnErrorGoto(ERROR1);
					}
					if(!NyLPC_cUdpSocket_psend(i_inst,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){
						NyLPC_OnErrorGoto(ERROR2);
					}
				}
			}
		}
	}
	//正常終了
	return NyLPC_TBool_FALSE;
ERROR2:
	NyLPC_cUdpSocket_releaseSendBuf(i_inst,tx);
ERROR1:
	return NyLPC_TBool_FALSE;
}

#define SSDP_NOTIFY_INTERVAL 150*1000	//300*0.5*1000
#define FLAG_ORDER_START_SERVICE	0
#define FLAG_ORDER_STOP_SERVICE		1
#define FLAG_IS_SERVICE_RUNNING		2

static void onPeriodic(NyLPC_TcUdpSocket_t* i_inst)
{
	NyLPC_TcSsdpSocket_t* sock=(NyLPC_TcSsdpSocket_t*)i_inst;
	if(NyLPC_TUInt8_isBitOn(sock->_flags,FLAG_IS_SERVICE_RUNNING)){
	//実行中
		//停止要求着てる?
		if(NyLPC_TUInt8_isBitOn(sock->_flags,FLAG_ORDER_STOP_SERVICE))
		{
			//状態変更
			NyLPC_TUInt8_unsetBit(sock->_flags,FLAG_IS_SERVICE_RUNNING);
			//要求フラグクリア
			NyLPC_TUInt8_unsetBit(sock->_flags,FLAG_ORDER_STOP_SERVICE);
			//@bug ByeBye送信しろ
		}else if(NyLPC_cStopwatch_isExpired(&sock->_periodic_sw)){
			//Notify送信
			NyLPC_cSsdpSocket_notify(sock);
			//タイマ再始動
			NyLPC_cStopwatch_startExpire(&sock->_periodic_sw,SSDP_NOTIFY_INTERVAL);
		}
	}else{
	//停止中
		//開始要求着てる?
		if(NyLPC_TUInt8_isBitOn(sock->_flags,FLAG_ORDER_START_SERVICE))
		{
			//状態変更
			NyLPC_TUInt8_setBit(sock->_flags,FLAG_IS_SERVICE_RUNNING);
			//要求フラグクリア
			NyLPC_TUInt8_unsetBit(sock->_flags,FLAG_ORDER_START_SERVICE);
			//次回expireするように
			NyLPC_cStopwatch_startExpire(&sock->_periodic_sw,SSDP_NOTIFY_INTERVAL);
		}
	}
}

/**
 * デバイスツリーを展開する。
 */
static void expandDeviceTree(NyLPC_TcSsdpSocket_t* i_inst,const struct NyLPC_TUPnPDevDescDevice* i_dev)
{
	NyLPC_TUInt16 i;
	if(i_inst->number_of_device>=NyLPC_TcSsdpSocket_MAX_DEVICES){
		NyLPC_Warning();//
	}
	i_inst->ref_device_record[i_inst->number_of_device]=i_dev;
	i_inst->number_of_device++;
	for(i=0;i<i_dev->number_of_devices;i++){
		expandDeviceTree(i_inst,i_dev->devices[i]);
	}
	return;
}

void NyLPC_cSsdpSocket_initialize(
		NyLPC_TcSsdpSocket_t* i_inst,
		const struct NyLPC_TUPnPDevDescDevice* i_ref_dev_record,
		NyLPC_TUInt16 i_server_port,const NyLPC_TChar* i_ref_location_path)
{
	NyLPC_cUdpSocket_initialize(&(i_inst->super),1900,NULL,0);
	NyLPC_cUdpSocket_setOnRxHandler(&(i_inst->super),onPacket);
	NyLPC_cUdpSocket_setOnPeriodicHandler(&(i_inst->super),onPeriodic);

	NyLPC_cUdpSocket_joinMulticast(&(i_inst->super),&SSDP_MCAST_IPADDR);
	i_inst->_flags=0;
	NyLPC_cStopwatch_initialize(&(i_inst->_periodic_sw));
	i_inst->number_of_device=0;
	expandDeviceTree(i_inst,i_ref_dev_record);
	i_inst->location_port=i_server_port;
	i_inst->location_path=i_ref_location_path;
}
void NyLPC_cSsdpSocket_finalize(NyLPC_TcSsdpSocket_t* i_inst)
{
	NyLPC_cStopwatch_finalize(&(i_inst->_periodic_sw));
	NyLPC_cUdpSocket_finalize(&(i_inst->super));
}

void NyLPC_cSsdpSocket_start(NyLPC_TcSsdpSocket_t* i_inst)
{
	//Notifyを3回送信
	NyLPC_TInt16 i;
	NyLPC_cSsdpSocket_notify(i_inst);
	for(i=0;i<2;i++){
		NyLPC_cThread_sleep(800);
		NyLPC_cSsdpSocket_notify(i_inst);
	}

	//ストップウォッチの開始要求
	NyLPC_TUInt8_setBit(i_inst->_flags,FLAG_ORDER_START_SERVICE);
	do{
		NyLPC_cThread_sleep(10);
		//開始フラグがクリアされるまでループ
	}while(NyLPC_TUInt8_isBitOn(i_inst->_flags,FLAG_ORDER_START_SERVICE));
}
void NyLPC_cSsdpSocket_stop(NyLPC_TcSsdpSocket_t* i_inst)
{
	//今は使えない。
	NyLPC_Abort();
	NyLPC_TUInt8_setBit(i_inst->_flags,FLAG_ORDER_STOP_SERVICE);
	do{
		NyLPC_cThread_sleep(10);
		//開始フラグがクリアされるまでループ
	}while(NyLPC_TUInt8_isBitOn(i_inst->_flags,FLAG_ORDER_STOP_SERVICE));
}
void NyLPC_cSsdpSocket_notify(NyLPC_TcSsdpSocket_t* i_inst)
{
	void* tx;
	NyLPC_TInt16 tx_len;
	NyLPC_TUInt8 i,i2;
	//rootdevice
	tx=allocNotifyTx(
		i_inst,
		i_inst->ref_device_record[0]->udn,STR_UPNP_ROOT_DEVICE,
		&tx_len);
	if(tx==NULL){
		NyLPC_OnErrorGoto(ERROR1);
	}
	if(!NyLPC_cUdpSocket_psend(&i_inst->super,&SSDP_MCAST_IPADDR,1900,tx,tx_len)){
		NyLPC_OnErrorGoto(ERROR2);
	}
	//all device
	for(i=0;i<i_inst->number_of_device;i++){
		//uuid
		tx=allocNotifyTx(
			i_inst,
			i_inst->ref_device_record[i]->udn,NULL,
			&tx_len);
		if(tx==NULL){
			NyLPC_OnErrorGoto(ERROR1);
		}
		if(!NyLPC_cUdpSocket_psend(&i_inst->super,&SSDP_MCAST_IPADDR,1900,tx,tx_len)){
			NyLPC_OnErrorGoto(ERROR2);
		}
		//devicatype
		tx=allocNotifyTx(
			i_inst,
			i_inst->ref_device_record[i]->udn,i_inst->ref_device_record[i]->device_type,
			&tx_len);
		if(tx==NULL){
			NyLPC_OnErrorGoto(ERROR1);
		}
		if(!NyLPC_cUdpSocket_psend(&i_inst->super,&SSDP_MCAST_IPADDR,1900,tx,tx_len)){
			NyLPC_OnErrorGoto(ERROR2);
		}
		for(i2=0;i2<i_inst->ref_device_record[i]->number_of_service;i2++){
			tx=allocNotifyTx(
				i_inst,
				i_inst->ref_device_record[i]->udn,i_inst->ref_device_record[i]->services[i2].service_type,
				&tx_len);
			if(tx==NULL){
				NyLPC_OnErrorGoto(ERROR1);
			}
			if(!NyLPC_cUdpSocket_psend(&i_inst->super,&SSDP_MCAST_IPADDR,1900,tx,tx_len)){
				NyLPC_OnErrorGoto(ERROR2);
			}
		}
	}
	return;
ERROR2:
	NyLPC_cUdpSocket_releaseSendBuf(&i_inst->super,tx);
ERROR1:
	return;
}