Skip to content

Commit

Permalink
implemented retry execution and added ATCA_WATCHDOG_ABOUT_TO_EXPIRE
Browse files Browse the repository at this point in the history
  • Loading branch information
dmazzella committed Jan 15, 2019
1 parent a4b3d5a commit 988d6d4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 27 deletions.
66 changes: 39 additions & 27 deletions cryptoauthlib/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,31 +64,43 @@ def sleep(self):
self._bus.writeto(self._address, b'\x01')

def execute(self, packet):
self.wake()
# Wait tWHI + tWLO
utime.sleep_us(WAKE_DELAY)

# Set device name
packet.device = self._device

# Send the command
self._bus.writeto(self._address, b'\x03' + packet.to_buffer())

# Delay for execution time
utime.sleep_ms(packet.delay)

response = packet.response_data_mv

# Receive the response
self._bus.readfrom_into(self._address, response[0:1])
self._bus.readfrom_into(self._address, response[1:response[0]])

# Check response
err, exc = self.is_error(response)
if err == ATCA_STATUS.ATCA_SUCCESS:
packet.response_data = response[:response[0]]
elif err == ATCA_STATUS.ATCA_WAKE_SUCCESS:
pass

while self._retries:
try:
self.wake()
# Wait tWHI + tWLO
utime.sleep_us(WAKE_DELAY)

# Set device name
packet.device = self._device

# Send the command
self._bus.writeto(self._address, b'\x03' + packet.to_buffer())

# Delay for execution time
utime.sleep_ms(packet.delay)

response = packet.response_data_mv

# Receive the response
self._bus.readfrom_into(self._address, response[0:1])
self._bus.readfrom_into(self._address, response[1:response[0]])

# Check response
err, exc = self.is_error(response)
if err == ATCA_STATUS.ATCA_SUCCESS:
packet.response_data = response[:response[0]]
self._retries = RX_RETRIES
return
elif err == ATCA_STATUS.ATCA_WAKE_SUCCESS:
return
elif err == ATCA_STATUS.ATCA_WATCHDOG_ABOUT_TO_EXPIRE:
self.sleep()
else:
if exc is not None:
raise exc(ubinascii.hexlify(response))
except OSError:
self._retries -= 1
else:
if exc is not None:
raise exc(ubinascii.hexlify(response))
self._retries = RX_RETRIES
raise ATCA_EXECUTIONS.GenericError("max retry")
7 changes: 7 additions & 0 deletions cryptoauthlib/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ def __init__(self, *args):
*args
)

class WatchDogAboutToExpireError(CryptoError):
def __init__(self, *args):
super().__init__(
"response status indicate insufficient time to execute the given "
"commmand begore watchdog timer will expire (status byte = 0xEE)",
*args
)

class CrcError(CryptoError):
def __init__(self, *args):
Expand Down
3 changes: 3 additions & 0 deletions cryptoauthlib/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
ATCA_HEALTH_TEST_ERROR = const(0xFA)
# Couldn't allocate required memory
ATCA_ALLOC_FAILURE = const(0xFB)
# Watchdog about to expire
ATCA_WATCHDOG_ABOUT_TO_EXPIRE = const(0xEE)


def decode_error(error):
Expand All @@ -88,5 +90,6 @@ def decode_error(error):
0x08: (ATCA_HEALTH_TEST_ERROR, ATCA_EXECUTIONS.HealthTestError),
0x0F: (ATCA_EXECUTION_ERROR, ATCA_EXECUTIONS.ExecutionError),
0x11: (ATCA_WAKE_SUCCESS, None),
0xEE: (ATCA_WATCHDOG_ABOUT_TO_EXPIRE, ATCA_EXECUTIONS.WatchDogAboutToExpireError),
0xFF: (ATCA_STATUS_CRC, ATCA_EXECUTIONS.CrcError),
}.get(error, (ATCA_GEN_FAIL, ATCA_EXECUTIONS.GenericError))

0 comments on commit 988d6d4

Please sign in to comment.