Skip to content

Commit

Permalink
feat: allow the use of a uuid urn as device ID in TD
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Nov 24, 2024
1 parent ec6fb75 commit 927ac01
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 21 additions & 3 deletions src/bin/async_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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)]
Expand All @@ -48,6 +53,8 @@ type AppRouter = impl picoserve::routing::PathRouter<AppState>;

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,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -173,11 +186,16 @@ async fn main(spawner: Spawner) {

let sht = mk_static!(ShtCx<Sht2Gen, &'static mut I2c<'static, Blocking, AnyI2c>>, 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"))
Expand Down

0 comments on commit 927ac01

Please sign in to comment.