From bd3fad0dd783da7b76f378f8a9edfb6e1514c7ea Mon Sep 17 00:00:00 2001 From: Jan Romann Date: Sun, 24 Nov 2024 14:28:49 +0100 Subject: [PATCH 1/3] chore(deps): add uuid crate as dependency --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index ae75b85..a765762 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,6 +46,7 @@ picoserve = { version = "0.12.2", features = ["embassy", "alloc"] } embassy-sync = "0.6.0" wot-td = { version = "0.6.2", default-features = false, features = ["alloc"] } serde_json = { version = "1.0.133", default-features = false, features = ["alloc"] } +uuid = { version = "1.11.0", default-features = false } [profile.dev] # Rust debug is too slow. From d807a418734a7ffa6ed8a6688d6fa6e3a87c4aa0 Mon Sep 17 00:00:00 2001 From: Jan Romann Date: Sun, 24 Nov 2024 16:21:57 +0100 Subject: [PATCH 2/3] chore(deps): add const-random crate as a dependency --- Cargo.toml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index a765762..d754c52 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,6 +47,11 @@ embassy-sync = "0.6.0" wot-td = { version = "0.6.2", default-features = false, features = ["alloc"] } serde_json = { version = "1.0.133", default-features = false, features = ["alloc"] } uuid = { version = "1.11.0", default-features = false } +const-random = "0.1.15" + +[patch.crates-io] +# TODO: Remove this override once https://github.com/tkaitchuck/constrandom/issues/36 has been resolved +const-random-macro = { git = 'https://github.com/tkaitchuck/constrandom.git', rev = "4f71cb510e77eb6a26f8c7296c17811d0416fd41"} [profile.dev] # Rust debug is too slow. From 76dbd256b935c1ae5646f24695c9c20c9beaaa72 Mon Sep 17 00:00:00 2001 From: Jan Romann Date: Sun, 24 Nov 2024 16:45:00 +0100 Subject: [PATCH 3/3] feat: allow the use of a uuid urn as device ID in TD --- Cargo.toml | 4 ++++ src/bin/thermometer.rs | 24 +++++++++++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d754c52..c8a15af 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,6 +53,10 @@ const-random = "0.1.15" # TODO: Remove this override once https://github.com/tkaitchuck/constrandom/issues/36 has been resolved const-random-macro = { git = 'https://github.com/tkaitchuck/constrandom.git', rev = "4f71cb510e77eb6a26f8c7296c17811d0416fd41"} +[features] +default = ["uuid-id"] +uuid-id = [] + [profile.dev] # Rust debug is too slow. # For debug builds always builds with some optimization diff --git a/src/bin/thermometer.rs b/src/bin/thermometer.rs index ddaeb6f..c1711e5 100644 --- a/src/bin/thermometer.rs +++ b/src/bin/thermometer.rs @@ -4,7 +4,11 @@ extern crate alloc; -use alloc::{format, string::String}; +use alloc::{ + format, + string::{String, ToString}, +}; +use const_random::const_random; use embassy_executor::Spawner; use embassy_net::{Stack, StackResources}; use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, mutex::Mutex}; @@ -33,6 +37,7 @@ use picoserve::{ routing::get, }; use shtcx::{self, sensor_class::Sht2Gen, shtc3, PowerMode, ShtCx}; +use uuid::Builder; use wot_td::{builder::*, Thing}; #[derive(Clone, Copy)] @@ -48,6 +53,8 @@ type AppRouter = impl picoserve::routing::PathRouter; const WEB_TASK_POOL_SIZE: usize = 1; +const UUID_SEED: [u8; 16] = const_random!([u8; 16]); + #[embassy_executor::task(pool_size = WEB_TASK_POOL_SIZE)] async fn web_task( id: usize, @@ -86,6 +93,12 @@ macro_rules! mk_static { const SSID: &str = env!("SSID"); const PASSWORD: &str = env!("PASSWORD"); +fn generate_uuid_urn() -> alloc::string::String { + let uuid = Builder::from_random_bytes(UUID_SEED).into_uuid(); + + uuid.urn().to_string() +} + #[esp_hal_embassy::main] async fn main(spawner: Spawner) { esp_println::logger::init_logger_from_env(); @@ -173,11 +186,16 @@ async fn main(spawner: Spawner) { let sht = mk_static!(ShtCx>, shtc3(i2c)); - let device_id = stack.hardware_address(); + let id = if cfg!(feature = "uuid-id") { + generate_uuid_urn() + } else { + let device_id = stack.hardware_address().to_string(); + format!("urn:example/shtc3/{device_id}") + }; let td = Thing::builder("shtc3") .finish_extend() - .id(format!("urn:example/shtc3/{device_id}")) + .id(id) .base(base_uri) .description("Example Thing exposing a shtc3 sensor") .security(|builder| builder.no_sec().required().with_key("nosec_sc"))