Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

266 lines
8.7 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. // File: P H Y S I C A L D E V I C E I N F O . C P P
  7. //
  8. // Contents: Manages an UPnP device assembly
  9. //
  10. // Notes:
  11. //
  12. // Author: mbend 12 Sep 2000
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include "uhbase.h"
  18. #include "PhysicalDeviceInfo.h"
  19. #include "uhutil.h"
  20. CPhysicalDeviceInfo::CPhysicalDeviceInfo()
  21. {
  22. m_bRunning = FALSE;
  23. }
  24. CPhysicalDeviceInfo::~CPhysicalDeviceInfo()
  25. {
  26. Clear();
  27. }
  28. HRESULT CPhysicalDeviceInfo::HrInitialize(
  29. const wchar_t * szProgIdDeviceControlClass,
  30. const wchar_t * szInitString,
  31. const wchar_t * szContainerId,
  32. long nUDNs,
  33. wchar_t * arszUDNs[])
  34. {
  35. CHECK_POINTER(szProgIdDeviceControlClass);
  36. CHECK_POINTER(szInitString);
  37. CHECK_POINTER(szContainerId);
  38. TraceTag(ttidRegistrar, "CPhysicalDeviceInfo::HrInitialize(ProgId=%S)", szProgIdDeviceControlClass);
  39. HRESULT hr = S_OK;
  40. hr = m_strProgIdDeviceControl.HrAssign(szProgIdDeviceControlClass);
  41. if(SUCCEEDED(hr))
  42. {
  43. hr = m_strInitString.HrAssign(szInitString);
  44. if(SUCCEEDED(hr))
  45. {
  46. hr = m_strContainerId.HrAssign(szContainerId);
  47. if(SUCCEEDED(hr))
  48. {
  49. for(long n = 0; n < nUDNs && SUCCEEDED(hr); ++n)
  50. {
  51. CDeviceInfo deviceInfo;
  52. CUString strUDN;
  53. hr = strUDN.HrAssign(arszUDNs[n]);
  54. if(SUCCEEDED(hr))
  55. {
  56. hr = m_deviceTable.HrInsertTransfer(strUDN, deviceInfo);
  57. }
  58. }
  59. }
  60. }
  61. }
  62. TraceHr(ttidRegistrar, FAL, hr, FALSE, "CPhysicalDeviceInfo::HrInitialize");
  63. return hr;
  64. }
  65. HRESULT CPhysicalDeviceInfo::HrInitializeRunning(
  66. const PhysicalDeviceIdentifier & pdi,
  67. IUnknown * pUnkDeviceControl,
  68. const wchar_t * szInitString,
  69. long nUDNs,
  70. wchar_t * arszUDNs[])
  71. {
  72. CHECK_POINTER(pUnkDeviceControl);
  73. CHECK_POINTER(szInitString);
  74. TraceTag(ttidRegistrar, "CPhysicalDeviceInfo::HrInitializeRunning");
  75. HRESULT hr = S_OK;
  76. m_bRunning = TRUE;
  77. hr = m_strInitString.HrAssign(szInitString);
  78. if(SUCCEEDED(hr))
  79. {
  80. HrEnableStaticCloaking(pUnkDeviceControl);
  81. hr = m_pDeviceControl.HrAttach(pUnkDeviceControl);
  82. if(SUCCEEDED(hr))
  83. {
  84. hr = m_pDeviceControl.HrEnableStaticCloaking();
  85. }
  86. if(SUCCEEDED(hr))
  87. {
  88. CUString strPdi;
  89. hr = HrPhysicalDeviceIdentifierToString(pdi, strPdi);
  90. if(SUCCEEDED(hr))
  91. {
  92. // Convert to BSTRs
  93. BSTR bstrPdi = NULL;
  94. hr = strPdi.HrGetBSTR(&bstrPdi);
  95. if(SUCCEEDED(hr))
  96. {
  97. BSTR bstrInitString = NULL;
  98. hr = m_strInitString.HrGetBSTR(&bstrInitString);
  99. if(SUCCEEDED(hr))
  100. {
  101. // Get description text
  102. BSTR bstrDescriptionDocument = NULL;
  103. IUPnPRegistrarPrivatePtr pPriv;
  104. hr = pPriv.HrCreateInstanceInproc(CLSID_UPnPRegistrar);
  105. if(SUCCEEDED(hr))
  106. {
  107. hr = pPriv->GetDescriptionText(pdi, &bstrDescriptionDocument);
  108. if(SUCCEEDED(hr))
  109. {
  110. hr = m_pDeviceControl->Initialize(
  111. bstrDescriptionDocument, bstrPdi, bstrInitString);
  112. SysFreeString(bstrDescriptionDocument);
  113. TraceHr(ttidRegistrar, FAL, hr, FALSE, "CPhysicalDeviceInfo::HrInitializeRunning - IUPnPDeviceControl::Initialize failed!");
  114. }
  115. }
  116. SysFreeString(bstrInitString);
  117. }
  118. SysFreeString(bstrPdi);
  119. }
  120. }
  121. for(long n = 0; n < nUDNs && SUCCEEDED(hr); ++n)
  122. {
  123. CDeviceInfo deviceInfo;
  124. CUString strUDN;
  125. hr = strUDN.HrAssign(arszUDNs[n]);
  126. if(SUCCEEDED(hr))
  127. {
  128. hr = m_deviceTable.HrInsertTransfer(strUDN, deviceInfo);
  129. }
  130. }
  131. }
  132. }
  133. if(FAILED(hr) && m_pDeviceControl.GetRawPointer())
  134. {
  135. m_pDeviceControl.Release();
  136. HrDereferenceContainer(m_strContainerId);
  137. }
  138. TraceHr(ttidRegistrar, FAL, hr, FALSE, "CPhysicalDeviceInfo::HrInitializeRunning");
  139. return hr;
  140. }
  141. HRESULT CPhysicalDeviceInfo::HrGetService(
  142. const PhysicalDeviceIdentifier & pdi,
  143. const wchar_t * szUDN,
  144. const wchar_t * szServiceId,
  145. CServiceInfo ** ppServiceInfo)
  146. {
  147. CHECK_POINTER(szUDN);
  148. CHECK_POINTER(szServiceId);
  149. CHECK_POINTER(ppServiceInfo);
  150. TraceTag(ttidRegistrar, "CPhysicalDeviceInfo::HrGetService(UDN=%S, ServiceId=%S)", szUDN, szServiceId);
  151. HRESULT hr = S_OK;
  152. // See if our device control object is running, create it if not
  153. if(!m_pDeviceControl)
  154. {
  155. // Create the device control object
  156. hr = HrCreateAndReferenceContainedObjectByProgId(m_strContainerId, m_strProgIdDeviceControl, SMART_QI(m_pDeviceControl));
  157. if(SUCCEEDED(hr))
  158. {
  159. CUString strPdi;
  160. hr = HrPhysicalDeviceIdentifierToString(pdi, strPdi);
  161. if(SUCCEEDED(hr))
  162. {
  163. // Convert to BSTRs
  164. BSTR bstrPdi = NULL;
  165. hr = strPdi.HrGetBSTR(&bstrPdi);
  166. if(SUCCEEDED(hr))
  167. {
  168. BSTR bstrInitString = NULL;
  169. hr = m_strInitString.HrGetBSTR(&bstrInitString);
  170. if(SUCCEEDED(hr))
  171. {
  172. // Get description text
  173. BSTR bstrDescriptionDocument = NULL;
  174. IUPnPRegistrarPrivatePtr pPriv;
  175. hr = pPriv.HrCreateInstanceInproc(CLSID_UPnPRegistrar);
  176. if(SUCCEEDED(hr))
  177. {
  178. hr = pPriv->GetDescriptionText(pdi, &bstrDescriptionDocument);
  179. if(SUCCEEDED(hr))
  180. {
  181. hr = m_pDeviceControl->Initialize(
  182. bstrDescriptionDocument, bstrPdi, bstrInitString);
  183. SysFreeString(bstrDescriptionDocument);
  184. }
  185. }
  186. SysFreeString(bstrInitString);
  187. }
  188. SysFreeString(bstrPdi);
  189. }
  190. }
  191. }
  192. }
  193. // Now fetch service from device object
  194. if(SUCCEEDED(hr))
  195. {
  196. CUString strUDN;
  197. hr = strUDN.HrAssign(szUDN);
  198. if(SUCCEEDED(hr))
  199. {
  200. CDeviceInfo * pDeviceInfo = m_deviceTable.Lookup(strUDN);
  201. if(pDeviceInfo)
  202. {
  203. hr = pDeviceInfo->HrGetService(pdi, szUDN, szServiceId, m_strContainerId, m_pDeviceControl, ppServiceInfo, m_bRunning);
  204. }
  205. else
  206. {
  207. hr = E_INVALIDARG;
  208. TraceTag(ttidRegistrar, "CPhysicalDeviceInfo::HrGetService - Unable to find UDN(%S)",
  209. static_cast<const wchar_t *>(strUDN));
  210. }
  211. }
  212. }
  213. if(FAILED(hr) && m_pDeviceControl.GetRawPointer())
  214. {
  215. m_pDeviceControl.Release();
  216. HrDereferenceContainer(m_strContainerId);
  217. }
  218. TraceHr(ttidRegistrar, FAL, hr, FALSE, "CPhysicalDeviceInfo::HrGetService");
  219. return hr;
  220. }
  221. void CPhysicalDeviceInfo::Transfer(CPhysicalDeviceInfo & ref)
  222. {
  223. // If we have a device object and a container id then deref container
  224. if(m_pDeviceControl.GetRawPointer() && m_strContainerId.GetLength())
  225. {
  226. HrDereferenceContainer(m_strContainerId);
  227. }
  228. m_deviceTable.Swap(ref.m_deviceTable);
  229. m_strProgIdDeviceControl.Transfer(ref.m_strProgIdDeviceControl);
  230. m_strInitString.Transfer(ref.m_strInitString);
  231. m_strContainerId.Transfer(ref.m_strContainerId);
  232. m_pDeviceControl.Swap(ref.m_pDeviceControl);
  233. }
  234. void CPhysicalDeviceInfo::Clear()
  235. {
  236. // If we have a device object and a container id then deref container
  237. if(m_pDeviceControl.GetRawPointer() && m_strContainerId.GetLength())
  238. {
  239. HrDereferenceContainer(m_strContainerId);
  240. }
  241. m_deviceTable.Clear();
  242. m_strProgIdDeviceControl.Clear();
  243. m_strInitString.Clear();
  244. m_strContainerId.Clear();
  245. m_pDeviceControl.Release();
  246. }