From b5d48a6503314f296c6cf4978305e0d39350ca2e Mon Sep 17 00:00:00 2001 From: Stephen Just Date: Sun, 10 Nov 2024 13:49:29 -0800 Subject: [PATCH] Automatically detect and report hardware model for most SBCs (#1540) ARM-based machines populate the device model into Device Tree. We can use this information to automatically detect and report the hardware model for most Single Board Computers (SBCs). Vendors who want to override this can still do so via the value in the configuration database. --- .../configuration/PhotonConfiguration.java | 6 +- .../common/hardware/Platform.java | 70 ++++++++++++++++--- 2 files changed, 65 insertions(+), 11 deletions(-) diff --git a/photon-core/src/main/java/org/photonvision/common/configuration/PhotonConfiguration.java b/photon-core/src/main/java/org/photonvision/common/configuration/PhotonConfiguration.java index 9453c53aa..74d67ec73 100644 --- a/photon-core/src/main/java/org/photonvision/common/configuration/PhotonConfiguration.java +++ b/photon-core/src/main/java/org/photonvision/common/configuration/PhotonConfiguration.java @@ -151,7 +151,11 @@ public class PhotonConfiguration { generalSubmap.put("availableModels", NeuralNetworkModelManager.getInstance().getModels()); generalSubmap.put( "supportedBackends", NeuralNetworkModelManager.getInstance().getSupportedBackends()); - generalSubmap.put("hardwareModel", hardwareConfig.deviceName); + generalSubmap.put( + "hardwareModel", + hardwareConfig.deviceName.isEmpty() + ? Platform.getHardwareModel() + : hardwareConfig.deviceName); generalSubmap.put("hardwarePlatform", Platform.getPlatformName()); settingsSubmap.put("general", generalSubmap); // AprilTagFieldLayout diff --git a/photon-targeting/src/main/java/org/photonvision/common/hardware/Platform.java b/photon-targeting/src/main/java/org/photonvision/common/hardware/Platform.java index fdb51c279..ea18baca7 100644 --- a/photon-targeting/src/main/java/org/photonvision/common/hardware/Platform.java +++ b/photon-targeting/src/main/java/org/photonvision/common/hardware/Platform.java @@ -22,37 +22,63 @@ import java.io.BufferedReader; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; +import java.util.function.Supplier; @SuppressWarnings("unused") public enum Platform { // WPILib Supported (JNI) - WINDOWS_64("Windows x64", "winx64", false, OSType.WINDOWS, true), - LINUX_32("Linux x86", "linuxx64", false, OSType.LINUX, true), - LINUX_64("Linux x64", "linuxx64", false, OSType.LINUX, true), + WINDOWS_64("Windows x64", Platform::getUnknownModel, "winx64", false, OSType.WINDOWS, true), + LINUX_32("Linux x86", Platform::getUnknownModel, "linuxx64", false, OSType.LINUX, true), + LINUX_64("Linux x64", Platform::getUnknownModel, "linuxx64", false, OSType.LINUX, true), LINUX_RASPBIAN32( "Linux Raspbian 32-bit", + Platform::getLinuxDeviceTreeModel, "linuxarm32", true, OSType.LINUX, true), // Raspberry Pi 3/4 with a 32-bit image LINUX_RASPBIAN64( "Linux Raspbian 64-bit", + Platform::getLinuxDeviceTreeModel, "linuxarm64", true, OSType.LINUX, true), // Raspberry Pi 3/4 with a 64-bit image - LINUX_RK3588_64("Linux AARCH 64-bit with RK3588", "linuxarm64", false, OSType.LINUX, true), + LINUX_RK3588_64( + "Linux AARCH 64-bit with RK3588", + Platform::getLinuxDeviceTreeModel, + "linuxarm64", + false, + OSType.LINUX, + true), LINUX_AARCH64( - "Linux AARCH64", "linuxarm64", false, OSType.LINUX, true), // Jetson Nano, Jetson TX2 + "Linux AARCH64", + Platform::getLinuxDeviceTreeModel, + "linuxarm64", + false, + OSType.LINUX, + true), // Jetson Nano, Jetson TX2 // PhotonVision Supported (Manual build/install) - LINUX_ARM64("Linux ARM64", "linuxarm64", false, OSType.LINUX, true), // ODROID C2, N2 + LINUX_ARM64( + "Linux ARM64", + Platform::getLinuxDeviceTreeModel, + "linuxarm64", + false, + OSType.LINUX, + true), // ODROID C2, N2 // Completely unsupported - WINDOWS_32("Windows x86", "windowsx64", false, OSType.WINDOWS, false), - MACOS("Mac OS", "osxuniversal", false, OSType.MACOS, false), - LINUX_ARM32("Linux ARM32", "linuxarm32", false, OSType.LINUX, false), // ODROID XU4, C1+ - UNKNOWN("Unsupported Platform", "", false, OSType.UNKNOWN, false); + WINDOWS_32("Windows x86", Platform::getUnknownModel, "windowsx64", false, OSType.WINDOWS, false), + MACOS("Mac OS", Platform::getUnknownModel, "osxuniversal", false, OSType.MACOS, false), + LINUX_ARM32( + "Linux ARM32", + Platform::getUnknownModel, + "linuxarm32", + false, + OSType.LINUX, + false), // ODROID XU4, C1+ + UNKNOWN("Unsupported Platform", Platform::getUnknownModel, "", false, OSType.UNKNOWN, false); public enum OSType { WINDOWS, @@ -62,6 +88,7 @@ public enum Platform { } public final String description; + public final String hardwareModel; public final String nativeLibraryFolderName; public final boolean isPi; public final OSType osType; @@ -72,11 +99,13 @@ public enum Platform { Platform( String description, + Supplier getHardwareModel, String nativeLibFolderName, boolean isPi, OSType osType, boolean isSupported) { this.description = description; + this.hardwareModel = getHardwareModel.get(); this.isPi = isPi; this.osType = osType; this.isSupported = isSupported; @@ -107,6 +136,10 @@ public enum Platform { } } + public static String getHardwareModel() { + return currentPlatform.hardwareModel; + } + public static String getNativeLibraryFolderName() { return currentPlatform.nativeLibraryFolderName; } @@ -122,6 +155,7 @@ public enum Platform { private static final String OS_ARCH = System.getProperty("os.arch"); private static final String UnknownPlatformString = String.format("Unknown Platform. OS: %s, Architecture: %s", OS_NAME, OS_ARCH); + private static final String UnknownDeviceModelString = "Unknown"; public static Platform getCurrentPlatform() { if (RuntimeDetector.isWindows()) { @@ -199,6 +233,22 @@ public enum Platform { return fileHasText("/proc/device-tree/model", "NVIDIA Jetson"); } + static String getLinuxDeviceTreeModel() { + var deviceTreeModelPath = Paths.get("/proc/device-tree/model"); + try { + if (Files.exists(deviceTreeModelPath)) { + return Files.readString(deviceTreeModelPath).trim(); + } + } catch (Exception ex) { + return UnknownDeviceModelString; + } + return UnknownDeviceModelString; + } + + static String getUnknownModel() { + return UnknownDeviceModelString; + } + // Checks for various names of linux OS private static boolean isStretch() { // TODO - this is a total guess