Example #1
0
void JNICore::onMediaServerStateVariablesChanged(DeviceDesc *deviceDesc, ServiceDesc *serviceDesc, const NPT_List<NPT_String>& nameList, const NPT_List<NPT_String>& valueList)
{
	NPT_LOG_FINEST("onMediaServerStateVariablesChanged aaaa");
	VMGuard vmguard;
	if (JNIEnv *env = vmguard.env()) {
	
		NPT_UInt64 u1, u2;
		deviceDesc->uuid().getData(u1, u2);
		jobject uuidObj = env->NewObject(CG::c_UUID, CG::m_UUID_Init, u1, u2);

		jstring serviceIdObj = env->NewStringUTF(serviceDesc->serviceId().GetChars());
		
		jobjectArray nameArr = env->NewObjectArray(nameList.GetItemCount(), CG::c_String, NULL);
		jobjectArray valueArr = env->NewObjectArray(valueList.GetItemCount(), CG::c_String, NULL);
		
		jstring sObj;
		for (NPT_Ordinal i = 0; i < nameList.GetItemCount(); i++) {
			sObj = env->NewStringUTF(nameList.GetItem(i)->GetChars());
			env->SetObjectArrayElement(nameArr, i, sObj);
			env->DeleteLocalRef(sObj);

			sObj = env->NewStringUTF(valueList.GetItem(i)->GetChars());
			env->SetObjectArrayElement(valueArr, i, sObj);
			env->DeleteLocalRef(sObj);
		}
		
		NPT_LOG_FINEST("onMediaServerStateVariablesChanged bbbb");
		env->CallVoidMethod(m_delegateObj, CG::f_DLNACore_hookMSSVC, uuidObj, serviceIdObj, nameArr, valueArr);
		env->DeleteLocalRef(uuidObj);
		env->DeleteLocalRef(serviceIdObj);
		env->DeleteLocalRef(nameArr);
		env->DeleteLocalRef(valueArr);
	}
}
Example #2
0
NPT_String RootContainer::generateDidl(const NPT_List<const Object*>& ls, const NPT_String& resUriTmpl)
{
	NPT_StringOutputStream outputStream;
	NPT_XmlSerializer xml(&outputStream, 0, true, true);

	xml.StartDocument();
	xml.StartElement(NULL, "DIDL-Lite");
	xml.Attribute(NULL, "xmlns", "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/");
	xml.Attribute("xmlns", "dc", "http://purl.org/dc/elements/1.1/");
	xml.Attribute("xmlns", "upnp", "urn:schemas-upnp-org:metadata-1-0/upnp/");

	for (NPT_Ordinal i = 0; i < ls.GetItemCount(); i++) {
		const Object *obj = *ls.GetItem(i);
		if (const Item *item = obj->asItem()) {
			outputItem(xml, item, resUriTmpl);
		} else if (const Container *container = obj->asContainer()) {
			outputContainer(xml, container, resUriTmpl);
		}
	}

	xml.EndElement(NULL, "DIDL-Lite");
	xml.EndDocument();
	return outputStream.GetString();
}
Example #3
0
int SimpleDMR::onAction(const ServiceDecl *serviceDecl, const ActionDecl *actionDecl, AbortableTask *task, const FrontEnd::InterfaceContext *ifctx, const FrontEnd::RequestContext& reqCtx, const NPT_HttpRequest *req, const NPT_List<NPT_String>& inputArgNames, const NPT_List<NPT_String>& inputArgValues, NPT_List<NPT_String>& outputArgValues)
{
	WriteLocker locker(m_stateLock);
	if (NPT_String::Compare(serviceDecl->serviceId, "urn:upnp-org:serviceId:AVTransport") == 0) {

		if (NPT_String::Compare(actionDecl->name, "SetAVTransportURI") == 0) {
			if (inputArgValues.GetItemCount() != 3) {
				// Invalid Args
				return 402;
			}

			if (inputArgValues.GetFirstItem()->Compare("0") != 0) {
				// Invalid InstanceID
				return 718;
			}

			NPT_HttpClient httpClient;
			NPT_HttpRequest req(*inputArgValues.GetItem(1), NPT_HTTP_METHOD_GET, NPT_HTTP_PROTOCOL_1_1);
			Helper::setupHttpRequest(req);
			NPT_HttpResponse *resp;
			NPT_Result nr;
			HttpClientAbortCallback abortCallback(&httpClient);
			if (task->registerAbortCallback(&abortCallback)) {
				nr = httpClient.SendRequest(req, resp);
				task->unregisterAbortCallback(&abortCallback);
			} else {
				return 715;
			}

			if (NPT_FAILED(nr)) {
				return 716;
			}

			PtrHolder<NPT_HttpResponse> resp1(resp);
			if (resp->GetStatusCode() != 200) {
				return 716;
			}

			NPT_HttpHeader *hdrContentType = resp->GetHeaders().GetHeader(NPT_HTTP_HEADER_CONTENT_TYPE);
			if (!hdrContentType) {
				return 714;
			}

			setVar(DMRVAR_AVTransportURI, *inputArgValues.GetItem(1));
			setVar(DMRVAR_AVTransportURIMetaData, *inputArgValues.GetItem(2));
			setVar(DMRVAR_CurrentTrackMetaData, *inputArgValues.GetItem(2));
			m_callback->doDmrOpen(this, *inputArgValues.GetItem(1), hdrContentType->GetValue(), *inputArgValues.GetItem(2));
			return 0;
		}

		if (NPT_String::Compare(actionDecl->name, "GetMediaInfo") == 0) {
			if (inputArgValues.GetItemCount() != 1) {
				// Invalid Args
				return 402;
			}

			if (inputArgValues.GetFirstItem()->Compare("0") != 0) {
				// Invalid InstanceID
				return 718;
			}

			*outputArgValues.GetItem(0) = m_avtVars[DMRVAR_NumberOfTracks - DMRVAR_BaseIndexAVT];
			*outputArgValues.GetItem(1) = m_avtVars[DMRVAR_CurrentMediaDuration - DMRVAR_BaseIndexAVT];
			*outputArgValues.GetItem(2) = m_avtVars[DMRVAR_AVTransportURI - DMRVAR_BaseIndexAVT];
			*outputArgValues.GetItem(3) = m_avtVars[DMRVAR_AVTransportURIMetaData - DMRVAR_BaseIndexAVT];
			*outputArgValues.GetItem(4) = m_avtVars[DMRVAR_NextAVTransportURI - DMRVAR_BaseIndexAVT];
			*outputArgValues.GetItem(5) = m_avtVars[DMRVAR_NextAVTransportURIMetaData - DMRVAR_BaseIndexAVT];
			*outputArgValues.GetItem(6) = m_avtVars[DMRVAR_PlaybackStorageMedium - DMRVAR_BaseIndexAVT];
			*outputArgValues.GetItem(7) = m_avtVars[DMRVAR_RecordStorageMedium - DMRVAR_BaseIndexAVT];
			*outputArgValues.GetItem(8) = m_avtVars[DMRVAR_RecordMediumWriteStatus - DMRVAR_BaseIndexAVT];
			return 0;
		}

		if (NPT_String::Compare(actionDecl->name, "GetTransportInfo") == 0) {
			if (inputArgValues.GetItemCount() != 1) {
				// Invalid Args
				return 402;
			}

			if (inputArgValues.GetFirstItem()->Compare("0") != 0) {
				// Invalid InstanceID
				return 718;
			}

			*outputArgValues.GetItem(0) = m_avtVars[DMRVAR_TransportState - DMRVAR_BaseIndexAVT];
			*outputArgValues.GetItem(1) = m_avtVars[DMRVAR_TransportStatus - DMRVAR_BaseIndexAVT];
			*outputArgValues.GetItem(2) = m_avtVars[DMRVAR_TransportPlaySpeed - DMRVAR_BaseIndexAVT];
			return 0;
		}

		if (NPT_String::Compare(actionDecl->name, "GetPositionInfo") == 0) {
			if (inputArgValues.GetItemCount() != 1) {
				// Invalid Args
				return 402;
			}

			if (inputArgValues.GetFirstItem()->Compare("0") != 0) {
				// Invalid InstanceID
				return 718;
			}

			*outputArgValues.GetItem(0) = m_avtVars[DMRVAR_CurrentTrack - DMRVAR_BaseIndexAVT];
			*outputArgValues.GetItem(1) = m_avtVars[DMRVAR_CurrentTrackDuration - DMRVAR_BaseIndexAVT];
			*outputArgValues.GetItem(2) = m_avtVars[DMRVAR_CurrentTrackMetaData - DMRVAR_BaseIndexAVT];
			*outputArgValues.GetItem(3) = m_avtVars[DMRVAR_CurrentTrackURI - DMRVAR_BaseIndexAVT];
			*outputArgValues.GetItem(4) = m_avtVars[DMRVAR_RelativeTimePosition - DMRVAR_BaseIndexAVT];
			*outputArgValues.GetItem(5) = m_avtVars[DMRVAR_AbsoluteTimePosition - DMRVAR_BaseIndexAVT];
			*outputArgValues.GetItem(6) = m_avtVars[DMRVAR_RelativeCounterPosition - DMRVAR_BaseIndexAVT];
			*outputArgValues.GetItem(7) = m_avtVars[DMRVAR_AbsoluteCounterPosition - DMRVAR_BaseIndexAVT];
			return 0;
		}

		if (NPT_String::Compare(actionDecl->name, "GetDeviceCapabilities") == 0) {
			if (inputArgValues.GetItemCount() != 1) {
				// Invalid Args
				return 402;
			}

			if (inputArgValues.GetFirstItem()->Compare("0") != 0) {
				// Invalid InstanceID
				return 718;
			}

			*outputArgValues.GetItem(0) = m_avtVars[DMRVAR_PossiblePlaybackStorageMedia - DMRVAR_BaseIndexAVT];
			*outputArgValues.GetItem(1) = m_avtVars[DMRVAR_PossibleRecordStorageMedia - DMRVAR_BaseIndexAVT];
			*outputArgValues.GetItem(2) = m_avtVars[DMRVAR_PossibleRecordQualityModes - DMRVAR_BaseIndexAVT];
			return 0;
		}

		if (NPT_String::Compare(actionDecl->name, "GetTransportSettings") == 0) {
			if (inputArgValues.GetItemCount() != 1) {
				// Invalid Args
				return 402;
			}

			if (inputArgValues.GetFirstItem()->Compare("0") != 0) {
				// Invalid InstanceID
				return 718;
			}

			*outputArgValues.GetItem(0) = m_avtVars[DMRVAR_CurrentPlayMode - DMRVAR_BaseIndexAVT];
			*outputArgValues.GetItem(1) = m_avtVars[DMRVAR_CurrentRecordQualityMode - DMRVAR_BaseIndexAVT];
			return 0;
		}

		if (NPT_String::Compare(actionDecl->name, "Stop") == 0) {
			m_callback->doDmrStop(this);
			return 0;
		}

		if (NPT_String::Compare(actionDecl->name, "Play") == 0) {
			m_callback->doDmrPlay(this);
			return 0;
		}

		if (NPT_String::Compare(actionDecl->name, "Pause") == 0) {
			m_callback->doDmrPause(this);
			return 0;
		}

		if (NPT_String::Compare(actionDecl->name, "Seek") == 0) {
			if (inputArgValues.GetItemCount() != 3) {
				// Invalid Args
				return 402;
			}

			if (inputArgValues.GetFirstItem()->Compare("0") != 0) {
				// Invalid InstanceID
				return 718;
			}

			const NPT_String& seekMode = *inputArgValues.GetItem(1);
			const NPT_String& seekTarget = *inputArgValues.GetItem(2);
			if (seekMode.Compare("TRACK_NR") == 0) {
				// TODO:
			} else if (seekMode.Compare("REL_TIME") == 0) {
				NPT_UInt64 pos;
				if (NPT_FAILED(Helper::parseTrackDurationString(seekTarget, pos))) {
					// Illegal seek target
					return 711;
				}
				m_callback->doDmrSeekTo(this, pos);
			} else {
				// Seek mode not supported
				return 710;
			}

			return 0;
		}

		if (NPT_String::Compare(actionDecl->name, "Next") == 0) {
			return 0;
		}

		if (NPT_String::Compare(actionDecl->name, "Previous") == 0) {
			return 0;
		}

		return 602;
	}

	if (NPT_String::Compare(serviceDecl->serviceId, "urn:upnp-org:serviceId:RenderingControl") == 0) {

		if (NPT_String::Compare(actionDecl->name, "ListPresets") == 0) {
			if (inputArgValues.GetItemCount() != 1) {
				// Invalid Args
				return 402;
			}

			if (inputArgValues.GetFirstItem()->Compare("0") != 0) {
				// Invalid InstanceID
				return 702;
			}

			*outputArgValues.GetItem(0) = m_rcsVars[DMRVAR_PresetNameList - DMRVAR_BaseIndexRCS];
			return 0;
		}

		if (NPT_String::Compare(actionDecl->name, "SelectPreset") == 0) {
			return 0;
		}

		if (NPT_String::Compare(actionDecl->name, "GetMute") == 0) {
			if (inputArgValues.GetItemCount() != 2) {
				// Invalid Args
				return 402;
			}

			if (inputArgValues.GetFirstItem()->Compare("0") != 0) {
				// Invalid InstanceID
				return 702;
			}

			*outputArgValues.GetItem(0) = m_rcsVars[DMRVAR_Mute - DMRVAR_BaseIndexRCS];
			return 0;
		}

		if (NPT_String::Compare(actionDecl->name, "SetMute") == 0) {
			if (inputArgValues.GetItemCount() != 3) {
				// Invalid Args
				return 402;
			}

			if (inputArgValues.GetFirstItem()->Compare("0") != 0) {
				// Invalid InstanceID
				return 702;
			}

			const NPT_String& desiredMute = *inputArgValues.GetItem(2);
			m_callback->doDmrSetMute(this, desiredMute.Compare("true", true) == 0 || desiredMute.Compare("1") == 0);
			return 0;
		}

		if (NPT_String::Compare(actionDecl->name, "GetVolume") == 0) {
			if (inputArgValues.GetItemCount() != 2) {
				// Invalid Args
				return 402;
			}

			if (inputArgValues.GetFirstItem()->Compare("0") != 0) {
				// Invalid InstanceID
				return 702;
			}

			*outputArgValues.GetItem(0) = m_rcsVars[DMRVAR_Volume - DMRVAR_BaseIndexRCS];
			return 0;
		}

		if (NPT_String::Compare(actionDecl->name, "SetVolume") == 0) {
			if (inputArgValues.GetItemCount() != 3) {
				// Invalid Args
				return 402;
			}

			if (inputArgValues.GetFirstItem()->Compare("0") != 0) {
				// Invalid InstanceID
				return 702;
			}

			const NPT_String& desiredVolume = *inputArgValues.GetItem(2);
			int volume;
			if (NPT_FAILED(NPT_ParseInteger(desiredVolume, volume))) {
				// Invalid Args
				return 402;
			}
			m_callback->doDmrSetVolume(this, volume);
			return 0;
		}

		return 602;
	}

	if (NPT_String::Compare(serviceDecl->serviceId, "urn:upnp-org:serviceId:ConnectionManager") == 0) {

		if (NPT_String::Compare(actionDecl->name, "GetProtocolInfo") == 0) {
			NPT_String v;
			if (getStateValue(serviceDecl->serviceId, "SourceProtocolInfo", v)) {
				*outputArgValues.GetItem(0) = v;
			}

			if (getStateValue(serviceDecl->serviceId, "SinkProtocolInfo", v)) {
				*outputArgValues.GetItem(1) = v;
			}
			return 0;
		}

		if (NPT_String::Compare(actionDecl->name, "GetCurrentConnectionIDs") == 0) {
			NPT_String v;
			if (getStateValue(serviceDecl->serviceId, "SourceProtocolInfo", v)) {
				*outputArgValues.GetItem(0) = v;
			}
			return 0;
		}

		if (NPT_String::Compare(actionDecl->name, "GetCurrentConnectionInfo") == 0) {
			*outputArgValues.GetItem(0) = "0";
			*outputArgValues.GetItem(1) = "0";
			*outputArgValues.GetItem(2) = "";
			*outputArgValues.GetItem(3) = "";
			*outputArgValues.GetItem(4) = "-1";
			*outputArgValues.GetItem(5) = "Input"; // or "Output"? WTF!
			*outputArgValues.GetItem(6) = "OK";
			return 0;
		}

		return 602;
	}

	return 501;
}
Example #4
0
void FrontEnd::broadcastLocked(DeviceImplInfo *deviceInfo, bool avail)
{
	NPT_TimeStamp ts;
	NPT_System::GetCurrentTimeStamp(ts);

	NPT_List<MatchContext*> matchList;

	if (deviceInfo) {
		deviceInfo->m_updateTS = ts;
		MatchContext *matchContext = new MatchContext();
		if (deviceInfo->m_deviceImpl->match("ssdp:all", matchContext->matches)) {
			matchList.Add(matchContext);
			matchContext->deviceUuid = deviceInfo->m_deviceImpl->uuid();
			matchContext->expireSeconds = deviceInfo->m_deviceImpl->m_expireSeconds;
			matchContext->descPath = deviceInfo->m_deviceImpl->m_descPath;
			matchContext->httpRoot = deviceInfo->m_context.m_httpRoot;
		} else {
			delete matchContext;
		}
	} else {
		for (NPT_Ordinal i = 0; i < m_deviceImplList.GetItemCount(); i++) {
			NPT_List<DeviceImplInfo*>::Iterator it = m_deviceImplList.GetItem(i);
			DeviceImplInfo *info = *it;
			info->m_updateTS = ts;
			MatchContext *matchContext = new MatchContext();
			if (info->m_deviceImpl->match("ssdp:all", matchContext->matches)) {
				matchList.Add(matchContext);
				matchContext->deviceUuid = info->m_deviceImpl->uuid();
				matchContext->expireSeconds = info->m_deviceImpl->m_expireSeconds;
				matchContext->descPath = info->m_deviceImpl->m_descPath;
				matchContext->httpRoot = info->m_context.m_httpRoot;
			} else {
				delete matchContext;
			}
		}
	}

	NPT_SocketAddress targetAddr(NPT_IpAddress(239, 255, 255, 250), 1900);

	for (NPT_Ordinal i = 0; i < m_ifList.GetItemCount(); i++) {
		Interface *nif = *m_ifList.GetItem(i);
		NPT_UdpSocket sock(NPT_SOCKET_FLAG_CANCELLABLE);
		sock.Bind(NPT_SocketAddress(nif->m_context.m_ifAddr, 0));
		for (NPT_Ordinal j = 0; j < matchList.GetItemCount(); j++) {
			MatchContext *matchContext = *matchList.GetItem(j);
			NPT_String location = NPT_String::Format("http://%s:%d%s%s", nif->m_context.m_ifAddr.ToString().GetChars(), nif->m_context.m_httpPort, matchContext->httpRoot.GetChars(), matchContext->descPath.GetChars());
			for (NPT_Ordinal k = 0; k < matchContext->matches.GetItemCount(); k++) {
				NPT_List<DeviceImplMatch>::Iterator it2 = matchContext->matches.GetItem(k);
				NPT_String msg;
				if (avail) {
					//msg = NPT_String::Format("NOTIFY * HTTP/1.1\r\nHOST: 239.255.255.250:1900\r\nCACHE-CONTROL: max-age=%d\r\nLOCATION: %s\r\nNT: %s\r\nNTS: ssdp:alive\r\nSERVER: %s\r\nUSN: %s\r\n\r\n",
					//		matchContext->expireSeconds, location.GetChars(), it2->m_st.GetChars(), m_serverHeader.GetChars(), it2->m_usn.GetChars());
					msg = NPT_String::Format("NOTIFY * HTTP/1.1\r\nHOST: 239.255.255.250:1900\r\nCACHE-CONTROL: max-age=%d\r\nLOCATION: %s\r\nNT: %s\r\nNTS: ssdp:alive\r\nSERVER: %s\r\nUSN: %s\r\nCUSTOM:%s\r\n\r\n",
											matchContext->expireSeconds, location.GetChars(), it2->m_st.GetChars(), m_serverHeader.GetChars(), it2->m_usn.GetChars(), m_DevName.GetChars());
				} else {
					msg = NPT_String::Format("NOTIFY * HTTP/1.1\r\nHOST: 239.255.255.250:1900\r\nNT: %s\r\nNTS: ssdp:byebye\r\nUSN: %s\r\nCUSTOM:%s\r\n\r\n",
											it2->m_st.GetChars(), it2->m_usn.GetChars(), m_DevName.GetChars());
				}
				NPT_DataBuffer packet(msg.GetChars(), msg.GetLength(), false);
				sock.Send(packet, &targetAddr);
			}
		}
	}

	matchList.Apply(NPT_ObjectDeleter<MatchContext>());
}
Example #5
0
NPT_Result FrontEnd::start()
{
	WriteLocker locker(m_stateLock);
	if (m_state != State_Stopped) {
		return NPT_ERROR_INVALID_STATE;
	}

	NPT_Result nr;

	NPT_List<NPT_NetworkInterface*> ifList;
	nr = NPT_NetworkInterface::GetNetworkInterfaces(ifList);
	if (NPT_FAILED(nr)) {
		return nr;
	}

	for (NPT_Ordinal i = 0; i < ifList.GetItemCount(); i++) {
		NPT_NetworkInterface *nif = *ifList.GetItem(i);
		if (nif->GetAddresses().GetFirstItem() && (m_includeLoopback || ((nif->GetFlags() & NPT_NETWORK_INTERFACE_FLAG_LOOPBACK) == 0))) {
			Interface *intf = new Interface();
			intf->m_owner = this;
			intf->m_nif = nif;
			intf->m_context.m_ifAddr = nif->GetAddresses().GetFirstItem()->GetPrimaryAddress();
			intf->m_httpConnector = new HttpConnector(intf, intf->m_context.m_ifAddr);
			intf->m_ssdpConnector = new SsdpConnector(intf, intf->m_context.m_ifAddr);
			intf->m_context.m_httpPort = 0;
			intf->m_context.m_ssdpPort = 0;
			m_ifList.Add(intf);
			m_interfaceContextList.Add(&intf->m_context);
		} else {
			delete nif;
		}
	}

	if (m_ifList.GetItemCount() == 0) {
		return NPT_FAILURE;
	}

	for (NPT_Ordinal i = 0; i < m_ifList.GetItemCount(); i++) {
		Interface *intf = *m_ifList.GetItem(i);
		if (NPT_SUCCEEDED(intf->m_httpConnector->start())) {
			intf->m_context.m_httpPort = intf->m_httpConnector->port();
		}
		if (NPT_SUCCEEDED(intf->m_ssdpConnector->start())) {
			intf->m_context.m_ssdpPort = intf->m_ssdpConnector->port();
		}
	}

	m_taskGroup->reset();

	{
		ReadLocker locker1(m_cpLock);
		for (NPT_Ordinal i = 0; i < m_controlPointList.GetItemCount(); i++) {
			ControlPointInfo *info = *m_controlPointList.GetItem(i);
			info->m_controlPoint->implAttach(this, info->m_context);
		}

		for (NPT_Ordinal i = 0; i < m_deviceImplList.GetItemCount(); i++) {
			DeviceImplInfo *info = *m_deviceImplList.GetItem(i);
			info->m_deviceImpl->implAttach(this, info->m_context);
		}

		broadcastLocked(NULL, true);
	}

	m_taskGroup->startTask(new SsdpBroadcastTask(this));

	m_state = State_Running;
	return NPT_SUCCESS;
}
Example #6
0
void FrontEnd::processSsdpSearch(SsdpServerTask *task, Interface *intf, const NPT_DataBuffer& data, const NPT_SocketAddress& fromAddr)
{
	do {
		NPT_HttpRequest *req;
		NPT_InputStreamReference inputStream0(new NPT_MemoryStream(data.GetData(), data.GetDataSize()));
		NPT_BufferedInputStream inputStream(inputStream0);
		if (NPT_FAILED(NPT_HttpRequest::Parse(inputStream, NULL, req))) {
			break;
		}

		PtrHolder<NPT_HttpRequest> req1(req);
		if (req->GetMethod().Compare("M-SEARCH") != 0 || req->GetProtocol().Compare(NPT_HTTP_PROTOCOL_1_1) != 0 || req->GetUrl().GetPath().Compare("*") != 0) {
			break;
		}

		NPT_HttpHeader *hdrMan = req->GetHeaders().GetHeader("MAN");
		if (!hdrMan || hdrMan->GetValue().Compare("\"ssdp:discover\"") != 0) {
			break;
		}

		NPT_HttpHeader *hdrHost = req->GetHeaders().GetHeader("HOST");
		if (!hdrHost || (hdrHost->GetValue().Compare("239.255.255.250:1900") != 0 && hdrHost->GetValue().Compare("239.255.255.250") != 0)) {
			break;
		}

		int mx;
		NPT_HttpHeader *hdrMX = req->GetHeaders().GetHeader("MX");
		if (!hdrMX || NPT_FAILED(NPT_ParseInteger(hdrMX->GetValue(), mx)) || mx < 1) {
			break;
		}

		if (mx > 120) {
			mx = 120;
		}

		NPT_HttpHeader *hdrST = req->GetHeaders().GetHeader("ST");
		if (!hdrST) {
			break;
		}

		NPT_List<MatchContext*> matchList;

		NPT_UdpSocket sock(NPT_SOCKET_FLAG_CANCELLABLE);
		sock.Bind(NPT_SocketAddress(intf->m_context.m_ifAddr, 0));
		NPT_SharedVariable waitVar;
		waitVar.SetValue(0);

		{
			ReadLocker locker(m_dsLock);
			for (NPT_Ordinal i = 0; i < m_deviceImplList.GetItemCount(); i++) {
				NPT_List<DeviceImplInfo*>::Iterator it = m_deviceImplList.GetItem(i);
				DeviceImplInfo *info = *it;
				MatchContext *matchContext = new MatchContext();
				if (info->m_deviceImpl->match(hdrST->GetValue(), matchContext->matches)) {
					matchList.Add(matchContext);
					matchContext->deviceUuid = info->m_deviceImpl->uuid();
					matchContext->expireSeconds = info->m_deviceImpl->m_expireSeconds;
					matchContext->descPath = info->m_deviceImpl->m_descPath;
					matchContext->httpRoot = info->m_context.m_httpRoot;
				} else {
					delete matchContext;
				}
			}
		}

		SsdpSearchAbortCallback abortCallback(&sock, &waitVar);
		if (task->registerAbortCallback(&abortCallback)) {

			for (NPT_Ordinal i = 0; i < matchList.GetItemCount(); i++) {
				MatchContext *matchContext = *matchList.GetItem(i);

				NPT_String location = NPT_String::Format("http://%s:%d%s%s", intf->m_context.m_ifAddr.ToString().GetChars(), intf->m_context.m_httpPort, matchContext->httpRoot.GetChars(), matchContext->descPath.GetChars());

				bool broken = false;

				for (NPT_Ordinal j = 0; j < matchContext->matches.GetItemCount(); j++) {
					NPT_List<DeviceImplMatch>::Iterator it2 = matchContext->matches.GetItem(j);

					NPT_Timeout timeout = NPT_System::GetRandomInteger() % (mx * 1000);
					// TODO: wait or not ???
					timeout = 0;
					if (NPT_SUCCEEDED(waitVar.WaitWhileEquals(0, timeout))) {
						break;
					}

					{
						ReadLocker locker(m_dsLock);
						if (m_deviceImplIndex.HasKey(matchContext->deviceUuid)) {
							NPT_TimeStamp ts;
							NPT_System::GetCurrentTimeStamp(ts);
							NPT_String dateStr = NPT_DateTime(ts).ToString(NPT_DateTime::FORMAT_RFC_1123);
							NPT_String resp = NPT_String::Format("HTTP/1.1 200 OK\r\nCACHE-CONTROL: max-age=%d\r\nDATE: %s\r\nEXT: \r\nLOCATION: %s\r\nSERVER: %s\r\nST: %s\r\nUSN: %s\r\nCUSTOM:%s\r\n\r\n",
														matchContext->expireSeconds, dateStr.GetChars(), location.GetChars(), m_serverHeader.GetChars(), it2->m_st.GetChars(), it2->m_usn.GetChars(), m_DevName.GetChars());
							NPT_DataBuffer packet(resp.GetChars(), resp.GetLength(), false);
							sock.Send(packet, &fromAddr);
						}
					}
				}

				if (broken) {
					break;
				}
			}

			task->unregisterAbortCallback(&abortCallback);
		}

		matchList.Apply(NPT_ObjectDeleter<MatchContext>());
	} while (false);
}
Example #7
0
int SimpleDMS::onAction(const ServiceDecl *serviceDecl, const ActionDecl *actionDecl, AbortableTask *task, const FrontEnd::InterfaceContext *ifctx, const FrontEnd::RequestContext& reqCtx, const NPT_HttpRequest *req, const NPT_List<NPT_String>& inputArgNames, const NPT_List<NPT_String>& inputArgValues, NPT_List<NPT_String>& outputArgValues)
{
	if (NPT_String::Compare(serviceDecl->serviceId, "urn:upnp-org:serviceId:ContentDirectory") == 0) {

		if (NPT_String::Compare(actionDecl->name, "Browse") == 0) {
			NPT_UInt32 startingIndex;
			NPT_UInt32 requestedCount;
			NPT_ParseInteger(*inputArgValues.GetItem(3), startingIndex);
			NPT_ParseInteger(*inputArgValues.GetItem(4), requestedCount);
			NPT_String result;
			NPT_UInt32 numberReturned;
			NPT_UInt32 totalMatches;
			NPT_UInt32 updateID;
			int errorCode = m_store->browse(task, ifctx, reqCtx, NPT_String::Format("http://%s:%d%sdms/%%s", ifctx->m_ifAddr.ToString().GetChars(), ifctx->m_httpPort, frontEndContext()->m_httpRoot.GetChars()), *inputArgValues.GetItem(0), *inputArgValues.GetItem(1), *inputArgValues.GetItem(2), startingIndex, requestedCount, *inputArgValues.GetItem(5), result, numberReturned, totalMatches, updateID);
			if (errorCode == 0) {
				*outputArgValues.GetItem(0) = result;
				*outputArgValues.GetItem(1) = NPT_String::FromIntegerU(numberReturned);
				*outputArgValues.GetItem(2) = NPT_String::FromIntegerU(totalMatches);
				*outputArgValues.GetItem(3) = NPT_String::FromIntegerU(updateID);
			}
			return errorCode;
		}

		if (NPT_String::Compare(actionDecl->name, "Search") == 0) {
			NPT_UInt32 startingIndex;
			NPT_UInt32 requestedCount;
			NPT_ParseInteger(*inputArgValues.GetItem(3), startingIndex);
			NPT_ParseInteger(*inputArgValues.GetItem(4), requestedCount);
			NPT_String result;
			NPT_UInt32 numberReturned;
			NPT_UInt32 totalMatches;
			NPT_UInt32 updateID;
			int errorCode = m_store->search(task, ifctx, reqCtx, NPT_String::Format("http://%s:%d%sdms/%%s", ifctx->m_ifAddr.ToString().GetChars(), ifctx->m_httpPort, frontEndContext()->m_httpRoot.GetChars()), *inputArgValues.GetItem(0), *inputArgValues.GetItem(1), *inputArgValues.GetItem(2), startingIndex, requestedCount, *inputArgValues.GetItem(5), result, numberReturned, totalMatches, updateID);
			if (errorCode == 0) {
				*outputArgValues.GetItem(0) = result;
				*outputArgValues.GetItem(1) = NPT_String::FromIntegerU(numberReturned);
				*outputArgValues.GetItem(2) = NPT_String::FromIntegerU(totalMatches);
				*outputArgValues.GetItem(3) = NPT_String::FromIntegerU(updateID);
			}
			return errorCode;
		}

		if (NPT_String::Compare(actionDecl->name, "GetSearchCapabilities") == 0) {
			NPT_String v;
			if (getStateValue(serviceDecl->serviceId, "SearchCapabilities", v)) {
				*outputArgValues.GetItem(0) = v;
			}
			return 0;
		}

		if (NPT_String::Compare(actionDecl->name, "GetSortCapabilities") == 0) {
			NPT_String v;
			if (getStateValue(serviceDecl->serviceId, "SortCapabilities", v)) {
				*outputArgValues.GetItem(0) = v;
			}
			return 0;
		}

		if (NPT_String::Compare(actionDecl->name, "GetSystemUpdateID") == 0) {
			NPT_String v;
			if (getStateValue(serviceDecl->serviceId, "SystemUpdateID", v)) {
				*outputArgValues.GetItem(0) = v;
			}
			return 0;
		}

		return 602;
	}

	if (NPT_String::Compare(serviceDecl->serviceId, "urn:upnp-org:serviceId:ConnectionManager") == 0) {

		if (NPT_String::Compare(actionDecl->name, "GetProtocolInfo") == 0) {
			NPT_String v;
			if (getStateValue(serviceDecl->serviceId, "SourceProtocolInfo", v)) {
				*outputArgValues.GetItem(0) = v;
			}

			if (getStateValue(serviceDecl->serviceId, "SinkProtocolInfo", v)) {
				*outputArgValues.GetItem(1) = v;
			}
			return 0;
		}

		if (NPT_String::Compare(actionDecl->name, "GetCurrentConnectionIDs") == 0) {
			NPT_String v;
			if (getStateValue(serviceDecl->serviceId, "SourceProtocolInfo", v)) {
				*outputArgValues.GetItem(0) = v;
			}
			return 0;
		}

		if (NPT_String::Compare(actionDecl->name, "GetCurrentConnectionInfo") == 0) {
			*outputArgValues.GetItem(0) = "0";
			*outputArgValues.GetItem(1) = "0";
			*outputArgValues.GetItem(2) = "";
			*outputArgValues.GetItem(3) = "";
			*outputArgValues.GetItem(4) = "-1";
			*outputArgValues.GetItem(5) = "Input"; // or "Output"? WTF!
			*outputArgValues.GetItem(6) = "OK";
			return 0;
		}

		return 602;
	}

	if (NPT_String::Compare(serviceDecl->serviceId, "urn:microsoft.com:serviceId:X_MS_MediaReceiverRegistrar") == 0) {
		if (NPT_String::Compare(actionDecl->name, "IsAuthorized") == 0) {
			*outputArgValues.GetItem(0) = "1";
			return 0;
		}

		if (NPT_String::Compare(actionDecl->name, "IsValidated") == 0) {
			*outputArgValues.GetItem(0) = "1";
			return 0;
		}

		if (NPT_String::Compare(actionDecl->name, "RegisterDevice") == 0) {
			return 0;
		}
		return 602;
	}

	return 501;
}