OSDN Git Service

update libMiMic
[mimic/MiMicSDK.git] / lib / src / net / upnp / NyLPC_cSsdpSocket.c
index e7dbe7b..b04e19c 100644 (file)
@@ -25,8 +25,8 @@
  *********************************************************************************/\r
 #include "NyLPC_cSsdpSocket.h"\r
 #include "NyLPC_http.h"\r
-#include "NyLPC_uipService.h"\r
-#include "../uip/NyLPC_cUipService_protected.h"\r
+#include "NyLPC_netif.h"\r
+\r
 \r
 #include <stdio.h>\r
 #include <string.h>\r
@@ -82,7 +82,7 @@ static NyLPC_TBool urlHandler(NyLPC_TcHttpBasicHeaderParser_t* i_inst,NyLPC_TCha
  * SERVER MessageHeaderの値\r
  * 40文字以内であること。\r
  */\r
-#define SERVER_MESSAGE_HEADER "MiMic/1.4;UPnP/1.0;MiMicUPnP/0.1"\r
+#define SERVER_MESSAGE_HEADER "MiMic/1.4 UPnP/1.0 MiMicUPnP/0.2"\r
 \r
 \r
 /**\r
@@ -119,7 +119,7 @@ static void* allocMsearchResponeTx(
        //      "USN: %s%s\r\n"                                                                 //5+2=7\r
        //      "ST: %s\r\n\r\n"                                                                //4+4=8\r
        l=166+len_location+len_usn+len_udn+i_st_len;\r
-       obuf=NyLPC_cUdpSocket_allocSendBuf(&(i_inst->super),l,&l,TIMEOUT_IN_MS);\r
+       obuf=NyLPC_iUdpSocket_allocSendBuf(i_inst->_socket,l,&l,TIMEOUT_IN_MS);\r
 \r
        if(obuf==NULL){\r
                return NULL;\r
@@ -127,7 +127,7 @@ static void* allocMsearchResponeTx(
        //必要なメモリサイズを確保できた?\r
        if(l<161+len_location+len_usn+len_udn+i_st_len)\r
        {\r
-               NyLPC_cUdpSocket_releaseSendBuf(&i_inst->super,obuf);\r
+               NyLPC_iUdpSocket_releaseSendBuf(i_inst->_socket,obuf);\r
                return NULL;\r
        }\r
        //ワーク変数lの再初期化\r
@@ -140,7 +140,7 @@ static void* allocMsearchResponeTx(
                "LOCATION: http://");\r
        l+=strlen(obuf);\r
        //IP addr:port\r\n\r
-       l+=NyLPC_TIPv4Addr_toString(NyLPC_cUdpSocket_getSockIP(&i_inst->super),obuf+l);\r
+       l+=NyLPC_TIPv4Addr_toString(NyLPC_iUdpSocket_getSockIP(i_inst->_socket),obuf+l);\r
        *(obuf+l)=':';\r
        l+=1+NyLPC_itoa(i_inst->location_port,obuf+l+1,10);\r
        *(obuf+l)='/';l++;\r
@@ -203,14 +203,14 @@ static void* allocNotifyTx(
        //      "USN: %s%s\r\n"                                                                 //5+2=7\r
        //      "NT: %s\r\n\r\n"                                                                //4+4=8\r
        l2=204+len_location+len_usn+len_udn+((len_usn>0)?len_usn:len_udn);\r
-       obuf=NyLPC_cUdpSocket_allocSendBuf(&(i_inst->super),l2,&l,TIMEOUT_IN_MS);\r
+       obuf=NyLPC_iUdpSocket_allocSendBuf(i_inst->_socket,l2,&l,TIMEOUT_IN_MS);\r
        if(obuf==NULL){\r
                return NULL;\r
        }\r
        //必要なメモリサイズを確保できた?\r
        if(l<l2)\r
        {\r
-               NyLPC_cUdpSocket_releaseSendBuf(&i_inst->super,obuf);\r
+               NyLPC_iUdpSocket_releaseSendBuf(i_inst->_socket,obuf);\r
                return NULL;\r
        }\r
        //ワーク変数lの再初期化\r
@@ -224,7 +224,7 @@ static void* allocNotifyTx(
                "LOCATION: http://");\r
        l+=strlen(obuf);\r
        //IP addr:port\r\n\r
-       l+=NyLPC_TIPv4Addr_toString(NyLPC_cUdpSocket_getSockIP(&i_inst->super),obuf+l);\r
+       l+=NyLPC_TIPv4Addr_toString(NyLPC_iUdpSocket_getSockIP(i_inst->_socket),obuf+l);\r
        *(obuf+l)=':';\r
        l+=1+NyLPC_itoa(i_inst->location_port,obuf+l+1,10);\r
        *(obuf+l)='/';l++;\r
@@ -337,14 +337,14 @@ static NyLPC_TBool parseHeader(struct TMSearchHeader* i_out,const void* i_rx,NyL
        return NyLPC_TBool_TRUE;//OK\r
 }\r
 \r
-static NyLPC_TBool onPacket(NyLPC_TcUdpSocket_t* i_inst,const void* i_buf,const struct NyLPC_TIPv4RxInfo* i_info)\r
+static NyLPC_TBool onPacket(NyLPC_TiUdpSocket_t* i_sock,const void* i_buf,const struct NyLPC_TIPv4RxInfo* i_info)\r
 {\r
        //パケット解析\r
        void* tx;\r
        struct TMSearchHeader header;\r
        NyLPC_TInt16 tx_len;\r
        NyLPC_TInt8 i,i2;\r
-       NyLPC_TcSsdpSocket_t* sock=(NyLPC_TcSsdpSocket_t*)i_inst;\r
+       NyLPC_TcSsdpSocket_t* inst=((NyLPC_TcSsdpSocket_t*)i_sock->_tag);\r
        if(!parseHeader(&header,i_buf,i_info->size)){\r
                NyLPC_OnErrorGoto(ERROR1);\r
        }\r
@@ -364,40 +364,40 @@ static NyLPC_TBool onPacket(NyLPC_TcUdpSocket_t* i_inst,const void* i_buf,const
        //STによる処理分岐\r
        if(strncmp("ssdp:all",header.result.st_str,8)==0){\r
                tx=allocMsearchResponeTx(\r
-                       sock,header.result.st_str,\r
-                       sock->ref_device_record[0]->udn,STR_UPNP_ROOT_DEVICE,\r
+                       inst,header.result.st_str,\r
+                       inst->ref_device_record[0]->udn,STR_UPNP_ROOT_DEVICE,\r
                        header.result.st_len,\r
                        &tx_len);\r
                if(tx==NULL){\r
                        NyLPC_OnErrorGoto(ERROR1);\r
                }\r
-               if(!NyLPC_cUdpSocket_psend(i_inst,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){\r
+               if(!NyLPC_iUdpSocket_psend(i_sock,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){\r
                        NyLPC_OnErrorGoto(ERROR2);\r
                }\r
                //全デバイスの送信\r
-               for(i=0;i<sock->number_of_device;i++){\r
+               for(i=0;i<inst->number_of_device;i++){\r
                        tx=allocMsearchResponeTx(\r
-                               sock,header.result.st_str,\r
-                               sock->ref_device_record[i]->udn,sock->ref_device_record[i]->device_type,\r
+                               inst,header.result.st_str,\r
+                               inst->ref_device_record[i]->udn,inst->ref_device_record[i]->device_type,\r
                                header.result.st_len,\r
                                &tx_len);\r
                        if(tx==NULL){\r
                                NyLPC_OnErrorGoto(ERROR1);\r
                        }\r
-                       if(!NyLPC_cUdpSocket_psend(i_inst,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){\r
+                       if(!NyLPC_iUdpSocket_psend(i_sock,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){\r
                                NyLPC_OnErrorGoto(ERROR2);\r
                        }\r
-                       for(i2=0;i2<sock->ref_device_record[i]->number_of_service;i2++){\r
+                       for(i2=0;i2<inst->ref_device_record[i]->number_of_service;i2++){\r
                                //serviceに一致\r
                                tx=allocMsearchResponeTx(\r
-                                       sock,header.result.st_str,\r
-                                       sock->ref_device_record[i]->udn,sock->ref_device_record[i]->services[i2].service_type,\r
+                                       inst,header.result.st_str,\r
+                                       inst->ref_device_record[i]->udn,inst->ref_device_record[i]->services[i2].service_type,\r
                                        header.result.st_len,\r
                                        &tx_len);\r
                                if(tx==NULL){\r
                                        NyLPC_OnErrorGoto(ERROR1);\r
                                }\r
-                               if(!NyLPC_cUdpSocket_psend(i_inst,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){\r
+                               if(!NyLPC_iUdpSocket_psend(i_sock,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){\r
                                        NyLPC_OnErrorGoto(ERROR2);\r
                                }\r
                        }\r
@@ -405,66 +405,66 @@ static NyLPC_TBool onPacket(NyLPC_TcUdpSocket_t* i_inst,const void* i_buf,const
        }else if(strncmp("uuid:",header.result.st_str,5)==0){\r
                //UDNの一致するデバイスの送信\r
                NyLPC_TInt16 i;\r
-               for(i=sock->number_of_device-1;i>=0;i--){\r
-                       if(strncmp(header.result.st_str,sock->ref_device_record[i]->udn,header.result.st_len)==0){\r
+               for(i=inst->number_of_device-1;i>=0;i--){\r
+                       if(strncmp(header.result.st_str,inst->ref_device_record[i]->udn,header.result.st_len)==0){\r
                                //UDN一致\r
                                tx=allocMsearchResponeTx(\r
-                                       sock,header.result.st_str,\r
-                                       sock->ref_device_record[i]->udn,NULL,\r
+                                       inst,header.result.st_str,\r
+                                       inst->ref_device_record[i]->udn,NULL,\r
                                        header.result.st_len,\r
                                        &tx_len);\r
                                if(tx==NULL){\r
                                        NyLPC_OnErrorGoto(ERROR1);\r
                                }\r
-                               if(!NyLPC_cUdpSocket_psend(i_inst,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){\r
+                               if(!NyLPC_iUdpSocket_psend(i_sock,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){\r
                                        NyLPC_OnErrorGoto(ERROR2);\r
                                }\r
                                break;//送信処理終了\r
                        }\r
                }\r
        }else if(strncmp(STR_UPNP_ROOT_DEVICE,header.result.st_str,15)==0){\r
-               //rootDeviceはdevice0\r
+               //rootDeviceはSTR_UPNP_ROOT_DEVICE\r
                tx=allocMsearchResponeTx(\r
-                       sock,header.result.st_str,\r
-                       sock->ref_device_record[0]->udn,sock->ref_device_record[0]->device_type,\r
+                       inst,header.result.st_str,\r
+                       inst->ref_device_record[0]->udn,STR_UPNP_ROOT_DEVICE,\r
                        header.result.st_len,\r
                        &tx_len);\r
                if(tx==NULL){\r
                        NyLPC_OnErrorGoto(ERROR1);\r
                }\r
-               if(!NyLPC_cUdpSocket_psend(i_inst,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){\r
+               if(!NyLPC_iUdpSocket_psend(i_sock,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){\r
                        NyLPC_OnErrorGoto(ERROR2);\r
                }\r
        }else if(strncmp("urn:",header.result.st_str,4)==0){\r
-               for(i=0;i<sock->number_of_device;i++){\r
+               for(i=0;i<inst->number_of_device;i++){\r
                        //urn一致チェック\r
-                       if(strncmp(sock->ref_device_record[i]->device_type,header.result.st_str,header.result.st_len)==0){\r
+                       if(strncmp(inst->ref_device_record[i]->device_type,header.result.st_str,header.result.st_len)==0){\r
                                //deviceに一致\r
                                tx=allocMsearchResponeTx(\r
-                                       sock,header.result.st_str,\r
-                                       sock->ref_device_record[i]->udn,sock->ref_device_record[i]->device_type,\r
+                                       inst,header.result.st_str,\r
+                                       inst->ref_device_record[i]->udn,inst->ref_device_record[i]->device_type,\r
                                        header.result.st_len,\r
                                        &tx_len);\r
                                if(tx==NULL){\r
                                        NyLPC_OnErrorGoto(ERROR1);\r
                                }\r
-                               if(!NyLPC_cUdpSocket_psend(i_inst,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){\r
+                               if(!NyLPC_iUdpSocket_psend(i_sock,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){\r
                                        NyLPC_OnErrorGoto(ERROR2);\r
                                }\r
                                continue;\r
                        }\r
-                       for(i2=0;i2<sock->ref_device_record[i]->number_of_service;i2++){\r
-                               if(strncmp(sock->ref_device_record[i]->services[i2].service_type,header.result.st_str,header.result.st_len)==0){\r
+                       for(i2=0;i2<inst->ref_device_record[i]->number_of_service;i2++){\r
+                               if(strncmp(inst->ref_device_record[i]->services[i2].service_type,header.result.st_str,header.result.st_len)==0){\r
                                        //serviceに一致\r
                                        tx=allocMsearchResponeTx(\r
-                                               sock,header.result.st_str,\r
-                                               sock->ref_device_record[i]->udn,sock->ref_device_record[i]->services[i2].service_type,\r
+                                               inst,header.result.st_str,\r
+                                               inst->ref_device_record[i]->udn,inst->ref_device_record[i]->services[i2].service_type,\r
                                                header.result.st_len,\r
                                                &tx_len);\r
                                        if(tx==NULL){\r
                                                NyLPC_OnErrorGoto(ERROR1);\r
                                        }\r
-                                       if(!NyLPC_cUdpSocket_psend(i_inst,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){\r
+                                       if(!NyLPC_iUdpSocket_psend(i_sock,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){\r
                                                NyLPC_OnErrorGoto(ERROR2);\r
                                        }\r
                                }\r
@@ -474,46 +474,46 @@ static NyLPC_TBool onPacket(NyLPC_TcUdpSocket_t* i_inst,const void* i_buf,const
        //正常終了\r
        return NyLPC_TBool_FALSE;\r
 ERROR2:\r
-       NyLPC_cUdpSocket_releaseSendBuf(i_inst,tx);\r
+       NyLPC_iUdpSocket_releaseSendBuf(i_sock,tx);\r
 ERROR1:\r
        return NyLPC_TBool_FALSE;\r
 }\r
 \r
-#define SSDP_NOTIFY_INTERVAL 240*1000  //300*0.8*1000\r
+#define SSDP_NOTIFY_INTERVAL 150*1000  //300*0.5*1000\r
 #define FLAG_ORDER_START_SERVICE       0\r
 #define FLAG_ORDER_STOP_SERVICE                1\r
 #define FLAG_IS_SERVICE_RUNNING                2\r
 \r
-static void onPeriodic(NyLPC_TcUdpSocket_t* i_inst)\r
+static void onPeriodic(NyLPC_TiUdpSocket_t* i_sock)\r
 {\r
-       NyLPC_TcSsdpSocket_t* sock=(NyLPC_TcSsdpSocket_t*)i_inst;\r
-       if(NyLPC_TUInt8_isBitOn(sock->_flags,FLAG_IS_SERVICE_RUNNING)){\r
+       NyLPC_TcSsdpSocket_t* inst=(NyLPC_TcSsdpSocket_t*)i_sock->_tag;\r
+       if(NyLPC_TUInt8_isBitOn(inst->_flags,FLAG_IS_SERVICE_RUNNING)){\r
        //実行中\r
                //停止要求着てる?\r
-               if(NyLPC_TUInt8_isBitOn(sock->_flags,FLAG_ORDER_STOP_SERVICE))\r
+               if(NyLPC_TUInt8_isBitOn(inst->_flags,FLAG_ORDER_STOP_SERVICE))\r
                {\r
                        //状態変更\r
-                       NyLPC_TUInt8_unsetBit(sock->_flags,FLAG_IS_SERVICE_RUNNING);\r
+                       NyLPC_TUInt8_unsetBit(inst->_flags,FLAG_IS_SERVICE_RUNNING);\r
                        //要求フラグクリア\r
-                       NyLPC_TUInt8_unsetBit(sock->_flags,FLAG_ORDER_STOP_SERVICE);\r
+                       NyLPC_TUInt8_unsetBit(inst->_flags,FLAG_ORDER_STOP_SERVICE);\r
                        //@bug ByeBye送信しろ\r
-               }else if(NyLPC_cStopwatch_isExpired(&sock->_periodic_sw)){\r
+               }else if(NyLPC_cStopwatch_isExpired(&inst->_periodic_sw)){\r
                        //Notify送信\r
-                       NyLPC_cSsdpSocket_notify(sock);\r
+                       NyLPC_cSsdpSocket_notify(inst);\r
                        //タイマ再始動\r
-                       NyLPC_cStopwatch_startExpire(&sock->_periodic_sw,SSDP_NOTIFY_INTERVAL);\r
+                       NyLPC_cStopwatch_startExpire(&inst->_periodic_sw,SSDP_NOTIFY_INTERVAL);\r
                }\r
        }else{\r
        //停止中\r
                //開始要求着てる?\r
-               if(NyLPC_TUInt8_isBitOn(sock->_flags,FLAG_ORDER_START_SERVICE))\r
+               if(NyLPC_TUInt8_isBitOn(inst->_flags,FLAG_ORDER_START_SERVICE))\r
                {\r
                        //状態変更\r
-                       NyLPC_TUInt8_setBit(sock->_flags,FLAG_IS_SERVICE_RUNNING);\r
+                       NyLPC_TUInt8_setBit(inst->_flags,FLAG_IS_SERVICE_RUNNING);\r
                        //要求フラグクリア\r
-                       NyLPC_TUInt8_unsetBit(sock->_flags,FLAG_ORDER_START_SERVICE);\r
+                       NyLPC_TUInt8_unsetBit(inst->_flags,FLAG_ORDER_START_SERVICE);\r
                        //次回expireするように\r
-                       NyLPC_cStopwatch_startExpire(&sock->_periodic_sw,SSDP_NOTIFY_INTERVAL);\r
+                       NyLPC_cStopwatch_startExpire(&inst->_periodic_sw,SSDP_NOTIFY_INTERVAL);\r
                }\r
        }\r
 }\r
@@ -540,11 +540,13 @@ void NyLPC_cSsdpSocket_initialize(
                const struct NyLPC_TUPnPDevDescDevice* i_ref_dev_record,\r
                NyLPC_TUInt16 i_server_port,const NyLPC_TChar* i_ref_location_path)\r
 {\r
-       NyLPC_cUdpSocket_initialize(&(i_inst->super),1900,NULL,0);\r
-       NyLPC_cUdpSocket_setOnRxHandler(&(i_inst->super),onPacket);\r
-       NyLPC_cUdpSocket_setOnPeriodicHandler(&(i_inst->super),onPeriodic);\r
+       i_inst->_socket=NyLPC_cNet_createUdpSocketEx(1900,NyLPC_TSocketType_UDP_NOBUF);\r
+    i_inst->_socket->_tag=i_inst;\r
+\r
+       NyLPC_iUdpSocket_setOnRxHandler(i_inst->_socket,onPacket);\r
+       NyLPC_iUdpSocket_setOnPeriodicHandler(i_inst->_socket,onPeriodic);\r
 \r
-       NyLPC_cUdpSocket_joinMulticast(&(i_inst->super),&SSDP_MCAST_IPADDR);\r
+       NyLPC_iUdpSocket_joinMulticast(i_inst->_socket,&SSDP_MCAST_IPADDR);\r
        i_inst->_flags=0;\r
        NyLPC_cStopwatch_initialize(&(i_inst->_periodic_sw));\r
        i_inst->number_of_device=0;\r
@@ -555,7 +557,7 @@ void NyLPC_cSsdpSocket_initialize(
 void NyLPC_cSsdpSocket_finalize(NyLPC_TcSsdpSocket_t* i_inst)\r
 {\r
        NyLPC_cStopwatch_finalize(&(i_inst->_periodic_sw));\r
-       NyLPC_cUdpSocket_finalize(&(i_inst->super));\r
+       NyLPC_iUdpSocket_finalize(i_inst->_socket);\r
 }\r
 \r
 void NyLPC_cSsdpSocket_start(NyLPC_TcSsdpSocket_t* i_inst)\r
@@ -598,7 +600,7 @@ void NyLPC_cSsdpSocket_notify(NyLPC_TcSsdpSocket_t* i_inst)
        if(tx==NULL){\r
                NyLPC_OnErrorGoto(ERROR1);\r
        }\r
-       if(!NyLPC_cUdpSocket_psend(&i_inst->super,&SSDP_MCAST_IPADDR,1900,tx,tx_len)){\r
+       if(!NyLPC_iUdpSocket_psend(i_inst->_socket,&SSDP_MCAST_IPADDR,1900,tx,tx_len)){\r
                NyLPC_OnErrorGoto(ERROR2);\r
        }\r
        //all device\r
@@ -611,7 +613,7 @@ void NyLPC_cSsdpSocket_notify(NyLPC_TcSsdpSocket_t* i_inst)
                if(tx==NULL){\r
                        NyLPC_OnErrorGoto(ERROR1);\r
                }\r
-               if(!NyLPC_cUdpSocket_psend(&i_inst->super,&SSDP_MCAST_IPADDR,1900,tx,tx_len)){\r
+               if(!NyLPC_iUdpSocket_psend(i_inst->_socket,&SSDP_MCAST_IPADDR,1900,tx,tx_len)){\r
                        NyLPC_OnErrorGoto(ERROR2);\r
                }\r
                //devicatype\r
@@ -622,7 +624,7 @@ void NyLPC_cSsdpSocket_notify(NyLPC_TcSsdpSocket_t* i_inst)
                if(tx==NULL){\r
                        NyLPC_OnErrorGoto(ERROR1);\r
                }\r
-               if(!NyLPC_cUdpSocket_psend(&i_inst->super,&SSDP_MCAST_IPADDR,1900,tx,tx_len)){\r
+               if(!NyLPC_iUdpSocket_psend(i_inst->_socket,&SSDP_MCAST_IPADDR,1900,tx,tx_len)){\r
                        NyLPC_OnErrorGoto(ERROR2);\r
                }\r
                for(i2=0;i2<i_inst->ref_device_record[i]->number_of_service;i2++){\r
@@ -633,14 +635,14 @@ void NyLPC_cSsdpSocket_notify(NyLPC_TcSsdpSocket_t* i_inst)
                        if(tx==NULL){\r
                                NyLPC_OnErrorGoto(ERROR1);\r
                        }\r
-                       if(!NyLPC_cUdpSocket_psend(&i_inst->super,&SSDP_MCAST_IPADDR,1900,tx,tx_len)){\r
+                       if(!NyLPC_iUdpSocket_psend(i_inst->_socket,&SSDP_MCAST_IPADDR,1900,tx,tx_len)){\r
                                NyLPC_OnErrorGoto(ERROR2);\r
                        }\r
                }\r
        }\r
        return;\r
 ERROR2:\r
-       NyLPC_cUdpSocket_releaseSendBuf(&i_inst->super,tx);\r
+       NyLPC_iUdpSocket_releaseSendBuf(i_inst->_socket,tx);\r
 ERROR1:\r
        return;\r
 }\r