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.

236 lines
8.4 KiB

  1. /*******************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORP., 1993-1995
  4. * TITLE: OVRCURNT.CPP
  5. * VERSION: 1.0
  6. * AUTHOR: jsenior
  7. * DATE: 10/28/1998
  8. *
  9. ********************************************************************************
  10. *
  11. * CHANGE LOG:
  12. *
  13. * DATE REV DESCRIPTION
  14. * ---------- ------- ----------------------------------------------------------
  15. * 10/28/1998 jsenior Original implementation.
  16. *
  17. *******************************************************************************/
  18. #include "usbpopup.h"
  19. #include "itemfind.h"
  20. #include "debug.h"
  21. #include "usbutil.h"
  22. //
  23. // Refresh the contents of the treeview control.
  24. // Find all hubs with unused ports on them. If there are none, find some which
  25. // have devices requiring less than 100 mA.
  26. //
  27. BOOL UsbOvercurrentPopup::Refresh()
  28. {
  29. TV_INSERTSTRUCT item;
  30. int i=0; //, size;
  31. PUSB_ACQUIRE_INFO acquireInfoController = 0;
  32. BOOL result = FALSE;
  33. TCHAR buf[MAX_PATH], formatString[MAX_PATH];
  34. HWND hReset;
  35. String hubName = HubAcquireInfo->Buffer;
  36. //
  37. // Make the Reset button visible, since this is the overcurrent dialog
  38. //
  39. if (NULL != (hReset =GetDlgItem(hWnd, IDC_RESET_PORT)) ) {
  40. SetWindowLong(hReset,
  41. GWL_STYLE,
  42. (GetWindowLong(hReset, GWL_STYLE) | WS_VISIBLE)
  43. & ~WS_DISABLED);
  44. }
  45. //
  46. // Set the window's title bar
  47. //
  48. LoadString(gHInst, IDS_CURRENT_LIMIT_EXCEEDED, buf, MAX_PATH);
  49. SetWindowText(hWnd, buf);
  50. //
  51. // Clear all UI components, and then recreate the rootItem
  52. //
  53. UsbTreeView_DeleteAllItems(hTreeDevices);
  54. if (rootItem) {
  55. DeleteChunk(rootItem);
  56. delete rootItem;
  57. }
  58. rootItem = new UsbItem;
  59. if (!rootItem) {
  60. USBERROR((_T("Out of memory!\n")));
  61. goto OvercurrentRefreshError;
  62. }
  63. AddChunk(rootItem);
  64. //
  65. // Get the controller name and enumerate the tree
  66. //
  67. acquireInfoController = GetControllerName(WmiHandle,
  68. InstanceName);
  69. if (!acquireInfoController) {
  70. goto OvercurrentRefreshError;
  71. }
  72. if (!rootItem->EnumerateController(0,
  73. acquireInfoController->Buffer,
  74. &ImageList,
  75. 0)) {
  76. goto OvercurrentRefreshError;
  77. }
  78. acquireInfoController = (PUSB_ACQUIRE_INFO) LocalFree(acquireInfoController);
  79. LoadString(gHInst, IDS_UNKNOWNDEVICE, buf, MAX_PATH);
  80. LoadString(gHInst, IDS_UNKNOWNDEVICE, formatString, MAX_PATH);
  81. if (deviceItem.IsUnusedPort() ||
  82. !_tcscmp(deviceItem.configInfo->deviceDesc.c_str(), buf) ||
  83. !_tcscmp(deviceItem.configInfo->deviceDesc.c_str(), formatString)) {
  84. //
  85. // The hub has eroneously removed the device prior to throwing the
  86. // overcurrent notification
  87. //
  88. LoadString(gHInst,
  89. IDS_OVERCURRENT_NOTIFICATION_UNKNOWN,
  90. buf,
  91. MAX_PATH);
  92. } else {
  93. //
  94. // The device is still there
  95. // Set the notification using the name of the offending device
  96. //
  97. LoadString(gHInst,
  98. IDS_OVERCURRENT_NOTIFICATION,
  99. formatString,
  100. MAX_PATH);
  101. UsbSprintf(buf, formatString, deviceItem.configInfo->deviceDesc.c_str());
  102. }
  103. if (!SetTextItem(hWnd, IDC_POWER_NOTIFICATION, buf) ||
  104. !SetTextItem(hWnd, IDC_POWER_EXPLANATION, IDS_ENUMFAIL_COURSE) ||
  105. !SetTextItem(hWnd, IDC_POWER_RECOMMENDATION, IDS_OVERCURRENT_RECOMMENDATION)) {
  106. goto OvercurrentRefreshError;
  107. }
  108. if (rootItem->child) {
  109. if (deviceItem.configInfo->devInst) {
  110. //
  111. // The device hasn't been removed by either the hub or the user yet
  112. // Find the overcurrent device
  113. //
  114. UsbItemActionFindOvercurrentDevice f1(deviceItem.configInfo->devInst);
  115. rootItem->Walk(f1);
  116. if (f1.GetDevice()) {
  117. //
  118. // Device is still attached
  119. //
  120. result=InsertTreeItem (hTreeDevices,
  121. rootItem->child,
  122. TreeView_GetRoot(hTreeDevices),
  123. &item,
  124. TrueAlways,
  125. UsbItemActionFindOvercurrentDevice::IsValid,
  126. UsbItemActionFindOvercurrentDevice::IsExpanded);
  127. }
  128. }
  129. if (!result) {
  130. //
  131. // Device has been removed by either the hub or the user. Find the
  132. // hub that the device was attached to and highlight the port.
  133. //
  134. UsbItemActionFindOvercurrentHubPort f2(hubName, deviceItem.cxnAttributes.ConnectionIndex);
  135. rootItem->Walk(f2);
  136. if (f2.GetDevice()) {
  137. result=InsertTreeItem (hTreeDevices,
  138. rootItem->child,
  139. TreeView_GetRoot(hTreeDevices),
  140. &item,
  141. TrueAlways,
  142. UsbItemActionFindOvercurrentHubPort::IsValid,
  143. UsbItemActionFindOvercurrentHubPort::IsExpanded);
  144. }
  145. }
  146. }
  147. return result;
  148. OvercurrentRefreshError:
  149. if (acquireInfoController) {
  150. LocalFree(acquireInfoController);
  151. }
  152. return FALSE;
  153. }
  154. BOOL
  155. UsbOvercurrentPopup::OnCommand(INT wNotifyCode,
  156. INT wID,
  157. HWND hCtl)
  158. {
  159. switch (wID) {
  160. case IDC_RESET_PORT:
  161. PUSB_CONNECTION_NOTIFICATION resetNotification;
  162. ULONG size, res;
  163. _try {
  164. size = sizeof(USB_CONNECTION_NOTIFICATION);
  165. resetNotification =
  166. (PUSB_CONNECTION_NOTIFICATION) LocalAlloc(LMEM_ZEROINIT, size);
  167. if (!resetNotification) {
  168. //
  169. // do something here
  170. //
  171. return FALSE;
  172. }
  173. resetNotification->NotificationType = ResetOvercurrent;
  174. resetNotification->ConnectionNumber =
  175. ConnectionNotification->ConnectionNumber;
  176. res = WmiExecuteMethod(WmiHandle,
  177. InstanceName.c_str(),
  178. ResetOvercurrent,
  179. size,
  180. resetNotification,
  181. &size,
  182. resetNotification);
  183. if (res != ERROR_SUCCESS) {
  184. TCHAR szTitle[MAX_PATH], szMessage[MAX_PATH];
  185. LoadString(gHInst, IDS_RESET_FAILED, szMessage, MAX_PATH);
  186. LoadString(gHInst, IDS_USB_ERROR, szTitle, MAX_PATH);
  187. MessageBox(hWnd, szMessage, szTitle, MB_OK | MB_USERICON);
  188. }
  189. }
  190. _finally {
  191. if (resetNotification) {
  192. LocalFree(resetNotification);
  193. }
  194. EndDialog(hWnd, wID);
  195. }
  196. return TRUE;
  197. case IDOK:
  198. EndDialog(hWnd, wID);
  199. return TRUE;
  200. }
  201. return FALSE;
  202. }
  203. BOOL
  204. UsbOvercurrentPopup::CustomDialogWrap()
  205. {
  206. TCHAR unknownDevice[MAX_PATH], unknownHub[MAX_PATH];
  207. LoadString(gHInst, IDS_UNKNOWNDEVICE, unknownDevice, MAX_PATH);
  208. LoadString(gHInst, IDS_UNKNOWNHUB, unknownHub, MAX_PATH);
  209. if (deviceItem.IsUnusedPort() ||
  210. !_tcscmp(deviceItem.configInfo->deviceDesc.c_str(), unknownDevice) ||
  211. !_tcscmp(deviceItem.configInfo->deviceDesc.c_str(), unknownHub)) {
  212. return CustomDialog(IDD_INSUFFICIENT_POWER,
  213. NIIF_ERROR,
  214. IDS_OVERCURRENT_INITIAL_UNKNOWN,
  215. IDS_CURRENT_LIMIT_EXCEEDED);
  216. } else {
  217. return CustomDialog(IDD_INSUFFICIENT_POWER,
  218. NIIF_ERROR,
  219. IDS_OVERCURRENT_INITIAL,
  220. IDS_CURRENT_LIMIT_EXCEEDED);
  221. }
  222. }