SsdpDevice.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Globalization;
  5. using Rssdp.Infrastructure;
  6. namespace Rssdp
  7. {
  8. /// <summary>
  9. /// Base class representing the common details of a (root or embedded) device, either to be published or that has been located.
  10. /// </summary>
  11. /// <remarks>
  12. /// <para>Do not derive new types directly from this class. New device classes should derive from either <see cref="SsdpRootDevice"/> or <see cref="SsdpEmbeddedDevice"/>.</para>
  13. /// </remarks>
  14. /// <seealso cref="SsdpRootDevice"/>
  15. /// <seealso cref="SsdpEmbeddedDevice"/>
  16. public abstract class SsdpDevice
  17. {
  18. private string _Udn;
  19. private string _DeviceType;
  20. private string _DeviceTypeNamespace;
  21. private int _DeviceVersion;
  22. private IList<SsdpDevice> _Devices;
  23. /// <summary>
  24. /// Raised when a new child device is added.
  25. /// </summary>
  26. /// <seealso cref="AddDevice"/>
  27. /// <seealso cref="DeviceAdded"/>
  28. public event EventHandler<DeviceEventArgs> DeviceAdded;
  29. /// <summary>
  30. /// Raised when a child device is removed.
  31. /// </summary>
  32. /// <seealso cref="RemoveDevice"/>
  33. /// <seealso cref="DeviceRemoved"/>
  34. public event EventHandler<DeviceEventArgs> DeviceRemoved;
  35. /// <summary>
  36. /// Derived type constructor, allows constructing a device with no parent. Should only be used from derived types that are or inherit from <see cref="SsdpRootDevice"/>.
  37. /// </summary>
  38. protected SsdpDevice()
  39. {
  40. _DeviceTypeNamespace = SsdpConstants.UpnpDeviceTypeNamespace;
  41. _DeviceType = SsdpConstants.UpnpDeviceTypeBasicDevice;
  42. _DeviceVersion = 1;
  43. _Devices = new List<SsdpDevice>();
  44. this.Devices = new ReadOnlyCollection<SsdpDevice>(_Devices);
  45. }
  46. public SsdpRootDevice ToRootDevice()
  47. {
  48. var device = this;
  49. var rootDevice = device as SsdpRootDevice;
  50. if (rootDevice == null)
  51. {
  52. rootDevice = ((SsdpEmbeddedDevice)device).RootDevice;
  53. }
  54. return rootDevice;
  55. }
  56. /// <summary>
  57. /// Sets or returns the core device type (not including namespace, version etc.). Required.
  58. /// </summary>
  59. /// <remarks><para>Defaults to the UPnP basic device type.</para></remarks>
  60. /// <seealso cref="DeviceTypeNamespace"/>
  61. /// <seealso cref="DeviceVersion"/>
  62. /// <seealso cref="FullDeviceType"/>
  63. public string DeviceType
  64. {
  65. get
  66. {
  67. return _DeviceType;
  68. }
  69. set
  70. {
  71. _DeviceType = value;
  72. }
  73. }
  74. public string DeviceClass { get; set; }
  75. /// <summary>
  76. /// Sets or returns the namespace for the <see cref="DeviceType"/> of this device. Optional, but defaults to UPnP schema so should be changed if <see cref="DeviceType"/> is not a UPnP device type.
  77. /// </summary>
  78. /// <remarks><para>Defaults to the UPnP standard namespace.</para></remarks>
  79. /// <seealso cref="DeviceType"/>
  80. /// <seealso cref="DeviceVersion"/>
  81. /// <seealso cref="FullDeviceType"/>
  82. public string DeviceTypeNamespace
  83. {
  84. get
  85. {
  86. return _DeviceTypeNamespace;
  87. }
  88. set
  89. {
  90. _DeviceTypeNamespace = value;
  91. }
  92. }
  93. /// <summary>
  94. /// Sets or returns the version of the device type. Optional, defaults to 1.
  95. /// </summary>
  96. /// <remarks><para>Defaults to a value of 1.</para></remarks>
  97. /// <seealso cref="DeviceType"/>
  98. /// <seealso cref="DeviceTypeNamespace"/>
  99. /// <seealso cref="FullDeviceType"/>
  100. public int DeviceVersion
  101. {
  102. get
  103. {
  104. return _DeviceVersion;
  105. }
  106. set
  107. {
  108. _DeviceVersion = value;
  109. }
  110. }
  111. /// <summary>
  112. /// Returns the full device type string.
  113. /// </summary>
  114. /// <remarks>
  115. /// <para>The format used is urn:<see cref="DeviceTypeNamespace"/>:device:<see cref="DeviceType"/>:<see cref="DeviceVersion"/></para>
  116. /// </remarks>
  117. public string FullDeviceType
  118. {
  119. get
  120. {
  121. return String.Format(
  122. CultureInfo.InvariantCulture,
  123. "urn:{0}:{3}:{1}:{2}",
  124. this.DeviceTypeNamespace ?? String.Empty,
  125. this.DeviceType ?? String.Empty,
  126. this.DeviceVersion,
  127. this.DeviceClass ?? "device");
  128. }
  129. }
  130. /// <summary>
  131. /// Sets or returns the universally unique identifier for this device (without the uuid: prefix). Required.
  132. /// </summary>
  133. /// <remarks>
  134. /// <para>Must be the same over time for a specific device instance (i.e. must survive reboots).</para>
  135. /// <para>For UPnP 1.0 this can be any unique string. For UPnP 1.1 this should be a 128 bit number formatted in a specific way, preferably generated using the time and MAC based algorithm. See section 1.1.4 of http://upnp.org/specs/arch/UPnP-arch-DeviceArchitecture-v1.1.pdf for details.</para>
  136. /// <para>Technically this library implements UPnP 1.0, so any value is allowed, but we advise using UPnP 1.1 compatible values for good behaviour and forward compatibility with future versions.</para>
  137. /// </remarks>
  138. public string Uuid { get; set; }
  139. /// <summary>
  140. /// Returns (or sets*) a unique device name for this device. Optional, not recommended to be explicitly set.
  141. /// </summary>
  142. /// <remarks>
  143. /// <para>* In general you should not explicitly set this property. If it is not set (or set to null/empty string) the property will return a UDN value that is correct as per the UPnP specification, based on the other device properties.</para>
  144. /// <para>The setter is provided to allow for devices that do not correctly follow the specification (when we discover them), rather than to intentionally deviate from the specification.</para>
  145. /// <para>If a value is explicitly set, it is used verbatim, and so any prefix (such as uuid:) must be provided in the value.</para>
  146. /// </remarks>
  147. public string Udn
  148. {
  149. get
  150. {
  151. if (String.IsNullOrEmpty(_Udn) && !String.IsNullOrEmpty(this.Uuid))
  152. {
  153. return "uuid:" + this.Uuid;
  154. }
  155. return _Udn;
  156. }
  157. set
  158. {
  159. _Udn = value;
  160. }
  161. }
  162. /// <summary>
  163. /// Sets or returns a friendly/display name for this device on the network. Something the user can identify the device/instance by, i.e Lounge Main Light. Required.
  164. /// </summary>
  165. /// <remarks><para>A short description for the end user. </para></remarks>
  166. public string FriendlyName { get; set; }
  167. /// <summary>
  168. /// Sets or returns the name of the manufacturer of this device. Required.
  169. /// </summary>
  170. public string Manufacturer { get; set; }
  171. /// <summary>
  172. /// Sets or returns a URL to the manufacturers web site. Optional.
  173. /// </summary>
  174. public Uri ManufacturerUrl { get; set; }
  175. /// <summary>
  176. /// Sets or returns a description of this device model. Recommended.
  177. /// </summary>
  178. /// <remarks><para>A long description for the end user.</para></remarks>
  179. public string ModelDescription { get; set; }
  180. /// <summary>
  181. /// Sets or returns the name of this model. Required.
  182. /// </summary>
  183. public string ModelName { get; set; }
  184. /// <summary>
  185. /// Sets or returns the number of this model. Recommended.
  186. /// </summary>
  187. public string ModelNumber { get; set; }
  188. /// <summary>
  189. /// Sets or returns a URL to a web page with details of this device model. Optional.
  190. /// </summary>
  191. /// <remarks>
  192. /// <para>Optional. May be relative to base URL.</para>
  193. /// </remarks>
  194. public Uri ModelUrl { get; set; }
  195. /// <summary>
  196. /// Sets or returns the serial number for this device. Recommended.
  197. /// </summary>
  198. public string SerialNumber { get; set; }
  199. /// <summary>
  200. /// Sets or returns the universal product code of the device, if any. Optional.
  201. /// </summary>
  202. /// <remarks>
  203. /// <para>If not blank, must be exactly 12 numeric digits.</para>
  204. /// </remarks>
  205. public string Upc { get; set; }
  206. /// <summary>
  207. /// Sets or returns the URL to a web page that can be used to configure/manager/use the device. Recommended.
  208. /// </summary>
  209. /// <remarks>
  210. /// <para>May be relative to base URL. </para>
  211. /// </remarks>
  212. public Uri PresentationUrl { get; set; }
  213. /// <summary>
  214. /// Returns a read-only enumerable set of <see cref="SsdpDevice"/> objects representing children of this device. Child devices are optional.
  215. /// </summary>
  216. /// <seealso cref="AddDevice"/>
  217. /// <seealso cref="RemoveDevice"/>
  218. public IList<SsdpDevice> Devices
  219. {
  220. get;
  221. private set;
  222. }
  223. /// <summary>
  224. /// Adds a child device to the <see cref="Devices"/> collection.
  225. /// </summary>
  226. /// <param name="device">The <see cref="SsdpEmbeddedDevice"/> instance to add.</param>
  227. /// <remarks>
  228. /// <para>If the device is already a member of the <see cref="Devices"/> collection, this method does nothing.</para>
  229. /// <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>
  230. /// </remarks>
  231. /// <exception cref="ArgumentNullException">Thrown if the <paramref name="device"/> argument is null.</exception>
  232. /// <exception cref="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>
  233. /// <seealso cref="DeviceAdded"/>
  234. public void AddDevice(SsdpEmbeddedDevice device)
  235. {
  236. if (device == null)
  237. {
  238. throw new ArgumentNullException(nameof(device));
  239. }
  240. if (device.RootDevice != null && device.RootDevice != this.ToRootDevice())
  241. {
  242. throw new InvalidOperationException("This device is already associated with a different root device (has been added as a child in another branch).");
  243. }
  244. if (device == this)
  245. {
  246. throw new InvalidOperationException("Can't add device to itself.");
  247. }
  248. bool wasAdded = false;
  249. lock (_Devices)
  250. {
  251. device.RootDevice = this.ToRootDevice();
  252. _Devices.Add(device);
  253. wasAdded = true;
  254. }
  255. if (wasAdded)
  256. {
  257. OnDeviceAdded(device);
  258. }
  259. }
  260. /// <summary>
  261. /// Removes a child device from the <see cref="Devices"/> collection.
  262. /// </summary>
  263. /// <param name="device">The <see cref="SsdpEmbeddedDevice"/> instance to remove.</param>
  264. /// <remarks>
  265. /// <para>If the device is not a member of the <see cref="Devices"/> collection, this method does nothing.</para>
  266. /// <para>Also sets the <see cref="SsdpEmbeddedDevice.RootDevice"/> property to null for the removed device and all descendant devices.</para>
  267. /// </remarks>
  268. /// <exception cref="ArgumentNullException">Thrown if the <paramref name="device"/> argument is null.</exception>
  269. /// <seealso cref="DeviceRemoved"/>
  270. public void RemoveDevice(SsdpEmbeddedDevice device)
  271. {
  272. if (device == null)
  273. {
  274. throw new ArgumentNullException(nameof(device));
  275. }
  276. bool wasRemoved = false;
  277. lock (_Devices)
  278. {
  279. wasRemoved = _Devices.Remove(device);
  280. if (wasRemoved)
  281. {
  282. device.RootDevice = null;
  283. }
  284. }
  285. if (wasRemoved)
  286. {
  287. OnDeviceRemoved(device);
  288. }
  289. }
  290. /// <summary>
  291. /// Raises the <see cref="DeviceAdded"/> event.
  292. /// </summary>
  293. /// <param name="device">The <see cref="SsdpEmbeddedDevice"/> instance added to the <see cref="Devices"/> collection.</param>
  294. /// <seealso cref="AddDevice"/>
  295. /// <seealso cref="DeviceAdded"/>
  296. protected virtual void OnDeviceAdded(SsdpEmbeddedDevice device)
  297. {
  298. var handlers = this.DeviceAdded;
  299. if (handlers != null)
  300. {
  301. handlers(this, new DeviceEventArgs(device));
  302. }
  303. }
  304. /// <summary>
  305. /// Raises the <see cref="DeviceRemoved"/> event.
  306. /// </summary>
  307. /// <param name="device">The <see cref="SsdpEmbeddedDevice"/> instance removed from the <see cref="Devices"/> collection.</param>
  308. /// <seealso cref="RemoveDevice"/>
  309. /// <see cref="DeviceRemoved"/>
  310. protected virtual void OnDeviceRemoved(SsdpEmbeddedDevice device)
  311. {
  312. var handlers = this.DeviceRemoved;
  313. if (handlers != null)
  314. {
  315. handlers(this, new DeviceEventArgs(device));
  316. }
  317. }
  318. }
  319. }