Skip to content

Commit

Permalink
Helper: More DeviceInfo improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
dainnilsson committed Sep 13, 2024
1 parent d8e9cf3 commit 5ba0957
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions helper/helper/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,14 @@ def __init__(self, device):
super().__init__()
self._device = device
self._info = None
self._data = None
self._data = self._refresh_data()

def __call__(self, *args, **kwargs):
try:
response = super().__call__(*args, **kwargs)
if "device_info" in response.flags:
old_info = self._info
# Refresh data, and close any open child
self._close_child()
# Refresh data
self._data = self._refresh_data()
if old_info == self._info:
# No change to DeviceInfo, further propagation not needed.
Expand All @@ -297,8 +296,6 @@ def create_child(self, name):
raise NoSuchNodeException(name)

def get_data(self):
if not self._data:
self._data = self._refresh_data()
return self._data

def _refresh_data(self):
Expand All @@ -322,7 +319,6 @@ def _supports_connection(self, conn_type):

def _create_connection(self, conn_type):
connection = self._device.open_connection(conn_type)
self._data = self._read_data(connection)
return ConnectionNode(self._device, connection, self._info)

def _refresh_data(self):
Expand All @@ -332,7 +328,7 @@ def _refresh_data(self):
self._child._close_child()
return self._read_data(self._child._connection)

# New connection
# No child, open new connection
for conn_type in (SmartCardConnection, OtpConnection, FidoConnection):
if self._supports_connection(conn_type):
try:
Expand Down Expand Up @@ -396,18 +392,18 @@ def update(self, observable, actions):

class ReaderDeviceNode(AbstractDeviceNode):
def __init__(self, device):
super().__init__(device)
self._observer = _ReaderObserver(device)
self._monitor = CardMonitor()
self._monitor.addObserver(self._observer)
super().__init__(device)

def close(self):
self._monitor.deleteObserver(self._observer)
super().close()

def get_data(self):
if self._observer.needs_refresh:
self._data = None
self._data = self._refresh_data()
return super().get_data()

def _read_data(self, conn):
Expand All @@ -418,6 +414,7 @@ def _refresh_data(self):
if card is None:
return dict(present=False, status="no-card")
try:
self._close_child()
with self._device.open_connection(SmartCardConnection) as conn:
try:
data = self._read_data(conn)
Expand Down Expand Up @@ -449,7 +446,6 @@ def get(self, params, event, signal):
def ccid(self):
try:
connection = self._device.open_connection(SmartCardConnection)
self._data = self._read_data(connection)
return ScpConnectionNode(self._device, connection, self._info)
except (ValueError, SmartcardException, EstablishContextException) as e:
logger.warning("Error opening connection", exc_info=True)
Expand All @@ -458,8 +454,6 @@ def ccid(self):
@child
def fido(self):
try:
with self._device.open_connection(SmartCardConnection) as conn:
self._data = self._read_data(conn)
connection = self._device.open_connection(FidoConnection)
return ConnectionNode(self._device, connection, self._info)
except (ValueError, SmartcardException, EstablishContextException) as e:
Expand Down

0 comments on commit 5ba0957

Please sign in to comment.