Esempio n. 1
0
        public void DiscoveredDevice_GetDeviceInfo_DoesNotMakeHttpRequestIfDataCached()
        {
            var publishedDevice = new SsdpRootDevice()
            {
                Location = new Uri("http://192.168.1.100:1702/description"),
                CacheLifetime = TimeSpan.FromMinutes(1),
                DeviceType = "TestDeviceType",
                Uuid = System.Guid.NewGuid().ToString()
            };

            var discoveredDevice = new DiscoveredSsdpDevice();
            discoveredDevice.Usn = "test usn";
            discoveredDevice.AsAt = DateTimeOffset.Now;
            discoveredDevice.CacheLifetime = publishedDevice.CacheLifetime;
            discoveredDevice.DescriptionLocation = publishedDevice.Location;

            var client = new MockHttpClient(publishedDevice.ToDescriptionDocument());
            var device = discoveredDevice.GetDeviceInfo(client).GetAwaiter().GetResult();

            client = new MockHttpClient(publishedDevice.ToDescriptionDocument());
            device = discoveredDevice.GetDeviceInfo(client).GetAwaiter().GetResult();

            Assert.IsNull(client.LastRequest);
            Assert.AreEqual(device.Uuid, publishedDevice.Uuid);
            Assert.AreEqual(device.DeviceType, publishedDevice.DeviceType);
        }
Esempio n. 2
0
        /// <summary>
        /// Retrieves the device description document specified by the <see cref="DescriptionLocation"/> property using the provided <see cref="System.Net.Http.HttpClient"/> instance.
        /// </summary>
        /// <remarks>
        /// <para>This method may choose to cache (or return cached) information if called multiple times within the <see cref="CacheLifetime"/> period.</para>
        /// <para>This method performs no error handling, if an exception occurs downloading or parsing the document it will be thrown to the calling code. Ensure you setup correct error handling for these scenarios.</para>
        /// </remarks>
        /// <param name="downloadHttpClient">A <see cref="System.Net.Http.HttpClient"/> to use when downloading the document data.</param>
        /// <exception cref="System.Exception">This method using an HttpClient instance to retrieve the device description document, and as such any exception that can be thrown by HttpClient may be rethrown by this method.
        /// On the UWP platform this is likely to be a <see cref="System.Exception"/> instance and the hresult can be checked to determine the exact nature of the error. On other platforms it is likely to be a System.Net.WebException or System.Net.Http.HttpRequestException.
        /// Check the documentation for HttpClient on the platform(s) you're using.</exception>
        /// <returns>An <see cref="SsdpDevice"/> instance describing the full device details.</returns>
        public async Task <SsdpRootDevice> GetDeviceInfo(HttpClient downloadHttpClient)
        {
            if (_Device == null || this.IsExpired())
            {
                var rawDescriptionDocument = await downloadHttpClient.GetAsync(this.DescriptionLocation);

                rawDescriptionDocument.EnsureSuccessStatusCode();

                // Not using ReadAsStringAsync() here as some devices return the content type as utf-8 not UTF-8,
                // which causes an (unneccesary) exception.
                var data = await rawDescriptionDocument.Content.ReadAsByteArrayAsync();

                _Device = new SsdpRootDevice(this.DescriptionLocation, this.CacheLifetime, System.Text.UTF8Encoding.UTF8.GetString(data, 0, data.Length));
            }

            return(_Device);
        }
Esempio n. 3
0
        public void DiscoveredDevice_GetDeviceInfo_CreatesDefaultClient()
        {
            var publishedDevice = new SsdpRootDevice()
            {
                Location = new Uri("http://192.168.1.100:1702/description"),
                CacheLifetime = TimeSpan.FromMinutes(1),
                DeviceType = "TestDeviceType",
                Uuid = System.Guid.NewGuid().ToString()
            };

            var discoveredDevice = new DiscoveredSsdpDevice();
            discoveredDevice.Usn = "test usn";
            discoveredDevice.AsAt = DateTimeOffset.Now;
            discoveredDevice.CacheLifetime = TimeSpan.FromSeconds(1);
            discoveredDevice.DescriptionLocation = publishedDevice.Location;

            var device = discoveredDevice.GetDeviceInfo().GetAwaiter().GetResult();
        }
Esempio n. 4
0
        public void Publisher_AddDeviceThrowsWhenDisposed()
        {
            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 server = new MockCommsServer();
            var publisher = new TestDevicePublisher(server);
            publisher.Dispose();
            publisher.AddDevice(rootDevice);
        }
Esempio n. 5
0
        public void UPnP10DeviceValidator_FailsDeviceTypeNamespaceWithPeriods()
        {
            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 validator = new Upnp10DeviceValidator();
            var results = validator.GetValidationErrors(testDevice);
            Assert.IsNotNull(results);
            Assert.IsTrue(results.First().IndexOf("DeviceTypeNamespace", StringComparison.OrdinalIgnoreCase) >= 0);
        }
Esempio n. 6
0
 private static SsdpRootDevice CreateValidRootDevice()
 {
     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"),
         CacheLifetime = TimeSpan.FromMinutes(30)
     };
     return rootDevice;
 }
Esempio n. 7
0
        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;
        }
Esempio n. 8
0
		/// <summary>
		/// Retrieves the device description document specified by the <see cref="DescriptionLocation"/> property using the provided <see cref="System.Net.Http.HttpClient"/> instance.
		/// </summary>
		/// <remarks>
		/// <para>This method may choose to cache (or return cached) information if called multiple times within the <see cref="CacheLifetime"/> period.</para>
		/// <para>This method performs no error handling, if an exception occurs downloading or parsing the document it will be thrown to the calling code. Ensure you setup correct error handling for these scenarios.</para>
		/// </remarks>
		/// <param name="downloadHttpClient">A <see cref="System.Net.Http.HttpClient"/> to use when downloading the document data.</param>
		/// <returns>An <see cref="SsdpDevice"/> instance describing the full device details.</returns>
		public async Task<SsdpRootDevice> GetDeviceInfo(HttpClient downloadHttpClient)
		{
			if (_Device == null || this.IsExpired())
			{
				var rawDescriptionDocument = await downloadHttpClient.GetAsync(this.DescriptionLocation);
				rawDescriptionDocument.EnsureSuccessStatusCode();

				// Not using ReadAsStringAsync() here as some devices return the content type as utf-8 not UTF-8,
				// which causes an (unneccesary) exception.
				var data = await rawDescriptionDocument.Content.ReadAsByteArrayAsync();
				_Device = new SsdpRootDevice(this.DescriptionLocation, this.CacheLifetime, System.Text.UTF8Encoding.UTF8.GetString(data, 0, data.Length));
			}

			return _Device;
		}
Esempio n. 9
0
        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);
        }
Esempio n. 10
0
        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());
        }
Esempio n. 11
0
        public void UPnP10DeviceValidator_ThrowIfInvalidThrowsOnValidationError()
        {
            var testDevice = 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 = null,
                Location = new Uri("http://testdevice:1700/xml")
            };

            var validator = new Upnp10DeviceValidator();
            validator.ThrowIfDeviceInvalid(testDevice);
        }
Esempio n. 12
0
        public void UPnP10DeviceValidator_RootDeviceRequiresLocation()
        {
            var testDevice = 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()
            };

            var validator = new Upnp10DeviceValidator();
            var results = validator.GetValidationErrors(testDevice);
            Assert.IsNotNull(results);
            Assert.AreEqual(1, results.Count());
            Assert.IsTrue(results.First().IndexOf("location", StringComparison.OrdinalIgnoreCase) >= 0);
        }
Esempio n. 13
0
        private SsdpRootDevice CreateSampleRootDevice()
        {
            var retVal = new SsdpRootDevice()
            {
                CacheLifetime = TimeSpan.FromMinutes(30),
                DeviceType = "TestDeviceType",
                DeviceTypeNamespace = "test-device-ns",
                FriendlyName = "Test Device 1",
                Location = new Uri("http://testdevice:1700/xml"),
                Manufacturer = "Test Manufacturer",
                ManufacturerUrl = new Uri("http://testman.com"),
                ModelDescription = "A test device",
                ModelName = "Test Model",
                ModelNumber = "1234",
                ModelUrl = new Uri("http://testmodel.com"),
                PresentationUrl = new Uri("http://testmodel.com/presentation"),
                SerialNumber = "TM-12345",
                Upc = "123456789012",
                UrlBase = new Uri("http://testdevice:1700"),
                Uuid = Guid.NewGuid().ToString()
            };

            var customProp = new SsdpDeviceProperty() { Namespace = "custom-ns", Name = "TestProp1", Value = "Test" };
            retVal.CustomProperties.Add(customProp);
            customProp = new SsdpDeviceProperty() { Namespace = "custom-ns", Name = "TestProp2", Value = "Test" };
            retVal.CustomProperties.Add(customProp);

            var icon = new SsdpDeviceIcon() { ColorDepth = 32, Height = 48, Width = 48, MimeType = "image/png", Url = new Uri("icons/48", UriKind.Relative) };
            retVal.Icons.Add(icon);
            icon = new SsdpDeviceIcon() { ColorDepth = 32, Height = 120, Width = 120, MimeType = "image/png", Url = new Uri("icons/120", UriKind.Relative) };
            retVal.Icons.Add(icon);

            return retVal;
        }
Esempio n. 14
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;
        }
Esempio n. 15
0
        public void Publisher_Constructor_ThrowsOnNullOSVersion()
        {
            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 server = new MockCommsServer();
            var publisher = new TestDevicePublisher(server, "TestOS", null);
        }
Esempio n. 16
0
        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);
                }
            }
        }