📄 upnp_z256_ctrlpt.c
字号:
/////////////////////////////////////////////////////////////////////////////// Copyright (c) 2000 Intel Corporation // All rights reserved. //// Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: //// * Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // * Neither name of Intel Corporation nor the names of its contributors // may be used to endorse or promote products derived from this software // without specific prior written permission.// // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.///////////////////////////////////////////////////////////////////////////////// $Revision: 1.1.1.4 $// $Date: 2001/06/15 00:21:32 $//#include "upnp_z256_ctrlpt.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. */pthread_mutex_t DeviceListMutex = PTHREAD_MUTEX_INITIALIZER;UpnpClient_Handle ctrlpt_handle = -1;char Z256DeviceType[] = "urn:schemas-upnp-org:device:z256device:1";char *Z256ServiceType[] = {"urn:schemas-upnp-org:service:z256control:1", "urn:schemas-upnp-org:service:z256picture:1"};char *Z256ServiceName[] = {"Control", "Picture"};/* Global arrays for storing variable names and counts for Z256Control and Z256Picture services */char *Z256VarName[Z256_SERVICE_SERVCOUNT][Z256_MAXVARS] = { {"Power","Channel","Volume",""}, {"Color","Tint","Contrast","Brightness"}};char Z256VarCount[Z256_SERVICE_SERVCOUNT] = {Z256_CONTROL_VARCOUNT, Z256_PICTURE_VARCOUNT};/* Timeout to request during subscriptions */int default_timeout = 1800;/* The first node in the global device list, or NULL if empty */struct Z256DeviceNode *GlobalDeviceList=NULL;/* Tags for valid commands issued at the command prompt */enum cmdloop_tvcmds { PRTHELP=0, POW_ON, POW_OFF, POWON, POWOFF, CTRLACTION, CTRLGETVAR, PRTDEV, LSTDEV, REFRESH, EXITCMD};/* Data structure for parsing commands from the command line */struct cmdloop_commands { char *str; // the string int cmdnum; // the command char *args; // the 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, ""}, {"ListDev", LSTDEV, ""}, {"Refresh", REFRESH, ""}, {"PrintDev", PRTDEV, "<devnum>"}, {"Power_On", POW_ON, "<devnum>"}, {"Power_Off", POW_OFF, "<devnum>"}, {"PowerOn", POWON, "<devnum>"}, {"PowerOff", POWOFF, "<devnum>"}, {"CtrlAction", CTRLACTION, "<devnum> <action (string)>"}, {"CtrlGetVar", CTRLGETVAR, "<devnum> <varname (string)>"}, {"Exit", EXITCMD, ""}};/******************************************************************************** * Z256CtrlPointPrintHelp * * Description: * Print help info for this application. * * Parameters: * None * ********************************************************************************/int Z256CtrlPointPrintHelp() { printf("\n\n"); printf("******************************\n"); printf("* Z256 Control Point Help Info *\n"); printf("******************************\n"); printf("\n\n"); printf("This sample control point application automatically searches\n"); printf("for and subscribes to the services of television device emulator\n"); printf("devices, described in the z256devicedesc.xml description document.\n"); printf("\n\n"); printf("Commands:\n\n"); printf(" Help\n"); printf(" Print this help info.\n\n"); printf(" ListDev\n"); printf(" Print the current list of Z256 Device Emulators that this\n"); printf(" control point is aware of. Each device is preceded by a\n"); printf(" device number which corresponds to the devnum argument of\n"); printf(" commands listed below.\n\n"); printf(" Refresh\n"); printf(" Delete all of the devices from the device list and issue new\n"); printf(" search request to rebuild the list from scratch.\n\n"); printf(" PrintDev <devnum>\n"); printf(" Print the state table for the device <devnum>.\n"); printf(" e.g., 'PrintDev 1' prints the state table for the first\n"); printf(" device in the device list.\n\n"); printf(" Power_On <devnum>\n"); printf(" Sends the PowerOn action to the Control Service of\n"); printf(" device <devnum>.\n\n"); printf(" Power_Off <devnum>\n"); printf(" Sends the PowerOff action to the Control Service of\n"); printf(" device <devnum>.\n\n"); printf(" PowerOn <devnum>\n"); printf(" Sends the PowerOn action to the Control Service of\n"); printf(" device <devnum>.\n\n"); printf(" PowerOff <devnum>\n"); printf(" Sends the PowerOff action to the Control Service of\n"); printf(" device <devnum>.\n\n"); printf(" CtrlAction <devnum> <action>\n"); printf(" Sends an action request specified by the string <action>\n"); printf(" to the Control Service of device <devnum>. This command\n"); printf(" only works for actions that have no arguments.\n"); printf(" (e.g., \"CtrlAction 1 IncreaseChannel\")\n\n"); printf(" CtrlGetVar <devnum> <varname>\n"); printf(" Requests the value of a variable specified by the string <varname>\n"); printf(" from the Control Service of device <devnum>.\n"); printf(" (e.g., \"CtrlGetVar 1 Volume\")\n\n"); printf(" Exit\n"); printf(" Exits the control point application.\n"); return(1);}/******************************************************************************** * Z256CtrlPointDeleteNode * * Description: * Delete a device node from the global device list. Note that this * function is NOT thread safe, and should be called from another * function that has already locked the global device list. * * Parameters: * node -- The device node * ********************************************************************************/int Z256CtrlPointDeleteNode(struct Z256DeviceNode *node) { int ret=UPNP_E_SUCCESS; int service, var; if (!node) { printf("error in Z256CtrlPointDeleteNode: node is empty\n"); return(1); } for (service=0; service<Z256_SERVICE_SERVCOUNT; service++) { /* If we have a valid control SID, then unsubscribe */ if (strcmp(node->device.Z256Service[service].SID, "") != 0) { ret=UpnpUnSubscribe(ctrlpt_handle, node->device.Z256Service[service].SID); if (ret==UPNP_E_SUCCESS) printf("Unsubscribed from Z256 %s EventURL with SID=%s\n", Z256ServiceName[service], node->device.Z256Service[service].SID); else printf("Error unsubscribing to Z256 %s EventURL -- %d\n", Z256ServiceName[service], ret); } for (var = 0; var < Z256VarCount[service]; var++) { if(node->device.Z256Service[service].VariableStrVal[var]) free(node->device.Z256Service[service].VariableStrVal[var]); } } free(node); node = NULL; return(1);}/******************************************************************************** * Z256CtrlPointRemoveDevice * * Description: * Remove a device from the global device list. * * Parameters: * UDN -- The Unique Device Name for the device to remove * ********************************************************************************/int Z256CtrlPointRemoveDevice(char* UDN) { struct Z256DeviceNode *curdevnode, *prevdevnode; int ret=0; pthread_mutex_lock(&DeviceListMutex); curdevnode = GlobalDeviceList; if (!curdevnode) { printf("Warning in Z256CtrlPointRemoveDevice -- Device list empty\n"); } else { if (strcmp(curdevnode->device.UDN,UDN) == 0) { GlobalDeviceList = curdevnode->next; Z256CtrlPointDeleteNode(curdevnode); } else { prevdevnode = curdevnode; curdevnode = curdevnode->next; while (curdevnode) { if (strcmp(curdevnode->device.UDN,UDN) == 0) { prevdevnode->next = curdevnode->next; Z256CtrlPointDeleteNode(curdevnode); break; } prevdevnode = curdevnode; curdevnode = curdevnode->next; } } } pthread_mutex_unlock(&DeviceListMutex); return ret;}/******************************************************************************** * Z256CtrlPointRemoveAll * * Description: * Remove all devices from the global device list. * * Parameters: * None * ********************************************************************************/int Z256CtrlPointRemoveAll() { struct Z256DeviceNode *curdevnode, *next; pthread_mutex_lock(&DeviceListMutex); curdevnode = GlobalDeviceList; GlobalDeviceList = NULL; while (curdevnode) { next = curdevnode->next; Z256CtrlPointDeleteNode(curdevnode); curdevnode = next; } pthread_mutex_unlock(&DeviceListMutex); return(1);}/******************************************************************************** * Z256CtrlPointRefresh * * Description: * Clear the current global device list and issue new search * requests to build it up again from scratch. * * Parameters: * None * ********************************************************************************/int Z256CtrlPointRefresh() { int ret; Z256CtrlPointRemoveAll(); /* Search for device with particular UDN, waiting for up to 5 seconds for the response */ /* ret = UpnpSearchAsync(ctrlpt_handle, 5, "uuid:Upnp-Z256DeviceEmulator-1_0-1234567890001", NULL); */ /* Search for all devices of type z256 device version 1, waiting for up to 5 seconds for the response */ /*ret = UpnpSearchAsync(ctrlpt_handle, 5, Z256DeviceType, NULL);*/ /* Search for all services of type z256 control version 1, waiting for up to 5 seconds for the response */ /* ret = UpnpSearchAsync(ctrlpt_handle, 5, Z256ServiceType[Z256_SERVICE_CONTROL], NULL); */ /* Search for all root devices, waiting for up to 5 seconds for the response */ ret = UpnpSearchAsync(ctrlpt_handle, 5, "upnp:rootdevice", NULL); if (ret != UPNP_E_SUCCESS) printf("Error sending search request%d\n", ret); return(1);}/******************************************************************************** * Z256CtrlPointGetVar * * Description: * Send a GetVar request to the specified service of a device. * * Parameters: * service -- The service * devnum -- The number of the device (order in the list, * starting with 1) * varname -- The name of the variable to request. * ********************************************************************************/int Z256CtrlPointGetVar(int service, int devnum, char* varname){ struct Z256DeviceNode *devnode; int ret=0; Upnp_DOMString StVarVal = NULL; pthread_mutex_lock(&DeviceListMutex); if (!Z256CtrlPointGetDevice(devnum, &devnode)) { ret = 0;; } else { //ret = UpnpGetServiceVarStatusAsync( ctrlpt_handle, devnode->device.Z256Service[service].ControlURL, varname, Z256CtrlPointCallbackEventHandler, NULL); ret = UpnpGetServiceVarStatus(ctrlpt_handle, devnode->device.Z256Service[service].ControlURL, varname, &StVarVal); if (ret != UPNP_E_SUCCESS) { printf("Error in UpnpGetServiceVarStatusAsync -- %d\n", ret); } else { printf(" %s \n", StVarVal); UpnpDOMString_free(StVarVal); } } pthread_mutex_unlock(&DeviceListMutex); return(ret);}/******************************************************************************** * Z256CtrlPointSendAction * * Description: * Send an Action request to the specified service of a device. * * Parameters: * service -- The service * devnum -- The number of the device (order in the list, * starting with 1) * actionname -- The name of the action. * param_name -- An array of parameter names * param_val -- The corresponding parameter values * param_count -- The number of parameters * ********************************************************************************/int Z256CtrlPointSendAction(int service, int devnum, char *actionname, char **param_name, char **param_val, int param_count){ struct Z256DeviceNode *devnode; char ActionXml[250]; char tmpstr[100]; Upnp_Document actionNode=NULL; int ret=0; int param; pthread_mutex_lock(&DeviceListMutex); if (!Z256CtrlPointGetDevice(devnum, &devnode)) { ret = 0;; } else { /*sprintf(ActionXml, "<u:%s xmlns:u=\"%s\">", actionname, Z256ServiceType[service]); for (param = 0; param < param_count; param++) { sprintf(tmpstr, "<%s>%s</%s>", param_name[param], param_val[param], param_name[param]); strcat(ActionXml, tmpstr); } sprintf(tmpstr, "</u:%s>", actionname); strcat(ActionXml, tmpstr); actionNode = UpnpParse_Buffer( ActionXml);*/ if( param_count== 0) //UpnpAddToAction(&actionNode,actionname,Z256ServiceType[service],NULL,NULL); OR actionNode = UpnpMakeAction(actionname,Z256ServiceType[service],0,NULL); else for (param = 0; param < param_count; param++) { if( UpnpAddToAction(&actionNode,actionname,Z256ServiceType[service],param_name[param], param_val[param]) != UPNP_E_SUCCESS) { printf("Error in adding action parameter !!!!!!!!!!!!!!!!\n"); return -1; } } ret = UpnpSendActionAsync( ctrlpt_handle, devnode->device.Z256Service[service].ControlURL, Z256ServiceType[service], devnode->device.UDN, actionNode, Z256CtrlPointCallbackEventHandler, NULL); if (ret != UPNP_E_SUCCESS) printf("Error in UpnpSendActionAsync -- %d\n", ret); } pthread_mutex_unlock(&DeviceListMutex); if (actionNode) UpnpDocument_free(actionNode); return(ret);}/******************************************************************************** * Z256CtrlPointGetDevice * * Description: * Given a list number, returns the pointer to the device
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -