/// <summary> /// Encodes a call of the specified <paramref name="action"/> with the given <paramref name="inParamValues"/> and /// returns the resulting SOAP XML string. /// </summary> /// <param name="action">Action to be called.</param> /// <param name="inParamValues">List of parameter values which must match the action's signature. /// Can be <c>null</c> if the parameter list is empty.</param> /// <param name="upnpVersion">UPnP version to use for the encoding.</param> /// <returns>XML string which contains the SOAP document.</returns> public static string EncodeCall(CpAction action, IList<object> inParamValues, UPnPVersion upnpVersion) { bool targetSupportsUPnP11 = upnpVersion.VerMin >= 1; StringBuilder result = new StringBuilder(5000); using (StringWriterWithEncoding stringWriter = new StringWriterWithEncoding(result, UPnPConsts.UTF8_NO_BOM)) using (XmlWriter writer = XmlWriter.Create(stringWriter, UPnPConfiguration.DEFAULT_XML_WRITER_SETTINGS)) { SoapHelper.WriteSoapEnvelopeStart(writer, true); writer.WriteStartElement("u", action.Name, action.ParentService.ServiceTypeVersion_URN); // Check input parameters IList<CpArgument> formalArguments = action.InArguments; if (inParamValues == null) inParamValues = EMPTY_OBJECT_LIST; if (inParamValues.Count != formalArguments.Count) throw new ArgumentException("Invalid argument count"); for (int i = 0; i < formalArguments.Count; i++) { CpArgument argument = formalArguments[i]; object value = inParamValues[i]; writer.WriteStartElement(argument.Name); argument.SoapSerializeArgument(value, !targetSupportsUPnP11, writer); writer.WriteEndElement(); // argument.Name } SoapHelper.WriteSoapEnvelopeEndAndClose(writer); } return result.ToString(); }
public static string BuildEventNotificationMessage(IEnumerable<DvStateVariable> variables, bool forceSimpleValue) { StringBuilder result = new StringBuilder(1000); using (StringWriterWithEncoding stringWriter = new StringWriterWithEncoding(result, UPnPConsts.UTF8_NO_BOM)) using (XmlWriter writer = XmlWriter.Create(stringWriter, UPnPConfiguration.DEFAULT_XML_WRITER_SETTINGS)) { writer.WriteStartDocument(); writer.WriteStartElement("e", "propertyset", UPnPConsts.NS_UPNP_EVENT); writer.WriteAttributeString("xmlns", "xsi", null, UPnPConsts.NS_XSI); foreach (DvStateVariable variable in variables) { writer.WriteStartElement("property", UPnPConsts.NS_UPNP_EVENT); writer.WriteStartElement(variable.Name); variable.DataType.SoapSerializeValue(variable.Value, forceSimpleValue, writer); writer.WriteEndElement(); // variable.Name writer.WriteEndElement(); // property } writer.WriteEndElement(); // propertyset writer.WriteEndDocument(); writer.Close(); } return result.ToString(); }
/// <summary> /// Creates the UPnP description document for this root device and all embedded devices. /// </summary> /// <param name="serverData">Current server data structure.</param> /// <param name="config">UPnP endpoint which will be used to create the endpoint specific information. The comment of /// <see cref="DeviceTreeURLGenerator"/> describes why URLs must be adapted for each UPnP endpoint.</param> /// <param name="culture">The culture to localize strings and URLs of the returned description document.</param> /// <returns>UPnP device description document for this root device and all embedded devices.</returns> public string BuildRootDeviceDescription(ServerData serverData, EndpointConfiguration config, CultureInfo culture) { StringBuilder result = new StringBuilder(10000); using (StringWriterWithEncoding stringWriter = new StringWriterWithEncoding(result, UPnPConsts.UTF8_NO_BOM)) using (XmlWriter writer = XmlWriter.Create(stringWriter, UPnPConfiguration.DEFAULT_XML_WRITER_SETTINGS)) { GenerateDescriptionDlgt dgh = DescriptionGenerateHook; writer.WriteStartDocument(); writer.WriteStartElement(string.Empty, "root", UPnPConsts.NS_DEVICE_DESCRIPTION); if (dgh != null) dgh(writer, this, GenerationPosition.RootDeviceStart, config, culture); writer.WriteAttributeString("configId", config.ConfigId.ToString()); writer.WriteStartElement("specVersion"); writer.WriteElementString("major", UPnPConsts.UPNP_VERSION_MAJOR.ToString()); writer.WriteElementString("minor", UPnPConsts.UPNP_VERSION_MINOR.ToString()); writer.WriteEndElement(); // specVersion AddDeviceDescriptionsRecursive(writer, config, culture); if (dgh != null) dgh(writer, this, GenerationPosition.RootDeviceEnd, config, culture); writer.WriteEndElement(); // root writer.Close(); } return result.ToString(); }
public static string CreateFaultDocument(uint errorCode, string errorDescription) { StringBuilder result = new StringBuilder(2000); using (StringWriterWithEncoding stringWriter = new StringWriterWithEncoding(result, UPnPConsts.UTF8_NO_BOM)) using (XmlWriter writer = XmlWriter.Create(stringWriter, UPnPConfiguration.DEFAULT_XML_WRITER_SETTINGS)) { SoapHelper.WriteSoapEnvelopeStart(writer, false); writer.WriteStartElement("Fault", UPnPConsts.NS_SOAP_ENVELOPE); string soapNamespacePrefix = writer.LookupPrefix(UPnPConsts.NS_SOAP_ENVELOPE); writer.WriteElementString("faultcode", soapNamespacePrefix + ":Client"); writer.WriteElementString("faultstring", "UPnPError"); writer.WriteStartElement("detail"); writer.WriteStartElement(string.Empty, "UPnPError", UPnPConsts.NS_UPNP_CONTROL); writer.WriteElementString("errorCode", errorCode.ToString()); writer.WriteElementString("errorDescription", errorDescription); writer.WriteEndElement(); // UPnPError writer.WriteEndElement(); // detail writer.WriteEndElement(); // s:Fault SoapHelper.WriteSoapEnvelopeEndAndClose(writer); } return result.ToString(); }
protected static string CreateResultDocument(DvAction action, IList<OutParameter> outParameters, bool forceSimpleValues) { StringBuilder result = new StringBuilder(2000); using (StringWriterWithEncoding stringWriter = new StringWriterWithEncoding(result, UPnPConsts.UTF8_NO_BOM)) using (XmlWriter writer = XmlWriter.Create(stringWriter, UPnPConfiguration.DEFAULT_XML_WRITER_SETTINGS)) { SoapHelper.WriteSoapEnvelopeStart(writer, true); writer.WriteStartElement("u", action.Name + "Response", action.ParentService.ServiceTypeVersion_URN); foreach (OutParameter parameter in outParameters) { writer.WriteStartElement(parameter.Argument.Name); parameter.Argument.SoapSerializeArgument(parameter.Value, forceSimpleValues, writer); writer.WriteEndElement(); // parameter.Argument.Name } writer.WriteEndElement(); // u:[action.Name]Response SoapHelper.WriteSoapEnvelopeEndAndClose(writer); } return result.ToString(); }
/// <summary> /// Creates the UPnP service description for this service. /// </summary> /// <param name="config">Endpoint configuration for that the SCPD document should be created.</param> /// <param name="serverData">Global server data structure.</param> /// <returns>UPnP service description document.</returns> public string BuildSCPDDocument(EndpointConfiguration config, ServerData serverData) { StringBuilder result = new StringBuilder(10000); using (StringWriterWithEncoding stringWriter = new StringWriterWithEncoding(result, UPnPConsts.UTF8_NO_BOM)) using (XmlWriter writer = XmlWriter.Create(stringWriter, UPnPConfiguration.DEFAULT_XML_WRITER_SETTINGS)) { writer.WriteStartDocument(); writer.WriteStartElement(string.Empty, "scpd", UPnPConsts.NS_SERVICE_DESCRIPTION); // Datatype schema namespaces uint ct = 0; HashSet<string> schemaURIs = new HashSet<string>(); foreach (DvStateVariable stateVariable in _stateVariables.Values) { DvDataType dataType = stateVariable.DataType; if (dataType is DvExtendedDataType) { string schemaURI = ((DvExtendedDataType) dataType).SchemaURI; if (schemaURIs.Contains(schemaURI)) continue; schemaURIs.Add(schemaURI); writer.WriteAttributeString("xmlns", "dt" + ct++, null, schemaURI); } } writer.WriteAttributeString("configId", config.ConfigId.ToString()); writer.WriteStartElement("specVersion"); writer.WriteElementString("major", UPnPConsts.UPNP_VERSION_MAJOR.ToString()); writer.WriteElementString("minor", UPnPConsts.UPNP_VERSION_MINOR.ToString()); writer.WriteEndElement(); // specVersion ICollection<DvAction> actions = _actions.Values; if (actions.Count > 0) { writer.WriteStartElement("actionList"); foreach (DvAction action in actions) action.AddSCPDDescriptionForAction(writer); writer.WriteEndElement(); // actionList } writer.WriteStartElement("serviceStateTable"); foreach (DvStateVariable stateVariable in _stateVariables.Values) stateVariable.AddSCPDDescriptionForStateVariable(writer); writer.WriteEndElement(); // serviceStateTable writer.WriteEndElement(); // scpd writer.WriteEndDocument(); writer.Close(); } return result.ToString(); }