/**\r
* tcp初期設定モードのフラグ値\r
* NyLPC_TcNetConfig_IPV4_FLAG_Xの組み合わせ\r
+ * bit 01:IP初期設定のモード. 0:Manual指定,1:DHCP指定,2:AutoIP指定,3:APIPA指定\r
*/\r
NyLPC_TUInt32 tcp_mode;\r
struct{\r
/**\r
* サービスのフラグセット。\r
* NyLPC_TcNetConfig_SERVICE_FLAG_xの組み合わせ\r
- * bit 01:IP初期設定のモード. 0:Manual指定,1:DHCP指定,2:AutoIP指定,3:APIPA指定\r
*/\r
NyLPC_TUInt32 flags;\r
NyLPC_TUInt16 http_port;\r
*********************************************************************************/\r
#ifndef NYLPC_CAPIPA_H_\r
#define NYLPC_CAPIPA_H_\r
+\r
+#ifdef __cplusplus\r
+extern "C" {\r
+#endif /* __cplusplus */\r
+\r
#include "NyLPC_stdlib.h"\r
-typedef struct NyLPC_cApipa NyLPC_TcApipa_t;\r
#include "NyLPC_net.h"\r
\r
+typedef struct NyLPC_cApipa NyLPC_TcApipa_t;\r
+\r
struct NyLPC_cApipa\r
{\r
NyLPC_TUInt32 _seed;\r
*/\r
NyLPC_TBool NyLPC_cApipa_requestAddr(NyLPC_TcApipa_t* i_inst,NyLPC_TcIPv4Config_t* i_cfg,NyLPC_TInt16 i_repeat);\r
\r
-\r
+#ifdef __cplusplus\r
+}\r
+#endif /* __cplusplus */\r
#endif /* NYLPC_CAPIPA_H_ */\r
NyLPC_TUInt16 i;\r
const char* p=(const char*)i_qpacket;\r
//QNameのパース'0終端'\r
- l=i_len-4-1;//スキャン長はパケットサイズ-5まで\r
+ l=i_len-4;//スキャン長はパケットサイズ-5まで\r
for(i=0;i<l;i++){\r
if(*(p+i)==0){\r
l=i+1;//NULL終端を加味した文字列長\r
if(i_info->peer_port!=MDNS_MCAST_PORT || !NyLPC_TIPv4Addr_isEqual(&MDNS_MCAST_IPADDR,&i_info->ip)){\r
return NyLPC_TBool_FALSE;\r
}\r
- //パケット処理中\r
- NyLPC_TUInt8_setBit(((NyLPC_TcMDnsServer_t*)i_inst)->_ctrlflag,CTRL_FLAG_PROCESS_PACKET);\r
-\r
- if(!NyLPC_TUInt8_isBitOn(((NyLPC_TcMDnsServer_t*)i_inst)->_ctrlflag,CTRL_FLAG_STARTED)){\r
- //停止中なら何もしない\r
- NyLPC_TUInt8_unsetBit(((NyLPC_TcMDnsServer_t*)i_inst)->_ctrlflag,CTRL_FLAG_PROCESS_PACKET);\r
- return NyLPC_TBool_FALSE;\r
- }\r
\r
num_of_query=getNumberOfQuestion(i_buf,in_len);\r
if(num_of_query==0){\r
sendReply2((NyLPC_TcMDnsServer_t*)i_inst,(const struct NyLPC_TDnsHeader*)i_buf,&q);\r
}\r
//パケット処理終了\r
- NyLPC_TUInt8_unsetBit(((NyLPC_TcMDnsServer_t*)i_inst)->_ctrlflag,CTRL_FLAG_PROCESS_PACKET);\r
return NyLPC_TBool_FALSE;\r
DROP:\r
- NyLPC_TUInt8_unsetBit(((NyLPC_TcMDnsServer_t*)i_inst)->_ctrlflag,CTRL_FLAG_PROCESS_PACKET);\r
return NyLPC_TBool_FALSE;\r
}\r
\r
+static void onPeriodic(NyLPC_TcUdpSocket_t* i_inst)\r
+{\r
\r
+ if(NyLPC_cStopwatch_isExpired(&((NyLPC_TcMDnsServer_t*)i_inst)->_periodic_sw)){\r
+ //アナウンス\r
+ sendAnnounse(((NyLPC_TcMDnsServer_t*)i_inst));\r
+ //TTL(msec)*1000*80%\r
+ NyLPC_cStopwatch_startExpire((&((NyLPC_TcMDnsServer_t*)i_inst)->_periodic_sw),NyLPC_TcMDns_TTL*5000/4);\r
+ }\r
+}\r
\r
NyLPC_TBool NyLPC_cMDnsServer_initialize(\r
NyLPC_TcMDnsServer_t* i_inst,struct NyLPC_TDnsRecord* i_ref_record)\r
{\r
+ NyLPC_cStopwatch_initialize(&(i_inst->_periodic_sw));\r
+ NyLPC_cStopwatch_startExpire(&(i_inst->_periodic_sw),1000);\r
NyLPC_cUdpSocket_initialize(&(i_inst->_super),MDNS_MCAST_PORT,NULL,0);\r
NyLPC_cUdpSocket_setOnRxHandler(&(i_inst->_super),onPacket);\r
+ NyLPC_cUdpSocket_setOnPerriodicHandler(&(i_inst->_super),onPeriodic);\r
NyLPC_cUdpSocket_joinMulticast(&(i_inst->_super),&MDNS_MCAST_IPADDR);\r
- i_inst->_ctrlflag=CTRL_FLAG_INIT;\r
i_inst->_ref_record=i_ref_record;\r
return NyLPC_TBool_TRUE;\r
}\r
void NyLPC_cMDnsServer_finalize(\r
NyLPC_TcMDnsServer_t* i_inst)\r
{\r
- //起動中なら停止\r
- if(i_inst->_ctrlflag & CTRL_FLAG_STARTED){\r
- NyLPC_cMDnsServer_stop(i_inst);\r
- }\r
NyLPC_cUdpSocket_finalize(&(i_inst->_super));\r
+ NyLPC_cStopwatch_finalize(&(i_inst->_periodic_sw));\r
}\r
\r
-void NyLPC_cMDnsServer_start(NyLPC_TcMDnsServer_t* i_inst)\r
-{\r
- NyLPC_TcStopwatch_t sw;\r
- NyLPC_Assert((i_inst->_ctrlflag & CTRL_FLAG_STARTED)==0x0);\r
- //開始ビットのセット\r
- NyLPC_TUInt8_setBit(i_inst->_ctrlflag,CTRL_FLAG_STARTED);//開始状態にする。\r
- sendAnnounse(i_inst);\r
- NyLPC_cStopwatch_initialize(&sw);\r
- NyLPC_cStopwatch_startExpire(&sw,NyLPC_TcMDns_TTL*5000/4);\r
- while(!NyLPC_TUInt8_isBitOn(i_inst->_ctrlflag,CTRL_FLAG_STOP_REQUESTED)){\r
- //受信処理\r
- NyLPC_cThread_sleep(100);\r
- if(NyLPC_cStopwatch_isExpired(&sw)){\r
- //アナウンス\r
- sendAnnounse(i_inst);\r
- NyLPC_cStopwatch_startExpire(&sw,NyLPC_TcMDns_TTL*5000/4);\r
- }\r
- }\r
- //開始ビットのクリア\r
- NyLPC_TUInt8_unsetBit(i_inst->_ctrlflag,CTRL_FLAG_STARTED);\r
-}\r
-\r
-void NyLPC_cMDnsServer_stop(NyLPC_TcMDnsServer_t* i_inst)\r
-{\r
- //停止要求ビットのセット\r
- NyLPC_TUInt8_setBit(i_inst->_ctrlflag,CTRL_FLAG_STOP_REQUESTED);\r
- while(NyLPC_TUInt8_isBitOn(i_inst->_ctrlflag,CTRL_FLAG_STARTED)){\r
- NyLPC_cThread_sleep(100);\r
- }\r
- //パケット処理終了待ち\r
- while(NyLPC_TUInt8_isBitOn(i_inst->_ctrlflag,CTRL_FLAG_PROCESS_PACKET)){\r
- NyLPC_cThread_sleep(100);\r
- }\r
-\r
- //停止要求ビットのクリア\r
- NyLPC_TUInt8_unsetBit(i_inst->_ctrlflag,CTRL_FLAG_STOP_REQUESTED);\r
\r
\r
-}\r
-\r
{\r
/** マルチキャストのUDPソケット*/\r
NyLPC_TcUdpSocket_t _super;\r
- /** 動作フラグ\r
- * 7 - サービス起動中\r
- * 6 - サービス停止要求\r
- * 5-0 Reserved\r
- */\r
- volatile NyLPC_TUInt8 _ctrlflag;\r
- /** 受信バッファ*/\r
+ /**周期実行タイマ*/\r
+ NyLPC_TcStopwatch_t _periodic_sw;\r
/** DNSレコードの参照情報*/\r
const struct NyLPC_TDnsRecord* _ref_record;\r
};\r
//downcast!\r
NyLPC_cTcpSocket_periodic((NyLPC_TcTcpSocket_t*)(p[i]));\r
break;\r
+ case NyLPC_TcBaseSocket_TYPEID_UDP_SOCK:\r
+ NyLPC_cUdpSocket_periodic((NyLPC_TcUdpSocket_t*)(p[i]));\r
+ break;\r
default:\r
continue;\r
}\r
i_inst->uip_udp_conn.lport=NyLPC_htons(i_port);\r
i_inst->uip_udp_conn.mcastaddr=NyLPC_TIPv4Addr_ZERO;\r
i_inst->uip_udp_conn.flags=0x00;\r
- i_inst->_handler=NULL;\r
+ i_inst->as_handler.rx=NULL;\r
+ i_inst->as_handler.periodic=NULL;\r
\r
NyLPC_cFifoBuffer_initialize(&(i_inst->rxbuf),i_rbuf,i_rbuf_len);\r
//管理リストへ登録。\r
dheader.peer_port=NyLPC_ntohs(o_ipp->payload.udp->srcport);\r
dheader.ip=o_ipp->header->destipaddr;\r
dheader.port=NyLPC_ntohs(o_ipp->payload.udp->destport);\r
- if(i_inst->_handler!=NULL){\r
- if(!i_inst->_handler(i_inst,o_ipp->payload.rawbuf+tmp16,&dheader)){\r
+ if(i_inst->as_handler.rx!=NULL){\r
+ if(!i_inst->as_handler.rx(i_inst,o_ipp->payload.rawbuf+tmp16,&dheader)){\r
return NyLPC_TBool_FALSE;//UDPはReturnパケットなし\r
}\r
}\r
\r
/**\r
* 受信時に非同期にコールされるハンドラ\r
+ * UIPサービスタスクが実行する。\r
* @return\r
* TRUEならパケットを受信キューへ追加する。FALSEならパケットを受信キューへ追加しない。\r
*/\r
typedef NyLPC_TBool (*NyLPC_TcUdpSocket_onRxHandler)(NyLPC_TcUdpSocket_t* i_inst,const void* i_buf,const struct NyLPC_TIPv4RxInfo* i_info);\r
+/**\r
+ * 非同期にコールされるハンドラ。\r
+ * UIPサービスタスクが実行する。\r
+ */\r
+typedef void (*NyLPC_TcUdpSocket_onPeriodic)(NyLPC_TcUdpSocket_t* i_inst);\r
\r
struct NyLPC_TcUdpSocket\r
{\r
struct uip_udp_conn uip_udp_conn;\r
NyLPC_TcFifoBuffer_t rxbuf;\r
NyLPC_TcMutex_t* _smutex;\r
- NyLPC_TcUdpSocket_onRxHandler _handler;\r
+ struct{\r
+ /** 受信ハンドラ。サービス実装に使用する。*/\r
+ NyLPC_TcUdpSocket_onRxHandler rx;\r
+ /** 定期実行ハンドラ。サービス実装に使用する。最低保障周期は1s*/\r
+ NyLPC_TcUdpSocket_onPeriodic periodic;\r
+ }as_handler;\r
};\r
\r
\r
/**\r
* 非同期パケットハンドラを設定する。\r
*/\r
-#define NyLPC_cUdpSocket_setOnRxHandler(i_inst,i_handler) (i_inst)->_handler=i_handler;\r
+#define NyLPC_cUdpSocket_setOnRxHandler(i_inst,i_handler) (i_inst)->as_handler.rx=i_handler;\r
+\r
+/**\r
+ * 非同期タイマ呼び出しハンドラを設定する。\r
+ */\r
+#define NyLPC_cUdpSocket_setOnPerriodicHandler(i_inst,i_handler) (i_inst)->as_handler.periodic=i_handler;\r
\r
\r
#ifdef __cplusplus\r
void NyLPC_cUdpSocket_stopService(NyLPC_TcUdpSocket_t* i_inst);\r
\r
\r
+/**\r
+ * 定期的に実行する関数。最低でも1s単位で実行してください。\r
+ * uipサービスタスクが実行する関数です。\r
+ */\r
+#define NyLPC_cUdpSocket_periodic(i_inst) if((i_inst)->as_handler.periodic!=NULL){(i_inst)->as_handler.periodic(i_inst);}\r
+\r
#ifdef __cplusplus\r
}\r
#endif /* __cplusplus */\r
NyLPC_cStopwatch_startExpire(&(inst->_arp_sw),ARP_TIMER);\r
}\r
if(NyLPC_cStopwatch_isExpired(&(inst->_periodic_sw))){\r
+ NyLPC_cMutex_unlock(&(inst->_mutex));\r
NyLPC_cIPv4_periodec(&(inst->_tcpv4));\r
+ NyLPC_cMutex_lock(&(inst->_mutex));\r
NyLPC_cStopwatch_startExpire(&(inst->_periodic_sw),PERIODIC_TIMER);\r
}\r
//リソースロックの解除\r
NyLPC_TBool NyLPC_cUipService_hasArpInfo(const struct NyLPC_TIPv4Addr* i_addr);\r
\r
\r
-void NyLPC_cTcpSocket_periodic(\r
- NyLPC_TcTcpSocket_t* i_inst);\r
\r
\r
\r
return (p-buf);\r
}\r
/**\r
+ * 文字列から10進数の数値を読み出します。\r
+ * @return\r
+ * 読み出した文字数\r
+ */\r
+NyLPC_TInt32 NyLPC_cFormatTextReader_readUInt(const NyLPC_TChar* buf,NyLPC_TUInt32* v)\r
+{\r
+ NyLPC_TUInt32 t;\r
+ const NyLPC_TChar* p=buf;\r
+ t=0;\r
+ for(;isdigit(*p);p++){\r
+ t=t*10+NyLPC_ctoi(*p);\r
+ }\r
+ *v=t;\r
+ return (p-buf);\r
+}\r
+\r
+/**\r
* 文字列からMACアドレスを取得します。\r
* [:hex:]:[:hex:]:[:hex:]:[:hex:]\r
*/\r
\r
/**\r
* 連続するスペースを読み飛ばします。\r
+ */\r
+NyLPC_TInt32 NyLPC_cFormatTextReader_seekSpace(const NyLPC_TChar* s);\r
+\r
+/**\r
+ * 文字列から10進数の数値を読み出します。\r
* @return\r
* 読み飛ばしたスペース\r
*/\r
-NyLPC_TInt32 NyLPC_cFormatTextReader_seekSpace(const NyLPC_TChar* s);\r
+NyLPC_TInt32 NyLPC_cFormatTextReader_readUInt(const NyLPC_TChar* buf,NyLPC_TUInt32* v);\r
\r
#ifdef __cplusplus\r
}\r
NyLPC_cNetConfig_setIpAddr(&c2,192,168,128,39);\r
NyLPC_cNetConfig_setGateway(&c2,192,168,128,254);\r
NyLPC_cNet_start(&net,&c2);\r
- NyLPC_cMDnsServer_start(&mdns);\r
+ for(;;);\r
}\r
\r
\r