From 9759a205ac35b003e3a5d81bb2704d5fd452cc08 Mon Sep 17 00:00:00 2001 From: Hugo Osvaldo Barrera Date: Fri, 22 May 2026 00:39:41 +0200 Subject: [PATCH] Auto-detect device from device tree Attempt to auto-detect the device by reading the device tree compatibility from the kernel. If this information is unavailable or the values are unknown, gracefully fall back to the current approach (using environment variables). I've only included the Clara HD for now: I don't have access to other devices in order to figure out which values they return, but I've left a clear place where they can be added progressively by others. --- crates/core/src/device.rs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/crates/core/src/device.rs b/crates/core/src/device.rs index 43cf197e..61a8707f 100644 --- a/crates/core/src/device.rs +++ b/crates/core/src/device.rs @@ -426,9 +426,37 @@ impl Device { } } +fn detect_product_from_devicetree() -> Option { + let paths = [ + "/proc/device-tree/compatible", + "/sys/firmware/devicetree/base/compatible", + ]; + + for path in &paths { + if let Ok(content) = std::fs::read_to_string(path) { + // Device tree compatible strings are null-separated. + for part in content.split('\0') { + if let Some(product) = part.strip_prefix("kobo,") { + // Map device tree identifier to product codename: + let codename = match product { + "clarahd" => "nova", + // TODO: add other devices here. + _ => continue, + }; + return Some(codename.to_string()); + } + } + } + } + None +} + lazy_static! { pub static ref CURRENT_DEVICE: Device = { - let product = env::var("PRODUCT").unwrap_or_default(); + let product = env::var("PRODUCT") + .ok() + .or_else(detect_product_from_devicetree) + .unwrap_or_default(); let model_number = env::var("MODEL_NUMBER").unwrap_or_default(); Device::new(&product, &model_number)