Small tweaks

This commit is contained in:
2023-09-16 03:58:06 +02:00
parent ad2ecdfa52
commit a81c17cbb1
3 changed files with 18 additions and 9 deletions

View File

@@ -30,7 +30,7 @@ pub use crate::error::Error;
#[derive(Serialize)]
#[serde(rename_all = "snake_case", tag = "status")]
enum Status<'a> {
pub enum Status<'a> {
Connected { version: &'a str },
Disconnected,
PreparingUpdate,
@@ -48,6 +48,9 @@ impl Status<'_> {
}
}
/// This is a wrapper around `BlockingFirmwareUpdater` that downloads signed updates
/// from a HTTPS url.
/// It also provides the current device status over MQTT
// TODO: Make this the owner of the blocking firmware updater
// TODO: When fixed, use the async firmware updater
pub struct Updater<'a, DFU, STATE>
@@ -59,7 +62,6 @@ where
updater: BlockingFirmwareUpdater<'a, DFU, STATE>,
topic_status: &'static str,
topic_update: &'static str,
version: &'static str,
public_key: &'static [u8],
}
@@ -70,22 +72,22 @@ where
STATE: NorFlash,
DFU::Error: Format,
{
/// Wrap the `BlockingFirmwareUpdater`
pub fn new(
updater: BlockingFirmwareUpdater<'a, DFU, STATE>,
topic_status: &'static str,
topic_update: &'static str,
version: &'static str,
public_key: &'static [u8],
) -> Self {
Self {
updater,
topic_status,
topic_update,
version,
public_key,
}
}
/// Set MQTT connection up to notify over MQTT when the device loses connection
pub fn add_will<const MAX_PROPERTIES: usize>(
&self,
config: &mut ClientConfig<'_, MAX_PROPERTIES, impl RngCore>,
@@ -94,6 +96,8 @@ where
config.add_will(self.topic_status, msg, true);
}
/// Mark the device is ready and booted, will notify over MQTT that the device is connected and the
/// currently running firmware version
pub async fn ready<const MAX_PROPERTIES: usize>(
&mut self,
client: &mut MqttClient<'_, impl Write + Read, MAX_PROPERTIES, impl RngCore>,
@@ -107,21 +111,20 @@ where
.send_message(self.topic_status, &status, QualityOfService::QoS1, true)
.await?;
client.subscribe_to_topic(self.topic_update).await?;
self.updater.mark_booted()?;
Ok(())
}
/// Download signed update from specified url and notify progress over MQTT
pub async fn update<const MAX_PROPERTIES: usize>(
&mut self,
url: Url<'_>,
stack: &'static Stack<impl Driver>,
rng: &mut (impl RngCore + CryptoRng),
client: &mut MqttClient<'_, impl Write + Read, MAX_PROPERTIES, impl RngCore>,
url: Url<'_>,
) -> Result<!, Error<DFU::Error>> {
let result = self._update(stack, rng, client, url).await;
let result = self._update(url, stack, rng, client).await;
if let Err(err) = &result {
let status = Status::UpdateFailed {
@@ -139,10 +142,10 @@ where
async fn _update<const MAX_PROPERTIES: usize>(
&mut self,
url: Url<'_>,
stack: &'static Stack<impl Driver>,
rng: &mut (impl RngCore + CryptoRng),
client: &mut MqttClient<'_, impl Write + Read, MAX_PROPERTIES, impl RngCore>,
url: Url<'_>,
) -> Result<!, Error<DFU::Error>> {
info!("Preparing for OTA...");
let status = Status::PreparingUpdate.json();