📄 upnp_z256_device.c
字号:
/////////////////////////////////////////////////////////////////////////////// Copyright (c) 2004 RPA Network///////////////////////////////////////////////////////////////////////////////// $Revision: 1.0.0.0 $// $Date: 2004/02/23 $//#include "upnp_z256_device.h"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"};/* Global arrays for storing Z256 Control Service variable names, values, and defaults */char *z256c_varname[] = {"Power"};char z256c_varval[Z256_CONTROL_VARCOUNT][Z256_MAX_VAL_LEN];char *z256c_varval_def[] = {"0"};/* Global arrays for storing Tv Picture Service variable names, values, and defaults */char *z256p_varname[] = {"Color","Tint","Contrast","Brightness"};char z256p_varval[Z256_PICTURE_VARCOUNT][Z256_MAX_VAL_LEN];char *z256p_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 Z256Service z256_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 Z256DevMutex= PTHREAD_MUTEX_INITIALIZER;/******************************************************************************** * Z256DeviceHandleSubscriptionRequest * * 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 Z256DeviceHandleSubscriptionRequest(struct Upnp_Subscription_Request *sr_event){ int i,j; Upnp_Document PropSet; pthread_mutex_lock(&Z256DevMutex); for (i=0; i<Z256_SERVICE_SERVCOUNT; i++) { if ((strcmp(sr_event->UDN,z256_service_table[i].UDN) == 0) && (strcmp(sr_event->ServiceId,z256_service_table[i].ServiceId) == 0)) { /* This is a request for the Z256Device Control Service */ /* UpnpAcceptSubscription(device_handle, sr_event->UDN, sr_event->ServiceId, (char **)z256_service_table[i].VariableName, (char **)z256_service_table[i].VariableStrVal, z256_service_table[i].VariableCount, sr_event->Sid);*/ //Another way of doing the same thing. PropSet = NULL; for (j=0; j< z256_service_table[i].VariableCount; j++) UpnpAddToPropertySet(&PropSet, z256_service_table[i].VariableName[j],z256_service_table[i].VariableStrVal[j]); UpnpAcceptSubscriptionExt(device_handle, sr_event->UDN, sr_event->ServiceId,PropSet,sr_event->Sid); UpnpDocument_free(PropSet); } } pthread_mutex_unlock(&Z256DevMutex); return(1);}/******************************************************************************** * Z256DeviceHandleGetVarRequest * * 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 Z256DeviceHandleGetVarRequest(struct Upnp_State_Var_Request *cgv_event){ int i, j; int getvar_succeeded = 0; cgv_event->CurrentVal = NULL; pthread_mutex_lock(&Z256DevMutex); for (i=0; i<Z256_SERVICE_SERVCOUNT; i++) { if ((strcmp(cgv_event->DevUDN,z256_service_table[i].UDN)==0) && (strcmp(cgv_event->ServiceID,z256_service_table[i].ServiceId)==0)) { /* Request for variable in the Z256Device Service */ for (j=0; j< z256_service_table[i].VariableCount; j++) { if (strcmp(cgv_event->StateVarName, z256_service_table[i].VariableName[j])==0) { getvar_succeeded = 1; cgv_event->CurrentVal = (Upnp_DOMString) malloc(sizeof(z256_service_table[i].VariableStrVal[j])); strcpy(cgv_event->CurrentVal, z256_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(&Z256DevMutex); return(cgv_event->ErrCode == UPNP_E_SUCCESS);}/******************************************************************************** * Z256DeviceHandleActionRequest * * 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 Z256DeviceHandleActionRequest(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,z256_service_table[Z256_SERVICE_CONTROL].UDN)==0) && (strcmp(ca_event->ServiceID,z256_service_table[Z256_SERVICE_CONTROL].ServiceId)==0)) { /* Request for action in the Z256Device Control Service */ strcpy(service_type, z256_service_table[Z256_SERVICE_CONTROL].ServiceType); if (strcmp(ca_event->ActionName, "Power_On") == 0) { action_succeeded = Z256DevicePowerOn(); } else if (strcmp(ca_event->ActionName, "Power_Off") == 0) { action_succeeded = Z256DevicePowerOff(); } else if (strcmp(ca_event->ActionName, "PowerOn") == 0) { action_succeeded = Z256DevicePowerOn(); } else if (strcmp(ca_event->ActionName, "PowerOff") == 0) { action_succeeded = Z256DevicePowerOff(); } else { /* Do Nothing */; } } else if ((strcmp(ca_event->DevUDN,z256_service_table[Z256_SERVICE_PICTURE].UDN)==0) && (strcmp(ca_event->ServiceID,z256_service_table[Z256_SERVICE_PICTURE].ServiceId)==0)) { /* Request for action in the Z256Device Picture Service */ strcpy(service_type, z256_service_table[Z256_SERVICE_PICTURE].ServiceType); } else { printf("Error in UPNP_CONTROL_ACTION_REQUEST callback:\n"); printf(" Unknown UDN = %s or ServiceId = %s\n", ca_event->DevUDN, ca_event->ServiceID ); } if (action_succeeded > 0) { ca_event->ErrCode = UPNP_E_SUCCESS; sprintf(result_str, "<u:%sResponse xmlns:u=\"%s\"> </u:%sResponse>", ca_event->ActionName, service_type, ca_event->ActionName); ca_event->ActionResult = UpnpParse_Buffer(result_str); } else if (action_succeeded == -1) { printf("Error in UPNP_CONTROL_ACTION_REQUEST callback:\n"); printf(" Unknown ActionName = %s\n", ca_event->ActionName); ca_event->ErrCode = 401; strcpy(ca_event->ErrStr, "Invalid Action"); ca_event->ActionResult = NULL; } else { printf("Error in UPNP_CONTROL_ACTION_REQUEST callback:\n"); printf(" Failure while running %s\n", ca_event->ActionName); ca_event->ErrCode = err; strcpy(ca_event->ErrStr, "Invalid Args"); ca_event->ActionResult = NULL; } if (value) free(value); return(ca_event->ErrCode);}/******************************************************************************** * Z256DeviceSetServiceTableVar * * Description: * Update the Z256Device service state table, and notify all subscribed * control points of the updated state. Note that since this function * blocks on the mutex Z256DevMutex, to avoid a hang this function should * not be called within any other function that currently has this mutex * locked. * * Parameters: * service -- The service number (Z256_SERVICE_CONTROL or Z256_SERVICE_PICTURE) * variable -- The variable number (Z256_CONTROL_POWER, Z256_CONTROL_CHANNEL, * Z256_CONTROL_VOLUME, Z256_PICTURE_COLOR, Z256_PICTURE_TINT, * Z256_PICTURE_CONTRAST, or Z256_PICTURE_BRIGHTNESS) * value -- The string representation of the new value * ********************************************************************************/int Z256DeviceSetServiceTableVar(unsigned int service, unsigned int variable, char *value) { Upnp_Document PropSet= NULL; if ((service >= Z256_SERVICE_SERVCOUNT) || (variable >= z256_service_table[service].VariableCount) || (strlen(value) >= Z256_MAX_VAL_LEN)) { return(0); } pthread_mutex_lock(&Z256DevMutex); strcpy(z256_service_table[service].VariableStrVal[variable], value); // Using the first utility api /*UpnpAddToPropertySet(&PropSet, z256_service_table[service].VariableName[variable], z256_service_table[service].VariableStrVal[variable]); UpnpNotifyExt(device_handle, z256_service_table[service].UDN, z256_service_table[service].ServiceId,PropSet); UpnpDocument_free(PropSet); */ //Using second utility API PropSet= UpnpCreatePropertySet(1,z256_service_table[service].VariableName[variable], z256_service_table[service].VariableStrVal[variable]); UpnpNotifyExt(device_handle, z256_service_table[service].UDN, z256_service_table[service].ServiceId,PropSet); UpnpDocument_free(PropSet); /* Send updated power setting notification to subscribed control points */ /* UpnpNotify(device_handle, z256_service_table[service].UDN, z256_service_table[service].ServiceId, (char **)&z256_service_table[service].VariableName[variable], (char **)&z256_service_table[service].VariableStrVal[variable], 1); */ pthread_mutex_unlock(&Z256DevMutex); return(1);}/******************************************************************************** * Z256DeviceSetPower * * Description: * Turn the power on/off, update the Z256Device control service * state table, and notify all subscribed control points of the * updated state. * * Parameters: * on -- If 1, turn power on. If 0, turn power off. * ********************************************************************************/int Z256DeviceSetPower(int on){ char value[Z256_MAX_VAL_LEN]; int ret=0; if (on != 0 && on != 1) { printf("error: can't set power to value %d\n", on); return(0); } /* Vendor-specific code to turn the power on/off goes here */ /* ############# PLC Operation goes here ############# */ sprintf(value, "%d", on); ret = Z256DeviceSetServiceTableVar(Z256_SERVICE_CONTROL, Z256_CONTROL_POWER, value); return(ret);}/******************************************************************************** * Z256DevicePowerOn * * Description: * Turn the power on. * * Parameters: * ********************************************************************************/int Z256DevicePowerOn(){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -