def stress(*args): """ stress method """ thread = args[0] for j in range(0, MAX_ITER): print(thread, end=' ') hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER) if hresult != SCARD_S_SUCCESS: raise EstablishContextException(hresult) hresult = SCardReleaseContext(hcontext) # print("SCardReleaseContext()", SCardGetErrorMessage(hresult)) if hresult != SCARD_S_SUCCESS: raise ReleaseContextException(hresult)
def launch_command(self, reader_name, command): try: hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER) assert hresult == SCARD_S_SUCCESS hresult, hcard, dwActiveProtocol = SCardConnect( hcontext, reader_name, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1) # hresult, response = SCardTransmit(hcard,dwActiveProtocol,[0xFF,0xCA,0x00,0x00,0x00]) hresult, response = SCardTransmit(hcard, dwActiveProtocol, command) return response[:len(response) - 2] except SystemError as err: print("Error in launch command : {}".format(err)) return None
def open(self): if self._reader: try: hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER) if hresult != SCARD_S_SUCCESS: raise Exception('Failed to establish context : ' + SCardGetErrorMessage(hresult)) hresult, hcard, dwActiveProtocol = SCardConnect( hcontext, self._reader, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1) if hresult != SCARD_S_SUCCESS: raise Exception('Unable to connect: ' + SCardGetErrorMessage(hresult)) return LLScardDevice(hcontext, hcard, dwActiveProtocol) except Exception: self._set_status(CardStatus.InUse)
def _list(self): try: hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER) if hresult != SCARD_S_SUCCESS: raise Exception('Failed to establish context : ' + SCardGetErrorMessage(hresult)) try: hresult, readers = SCardListReaders(hcontext, []) if hresult != SCARD_S_SUCCESS: raise Exception('Failed to list readers: ' + SCardGetErrorMessage(hresult)) return readers finally: hresult = SCardReleaseContext(hcontext) if hresult != SCARD_S_SUCCESS: raise Exception('Failed to release context: ' + SCardGetErrorMessage(hresult)) except: # noqa: E722 return []
def stress(*args): """ stress method """ thread = args[0] print("Starting thread:", thread) hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER) if hresult != SCARD_S_SUCCESS: raise EstablishContextException(hresult) hresult, readers = SCardListReaders(hcontext, []) if hresult != SCARD_S_SUCCESS: raise ListReadersException(hresult) for j in range(0, MAX_ITER): # Connect in SCARD_SHARE_SHARED mode hresult, hcard, dwActiveProtocol = SCardConnect( hcontext, readers[0], SCARD_SHARE_SHARED, SCARD_PROTOCOL_ANY) if hresult != SCARD_S_SUCCESS: raise BaseSCardException(hresult) log = "%d:%d" % (thread, j) print(log, end=' ') hresult = SCardDisconnect(hcard, SCARD_LEAVE_CARD) if hresult != SCARD_S_SUCCESS: raise BaseSCardException(hresult) print() hresult = SCardReleaseContext(hcontext) if hresult != SCARD_S_SUCCESS: raise ReleaseContextException(hresult) print("Exiting thread:", thread)
def print_hex_response(response): print("response: " + toHexString(response, HEX)) def transmit_command(command): hresult, response = SCardTransmit(hcard, dwActiveProtocol, command) if hresult != SCARD_S_SUCCESS or response[1] != 0x9F: raise error("Failed to select ARA: " + SCardGetErrorMessage(hresult)) print_hex_response(response) if __name__ == "__main__": try: hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER) if hresult != SCARD_S_SUCCESS: raise error( "Failed to establish context : " + SCardGetErrorMessage(hresult) ) print("Context established!") hresult, readers = SCardListReaders(hcontext, []) if hresult != SCARD_S_SUCCESS: raise error("failed to get readers: " + SCardGetErrorMessage(hresult)) print("readers:", readers) if len(readers) < 1: raise error("No smart card readers") # default to select first reader
def __establishContext(self): hresult, self.hcontext = SCardEstablishContext(SCARD_SCOPE_USER) if hresult != SCARD_S_SUCCESS: raise CardConnectionException('Failed to establish context: ' + SCardGetErrorMessage(hresult))