/// <summary> /// Adds a child device to the <see cref="Devices"/> collection. /// </summary> /// <param name="device">The <see cref="SsdpEmbeddedDevice"/> instance to add.</param> /// <remarks> /// <para>If the device is already a member of the <see cref="Devices"/> collection, this method does nothing.</para> /// <para>Also sets the <see cref="SsdpEmbeddedDevice.RootDevice"/> property of the added device and all descendant devices to the relevant <see cref="SsdpRootDevice"/> instance.</para> /// </remarks> /// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="device"/> argument is null.</exception> /// <exception cref="System.InvalidOperationException">Thrown if the <paramref name="device"/> is already associated with a different <see cref="SsdpRootDevice"/> instance than used in this tree. Can occur if you try to add the same device instance to more than one tree. Also thrown if you try to add a device to itself.</exception> /// <seealso cref="DeviceAdded"/> public void AddDevice(SsdpEmbeddedDevice device) { if (device == null) { throw new ArgumentNullException("device"); } if (device.RootDevice != null && device.RootDevice != this.ToRootDevice()) { throw new InvalidOperationException("This device is already associated with a different root device (has been added as a child in another branch)."); } if (device == this) { throw new InvalidOperationException("Can't add device to itself."); } bool wasAdded = false; lock (_Devices) { if (!ChildDeviceExists(device)) { device.RootDevice = this.ToRootDevice(); _Devices.Add(device); wasAdded = true; } } if (wasAdded) { OnDeviceAdded(device); } }
private void LoadChildDevices(XmlReader reader, SsdpDevice device) { while (!reader.EOF && reader.NodeType != XmlNodeType.Element) { reader.Read(); } while (!reader.EOF) { while (!reader.EOF && reader.NodeType != XmlNodeType.Element) { reader.Read(); } if (reader.LocalName == "device") { var childDevice = new SsdpEmbeddedDevice(); LoadDeviceProperties(reader, childDevice); device.AddDevice(childDevice); } else { break; } } }
/// <summary> /// Raises the <see cref="DeviceRemoved"/> event. /// </summary> /// <param name="device">The <see cref="SsdpEmbeddedDevice"/> instance removed from the <see cref="Devices"/> collection.</param> /// <seealso cref="RemoveDevice"/> /// <see cref="DeviceRemoved"/> protected virtual void OnDeviceRemoved(SsdpEmbeddedDevice device) { var handlers = this.DeviceRemoved; if (handlers != null) { handlers(this, new DeviceEventArgs(device)); } }
/// <summary> /// Removes a child device from the <see cref="Devices"/> collection. /// </summary> /// <param name="device">The <see cref="SsdpEmbeddedDevice"/> instance to remove.</param> /// <remarks> /// <para>If the device is not a member of the <see cref="Devices"/> collection, this method does nothing.</para> /// <para>Also sets the <see cref="SsdpEmbeddedDevice.RootDevice"/> property to null for the removed device and all descendant devices.</para> /// </remarks> /// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="device"/> argument is null.</exception> /// <seealso cref="DeviceRemoved"/> public void RemoveDevice(SsdpEmbeddedDevice device) { if (device == null) { throw new ArgumentNullException("device"); } lock (_Devices) { var wasRemoved = _Devices.Remove(device); if (wasRemoved) { device.RootDevice = null; } } }
/// <summary> /// Removes a child device from the <see cref="Devices"/> collection. /// </summary> /// <param name="device">The <see cref="SsdpEmbeddedDevice"/> instance to remove.</param> /// <remarks> /// <para>If the device is not a member of the <see cref="Devices"/> collection, this method does nothing.</para> /// <para>Also sets the <see cref="SsdpEmbeddedDevice.RootDevice"/> property to null for the removed device and all descendant devices.</para> /// </remarks> /// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="device"/> argument is null.</exception> /// <seealso cref="DeviceRemoved"/> public void RemoveDevice(SsdpEmbeddedDevice device) { if (device == null) { throw new ArgumentNullException("device"); } bool wasRemoved = false; lock (_Devices) { wasRemoved = _Devices.Remove(device); } if (wasRemoved) { device.RootDevice = null; OnDeviceRemoved(device); } }
public void UPnP10DeviceValidator_FailsDeviceTypeNamespaceOverMaxLength() { var rootDevice = new SsdpRootDevice() { FriendlyName = "Basic Device 1", Manufacturer = "Test Manufacturer", ManufacturerUrl = new Uri("http://testmanufacturer.com"), ModelDescription = "A test model device", ModelName = "Test Model", ModelNumber = "Model #1234", ModelUrl = new Uri("http://modelurl.com"), SerialNumber = "SN-123", Uuid = System.Guid.NewGuid().ToString(), Location = new Uri("http://testdevice:1700/xml"), }; var testDevice = new SsdpEmbeddedDevice() { DeviceType = "TestEmbeddedDevice", FriendlyName = "Embedded Device 1", DeviceTypeNamespace = new String('A', 65), Manufacturer = "Test Manufacturer", ManufacturerUrl = new Uri("http://testmanufacturer.com"), ModelDescription = "A test model device", ModelName = "Test Model", ModelNumber = "Model #1234", ModelUrl = new Uri("http://modelurl.com"), SerialNumber = "SN-123", Uuid = System.Guid.NewGuid().ToString(), }; rootDevice.AddDevice(testDevice); var validator = new Upnp10DeviceValidator(); var results = validator.GetValidationErrors(testDevice); Assert.IsNotNull(results); Assert.IsTrue(results.First().IndexOf("DeviceTypeNamespace", StringComparison.OrdinalIgnoreCase) >= 0); }
/// <summary> /// Removes a child device from the <see cref="Devices"/> collection. /// </summary> /// <param name="device">The <see cref="SsdpEmbeddedDevice"/> instance to remove.</param> /// <remarks> /// <para>If the device is not a member of the <see cref="Devices"/> collection, this method does nothing.</para> /// <para>Also sets the <see cref="SsdpEmbeddedDevice.RootDevice"/> property to null for the removed device and all descendant devices.</para> /// </remarks> /// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="device"/> argument is null.</exception> /// <seealso cref="DeviceRemoved"/> public void RemoveDevice(SsdpEmbeddedDevice device) { if (device == null) { throw new ArgumentNullException("device"); } bool wasRemoved = false; lock (_Devices) { if (ChildDeviceExists(device)) { _Devices.Remove(device); wasRemoved = true; device.RootDevice = null; } } if (wasRemoved) { OnDeviceRemoved(device); } }
private SsdpEmbeddedDevice CreateValidEmbeddedDevice(SsdpRootDevice rootDevice) { var uuid = Guid.NewGuid().ToString(); var retVal = new SsdpEmbeddedDevice() { DeviceType = "TestEmbeddedDevice", FriendlyName = "Test Embedded Device " + uuid, Manufacturer = "Test Manufacturer", ModelName = "Test Model", Uuid = uuid }; rootDevice.AddDevice(retVal); return retVal; }
public void UPnP10DeviceValidator_FailsNegativeWidth() { var rootDevice = new SsdpRootDevice() { FriendlyName = "Basic Device 1", Manufacturer = "Test Manufacturer", ManufacturerUrl = new Uri("http://testmanufacturer.com"), ModelDescription = "A test model device", ModelName = "Test Model", ModelNumber = "Model #1234", ModelUrl = new Uri("http://modelurl.com"), SerialNumber = "SN-123", Uuid = System.Guid.NewGuid().ToString(), Location = new Uri("http://testdevice:1700/xml"), }; var testDevice = new SsdpEmbeddedDevice() { DeviceType = "TestEmbeddedDevice", FriendlyName = "Embedded Device 1", DeviceTypeNamespace = "testdevice-org", Manufacturer = "Test Manufacturer", ManufacturerUrl = new Uri("http://testmanufacturer.com"), ModelDescription = "A test model device", ModelName = "Test Model", ModelNumber = "Model #1234", ModelUrl = new Uri("http://modelurl.com"), SerialNumber = "SN-123", Uuid = System.Guid.NewGuid().ToString() }; rootDevice.AddDevice(testDevice); var icon = new SsdpDeviceIcon() { ColorDepth = 32, Width = -1, Height = 48, MimeType = "image/png", Url = new Uri("someimage.png", UriKind.Relative) }; testDevice.Icons.Add(icon); var validator = new Upnp10DeviceValidator(); var results = validator.GetValidationErrors(testDevice); Assert.IsNotNull(results); Assert.AreEqual(1, results.Count()); Assert.IsTrue(results.First().IndexOf("width", StringComparison.OrdinalIgnoreCase) >= 0); }
/// <summary> /// Adds a child device to the <see cref="Devices"/> collection. /// </summary> /// <param name="device">The <see cref="SsdpEmbeddedDevice"/> instance to add.</param> /// <remarks> /// <para>If the device is already a member of the <see cref="Devices"/> collection, this method does nothing.</para> /// <para>Also sets the <see cref="SsdpEmbeddedDevice.RootDevice"/> property of the added device and all descendant devices to the relevant <see cref="SsdpRootDevice"/> instance.</para> /// </remarks> /// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="device"/> argument is null.</exception> /// <exception cref="System.InvalidOperationException">Thrown if the <paramref name="device"/> is already associated with a different <see cref="SsdpRootDevice"/> instance than used in this tree. Can occur if you try to add the same device instance to more than one tree. Also thrown if you try to add a device to itself.</exception> /// <seealso cref="DeviceAdded"/> public void AddDevice(SsdpEmbeddedDevice device) { if (device == null) throw new ArgumentNullException("device"); if (device.RootDevice != null && device.RootDevice != this.ToRootDevice()) throw new InvalidOperationException("This device is already associated with a different root device (has been added as a child in another branch)."); if (device == this) throw new InvalidOperationException("Can't add device to itself."); bool wasAdded = false; lock (_Devices) { if (!ChildDeviceExists(device)) { device.RootDevice = this.ToRootDevice(); _Devices.Add(device); wasAdded = true; } } if (wasAdded) OnDeviceAdded(device); }
public void UPnP10DeviceValidator_FailsUpcCodeMoreThan12Chars() { var rootDevice = new SsdpRootDevice() { FriendlyName = "Basic Device 1", Manufacturer = "Test Manufacturer", ManufacturerUrl = new Uri("http://testmanufacturer.com"), ModelDescription = "A test model device", ModelName = "Test Model", ModelNumber = "Model #1234", ModelUrl = new Uri("http://modelurl.com"), SerialNumber = "SN-123", Uuid = System.Guid.NewGuid().ToString(), Location = new Uri("http://testdevice:1700/xml") }; var testDevice = new SsdpEmbeddedDevice() { DeviceType = "TestEmbeddedDevice", FriendlyName = "Embedded Device 1", Manufacturer = "Test Manufacturer", ManufacturerUrl = new Uri("http://testmanufacturer.com"), ModelDescription = "A test model device", ModelName = "Test Model", ModelNumber = "Model #1234", ModelUrl = new Uri("http://modelurl.com"), SerialNumber = "SN-123", Uuid = System.Guid.NewGuid().ToString(), Upc = "1234567890123" }; rootDevice.AddDevice(testDevice); var validator = new Upnp10DeviceValidator(); var results = validator.GetValidationErrors(testDevice); Assert.IsNotNull(results); Assert.AreEqual(1, results.Count()); Assert.IsTrue(results.First().IndexOf("UPC", StringComparison.OrdinalIgnoreCase) >= 0); }
private SsdpEmbeddedDevice CreateEmbeddedDevice(SsdpRootDevice rootDevice) { var retVal = new SsdpEmbeddedDevice() { DeviceType = "TestEmbeddedDeviceType", DeviceTypeNamespace = "test-device-ns", FriendlyName = "Test Embedded Device 1", Manufacturer = "Test Manufacturer", ManufacturerUrl = new Uri("http://testman.com"), ModelDescription = "A test embeddeddevice", ModelName = "Test Model", ModelNumber = "1234", ModelUrl = new Uri("http://testmodel.com"), PresentationUrl = new Uri("http://testmodel.com/embedded/presentation"), SerialNumber = "TM-12345", Upc = "123456789012", Uuid = Guid.NewGuid().ToString() }; rootDevice.AddDevice(retVal); return retVal; }
private void LoadChildDevices(XmlReader reader, SsdpDevice device) { while (!reader.EOF && reader.NodeType != XmlNodeType.Element) { reader.Read(); } while (!reader.EOF) { while (!reader.EOF && reader.NodeType != XmlNodeType.Element) { reader.Read(); } if (reader.LocalName == "device") { var childDevice = new SsdpEmbeddedDevice(); LoadDeviceProperties(reader, childDevice); device.AddDevice(childDevice); } else break; } }
/// <summary> /// Raises the <see cref="DeviceRemoved"/> event. /// </summary> /// <param name="device">The <see cref="SsdpEmbeddedDevice"/> instance removed from the <see cref="Devices"/> collection.</param> /// <seealso cref="RemoveDevice"/> /// <see cref="DeviceRemoved"/> protected virtual void OnDeviceRemoved(SsdpEmbeddedDevice device) { var handlers = this.DeviceRemoved; if (handlers != null) handlers(this, new DeviceEventArgs(device)); }
/// <summary> /// Removes a child device from the <see cref="Devices"/> collection. /// </summary> /// <param name="device">The <see cref="SsdpEmbeddedDevice"/> instance to remove.</param> /// <remarks> /// <para>If the device is not a member of the <see cref="Devices"/> collection, this method does nothing.</para> /// <para>Also sets the <see cref="SsdpEmbeddedDevice.RootDevice"/> property to null for the removed device and all descendant devices.</para> /// </remarks> /// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="device"/> argument is null.</exception> /// <seealso cref="DeviceRemoved"/> public void RemoveDevice(SsdpEmbeddedDevice device) { if (device == null) throw new ArgumentNullException("device"); bool wasRemoved = false; lock (_Devices) { if (ChildDeviceExists(device)) { _Devices.Remove(device); wasRemoved = true; device.RootDevice = null; } } if (wasRemoved) OnDeviceRemoved(device); }
public void UPnP10DeviceValidator_UpcCodeIsOptional() { var rootDevice = new SsdpRootDevice() { FriendlyName = "Basic Device 1", Manufacturer = "Test Manufacturer", ManufacturerUrl = new Uri("http://testmanufacturer.com"), ModelDescription = "A test model device", ModelName = "Test Model", ModelNumber = "Model #1234", ModelUrl = new Uri("http://modelurl.com"), SerialNumber = "SN-123", Uuid = System.Guid.NewGuid().ToString(), Location = new Uri("http://testdevice:1700/xml") }; var testDevice = new SsdpEmbeddedDevice() { DeviceType = "TestEmbeddedDevice", FriendlyName = "Embedded Device 1", Manufacturer = "Test Manufacturer", ManufacturerUrl = new Uri("http://testmanufacturer.com"), ModelDescription = "A test model device", ModelName = "Test Model", ModelNumber = "Model #1234", ModelUrl = new Uri("http://modelurl.com"), SerialNumber = "SN-123", Uuid = System.Guid.NewGuid().ToString(), Upc = null }; rootDevice.AddDevice(testDevice); var validator = new Upnp10DeviceValidator(); var results = validator.GetValidationErrors(testDevice); Assert.IsNotNull(results); Assert.AreEqual(0, results.Count()); }
private async Task RegisterServerEndpoints() { if (!_config.GetDlnaConfiguration().BlastAliveMessages) { return; } var cacheLength = _config.GetDlnaConfiguration().BlastAliveMessageIntervalSeconds; _Publisher.SupportPnpRootDevice = false; var addresses = (await _appHost.GetLocalIpAddresses().ConfigureAwait(false)).ToList(); foreach (var address in addresses) { //if (IPAddress.IsLoopback(address)) //{ // // Should we allow this? // continue; //} var addressString = address.ToString(); var udn = CreateUuid(addressString); var fullService = "urn:schemas-upnp-org:device:MediaServer:1"; _logger.Info("Registering publisher for {0} on {1}", fullService, addressString); var descriptorUri = "/dlna/" + udn + "/description.xml"; var uri = new Uri(_appHost.GetLocalApiUrl(address) + descriptorUri); var device = new SsdpRootDevice { CacheLifetime = TimeSpan.FromSeconds(cacheLength), //How long SSDP clients can cache this info. Location = uri, // Must point to the URL that serves your devices UPnP description document. FriendlyName = "Emby Server", Manufacturer = "Emby", ModelName = "Emby Server", Uuid = udn // This must be a globally unique value that survives reboots etc. Get from storage or embedded hardware etc. }; SetProperies(device, fullService); _Publisher.AddDevice(device); var embeddedDevices = new List<string> { "urn:schemas-upnp-org:service:ContentDirectory:1", "urn:schemas-upnp-org:service:ConnectionManager:1", "urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1" }; foreach (var subDevice in embeddedDevices) { var embeddedDevice = new SsdpEmbeddedDevice { FriendlyName = device.FriendlyName, Manufacturer = device.Manufacturer, ModelName = device.ModelName, Uuid = udn // This must be a globally unique value that survives reboots etc. Get from storage or embedded hardware etc. }; SetProperies(embeddedDevice, subDevice); device.AddDevice(embeddedDevice); } } }