-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #139 from NebraLtd/automatic_sbc_detection
fix: is_rockpi and is_raspberry_pi should not break outside Balena #69
- Loading branch information
Showing
4 changed files
with
156 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
""" | ||
This module provides a set of functions to extract information about | ||
the Single Board Computer in use. | ||
It considers Balena environment variables as primary source of truth. | ||
It also uses the device tree to extract information about the SBC. | ||
""" | ||
|
||
import os | ||
from enum import Enum, auto | ||
from collections import namedtuple | ||
|
||
SBCInfo = namedtuple('SBCInfo', ['vendor_id', 'vendor_name', 'model_name']) | ||
|
||
|
||
class DeviceVendorID(Enum): | ||
""" | ||
Enum for device vendors. | ||
""" | ||
INVALID = auto() | ||
ROCK_PI = auto() | ||
RASPBERRY_PI = auto() | ||
|
||
|
||
BALENA_ENV_RASPBERRY_PI_MODELS = [ | ||
""" | ||
Pulled from | ||
https://www.balena.io/docs/reference/base-images/devicetypes/ | ||
""" | ||
'raspberry-pi2', | ||
'raspberrypi3', | ||
'raspberrypi3-64', | ||
'raspberrypi4-64', | ||
'nebra-hnt', | ||
'raspberrypicm4-ioboard' | ||
] | ||
|
||
BALENA_ENV_ROCKPI_MODELS = ['rockpi-4b-rk3399'] | ||
|
||
BALENA_MODELS = { | ||
DeviceVendorID.ROCK_PI: BALENA_ENV_ROCKPI_MODELS, | ||
DeviceVendorID.RASPBERRY_PI: BALENA_ENV_RASPBERRY_PI_MODELS | ||
} | ||
|
||
|
||
def device_model(): | ||
with open('/proc/device-tree/model', 'r') as f: | ||
return f.readline().strip() | ||
|
||
|
||
def sbc_info() -> SBCInfo: | ||
''' | ||
return SBCInfo formed by reading '/proc/device-tree/model' | ||
''' | ||
sbc_info = SBCInfo(vendor_id=DeviceVendorID.INVALID, vendor_name='', model_name='') | ||
dev_model = device_model() | ||
if dev_model.lower().find('raspberry') >= 0: | ||
sbc_info = SBCInfo(vendor_id=DeviceVendorID.RASPBERRY_PI, | ||
vendor_name='Raspberry Pi', | ||
model_name=dev_model) | ||
elif dev_model.lower().find('rock') >= 0: | ||
sbc_info = SBCInfo(vendor_id=DeviceVendorID.ROCK_PI, | ||
vendor_name='Radxa Rock Pi', | ||
model_name=dev_model) | ||
return sbc_info | ||
|
||
|
||
def is_sbc_type(device_id: DeviceVendorID) -> bool: | ||
''' | ||
Return true if the sbc matches the type supplied. | ||
''' | ||
device_type = os.getenv('BALENA_DEVICE_TYPE') | ||
|
||
# use device tree supplied model name if evn not set | ||
if not device_type: | ||
return sbc_info().vendor_id == device_id | ||
|
||
# honor env override | ||
return device_type in BALENA_MODELS.get(device_id, []) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
|
||
setup( | ||
name='hm_pyhelper', | ||
version='0.13.15', | ||
version='0.13.16', | ||
author="Nebra Ltd", | ||
author_email="[email protected]", | ||
description="Helium Python Helper", | ||
|