Leaked source code of windows server 2003
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.

430 lines
12 KiB

  1. //***************************************************************************
  2. //
  3. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  4. //
  5. // USBHub.cpp
  6. //
  7. // Purpose: USB Hub property set provider
  8. //
  9. //***************************************************************************
  10. #include "precomp.h"
  11. #include "LPVParams.h"
  12. #include <FRQueryEx.h>
  13. #include <ProvExce.h>
  14. #include "USBHub.h"
  15. // Property set declaration
  16. //=========================
  17. CWin32USBHub MyUSBHub( PROPSET_NAME_USBHUB, IDS_CimWin32Namespace );
  18. /*****************************************************************************
  19. *
  20. * FUNCTION : CWin32USBHub::CWin32USBHub
  21. *
  22. * DESCRIPTION : Constructor
  23. *
  24. * INPUTS : const CHString& strName - Name of the class.
  25. *
  26. * OUTPUTS : none
  27. *
  28. * RETURNS : nothing
  29. *
  30. * COMMENTS : Registers property set with framework
  31. *
  32. *****************************************************************************/
  33. CWin32USBHub::CWin32USBHub
  34. (
  35. const CHString &a_strName,
  36. LPCWSTR a_pszNamespace
  37. )
  38. : Provider( a_strName, a_pszNamespace )
  39. {
  40. m_ptrProperties.SetSize(11);
  41. m_ptrProperties[0] = ( (LPVOID) IDS_ConfigManagerErrorCode );
  42. m_ptrProperties[1] = ( (LPVOID) IDS_ConfigManagerUserConfig );
  43. m_ptrProperties[2] = ( (LPVOID) IDS_Status);
  44. m_ptrProperties[3] = ( (LPVOID) IDS_PNPDeviceID);
  45. m_ptrProperties[4] = ( (LPVOID) IDS_DeviceID);
  46. m_ptrProperties[5] = ( (LPVOID) IDS_SystemCreationClassName);
  47. m_ptrProperties[6] = ( (LPVOID) IDS_SystemName);
  48. m_ptrProperties[7] = ( (LPVOID) IDS_Description);
  49. m_ptrProperties[8] = ( (LPVOID) IDS_Caption);
  50. m_ptrProperties[9] = ( (LPVOID) IDS_Name);
  51. m_ptrProperties[10] = ( (LPVOID) IDS_CreationClassName );
  52. }
  53. /*****************************************************************************
  54. *
  55. * FUNCTION : CWin32USBHub::~CWin32USBHub
  56. *
  57. * DESCRIPTION : Destructor
  58. *
  59. * INPUTS : none
  60. *
  61. * OUTPUTS : none
  62. *
  63. * RETURNS : nothing
  64. *
  65. * COMMENTS : Deregisters property set from framework
  66. *
  67. ****************************************************************************/
  68. CWin32USBHub::~CWin32USBHub()
  69. {
  70. }
  71. ////////////////////////////////////////////////////////////////////////
  72. //
  73. // Function: CWin32USBHub::GetObject
  74. //
  75. // Inputs: CInstance *a_pInst - Instance into which we
  76. // retrieve data.
  77. //
  78. // Outputs: None.
  79. //
  80. // Returns: HRESULT Success/Failure code.
  81. //
  82. // Comments: The Calling function will Commit the instance.
  83. //
  84. ////////////////////////////////////////////////////////////////////////
  85. HRESULT CWin32USBHub::GetObject
  86. (
  87. CInstance *a_pInst,
  88. long a_lFlags,
  89. CFrameworkQuery& pQuery
  90. )
  91. {
  92. HRESULT t_hResult = WBEM_E_NOT_FOUND ;
  93. CConfigManager t_cfgmgr ;
  94. // Let's see if config manager recognizes this device at all
  95. CHString t_sDeviceID ;
  96. a_pInst->GetCHString( IDS_DeviceID, t_sDeviceID ) ;
  97. CConfigMgrDevicePtr t_pDevice;
  98. if( t_cfgmgr.LocateDevice( t_sDeviceID, t_pDevice ) )
  99. {
  100. // OK, it knows about it. Is it a USB Hub?
  101. if( IsOneOfMe(t_pDevice ) )
  102. {
  103. CFrameworkQueryEx *t_pQuery2 = static_cast <CFrameworkQueryEx*>( &pQuery ) ;
  104. DWORD t_dwProperties ;
  105. t_pQuery2->GetPropertyBitMask( m_ptrProperties, &t_dwProperties ) ;
  106. t_hResult = LoadPropertyValues( &CLPVParams( a_pInst,
  107. t_pDevice,
  108. t_dwProperties ) ) ;
  109. }
  110. }
  111. return t_hResult;
  112. }
  113. ////////////////////////////////////////////////////////////////////////
  114. //
  115. // Function: CWin32IDE::ExecQuery
  116. //
  117. // Inputs: MethodContext *a_pMethodContext - Context to enum
  118. // instance data in.
  119. // CFrameworkQuery& the query object
  120. //
  121. // Outputs: None.
  122. //
  123. // Returns: HRESULT Success/Failure code.
  124. //
  125. // Comments: None.
  126. //
  127. ////////////////////////////////////////////////////////////////////////
  128. HRESULT CWin32USBHub::ExecQuery
  129. (
  130. MethodContext *a_pMethodContext,
  131. CFrameworkQuery &a_pQuery,
  132. long a_lFlags
  133. )
  134. {
  135. CFrameworkQueryEx *t_pQuery2 = static_cast <CFrameworkQueryEx*>( &a_pQuery ) ;
  136. DWORD t_dwProperties ;
  137. t_pQuery2->GetPropertyBitMask( m_ptrProperties, &t_dwProperties ) ;
  138. return Enumerate( a_pMethodContext, a_lFlags, t_dwProperties ) ;
  139. }
  140. ////////////////////////////////////////////////////////////////////////
  141. //
  142. // Function: CWin32USBHub::EnumerateInstances
  143. //
  144. // Inputs: MethodContext *a_pMethodContext - Context to enum
  145. // instance data in.
  146. //
  147. // Outputs: None.
  148. //
  149. // Returns: HRESULT Success/Failure code.
  150. //
  151. // Comments: None.
  152. //
  153. ////////////////////////////////////////////////////////////////////////
  154. HRESULT CWin32USBHub::EnumerateInstances
  155. (
  156. MethodContext *a_pMethodContext,
  157. long a_lFlags /*= 0L*/
  158. )
  159. {
  160. return Enumerate( a_pMethodContext, a_lFlags ) ;
  161. }
  162. ////////////////////////////////////////////////////////////////////////
  163. //
  164. // Function: CWin32USBHub::Enumerate
  165. //
  166. // Inputs: MethodContext *a_pMethodContext - Context to enum
  167. // instance data in.
  168. //
  169. // Outputs: None.
  170. //
  171. // Returns: HRESULT Success/Failure code.
  172. //
  173. // Comments: None.
  174. //
  175. ////////////////////////////////////////////////////////////////////////
  176. HRESULT CWin32USBHub::Enumerate
  177. (
  178. MethodContext *a_pMethodContext,
  179. long a_lFlags,
  180. DWORD a_dwReqProps
  181. )
  182. {
  183. HRESULT t_hResult = WBEM_E_FAILED ;
  184. CConfigManager t_cfgManager ;
  185. CDeviceCollection t_deviceList ;
  186. CInstancePtr t_pInst;
  187. CConfigMgrDevicePtr t_pDevice;
  188. if( t_cfgManager.GetDeviceListFilterByClass( t_deviceList, L"USB" ) )
  189. {
  190. REFPTR_POSITION t_pos;
  191. if( t_deviceList.BeginEnum( t_pos ) )
  192. {
  193. t_hResult = WBEM_S_NO_ERROR;
  194. // Walk the list
  195. for (t_pDevice.Attach(t_deviceList.GetNext( t_pos ));
  196. SUCCEEDED( t_hResult ) && (t_pDevice != NULL);
  197. t_pDevice.Attach(t_deviceList.GetNext( t_pos )))
  198. {
  199. // Now to find out if this is the usb Hub
  200. if( IsOneOfMe( t_pDevice ) )
  201. {
  202. t_pInst.Attach(CreateNewInstance( a_pMethodContext ) );
  203. if( SUCCEEDED( t_hResult = LoadPropertyValues( &CLPVParams(
  204. t_pInst,
  205. t_pDevice,
  206. a_dwReqProps ) ) ) )
  207. {
  208. // Derived classes (like CW32USBCntrlDev) may
  209. // commit as result of call to
  210. // LoadPropertyValues, so check if we should
  211. // (only do so if we are of this class's type).
  212. if( ShouldBaseCommit( NULL ) )
  213. {
  214. t_hResult = t_pInst->Commit( ) ;
  215. }
  216. }
  217. }
  218. }
  219. // Always call EndEnum().
  220. t_deviceList.EndEnum();
  221. }
  222. }
  223. return t_hResult;
  224. }
  225. /*****************************************************************************
  226. *
  227. * FUNCTION : CWin32USBHub::LoadPropertyValues
  228. *
  229. * DESCRIPTION : Assigns values to properties
  230. *
  231. * INPUTS : void *a_pv - Instance package to load values into.
  232. *
  233. * OUTPUTS :
  234. *
  235. * RETURNS : HRESULT error/success code.
  236. *
  237. * COMMENTS :
  238. *
  239. *****************************************************************************/
  240. HRESULT CWin32USBHub::LoadPropertyValues
  241. (
  242. void *a_pv
  243. )
  244. {
  245. HRESULT t_hResult = WBEM_S_NO_ERROR;
  246. CHString t_chstrDeviceID, t_chstrDesc, t_chstrTemp;
  247. /*************************************
  248. * Unpack and confirm our parameters...
  249. *************************************/
  250. CLPVParams *t_pData = ( CLPVParams * ) a_pv ;
  251. CInstance *t_pInst = ( CInstance * )( t_pData->m_pInstance ) ; // This instance released by caller
  252. CConfigMgrDevice *t_pDevice = ( CConfigMgrDevice * )( t_pData->m_pDevice ) ;
  253. DWORD t_dwReqProps = ( DWORD )( t_pData->m_dwReqProps ) ;
  254. if( t_pInst == NULL || t_pDevice == NULL )
  255. {
  256. return WBEM_E_PROVIDER_FAILURE;
  257. }
  258. /***********************
  259. * Set the key properties
  260. ***********************/
  261. t_pDevice->GetDeviceID( t_chstrDeviceID ) ;
  262. if( t_chstrDeviceID.GetLength() == 0 )
  263. {
  264. // We need the device id for the key property of this class. If we can
  265. // not obtain it, we can't set the key, which is an unacceptable error.
  266. return WBEM_E_PROVIDER_FAILURE;
  267. }
  268. else
  269. {
  270. t_pInst->SetCHString( IDS_DeviceID, t_chstrDeviceID ) ;
  271. }
  272. /*********************************
  273. * Set CIM_LogicalDevice properties
  274. *********************************/
  275. if( t_dwReqProps & USBHUB_PROP_PNPDeviceID )
  276. {
  277. t_pInst->SetCHString( IDS_PNPDeviceID, t_chstrDeviceID ) ;
  278. }
  279. if( t_dwReqProps & USBHUB_PROP_SystemCreationClassName )
  280. {
  281. t_pInst->SetCHString( IDS_SystemCreationClassName,
  282. IDS_Win32ComputerSystem ) ;
  283. }
  284. if( t_dwReqProps & USBHUB_PROP_CreationClassName )
  285. {
  286. SetCreationClassName(t_pInst);
  287. }
  288. if( t_dwReqProps & USBHUB_PROP_SystemName )
  289. {
  290. t_pInst->SetCHString( IDS_SystemName, GetLocalComputerName() ) ;
  291. }
  292. if( t_dwReqProps & (USBHUB_PROP_Description | USBHUB_PROP_Caption | USBHUB_PROP_Name) )
  293. {
  294. if( t_pDevice->GetDeviceDesc( t_chstrDesc ) )
  295. {
  296. t_pInst->SetCHString( IDS_Description, t_chstrDesc ) ;
  297. }
  298. }
  299. if( t_dwReqProps & USBHUB_PROP_ConfigManagerErrorCode ||
  300. t_dwReqProps & USBHUB_PROP_Status )
  301. {
  302. DWORD t_dwStatus = 0L;
  303. DWORD t_dwProblem = 0L;
  304. if( t_pDevice->GetStatus( &t_dwStatus, &t_dwProblem ) )
  305. {
  306. if( t_dwReqProps & USBHUB_PROP_ConfigManagerErrorCode )
  307. {
  308. t_pInst->SetDWORD( IDS_ConfigManagerErrorCode, t_dwProblem ) ;
  309. }
  310. if( t_dwReqProps & USBHUB_PROP_Status )
  311. {
  312. CHString t_chsTmp;
  313. ConfigStatusToCimStatus ( t_dwStatus , t_chsTmp ) ;
  314. t_pInst->SetCHString(IDS_Status, t_chsTmp);
  315. }
  316. }
  317. }
  318. if( t_dwReqProps & USBHUB_PROP_ConfigManagerUserConfig )
  319. {
  320. t_pInst->SetDWORD( IDS_ConfigManagerUserConfig,
  321. t_pDevice->IsUsingForcedConfig() ) ;
  322. }
  323. // Use the friendly name for caption and name
  324. if( t_dwReqProps & USBHUB_PROP_Caption || t_dwReqProps & USBHUB_PROP_Name )
  325. {
  326. if( t_pDevice->GetFriendlyName( t_chstrTemp ) )
  327. {
  328. t_pInst->SetCHString( IDS_Caption, t_chstrTemp ) ;
  329. t_pInst->SetCHString( IDS_Name, t_chstrTemp ) ;
  330. }
  331. else
  332. {
  333. // If we can't get the name, settle for the description
  334. if( t_chstrDesc.GetLength() > 0 )
  335. {
  336. t_pInst->SetCHString( IDS_Caption, t_chstrDesc ) ;
  337. t_pInst->SetCHString( IDS_Name, t_chstrDesc ) ;
  338. }
  339. }
  340. }
  341. return t_hResult;
  342. }
  343. /*****************************************************************************
  344. *
  345. * FUNCTION : CWin32USBHub::IsOneOfMe
  346. *
  347. * DESCRIPTION : Checks to make sure pDevice is a hub and not some
  348. * other type of USB device.
  349. *
  350. * INPUTS : void *a_pv - The device to check.
  351. *
  352. * OUTPUTS :
  353. *
  354. * RETURNS : HRESULT error/success code.
  355. *
  356. * COMMENTS :
  357. *
  358. *****************************************************************************/
  359. bool CWin32USBHub::IsOneOfMe
  360. (
  361. void *a_pv
  362. )
  363. {
  364. bool t_fRet = false;
  365. if( NULL != a_pv )
  366. {
  367. CConfigMgrDevice *t_pDevice = ( CConfigMgrDevice * ) a_pv ;
  368. // Is it a usb device?
  369. if( t_pDevice->IsClass( L"USB" ) )
  370. {
  371. // Now to find out if this is a usb hub
  372. CConfigMgrDevicePtr t_pParentDevice;
  373. if( t_pDevice->GetParent( t_pParentDevice ) )
  374. {
  375. if( t_pParentDevice->IsClass( L"USB" ) )
  376. {
  377. t_fRet = true ;
  378. }
  379. }
  380. }
  381. }
  382. return t_fRet;
  383. }