Skip to content

Commit

Permalink
fixup! feat: use 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 17fa031 commit ea8110d
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/bin/async_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

extern crate alloc;

use alloc::{borrow::ToOwned, format, string::String};
use alloc::{
format,
string::{String, ToString},
};
use embassy_executor::Spawner;
use embassy_net::{Stack, StackResources};
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, mutex::Mutex};
Expand All @@ -27,13 +30,13 @@ use esp_wifi::{
},
EspWifiController,
};
use nuuid::Uuid;
use picoserve::{
extract::State,
response::{Response, StatusCode},
routing::get,
};
use shtcx::{self, sensor_class::Sht2Gen, shtc3, PowerMode, ShtCx};
use uuid::Builder;
use wot_td::{builder::*, Thing};

#[derive(Clone, Copy)]
Expand Down Expand Up @@ -87,19 +90,15 @@ macro_rules! mk_static {
const SSID: &str = env!("SSID");
const PASSWORD: &str = env!("PASSWORD");

fn convert_integer_buffer_to_bytes(data: [u32; 8]) -> [u8; 32] {
fn convert_integer_buffer_to_bytes(data: [u32; 4]) -> [u8; 16] {
unsafe { core::mem::transmute(data) }
}

fn generate_uuid_urn(rng: &mut Rng) -> alloc::string::String {
let mut uuid_buffer: [u8; 45] = [0; 45];
let seed: [u8; 32] = convert_integer_buffer_to_bytes([(); 8].map(|()| rng.random()));

let mut uuid_rng = nuuid::Rng::from_seed(seed);
let uuid = Uuid::new_v4_rng(&mut uuid_rng);
let random_bytes: [u8; 16] = convert_integer_buffer_to_bytes([(); 4].map(|()| rng.random()));
let uuid = Builder::from_random_bytes(random_bytes).into_uuid();

uuid.to_urn(&mut uuid_buffer);
core::str::from_utf8(&uuid_buffer).unwrap().to_owned()
uuid.urn().to_string()
}

#[esp_hal_embassy::main]
Expand Down

0 comments on commit ea8110d

Please sign in to comment.