Skip to content

Commit

Permalink
Fix provisioning topic resulting in status code 143, by subscribing t…
Browse files Browse the repository at this point in the history
…o individual accepted and rejected topic for now
  • Loading branch information
MathiasKoch committed Jul 19, 2024
1 parent 604ca46 commit d6d4af3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 40 deletions.
12 changes: 10 additions & 2 deletions src/provisioning/error.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]

pub enum Error {
Overflow,
InvalidPayload,
InvalidState,
Mqtt,
DeserializeJson(serde_json_core::de::Error),
Mqtt(embedded_mqtt::Error),
DeserializeJson(#[cfg_attr(feature = "defmt", defmt(Debug2Format))] serde_json_core::de::Error),
DeserializeCbor,
CertificateStorage,
Response(u16),
Expand All @@ -27,3 +29,9 @@ impl From<serde_cbor::Error> for Error {
Self::DeserializeCbor
}
}

impl From<embedded_mqtt::Error> for Error {
fn from(e: embedded_mqtt::Error) -> Self {
Self::Mqtt(e)
}
}
86 changes: 48 additions & 38 deletions src/provisioning/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,7 @@ impl FleetProvisioner {
retain_as_published: false,
retain_handling: RetainHandling::SendAtSubscribeTime,
}]))
.await
.map_err(|e| {
error!("Failed subscription to RegisterThingAny! {:?}", e);
Error::Mqtt
})?;
.await?;

mqtt.publish(Publish {
dup: false,
Expand All @@ -249,11 +245,7 @@ impl FleetProvisioner {
payload,
properties: embedded_mqtt::Properties::Slice(&[]),
})
.await
.map_err(|e| {
error!("Failed publish to RegisterThing! {:?}", e);
Error::Mqtt
})?;
.await?;

drop(message);
drop(create_subscription);
Expand Down Expand Up @@ -288,20 +280,30 @@ impl FleetProvisioner {
mqtt: &'b embedded_mqtt::MqttClient<'a, M, SUBS>,
csr: Option<&str>,
payload_format: PayloadFormat,
) -> Result<Subscription<'a, 'b, M, SUBS, 1>, Error> {
) -> Result<Subscription<'a, 'b, M, SUBS, 2>, Error> {
if let Some(csr) = csr {
let subscription = mqtt
.subscribe::<1>(Subscribe::new(&[SubscribeTopic {
topic_path: Topic::CreateCertificateFromCsrAccepted(payload_format)
.format::<47>()?
.as_str(),
maximum_qos: QoS::AtLeastOnce,
no_local: false,
retain_as_published: false,
retain_handling: RetainHandling::SendAtSubscribeTime,
}]))
.await
.map_err(|_| Error::Mqtt)?;
.subscribe(Subscribe::new(&[
SubscribeTopic {
topic_path: Topic::CreateCertificateFromCsrRejected(payload_format)
.format::<47>()?
.as_str(),
maximum_qos: QoS::AtLeastOnce,
no_local: false,
retain_as_published: false,
retain_handling: RetainHandling::SendAtSubscribeTime,
},
SubscribeTopic {
topic_path: Topic::CreateCertificateFromCsrAccepted(payload_format)
.format::<47>()?
.as_str(),
maximum_qos: QoS::AtLeastOnce,
no_local: false,
retain_as_published: false,
retain_handling: RetainHandling::SendAtSubscribeTime,
},
]))
.await?;

let request = CreateCertificateFromCsrRequest {
certificate_signing_request: csr,
Expand Down Expand Up @@ -333,28 +335,37 @@ impl FleetProvisioner {
retain: false,
pid: None,
topic_name: Topic::CreateCertificateFromCsr(payload_format)
.format::<38>()?
.format::<40>()?
.as_str(),
payload,
properties: embedded_mqtt::Properties::Slice(&[]),
})
.await
.map_err(|_| Error::Mqtt)?;
.await?;

Ok(subscription)
} else {
let subscription = mqtt
.subscribe::<1>(Subscribe::new(&[SubscribeTopic {
topic_path: Topic::CreateKeysAndCertificateAny(payload_format)
.format::<31>()?
.as_str(),
maximum_qos: QoS::AtLeastOnce,
no_local: false,
retain_as_published: false,
retain_handling: RetainHandling::SendAtSubscribeTime,
}]))
.await
.map_err(|_| Error::Mqtt)?;
.subscribe(Subscribe::new(&[
SubscribeTopic {
topic_path: Topic::CreateKeysAndCertificateAccepted(payload_format)
.format::<38>()?
.as_str(),
maximum_qos: QoS::AtLeastOnce,
no_local: false,
retain_as_published: false,
retain_handling: RetainHandling::SendAtSubscribeTime,
},
SubscribeTopic {
topic_path: Topic::CreateKeysAndCertificateRejected(payload_format)
.format::<38>()?
.as_str(),
maximum_qos: QoS::AtLeastOnce,
no_local: false,
retain_as_published: false,
retain_handling: RetainHandling::SendAtSubscribeTime,
},
]))
.await?;

mqtt.publish(Publish {
dup: false,
Expand All @@ -367,8 +378,7 @@ impl FleetProvisioner {
payload: b"",
properties: embedded_mqtt::Properties::Slice(&[]),
})
.await
.map_err(|_| Error::Mqtt)?;
.await?;

Ok(subscription)
}
Expand Down

0 comments on commit d6d4af3

Please sign in to comment.