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.

194 lines
4.9 KiB

  1. // statmon.h
  2. /*++
  3. Copyright (C) Microsoft Corporation, 1996 - 1999
  4. Module Name:
  5. StatMon
  6. Abstract:
  7. This file contains the implementation of CScStatusMonitor
  8. (an object that watches for status changes of readers recognized
  9. by the smart card service, handling PNP and requests for copies of
  10. the status array)
  11. Author:
  12. Amanda Matlosz 02/26/98
  13. Environment:
  14. Win32, C++ with exceptions
  15. Revision History:
  16. Notes:
  17. In order to use this class, first declare an object, then initialize it w/
  18. your HWND and a UINT indicating the message you want to receive when a
  19. change in the system reader status has occurred. Handle that message
  20. simply by calling GetReaderStatus(). When the object is destroyed, any
  21. outstanding threads will be stopped.
  22. CScStatusMonitor m_monitor;
  23. m_monitor.Start(hWnd, STATUS_MONITOR_CHANGE, NULL) // use default reader group
  24. on STATUS_MONITOR_CHANGE()
  25. {
  26. m_monitor.GetStatus(); ???????
  27. m_monitor.GetReaderStatus(ReaderStatusArray); // check return val for system errors
  28. }
  29. m_monitor.Stop(); // also in ~CScStatusMonitor(); closes threads
  30. The monitor object maintains a separate thread to signal and record changes in
  31. reader status, as well as an enhanced record of that status that is available
  32. to the object's caller via GetReaderStatus(). The enhnced record includes the
  33. first card name associated with the inserted card's ATR.
  34. In the event that the resource manager service is stopped, the monitor will
  35. move to a stopped state and send the status_change message to the caller's hWnd.
  36. The caller is responsible for starting the monitor again (if it so wishes)
  37. when the service is back up.
  38. --*/
  39. #ifndef _STATUS_MONITOR
  40. #define _STATUS_MONITOR
  41. #include <afxwin.h>
  42. #include "afxtempl.h"
  43. #include <afxmt.h>
  44. #include "winscard.h"
  45. #include "calaislb.h"
  46. // Status of reader
  47. #define SC_STATUS_FIRST SC_STATUS_NO_CARD
  48. #define SC_STATUS_NO_CARD 0 // SCARD_STATE_EMPTY
  49. #define SC_STATUS_UNKNOWN 1 // SCARD_STATE_PRESENT | SCARD_STATE_MUTE
  50. #define SC_SATATUS_AVAILABLE 2 // SCARD_STATE_PRESENT (| SCARD_STATE_UNPOWERED)
  51. #define SC_STATUS_SHARED 3 // SCARD_SATATE_PRESENT | SCARD_STATE_INUSE
  52. #define SC_STATUS_EXCLUSIVE 4 // "" | SCARD_STATE_EXCLUSIVE
  53. #define SC_STATUS_ERROR 5 // SCARD_STATE_UNAVAILABLE (reader or card error)
  54. #define SC_STATUS_LAST SC_STATUS_ERROR
  55. class CSCardReaderState
  56. {
  57. public:
  58. CSCardReaderState(const CSCardReaderState* pCRS=NULL)
  59. {
  60. if(NULL != pCRS)
  61. {
  62. strReader = (LPCTSTR)pCRS->strReader;
  63. dwCurrentState = pCRS->dwCurrentState;
  64. dwEventState = pCRS->dwEventState;
  65. cbAtr = pCRS->cbAtr;
  66. memcpy(rgbAtr, pCRS->rgbAtr, pCRS->cbAtr);
  67. strCard = (LPCTSTR)pCRS->strCard;
  68. dwState = pCRS->dwState;
  69. fOK = pCRS->fOK;
  70. }
  71. else
  72. {
  73. strReader = _T("");
  74. dwCurrentState = 0;
  75. dwEventState = 0;
  76. ZeroMemory(rgbAtr, sizeof(rgbAtr));
  77. cbAtr = 0;
  78. strCard = _T("");
  79. dwState = 0;
  80. fOK = FALSE;
  81. }
  82. }
  83. // used to talk w/ Resource Manager
  84. CString strReader; // reader name
  85. DWORD dwCurrentState; // current state of reader at time of call
  86. DWORD dwEventState; // state of reader after state change
  87. DWORD cbAtr; // Number of bytes in the returned ATR.
  88. BYTE rgbAtr[36]; // Atr of inserted card, (extra alignment bytes)
  89. // used by caller for easier user-friendly UI
  90. CString strCard; // first card name RM returns for ATR
  91. DWORD dwState; // simplified reader state; see #defines above
  92. // BOOL for Smart Card Common Dialog's use (not used by status monitor)
  93. BOOL fOK;
  94. };
  95. typedef CTypedPtrArray<CPtrArray, CSCardReaderState*> CSCardReaderStateArray;
  96. class CScStatusMonitor
  97. {
  98. public:
  99. // status
  100. enum status{ uninitialized=0,
  101. stopped,
  102. running,
  103. no_service,
  104. no_readers,
  105. unknown};
  106. // constructors
  107. CScStatusMonitor()
  108. {
  109. m_status = CScStatusMonitor::uninitialized;
  110. m_uiStatusChangeMsg=0;
  111. m_pStatusThrd=NULL;
  112. m_szReaderNames = NULL;
  113. m_pInternalReaderStatus = NULL;
  114. m_dwInternalNumReaders = 0;
  115. }
  116. ~CScStatusMonitor();
  117. // operations & attributes
  118. LONG Start(HWND hWnd, UINT uiMsg, LPCTSTR szGroupNames=NULL);
  119. void Stop();
  120. void GetReaderStatus(CSCardReaderStateArray& aReaderStatus);
  121. void SetReaderStatus(); // uses same lock as above...
  122. status GetStatus() { return m_status; }
  123. UINT GetStatusChangeProc();
  124. private:
  125. LONG InitInternalReaderStatus();
  126. LONG UpdateInternalReaderStatus();
  127. void EmptyExternalReaderStatus();
  128. // members
  129. status m_status;
  130. HWND m_hwnd;
  131. UINT m_uiStatusChangeMsg;
  132. CWinThread* m_pStatusThrd;
  133. HANDLE m_hEventKillStatus;
  134. SCARDCONTEXT m_hContext;
  135. SCARDCONTEXT m_hInternalContext;
  136. CTextMultistring m_strGroupNames;
  137. CCriticalSection m_csRdrStsLock;
  138. CSCardReaderStateArray m_aReaderStatus;
  139. // internally maintained -- does not include card name, rebuilt during each SetReaderStatus
  140. CCriticalSection m_csInternalRdrStsLock;
  141. SCARD_READERSTATE* m_pInternalReaderStatus;
  142. DWORD m_dwInternalNumReaders;
  143. LPTSTR m_szReaderNames;
  144. };
  145. #endif // _STATUS_MONITOR