From ef0aa3895825b472fbc449fb4ffe397a59f6db72 Mon Sep 17 00:00:00 2001 From: Marcelo Roberto Jimenez Date: Thu, 18 Nov 2010 12:02:38 -0200 Subject: [PATCH] samples: More code reorganization. --- upnp/sample/common/common_data.h | 61 - upnp/sample/common/sample_util.c | 16 + upnp/sample/common/sample_util.h | 10 + upnp/sample/common/tv_ctrlpt.c | 1559 +++++++++++-------- upnp/sample/common/tv_ctrlpt.h | 45 +- upnp/sample/common/tv_device.c | 45 + upnp/sample/common/tv_device.h | 16 + upnp/sample/tvcombo/linux/tv_combo_main.c | 321 +--- upnp/sample/tvctrlpt/linux/tv_ctrlpt_main.c | 277 ---- upnp/sample/tvdevice/linux/tv_device_main.c | 68 +- 10 files changed, 1058 insertions(+), 1360 deletions(-) diff --git a/upnp/sample/common/common_data.h b/upnp/sample/common/common_data.h index 47c37f5..03998f5 100644 --- a/upnp/sample/common/common_data.h +++ b/upnp/sample/common/common_data.h @@ -9,67 +9,6 @@ * \file */ -#ifdef ALLOC_CMD_LINE - /*! Tags for valid commands issued at the command prompt. */ - enum cmdloop_tvcmds { - PRTHELP = 0, - PRTFULLHELP, - POWON, - POWOFF, - SETCHAN, - SETVOL, - SETCOL, - SETTINT, - SETCONT, - SETBRT, - CTRLACTION, - PICTACTION, - CTRLGETVAR, - PICTGETVAR, - PRTDEV, - LSTDEV, - REFRESH, - EXITCMD - }; - - /*! Data structure for parsing commands from the command line. */ - struct cmdloop_commands { - /* the string */ - const char *str; - /* the command */ - int cmdnum; - /* the number of arguments */ - int numargs; - /* the args */ - const char *args; - } cmdloop_commands; - - /*! Mappings between command text names, command tag, - * and required command arguments for command line - * commands */ - static struct cmdloop_commands cmdloop_cmdlist[] = { - {"Help", PRTHELP, 1, ""}, - {"HelpFull", PRTFULLHELP, 1, ""}, - {"ListDev", LSTDEV, 1, ""}, - {"Refresh", REFRESH, 1, ""}, - {"PrintDev", PRTDEV, 2, ""}, - {"PowerOn", POWON, 2, ""}, - {"PowerOff", POWOFF, 2, ""}, - {"SetChannel", SETCHAN, 3, " "}, - {"SetVolume", SETVOL, 3, " "}, - {"SetColor", SETCOL, 3, " "}, - {"SetTint", SETTINT, 3, " "}, - {"SetContrast", SETCONT, 3, " "}, - {"SetBrightness", SETBRT, 3, " "}, - {"CtrlAction", CTRLACTION, 2, " "}, - {"PictAction", PICTACTION, 2, " "}, - {"CtrlGetVar", CTRLGETVAR, 2, " "}, - {"PictGetVar", PICTGETVAR, 2, " "}, - {"Exit", EXITCMD, 1, ""} - }; -#else /* ALLOC_CMD_LINE */ -#endif /* ALLOC_CMD_LINE */ - #ifdef ALLOC_COMMON_DATA /*! Service types for tv services. */ const char *TvServiceType[] = { diff --git a/upnp/sample/common/sample_util.c b/upnp/sample/common/sample_util.c index 0e74872..ccf06ae 100644 --- a/upnp/sample/common/sample_util.c +++ b/upnp/sample/common/sample_util.c @@ -32,6 +32,10 @@ #include "sample_util.h" +#define ALLOC_COMMON_DATA +#include "common_data.h" +#include "tv_ctrlpt.h" +#include "tv_device.h" #include #include @@ -620,3 +624,15 @@ void SampleUtil_StateUpdate(const char *varName, const char *varValue, } } +/*! + * \brief Prints a string to standard out. + */ +void linux_print(const char *format, ...) +{ + va_list argList; + va_start(argList, format); + vfprintf(stdout, format, argList); + fflush(stdout); + va_end(argList); +} + diff --git a/upnp/sample/common/sample_util.h b/upnp/sample/common/sample_util.h index e722555..8caa83f 100644 --- a/upnp/sample/common/sample_util.h +++ b/upnp/sample/common/sample_util.h @@ -230,6 +230,16 @@ void SampleUtil_StateUpdate( /*! [in] . */ eventType type); +/*! + * \brief Prints a string to standard out. + */ +void linux_print(const char *format, ...) +#if (__GNUC__ >= 3) + /* This enables printf like format checking by the compiler */ + __attribute__((format (__printf__, 1, 2))) +#endif +; + #ifdef __cplusplus }; #endif /* __cplusplus */ diff --git a/upnp/sample/common/tv_ctrlpt.c b/upnp/sample/common/tv_ctrlpt.c index 7de319e..2b88d8b 100644 --- a/upnp/sample/common/tv_ctrlpt.c +++ b/upnp/sample/common/tv_ctrlpt.c @@ -35,10 +35,9 @@ #include "upnp.h" /*! - Mutex for protecting the global device list - in a multi-threaded, asynchronous environment. - All functions should lock this mutex before reading - or writing the device list. + * Mutex for protecting the global device list in a multi-threaded, + * asynchronous environment. All functions should lock this mutex before + * reading or writing the device list. */ ithread_mutex_t DeviceListMutex; @@ -86,48 +85,48 @@ struct TvDeviceNode *GlobalDeviceList = NULL; int TvCtrlPointDeleteNode( struct TvDeviceNode *node ) { - int rc, - service, - var; + int rc, service, var; - if( NULL == node ) { - SampleUtil_Print("ERROR: TvCtrlPointDeleteNode: Node is empty\n"); - return TV_ERROR; - } + if (NULL == node) { + SampleUtil_Print + ("ERROR: TvCtrlPointDeleteNode: Node is empty\n"); + return TV_ERROR; + } - for( service = 0; service < TV_SERVICE_SERVCOUNT; service++ ) { - /* - If we have a valid control SID, then unsubscribe - */ - if( strcmp( node->device.TvService[service].SID, "" ) != 0 ) { - rc = UpnpUnSubscribe( ctrlpt_handle, - node->device.TvService[service].SID ); - if( UPNP_E_SUCCESS == rc ) { - SampleUtil_Print - ( "Unsubscribed from Tv %s EventURL with SID=%s\n", - TvServiceName[service], - node->device.TvService[service].SID ); - } else { - SampleUtil_Print - ( "Error unsubscribing to Tv %s EventURL -- %d\n", - TvServiceName[service], rc ); - } - } + for (service = 0; service < TV_SERVICE_SERVCOUNT; service++) { + /* + If we have a valid control SID, then unsubscribe + */ + if (strcmp(node->device.TvService[service].SID, "") != 0) { + rc = UpnpUnSubscribe(ctrlpt_handle, + node->device.TvService[service]. + SID); + if (UPNP_E_SUCCESS == rc) { + SampleUtil_Print + ("Unsubscribed from Tv %s EventURL with SID=%s\n", + TvServiceName[service], + node->device.TvService[service].SID); + } else { + SampleUtil_Print + ("Error unsubscribing to Tv %s EventURL -- %d\n", + TvServiceName[service], rc); + } + } - for( var = 0; var < TvVarCount[service]; var++ ) { - if( node->device.TvService[service].VariableStrVal[var] ) { - free( node->device.TvService[service]. - VariableStrVal[var] ); - } - } - } + for (var = 0; var < TvVarCount[service]; var++) { + if (node->device.TvService[service].VariableStrVal[var]) { + free(node->device. + TvService[service].VariableStrVal[var]); + } + } + } - /*Notify New Device Added */ - SampleUtil_StateUpdate( NULL, NULL, node->device.UDN, DEVICE_REMOVED ); - free( node ); - node = NULL; + /*Notify New Device Added */ + SampleUtil_StateUpdate(NULL, NULL, node->device.UDN, DEVICE_REMOVED); + free(node); + node = NULL; - return TV_SUCCESS; + return TV_SUCCESS; } /******************************************************************************** @@ -142,38 +141,37 @@ TvCtrlPointDeleteNode( struct TvDeviceNode *node ) ********************************************************************************/ int TvCtrlPointRemoveDevice(const char *UDN) { - struct TvDeviceNode *curdevnode; - struct TvDeviceNode *prevdevnode; + struct TvDeviceNode *curdevnode; + struct TvDeviceNode *prevdevnode; - ithread_mutex_lock( &DeviceListMutex ); + ithread_mutex_lock(&DeviceListMutex); - curdevnode = GlobalDeviceList; - if( !curdevnode ) { - SampleUtil_Print("WARNING: TvCtrlPointRemoveDevice: Device list empty\n"); - } else { - if( 0 == strcmp( curdevnode->device.UDN, UDN ) ) { - GlobalDeviceList = curdevnode->next; - TvCtrlPointDeleteNode( curdevnode ); - } else { - prevdevnode = curdevnode; - curdevnode = curdevnode->next; + curdevnode = GlobalDeviceList; + if (!curdevnode) { + SampleUtil_Print( + "WARNING: TvCtrlPointRemoveDevice: Device list empty\n"); + } else { + if (0 == strcmp(curdevnode->device.UDN, UDN)) { + GlobalDeviceList = curdevnode->next; + TvCtrlPointDeleteNode(curdevnode); + } else { + prevdevnode = curdevnode; + curdevnode = curdevnode->next; + while (curdevnode) { + if (strcmp(curdevnode->device.UDN, UDN) == 0) { + prevdevnode->next = curdevnode->next; + TvCtrlPointDeleteNode(curdevnode); + break; + } + prevdevnode = curdevnode; + curdevnode = curdevnode->next; + } + } + } - while( curdevnode ) { - if( strcmp( curdevnode->device.UDN, UDN ) == 0 ) { - prevdevnode->next = curdevnode->next; - TvCtrlPointDeleteNode( curdevnode ); - break; - } + ithread_mutex_unlock(&DeviceListMutex); - prevdevnode = curdevnode; - curdevnode = curdevnode->next; - } - } - } - - ithread_mutex_unlock( &DeviceListMutex ); - - return TV_SUCCESS; + return TV_SUCCESS; } /******************************************************************************** @@ -186,26 +184,24 @@ int TvCtrlPointRemoveDevice(const char *UDN) * None * ********************************************************************************/ -int -TvCtrlPointRemoveAll( void ) +int TvCtrlPointRemoveAll(void) { - struct TvDeviceNode *curdevnode, - *next; + struct TvDeviceNode *curdevnode, *next; - ithread_mutex_lock( &DeviceListMutex ); + ithread_mutex_lock(&DeviceListMutex); - curdevnode = GlobalDeviceList; - GlobalDeviceList = NULL; + curdevnode = GlobalDeviceList; + GlobalDeviceList = NULL; - while( curdevnode ) { - next = curdevnode->next; - TvCtrlPointDeleteNode( curdevnode ); - curdevnode = next; - } + while (curdevnode) { + next = curdevnode->next; + TvCtrlPointDeleteNode(curdevnode); + curdevnode = next; + } - ithread_mutex_unlock( &DeviceListMutex ); + ithread_mutex_unlock(&DeviceListMutex); - return TV_SUCCESS; + return TV_SUCCESS; } /******************************************************************************** @@ -219,24 +215,21 @@ TvCtrlPointRemoveAll( void ) * None * ********************************************************************************/ -int -TvCtrlPointRefresh( void ) +int TvCtrlPointRefresh(void) { - int rc; + int rc; - TvCtrlPointRemoveAll(); + TvCtrlPointRemoveAll(); + /* Search for all devices of type tvdevice version 1, + * waiting for up to 5 seconds for the response */ + rc = UpnpSearchAsync(ctrlpt_handle, 5, TvDeviceType, NULL); + if (UPNP_E_SUCCESS != rc) { + SampleUtil_Print("Error sending search request%d\n", rc); - /* - Search for all devices of type tvdevice version 1, - waiting for up to 5 seconds for the response - */ - rc = UpnpSearchAsync( ctrlpt_handle, 5, TvDeviceType, NULL ); - if( UPNP_E_SUCCESS != rc ) { - SampleUtil_Print("Error sending search request%d\n", rc); - return TV_ERROR; - } + return TV_ERROR; + } - return TV_SUCCESS; + return TV_SUCCESS; } /******************************************************************************** @@ -254,71 +247,66 @@ TvCtrlPointRefresh( void ) ********************************************************************************/ int TvCtrlPointGetVar(int service, int devnum, const char *varname) { - struct TvDeviceNode *devnode; - int rc; + struct TvDeviceNode *devnode; + int rc; - ithread_mutex_lock( &DeviceListMutex ); + ithread_mutex_lock(&DeviceListMutex); - rc = TvCtrlPointGetDevice( devnum, &devnode ); + rc = TvCtrlPointGetDevice(devnum, &devnode); - if( TV_SUCCESS == rc ) { - rc = UpnpGetServiceVarStatusAsync( ctrlpt_handle, - devnode->device. - TvService[service].ControlURL, - varname, - TvCtrlPointCallbackEventHandler, - NULL ); - if( rc != UPNP_E_SUCCESS ) { - SampleUtil_Print("Error in UpnpGetServiceVarStatusAsync -- %d\n", rc); - rc = TV_ERROR; - } - } + if (TV_SUCCESS == rc) { + rc = UpnpGetServiceVarStatusAsync( + ctrlpt_handle, + devnode->device.TvService[service].ControlURL, + varname, + TvCtrlPointCallbackEventHandler, + NULL); + if (rc != UPNP_E_SUCCESS) { + SampleUtil_Print( + "Error in UpnpGetServiceVarStatusAsync -- %d\n", + rc); + rc = TV_ERROR; + } + } - ithread_mutex_unlock( &DeviceListMutex ); + ithread_mutex_unlock(&DeviceListMutex); - return rc; + return rc; } -int -TvCtrlPointGetPower( int devnum ) +int TvCtrlPointGetPower(int devnum) { - return TvCtrlPointGetVar( TV_SERVICE_CONTROL, devnum, "Power" ); + return TvCtrlPointGetVar(TV_SERVICE_CONTROL, devnum, "Power"); } -int -TvCtrlPointGetChannel( int devnum ) +int TvCtrlPointGetChannel(int devnum) { - return TvCtrlPointGetVar( TV_SERVICE_CONTROL, devnum, "Channel" ); + return TvCtrlPointGetVar(TV_SERVICE_CONTROL, devnum, "Channel"); } -int -TvCtrlPointGetVolume( int devnum ) +int TvCtrlPointGetVolume(int devnum) { - return TvCtrlPointGetVar( TV_SERVICE_CONTROL, devnum, "Volume" ); + return TvCtrlPointGetVar(TV_SERVICE_CONTROL, devnum, "Volume"); } -int -TvCtrlPointGetColor( int devnum ) +int TvCtrlPointGetColor(int devnum) { - return TvCtrlPointGetVar( TV_SERVICE_PICTURE, devnum, "Color" ); + return TvCtrlPointGetVar(TV_SERVICE_PICTURE, devnum, "Color"); } -int -TvCtrlPointGetTint( int devnum ) +int TvCtrlPointGetTint(int devnum) { - return TvCtrlPointGetVar( TV_SERVICE_PICTURE, devnum, "Tint" ); + return TvCtrlPointGetVar(TV_SERVICE_PICTURE, devnum, "Tint"); } -int -TvCtrlPointGetContrast( int devnum ) +int TvCtrlPointGetContrast(int devnum) { - return TvCtrlPointGetVar( TV_SERVICE_PICTURE, devnum, "Contrast" ); + return TvCtrlPointGetVar(TV_SERVICE_PICTURE, devnum, "Contrast"); } -int -TvCtrlPointGetBrightness( int devnum ) +int TvCtrlPointGetBrightness(int devnum) { - return TvCtrlPointGetVar( TV_SERVICE_PICTURE, devnum, "Brightness" ); + return TvCtrlPointGetVar(TV_SERVICE_PICTURE, devnum, "Brightness"); } /******************************************************************************** @@ -345,49 +333,52 @@ int TvCtrlPointSendAction( char **param_val, int param_count) { - struct TvDeviceNode *devnode; - IXML_Document *actionNode = NULL; - int rc = TV_SUCCESS; - int param; + struct TvDeviceNode *devnode; + IXML_Document *actionNode = NULL; + int rc = TV_SUCCESS; + int param; - ithread_mutex_lock( &DeviceListMutex ); + ithread_mutex_lock(&DeviceListMutex); - rc = TvCtrlPointGetDevice( devnum, &devnode ); - if( TV_SUCCESS == rc ) { - if( 0 == param_count ) { - actionNode = - UpnpMakeAction( actionname, TvServiceType[service], 0, - NULL ); - } else { - for( param = 0; param < param_count; param++ ) { - if( UpnpAddToAction - ( &actionNode, actionname, TvServiceType[service], - param_name[param], - param_val[param] ) != UPNP_E_SUCCESS ) { - SampleUtil_Print("ERROR: TvCtrlPointSendAction: Trying to add action param\n"); - /*return -1; // TBD - BAD! leaves mutex locked */ - } - } - } + rc = TvCtrlPointGetDevice(devnum, &devnode); + if (TV_SUCCESS == rc) { + if (0 == param_count) { + actionNode = + UpnpMakeAction(actionname, TvServiceType[service], + 0, NULL); + } else { + for (param = 0; param < param_count; param++) { + if (UpnpAddToAction + (&actionNode, actionname, + TvServiceType[service], param_name[param], + param_val[param]) != UPNP_E_SUCCESS) { + SampleUtil_Print + ("ERROR: TvCtrlPointSendAction: Trying to add action param\n"); + /*return -1; // TBD - BAD! leaves mutex locked */ + } + } + } - rc = UpnpSendActionAsync( ctrlpt_handle, - devnode->device.TvService[service]. - ControlURL, TvServiceType[service], - NULL, actionNode, - TvCtrlPointCallbackEventHandler, NULL ); + rc = UpnpSendActionAsync(ctrlpt_handle, + devnode->device. + TvService[service].ControlURL, + TvServiceType[service], NULL, + actionNode, + TvCtrlPointCallbackEventHandler, NULL); - if( rc != UPNP_E_SUCCESS ) { - SampleUtil_Print( "Error in UpnpSendActionAsync -- %d\n", rc ); - rc = TV_ERROR; - } - } + if (rc != UPNP_E_SUCCESS) { + SampleUtil_Print("Error in UpnpSendActionAsync -- %d\n", + rc); + rc = TV_ERROR; + } + } - ithread_mutex_unlock( &DeviceListMutex ); + ithread_mutex_unlock(&DeviceListMutex); - if( actionNode ) - ixmlDocument_free( actionNode ); + if (actionNode) + ixmlDocument_free(actionNode); - return rc; + return rc; } /******************************************************************************** @@ -403,86 +394,66 @@ int TvCtrlPointSendAction( * paramValue -- Actual value of the parameter being passed * ********************************************************************************/ -int -TvCtrlPointSendActionNumericArg( int devnum, - int service, - const char *actionName, - const char *paramName, - int paramValue ) +int TvCtrlPointSendActionNumericArg(int devnum, int service, + const char *actionName, const char *paramName, int paramValue) { - char param_val_a[50]; - char *param_val = param_val_a; + char param_val_a[50]; + char *param_val = param_val_a; - sprintf( param_val_a, "%d", paramValue ); - - return TvCtrlPointSendAction( service, devnum, actionName, ¶mName, - ¶m_val, 1 ); + sprintf(param_val_a, "%d", paramValue); + return TvCtrlPointSendAction( + service, devnum, actionName, ¶mName, + ¶m_val, 1); } -int -TvCtrlPointSendPowerOn( int devnum ) +int TvCtrlPointSendPowerOn(int devnum) { - return TvCtrlPointSendAction( TV_SERVICE_CONTROL, devnum, "PowerOn", - NULL, NULL, 0 ); + return TvCtrlPointSendAction( + TV_SERVICE_CONTROL, devnum, "PowerOn", NULL, NULL, 0); } -int -TvCtrlPointSendPowerOff( int devnum ) +int TvCtrlPointSendPowerOff(int devnum) { - return TvCtrlPointSendAction( TV_SERVICE_CONTROL, devnum, "PowerOff", - NULL, NULL, 0 ); + return TvCtrlPointSendAction( + TV_SERVICE_CONTROL, devnum, "PowerOff", NULL, NULL, 0); } -int -TvCtrlPointSendSetChannel( int devnum, - int channel ) +int TvCtrlPointSendSetChannel(int devnum, int channel) { - return TvCtrlPointSendActionNumericArg( devnum, TV_SERVICE_CONTROL, - "SetChannel", "Channel", - channel ); + return TvCtrlPointSendActionNumericArg( + devnum, TV_SERVICE_CONTROL, "SetChannel", "Channel", channel); } -int -TvCtrlPointSendSetVolume( int devnum, - int volume ) +int TvCtrlPointSendSetVolume(int devnum, int volume) { - return TvCtrlPointSendActionNumericArg( devnum, TV_SERVICE_CONTROL, - "SetVolume", "Volume", - volume ); + return TvCtrlPointSendActionNumericArg( + devnum, TV_SERVICE_CONTROL, "SetVolume", "Volume", volume); } -int -TvCtrlPointSendSetColor( int devnum, - int color ) +int TvCtrlPointSendSetColor(int devnum, int color) { - return TvCtrlPointSendActionNumericArg( devnum, TV_SERVICE_PICTURE, - "SetColor", "Color", color ); + return TvCtrlPointSendActionNumericArg( + devnum, TV_SERVICE_PICTURE, "SetColor", "Color", color); } -int -TvCtrlPointSendSetTint( int devnum, - int tint ) +int TvCtrlPointSendSetTint(int devnum, int tint) { - return TvCtrlPointSendActionNumericArg( devnum, TV_SERVICE_PICTURE, - "SetTint", "Tint", tint ); + return TvCtrlPointSendActionNumericArg( + devnum, TV_SERVICE_PICTURE, "SetTint", "Tint", tint); } -int -TvCtrlPointSendSetContrast( int devnum, - int contrast ) +int TvCtrlPointSendSetContrast(int devnum, int contrast) { - return TvCtrlPointSendActionNumericArg( devnum, TV_SERVICE_PICTURE, - "SetContrast", "Contrast", - contrast ); + return TvCtrlPointSendActionNumericArg( + devnum, TV_SERVICE_PICTURE, "SetContrast", "Contrast", + contrast); } -int -TvCtrlPointSendSetBrightness( int devnum, - int brightness ) +int TvCtrlPointSendSetBrightness(int devnum, int brightness) { - return TvCtrlPointSendActionNumericArg( devnum, TV_SERVICE_PICTURE, - "SetBrightness", "Brightness", - brightness ); + return TvCtrlPointSendActionNumericArg( + devnum, TV_SERVICE_PICTURE, "SetBrightness", "Brightness", + brightness); } /******************************************************************************** @@ -500,27 +471,24 @@ TvCtrlPointSendSetBrightness( int devnum, * devnode -- The output device node pointer * ********************************************************************************/ -int -TvCtrlPointGetDevice( int devnum, - struct TvDeviceNode **devnode ) +int TvCtrlPointGetDevice(int devnum, struct TvDeviceNode **devnode) { - int count = devnum; - struct TvDeviceNode *tmpdevnode = NULL; + int count = devnum; + struct TvDeviceNode *tmpdevnode = NULL; - if( count ) - tmpdevnode = GlobalDeviceList; + if (count) + tmpdevnode = GlobalDeviceList; + while (--count && tmpdevnode) { + tmpdevnode = tmpdevnode->next; + } + if (!tmpdevnode) { + SampleUtil_Print("Error finding TvDevice number -- %d\n", + devnum); + return TV_ERROR; + } + *devnode = tmpdevnode; - while( --count && tmpdevnode ) { - tmpdevnode = tmpdevnode->next; - } - - if( !tmpdevnode ) { - SampleUtil_Print( "Error finding TvDevice number -- %d\n", devnum ); - return TV_ERROR; - } - - *devnode = tmpdevnode; - return TV_SUCCESS; + return TV_SUCCESS; } /******************************************************************************** @@ -533,24 +501,23 @@ TvCtrlPointGetDevice( int devnum, * None * ********************************************************************************/ -int -TvCtrlPointPrintList() +int TvCtrlPointPrintList() { - struct TvDeviceNode *tmpdevnode; - int i = 0; + struct TvDeviceNode *tmpdevnode; + int i = 0; - ithread_mutex_lock( &DeviceListMutex ); + ithread_mutex_lock(&DeviceListMutex); - SampleUtil_Print("TvCtrlPointPrintList:\n"); - tmpdevnode = GlobalDeviceList; - while( tmpdevnode ) { - SampleUtil_Print( " %3d -- %s\n", ++i, tmpdevnode->device.UDN ); - tmpdevnode = tmpdevnode->next; - } - SampleUtil_Print("\n"); - ithread_mutex_unlock( &DeviceListMutex ); + SampleUtil_Print("TvCtrlPointPrintList:\n"); + tmpdevnode = GlobalDeviceList; + while (tmpdevnode) { + SampleUtil_Print(" %3d -- %s\n", ++i, tmpdevnode->device.UDN); + tmpdevnode = tmpdevnode->next; + } + SampleUtil_Print("\n"); + ithread_mutex_unlock(&DeviceListMutex); - return TV_SUCCESS; + return TV_SUCCESS; } /******************************************************************************** @@ -565,64 +532,89 @@ TvCtrlPointPrintList() * starting with 1) * ********************************************************************************/ -int -TvCtrlPointPrintDevice( int devnum ) +int TvCtrlPointPrintDevice(int devnum) { - struct TvDeviceNode *tmpdevnode; - int i = 0, - service, - var; - char spacer[15]; + struct TvDeviceNode *tmpdevnode; + int i = 0, service, var; + char spacer[15]; - if( devnum <= 0 ) { - SampleUtil_Print("Error in TvCtrlPointPrintDevice: invalid devnum = %d\n", devnum); - return TV_ERROR; - } + if (devnum <= 0) { + SampleUtil_Print( + "Error in TvCtrlPointPrintDevice: " + "invalid devnum = %d\n", + devnum); + return TV_ERROR; + } - ithread_mutex_lock( &DeviceListMutex ); + ithread_mutex_lock(&DeviceListMutex); - SampleUtil_Print("TvCtrlPointPrintDevice:\n"); - tmpdevnode = GlobalDeviceList; - while( tmpdevnode ) { - i++; - if( i == devnum ) - break; - tmpdevnode = tmpdevnode->next; - } + SampleUtil_Print("TvCtrlPointPrintDevice:\n"); + tmpdevnode = GlobalDeviceList; + while (tmpdevnode) { + i++; + if (i == devnum) + break; + tmpdevnode = tmpdevnode->next; + } + if (!tmpdevnode) { + SampleUtil_Print( + "Error in TvCtrlPointPrintDevice: " + "invalid devnum = %d -- actual device count = %d\n", + devnum, i); + } else { + SampleUtil_Print( + " TvDevice -- %d\n" + " | \n" + " +- UDN = %s\n" + " +- DescDocURL = %s\n" + " +- FriendlyName = %s\n" + " +- PresURL = %s\n" + " +- Adver. TimeOut = %d\n", + devnum, + tmpdevnode->device.UDN, + tmpdevnode->device.DescDocURL, + tmpdevnode->device.FriendlyName, + tmpdevnode->device.PresURL, + tmpdevnode->device.AdvrTimeOut); + for (service = 0; service < TV_SERVICE_SERVCOUNT; service++) { + if (service < TV_SERVICE_SERVCOUNT - 1) + sprintf(spacer, " | "); + else + sprintf(spacer, " "); + SampleUtil_Print( + " | \n" + " +- Tv %s Service\n" + "%s+- ServiceId = %s\n" + "%s+- ServiceType = %s\n" + "%s+- EventURL = %s\n" + "%s+- ControlURL = %s\n" + "%s+- SID = %s\n" + "%s+- ServiceStateTable\n", + TvServiceName[service], + spacer, + tmpdevnode->device.TvService[service].ServiceId, + spacer, + tmpdevnode->device.TvService[service].ServiceType, + spacer, + tmpdevnode->device.TvService[service].EventURL, + spacer, + tmpdevnode->device.TvService[service].ControlURL, + spacer, + tmpdevnode->device.TvService[service].SID, + spacer); + for (var = 0; var < TvVarCount[service]; var++) { + SampleUtil_Print( + "%s +- %-10s = %s\n", + spacer, + TvVarName[service][var], + tmpdevnode->device.TvService[service].VariableStrVal[var]); + } + } + } + SampleUtil_Print("\n"); + ithread_mutex_unlock(&DeviceListMutex); - if( !tmpdevnode ) { - SampleUtil_Print("Error in TvCtrlPointPrintDevice: invalid devnum = %d -- actual device count = %d\n", devnum, i); - } else { - SampleUtil_Print(" TvDevice -- %d\n", devnum); - SampleUtil_Print(" | \n"); - SampleUtil_Print(" +- UDN = %s\n" , tmpdevnode->device.UDN); - SampleUtil_Print(" +- DescDocURL = %s\n", tmpdevnode->device.DescDocURL); - SampleUtil_Print(" +- FriendlyName = %s\n", tmpdevnode->device.FriendlyName); - SampleUtil_Print(" +- PresURL = %s\n", tmpdevnode->device.PresURL); - SampleUtil_Print(" +- Adver. TimeOut = %d\n", tmpdevnode->device.AdvrTimeOut); - for( service = 0; service < TV_SERVICE_SERVCOUNT; service++ ) { - if(service < TV_SERVICE_SERVCOUNT - 1) - sprintf(spacer, " | "); - else - sprintf(spacer, " "); - SampleUtil_Print(" | "); - SampleUtil_Print(" +- Tv %s Service", TvServiceName[service]); - SampleUtil_Print("%s+- ServiceId = %s\n", spacer, tmpdevnode->device.TvService[service].ServiceId); - SampleUtil_Print("%s+- ServiceType = %s\n", spacer, tmpdevnode->device.TvService[service].ServiceType); - SampleUtil_Print("%s+- EventURL = %s\n", spacer, tmpdevnode->device.TvService[service].EventURL); - SampleUtil_Print("%s+- ControlURL = %s\n", spacer, tmpdevnode->device.TvService[service].ControlURL); - SampleUtil_Print("%s+- SID = %s\n", spacer, tmpdevnode->device.TvService[service].SID); - SampleUtil_Print("%s+- ServiceStateTable", spacer); - for( var = 0; var < TvVarCount[service]; var++ ) { - SampleUtil_Print("%s +- %-10s = %s\n", spacer, TvVarName[service][var], - tmpdevnode->device.TvService[service].VariableStrVal[var]); - } - } - } - SampleUtil_Print("\n"); - ithread_mutex_unlock( &DeviceListMutex ); - - return TV_SUCCESS; + return TV_SUCCESS; } /******************************************************************************** @@ -638,222 +630,225 @@ TvCtrlPointPrintDevice( int devnum ) * expires -- The expiration time for this advertisement * ********************************************************************************/ -void -TvCtrlPointAddDevice( IXML_Document *DescDoc, - const char *location, - int expires ) +void TvCtrlPointAddDevice( + IXML_Document *DescDoc, + const char *location, + int expires) { - char *deviceType = NULL; - char *friendlyName = NULL; - char presURL[200]; - char *baseURL = NULL; - char *relURL = NULL; - char *UDN = NULL; - char *serviceId[TV_SERVICE_SERVCOUNT] = { NULL, NULL }; - char *eventURL[TV_SERVICE_SERVCOUNT] = { NULL, NULL }; - char *controlURL[TV_SERVICE_SERVCOUNT] = { NULL, NULL }; - Upnp_SID eventSID[TV_SERVICE_SERVCOUNT]; - int TimeOut[TV_SERVICE_SERVCOUNT] = { - default_timeout, - default_timeout }; - struct TvDeviceNode *deviceNode; - struct TvDeviceNode *tmpdevnode; - int ret = 1; - int found = 0; - int service; - int var; + char *deviceType = NULL; + char *friendlyName = NULL; + char presURL[200]; + char *baseURL = NULL; + char *relURL = NULL; + char *UDN = NULL; + char *serviceId[TV_SERVICE_SERVCOUNT] = { NULL, NULL }; + char *eventURL[TV_SERVICE_SERVCOUNT] = { NULL, NULL }; + char *controlURL[TV_SERVICE_SERVCOUNT] = { NULL, NULL }; + Upnp_SID eventSID[TV_SERVICE_SERVCOUNT]; + int TimeOut[TV_SERVICE_SERVCOUNT] = { + default_timeout, + default_timeout + }; + struct TvDeviceNode *deviceNode; + struct TvDeviceNode *tmpdevnode; + int ret = 1; + int found = 0; + int service; + int var; - ithread_mutex_lock( &DeviceListMutex ); + ithread_mutex_lock(&DeviceListMutex); - /* Read key elements from description document */ - UDN = SampleUtil_GetFirstDocumentItem( DescDoc, "UDN" ); - deviceType = SampleUtil_GetFirstDocumentItem( DescDoc, "deviceType" ); - friendlyName = - SampleUtil_GetFirstDocumentItem( DescDoc, "friendlyName" ); - baseURL = SampleUtil_GetFirstDocumentItem( DescDoc, "URLBase" ); - relURL = SampleUtil_GetFirstDocumentItem( DescDoc, "presentationURL" ); + /* Read key elements from description document */ + UDN = SampleUtil_GetFirstDocumentItem(DescDoc, "UDN"); + deviceType = SampleUtil_GetFirstDocumentItem(DescDoc, "deviceType"); + friendlyName = SampleUtil_GetFirstDocumentItem(DescDoc, "friendlyName"); + baseURL = SampleUtil_GetFirstDocumentItem(DescDoc, "URLBase"); + relURL = SampleUtil_GetFirstDocumentItem(DescDoc, "presentationURL"); - ret = UpnpResolveURL( - ( baseURL ? baseURL : location ), relURL, presURL); + ret = UpnpResolveURL((baseURL ? baseURL : location), relURL, presURL); - if( UPNP_E_SUCCESS != ret ) - SampleUtil_Print( "Error generating presURL from %s + %s\n", baseURL, relURL ); + if (UPNP_E_SUCCESS != ret) + SampleUtil_Print("Error generating presURL from %s + %s\n", + baseURL, relURL); - if( strcmp( deviceType, TvDeviceType ) == 0 ) { - SampleUtil_Print("Found Tv device\n"); + if (strcmp(deviceType, TvDeviceType) == 0) { + SampleUtil_Print("Found Tv device\n"); - /* Check if this device is already in the list */ - tmpdevnode = GlobalDeviceList; - while( tmpdevnode ) { - if( strcmp( tmpdevnode->device.UDN, UDN ) == 0 ) { - found = 1; - break; - } - tmpdevnode = tmpdevnode->next; - } + /* Check if this device is already in the list */ + tmpdevnode = GlobalDeviceList; + while (tmpdevnode) { + if (strcmp(tmpdevnode->device.UDN, UDN) == 0) { + found = 1; + break; + } + tmpdevnode = tmpdevnode->next; + } - if( found ) { - /* The device is already there, so just update */ - /* the advertisement timeout field */ - tmpdevnode->device.AdvrTimeOut = expires; - } else { - for( service = 0; service < TV_SERVICE_SERVCOUNT; service++ ) { - if( SampleUtil_FindAndParseService(DescDoc, location, TvServiceType[service], - &serviceId[service], &eventURL[service], - &controlURL[service])) { - SampleUtil_Print("Subscribing to EventURL %s...\n", eventURL[service]); - ret = UpnpSubscribe(ctrlpt_handle, eventURL[service], - &TimeOut[service], eventSID[service]); - if( ret == UPNP_E_SUCCESS ) { - SampleUtil_Print("Subscribed to EventURL with SID=%s\n", eventSID[service]); - } else { - SampleUtil_Print("Error Subscribing to EventURL -- %d\n", ret); - strcpy(eventSID[service], ""); - } - } else { - SampleUtil_Print("Error: Could not find Service: %s\n", TvServiceType[service]); - } - } - /* Create a new device node */ - deviceNode = (struct TvDeviceNode *)malloc(sizeof(struct TvDeviceNode)); - strcpy( deviceNode->device.UDN, UDN ); - strcpy( deviceNode->device.DescDocURL, location ); - strcpy( deviceNode->device.FriendlyName, friendlyName ); - strcpy( deviceNode->device.PresURL, presURL ); - deviceNode->device.AdvrTimeOut = expires; - for( service = 0; service < TV_SERVICE_SERVCOUNT; service++ ) { - strcpy( deviceNode->device.TvService[service].ServiceId, - serviceId[service] ); - strcpy( deviceNode->device.TvService[service].ServiceType, - TvServiceType[service] ); - strcpy( deviceNode->device.TvService[service].ControlURL, - controlURL[service] ); - strcpy( deviceNode->device.TvService[service].EventURL, - eventURL[service] ); - strcpy( deviceNode->device.TvService[service].SID, - eventSID[service] ); - for( var = 0; var < TvVarCount[service]; var++ ) { - deviceNode->device.TvService[service]. - VariableStrVal[var] = - ( char * )malloc( TV_MAX_VAL_LEN ); - strcpy( deviceNode->device.TvService[service]. - VariableStrVal[var], "" ); - } - } - deviceNode->next = NULL; - /* Insert the new device node in the list */ - if( ( tmpdevnode = GlobalDeviceList ) ) { - while( tmpdevnode ) { - if( tmpdevnode->next ) { - tmpdevnode = tmpdevnode->next; - } else { - tmpdevnode->next = deviceNode; - break; - } - } - } else { - GlobalDeviceList = deviceNode; - } - /*Notify New Device Added */ - SampleUtil_StateUpdate(NULL, NULL, deviceNode->device.UDN, DEVICE_ADDED); - } - } + if (found) { + /* The device is already there, so just update */ + /* the advertisement timeout field */ + tmpdevnode->device.AdvrTimeOut = expires; + } else { + for (service = 0; service < TV_SERVICE_SERVCOUNT; + service++) { + if (SampleUtil_FindAndParseService + (DescDoc, location, TvServiceType[service], + &serviceId[service], &eventURL[service], + &controlURL[service])) { + SampleUtil_Print + ("Subscribing to EventURL %s...\n", + eventURL[service]); + ret = + UpnpSubscribe(ctrlpt_handle, + eventURL[service], + &TimeOut[service], + eventSID[service]); + if (ret == UPNP_E_SUCCESS) { + SampleUtil_Print + ("Subscribed to EventURL with SID=%s\n", + eventSID[service]); + } else { + SampleUtil_Print + ("Error Subscribing to EventURL -- %d\n", + ret); + strcpy(eventSID[service], ""); + } + } else { + SampleUtil_Print + ("Error: Could not find Service: %s\n", + TvServiceType[service]); + } + } + /* Create a new device node */ + deviceNode = + (struct TvDeviceNode *) + malloc(sizeof(struct TvDeviceNode)); + strcpy(deviceNode->device.UDN, UDN); + strcpy(deviceNode->device.DescDocURL, location); + strcpy(deviceNode->device.FriendlyName, friendlyName); + strcpy(deviceNode->device.PresURL, presURL); + deviceNode->device.AdvrTimeOut = expires; + for (service = 0; service < TV_SERVICE_SERVCOUNT; + service++) { + strcpy(deviceNode->device.TvService[service]. + ServiceId, serviceId[service]); + strcpy(deviceNode->device.TvService[service]. + ServiceType, TvServiceType[service]); + strcpy(deviceNode->device.TvService[service]. + ControlURL, controlURL[service]); + strcpy(deviceNode->device.TvService[service]. + EventURL, eventURL[service]); + strcpy(deviceNode->device.TvService[service]. + SID, eventSID[service]); + for (var = 0; var < TvVarCount[service]; var++) { + deviceNode->device. + TvService[service].VariableStrVal + [var] = + (char *)malloc(TV_MAX_VAL_LEN); + strcpy(deviceNode->device. + TvService[service].VariableStrVal + [var], ""); + } + } + deviceNode->next = NULL; + /* Insert the new device node in the list */ + if ((tmpdevnode = GlobalDeviceList)) { + while (tmpdevnode) { + if (tmpdevnode->next) { + tmpdevnode = tmpdevnode->next; + } else { + tmpdevnode->next = deviceNode; + break; + } + } + } else { + GlobalDeviceList = deviceNode; + } + /*Notify New Device Added */ + SampleUtil_StateUpdate(NULL, NULL, + deviceNode->device.UDN, + DEVICE_ADDED); + } + } - ithread_mutex_unlock( &DeviceListMutex ); + ithread_mutex_unlock(&DeviceListMutex); - if( deviceType ) - free( deviceType ); - if( friendlyName ) - free( friendlyName ); - if( UDN ) - free( UDN ); - if( baseURL ) - free( baseURL ); - if( relURL ) - free( relURL ); - for( service = 0; service < TV_SERVICE_SERVCOUNT; service++ ) { - if( serviceId[service] ) - free( serviceId[service] ); - if( controlURL[service] ) - free( controlURL[service] ); - if( eventURL[service] ) - free( eventURL[service] ); - } + if (deviceType) + free(deviceType); + if (friendlyName) + free(friendlyName); + if (UDN) + free(UDN); + if (baseURL) + free(baseURL); + if (relURL) + free(relURL); + for (service = 0; service < TV_SERVICE_SERVCOUNT; service++) { + if (serviceId[service]) + free(serviceId[service]); + if (controlURL[service]) + free(controlURL[service]); + if (eventURL[service]) + free(eventURL[service]); + } } -/******************************************************************************** - * TvStateUpdate - * - * Description: - * Update a Tv state table. Called when an event is - * received. Note: this function is NOT thread save. It must be - * called from another function that has locked the global device list. - * - * Parameters: - * UDN -- The UDN of the parent device. - * Service -- The service state table to update - * ChangedVariables -- DOM document representing the XML received - * with the event - * State -- pointer to the state table for the Tv service - * to update - * - ********************************************************************************/ -void -TvStateUpdate( char *UDN, - int Service, - IXML_Document * ChangedVariables, - char **State ) +void TvStateUpdate(char *UDN, int Service, IXML_Document *ChangedVariables, + char **State) { - IXML_NodeList *properties, - *variables; - IXML_Element *property, - *variable; - int length, - length1; - int i, - j; - char *tmpstate = NULL; + IXML_NodeList *properties; + IXML_NodeList *variables; + IXML_Element *property; + IXML_Element *variable; + int length; + int length1; + int i; + int j; + char *tmpstate = NULL; - SampleUtil_Print( "Tv State Update (service %d):\n", Service ); - /* Find all of the e:property tags in the document */ - properties = ixmlDocument_getElementsByTagName( - ChangedVariables, "e:property" ); - if( NULL != properties ) { - length = ixmlNodeList_length( properties ); - for( i = 0; i < length; i++ ) { /* Loop through each property change found */ - property = - ( IXML_Element * ) ixmlNodeList_item( properties, i ); - /* For each variable name in the state table, check if this - * is a corresponding property change */ - for( j = 0; j < TvVarCount[Service]; j++ ) { - variables = - ixmlElement_getElementsByTagName( property, - TvVarName[Service] - [j] ); - /* If a match is found, extract the value, and update the state table */ - if( variables ) { - length1 = ixmlNodeList_length( variables ); - if( length1 ) { - variable = - ( IXML_Element * ) - ixmlNodeList_item( variables, 0 ); - tmpstate = SampleUtil_GetElementValue( variable ); - - if( tmpstate ) { - strcpy( State[j], tmpstate ); - SampleUtil_Print(" Variable Name: %s New Value:'%s'\n", TvVarName[Service][j], State[j] ); - } - if( tmpstate ) - free( tmpstate ); - tmpstate = NULL; - } - ixmlNodeList_free( variables ); - variables = NULL; - } - } - } - ixmlNodeList_free( properties ); - } + SampleUtil_Print("Tv State Update (service %d):\n", Service); + /* Find all of the e:property tags in the document */ + properties = ixmlDocument_getElementsByTagName(ChangedVariables, + "e:property"); + if (properties) { + length = ixmlNodeList_length(properties); + for (i = 0; i < length; i++) { + /* Loop through each property change found */ + property = (IXML_Element *)ixmlNodeList_item( + properties, i); + /* For each variable name in the state table, + * check if this is a corresponding property change */ + for (j = 0; j < TvVarCount[Service]; j++) { + variables = ixmlElement_getElementsByTagName( + property, TvVarName[Service][j]); + /* If a match is found, extract + * the value, and update the state table */ + if (variables) { + length1 = ixmlNodeList_length(variables); + if (length1) { + variable = (IXML_Element *) + ixmlNodeList_item(variables, 0); + tmpstate = + SampleUtil_GetElementValue(variable); + if (tmpstate) { + strcpy(State[j], tmpstate); + SampleUtil_Print( + " Variable Name: %s New Value:'%s'\n", + TvVarName[Service][j], State[j]); + } + if (tmpstate) + free(tmpstate); + tmpstate = NULL; + } + ixmlNodeList_free(variables); + variables = NULL; + } + } + } + ixmlNodeList_free(properties); + } + return; + UDN = UDN; } /******************************************************************************** @@ -920,27 +915,33 @@ void TvCtrlPointHandleSubscribeUpdate( const Upnp_SID sid, int timeout) { - struct TvDeviceNode *tmpdevnode; - int service; + struct TvDeviceNode *tmpdevnode; + int service; - ithread_mutex_lock( &DeviceListMutex ); + ithread_mutex_lock(&DeviceListMutex); - tmpdevnode = GlobalDeviceList; - while( tmpdevnode ) { - for( service = 0; service < TV_SERVICE_SERVCOUNT; service++ ) { - if( strcmp - ( tmpdevnode->device.TvService[service].EventURL, - eventURL ) == 0 ) { - SampleUtil_Print("Received Tv %s Event Renewal for eventURL %s\n", TvServiceName[service], eventURL ); - strcpy( tmpdevnode->device.TvService[service].SID, sid ); - break; - } - } + tmpdevnode = GlobalDeviceList; + while (tmpdevnode) { + for (service = 0; service < TV_SERVICE_SERVCOUNT; service++) { + if (strcmp + (tmpdevnode->device.TvService[service].EventURL, + eventURL) == 0) { + SampleUtil_Print + ("Received Tv %s Event Renewal for eventURL %s\n", + TvServiceName[service], eventURL); + strcpy(tmpdevnode->device.TvService[service]. + SID, sid); + break; + } + } - tmpdevnode = tmpdevnode->next; - } + tmpdevnode = tmpdevnode->next; + } - ithread_mutex_unlock( &DeviceListMutex ); + ithread_mutex_unlock(&DeviceListMutex); + + return; + timeout = timeout; } void TvCtrlPointHandleGetVar( @@ -949,24 +950,27 @@ void TvCtrlPointHandleGetVar( const DOMString varValue) { - struct TvDeviceNode *tmpdevnode; - int service; + struct TvDeviceNode *tmpdevnode; + int service; - ithread_mutex_lock( &DeviceListMutex ); + ithread_mutex_lock(&DeviceListMutex); - tmpdevnode = GlobalDeviceList; - while (tmpdevnode) { - for (service = 0; service < TV_SERVICE_SERVCOUNT; service++) { - if (strcmp(tmpdevnode->device.TvService[service].ControlURL, controlURL) == 0) { - SampleUtil_StateUpdate( - varName, varValue, tmpdevnode->device.UDN, GET_VAR_COMPLETE); - break; - } - } - tmpdevnode = tmpdevnode->next; - } + tmpdevnode = GlobalDeviceList; + while (tmpdevnode) { + for (service = 0; service < TV_SERVICE_SERVCOUNT; service++) { + if (strcmp + (tmpdevnode->device.TvService[service].ControlURL, + controlURL) == 0) { + SampleUtil_StateUpdate(varName, varValue, + tmpdevnode->device.UDN, + GET_VAR_COMPLETE); + break; + } + } + tmpdevnode = tmpdevnode->next; + } - ithread_mutex_unlock( &DeviceListMutex ); + ithread_mutex_unlock(&DeviceListMutex); } /******************************************************************************** @@ -1113,6 +1117,7 @@ int TvCtrlPointCallbackEventHandler(Upnp_EventType EventType, void *Event, void } return 0; + Cookie = Cookie; } /******************************************************************************** @@ -1129,97 +1134,77 @@ int TvCtrlPointCallbackEventHandler(Upnp_EventType EventType, void *Event, void * function is called. * ********************************************************************************/ -void -TvCtrlPointVerifyTimeouts( int incr ) +void TvCtrlPointVerifyTimeouts(int incr) { - struct TvDeviceNode *prevdevnode, - *curdevnode; - int ret; + struct TvDeviceNode *prevdevnode, *curdevnode; + int ret; - ithread_mutex_lock( &DeviceListMutex ); + ithread_mutex_lock(&DeviceListMutex); - prevdevnode = NULL; - curdevnode = GlobalDeviceList; + prevdevnode = NULL; + curdevnode = GlobalDeviceList; + while (curdevnode) { + curdevnode->device.AdvrTimeOut -= incr; + /*SampleUtil_Print("Advertisement Timeout: %d\n", curdevnode->device.AdvrTimeOut); */ + if (curdevnode->device.AdvrTimeOut <= 0) { + /* This advertisement has expired, so we should remove the device + * from the list */ + if (GlobalDeviceList == curdevnode) + GlobalDeviceList = curdevnode->next; + else + prevdevnode->next = curdevnode->next; + TvCtrlPointDeleteNode(curdevnode); + if (prevdevnode) + curdevnode = prevdevnode->next; + else + curdevnode = GlobalDeviceList; + } else { + if (curdevnode->device.AdvrTimeOut < 2 * incr) { + /* This advertisement is about to expire, so + * send out a search request for this device + * UDN to try to renew */ + ret = UpnpSearchAsync(ctrlpt_handle, incr, + curdevnode->device.UDN, + NULL); + if (ret != UPNP_E_SUCCESS) + SampleUtil_Print + ("Error sending search request for Device UDN: %s -- err = %d\n", + curdevnode->device.UDN, ret); + } + prevdevnode = curdevnode; + curdevnode = curdevnode->next; + } + } - while( curdevnode ) { - curdevnode->device.AdvrTimeOut -= incr; - /*SampleUtil_Print("Advertisement Timeout: %d\n", curdevnode->device.AdvrTimeOut); */ - - if( curdevnode->device.AdvrTimeOut <= 0 ) { - /* - This advertisement has expired, so we should remove the device - from the list - */ - - if( GlobalDeviceList == curdevnode ) - GlobalDeviceList = curdevnode->next; - else - prevdevnode->next = curdevnode->next; - TvCtrlPointDeleteNode( curdevnode ); - if( prevdevnode ) - curdevnode = prevdevnode->next; - else - curdevnode = GlobalDeviceList; - } else { - - if( curdevnode->device.AdvrTimeOut < 2 * incr ) { - /* - This advertisement is about to expire, so send - out a search request for this device UDN to - try to renew - */ - ret = UpnpSearchAsync( ctrlpt_handle, incr, - curdevnode->device.UDN, NULL ); - if( ret != UPNP_E_SUCCESS ) - SampleUtil_Print("Error sending search request for Device UDN: %s -- err = %d\n", - curdevnode->device.UDN, ret ); - } - prevdevnode = curdevnode; - curdevnode = curdevnode->next; - } - } - ithread_mutex_unlock( &DeviceListMutex ); + ithread_mutex_unlock(&DeviceListMutex); } -/******************************************************************************** - * TvCtrlPointTimerLoop - * - * Description: - * Function that runs in its own thread and monitors advertisement - * and subscription timeouts for devices in the global device list. - * - * Parameters: - * None - * - ********************************************************************************/ +/*! + * \brief Function that runs in its own thread and monitors advertisement + * and subscription timeouts for devices in the global device list. + */ static int TvCtrlPointTimerLoopRun = 1; void *TvCtrlPointTimerLoop(void *args) { - int incr = 30; /* how often to verify the timeouts, in seconds */ + /* how often to verify the timeouts, in seconds */ + int incr = 30; - while (TvCtrlPointTimerLoopRun) { - isleep( incr ); - TvCtrlPointVerifyTimeouts( incr ); - } + while (TvCtrlPointTimerLoopRun) { + isleep(incr); + TvCtrlPointVerifyTimeouts(incr); + } - return NULL; + return NULL; + args = args; } -/******************************************************************************** - * TvCtrlPointStart +/*! + * \brief Call this function to initialize the UPnP library and start the TV + * Control Point. This function creates a timer thread and provides a + * callback handler to process any UPnP events that are received. * - * Description: - * Call this function to initialize the UPnP library and start the TV Control - * Point. This function creates a timer thread and provides a callback - * handler to process any UPnP events that are received. - * - * Parameters: - * None - * - * Returns: - * TV_SUCCESS if everything went well, else TV_ERROR - * - ********************************************************************************/ + * \return TV_SUCCESS if everything went well, else TV_ERROR. + */ int TvCtrlPointStart(print_string printFunctionPtr, state_update updateFunctionPtr, int combo) { ithread_t timer_thread; @@ -1232,11 +1217,9 @@ int TvCtrlPointStart(print_string printFunctionPtr, state_update updateFunctionP ithread_mutex_init(&DeviceListMutex, 0); - SampleUtil_Print( - "Initializing UPnP Sdk with\n" - "\tipaddress = %s port = %u\n", - ip_address ? ip_address : "{NULL}", - port); + SampleUtil_Print("Initializing UPnP Sdk with\n" + "\tipaddress = %s port = %u\n", + ip_address ? ip_address : "{NULL}", port); rc = UpnpInit(ip_address, port); if (rc != UPNP_E_SUCCESS) { @@ -1254,16 +1237,14 @@ int TvCtrlPointStart(print_string printFunctionPtr, state_update updateFunctionP port = UpnpGetServerPort(); } - SampleUtil_Print( - "UPnP Initialized\n" - "\tipaddress = %s port = %u\n", - ip_address ? ip_address : "{NULL}", - port); + SampleUtil_Print("UPnP Initialized\n" + "\tipaddress = %s port = %u\n", + ip_address ? ip_address : "{NULL}", port); SampleUtil_Print("Registering Control Point\n"); rc = UpnpRegisterClient(TvCtrlPointCallbackEventHandler, - &ctrlpt_handle, &ctrlpt_handle); + &ctrlpt_handle, &ctrlpt_handle); if (rc != UPNP_E_SUCCESS) { - SampleUtil_Print( "Error registering CP: %d\n", rc ); + SampleUtil_Print("Error registering CP: %d\n", rc); UpnpFinish(); return TV_ERROR; @@ -1291,3 +1272,313 @@ int TvCtrlPointStop(void) return TV_SUCCESS; } +void TvCtrlPointPrintShortHelp(void) +{ + SampleUtil_Print( + "Commands:\n" + " Help\n" + " HelpFull\n" + " ListDev\n" + " Refresh\n" + " PrintDev \n" + " PowerOn \n" + " PowerOff \n" + " SetChannel \n" + " SetVolume \n" + " SetColor \n" + " SetTint \n" + " SetContrast \n" + " SetBrightness \n" + " CtrlAction \n" + " PictAction \n" + " CtrlGetVar \n" + " PictGetVar \n" + " Exit\n"); +} + +void TvCtrlPointPrintLongHelp(void) +{ + SampleUtil_Print( + "\n" + "******************************\n" + "* TV Control Point Help Info *\n" + "******************************\n" + "\n" + "This sample control point application automatically searches\n" + "for and subscribes to the services of television device emulator\n" + "devices, described in the tvdevicedesc.xml description document.\n" + "It also registers itself as a tv device.\n" + "\n" + "Commands:\n" + " Help\n" + " Print this help info.\n" + " ListDev\n" + " Print the current list of TV Device Emulators that this\n" + " control point is aware of. Each device is preceded by a\n" + " device number which corresponds to the devnum argument of\n" + " commands listed below.\n" + " Refresh\n" + " Delete all of the devices from the device list and issue new\n" + " search request to rebuild the list from scratch.\n" + " PrintDev \n" + " Print the state table for the device .\n" + " e.g., 'PrintDev 1' prints the state table for the first\n" + " device in the device list.\n" + " PowerOn \n" + " Sends the PowerOn action to the Control Service of\n" + " device .\n" + " PowerOff \n" + " Sends the PowerOff action to the Control Service of\n" + " device .\n" + " SetChannel \n" + " Sends the SetChannel action to the Control Service of\n" + " device , requesting the channel to be changed\n" + " to .\n" + " SetVolume \n" + " Sends the SetVolume action to the Control Service of\n" + " device , requesting the volume to be changed\n" + " to .\n" + " SetColor \n" + " Sends the SetColor action to the Control Service of\n" + " device , requesting the color to be changed\n" + " to .\n" + " SetTint \n" + " Sends the SetTint action to the Control Service of\n" + " device , requesting the tint to be changed\n" + " to .\n" + " SetContrast \n" + " Sends the SetContrast action to the Control Service of\n" + " device , requesting the contrast to be changed\n" + " to .\n" + " SetBrightness \n" + " Sends the SetBrightness action to the Control Service of\n" + " device , requesting the brightness to be changed\n" + " to .\n" + " CtrlAction \n" + " Sends an action request specified by the string \n" + " to the Control Service of device . This command\n" + " only works for actions that have no arguments.\n" + " (e.g., \"CtrlAction 1 IncreaseChannel\")\n" + " PictAction \n" + " Sends an action request specified by the string \n" + " to the Picture Service of device . This command\n" + " only works for actions that have no arguments.\n" + " (e.g., \"PictAction 1 DecreaseContrast\")\n" + " CtrlGetVar \n" + " Requests the value of a variable specified by the string \n" + " from the Control Service of device .\n" + " (e.g., \"CtrlGetVar 1 Volume\")\n" + " PictGetVar \n" + " Requests the value of a variable specified by the string \n" + " from the Picture Service of device .\n" + " (e.g., \"PictGetVar 1 Tint\")\n" + " Exit\n" + " Exits the control point application.\n"); +} + +/*! Tags for valid commands issued at the command prompt. */ +enum cmdloop_tvcmds { + PRTHELP = 0, + PRTFULLHELP, + POWON, + POWOFF, + SETCHAN, + SETVOL, + SETCOL, + SETTINT, + SETCONT, + SETBRT, + CTRLACTION, + PICTACTION, + CTRLGETVAR, + PICTGETVAR, + PRTDEV, + LSTDEV, + REFRESH, + EXITCMD +}; + +/*! Data structure for parsing commands from the command line. */ +struct cmdloop_commands { + /* the string */ + const char *str; + /* the command */ + int cmdnum; + /* the number of arguments */ + int numargs; + /* the args */ + const char *args; +} cmdloop_commands; + +/*! Mappings between command text names, command tag, + * and required command arguments for command line + * commands */ +static struct cmdloop_commands cmdloop_cmdlist[] = { + {"Help", PRTHELP, 1, ""}, + {"HelpFull", PRTFULLHELP, 1, ""}, + {"ListDev", LSTDEV, 1, ""}, + {"Refresh", REFRESH, 1, ""}, + {"PrintDev", PRTDEV, 2, ""}, + {"PowerOn", POWON, 2, ""}, + {"PowerOff", POWOFF, 2, ""}, + {"SetChannel", SETCHAN, 3, " "}, + {"SetVolume", SETVOL, 3, " "}, + {"SetColor", SETCOL, 3, " "}, + {"SetTint", SETTINT, 3, " "}, + {"SetContrast", SETCONT, 3, " "}, + {"SetBrightness", SETBRT, 3, " "}, + {"CtrlAction", CTRLACTION, 2, " "}, + {"PictAction", PICTACTION, 2, " "}, + {"CtrlGetVar", CTRLGETVAR, 2, " "}, + {"PictGetVar", PICTGETVAR, 2, " "}, + {"Exit", EXITCMD, 1, ""} +}; + +void TvCtrlPointPrintCommands(void) +{ + int i; + int numofcmds = (sizeof cmdloop_cmdlist) / sizeof (cmdloop_commands); + + SampleUtil_Print("Valid Commands:\n"); + for (i = 0; i < numofcmds; ++i) { + SampleUtil_Print(" %-14s %s\n", + cmdloop_cmdlist[i].str, cmdloop_cmdlist[i].args); + } + SampleUtil_Print("\n"); +} + +void *TvCtrlPointCommandLoop(void *args) +{ + char cmdline[100]; + + while (1) { + SampleUtil_Print("\n>> "); + fgets(cmdline, 100, stdin); + TvCtrlPointProcessCommand(cmdline); + } + + return NULL; + args = args; +} + +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; + int 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'\n"); + return TV_SUCCESS; + } + if (invalidargs) { + SampleUtil_Print("Invalid arguments; try 'Help'\n"); + 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 (validargs == 3) + 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 (validargs == 3) + 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 (validargs == 3) + 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 (validargs == 3) + 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'\n"); + break; + } + if(invalidargs) + SampleUtil_Print("Invalid args in command; see 'Help'\n"); + + return TV_SUCCESS; +} + diff --git a/upnp/sample/common/tv_ctrlpt.h b/upnp/sample/common/tv_ctrlpt.h index 8d11e99..b853795 100644 --- a/upnp/sample/common/tv_ctrlpt.h +++ b/upnp/sample/common/tv_ctrlpt.h @@ -143,7 +143,23 @@ int TvCtrlPointPrintList(void); int TvCtrlPointPrintDevice(int); void TvCtrlPointAddDevice(IXML_Document *, const char *, int); void TvCtrlPointHandleGetVar(const char *, const char *, const DOMString); -void TvStateUpdate(char*,int, IXML_Document * , char **); + +/*! + * \brief Update a Tv state table. Called when an event is received. + * + * Note: this function is NOT thread save. It must be called from another + * function that has locked the global device list. + **/ +void TvStateUpdate( + /*! [in] The UDN of the parent device. */ + char *UDN, + /*! [in] The service state table to update. */ + int Service, + /*! [out] DOM document representing the XML received with the event. */ + IXML_Document *ChangedVariables, + /*! [out] pointer to the state table for the Tv service to update. */ + char **State); + void TvCtrlPointHandleEvent(const char *, int, IXML_Document *); void TvCtrlPointHandleSubscribeUpdate(const char *, const Upnp_SID, int); int TvCtrlPointCallbackEventHandler(Upnp_EventType, void *, void *); @@ -154,6 +170,33 @@ int TvCtrlPointStart(print_string printFunctionPtr, state_update updateFunction int TvCtrlPointStop(void); int TvCtrlPointProcessCommand(char *cmdline); +/*! + * \brief Print help info for this application. + */ +void TvCtrlPointPrintShortHelp(void); + +/*! + * \brief Print long help info for this application. + */ +void TvCtrlPointPrintLongHelp(void); + +/*! + * \briefPrint the list of valid command line commands to the user + */ +void TvCtrlPointPrintCommands(void); + +/*! + * \brief Function that receives commands from the user at the command prompt + * during the lifetime of the device, and calls the appropriate + * functions for those commands. + */ +void *TvCtrlPointCommandLoop(void *args); + +/*! + * \brief + */ +int TvCtrlPointProcessCommand(char *cmdline); + #ifdef __cplusplus }; #endif diff --git a/upnp/sample/common/tv_device.c b/upnp/sample/common/tv_device.c index faf0f03..61061d7 100644 --- a/upnp/sample/common/tv_device.c +++ b/upnp/sample/common/tv_device.c @@ -1435,3 +1435,48 @@ int TvDeviceStart(char *ip_address, unsigned short port, return UPNP_E_SUCCESS; } +int device_main(int argc, char *argv[]) +{ + unsigned int portTemp = 0; + char *ip_address = NULL; + char *desc_doc_name = NULL; + char *web_dir_path = NULL; + unsigned short port = 0; + int i = 0; + + SampleUtil_Initialize(linux_print); + /* Parse options */ + for(i = 1; i < argc; i++) { + if (strcmp(argv[i], "-ip") == 0) { + ip_address = argv[++i]; + } else if(strcmp(argv[i], "-port") == 0) { + sscanf(argv[++i], "%u", &portTemp); + } else if(strcmp(argv[i], "-desc") == 0) { + desc_doc_name = argv[++i]; + } else if(strcmp(argv[i], "-webdir") == 0) { + web_dir_path = argv[++i]; + } else if(strcmp(argv[i], "-help") == 0) { + SampleUtil_Print( + "Usage: %s -ip ipaddress -port port" + " -desc desc_doc_name -webdir web_dir_path" + " -help (this message)\n", argv[0]); + SampleUtil_Print( + "\tipaddress: IP address of the device" + " (must match desc. doc)\n" + "\t\te.g.: 192.168.0.4\n" + "\tport: Port number to use for" + " receiving UPnP messages (must match desc. doc)\n" + "\t\te.g.: 5431\n" + "\tdesc_doc_name: name of device description document\n" + "\t\te.g.: tvdevicedesc.xml\n" + "\tweb_dir_path: Filesystem path where web files" + " related to the device are stored\n" + "\t\te.g.: /upnp/sample/tvdevice/web\n"); + return 1; + } + } + port = (unsigned short)portTemp; + return TvDeviceStart(ip_address, port, desc_doc_name, web_dir_path, + linux_print, 0); +} + diff --git a/upnp/sample/common/tv_device.h b/upnp/sample/common/tv_device.h index 19356fa..4541be1 100644 --- a/upnp/sample/common/tv_device.h +++ b/upnp/sample/common/tv_device.h @@ -529,6 +529,22 @@ int TvDeviceStart( /*! [in] Non-zero if called from the combo application. */ int combo); +/*! + * \brief Main entry point for tv device application. + * + * Initializes and registers with the sdk. + * Initializes the state stables of the service. + * Starts the command loop. + * + * Accepts the following optional arguments: + * \li \c -ip ipaddress + * \li \c -port port + * \li \c -desc desc_doc_name + * \li \c -webdir web_dir_path" + * \li \c -help + */ +int device_main(int argc, char *argv[]); + /*! * \brief Stops the device. Uninitializes the sdk. */ diff --git a/upnp/sample/tvcombo/linux/tv_combo_main.c b/upnp/sample/tvcombo/linux/tv_combo_main.c index 32a617e..0e87f96 100644 --- a/upnp/sample/tvcombo/linux/tv_combo_main.c +++ b/upnp/sample/tvcombo/linux/tv_combo_main.c @@ -29,334 +29,15 @@ * ******************************************************************************/ -#define ALLOC_CMD_LINE -#define ALLOC_COMMON_DATA #include "common_data.h" #include "sample_util.h" #include "tv_ctrlpt.h" #include "tv_device.h" -#include #include #include -/*! - * \brief Prints a string to standard out. - */ -void linux_print(const char *format, ...) -{ - va_list argList; - va_start(argList, format); - vfprintf(stdout, format, argList); - fflush(stdout); - va_end(argList); -} - -/*! - * \brief Print help info for this application. - */ -void TvCtrlPointPrintShortHelp(void) -{ - SampleUtil_Print( - "Commands:\n" - " Help\n" - " HelpFull\n" - " ListDev\n" - " Refresh\n" - " PrintDev \n" - " PowerOn \n" - " PowerOff \n" - " SetChannel \n" - " SetVolume \n" - " SetColor \n" - " SetTint \n" - " SetContrast \n" - " SetBrightness \n" - " CtrlAction \n" - " PictAction \n" - " CtrlGetVar \n" - " PictGetVar \n" - " Exit\n"); -} - -void TvCtrlPointPrintLongHelp(void) -{ - SampleUtil_Print( - "\n" - "******************************\n" - "* TV Control Point Help Info *\n" - "******************************\n" - "\n" - "This sample control point application automatically searches\n" - "for and subscribes to the services of television device emulator\n" - "devices, described in the tvdevicedesc.xml description document.\n" - "It also registers itself as a tv device.\n" - "\n" - "Commands:\n" - " Help\n" - " Print this help info.\n" - " ListDev\n" - " Print the current list of TV Device Emulators that this\n" - " control point is aware of. Each device is preceded by a\n" - " device number which corresponds to the devnum argument of\n" - " commands listed below.\n" - " Refresh\n" - " Delete all of the devices from the device list and issue new\n" - " search request to rebuild the list from scratch.\n" - " PrintDev \n" - " Print the state table for the device .\n" - " e.g., 'PrintDev 1' prints the state table for the first\n" - " device in the device list.\n" - " PowerOn \n" - " Sends the PowerOn action to the Control Service of\n" - " device .\n" - " PowerOff \n" - " Sends the PowerOff action to the Control Service of\n" - " device .\n" - " SetChannel \n" - " Sends the SetChannel action to the Control Service of\n" - " device , requesting the channel to be changed\n" - " to .\n" - " SetVolume \n" - " Sends the SetVolume action to the Control Service of\n" - " device , requesting the volume to be changed\n" - " to .\n" - " SetColor \n" - " Sends the SetColor action to the Control Service of\n" - " device , requesting the color to be changed\n" - " to .\n" - " SetTint \n" - " Sends the SetTint action to the Control Service of\n" - " device , requesting the tint to be changed\n" - " to .\n" - " SetContrast \n" - " Sends the SetContrast action to the Control Service of\n" - " device , requesting the contrast to be changed\n" - " to .\n" - " SetBrightness \n" - " Sends the SetBrightness action to the Control Service of\n" - " device , requesting the brightness to be changed\n" - " to .\n" - " CtrlAction \n" - " Sends an action request specified by the string \n" - " to the Control Service of device . This command\n" - " only works for actions that have no arguments.\n" - " (e.g., \"CtrlAction 1 IncreaseChannel\")\n" - " PictAction \n" - " Sends an action request specified by the string \n" - " to the Picture Service of device . This command\n" - " only works for actions that have no arguments.\n" - " (e.g., \"PictAction 1 DecreaseContrast\")\n" - " CtrlGetVar \n" - " Requests the value of a variable specified by the string \n" - " from the Control Service of device .\n" - " (e.g., \"CtrlGetVar 1 Volume\")\n" - " PictGetVar \n" - " Requests the value of a variable specified by the string \n" - " from the Picture Service of device .\n" - " (e.g., \"PictGetVar 1 Tint\")\n" - " Exit\n" - " Exits the control point application.\n"); -} - -/*! - * \briefPrint the list of valid command line commands to the user - */ -void TvCtrlPointPrintCommands() -{ - int i; - int numofcmds = (sizeof cmdloop_cmdlist) / sizeof (cmdloop_commands); - - SampleUtil_Print("Valid Commands:\n"); - for (i = 0; i < numofcmds; ++i) { - SampleUtil_Print(" %-14s %s\n", - cmdloop_cmdlist[i].str, cmdloop_cmdlist[i].args); - } - SampleUtil_Print("\n"); -} - -/*! - * \brief Function that receives commands from the user at the command prompt - * during the lifetime of the device, and calls the appropriate - * functions for those commands. - */ -void *TvCtrlPointCommandLoop(void *args) -{ - char cmdline[100]; - - while (1) { - SampleUtil_Print("\n>> "); - fgets(cmdline, 100, stdin); - TvCtrlPointProcessCommand(cmdline); - } - - return NULL; - args = args; -} - -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; - int 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'\n"); - return TV_SUCCESS; - } - if (invalidargs) { - SampleUtil_Print("Invalid arguments; try 'Help'\n"); - 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 (validargs == 3) - 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 (validargs == 3) - 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 (validargs == 3) - 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 (validargs == 3) - 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'\n"); - break; - } - if(invalidargs) - SampleUtil_Print("Invalid args in command; see 'Help'\n"); - - return TV_SUCCESS; -} - -int device_main(int argc, char **argv) -{ - unsigned int portTemp = 0; - char *ip_address = NULL, - *desc_doc_name = NULL, - *web_dir_path = NULL; - unsigned short port = 0; - int i = 0; - - SampleUtil_Initialize(linux_print); - /* Parse options */ - for( i = 1; i < argc; i++ ) { - if( strcmp( argv[i], "-ip" ) == 0 ) { - ip_address = argv[++i]; - } else if( strcmp( argv[i], "-port" ) == 0 ) { - sscanf( argv[++i], "%u", &portTemp ); - } else if( strcmp( argv[i], "-desc" ) == 0 ) { - desc_doc_name = argv[++i]; - } else if( strcmp( argv[i], "-webdir" ) == 0 ) { - web_dir_path = argv[++i]; - } else if( strcmp( argv[i], "-help" ) == 0 ) { - SampleUtil_Print( - "Usage: %s -ip ipaddress -port port\n" - " -desc desc_doc_name -webdir web_dir_path\n" - " -help (this message)\n\n", argv[0]); - SampleUtil_Print( - "\tipaddress:\tIP address of the device (must match desc. doc)\n" - "\t\te.g.: 192.168.0.4\n\n" - "\tport:\tPort number to use for receiving UPnP messages (must match desc. doc)\n" - "\t\te.g.: 5431\n\n" - "\tdesc_doc_name: name of device description document\n" - "\t\te.g.: tvcombodesc.xml\n\n" - "\tweb_dir_path: Filesystem path where web files related to the device are stored\n" - "\t\te.g.: /upnp/sample/web\n\n"); - return 1; - } - } - port = (unsigned short)portTemp; - return TvDeviceStart(ip_address, port, desc_doc_name, web_dir_path, linux_print, 1); -} - -int main(int argc, char **argv) +int main(int argc, char *argv[]) { int rc; ithread_t cmdloop_thread; diff --git a/upnp/sample/tvctrlpt/linux/tv_ctrlpt_main.c b/upnp/sample/tvctrlpt/linux/tv_ctrlpt_main.c index 4ed58e6..fd6793f 100644 --- a/upnp/sample/tvctrlpt/linux/tv_ctrlpt_main.c +++ b/upnp/sample/tvctrlpt/linux/tv_ctrlpt_main.c @@ -29,8 +29,6 @@ * ******************************************************************************/ -#define ALLOC_CMD_LINE -#define ALLOC_COMMON_DATA #include "common_data.h" #include "sample_util.h" #include "tv_ctrlpt.h" @@ -39,281 +37,6 @@ #include #include -/*! - * \brief Prints a string to standard out. - */ -void linux_print(const char *format, ...) -{ - va_list argList; - va_start(argList, format); - vfprintf(stdout, format, argList); - fflush(stdout); - va_end(argList); -} - -/*! - * \brief Print help info for this application. - */ -void TvCtrlPointPrintShortHelp(void) -{ - SampleUtil_Print( - "Commands:\n" - " Help\n" - " HelpFull\n" - " ListDev\n" - " Refresh\n" - " PrintDev \n" - " PowerOn \n" - " PowerOff \n" - " SetChannel \n" - " SetVolume \n" - " SetColor \n" - " SetTint \n" - " SetContrast \n" - " SetBrightness \n" - " CtrlAction \n" - " PictAction \n" - " CtrlGetVar \n" - " PictGetVar \n" - " Exit\n"); -} - -void TvCtrlPointPrintLongHelp(void) -{ - SampleUtil_Print( - "\n" - "******************************\n" - "* TV Control Point Help Info *\n" - "******************************\n" - "\n" - "This sample control point application automatically searches\n" - "for and subscribes to the services of television device emulator\n" - "devices, described in the tvdevicedesc.xml description document.\n" - "It also registers itself as a tv device.\n" - "\n" - "Commands:\n" - " Help\n" - " Print this help info.\n" - " ListDev\n" - " Print the current list of TV Device Emulators that this\n" - " control point is aware of. Each device is preceded by a\n" - " device number which corresponds to the devnum argument of\n" - " commands listed below.\n" - " Refresh\n" - " Delete all of the devices from the device list and issue new\n" - " search request to rebuild the list from scratch.\n" - " PrintDev \n" - " Print the state table for the device .\n" - " e.g., 'PrintDev 1' prints the state table for the first\n" - " device in the device list.\n" - " PowerOn \n" - " Sends the PowerOn action to the Control Service of\n" - " device .\n" - " PowerOff \n" - " Sends the PowerOff action to the Control Service of\n" - " device .\n" - " SetChannel \n" - " Sends the SetChannel action to the Control Service of\n" - " device , requesting the channel to be changed\n" - " to .\n" - " SetVolume \n" - " Sends the SetVolume action to the Control Service of\n" - " device , requesting the volume to be changed\n" - " to .\n" - " SetColor \n" - " Sends the SetColor action to the Control Service of\n" - " device , requesting the color to be changed\n" - " to .\n" - " SetTint \n" - " Sends the SetTint action to the Control Service of\n" - " device , requesting the tint to be changed\n" - " to .\n" - " SetContrast \n" - " Sends the SetContrast action to the Control Service of\n" - " device , requesting the contrast to be changed\n" - " to .\n" - " SetBrightness \n" - " Sends the SetBrightness action to the Control Service of\n" - " device , requesting the brightness to be changed\n" - " to .\n" - " CtrlAction \n" - " Sends an action request specified by the string \n" - " to the Control Service of device . This command\n" - " only works for actions that have no arguments.\n" - " (e.g., \"CtrlAction 1 IncreaseChannel\")\n" - " PictAction \n" - " Sends an action request specified by the string \n" - " to the Picture Service of device . This command\n" - " only works for actions that have no arguments.\n" - " (e.g., \"PictAction 1 DecreaseContrast\")\n" - " CtrlGetVar \n" - " Requests the value of a variable specified by the string \n" - " from the Control Service of device .\n" - " (e.g., \"CtrlGetVar 1 Volume\")\n" - " PictGetVar \n" - " Requests the value of a variable specified by the string \n" - " from the Picture Service of device .\n" - " (e.g., \"PictGetVar 1 Tint\")\n" - " Exit\n" - " Exits the control point application.\n"); -} - -/*! - * \briefPrint the list of valid command line commands to the user - */ -void TvCtrlPointPrintCommands() -{ - int i; - int numofcmds = (sizeof cmdloop_cmdlist) / sizeof (cmdloop_commands); - - SampleUtil_Print("Valid Commands:\n"); - for (i = 0; i < numofcmds; ++i) { - SampleUtil_Print(" %-14s %s\n", - cmdloop_cmdlist[i].str, cmdloop_cmdlist[i].args); - } - SampleUtil_Print("\n"); -} - -/*! - * \brief Function that receives commands from the user at the command prompt - * during the lifetime of the device, and calls the appropriate - * functions for those commands. - */ -void *TvCtrlPointCommandLoop(void *args) -{ - char cmdline[100]; - - while (1) { - SampleUtil_Print("\n>> "); - fgets(cmdline, 100, stdin); - TvCtrlPointProcessCommand(cmdline); - } - - return NULL; - args = args; -} - -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; - int 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'\n"); - return TV_SUCCESS; - } - if (invalidargs) { - SampleUtil_Print("Invalid arguments; try 'Help'\n"); - 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 (validargs == 3) - 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 (validargs == 3) - 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 (validargs == 3) - 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 (validargs == 3) - 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'\n"); - break; - } - if(invalidargs) - SampleUtil_Print("Invalid args in command; see 'Help'\n"); - - return TV_SUCCESS; -} - int main(int argc, char **argv) { int rc; diff --git a/upnp/sample/tvdevice/linux/tv_device_main.c b/upnp/sample/tvdevice/linux/tv_device_main.c index 0af811f..3475a6f 100644 --- a/upnp/sample/tvdevice/linux/tv_device_main.c +++ b/upnp/sample/tvdevice/linux/tv_device_main.c @@ -29,7 +29,6 @@ * ******************************************************************************/ -#define ALLOC_COMMON_DATA #include "common_data.h" #include "sample_util.h" #include "tv_device.h" @@ -37,18 +36,6 @@ #include #include -/*! - * \brief Prints a string to standard out. - */ -void linux_print(const char *format, ...) -{ - va_list argList; - va_start(argList, format); - vfprintf(stdout, format, argList); - fflush(stdout); - va_end(argList); -} - /*! * \brief Function that receives commands from the user at the command prompt * during the lifetime of the device, and calls the appropriate @@ -85,26 +72,8 @@ void *TvDeviceCommandLoop(void *args) args = args; } -/*! - * \brief Main entry point for tv device application. - * - * Initializes and registers with the sdk. - * Initializes the state stables of the service. - * Starts the command loop. - * - * Accepts the following optional arguments: - * \li \c -ip ipaddress - * \li \c -port port - * \li \c -desc desc_doc_name - * \li \c -webdir web_dir_path" - * \li \c -help - */ int main(int argc, char *argv[]) { - unsigned int portTemp = 0; - char *ip_address = NULL; - char *desc_doc_name = NULL; - char *web_dir_path = NULL; int rc; ithread_t cmdloop_thread; #ifdef WIN32 @@ -113,43 +82,8 @@ int main(int argc, char *argv[]) sigset_t sigs_to_catch; #endif int code; - unsigned short port = 0; - int i = 0; - SampleUtil_Initialize(linux_print); - /* Parse options */ - for(i = 1; i < argc; i++) { - if (strcmp(argv[i], "-ip") == 0) { - ip_address = argv[++i]; - } else if(strcmp(argv[i], "-port") == 0) { - sscanf(argv[++i], "%u", &portTemp); - } else if(strcmp(argv[i], "-desc") == 0) { - desc_doc_name = argv[++i]; - } else if(strcmp(argv[i], "-webdir") == 0) { - web_dir_path = argv[++i]; - } else if(strcmp(argv[i], "-help") == 0) { - SampleUtil_Print( - "Usage: %s -ip ipaddress -port port" - " -desc desc_doc_name -webdir web_dir_path" - " -help (this message)\n", argv[0]); - SampleUtil_Print( - "\tipaddress: IP address of the device" - " (must match desc. doc)\n" - "\t\te.g.: 192.168.0.4\n" - "\tport: Port number to use for" - " receiving UPnP messages (must match desc. doc)\n" - "\t\te.g.: 5431\n" - "\tdesc_doc_name: name of device description document\n" - "\t\te.g.: tvdevicedesc.xml\n" - "\tweb_dir_path: Filesystem path where web files" - " related to the device are stored\n" - "\t\te.g.: /upnp/sample/tvdevice/web\n"); - return 1; - } - } - port = (unsigned short)portTemp; - TvDeviceStart(ip_address, port, desc_doc_name, web_dir_path, - linux_print, 0); + device_main(argc, argv); /* start a command loop thread */ code = ithread_create(&cmdloop_thread, NULL, TvDeviceCommandLoop, NULL); #ifdef WIN32