SsdpDevice.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. else
  156. {
  157. return _Udn;
  158. }
  159. }
  160. set
  161. {
  162. _Udn = value;
  163. }
  164. }
  165. /// <summary>
  166. /// 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.
  167. /// </summary>
  168. /// <remarks><para>A short description for the end user. </para></remarks>
  169. public string FriendlyName { get; set; }
  170. /// <summary>
  171. /// Sets or returns the name of the manufacturer of this device. Required.
  172. /// </summary>
  173. public string Manufacturer { get; set; }
  174. /// <summary>
  175. /// Sets or returns a URL to the manufacturers web site. Optional.
  176. /// </summary>
  177. public Uri ManufacturerUrl { get; set; }
  178. /// <summary>
  179. /// Sets or returns a description of this device model. Recommended.
  180. /// </summary>
  181. /// <remarks><para>A long description for the end user.</para></remarks>
  182. public string ModelDescription { get; set; }
  183. /// <summary>
  184. /// Sets or returns the name of this model. Required.
  185. /// </summary>
  186. public string ModelName { get; set; }
  187. /// <summary>
  188. /// Sets or returns the number of this model. Recommended.
  189. /// </summary>
  190. public string ModelNumber { get; set; }
  191. /// <summary>
  192. /// Sets or returns a URL to a web page with details of this device model. Optional.
  193. /// </summary>
  194. /// <remarks>
  195. /// <para>Optional. May be relative to base URL.</para>
  196. /// </remarks>
  197. public Uri ModelUrl { get; set; }
  198. /// <summary>
  199. /// Sets or returns the serial number for this device. Recommended.
  200. /// </summary>
  201. public string SerialNumber { get; set; }
  202. /// <summary>
  203. /// Sets or returns the universal product code of the device, if any. Optional.
  204. /// </summary>
  205. /// <remarks>
  206. /// <para>If not blank, must be exactly 12 numeric digits.</para>
  207. /// </remarks>
  208. public string Upc { get; set; }
  209. /// <summary>
  210. /// Sets or returns the URL to a web page that can be used to configure/manager/use the device. Recommended.
  211. /// </summary>
  212. /// <remarks>
  213. /// <para>May be relative to base URL. </para>
  214. /// </remarks>
  215. public Uri PresentationUrl { get; set; }
  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. /// <summary>
  227. /// Adds a child device to the <see cref="Devices"/> collection.
  228. /// </summary>
  229. /// <param name="device">The <see cref="SsdpEmbeddedDevice"/> instance to add.</param>
  230. /// <remarks>
  231. /// <para>If the device is already a member of the <see cref="Devices"/> collection, this method does nothing.</para>
  232. /// <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>
  233. /// </remarks>
  234. /// <exception cref="ArgumentNullException">Thrown if the <paramref name="device"/> argument is null.</exception>
  235. /// <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>
  236. /// <seealso cref="DeviceAdded"/>
  237. public void AddDevice(SsdpEmbeddedDevice device)
  238. {
  239. if (device == null)
  240. {
  241. throw new ArgumentNullException(nameof(device));
  242. }
  243. if (device.RootDevice != null && device.RootDevice != this.ToRootDevice())
  244. {
  245. throw new InvalidOperationException("This device is already associated with a different root device (has been added as a child in another branch).");
  246. }
  247. if (device == this)
  248. {
  249. throw new InvalidOperationException("Can't add device to itself.");
  250. }
  251. bool wasAdded = false;
  252. lock (_Devices)
  253. {
  254. device.RootDevice = this.ToRootDevice();
  255. _Devices.Add(device);
  256. wasAdded = true;
  257. }
  258. if (wasAdded)
  259. {
  260. OnDeviceAdded(device);
  261. }
  262. }
  263. /// <summary>
  264. /// Removes a child device from the <see cref="Devices"/> collection.
  265. /// </summary>
  266. /// <param name="device">The <see cref="SsdpEmbeddedDevice"/> instance to remove.</param>
  267. /// <remarks>
  268. /// <para>If the device is not a member of the <see cref="Devices"/> collection, this method does nothing.</para>
  269. /// <para>Also sets the <see cref="SsdpEmbeddedDevice.RootDevice"/> property to null for the removed device and all descendant devices.</para>
  270. /// </remarks>
  271. /// <exception cref="ArgumentNullException">Thrown if the <paramref name="device"/> argument is null.</exception>
  272. /// <seealso cref="DeviceRemoved"/>
  273. public void RemoveDevice(SsdpEmbeddedDevice device)
  274. {
  275. if (device == null)
  276. {
  277. throw new ArgumentNullException(nameof(device));
  278. }
  279. bool wasRemoved = false;
  280. lock (_Devices)
  281. {
  282. wasRemoved = _Devices.Remove(device);
  283. if (wasRemoved)
  284. {
  285. device.RootDevice = null;
  286. }
  287. }
  288. if (wasRemoved)
  289. {
  290. OnDeviceRemoved(device);
  291. }
  292. }
  293. /// <summary>
  294. /// Raises the <see cref="DeviceAdded"/> event.
  295. /// </summary>
  296. /// <param name="device">The <see cref="SsdpEmbeddedDevice"/> instance added to the <see cref="Devices"/> collection.</param>
  297. /// <seealso cref="AddDevice"/>
  298. /// <seealso cref="DeviceAdded"/>
  299. protected virtual void OnDeviceAdded(SsdpEmbeddedDevice device)
  300. {
  301. var handlers = this.DeviceAdded;
  302. if (handlers != null)
  303. {
  304. handlers(this, new DeviceEventArgs(device));
  305. }
  306. }
  307. /// <summary>
  308. /// Raises the <see cref="DeviceRemoved"/> event.
  309. /// </summary>
  310. /// <param name="device">The <see cref="SsdpEmbeddedDevice"/> instance removed from the <see cref="Devices"/> collection.</param>
  311. /// <seealso cref="RemoveDevice"/>
  312. /// <see cref="DeviceRemoved"/>
  313. protected virtual void OnDeviceRemoved(SsdpEmbeddedDevice device)
  314. {
  315. var handlers = this.DeviceRemoved;
  316. if (handlers != null)
  317. {
  318. handlers(this, new DeviceEventArgs(device));
  319. }
  320. }
  321. }
  322. }