Skip to content

Commit

Permalink
Retry waking up ATECC on unexpected response
Browse files Browse the repository at this point in the history
The original code had intentions of retrying the wake up processor, but
it never used it. I.e., the default number of retries was 1. This
changes the default to 4 and adds a 500 ms wait in between tries.

The logic for this is that wakeup errors can happen for many reasons
especially since it's possible for other programs to access to ATECC
simultaneously. This is definitely a problem with OpenSSL engines since
they're independent of this library. The strategy for this library is to
minimize calls to the ATECC via caching of responses to read-only data,
but it still happens and retries are needed.

4 retries separated by 500 ms was picked since 1. it's still well under
the 5 second default Erlang timer on calls, so function calls shouldn't
time out, and 2. 500 ms is longer than most ATECC ops, so there's a good
change that whatever conflicted is done.
  • Loading branch information
fhunleth committed Nov 17, 2020
1 parent 732d31d commit b6422f3
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/atecc508a/transport/i2c_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ defmodule ATECC508A.Transport.I2CServer do
@atecc508a_wake_delay_ms 2
@atecc508a_signature <<0x04, 0x11, 0x33, 0x43>>
@atecc508a_poll_interval_ms 2
@atecc508a_retry_wakeup_ms 500
@atecc508a_default_wakeup_retries 4

@spec start_link(keyword()) :: :ignore | {:error, any()} | {:ok, pid()}
def start_link([bus_name, address, process_name]) do
Expand Down Expand Up @@ -164,7 +166,7 @@ defmodule ATECC508A.Transport.I2CServer do
end
end

defp wakeup(i2c, address, retries \\ 1)
defp wakeup(i2c, address, retries \\ @atecc508a_default_wakeup_retries)

defp wakeup(_i2c, _address, 0) do
{:error, :unexpected_wakeup_response}
Expand All @@ -188,10 +190,12 @@ defmodule ATECC508A.Transport.I2CServer do
:ok

{:ok, something_else} ->
_ = sleep(i2c, address)

Logger.warn("Unexpected wakeup response: #{inspect(something_else)}. Retrying.")
Logger.warn(
"Unexpected wakeup response: #{inspect(something_else)}. #{retries - 1} retries remaining."
)

Process.sleep(@atecc508a_retry_wakeup_ms)
_ = sleep(i2c, address)
wakeup(i2c, address, retries - 1)

error ->
Expand Down

0 comments on commit b6422f3

Please sign in to comment.