SsdpDevice.cs 14 KB

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