📄 upnp_z256_ctrlpt.c
字号:
* node at that position in the global device list. Note * that this function is not thread safe. It must be called * from a function that has locked the global device list. * * Parameters: * devnum -- The number of the device (order in the list, * starting with 1) * devnode -- The output device node pointer * ********************************************************************************/int Z256CtrlPointGetDevice(int devnum, struct Z256DeviceNode **devnode) { int count = devnum; struct Z256DeviceNode *tmpdevnode=NULL; if (count) tmpdevnode = GlobalDeviceList; while (--count && tmpdevnode) { tmpdevnode = tmpdevnode->next; } if (tmpdevnode) { *devnode = tmpdevnode; return(1); } else { printf("Error finding Z256Device number -- %d\n", devnum); return(0); }}/******************************************************************************** * Z256CtrlPointPrintList * * Description: * Print the universal device names for each device in the global device list * * Parameters: * None * ********************************************************************************/int Z256CtrlPointPrintList() { struct Z256DeviceNode *tmpdevnode; int i=0; pthread_mutex_lock(&DeviceListMutex); printf("\nZ256CtrlPointPrintList:\n"); tmpdevnode = GlobalDeviceList; while (tmpdevnode) { printf(" %3d -- %s\n", ++i, tmpdevnode->device.UDN); tmpdevnode = tmpdevnode->next; } printf("\n"); pthread_mutex_unlock(&DeviceListMutex); return(1);}/******************************************************************************** * Z256CtrlPointPrintDevice * * Description: * Print the identifiers and state table for a device from * the global device list. * * Parameters: * devnum -- The number of the device (order in the list, * starting with 1) * ********************************************************************************/int Z256CtrlPointPrintDevice(int devnum) { struct Z256DeviceNode *tmpdevnode; int i=0, service, var; char spacer[15]; if (devnum <= 0) { printf("Error in Z256CtrlPointPrintDevice: invalid devnum = %d\n", devnum); return(0); } pthread_mutex_lock(&DeviceListMutex); printf("\nZ256CtrlPointPrintDevice:\n"); tmpdevnode = GlobalDeviceList; while (tmpdevnode) { i++; if (i == devnum) break; tmpdevnode = tmpdevnode->next; } if (!tmpdevnode) { printf("Error in Z256CtrlPointPrintDevice: invalid devnum = %d -- actual device count = %d\n", devnum, i); } else { printf(" Z256Device -- %d\n", devnum); printf(" | \n"); printf(" +- UDN = %s\n", tmpdevnode->device.UDN); printf(" +- DescDocURL = %s\n", tmpdevnode->device.DescDocURL); printf(" +- FriendlyName = %s\n", tmpdevnode->device.FriendlyName); printf(" +- PresURL = %s\n", tmpdevnode->device.PresURL); printf(" +- Adver. TimeOut = %d\n", tmpdevnode->device.AdvrTimeOut); for (service=0; service<Z256_SERVICE_SERVCOUNT; service++) { if (service < Z256_SERVICE_SERVCOUNT-1) sprintf(spacer, " | "); else sprintf(spacer, " "); printf(" | \n"); printf(" +- Z256 %s Service\n", Z256ServiceName[service]); printf("%s+- ServiceId = %s\n", spacer, tmpdevnode->device.Z256Service[service].ServiceId); printf("%s+- ServiceType = %s\n", spacer, tmpdevnode->device.Z256Service[service].ServiceType); printf("%s+- EventURL = %s\n", spacer, tmpdevnode->device.Z256Service[service].EventURL); printf("%s+- ControlURL = %s\n", spacer, tmpdevnode->device.Z256Service[service].ControlURL); printf("%s+- SID = %s\n", spacer, tmpdevnode->device.Z256Service[service].SID); printf("%s+- ServiceStateTable\n", spacer); for (var=0; var<Z256VarCount[service]; var++) { printf("%s +- %-10s = %s\n", spacer, Z256VarName[service][var], tmpdevnode->device.Z256Service[service].VariableStrVal[var]); } } } printf("\n"); pthread_mutex_unlock(&DeviceListMutex); return(1);}/******************************************************************************** * Z256CtrlPointAddDevice * * Description: * If the device is not already included in the global device list, * add it. Otherwise, update its advertisement expiration timeout. * * Parameters: * DescDoc -- The description document for the device * location -- The location of the description document URL * expires -- The expiration time for this advertisement * ********************************************************************************/void Z256CtrlPointAddDevice (Upnp_Document DescDoc, char *location, int expires) { char *deviceType=NULL; char *friendlyName=NULL; char presURL[200]; char *baseURL=NULL; char *relURL=NULL; char *UDN=NULL; char *serviceId[Z256_SERVICE_SERVCOUNT] = {NULL, NULL}; char *eventURL[Z256_SERVICE_SERVCOUNT] = {NULL, NULL}; char *controlURL[Z256_SERVICE_SERVCOUNT] = {NULL, NULL}; Upnp_SID eventSID[Z256_SERVICE_SERVCOUNT]; int TimeOut[Z256_SERVICE_SERVCOUNT] = {default_timeout, default_timeout}; struct Z256DeviceNode *deviceNode; struct Z256DeviceNode *tmpdevnode; int ret=1; int found=0; int service, var; pthread_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"); if (baseURL) ret = UpnpResolveURL(baseURL, relURL, presURL); else ret = UpnpResolveURL(location, relURL, presURL); if (ret!=UPNP_E_SUCCESS) printf("Error generating presURL from %s + %s\n", baseURL, relURL); if (strcmp(deviceType, Z256DeviceType) == 0) { printf("Found Z256 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; } if (found) { // The device is already there, so just update // the advertisement timeout field tmpdevnode->device.AdvrTimeOut = expires; } else { for (service = 0; service<Z256_SERVICE_SERVCOUNT; service++) { if (SampleUtil_FindAndParseService(DescDoc, location, Z256ServiceType[service], &serviceId[service], &eventURL[service], &controlURL[service])) { printf("Subscribing to EventURL %s...\n", eventURL[service]); ret=UpnpSubscribe(ctrlpt_handle,eventURL[service], &TimeOut[service],eventSID[service]); if (ret==UPNP_E_SUCCESS) { printf("Subscribed to EventURL with SID=%s\n", eventSID[service]); } else { printf("Error Subscribing to EventURL -- %d\n", ret); strcpy(eventSID[service], ""); } } else { printf("Error: Could not find Service: %s\n", Z256ServiceType[service]); } } /* Create a new device node */ deviceNode = (struct Z256DeviceNode *) malloc (sizeof(struct Z256DeviceNode)); 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<Z256_SERVICE_SERVCOUNT; service++) { strcpy(deviceNode->device.Z256Service[service].ServiceId, serviceId[service]); strcpy(deviceNode->device.Z256Service[service].ServiceType, Z256ServiceType[service]); strcpy(deviceNode->device.Z256Service[service].ControlURL, controlURL[service]); strcpy(deviceNode->device.Z256Service[service].EventURL, eventURL[service]); strcpy(deviceNode->device.Z256Service[service].SID, eventSID[service]); for (var = 0; var < Z256VarCount[service]; var++) { deviceNode->device.Z256Service[service].VariableStrVal[var] = (char *) malloc (Z256_MAX_VAL_LEN); strcpy(deviceNode->device.Z256Service[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; } } } pthread_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<Z256_SERVICE_SERVCOUNT; service++) { if (serviceId[service]) free(serviceId[service]); if (controlURL[service]) free(controlURL[service]); if (eventURL[service]) free(eventURL[service]); }}/******************************************************************************** * Z256StateUpdate * * Description: * Update a Z256 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: * 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 Z256 service * to update * ********************************************************************************/void Z256StateUpdate(int Service, Upnp_Document ChangedVariables, char **State){ Upnp_NodeList properties; Upnp_NodeList variables; int length; int length1; int i; Upnp_Node property; Upnp_Node variable; int j; char *tmpstate=NULL; printf("Z256 State Update:\n"); /* Find all of the e:property tags in the document */ properties=UpnpDocument_getElementsByTagName(ChangedVariables, "e:property"); if (properties!=NULL) { length= UpnpNodeList_getLength(properties); /* Loop through each property change found */ for (i=0;i<(length);i++) { property = UpnpNodeList_item(properties,i); /* For each variable name in the state table, check if this is a corresponding property change */ for (j=0;j<Z256VarCount[Service];j++) { variables= UpnpElement_getElementsByTagName(property, Z256VarName[Service][j]); /* If a match is found, extract the value, and update the state table */ if (variables) { length1= UpnpNodeList_getLength(variables); if (length1) { variable=UpnpNodeList_item(variables,0); tmpstate=SampleUtil_GetElementValue(variable); if (tmpstate) { strcpy(State[j], tmpstate); printf("\tVariable Name: %s New Value:'%s'\n", Z256VarName[Service][j],State[j]); } UpnpNode_free(variable); variable=NULL; if (tmpstate) free(tmpstate); tmpstate=NULL; } UpnpNodeList_free(variables); variables=NULL; } } UpnpNode_free(property); } UpnpNodeList_free(properties); } }/******************************************************************************** * Z256CtrlPointHandleEvent * * Description: * Handle a UPnP event that was received. Process the event and update * the appropriate service state table. * * Parameters: * sid -- The subscription id for the event * eventkey -- The eventkey number for the event * changes -- The DOM document representing the changes * ********************************************************************************/void Z256CtrlPointHandleEvent(Upnp_SID sid, int evntkey, Upnp_Document changes) { struct Z256DeviceNode *tmpdevnode; int service; pthread_mutex_lock(&DeviceListMutex); tmpdevnode = GlobalDeviceList; while (tmpdevnode) { for (service=0; service<Z256_SERVICE_SERVCOUNT; service++) { if (strcmp(tmpdevnode->device.Z256Service[service].SID,sid) == 0) { printf("Received Z256 %s Event: %d for SID %s\n", Z256ServiceName[service], evntkey, sid); Z256StateUpdate(service,changes,(char **)&tmpdevnode->device.Z256Service[service].VariableStrVal); break; } } tmpdevnode = tmpdevnode->next; } pthread_mutex_unlock(&DeviceListMutex);}/******************************************************************************** * Z256CtrlPointHandleSubscribeUpdate * * Description: * Handle a UPnP subscription update that was received. Find the * service the update belongs to, and update its subscription * timeout. * * Parameters: * eventURL -- The event URL for the subscription * sid -- The subscription id for the subscription * timeout -- The new timeout for the subscription * ********************************************************************************/void Z256CtrlPointHandleSubscribeUpdate(char *eventURL, Upnp_SID sid, int timeout) { struct Z256DeviceNode *tmpdevnode; int service; pthread_mutex_lock(&DeviceListMutex); tmpdevnode = GlobalDeviceList; while (tmpdevnode) { for (service = 0; service<Z256_SERVICE_SERVCOUNT; service++) { if (strcmp(tmpdevnode->device.Z256Service[service].EventURL,eventURL) == 0) { printf("Received Z256 %s Event Renewal for eventURL %s\n", Z256ServiceName[service], eventURL); strcpy(tmpdevnode->device.Z256Service[service].SID, sid); break; } } tmpdevnode = tmpdevnode->next; } pthread_mutex_unlock(&DeviceListMutex);}/******************************************************************************** * Z256CtrlPointCallbackEventHandler * * Description: * The callback handler registered with the SDK while registering * the control point. Detects the type of callback, and passes the * request on to the appropriate function. * * Parameters: * EventType -- The type of callback event * Event -- Data structure containing event data * Cookie -- Optional data specified during callback registration * ********************************************************************************/int Z256CtrlPointCallbackEventHandler(Upnp_EventType EventType, void *Event, void *Cookie){ SampleUtil_PrintEvent(EventType, Event); switch ( EventType) { /* SSDP Stuff */ case UPNP_DISCOVERY_ADVERTISEMENT_ALIVE: case UPNP_DISCOVERY_SEARCH_RESULT: { struct Upnp_Discovery *d_event = (struct Upnp_Discovery * ) Event;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -