SsdpDevice.cs 14 KB

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