Skip to content

Commit

Permalink
feat: grab i2c address from hardware definitions and add override
Browse files Browse the repository at this point in the history
- allows us to get the i2c address from the SWARM_KEY_URI in hm-pyhelper instead of hard coding it
- removes legacy KEY_STORAGE_BUS commands and corresponding test cases
- adds SWARM_KEY_URI_OVERRIDE to override the key location using an environment variabls
  • Loading branch information
shawaj committed Mar 12, 2023
1 parent 23c6698 commit 00f492a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 33 deletions.
20 changes: 0 additions & 20 deletions hw_diag/tests/test_hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,26 +376,6 @@ def test_detect_ecc_from_SWARM_KEY_URI(
mocked_config_search_param.assert_called_once_with('i2cdetect -y 7',
self.ECC_I2C_DETECT_PATTERN)

@patch.dict('hw_diag.utilities.hardware.variant_definitions', {
"HNT-TEST": {
"KEY_STORAGE_BUS": "/dev/i2c-3",
"NO_SWARM_KEY_URI": "ecc://i2c-7:96?slot=0",
}
})
@patch('hw_diag.utilities.hardware.config_search_param', return_value=True)
def test_detect_ecc_from_KEY_STORAGE_BUS(
self,
mocked_config_search_param,
):
diagnostics = {
'VA': 'HNT-TEST',
'ECC': None,
}
detect_ecc(diagnostics)
self.assertTrue(diagnostics['ECC'])
mocked_config_search_param.assert_called_once_with('i2cdetect -y 3',
self.ECC_I2C_DETECT_PATTERN)

@patch.dict('hw_diag.utilities.hardware.variant_definitions', {
"HNT-TEST": {
"NO_KEY_STORAGE_BUS": "/dev/i2c-3",
Expand Down
30 changes: 17 additions & 13 deletions hw_diag/utilities/hardware.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import os
import dbus
import psutil
from typing import Union
Expand Down Expand Up @@ -191,12 +192,19 @@ def set_diagnostics_bt_lte(diagnostics):

def parse_i2c_bus(address):
"""
Takes i2c bus as input parameter and extracts the bus number and returns it.
Takes i2c bus as input parameter, extracts the bus number and returns it.
"""
i2c_bus_pattern = r'i2c-(\d+)'
return re.search(i2c_bus_pattern, address).group(1)


def parse_i2c_address(port):
"""
Takes i2c address in decimal as input parameter, extracts the hex version and returns it.
"""
return f'{port:x}'


def detect_ecc(diagnostics):
# The order of the values in the lists is important!
# It determines which value will be available for which key
Expand All @@ -205,34 +213,30 @@ def detect_ecc(diagnostics):
i2c_bus = ''
try:
# Example SWARM_KEY_URI: "ecc://i2c-1:96?slot=0"
swarm_key_uri = get_variant_attribute(variant, 'SWARM_KEY_URI')
if os.getenv('SWARM_KEY_URI_OVERRIDE'):
swarm_key_uri = os.getenv('SWARM_KEY_URI_OVERRIDE')
else:
swarm_key_uri = get_variant_attribute(variant, 'SWARM_KEY_URI')

parse_result = urlparse(swarm_key_uri)
i2c_bus = parse_i2c_bus(parse_result.hostname)
i2c_address = parse_i2c_address(parse_result.port)

except Exception as e:
logging.warn("Unable to lookup SWARM_KEY_URI from hardware definitions, "
"Exception message: {}".format(e))

if not i2c_bus or not i2c_bus.isnumeric():
try:
# Example KEY_STORAGE_BUS: "/dev/i2c-1"
key_storage_bus = get_variant_attribute(variant, 'KEY_STORAGE_BUS')
i2c_bus = parse_i2c_bus(key_storage_bus)

except Exception as e:
logging.warn("Unable to lookup KEY_STORAGE_BUS from hardware definitions, "
"Exception message: {}".format(e))

if not i2c_bus or not i2c_bus.isnumeric():
logging.warn("Unable to lookup storage bus from hardware definitions, "
"falling back to the default.")
i2c_bus = '1'
i2c_address = '60'

commands = [
f'i2cdetect -y {i2c_bus}'
]

parameters = ["60 --"]
parameters = [f'{i2c_address} --']
keys = ["ECC"]

for (command, param, key) in zip(commands, parameters, keys):
Expand Down

0 comments on commit 00f492a

Please sign in to comment.