mUpnpDevice *SelectDevice(mUpnpControlPoint *ctrlPoint) { mUpnpDevice *dev; int n; char key; int devNum; n = 0; for (dev = mupnp_controlpoint_getdevices(ctrlPoint); dev != NULL; dev = mupnp_device_next(dev)) { key = 'a' + n; if ('z' < key) break; printf(" [%c] = %s\n", key, mupnp_device_getfriendlyname(dev)); n++; } if (n == 0) return NULL; printf("Select Device : "); key = kbgetkey(); key = tolower(key); printf("%c\n", key); if (!isalpha(key)) return NULL; devNum = key - 'a'; dev = mupnp_controlpoint_getdevices(ctrlPoint); for (n=0; n<devNum; n++) dev = mupnp_device_next(dev); return dev; }
bool mupnp_controlpoint_parseservicesfordevice(mUpnpDevice *dev, mUpnpSSDPPacket *ssdpPkt) { mUpnpService *service; mUpnpDevice *childDev; mupnp_log_debug_l4("Entering...\n"); for (service=mupnp_device_getservices(dev); service != NULL; service = mupnp_service_next(service)) { if (mupnp_controlpoint_parsescservicescpd(service) == false) { return false; } } /* Now only root SCPDs for root services are parsed, but also child devices' services have to be parsed, so parse them */ for (childDev=mupnp_device_getdevices(dev); childDev != NULL; childDev = mupnp_device_next(childDev)) { if (mupnp_controlpoint_parseservicesfordevice(childDev, ssdpPkt) == false) { return false; } } mupnp_log_debug_l4("Leaving...\n"); return true; }
/** * Find a device from the controlpoint by the UDN of the device. * * \param ctrlPoint Controlpoint in question * \param udn Type of the device * */ mUpnpDevice *mupnp_controlpoint_getdevicebyudn(mUpnpControlPoint *ctrlPoint, char *udn) { mUpnpDevice *dev = NULL; mUpnpDevice *childDev = NULL; mupnp_log_debug_l4("Entering...\n"); if (mupnp_strlen(udn) <= 0 || ctrlPoint == NULL) { return NULL; } for (dev = mupnp_controlpoint_getdevices(ctrlPoint); dev != NULL; dev = mupnp_device_next(dev)) { if (mupnp_strcmp(mupnp_device_getudn(dev), udn) == 0) { return dev; } childDev = mupnp_device_getdevicebyudn(dev, udn); if (childDev != NULL) { return childDev; } } mupnp_log_debug_l4("Leaving...\n"); return NULL; }
/** * Find a device from the control point by the exact type of the device. * This function searches for devices, whose *complete type string* * matches the given string, including version number. For example: * "urn:schemas-upnp-org:device:FooDevice:1". If you need to disregard * the version, use \ref mupnp_controlpoint_getdevicebytype * * \param ctrlPoint Controlpoint in question * \param exacttype Type of the device * */ mUpnpDevice* mupnp_controlpoint_getdevicebyexacttype(mUpnpControlPoint* ctrlPoint, const char* exacttype) { mUpnpDevice* dev = NULL; mUpnpDevice* childDev = NULL; mupnp_log_debug_l4("Entering...\n"); if (mupnp_strlen(exacttype) <= 0 || ctrlPoint == NULL) { return NULL; } for (dev = mupnp_controlpoint_getdevices(ctrlPoint); dev != NULL; dev = mupnp_device_next(dev)) { if (mupnp_strcmp(mupnp_device_getdevicetype(dev), exacttype) == 0) { return dev; } childDev = mupnp_device_getdevicebyexacttype(dev, exacttype); if (childDev != NULL) { return childDev; } } mupnp_log_debug_l4("Leaving...\n"); return NULL; }
void PrintDevice(mUpnpDevice *dev, int indent) { mUpnpDevice *childDev; PrintDeviceInfo(dev, indent); for (childDev = mupnp_device_getdevices(dev); childDev != NULL; childDev = mupnp_device_next(childDev)) PrintDevice(childDev, indent+1); }
void PrintControlPointDevice(mUpnpControlPoint *ctrlPoint) { mUpnpDevice *dev; int devCnt; printf("Device Num = %d\n", mupnp_controlpoint_getndevices(ctrlPoint)); devCnt = 0; for (dev = mupnp_controlpoint_getdevices(ctrlPoint); dev != NULL; dev = mupnp_device_next(dev)) { printf("[%d] = %s\n", ++devCnt, mupnp_device_getfriendlyname(dev)); PrintDevice(dev, 1); } }
void PrintDMSInfos(mUpnpControlPoint* ctrlPoint) { mUpnpDevice* dev; int dmsNum; dmsNum = 0; for (dev = mupnp_controlpoint_getdevices(ctrlPoint); dev != NULL; dev = mupnp_device_next(dev)) { if (mupnp_device_isdevicetype(dev, UPNPAVDUMP_DMS_DEVICETYPE)) PrintDMSInfo(dev, ++dmsNum); } if (dmsNum <= 0) printf("Media Server is not found !!\n"); }
void PrintIGDInfos(mUpnpControlPoint *ctrlPoint) { mUpnpDevice *dev; int igdNum; igdNum = 0; for (dev = mupnp_controlpoint_getdevices(ctrlPoint); dev != NULL; dev = mupnp_device_next(dev)) { if (mupnp_device_isdevicetype(dev, UPNPAVDUMP_IGD_DEVICETYPE)) PrintIGDInfo(dev, ++igdNum); } if (igdNum <= 0) printf("IGD is not found !!\n"); }
/** * Find a device from the controlpoint by the type of the device. * This function searches for devices, whose *type part* (i.e. not including * the version) of the device type string matches the given string. * For example: "urn:schemas-upnp-org:device:FooDevice". If you need * to know the version of a device, use \ref mupnp_devicetype_getversion * * \param ctrlPoint Controlpoint in question * \param type Type of the device * */ mUpnpDevice *mupnp_controlpoint_getdevicebytype(mUpnpControlPoint *ctrlPoint, char *type) { mUpnpDevice *dev = NULL; mUpnpDevice *childDev = NULL; const char* typeString = NULL; char* part = NULL; mupnp_log_debug_l4("Entering...\n"); if (mupnp_strlen(type) <= 0 || ctrlPoint == NULL) { return NULL; } for (dev = mupnp_controlpoint_getdevices(ctrlPoint); dev != NULL; dev = mupnp_device_next(dev)) { typeString = mupnp_device_getdevicetype(dev); if (typeString != NULL) { part = mupnp_devicetype_getschematype(typeString); if (mupnp_strcmp(part, type) == 0) { free(part); return dev; } else { free(part); } } childDev = mupnp_device_getdevicebytype(dev, type); if (childDev != NULL) { return childDev; } } mupnp_log_debug_l4("Leaving...\n"); return NULL; }
bool mupnp_controlpoint_ipchanged(mUpnpControlPoint *ctrlPoint) { mUpnpNetworkInterfaceList *current, *added, *removed; mUpnpNetworkInterface *netIf; mUpnpDevice *dev, *tmp; mUpnpSSDPPacket *ssdpPkt; char *address; mupnp_log_debug_l4("Entering...\n"); current = mupnp_net_interfacelist_new(); added = mupnp_net_interfacelist_new(); removed = mupnp_net_interfacelist_new(); if (current == NULL || added == NULL || removed == NULL) { if (current != NULL) mupnp_net_interfacelist_delete(current); if (added != NULL) mupnp_net_interfacelist_delete(added); if (removed != NULL) mupnp_net_interfacelist_delete(removed); return false; } /* Get Interface changes */ mupnp_net_gethostinterfaces(current); mupnp_net_interfacelist_getchanges(ctrlPoint->ifCache, current, added, removed); /* Remove all devices registered through old interface */ for (netIf = mupnp_net_interfacelist_gets(removed); netIf != NULL; netIf = mupnp_net_interface_next(netIf)) { mupnp_controlpoint_lock(ctrlPoint); tmp = mupnp_controlpoint_getdevices(ctrlPoint); while (tmp != NULL) { dev = tmp; tmp = mupnp_device_next(dev); ssdpPkt = mupnp_device_getssdppacket(dev); address = mupnp_ssdp_packet_getlocaladdress(ssdpPkt); if (address != NULL && mupnp_strcmp(address, mupnp_net_interface_getaddress(netIf)) == 0) { /* This device has been received from the removed interface, so it does not exist */ mupnp_controlpoint_unlock(ctrlPoint); mupnp_controlpoint_removedevicebyssdppacket(ctrlPoint, ssdpPkt); mupnp_controlpoint_lock(ctrlPoint); address = NULL; dev = NULL; ssdpPkt = NULL; } } mupnp_controlpoint_unlock(ctrlPoint); } /* Launch new M-SEARCH */ mupnp_controlpoint_search(ctrlPoint, MUPNP_ST_ROOT_DEVICE); /**** Cache current interfaces ****/ mupnp_net_gethostinterfaces(ctrlPoint->ifCache); mupnp_net_interfacelist_delete(current); mupnp_net_interfacelist_delete(added); mupnp_net_interfacelist_delete(removed); mupnp_log_debug_l4("Leaving...\n"); return true; }
/** * Stop the control point. Stops sending/receiveing/responding to any messages. * * @param ctrlPoint The control point to stop * * @return true if successful; otherwise false * */ bool mupnp_controlpoint_stop(mUpnpControlPoint *ctrlPoint) { mUpnpDevice *dev = NULL; mUpnpSSDPServerList *ssdpServerList; mUpnpSSDPResponseServerList *ssdpResServerList; mUpnpHttpServerList *httpServerList; const char *udn = NULL; MUPNP_DEVICE_LISTENER listener = mupnp_controlpoint_getdevicelistener(ctrlPoint); mupnp_log_debug_l4("Entering...\n"); /* Stop expiration handling */ mupnp_thread_stop_with_cond(ctrlPoint->expThread, ctrlPoint->expCond); mupnp_log_debug_s("Expiration thread stopped.\n"); /**** SSDP Server ****/ ssdpServerList = mupnp_controlpoint_getssdpserverlist(ctrlPoint); mupnp_log_debug_s("Stopping ssdp servers.\n"); mupnp_ssdp_serverlist_setlistener(ssdpServerList, NULL); mupnp_ssdp_serverlist_setuserdata(ssdpServerList, NULL); mupnp_ssdp_serverlist_stop(ssdpServerList); mupnp_log_debug_s("Done\n"); mupnp_ssdp_serverlist_close(ssdpServerList); mupnp_ssdp_serverlist_clear(ssdpServerList); /**** SSDP Response Server ****/ ssdpResServerList = mupnp_controlpoint_getssdpresponseserverlist(ctrlPoint); mupnp_log_debug_s("Stopping ssdp response servers.\n"); mupnp_ssdpresponse_serverlist_setlistener(ssdpResServerList, NULL); mupnp_ssdpresponse_serverlist_setuserdata(ssdpResServerList, NULL); mupnp_ssdpresponse_serverlist_stop(ssdpResServerList); mupnp_log_debug_s("Done\n"); mupnp_ssdpresponse_serverlist_close(ssdpResServerList); mupnp_ssdpresponse_serverlist_clear(ssdpResServerList); /**** HTTP Server ****/ httpServerList = mupnp_controlpoint_gethttpserverlist(ctrlPoint); mupnp_log_debug_s("Stopping http servers.\n"); mupnp_http_serverlist_setlistener(httpServerList, NULL); mupnp_http_serverlist_stop(httpServerList); mupnp_log_debug_s("Done\n"); mupnp_http_serverlist_close(httpServerList); mupnp_http_serverlist_clear(httpServerList); mupnp_controlpoint_lock(ctrlPoint); mupnp_log_debug_s("Got controlpoint lock.\n"); /* Unsubscribe from all services */ for (dev = mupnp_controlpoint_getdevices(ctrlPoint); dev != NULL; dev = mupnp_device_next(dev)) { udn = mupnp_device_getudn(dev); /* Call device listener for each device */ if (udn != NULL && listener != NULL) { mupnp_controlpoint_unlock(ctrlPoint); listener(ctrlPoint, udn, mUpnpDeviceStatusRemoved); mupnp_controlpoint_lock(ctrlPoint); } } /* Empty device cache */ mupnp_devicelist_clear(ctrlPoint->deviceList); mupnp_log_debug_s("Device list cleared.\n"); mupnp_controlpoint_unlock(ctrlPoint); mupnp_log_debug_l4("Leaving...\n"); return true; }
/** * The function that calls all HTTP listener callback functions. Do not call * this from applications. * * @param httpReq The received HTTP request */ void mupnp_controlpoint_httprequestreceived(mUpnpHttpRequest *httpReq) { mUpnpControlPoint *ctrlPoint = NULL; mUpnpNotifyRequest *notifyReq = NULL; mUpnpPropertyList *propList = NULL; mUpnpProperty *prop = NULL; mUpnpEventListenerList *eventListeners = NULL; const char *sid = NULL; long seq = 0; long timeout = 0; mUpnpDevice *dev = NULL; mUpnpService *service = NULL; int notifyListeners = 0; mupnp_log_debug_l4("Entering...\n"); ctrlPoint = (mUpnpControlPoint *)mupnp_http_request_getuserdata(httpReq); mupnp_controlpoint_lock(ctrlPoint); #if !defined(MUPNP_NOUSE_SUBSCRIPTION) if (mupnp_http_request_isnotifyrequest(httpReq) == true) { notifyReq = mupnp_event_notify_request_new(); mupnp_event_notify_request_sethttprequest(notifyReq, httpReq); /* Get service according to SID */ sid = mupnp_event_notify_request_getsid(notifyReq); for (dev = mupnp_controlpoint_getdevices(ctrlPoint); dev != NULL; dev = mupnp_device_next(dev)) { service = mupnp_device_getservicebysid(dev, sid); if (service != NULL) break; } if (service != NULL) { /* We found a service */ seq = mupnp_event_notify_request_getseq(notifyReq); /* Check that event key = previous + 1 */ if (seq != 0 && seq != mupnp_service_geteventkey(service) + 1) { /* The sequence does not match, unsubscribe and subscribe */ timeout = mupnp_service_getsubscriptiontimeout(service); mupnp_controlpoint_unsubscribe(ctrlPoint, service); mupnp_controlpoint_subscribe(ctrlPoint, service, timeout); } else { /* Wrap seq, so that assertion is true next time */ if (seq == MUPNP_EVENT_MAX_SEQ) seq = 0; /* Set event key */ mupnp_service_seteventkey(service, seq); notifyListeners = 1; propList = mupnp_event_notify_request_getpropertylist(notifyReq); for (prop=mupnp_propertylist_gets(propList); prop != NULL; prop = mupnp_property_next(prop)) { /* Update the service's state table from the event */ mupnp_controlpoint_updatestatetablefromproperty(service, prop); } } } eventListeners = mupnp_controlpoint_geteventlisteners(ctrlPoint); mupnp_controlpoint_unlock(ctrlPoint); if (notifyListeners && propList != NULL) { /* Notify listeners out of control point lock */ for (prop=mupnp_propertylist_gets(propList); prop != NULL; prop = mupnp_property_next(prop)) { // printf("\n%s\n%s\n%s\n", prop->name->value, prop->sid->value, prop->value->value); mupnp_eventlistenerlist_notify(ctrlPoint, eventListeners, prop); } } mupnp_event_notify_request_delete(notifyReq); mupnp_http_request_postokrequest(httpReq); return; } #endif mupnp_controlpoint_unlock(ctrlPoint); mupnp_http_request_postbadrequest(httpReq); mupnp_log_debug_l4("Leaving...\n"); }
void mupnp_device_ssdpmessagereceived(mUpnpDevice *dev, mUpnpSSDPPacket *ssdpPkt, int filter) { BOOL isRootDev; const char *ssdpST; const char *devUDN, *devType; char ssdpMsg[MUPNP_SSDP_HEADER_LINE_MAXSIZE]; char deviceUSN[MUPNP_SSDP_HEADER_LINE_MAXSIZE]; #if defined WINCE size_t n; #else int n; #endif mUpnpService *service; mUpnpDevice *childDev; const char *ssdpMXString; int ssdpMX; const char *ssdpTargetAddr; mupnp_log_debug_l4("Entering...\n"); ssdpMXString = mupnp_http_headerlist_getvalue(ssdpPkt->headerList, CG_HTTP_MX); ssdpST = mupnp_ssdp_packet_getst(ssdpPkt); /* Check if this ssdp packet has already been checked + filtered */ if (filter) { /**************************************** * Request line * Check the request line for errors, this is not ideal as it currently only * checks for the presence of the strings and not the order. ***************************************/ /**** check for M-SEARCH and return if not found ****/ if (mupnp_strstr(mupnp_string_getvalue(ssdpPkt->dgmPkt->data), CG_HTTP_MSEARCH) < 0) return; /**** check for * and return if not found ****/ if (mupnp_strstr(mupnp_string_getvalue(ssdpPkt->dgmPkt->data), "*") < 0) return; /**** check HTTP version and return if not found ****/ if (mupnp_strstr(mupnp_string_getvalue(ssdpPkt->dgmPkt->data), CG_HTTP_VER11) < 0) return; /**************************************** * check HOST header, should always be 239.255.255.250:1900, return if incorrect ***************************************/ ssdpTargetAddr = mupnp_ssdp_packet_gethost(ssdpPkt); if (mupnp_strcmp(ssdpTargetAddr, MUPNP_SSDP_MULTICAST_ADDRESS) != 0 && !mupnp_net_isipv6address(ssdpTargetAddr) ) return; /**************************************** * check MAN header, return if incorrect ***************************************/ if (mupnp_ssdp_packet_isdiscover(ssdpPkt) == FALSE) return; /**************************************** * check MX header, return if incorrect ***************************************/ if (ssdpMXString == NULL || mupnp_strlen(ssdpMXString)==0) /* return if the MX value does not exist or is empty */ return; /* check if MX value is not an integer */ for (n=0; n<strlen(ssdpMXString); n++) { if (isdigit(ssdpMXString[n]) == 0) /* MX value contains a non-digit so is invalid */ return; } /**************************************** * check ST header and if empty return ***************************************/ if (mupnp_strlen(ssdpST) <= 0) return; /* Check if we have received this search recently * and ignore duplicates. */ if ( filter_duplicate_m_search(ssdpPkt) ) return; ssdpMX = mupnp_ssdp_packet_getmx(ssdpPkt); mupnp_log_debug("Sleeping for a while... (MX:%d)\n", ssdpMX); mupnp_waitrandom((ssdpMX*1000)/4); } isRootDev = mupnp_device_isrootdevice(dev); if (mupnp_st_isalldevice(ssdpST) == TRUE) { /* for root device only */ if (isRootDev == TRUE) { mupnp_device_getnotifydevicent(dev, ssdpMsg, sizeof(ssdpMsg)); mupnp_device_getnotifydeviceusn(dev, deviceUSN, sizeof(deviceUSN)); mupnp_device_postsearchresponse(dev, ssdpPkt, ssdpMsg, deviceUSN); } /* for all devices send */ /* device type : device version */ mupnp_device_getnotifydevicetypent(dev, ssdpMsg, sizeof(ssdpMsg)); mupnp_device_getnotifydevicetypeusn(dev, deviceUSN, sizeof(deviceUSN)); mupnp_device_postsearchresponse(dev, ssdpPkt, ssdpMsg, deviceUSN); /* device UUID */ mupnp_device_postsearchresponse(dev, ssdpPkt, mupnp_device_getudn(dev), mupnp_device_getudn(dev)); } else if (mupnp_st_isrootdevice(ssdpST) == TRUE) { if (isRootDev == TRUE) { mupnp_device_getnotifydeviceusn(dev, deviceUSN, sizeof(deviceUSN)); mupnp_device_postsearchresponse(dev, ssdpPkt, MUPNP_ST_ROOT_DEVICE, deviceUSN); } } else if (mupnp_st_isuuiddevice(ssdpST) == TRUE) { devUDN = mupnp_device_getudn(dev); if (mupnp_streq(ssdpST, devUDN) == TRUE) mupnp_device_postsearchresponse(dev, ssdpPkt, devUDN, devUDN); } else if (mupnp_st_isurn(ssdpST) == TRUE) { devType = mupnp_device_getdevicetype(dev); if (mupnp_streq(ssdpST, devType) == TRUE) { mupnp_device_getnotifydevicetypeusn(dev, deviceUSN, sizeof(deviceUSN)); mupnp_device_postsearchresponse(dev, ssdpPkt, devType, deviceUSN); } } for (service=mupnp_device_getservices(dev); service != NULL; service = mupnp_service_next(service)) mupnp_service_ssdpmessagereceived(service, ssdpPkt); for (childDev = mupnp_device_getdevices(dev); childDev != NULL; childDev = mupnp_device_next(childDev)) mupnp_device_ssdpmessagereceived(childDev, ssdpPkt, FALSE); mupnp_log_debug_l4("Leaving...\n"); }