def connect(self, protocol=None, mode=None, disposition=None):
        '''Disconnect and reconnect in exclusive mode PCSCCardconnections.'''
        CardConnectionDecorator.connect(self, protocol, mode, disposition)
        component = self.component
        while True:
            if isinstance(
                    component,
                    smartcard.pcsc.PCSCCardConnection.PCSCCardConnection):
                pcscprotocol = PCSCCardConnection.translateprotocolmask(
                    protocol)
                if 0 == pcscprotocol:
                    pcscprotocol = component.getProtocol()

                if component.hcard is not None:
                    hresult = SCardDisconnect(component.hcard,
                                              SCARD_LEAVE_CARD)
                    if hresult != 0:
                        raise CardConnectionException(
                            'Failed to disconnect: ' +
                            SCardGetErrorMessage(hresult))
                hresult, component.hcard, dwActiveProtocol = SCardConnect(
                    component.hcontext, str(component.reader),
                    SCARD_SHARE_EXCLUSIVE, pcscprotocol)
                if hresult != 0:
                    raise CardConnectionException(
                        'Failed to connect with SCARD_SHARE_EXCLUSIVE' +
                        SCardGetErrorMessage(hresult))
                # print('reconnected exclusive')
                break
            if hasattr(component, 'component'):
                component = component.component
            else:
                break
Example #2
0
    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
Example #3
0
    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 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)
Example #5
0
        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
        reader = readers[0]
        print("select reader:", reader)

        try:
            # connect to card
            hresult, hcard, dwActiveProtocol = SCardConnect(
                hcontext,
                reader,
                SCARD_SHARE_SHARED,
                SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1,
            )
            if hresult != SCARD_S_SUCCESS:
                raise error("Unable to connect: " + SCardGetErrorMessage(hresult))
            print("Connected with active protocol", dwActiveProtocol)

            # select ARA application
            transmit_command(SELECT_ARA)
            # load new rule to ARA
            transmit_command(LOAD)

        finally:
            hresult = SCardDisconnect(hcard, SCARD_UNPOWER_CARD)
            if hresult != SCARD_S_SUCCESS:
                raise error("Failed to disconnect: " + SCardGetErrorMessage(hresult))