Protection class: a solution resolving I2C communication issues in complex systems

Introduction

The Basic Software

  1. Drivers
  2. Wrappers
  3. Main Class

The I2C Failure

The Protection Class

def __init__(switch, line, address):
"""Init the class."""
Protection.switch = switch # switch wrapper class
Protection.line = line # on which switch line the wrapper class is
Protection.address = address # on which i2c address the device is
  1. Wrapper wants to call a function from a driver, the call gets forwarded to the protection class which calls the desired drivers’ function,
  2. Driver returns the result to the wrapper, depending on what happened to the desired write or read operation (successful — including actual device data if any, unsuccessful or it timed out),
  3. Protection class then decides how to proceed. If the operation was successful, it returns the result (including data) to the wrapper class and terminates itself. In any other case, it proceeds with resolving.
  • First it performs a re-try for user-configurable number of times
# 1. retry a few times
logging.info("Retrying the call...")
for _ in range(RETRY_COUNT):
#print(_)
fun_ret = Protection.perform_function(function, args)
if fun_ret is not None: # call went ok, return result of actual function
logging.info("Retry call went ok, returning actual result.")
return fun_ret
time.sleep(0.05)
  • perform the I2C line test
  • Try to disable the line that the wrapper class wants to use
  • Re-enable the line it
  • Check if the device appears on I2C
  • If it doesn’t, disable it and return error to wrapper class
  • If it does, retry the function call
  • If function call was successful, return an actual result and restore the original state of all switch lines
  • If function call was not successful, return error to wrapper class
channels_state = Protection.switch.get_all_channels()  # get current states of all switch lines
if channels_state is not None:
# try to disable the line that wrapper class uses
ret_val = Protection.switch.set_channel(Protection.line, 0)
if not ret_val:
logging.error("Switch has failed to disable line {}.".format(Protection.line))
# re enable the line that wrapper class uses
ret_val = Protection.switch.set_channel(Protection.line, 1)
if not ret_val: # break
logging.error("Switch has failed to enable line {}.".format(Protection.line))
else: # continue
# check if device appears on i2c
active = Protection.switch.get_active_i2c_ports()
if not active or Protection.address not in active: # break
logging.error("Device did not appear on i2c bus.")
else: # continue
# retry the function call
fun_ret = Protection.perform_function(function, args)
if fun_ret is not None: # call went ok
logging.info("I2c test retry call went ok, returning actual result.")
# restore states of all switch lines
ret_val = Protection.switch.set_all_channels(channels_state)
if ret_val: # restore went ok
return fun_ret # return result of actual function
else: # restore failed
logging.error("Failed to restore states of all switch lines.")
else:
logging.error("Failed to communicate with switch driver...")

Conclusion

--

--

--

We are applying today’s knowledge to create systems for an open future.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Synchronizing: Part 2

Sending automatic messages to Slack from ASP.NET Core with Hangfire

Easy Enhancements for VS Code’s Terminal

What are Text Based Adventure Games: The Perfect Beginner Python Project

Get Acquainted with Facebook Login and the Graph API

Photo by Kaboompics.com from Pexels

Ingress: A Guide to configure on GKE

Oracle SQL online course — lesson 1

AppLibrary: A Step Towards Digital Decluttering?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Institute IRNAS

Institute IRNAS

We are applying today’s knowledge to create systems for an open future.

More from Medium

How Much Does It Cost To Build An App Like UberEats?

We’re rebuilding streaming discovery from scratch, wanna join?

How Kambr Joined the Metaverse Thanks to Gather

I Tried ANOTHER Artificial Intelligence Software