Esempio n. 1
0
/********************************************************************************
 * TvCtrlPointCallbackEventHandler
 *
 * Description: 
 *       The callback handler registered with the SDK while registering
 *       the control point.  Detects the type of callback, and passes the 
 *       request on to the appropriate function.
 *
 * Parameters:
 *   EventType -- The type of callback event
 *   Event -- Data structure containing event data
 *   Cookie -- Optional data specified during callback registration
 *
 ********************************************************************************/
int TvCtrlPointCallbackEventHandler(Upnp_EventType EventType, const void *Event, void *Cookie)
{
	int errCode = 0;

	SampleUtil_PrintEvent(EventType, Event);
	switch ( EventType ) {
	/* SSDP Stuff */
	case UPNP_DISCOVERY_ADVERTISEMENT_ALIVE:
	case UPNP_DISCOVERY_SEARCH_RESULT: {
		const UpnpDiscovery *d_event = (UpnpDiscovery *)Event;
		IXML_Document *DescDoc = NULL;
		const char *location = NULL;
		int errCode = UpnpDiscovery_get_ErrCode(d_event);

		if (errCode != UPNP_E_SUCCESS) {
			SampleUtil_Print(
				"Error in Discovery Callback -- %d\n", errCode);
		}

		location = UpnpString_get_String(UpnpDiscovery_get_Location(d_event));
		errCode = UpnpDownloadXmlDoc(location, &DescDoc);
		if (errCode != UPNP_E_SUCCESS) {
			SampleUtil_Print(
				"Error obtaining device description from %s -- error = %d\n",
				location, errCode);
		} else {
			TvCtrlPointAddDevice(
				DescDoc, location, UpnpDiscovery_get_Expires(d_event));
		}
		if (DescDoc) {
			ixmlDocument_free(DescDoc);
		}
		TvCtrlPointPrintList();
		break;
	}
	case UPNP_DISCOVERY_SEARCH_TIMEOUT:
		/* Nothing to do here... */
		break;
	case UPNP_DISCOVERY_ADVERTISEMENT_BYEBYE: {
		UpnpDiscovery *d_event = (UpnpDiscovery *)Event;
		int errCode = UpnpDiscovery_get_ErrCode(d_event);
		const char *deviceId = UpnpString_get_String(
		UpnpDiscovery_get_DeviceID(d_event));

		if (errCode != UPNP_E_SUCCESS) {
			SampleUtil_Print(
				"Error in Discovery ByeBye Callback -- %d\n", errCode);
		}
		SampleUtil_Print("Received ByeBye for Device: %s\n", deviceId);
		TvCtrlPointRemoveDevice(deviceId);
		SampleUtil_Print("After byebye:\n");
		TvCtrlPointPrintList();
		break;
	}
	/* SOAP Stuff */
	case UPNP_CONTROL_ACTION_COMPLETE: {
		UpnpActionComplete *a_event = (UpnpActionComplete *)Event;
		int errCode = UpnpActionComplete_get_ErrCode(a_event);
		if (errCode != UPNP_E_SUCCESS) {
			SampleUtil_Print("Error in  Action Complete Callback -- %d\n",
				errCode);
		}
		/* No need for any processing here, just print out results.
		 * Service state table updates are handled by events. */
		break;
	}
	case UPNP_CONTROL_GET_VAR_COMPLETE: {
		UpnpStateVarComplete *sv_event = (UpnpStateVarComplete *)Event;
		int errCode = UpnpStateVarComplete_get_ErrCode(sv_event);
		if (errCode != UPNP_E_SUCCESS) {
			SampleUtil_Print(
				"Error in Get Var Complete Callback -- %d\n", errCode);
		} else {
			TvCtrlPointHandleGetVar(
				UpnpString_get_String(UpnpStateVarComplete_get_CtrlUrl(sv_event)),
				UpnpString_get_String(UpnpStateVarComplete_get_StateVarName(sv_event)),
				UpnpStateVarComplete_get_CurrentVal(sv_event));
		}
		break;
	}
	/* GENA Stuff */
	case UPNP_EVENT_RECEIVED: {
		UpnpEvent *e_event = (UpnpEvent *)Event;
		TvCtrlPointHandleEvent(
			UpnpEvent_get_SID_cstr(e_event),
			UpnpEvent_get_EventKey(e_event),
			UpnpEvent_get_ChangedVariables(e_event));
		break;
	}
	case UPNP_EVENT_SUBSCRIBE_COMPLETE:
	case UPNP_EVENT_UNSUBSCRIBE_COMPLETE:
	case UPNP_EVENT_RENEWAL_COMPLETE: {
		UpnpEventSubscribe *es_event = (UpnpEventSubscribe *)Event;

		errCode = UpnpEventSubscribe_get_ErrCode(es_event);
		if (errCode != UPNP_E_SUCCESS) {
			SampleUtil_Print(
				"Error in Event Subscribe Callback -- %d\n", errCode);
		} else {
			TvCtrlPointHandleSubscribeUpdate(
				UpnpString_get_String(UpnpEventSubscribe_get_PublisherUrl(es_event)),
				UpnpString_get_String(UpnpEventSubscribe_get_SID(es_event)),
				UpnpEventSubscribe_get_TimeOut(es_event));
		}
		break;
	}
	case UPNP_EVENT_AUTORENEWAL_FAILED:
	case UPNP_EVENT_SUBSCRIPTION_EXPIRED: {
		UpnpEventSubscribe *es_event = (UpnpEventSubscribe *)Event;
		int TimeOut = default_timeout;
		Upnp_SID newSID;

		errCode = UpnpSubscribe(
			ctrlpt_handle,
			UpnpString_get_String(UpnpEventSubscribe_get_PublisherUrl(es_event)),
			&TimeOut,
			newSID);
		if (errCode == UPNP_E_SUCCESS) {
			SampleUtil_Print("Subscribed to EventURL with SID=%s\n", newSID);
			TvCtrlPointHandleSubscribeUpdate(
				UpnpString_get_String(UpnpEventSubscribe_get_PublisherUrl(es_event)),
				newSID,
				TimeOut);
		} else {
			SampleUtil_Print("Error Subscribing to EventURL -- %d\n", errCode);
		}
		break;
	}
	/* ignore these cases, since this is not a device */
	case UPNP_EVENT_SUBSCRIPTION_REQUEST:
	case UPNP_CONTROL_GET_VAR_REQUEST:
	case UPNP_CONTROL_ACTION_REQUEST:
		break;
	}

	return 0;
	Cookie = Cookie;
}
Esempio n. 2
0
int
TvCtrlPointProcessCommand( char *cmdline )
{
    char cmd[100];
    char strarg[100];
    int arg_val_err = -99999;
    int arg1 = arg_val_err;
    int arg2 = arg_val_err;
    int cmdnum = -1;
    int numofcmds = sizeof( cmdloop_cmdlist ) / sizeof( cmdloop_commands );
    int cmdfound = 0;
    int i,
      rc;
    int invalidargs = 0;
    int validargs;

    validargs = sscanf( cmdline, "%s %d %d", cmd, &arg1, &arg2 );

    for( i = 0; i < numofcmds; i++ ) {
        if( strcasecmp( cmd, cmdloop_cmdlist[i].str ) == 0 ) {
            cmdnum = cmdloop_cmdlist[i].cmdnum;
            cmdfound++;
            if( validargs != cmdloop_cmdlist[i].numargs )
                invalidargs++;
            break;
        }
    }

    if( !cmdfound ) {
        SampleUtil_Print( "Command not found; try 'Help'" );
        return TV_SUCCESS;
    }

    if( invalidargs ) {
        SampleUtil_Print( "Invalid arguments; try 'Help'" );
        return TV_SUCCESS;
    }

    switch ( cmdnum ) {
        case PRTHELP:
            TvCtrlPointPrintShortHelp(  );
            break;

        case PRTFULLHELP:
            TvCtrlPointPrintLongHelp(  );
            break;

        case POWON:
            TvCtrlPointSendPowerOn( arg1 );
            break;

        case POWOFF:
            TvCtrlPointSendPowerOff( arg1 );
            break;

        case SETCHAN:
            TvCtrlPointSendSetChannel( arg1, arg2 );
            break;

        case SETVOL:
            TvCtrlPointSendSetVolume( arg1, arg2 );
            break;

        case SETCOL:
            TvCtrlPointSendSetColor( arg1, arg2 );
            break;

        case SETTINT:
            TvCtrlPointSendSetTint( arg1, arg2 );
            break;

        case SETCONT:
            TvCtrlPointSendSetContrast( arg1, arg2 );
            break;

        case SETBRT:
            TvCtrlPointSendSetBrightness( arg1, arg2 );
            break;

        case CTRLACTION:
            /*
               re-parse commandline since second arg is string 
             */
            validargs = sscanf( cmdline, "%s %d %s", cmd, &arg1, strarg );
            if( 3 == validargs )
                TvCtrlPointSendAction( TV_SERVICE_CONTROL, arg1, strarg,
                                       NULL, NULL, 0 );
            else
                invalidargs++;
            break;

        case PICTACTION:
            /*
               re-parse commandline since second arg is string 
             */
            validargs = sscanf( cmdline, "%s %d %s", cmd, &arg1, strarg );
            if( 3 == validargs )
                TvCtrlPointSendAction( TV_SERVICE_PICTURE, arg1, strarg,
                                       NULL, NULL, 0 );
            else
                invalidargs++;
            break;

        case CTRLGETVAR:
            /*
               re-parse commandline since second arg is string 
             */
            validargs = sscanf( cmdline, "%s %d %s", cmd, &arg1, strarg );
            if( 3 == validargs )
                TvCtrlPointGetVar( TV_SERVICE_CONTROL, arg1, strarg );
            else
                invalidargs++;
            break;

        case PICTGETVAR:
            /*
               re-parse commandline since second arg is string 
             */
            validargs = sscanf( cmdline, "%s %d %s", cmd, &arg1, strarg );
            if( 3 == validargs )
                TvCtrlPointGetVar( TV_SERVICE_PICTURE, arg1, strarg );
            else
                invalidargs++;
            break;

        case PRTDEV:
            TvCtrlPointPrintDevice( arg1 );
            break;

        case LSTDEV:
            TvCtrlPointPrintList(  );
            break;

        case REFRESH:
            TvCtrlPointRefresh(  );
            break;

        case EXITCMD:
            rc = TvCtrlPointStop(  );
            exit( rc );
            break;

        default:
            SampleUtil_Print( "Command not implemented; see 'Help'" );
            break;
    }

    if( invalidargs )
        SampleUtil_Print( "Invalid args in command; see 'Help'" );

    return TV_SUCCESS;
}
Esempio n. 3
0
/********************************************************************************
 * TvCtrlPointCallbackEventHandler
 *
 * Description: 
 *       The callback handler registered with the SDK while registering
 *       the control point.  Detects the type of callback, and passes the 
 *       request on to the appropriate function.
 *
 * Parameters:
 *   EventType -- The type of callback event
 *   Event -- Data structure containing event data
 *   Cookie -- Optional data specified during callback registration
 *
 ********************************************************************************/
int
TvCtrlPointCallbackEventHandler( Upnp_EventType EventType,
                                 void *Event,
                                 void *Cookie )
{
    SampleUtil_PrintEvent( EventType, Event );

    switch ( EventType ) {
            /*
               SSDP Stuff 
             */
        case UPNP_DISCOVERY_ADVERTISEMENT_ALIVE:
        case UPNP_DISCOVERY_SEARCH_RESULT:
            {
                struct Upnp_Discovery *d_event =
                    ( struct Upnp_Discovery * )Event;
                IXML_Document *DescDoc = NULL;
                int ret;

                if( d_event->ErrCode != UPNP_E_SUCCESS ) {
                    SampleUtil_Print( "Error in Discovery Callback -- %d",
                                      d_event->ErrCode );
                }

                if( ( ret =
                      UpnpDownloadXmlDoc( d_event->Location,
                                          &DescDoc ) ) !=
                    UPNP_E_SUCCESS ) {
                    SampleUtil_Print
                        ( "Error obtaining device description from %s -- error = %d",
                          d_event->Location, ret );
                } else {
                    TvCtrlPointAddDevice( DescDoc, d_event->Location,
                                          d_event->Expires );
                }

                if( DescDoc )
                    ixmlDocument_free( DescDoc );

                TvCtrlPointPrintList(  );
                break;
            }

        case UPNP_DISCOVERY_SEARCH_TIMEOUT:
            /*
               Nothing to do here... 
             */
            break;

        case UPNP_DISCOVERY_ADVERTISEMENT_BYEBYE:
            {
                struct Upnp_Discovery *d_event =
                    ( struct Upnp_Discovery * )Event;

                if( d_event->ErrCode != UPNP_E_SUCCESS ) {
                    SampleUtil_Print
                        ( "Error in Discovery ByeBye Callback -- %d",
                          d_event->ErrCode );
                }

                SampleUtil_Print( "Received ByeBye for Device: %s",
                                  d_event->DeviceId );
                TvCtrlPointRemoveDevice( d_event->DeviceId );

                SampleUtil_Print( "After byebye:" );
                TvCtrlPointPrintList(  );

                break;
            }

            /*
               SOAP Stuff 
             */
        case UPNP_CONTROL_ACTION_COMPLETE:
            {
                struct Upnp_Action_Complete *a_event =
                    ( struct Upnp_Action_Complete * )Event;

                if( a_event->ErrCode != UPNP_E_SUCCESS ) {
                    SampleUtil_Print
                        ( "Error in  Action Complete Callback -- %d",
                          a_event->ErrCode );
                }

                /*
                   No need for any processing here, just print out results.  Service state
                   table updates are handled by events. 
                 */

                break;
            }

        case UPNP_CONTROL_GET_VAR_COMPLETE:
            {
                struct Upnp_State_Var_Complete *sv_event =
                    ( struct Upnp_State_Var_Complete * )Event;

                if( sv_event->ErrCode != UPNP_E_SUCCESS ) {
                    SampleUtil_Print
                        ( "Error in Get Var Complete Callback -- %d",
                          sv_event->ErrCode );
                } else {
                    TvCtrlPointHandleGetVar( sv_event->CtrlUrl,
                                             sv_event->StateVarName,
                                             sv_event->CurrentVal );
                }

                break;
            }

            /*
               GENA Stuff 
             */
        case UPNP_EVENT_RECEIVED:
            {
                struct Upnp_Event *e_event = ( struct Upnp_Event * )Event;

                TvCtrlPointHandleEvent( e_event->Sid, e_event->EventKey,
                                        e_event->ChangedVariables );
                break;
            }

        case UPNP_EVENT_SUBSCRIBE_COMPLETE:
        case UPNP_EVENT_UNSUBSCRIBE_COMPLETE:
        case UPNP_EVENT_RENEWAL_COMPLETE:
            {
                struct Upnp_Event_Subscribe *es_event =
                    ( struct Upnp_Event_Subscribe * )Event;

                if( es_event->ErrCode != UPNP_E_SUCCESS ) {
                    SampleUtil_Print
                        ( "Error in Event Subscribe Callback -- %d",
                          es_event->ErrCode );
                } else {
                    TvCtrlPointHandleSubscribeUpdate( es_event->
                                                      PublisherUrl,
                                                      es_event->Sid,
                                                      es_event->TimeOut );
                }

                break;
            }

        case UPNP_EVENT_AUTORENEWAL_FAILED:
        case UPNP_EVENT_SUBSCRIPTION_EXPIRED:
            {
                int TimeOut = default_timeout;
                Upnp_SID newSID;
                int ret;

                struct Upnp_Event_Subscribe *es_event =
                    ( struct Upnp_Event_Subscribe * )Event;

                ret =
                    UpnpSubscribe( ctrlpt_handle, es_event->PublisherUrl,
                                   &TimeOut, newSID );

                if( ret == UPNP_E_SUCCESS ) {
                    SampleUtil_Print( "Subscribed to EventURL with SID=%s",
                                      newSID );
                    TvCtrlPointHandleSubscribeUpdate( es_event->
                                                      PublisherUrl, newSID,
                                                      TimeOut );
                } else {
                    SampleUtil_Print
                        ( "Error Subscribing to EventURL -- %d", ret );
                }
                break;
            }

            /*
               ignore these cases, since this is not a device 
             */
        case UPNP_EVENT_SUBSCRIPTION_REQUEST:
        case UPNP_CONTROL_GET_VAR_REQUEST:
        case UPNP_CONTROL_ACTION_REQUEST:
            break;
    }

    return 0;
}