📄 upnpapi.c
字号:
FreeHandle( *Hnd ); HandleUnlock( ); DBGONLY( UpnpPrintf( UPNP_INFO, API, __FILE__, __LINE__, "UpnpRegisterRootDevice2: No services found for RootDevice\n" ); ) return UPNP_E_INVALID_DESC; } DBGONLY( UpnpPrintf( UPNP_ALL, API, __FILE__, __LINE__, "UpnpRegisterRootDevice2: Gena Check\n" ); ) //******************************* //GENA SET UP //******************************* if( getServiceTable( ( IXML_Node * ) HInfo->DescDocument, &HInfo->ServiceTable, HInfo->DescURL ) ) { DBGONLY( UpnpPrintf( UPNP_ALL, API, __FILE__, __LINE__, "UpnpRegisterRootDevice2: GENA Service Table \n" ); ) } else { CLIENTONLY( ListDestroy( &HInfo->SsdpSearchList, 0 ) ); FreeHandle( *Hnd ); HandleUnlock( ); DBGONLY( UpnpPrintf( UPNP_INFO, API, __FILE__, __LINE__, "\nUpnpRegisterRootDevice: Errors retrieving service table \n" ); ) return UPNP_E_INVALID_DESC; } UpnpSdkDeviceRegistered = 1; HandleUnlock( ); DBGONLY( UpnpPrintf( UPNP_ALL, API, __FILE__, __LINE__, "Exiting RegisterRootDevice2 Successfully\n" ); ) return UPNP_E_SUCCESS;} /****************** End of UpnpRegisterRootDevice2 *********************/#endif //INCLUDE_DEVICE_APIS#ifdef INCLUDE_CLIENT_APIS/************************************************************************** * Function: UpnpRegisterClient * * Parameters: * IN Upnp_FunPtr Fun: Pointer to a function for receiving * asynchronous events. * IN const void * Cookie: Pointer to user data returned with the * callback function when invoked. * OUT UpnpClient_Handle *Hnd: Pointer to a variable to store * the new control point handle. * * Description: * This function registers a control point application with the * UPnP Library. A control point application cannot make any other API * calls until it registers using this function. * * Return Values: int * ***************************************************************************/intUpnpRegisterClient( IN Upnp_FunPtr Fun, IN const void *Cookie, OUT UpnpClient_Handle * Hnd ){ struct Handle_Info *HInfo; if( UpnpSdkInit != 1 ) { return UPNP_E_FINISH; } DBGONLY( UpnpPrintf( UPNP_ALL, API, __FILE__, __LINE__, "Inside UpnpRegisterClient \n" ); ) if( Fun == NULL || Hnd == NULL ) { return UPNP_E_INVALID_PARAM; } HandleLock( ); if( UpnpSdkClientRegistered ) { HandleUnlock( ); return UPNP_E_ALREADY_REGISTERED; } if( ( *Hnd = GetFreeHandle( ) ) == UPNP_E_OUTOF_HANDLE ) { HandleUnlock( ); return UPNP_E_OUTOF_MEMORY; } HInfo = ( struct Handle_Info * )malloc( sizeof( struct Handle_Info ) ); if( HInfo == NULL ) { HandleUnlock( ); return UPNP_E_OUTOF_MEMORY; } HInfo->HType = HND_CLIENT; HInfo->Callback = Fun; HInfo->Cookie = ( void * )Cookie; HInfo->MaxAge = 0; HInfo->ClientSubList = NULL; ListInit( &HInfo->SsdpSearchList, NULL, NULL ); DEVICEONLY( HInfo->MaxSubscriptions = UPNP_INFINITE; ) DEVICEONLY( HInfo->MaxSubscriptionTimeOut = UPNP_INFINITE; ) HandleTable[*Hnd] = HInfo; UpnpSdkClientRegistered = 1; HandleUnlock( ); DBGONLY( UpnpPrintf( UPNP_ALL, API, __FILE__, __LINE__, "Exiting UpnpRegisterClient \n" ); ) return UPNP_E_SUCCESS;} /****************** End of UpnpRegisterClient *********************/#endif // INCLUDE_CLIENT_APIS#ifdef INCLUDE_CLIENT_APIS/**************************************************************************** * Function: UpnpUnRegisterClient * * Parameters: * IN UpnpClient_Handle Hnd: The handle of the control point instance * to unregister * Description: * This function unregisters a client registered with * UpnpRegisterclient or UpnpRegisterclient2. After this call, the * UpnpDevice_Handle Hnd is no longer valid. The UPnP Library generates * no more callbacks after this function returns. * * Return Values: * UPNP_E_SUCCESS on success, nonzero on failure. *****************************************************************************/intUpnpUnRegisterClient( IN UpnpClient_Handle Hnd ){ struct Handle_Info *HInfo; ListNode *node = NULL; SsdpSearchArg *searchArg = NULL; if( UpnpSdkInit != 1 ) { return UPNP_E_FINISH; } DBGONLY( UpnpPrintf( UPNP_ALL, API, __FILE__, __LINE__, "Inside UpnpUnRegisterClient \n" ); ) HandleLock( ); if( !UpnpSdkClientRegistered ) { HandleUnlock( ); return UPNP_E_INVALID_HANDLE; } HandleUnlock( );#if EXCLUDE_GENA == 0 if( genaUnregisterClient( Hnd ) != UPNP_E_SUCCESS ) return UPNP_E_INVALID_HANDLE;#endif HandleLock( ); if( GetHandleInfo( Hnd, &HInfo ) == UPNP_E_INVALID_HANDLE ) { HandleUnlock( ); return UPNP_E_INVALID_HANDLE; } //clean up search list node = ListHead( &HInfo->SsdpSearchList ); while( node != NULL ) { searchArg = ( SsdpSearchArg * ) node->item; if( searchArg ) { free( searchArg->searchTarget ); free( searchArg ); } ListDelNode( &HInfo->SsdpSearchList, node, 0 ); node = ListHead( &HInfo->SsdpSearchList ); } ListDestroy( &HInfo->SsdpSearchList, 0 ); FreeHandle( Hnd ); UpnpSdkClientRegistered = 0; HandleUnlock( ); DBGONLY( UpnpPrintf( UPNP_ALL, API, __FILE__, __LINE__, "Exiting UpnpUnRegisterClient \n" ); ) return UPNP_E_SUCCESS;} /****************** End of UpnpUnRegisterClient *********************/#endif // INCLUDE_CLIENT_APIS//-----------------------------------------------------------------------------//// SSDP interface////-----------------------------------------------------------------------------#ifdef INCLUDE_DEVICE_APIS#if EXCLUDE_SSDP == 0/************************************************************************** * Function: UpnpSendAdvertisement * * Parameters: * IN UpnpDevice_Handle Hnd: handle of the device instance * IN int Exp : Timer for resending the advertisement * * Description: * This function sends the device advertisement. It also schedules a * job for the next advertisement after "Exp" time. * * Return Values: int * UPNP_E_SUCCESS if successful else sends appropriate error. ***************************************************************************/intUpnpSendAdvertisement( IN UpnpDevice_Handle Hnd, IN int Exp ){ struct Handle_Info *SInfo = NULL; int retVal = 0, *ptrMx; upnp_timeout *adEvent; ThreadPoolJob job; if( UpnpSdkInit != 1 ) { return UPNP_E_FINISH; } DBGONLY( UpnpPrintf( UPNP_ALL, API, __FILE__, __LINE__, "Inside UpnpSendAdvertisement \n" ); ) HandleLock( ); if( GetHandleInfo( Hnd, &SInfo ) != HND_DEVICE ) { HandleUnlock( ); return UPNP_E_INVALID_HANDLE; } if( Exp < 1 ) Exp = DEFAULT_MAXAGE; SInfo->MaxAge = Exp; HandleUnlock( ); retVal = AdvertiseAndReply( 1, Hnd, 0, ( struct sockaddr_in * )NULL, ( char * )NULL, ( char * )NULL, ( char * )NULL, Exp ); if( retVal != UPNP_E_SUCCESS ) return retVal; ptrMx = ( int * )malloc( sizeof( int ) ); if( ptrMx == NULL ) return UPNP_E_OUTOF_MEMORY; adEvent = ( upnp_timeout * ) malloc( sizeof( upnp_timeout ) ); if( adEvent == NULL ) { free( ptrMx ); return UPNP_E_OUTOF_MEMORY; } *ptrMx = Exp; adEvent->handle = Hnd; adEvent->Event = ptrMx; HandleLock( ); if( GetHandleInfo( Hnd, &SInfo ) != HND_DEVICE ) { HandleUnlock( ); free( adEvent ); free( ptrMx ); return UPNP_E_INVALID_HANDLE; }#ifdef SSDP_PACKET_DISTRIBUTE TPJobInit( &job, ( start_routine ) AutoAdvertise, adEvent ); TPJobSetFreeFunction( &job, ( free_routine ) free_upnp_timeout ); TPJobSetPriority( &job, MED_PRIORITY ); if( ( retVal = TimerThreadSchedule( &gTimerThread, ( ( Exp / 2 ) - ( AUTO_ADVERTISEMENT_TIME ) ), REL_SEC, &job, SHORT_TERM, &( adEvent->eventId ) ) ) != UPNP_E_SUCCESS ) { HandleUnlock( ); free( adEvent ); free( ptrMx ); return retVal; }#else TPJobInit( &job, ( start_routine ) AutoAdvertise, adEvent ); TPJobSetFreeFunction( &job, ( free_routine ) free_upnp_timeout ); TPJobSetPriority( &job, MED_PRIORITY ); if( ( retVal = TimerThreadSchedule( &gTimerThread, Exp - AUTO_ADVERTISEMENT_TIME, REL_SEC, &job, SHORT_TERM, &( adEvent->eventId ) ) ) != UPNP_E_SUCCESS ) { HandleUnlock( ); free( adEvent ); free( ptrMx ); return retVal; }#endif HandleUnlock( ); DBGONLY( UpnpPrintf( UPNP_ALL, API, __FILE__, __LINE__, "Exiting UpnpSendAdvertisement \n" ); ) return retVal;} /****************** End of UpnpSendAdvertisement *********************/#endif // INCLUDE_DEVICE_APIS#endif#if EXCLUDE_SSDP == 0#ifdef INCLUDE_CLIENT_APIS/************************************************************************** * Function: UpnpSendAdvertisement * * Parameters: * IN UpnpClient_Handle Hnd: handle of the control point instance * IN int Mx : Maximum time to wait for the search reply * IN const char *Target_const: * IN const void *Cookie_const: * * Description: * This function searches for the devices for the provided maximum time. * It is a asynchronous function. It schedules a search job and returns. * client is notified about the search results after search timer. * * Return Values: int * UPNP_E_SUCCESS if successful else sends appropriate error. ***************************************************************************/intUpnpSearchAsync( IN UpnpClient_Handle Hnd, IN int Mx, IN const char *Target_const, IN const void *Cookie_const ){ struct Handle_Info *SInfo = NULL; char *Target = ( char * )Target_const; if( UpnpSdkInit != 1 ) { return UPNP_E_FINISH; } DBGONLY( UpnpPrintf( UPNP_ALL, API, __FILE__, __LINE__, "Inside UpnpSearchAsync \n" ); ) HandleLock( ); if( GetHandleInfo( Hnd, &SInfo ) != HND_CLIENT ) { HandleUnlock( ); return UPNP_E_INVALID_HANDLE; } if( Mx < 1 ) Mx = DEFAULT_MX; if( Target == NULL ) { HandleUnlock( ); return UPNP_E_INVALID_PARAM; } HandleUnlock( ); SearchByTarget( Mx, Target, ( void * )Cookie_const ); //HandleUnlock(); DBGONLY( UpnpPrintf( UPNP_ALL, API, __FILE__, __LINE__, "Exiting UpnpSearchAsync \n" ); ) return UPNP_E_SUCCESS;} /****************** End of UpnpSearchAsync *********************/#endif // INCLUDE_CLIENT_APIS#endif//-----------------------------------------------------------------------------//// GENA interface ////-----------------------------------------------------------------------------#if EXCLUDE_GENA == 0#ifdef INCLUDE_DEVICE_APIS/************************************************************************** * Function: UpnpSetMaxSubscriptions * * Parameters: * IN UpnpDevice_Handle Hnd: The handle of the device for which * the maximum subscriptions is being set. * IN int MaxSubscriptions: The maximum number of subscriptions to be * allowed per service. * * Description: * This function sets the maximum subscriptions of the control points * Return Values: int * UPNP_E_SUCCESS if successful else sends appropriate error. ***************************************************************************/int
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -