Skip to content

Commit

Permalink
Merge 541a770 into 6836b14
Browse files Browse the repository at this point in the history
  • Loading branch information
ElouanPetereau committed Mar 18, 2021
2 parents 6836b14 + 541a770 commit 221b424
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 2 deletions.
15 changes: 15 additions & 0 deletions smartcard/CardConnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ def connect(self, protocol=None, mode=None, disposition=None):
Observable.setChanged(self)
Observable.notifyObservers(self, CardConnectionEvent('connect'))

def reconnect(self, protocol=None, mode=None, disposition=None):
"""Reconnect to card.
@param protocol: a bit mask of the protocols to use, from
L{CardConnection.T0_protocol}, L{CardConnection.T1_protocol},
L{CardConnection.RAW_protocol}, L{CardConnection.T15_protocol}
@param mode: SCARD_SHARE_SHARED (default), SCARD_SHARE_EXCLUSIVE or
SCARD_SHARE_DIRECT
@param disposition: SCARD_LEAVE_CARD, SCARD_RESET_CARD (default),
SCARD_UNPOWER_CARD or SCARD_EJECT_CARD
"""
Observable.setChanged(self)
Observable.notifyObservers(self, CardConnectionEvent('reconnect'))

def disconnect(self):
"""Disconnect from card."""
Observable.setChanged(self)
Expand Down
4 changes: 4 additions & 0 deletions smartcard/CardConnectionDecorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def connect(self, protocol=None, mode=None, disposition=None):
"""call inner component connect"""
self.component.connect(protocol, mode, disposition)

def reconnect(self, protocol=None, mode=None, disposition=None):
"""call inner component connect"""
self.component.reconnect(protocol, mode, disposition)

def disconnect(self):
"""call inner component disconnect"""
self.component.disconnect()
Expand Down
4 changes: 2 additions & 2 deletions smartcard/CardConnectionEvent.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class CardConnectionEvent(object):

def __init__(self, type, args=None):
"""
@param type: 'connect', 'disconnect', 'command', 'response'
@param args: None for 'connect' or 'disconnect'
@param type: 'connect', 'reconnect', 'disconnect', 'command', 'response'
@param args: None for 'connect', 'reconnect' or 'disconnect'
command APDU byte list for 'command'
[response data, sw1, sw2] for 'response'
"""
Expand Down
3 changes: 3 additions & 0 deletions smartcard/CardConnectionObserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def update(self, cardconnection, ccevent):
if 'connect' == ccevent.type:
print('connecting to ' + cardconnection.getReader())

elif 'reconnect' == ccevent.type:
print('reconnecting to ' + cardconnection.getReader())

elif 'disconnect' == ccevent.type:
print('disconnecting from ' + cardconnection.getReader())

Expand Down
47 changes: 47 additions & 0 deletions smartcard/pcsc/PCSCCardConnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,53 @@ def connect(self, protocol=None, mode=None, disposition=None):
protocol = eval("CardConnection.%s_protocol" % dictProtocol[p])
PCSCCardConnection.setProtocol(self, protocol)

def reconnect(self, protocol=None, mode=None, disposition=None):
"""Reconnect to the card.
If protocol is not specified, connect with the default
connection protocol.
If mode is not specified, connect with SCARD_SHARE_SHARED.
If disposition is not specified, do a warm reset (SCARD_RESET_CARD)"""
CardConnection.reconnect(self, protocol)
pcscprotocol = translateprotocolmask(protocol)
if 0 == pcscprotocol:
pcscprotocol = self.getProtocol()

if mode == None:
mode = SCARD_SHARE_SHARED

# store the way to dispose the card
if disposition == None:
if self.disposition == None:
self.disposition = SCARD_RESET_CARD
else:
self.disposition = disposition

hresult, dwActiveProtocol = SCardReconnect(self.hcard, mode, pcscprotocol, self.disposition)
if hresult != 0:
self.hcard = None
if hresult in (SCARD_W_REMOVED_CARD, SCARD_E_NO_SMARTCARD):
raise NoCardException('Unable to reconnect', hresult=hresult)
else:
raise CardConnectionException(
'Unable to reconnect with protocol: ' + \
dictProtocol[pcscprotocol] + '. ' + \
SCardGetErrorMessage(hresult))

protocol = 0
if dwActiveProtocol == SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1:
# special case for T0 | T1
# this happen when mode=SCARD_SHARE_DIRECT and no protocol is
# then negociated with the card
protocol = CardConnection.T0_protocol | CardConnection.T1_protocol
else:
for p in dictProtocol:
if p == dwActiveProtocol:
protocol = eval("CardConnection.%s_protocol" % dictProtocol[p])
PCSCCardConnection.setProtocol(self, protocol)

def disconnect(self):
"""Disconnect from the card."""

Expand Down

0 comments on commit 221b424

Please sign in to comment.