Skip to content

Commit

Permalink
client: Add IoTHubClientBuilder::pnp_model_id()
Browse files Browse the repository at this point in the history
The idea with PNP is that connecting devices can announce a model ID
which should be a valid DTMI when connecting. This functionality is only
really supported for MQTT.

The IoTHubClientBuilder accepts a str that it attempts to turn into a
Dtmi instance which can only be constructed if it passes validation.
  • Loading branch information
laumann committed Oct 6, 2021
1 parent 05226a6 commit f9d3f5d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
24 changes: 21 additions & 3 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::dtmi::Dtmi;
use crate::message::Message;
#[cfg(feature = "direct-methods")]
use crate::DirectMethodResponse;
Expand Down Expand Up @@ -91,6 +92,7 @@ impl IoTHubClient {
device_id,
token_source,
root_ca: None,
pnp_model_id: None,
}
}

Expand Down Expand Up @@ -207,6 +209,7 @@ pub struct IoTHubClientBuilder<TS> {
device_id: String,
token_source: TS,
root_ca: Option<native_tls::Certificate>,
pnp_model_id: Option<Dtmi>,
}

impl<TS: TokenSource + Send + Sync + Clone + 'static> IoTHubClientBuilder<TS> {
Expand All @@ -219,12 +222,18 @@ impl<TS: TokenSource + Send + Sync + Clone + 'static> IoTHubClientBuilder<TS> {
device_id,
token_source,
root_ca,
pnp_model_id,
} = self;

#[cfg(not(feature = "https-transport"))]
let transport =
ClientTransport::new(&hub_name, device_id.clone(), token_source.clone(), root_ca)
.await?;
let transport = ClientTransport::new(
&hub_name,
device_id.clone(),
token_source.clone(),
root_ca,
pnp_model_id,
)
.await?;
#[cfg(feature = "https-transport")]
let transport =
ClientTransport::new(&hub_name, device_id.clone(), token_source.clone()).await?;
Expand All @@ -243,4 +252,13 @@ impl<TS: TokenSource + Send + Sync + Clone + 'static> IoTHubClientBuilder<TS> {
self.root_ca = Some(root_ca);
Ok(self)
}

/// Provide a PNP model ID
///
/// Is only use with MQTT transport
pub fn pnp_model_id(mut self, pnp_model_id: impl AsRef<str>) -> crate::Result<Self> {
let dtmi = pnp_model_id.as_ref().parse()?;
self.pnp_model_id = Some(dtmi);
Ok(self)
}
}
11 changes: 10 additions & 1 deletion src/mqtt_transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use tokio_native_tls::{TlsConnector, TlsStream};

use async_trait::async_trait;

use crate::dtmi::Dtmi;
use crate::message::Message;
#[cfg(any(
feature = "direct-methods",
Expand Down Expand Up @@ -204,11 +205,19 @@ impl MqttTransport {
device_id: String,
token_source: TS,
root_ca: Option<native_tls::Certificate>,
pnp_model_id: Option<Dtmi>,
) -> crate::Result<Self>
where
TS: TokenSource + Send + Sync + 'static,
{
let user_name = format!("{}/{}/?api-version=2018-06-30", hub_name, device_id);
let user_name = if let Some(Dtmi(model_id)) = pnp_model_id {
format!(
"{}/{}/?api-version=2020-09-30&model-id={}",
hub_name, device_id, model_id
)
} else {
format!("{}/{}/?api-version=2018-06-30", hub_name, device_id)
};

let expiry = Utc::now() + Duration::days(1);
trace!("Generating token that will expire at {}", expiry);
Expand Down

0 comments on commit f9d3f5d

Please sign in to comment.