📄 upnp_tv_device.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:33 $//#include "upnp_tv_device.h"char TvDeviceType[] = "urn:schemas-upnp-org:device:tvdevice:1";char *TvServiceType[] = {"urn:schemas-upnp-org:service:tvcontrol:1", "urn:schemas-upnp-org:service:tvpicture:1"};/* Global arrays for storing Tv Control Service variable names, values, and defaults */char *tvc_varname[] = {"Power","Channel","Volume"};char tvc_varval[TV_CONTROL_VARCOUNT][TV_MAX_VAL_LEN];char *tvc_varval_def[] = {"0", "1", "5"};/* Global arrays for storing Tv Picture Service variable names, values, and defaults */char *tvp_varname[] = {"Color","Tint","Contrast","Brightness"};char tvp_varval[TV_PICTURE_VARCOUNT][TV_MAX_VAL_LEN];char *tvp_varval_def[] = {"5","5","5","5"};/* The amount of time before advertisements will expire */int default_advr_expire=1800;/* Global structure for storing the state table for this device */struct TvService tv_service_table[2];UpnpDevice_Handle device_handle=-1;/* Mutex for protecting the global state table data in a multi-threaded, asynchronous environment. All functions should lock this mutex before reading or writing the state table data. */pthread_mutex_t TVDevMutex= PTHREAD_MUTEX_INITIALIZER;/******************************************************************************** * TvDeviceStateTableInit * * Description: * Initialize the device state table for * this TvDevice, pulling identifier info * from the description Document. Note that * knowledge of the service description is * assumed. State table variables and default * values are currently hard coded in this file * rather than being read from service description * documents. * * Parameters: * DescDocURL -- The description document URL * ********************************************************************************/int TvDeviceStateTableInit(char* DescDocURL) { Upnp_Document DescDoc=NULL; int ret = UPNP_E_SUCCESS; char *servid_ctrl=NULL, *evnturl_ctrl=NULL, *ctrlurl_ctrl=NULL; char *servid_pict=NULL, *evnturl_pict=NULL, *ctrlurl_pict=NULL; char *udn=NULL; int i; if (UpnpDownloadXmlDoc(DescDocURL, &DescDoc) != UPNP_E_SUCCESS) { printf("TvDeviceStateTableInit -- Error Parsing %s\n", DescDocURL); ret = UPNP_E_INVALID_DESC; } /* Find the Tv Control Service identifiers */ if (!SampleUtil_FindAndParseService(DescDoc, DescDocURL, TvServiceType[TV_SERVICE_CONTROL], &servid_ctrl, &evnturl_ctrl, &ctrlurl_ctrl)) { printf("TvDeviceStateTableInit -- Error: Could not find Service: %s\n", TvServiceType[TV_SERVICE_CONTROL]); if (servid_ctrl) free(servid_ctrl); if (evnturl_ctrl) free(evnturl_ctrl); if (ctrlurl_ctrl) free(ctrlurl_ctrl); return(UPNP_E_INVALID_DESC); } /* Find the Tv Picture Service identifiers */ if (!SampleUtil_FindAndParseService(DescDoc, DescDocURL, TvServiceType[TV_SERVICE_PICTURE], &servid_pict, &evnturl_pict, &ctrlurl_pict)) { printf("TvDeviceStateTableInit -- Error: Could not find Service: %s\n", TvServiceType[TV_SERVICE_PICTURE]); if (servid_ctrl) free(servid_ctrl); if (evnturl_ctrl) free(evnturl_ctrl); if (ctrlurl_ctrl) free(ctrlurl_ctrl); if (servid_pict) free(servid_pict); if (evnturl_pict) free(evnturl_pict); if (ctrlurl_pict) free(ctrlurl_pict); return(UPNP_E_INVALID_DESC); } udn = SampleUtil_GetFirstDocumentItem(DescDoc, "UDN"); strcpy(tv_service_table[TV_SERVICE_CONTROL].UDN, udn); strcpy(tv_service_table[TV_SERVICE_CONTROL].ServiceId, servid_ctrl); strcpy(tv_service_table[TV_SERVICE_CONTROL].ServiceType, TvServiceType[TV_SERVICE_CONTROL]); tv_service_table[TV_SERVICE_CONTROL].VariableCount=TV_CONTROL_VARCOUNT; for (i=0; i<tv_service_table[TV_SERVICE_CONTROL].VariableCount; i++) { tv_service_table[TV_SERVICE_CONTROL].VariableName[i] = tvc_varname[i]; tv_service_table[TV_SERVICE_CONTROL].VariableStrVal[i] = tvc_varval[i]; strcpy(tv_service_table[TV_SERVICE_CONTROL].VariableStrVal[i], tvc_varval_def[i]); } strcpy(tv_service_table[TV_SERVICE_PICTURE].UDN, udn); strcpy(tv_service_table[TV_SERVICE_PICTURE].ServiceId, servid_pict); strcpy(tv_service_table[TV_SERVICE_PICTURE].ServiceType, TvServiceType[TV_SERVICE_PICTURE]); tv_service_table[TV_SERVICE_PICTURE].VariableCount=TV_PICTURE_VARCOUNT; for (i=0; i<tv_service_table[TV_SERVICE_PICTURE].VariableCount; i++) { tv_service_table[TV_SERVICE_PICTURE].VariableName[i] = tvp_varname[i]; tv_service_table[TV_SERVICE_PICTURE].VariableStrVal[i] = tvp_varval[i]; strcpy(tv_service_table[TV_SERVICE_PICTURE].VariableStrVal[i], tvp_varval_def[i]); } if (udn) free(udn); if (servid_ctrl) free(servid_ctrl); if (evnturl_ctrl) free(evnturl_ctrl); if (ctrlurl_ctrl) free(ctrlurl_ctrl); if (servid_pict) free(servid_pict); if (evnturl_pict) free(evnturl_pict); if (ctrlurl_pict) free(ctrlurl_pict); return(ret);}/******************************************************************************** * TvDeviceHandleSubscriptionRequest * * Description: * Called during a subscription request callback. If the * subscription request is for this device and either its * control service or picture service, then accept it. * * Parameters: * sr_event -- The subscription request event structure * ********************************************************************************/int TvDeviceHandleSubscriptionRequest(struct Upnp_Subscription_Request *sr_event) { int i,j; Upnp_Document PropSet; pthread_mutex_lock(&TVDevMutex); for (i=0; i<TV_SERVICE_SERVCOUNT; i++) { if ((strcmp(sr_event->UDN,tv_service_table[i].UDN) == 0) && (strcmp(sr_event->ServiceId,tv_service_table[i].ServiceId) == 0)) { /* This is a request for the TvDevice Control Service */ /* UpnpAcceptSubscription(device_handle, sr_event->UDN, sr_event->ServiceId, (char **)tv_service_table[i].VariableName, (char **)tv_service_table[i].VariableStrVal, tv_service_table[i].VariableCount, sr_event->Sid);*/ //Another way of doing the same thing. PropSet = NULL; for (j=0; j< tv_service_table[i].VariableCount; j++) UpnpAddToPropertySet(&PropSet, tv_service_table[i].VariableName[j],tv_service_table[i].VariableStrVal[j]); UpnpAcceptSubscriptionExt(device_handle, sr_event->UDN, sr_event->ServiceId,PropSet,sr_event->Sid); UpnpDocument_free(PropSet); } } pthread_mutex_unlock(&TVDevMutex); return(1);}/******************************************************************************** * TvDeviceHandleGetVarRequest * * Description: * Called during a get variable request callback. If the * request is for this device and either its control service * or picture service, then respond with the variable value. * * Parameters: * cgv_event -- The control get variable request event structure * ********************************************************************************/int TvDeviceHandleGetVarRequest(struct Upnp_State_Var_Request *cgv_event) { int i, j; int getvar_succeeded = 0; cgv_event->CurrentVal = NULL; pthread_mutex_lock(&TVDevMutex); for (i=0; i<TV_SERVICE_SERVCOUNT; i++) { if ((strcmp(cgv_event->DevUDN,tv_service_table[i].UDN)==0) && (strcmp(cgv_event->ServiceID,tv_service_table[i].ServiceId)==0)) { /* Request for variable in the TvDevice Service */ for (j=0; j< tv_service_table[i].VariableCount; j++) { if (strcmp(cgv_event->StateVarName, tv_service_table[i].VariableName[j])==0) { getvar_succeeded = 1; cgv_event->CurrentVal = (Upnp_DOMString) malloc(sizeof(tv_service_table[i].VariableStrVal[j])); strcpy(cgv_event->CurrentVal, tv_service_table[i].VariableStrVal[j]); break; } } } } if (getvar_succeeded) { cgv_event->ErrCode = UPNP_E_SUCCESS; } else { printf("Error in UPNP_CONTROL_GET_VAR_REQUEST callback:\n"); printf(" Unknown variable name = %s\n", cgv_event->StateVarName); cgv_event->ErrCode = 404; strcpy(cgv_event->ErrStr, "Invalid Variable"); } pthread_mutex_unlock(&TVDevMutex); return(cgv_event->ErrCode == UPNP_E_SUCCESS);}/******************************************************************************** * TvDeviceHandleActionRequest * * Description: * Called during an action request callback. If the * request is for this device and either its control service * or picture service, then perform the action and respond. * * Parameters: * ca_event -- The control action request event structure * ********************************************************************************/int TvDeviceHandleActionRequest(struct Upnp_Action_Request *ca_event) { char result_str[500]; char service_type[500]; char *value=NULL; /* Defaults if action not found */ int action_succeeded = -1; int err=401; ca_event->ErrCode = 0; ca_event->ActionResult = NULL; if ((strcmp(ca_event->DevUDN,tv_service_table[TV_SERVICE_CONTROL].UDN)==0) && (strcmp(ca_event->ServiceID,tv_service_table[TV_SERVICE_CONTROL].ServiceId)==0)) { /* Request for action in the TvDevice Control Service */ strcpy(service_type, tv_service_table[TV_SERVICE_CONTROL].ServiceType); if (strcmp(ca_event->ActionName, "PowerOn") == 0) { action_succeeded = TvDevicePowerOn(); } else if (strcmp(ca_event->ActionName, "PowerOff") == 0) { action_succeeded = TvDevicePowerOff(); } else if (strcmp(ca_event->ActionName, "SetChannel") == 0) { if ((value = SampleUtil_GetFirstDocumentItem(ca_event->ActionRequest, "Channel"))) { action_succeeded = TvDeviceSetChannel(atoi(value)); } else { // invalid args error err = 402; action_succeeded = 0; } } else if (strcmp(ca_event->ActionName, "IncreaseChannel") == 0) { action_succeeded = TvDeviceIncrChannel(1); } else if (strcmp(ca_event->ActionName, "DecreaseChannel") == 0) { action_succeeded = TvDeviceIncrChannel(-1); } else if (strcmp(ca_event->ActionName, "SetVolume") == 0) { if ((value = SampleUtil_GetFirstDocumentItem(ca_event->ActionRequest, "Volume"))) { action_succeeded = TvDeviceSetVolume(atoi(value)); } else { // invalid args error err = 402; action_succeeded = 0; } } else if (strcmp(ca_event->ActionName, "IncreaseVolume") == 0) { action_succeeded = TvDeviceIncrVolume(1); } else if (strcmp(ca_event->ActionName, "DecreaseVolume") == 0) { action_succeeded = TvDeviceIncrVolume(-1); } } else if ((strcmp(ca_event->DevUDN,tv_service_table[TV_SERVICE_PICTURE].UDN)==0) && (strcmp(ca_event->ServiceID,tv_service_table[TV_SERVICE_PICTURE].ServiceId)==0)) { /* Request for action in the TvDevice Picture Service */ strcpy(service_type, tv_service_table[TV_SERVICE_PICTURE].ServiceType); if (strcmp(ca_event->ActionName, "SetColor") == 0) { if ((value = SampleUtil_GetFirstDocumentItem(ca_event->ActionRequest, "Color"))) { action_succeeded = TvDeviceSetColor(atoi(value)); } else { // invalid args error err = 402; action_succeeded = 0; } } else if (strcmp(ca_event->ActionName, "IncreaseColor") == 0) { action_succeeded = TvDeviceIncrColor(1); } else if (strcmp(ca_event->ActionName, "DecreaseColor") == 0) { action_succeeded = TvDeviceIncrColor(-1); } else if (strcmp(ca_event->ActionName, "SetTint") == 0) { if ((value = SampleUtil_GetFirstDocumentItem(ca_event->ActionRequest, "Tint"))) { action_succeeded = TvDeviceSetTint(atoi(value)); } else { // invalid args error err = 402; action_succeeded = 0; } } else if (strcmp(ca_event->ActionName, "IncreaseTint") == 0) { action_succeeded = TvDeviceIncrTint(1); } else if (strcmp(ca_event->ActionName, "DecreaseTint") == 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -