Skip to content

Commit

Permalink
feat: add support for multiple possible key locations
Browse files Browse the repository at this point in the history
Syncrobit has multiple potential key locations, change to list and parse list if necessary

Relates-to: #221
Relates-to: NebraLtd/helium-syncrobit#2 (comment)
Supersedes: #222
  • Loading branch information
shawaj committed Mar 15, 2023
1 parent f10b6bc commit 585901b
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 38 deletions.
53 changes: 27 additions & 26 deletions hm_pyhelper/hardware_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def is_raspberry_pi() -> bool:
'CPU_ARCH': 'arm64',
'BALENA_DEVICE_TYPE': ['raspberrypi3-64'],
'SPIBUS': 'spidev1.2',
'SWARM_KEY_URI': 'ecc://i2c-1:96?slot=0',
'SWARM_KEY_URI': ['ecc://i2c-1:96?slot=0'],
'RESET': 38,
'MAC': 'eth0',
'STATUS': 25,
Expand All @@ -47,7 +47,7 @@ def is_raspberry_pi() -> bool:
'CPU_ARCH': 'arm64',
'BALENA_DEVICE_TYPE': ['raspberrypi3-64'],
'SPIBUS': 'spidev1.2',
'SWARM_KEY_URI': 'ecc://i2c-1:96?slot=0',
'SWARM_KEY_URI': ['ecc://i2c-1:96?slot=0'],
'RESET': 38,
'MAC': 'eth0',
'STATUS': 25,
Expand All @@ -70,7 +70,7 @@ def is_raspberry_pi() -> bool:
'CPU_ARCH': 'arm64',
'BALENA_DEVICE_TYPE': ['rockpi-4b-rk3399'],
'SPIBUS': 'spidev32766.0',
'SWARM_KEY_URI': 'ecc://i2c-7:96?slot=0',
'SWARM_KEY_URI': ['ecc://i2c-7:96?slot=0'],
'RESET': 149,
'MAC': 'eth0',
'STATUS': 156,
Expand All @@ -95,7 +95,7 @@ def is_raspberry_pi() -> bool:
'CPU_ARCH': 'armv6hf',
'BALENA_DEVICE_TYPE': ['raspberry-pi'],
'SPIBUS': 'spidev1.2',
'SWARM_KEY_URI': 'ecc://i2c-1:96?slot=0',
'SWARM_KEY_URI': ['ecc://i2c-1:96?slot=0'],
'RESET': 22,
'MAC': 'wlan0',
'STATUS': 24,
Expand All @@ -116,7 +116,7 @@ def is_raspberry_pi() -> bool:
'CPU_ARCH': 'armv6hf',
'BALENA_DEVICE_TYPE': ['raspberry-pi'],
'SPIBUS': 'spidev1.2',
'SWARM_KEY_URI': 'ecc://i2c-1:96?slot=0',
'SWARM_KEY_URI': ['ecc://i2c-1:96?slot=0'],
'RESET': 22,
'MAC': 'wlan0',
'STATUS': 24,
Expand All @@ -137,7 +137,7 @@ def is_raspberry_pi() -> bool:
'CPU_ARCH': 'arm64',
'BALENA_DEVICE_TYPE': ['radxa-zero'],
'SPIBUS': 'spidev0.0',
'SWARM_KEY_URI': 'ecc://i2c-3:96?slot=0',
'SWARM_KEY_URI': ['ecc://i2c-3:96?slot=0'],
'RESET': 415,
'MAC': 'wlan0',
'STATUS': 421,
Expand All @@ -158,7 +158,7 @@ def is_raspberry_pi() -> bool:
'CPU_ARCH': 'armv6hf',
'BALENA_DEVICE_TYPE': ['raspberry-pi'],
'SPIBUS': 'spidev1.2',
'SWARM_KEY_URI': 'ecc://i2c-1:96?slot=0',
'SWARM_KEY_URI': ['ecc://i2c-1:96?slot=0'],
'RESET': 4,
'MAC': 'wlan0',
'STATUS': 26,
Expand All @@ -179,7 +179,7 @@ def is_raspberry_pi() -> bool:
'CPU_ARCH': 'armv7hf',
'BALENA_DEVICE_TYPE': ['beaglebone-black'],
'SPIBUS': 'spidev1.0',
'SWARM_KEY_URI': 'ecc://i2c-1:96?slot=0',
'SWARM_KEY_URI': ['ecc://i2c-1:96?slot=0'],
'RESET': 60,
'MAC': 'eth0',
'STATUS': 31,
Expand All @@ -200,7 +200,7 @@ def is_raspberry_pi() -> bool:
'CPU_ARCH': 'armv7hf',
'BALENA_DEVICE_TYPE': ['beaglebone-pocket'],
'SPIBUS': 'spidev1.2',
'SWARM_KEY_URI': 'ecc://i2c-1:96?slot=0',
'SWARM_KEY_URI': ['ecc://i2c-1:96?slot=0'],
'RESET': 60,
'MAC': 'wlan0',
'STATUS': 31,
Expand All @@ -221,7 +221,7 @@ def is_raspberry_pi() -> bool:
'CPU_ARCH': 'arm64',
'BALENA_DEVICE_TYPE': ['rockpi-4b-rk3399'],
'SPIBUS': 'spidev32766.0',
'SWARM_KEY_URI': 'ecc://i2c-7:96?slot=0',
'SWARM_KEY_URI': ['ecc://i2c-7:96?slot=0'],
'RESET': 149,
'MAC': 'eth0',
'STATUS': 156,
Expand All @@ -244,7 +244,7 @@ def is_raspberry_pi() -> bool:
'CPU_ARCH': 'armv7hf',
'BALENA_DEVICE_TYPE': ['asus-tinker-board'],
'SPIBUS': 'spidev2.0',
'SWARM_KEY_URI': 'ecc://i2c-1:96?slot=0',
'SWARM_KEY_URI': ['ecc://i2c-1:96?slot=0'],
'RESET': 167,
'MAC': 'eth0',
'STATUS': 163,
Expand All @@ -265,7 +265,7 @@ def is_raspberry_pi() -> bool:
'CPU_ARCH': 'arm64',
'BALENA_DEVICE_TYPE': ['raspberrypi4-64'],
'SPIBUS': 'spidev0.0',
'SWARM_KEY_URI': 'ecc://i2c-1:96?slot=0',
'SWARM_KEY_URI': ['ecc://i2c-1:96?slot=0'],
'RESET': 25,
'MAC': 'wlan0',
'STATUS': 20,
Expand All @@ -286,8 +286,8 @@ def is_raspberry_pi() -> bool:
'CPU_ARCH': 'arm64',
'BALENA_DEVICE_TYPE': ['raspberrypi3-64', 'raspberrypi4-64'],
'SPIBUS': 'spidev0.0',
'SWARM_KEY_URI': 'ecc://i2c-1:96?slot=0',
'ONBOARDING_KEY_URI': 'ecc://i2c-1:96?slot=15',
'SWARM_KEY_URI': ['ecc://i2c-1:96?slot=0'],
'ONBOARDING_KEY_URI': ['ecc://i2c-1:96?slot=15'],
'RESET': 25,
'MAC': 'wlan0',
'STATUS': 20,
Expand All @@ -308,7 +308,7 @@ def is_raspberry_pi() -> bool:
'CPU_ARCH': 'arm64',
'BALENA_DEVICE_TYPE': ['raspberrypi4-64'],
'SPIBUS': 'spidev0.0',
'SWARM_KEY_URI': 'ecc://i2c-1:96?slot=0',
'SWARM_KEY_URI': ['ecc://i2c-1:96?slot=0'],
'RESET': 17,
'MAC': 'wlan0',
'STATUS': 22,
Expand All @@ -329,7 +329,7 @@ def is_raspberry_pi() -> bool:
'CPU_ARCH': 'arm64',
'BALENA_DEVICE_TYPE': ['raspberrypi4-64'],
'SPIBUS': 'spidev0.0',
'SWARM_KEY_URI': 'ecc://i2c-1:96?slot=0',
'SWARM_KEY_URI': ['ecc://i2c-1:96?slot=0'],
'RESET': 23,
'MAC': 'wlan0',
'STATUS': 22,
Expand All @@ -350,7 +350,7 @@ def is_raspberry_pi() -> bool:
'CPU_ARCH': 'arm64',
'BALENA_DEVICE_TYPE': ['raspberrypi4-64'],
'SPIBUS': 'spidev0.0',
'SWARM_KEY_URI': 'ecc://i2c-1:96?slot=0',
'SWARM_KEY_URI': ['ecc://i2c-1:96?slot=0'],
'RESET': 17,
'MAC': 'wlan0',
'STATUS_LED': {
Expand All @@ -376,7 +376,7 @@ def is_raspberry_pi() -> bool:
'CPU_ARCH': 'arm64',
'BALENA_DEVICE_TYPE': ['raspberrypi4-64'],
'SPIBUS': 'spidev0.0',
'SWARM_KEY_URI': 'ecc://i2c-0:96?slot=0',
'SWARM_KEY_URI': ['ecc://i2c-0:96?slot=0'],
'RESET': 23,
'MAC': 'eth0',
'STATUS': 17,
Expand All @@ -397,7 +397,7 @@ def is_raspberry_pi() -> bool:
'CPU_ARCH': 'arm64',
'BALENA_DEVICE_TYPE': ['raspberrypi4-64'],
'SPIBUS': 'spidev0.0',
'SWARM_KEY_URI': 'ecc://i2c-1:96?slot=0',
'SWARM_KEY_URI': ['ecc://i2c-1:96?slot=0'],
'RESET': 4,
'MAC': 'wlan0',
'STATUS_LED': {
Expand Down Expand Up @@ -425,7 +425,7 @@ def is_raspberry_pi() -> bool:
'raspberrypi0-2w-64', 'raspberrypi3-64',
'raspberrypi4-64'],
'SPIBUS': 'spidev0.0',
'SWARM_KEY_URI': 'ecc://i2c-1:96?slot=0',
'SWARM_KEY_URI': ['ecc://i2c-1:96?slot=0'],
'RESET': 22,
'MAC': 'eth0',
'STATUS': 20,
Expand All @@ -448,7 +448,7 @@ def is_raspberry_pi() -> bool:
'raspberrypi0-2w-64', 'raspberrypi3-64',
'raspberrypi4-64'],
'SPIBUS': 'spidev0.0',
'SWARM_KEY_URI': 'ecc://i2c-1:96?slot=0',
'SWARM_KEY_URI': ['ecc://i2c-1:96?slot=0'],
'RESET': 17,
'MAC': 'eth0',
'STATUS': 20,
Expand All @@ -469,7 +469,7 @@ def is_raspberry_pi() -> bool:
'CPU_ARCH': 'arm64',
'BALENA_DEVICE_TYPE': ['raspberrypicm4-ioboard'],
'SPIBUS': 'spidev0.0',
'SWARM_KEY_URI': 'ecc://i2c-1:96?slot=0',
'SWARM_KEY_URI': ['ecc://i2c-1:96?slot=0'],
'RESET': 17,
'MAC': 'wlan0',
'STATUS': 22,
Expand All @@ -490,7 +490,7 @@ def is_raspberry_pi() -> bool:
'CPU_ARCH': 'arm64',
'BALENA_DEVICE_TYPE': ['raspberrypicm4-ioboard'],
'SPIBUS': 'spidev0.0',
'SWARM_KEY_URI': 'ecc://i2c-10:96?slot=0',
'SWARM_KEY_URI': ['ecc://i2c-10:96?slot=0'],
'RESET': 23,
'MAC': 'wlan0',
'STATUS': 22,
Expand All @@ -511,7 +511,8 @@ def is_raspberry_pi() -> bool:
'CPU_ARCH': 'arm64',
'BALENA_DEVICE_TYPE': ['raspberrypicm4-ioboard'],
'SPIBUS': 'spidev0.0',
'SWARM_KEY_URI': 'ecc://i2c-1:96?slot=0',
'SWARM_KEY_URI': ['ecc://i2c-1:96?slot=0', 'ecc://i2c-1:88?slot=2'],
'ONBOARDING_KEY_URI': ['ecc://i2c-1:96?slot=0', 'ecc://i2c-1:88?slot=15'],
'RESET': 17,
'MAC': 'wlan0',
'STATUS': 22,
Expand All @@ -532,7 +533,7 @@ def is_raspberry_pi() -> bool:
'CPU_ARCH': 'arm64',
'BALENA_DEVICE_TYPE': ['raspberrypi4-64'],
'SPIBUS': 'spidev0.0', # There is a CSN1 pin which is connected to GPIO6 (HAT Pin 31)
'SWARM_KEY_URI': 'ecc://i2c-1:96?slot=0',
'SWARM_KEY_URI': ['ecc://i2c-1:96?slot=0'],
'RESET': 22,
'MAC': 'eth0',
'STATUS': 21, # Stub. There is no status LED on X3. I2C-3 is used for display
Expand All @@ -553,7 +554,7 @@ def is_raspberry_pi() -> bool:
'CPU_ARCH': 'arm64',
'BALENA_DEVICE_TYPE': ['raspberrypicm4-ioboard'],
'SPIBUS': 'spidev0.0',
'SWARM_KEY_URI': 'ecc://i2c-1:96?slot=0',
'SWARM_KEY_URI': ['ecc://i2c-1:96?slot=0'],
'RESET': 17,
'MAC': 'wlan0',
'STATUS': 22,
Expand Down
68 changes: 62 additions & 6 deletions hm_pyhelper/miner_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import subprocess
import json
import platform
from urllib.parse import urlparse
from packaging.version import Version

from retry import retry
Expand Down Expand Up @@ -111,6 +112,28 @@ def get_gateway_mfr_version() -> Version:
raise GatewayMFRInvalidVersion(err_str).with_traceback(e.__traceback__)


def get_ecc_location() -> str:
ecc_list = get_variant_attribute(os.getenv('VARIANT'), 'SWARM_KEY_URI')

if os.getenv('SWARM_KEY_URI_OVERRIDE'):
ecc_location = os.getenv('SWARM_KEY_URI_OVERRIDE')
elif len(ecc_list) == 1:
ecc_location = ecc_list[0]
else:
for location in ecc_list:
parse_result = urlparse(location)
i2c_bus = parse_i2c_bus(parse_result.hostname)
i2c_address = parse_i2c_address(parse_result.port)
command = f'i2cdetect -y {i2c_bus}'
parameter = f'{i2c_address} --'

if config_search_param(command, parameter):
ecc_location = location
return ecc_location

return ecc_location


def get_gateway_mfr_command(sub_command: str, slot: int = False) -> list:
gateway_mfr_path = get_gateway_mfr_path()
command = [gateway_mfr_path]
Expand All @@ -119,14 +142,9 @@ def get_gateway_mfr_command(sub_command: str, slot: int = False) -> list:

if gateway_mfr_version >= Version('0.2.0'):
try:
if os.getenv('SWARM_KEY_URI_OVERRIDE'):
ecc_location = os.getenv('SWARM_KEY_URI_OVERRIDE')
else:
ecc_location = get_variant_attribute(os.getenv('VARIANT'), 'SWARM_KEY_URI')

device_arg = [
'--device',
ecc_location
get_ecc_location()
]
command.extend(device_arg)
except (UnknownVariantException, UnknownVariantAttributeException) as e:
Expand Down Expand Up @@ -334,3 +352,41 @@ def await_spi_available(spi_bus):
return True
else:
raise SPIUnavailableException("SPI bus %s not found!" % spi_bus)


@lock_ecc()
def config_search_param(command, param):
"""
input:
command: Command to execute
param: The parameter we are looking for in the response
return: True is exist, or False if doesn't exist
Possible exceptions:
TypeError: If the arguments passed to the function are not strings.
"""
if type(command) is not str:
raise TypeError("The command must be a string value")
if type(param) is not str:
raise TypeError("The param must be a string value")
result = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
out, err = result.communicate()
out = out.decode("UTF-8")
if param in out:
return True
else:
return False


def parse_i2c_bus(address):
"""
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}'
Loading

0 comments on commit 585901b

Please sign in to comment.