📄 upnpapi.c
字号:
* 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; } 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->ClientSubList = NULL; ListInit( &HInfo->SsdpSearchList, NULL, NULL );#ifdef INCLUDE_DEVICE_APIS HInfo->MaxAge = 0; HInfo->MaxSubscriptions = UPNP_INFINITE; HInfo->MaxSubscriptionTimeOut = UPNP_INFINITE;#endif HandleTable[*Hnd] = HInfo; UpnpSdkClientRegistered = 1; HandleUnlock(); UpnpPrintf( UPNP_ALL, API, __FILE__, __LINE__, "Exiting UpnpRegisterClient \n" ); return UPNP_E_SUCCESS;} /****************** End of UpnpRegisterClient *********************/#endif // 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. *****************************************************************************/#ifdef INCLUDE_CLIENT_APISintUpnpUnRegisterClient( IN UpnpClient_Handle Hnd ){ struct Handle_Info *HInfo; ListNode *node = NULL; SsdpSearchArg *searchArg = NULL; if( UpnpSdkInit != 1 ) { return UPNP_E_FINISH; } 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(); 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; } 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(); 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: UpnpSearchAsync * * 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; } UpnpPrintf( UPNP_ALL, API, __FILE__, __LINE__, "Inside UpnpSearchAsync \n" ); HandleReadLock(); 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(); 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. ***************************************************************************/intUpnpSetMaxSubscriptions( IN UpnpDevice_Handle Hnd, IN int MaxSubscriptions ){ struct Handle_Info *SInfo = NULL; if( UpnpSdkInit != 1 ) { return UPNP_E_FINISH; } UpnpPrintf( UPNP_ALL, API, __FILE__, __LINE__, "Inside UpnpSetMaxSubscriptions \n" ); HandleLock(); if( ( ( MaxSubscriptions != UPNP_INFINITE ) && ( MaxSubscriptions < 0 ) ) || ( GetHandleInfo( Hnd, &SInfo ) != HND_DEVICE ) ) { HandleUnlock(); return UPNP_E_INVALID_HANDLE; } SInfo->MaxSubscriptions = MaxSubscriptions; HandleUnlock(); UpnpPrintf( UPNP_ALL, API, __FILE__, __LINE__, "Exiting UpnpSetMaxSubscriptions \n" ); return UPNP_E_SUCCESS;} /***************** End of UpnpSetMaxSubscriptions ********************/#endif // INCLUDE_DEVICE_APIS#ifdef INCLUDE_DEVICE_APIS/************************************************************************** * Function: UpnpSetMaxSubscriptionTimeOut * * Parameters: * IN UpnpDevice_Handle Hnd: The handle of the device for which the * maximum subscription time-out is being set. * IN int MaxSubscriptionTimeOut:The maximum subscription time-out * to be accepted * * Description: * This function sets the maximum subscription timer. Control points * will require to send the subscription request before timeout. * * Return Values: int * UPNP_E_SUCCESS if successful else sends appropriate error. ***************************************************************************/intUpnpSetMaxSubscriptionTimeOut( IN UpnpDevice_Handle Hnd, IN int MaxSubscriptionTimeOut ){ struct Handle_Info *SInfo = NULL; if( UpnpSdkInit != 1 ) { return UPNP_E_FINISH; } UpnpPrintf( UPNP_ALL, API, __FILE__, __LINE__, "Inside UpnpSetMaxSubscriptionTimeOut \n" ); HandleLock(); if( ( ( MaxSubscriptionTimeOut != UPNP_INFINITE ) && ( MaxSubscriptionTimeOut < 0 ) ) || ( GetHandleInfo( Hnd, &SInfo ) != HND_DEVICE ) ) { HandleUnlock(); return UPNP_E_INVALID_HANDLE; } SInfo->MaxSubscriptionTimeOut = MaxSubscriptionTimeOut; HandleUnlock(); UpnpPrintf( UPNP_ALL, API, __FILE__, __LINE__, "Exiting UpnpSetMaxSubscriptionTimeOut \n" ); return UPNP_E_SUCCESS;} /****************** End of UpnpSetMaxSubscriptionTimeOut ******************/#endif // INCLUDE_DEVICE_APIS#ifdef INCLUDE_CLIENT_APIS/************************************************************************** * Function: UpnpSubscribeAsync * * Parameters: * IN UpnpClient_Handle Hnd: The handle of the control point for which * the subscription request is to be sent. * IN const char * EvtUrl_const: URL that control point wants to * subscribe * IN int TimeOut: The requested subscription time. Upon * return, it contains the actual subscription time * returned from the service * IN Upnp_FunPtr Fun : callback function to tell result of the * subscription request * IN const void * Cookie_const: cookie passed by client to give back * in the callback function.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -