📄 upnp_tv_device.c
字号:
UpnpAcceptSubscription( device_handle, sr_event->UDN, sr_event->ServiceId, ( const char ** )tv_service_table[i]. VariableName, ( const char ** )tv_service_table[i]. VariableStrVal, tv_service_table[i].VariableCount, sr_event->Sid ); } } ithread_mutex_unlock( &TVDevMutex ); return ( 1 );}/****************************************************************************** * TvDeviceHandleGetVarRequest * * Description: * Called during a get variable request callback. If the * request is for this device and either its control service * or picture service, then respond with the variable value. * * Parameters: * cgv_event -- The control get variable request event structure * *****************************************************************************/intTvDeviceHandleGetVarRequest( INOUT struct Upnp_State_Var_Request *cgv_event ){ unsigned int i = 0, j = 0; int getvar_succeeded = 0; cgv_event->CurrentVal = NULL; ithread_mutex_lock( &TVDevMutex ); for( i = 0; i < TV_SERVICE_SERVCOUNT; i++ ) { //check udn and service id if( ( strcmp( cgv_event->DevUDN, tv_service_table[i].UDN ) == 0 ) && ( strcmp( cgv_event->ServiceID, tv_service_table[i].ServiceId ) == 0 ) ) { //check variable name for( j = 0; j < tv_service_table[i].VariableCount; j++ ) { if( strcmp( cgv_event->StateVarName, tv_service_table[i].VariableName[j] ) == 0 ) { getvar_succeeded = 1; cgv_event->CurrentVal = ixmlCloneDOMString( tv_service_table[i]. VariableStrVal[j] ); break; } } } } if( getvar_succeeded ) { cgv_event->ErrCode = UPNP_E_SUCCESS; } else { SampleUtil_Print ( "Error in UPNP_CONTROL_GET_VAR_REQUEST callback:\n" ); SampleUtil_Print( " Unknown variable name = %s\n", cgv_event->StateVarName ); cgv_event->ErrCode = 404; strcpy( cgv_event->ErrStr, "Invalid Variable" ); } ithread_mutex_unlock( &TVDevMutex ); return ( cgv_event->ErrCode == UPNP_E_SUCCESS );}/****************************************************************************** * TvDeviceHandleActionRequest * * Description: * Called during an action request callback. If the * request is for this device and either its control service * or picture service, then perform the action and respond. * * Parameters: * ca_event -- The control action request event structure * *****************************************************************************/intTvDeviceHandleActionRequest( INOUT struct Upnp_Action_Request *ca_event ){ /* Defaults if action not found */ int action_found = 0; int i = 0; int service = -1; int retCode = 0; char *errorString = NULL; ca_event->ErrCode = 0; ca_event->ActionResult = NULL; if( ( strcmp( ca_event->DevUDN, tv_service_table[TV_SERVICE_CONTROL].UDN ) == 0 ) && ( strcmp ( ca_event->ServiceID, tv_service_table[TV_SERVICE_CONTROL].ServiceId ) == 0 ) ) { /* Request for action in the TvDevice Control Service */ service = TV_SERVICE_CONTROL; } else if( ( strcmp( ca_event->DevUDN, tv_service_table[TV_SERVICE_PICTURE].UDN ) == 0 ) && ( strcmp ( ca_event->ServiceID, tv_service_table[TV_SERVICE_PICTURE].ServiceId ) == 0 ) ) { /* Request for action in the TvDevice Picture Service */ service = TV_SERVICE_PICTURE; } //Find and call appropriate procedure based on action name //Each action name has an associated procedure stored in the //service table. These are set at initialization. for( i = 0; ( ( i < TV_MAXACTIONS ) && ( tv_service_table[service].ActionNames[i] != NULL ) ); i++ ) { if( !strcmp( ca_event->ActionName, tv_service_table[service].ActionNames[i] ) ) { if( ( !strcmp( tv_service_table[TV_SERVICE_CONTROL]. VariableStrVal[TV_CONTROL_POWER], "1" ) ) || ( !strcmp( ca_event->ActionName, "PowerOn" ) ) ) { retCode = tv_service_table[service].actions[i] ( ca_event-> ActionRequest, &ca_event-> ActionResult, &errorString ); } else { errorString = "Power is Off"; retCode = UPNP_E_INTERNAL_ERROR; } action_found = 1; break; } } if( !action_found ) { ca_event->ActionResult = NULL; strcpy( ca_event->ErrStr, "Invalid Action" ); ca_event->ErrCode = 401; } else { if( retCode == UPNP_E_SUCCESS ) { ca_event->ErrCode = UPNP_E_SUCCESS; } else { //copy the error string strcpy( ca_event->ErrStr, errorString ); switch ( retCode ) { case UPNP_E_INVALID_PARAM: { ca_event->ErrCode = 402; break; } case UPNP_E_INTERNAL_ERROR: default: { ca_event->ErrCode = 501; break; } } } } return ( ca_event->ErrCode );}/****************************************************************************** * TvDeviceSetServiceTableVar * * Description: * Update the TvDevice service state table, and notify all subscribed * control points of the updated state. Note that since this function * blocks on the mutex TVDevMutex, to avoid a hang this function should * not be called within any other function that currently has this mutex * locked. * * Parameters: * service -- The service number (TV_SERVICE_CONTROL or TV_SERVICE_PICTURE) * variable -- The variable number (TV_CONTROL_POWER, TV_CONTROL_CHANNEL, * TV_CONTROL_VOLUME, TV_PICTURE_COLOR, TV_PICTURE_TINT, * TV_PICTURE_CONTRAST, or TV_PICTURE_BRIGHTNESS) * value -- The string representation of the new value * *****************************************************************************/intTvDeviceSetServiceTableVar( IN unsigned int service, IN unsigned int variable, IN char *value ){ //IXML_Document *PropSet= NULL; if( ( service >= TV_SERVICE_SERVCOUNT ) || ( variable >= tv_service_table[service].VariableCount ) || ( strlen( value ) >= TV_MAX_VAL_LEN ) ) { return ( 0 ); } ithread_mutex_lock( &TVDevMutex ); strcpy( tv_service_table[service].VariableStrVal[variable], value ); /* //Using utility api PropSet= UpnpCreatePropertySet(1,tv_service_table[service]. VariableName[variable], tv_service_table[service]. VariableStrVal[variable]); UpnpNotifyExt(device_handle, tv_service_table[service].UDN, tv_service_table[service].ServiceId,PropSet); //Free created property set Document_free(PropSet); */ UpnpNotify( device_handle, tv_service_table[service].UDN, tv_service_table[service].ServiceId, ( const char ** )&tv_service_table[service]. VariableName[variable], ( const char ** )&tv_service_table[service]. VariableStrVal[variable], 1 ); ithread_mutex_unlock( &TVDevMutex ); return ( 1 );}/****************************************************************************** * TvDeviceSetPower * * Description: * Turn the power on/off, update the TvDevice control service * state table, and notify all subscribed control points of the * updated state. * * Parameters: * on -- If 1, turn power on. If 0, turn power off. * *****************************************************************************/intTvDeviceSetPower( IN int on ){ char value[TV_MAX_VAL_LEN]; int ret = 0; if( on != POWER_ON && on != POWER_OFF ) { SampleUtil_Print( "error: can't set power to value %d\n", on ); return ( 0 ); } /* Vendor-specific code to turn the power on/off goes here */ sprintf( value, "%d", on ); ret = TvDeviceSetServiceTableVar( TV_SERVICE_CONTROL, TV_CONTROL_POWER, value ); return ( ret );}/****************************************************************************** * TvDevicePowerOn * * Description: * Turn the power on. * * Parameters: * * IXML_Document * in - document of action request * IXML_Document **out - action result * char **errorString - errorString (in case action was unsuccessful) * *****************************************************************************/intTvDevicePowerOn( IN IXML_Document * in, OUT IXML_Document ** out, OUT char **errorString ){ ( *out ) = NULL; ( *errorString ) = NULL; if( TvDeviceSetPower( POWER_ON ) ) { //create a response if( UpnpAddToActionResponse( out, "PowerOn", TvServiceType[TV_SERVICE_CONTROL], "Power", "1" ) != UPNP_E_SUCCESS ) { ( *out ) = NULL; ( *errorString ) = "Internal Error"; return UPNP_E_INTERNAL_ERROR; } return UPNP_E_SUCCESS; } else { ( *errorString ) = "Internal Error"; return UPNP_E_INTERNAL_ERROR; }}/****************************************************************************** * TvDevicePowerOff * * Description: * Turn the power off. * * Parameters: * * IXML_Document * in - document of action request * IXML_Document **out - action result * char **errorString - errorString (in case action was unsuccessful) * *****************************************************************************/intTvDevicePowerOff( IN IXML_Document * in, OUT IXML_Document ** out, OUT char **errorString ){ ( *out ) = NULL; ( *errorString ) = NULL; if( TvDeviceSetPower( POWER_OFF ) ) { //create a response if( UpnpAddToActionResponse( out, "PowerOff", TvServiceType[TV_SERVICE_CONTROL], "Power", "0" ) != UPNP_E_SUCCESS ) { ( *out ) = NULL; ( *errorString ) = "Internal Error"; return UPNP_E_INTERNAL_ERROR; } return UPNP_E_SUCCESS; } ( *errorString ) = "Internal Error"; return UPNP_E_INTERNAL_ERROR;}/****************************************************************************** * TvDeviceSetChannel * * Description: * Change the channel, update the TvDevice control service * state table, and notify all subscribed control points of the * updated state. * * Parameters: * * IXML_Document * in - action request document * IXML_Document **out - action result document * char **errorString - errorString (in case action was unsuccessful) * *****************************************************************************/intTvDeviceSetChannel( IN IXML_Document * in, OUT IXML_Document ** out, OUT char **errorString ){ char *value = NULL; int channel = 0; ( *out ) = NULL; ( *errorString ) = NULL; if( !( value = SampleUtil_GetFirstDocumentItem( in, "Channel" ) ) ) { ( *errorString ) = "Invalid Channel"; return UPNP_E_INVALID_PARAM; } channel = atoi( value ); if( channel < MIN_CHANNEL || channel > MAX_CHANNEL ) { free( value ); SampleUtil_Print( "error: can't change to channel %d\n", channel ); ( *errorString ) = "Invalid Channel"; return UPNP_E_INVALID_PARAM; } /* Vendor-specific code to set the channel goes here */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -