From dbb2c7747a6a88730f20a80261f5d0f76e2fbd4b Mon Sep 17 00:00:00 2001 From: Kenneth Knudsen Date: Tue, 5 Nov 2024 15:39:26 +0100 Subject: [PATCH 1/2] Replace serde_cbor with minicbor --- Cargo.toml | 12 ++++++++---- src/lib.rs | 2 -- src/ota/data_interface/mqtt.rs | 2 +- src/ota/encoding/cbor.rs | 6 +++--- src/provisioning/error.rs | 4 ++-- src/provisioning/mod.rs | 15 ++++++--------- src/shadows/dao.rs | 15 ++++++--------- 7 files changed, 26 insertions(+), 30 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9980820..7fc3caa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,10 @@ maintenance = { status = "actively-developed" } bitmaps = { version = "3.1", default-features = false } heapless = { version = "0.8", features = ["serde"] } serde = { version = "1.0", default-features = false, features = ["derive"] } -serde_cbor = { version = "0.11", default-features = false, optional = true } + +minicbor-serde = { version = "0.3.2", optional = true } + + serde-json-core = { version = "0.6" } shadow-derive = { path = "shadow_derive", version = "0.2.1" } embedded-storage-async = "0.4" @@ -69,12 +72,13 @@ hex = { version = "0.4.3", features = ["alloc"] } [features] default = ["ota_mqtt_data", "provision_cbor"] -provision_cbor = ["serde_cbor"] +provision_cbor = ["minicbor-serde"] + +ota_mqtt_data = ["minicbor-serde"] -ota_mqtt_data = ["serde_cbor"] ota_http_data = [] -std = ["serde/std", "serde_cbor?/std"] +std = ["serde/std", "minicbor-serde/std"] defmt = [ "dep:defmt", diff --git a/src/lib.rs b/src/lib.rs index ca160a6..f5d758f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,5 +11,3 @@ pub mod jobs; pub mod ota; pub mod provisioning; pub mod shadows; - -pub use serde_cbor; diff --git a/src/ota/data_interface/mqtt.rs b/src/ota/data_interface/mqtt.rs index b6516d1..4afc2fb 100644 --- a/src/ota/data_interface/mqtt.rs +++ b/src/ota/data_interface/mqtt.rs @@ -209,7 +209,7 @@ impl<'a, M: RawMutex> DataInterface for MqttClient<'a, M> { /// Decode a cbor encoded fileblock received from streaming service fn decode_file_block<'c>(&self, payload: &'c mut [u8]) -> Result, OtaError> { Ok( - serde_cbor::de::from_mut_slice::(payload) + minicbor_serde::from_slice::(payload) .map_err(|_| OtaError::Encoding)? .into(), ) diff --git a/src/ota/encoding/cbor.rs b/src/ota/encoding/cbor.rs index a30f45e..da371d2 100644 --- a/src/ota/encoding/cbor.rs +++ b/src/ota/encoding/cbor.rs @@ -76,9 +76,9 @@ pub fn to_slice(value: &T, slice: &mut [u8]) -> Result where T: serde::ser::Serialize, { - let mut serializer = serde_cbor::ser::Serializer::new(serde_cbor::ser::SliceWrite::new(slice)); + let mut serializer = minicbor_serde::Serializer::new(slice); value.serialize(&mut serializer).map_err(|_| ())?; - Ok(serializer.into_inner().bytes_written()) + Ok(serializer.into_encoder().writer().len()) } impl<'a> From> for FileBlock<'a> { @@ -170,7 +170,7 @@ mod test { 0, 0, 0, 0, 0, 0, 255, ]; - let response: GetStreamResponse = serde_cbor::de::from_mut_slice(payload).unwrap(); + let response: GetStreamResponse = minicbor_serde::from_slice(payload).unwrap(); assert_eq!( response, diff --git a/src/provisioning/error.rs b/src/provisioning/error.rs index 67f4696..20961fb 100644 --- a/src/provisioning/error.rs +++ b/src/provisioning/error.rs @@ -24,8 +24,8 @@ impl From for Error { } } -impl From for Error { - fn from(_e: serde_cbor::Error) -> Self { +impl From for Error { + fn from(_e: minicbor_serde::error::DecodeError) -> Self { Self::DeserializeCbor } } diff --git a/src/provisioning/mod.rs b/src/provisioning/mod.rs index 0a27f83..3797208 100644 --- a/src/provisioning/mod.rs +++ b/src/provisioning/mod.rs @@ -206,12 +206,11 @@ impl FleetProvisioner { Ok(match payload_format { #[cfg(feature = "provision_cbor")] PayloadFormat::Cbor => { - let mut serializer = - serde_cbor::ser::Serializer::new(serde_cbor::ser::SliceWrite::new(buf)); + let mut serializer = minicbor_serde::Serializer::new(buf); register_request .serialize(&mut serializer) .map_err(|_| EncodingError::BufferSize)?; - serializer.into_inner().bytes_written() + serializer.into_encoder().writer().len() } PayloadFormat::Json => serde_json_core::to_slice(®ister_request, buf) .map_err(|_| EncodingError::BufferSize)?, @@ -318,13 +317,11 @@ impl FleetProvisioner { Ok(match payload_format { #[cfg(feature = "provision_cbor")] PayloadFormat::Cbor => { - let mut serializer = serde_cbor::ser::Serializer::new( - serde_cbor::ser::SliceWrite::new(buf), - ); + let mut serializer = minicbor_serde::Serializer::new(buf); request .serialize(&mut serializer) .map_err(|_| EncodingError::BufferSize)?; - serializer.into_inner().bytes_written() + serializer.into_encoder().writer().len() } PayloadFormat::Json => serde_json_core::to_slice(&request, buf) .map_err(|_| EncodingError::BufferSize)?, @@ -398,7 +395,7 @@ impl FleetProvisioner { Ok(match payload_format { #[cfg(feature = "provision_cbor")] - PayloadFormat::Cbor => serde_cbor::de::from_mut_slice::(message.payload_mut())?, + PayloadFormat::Cbor => minicbor_serde::from_slice::(message.payload_mut())?, PayloadFormat::Json => serde_json_core::from_slice::(message.payload())?.0, }) } @@ -412,7 +409,7 @@ impl FleetProvisioner { let response = match format { #[cfg(feature = "provision_cbor")] PayloadFormat::Cbor => { - serde_cbor::de::from_mut_slice::(message.payload_mut())? + minicbor_serde::from_slice::(message.payload_mut())? } PayloadFormat::Json => { serde_json_core::from_slice::(message.payload())?.0 diff --git a/src/shadows/dao.rs b/src/shadows/dao.rs index 1435cd6..abbb85f 100644 --- a/src/shadows/dao.rs +++ b/src/shadows/dao.rs @@ -28,10 +28,8 @@ where } Ok( - serde_cbor::de::from_mut_slice::( - &mut buf[U32_SIZE..len as usize + U32_SIZE], - ) - .map_err(|_| Error::InvalidPayload)?, + minicbor_serde::from_slice::(&mut buf[U32_SIZE..len as usize + U32_SIZE]) + .map_err(|_| Error::InvalidPayload)?, ) } _ => Err(Error::InvalidPayload), @@ -43,14 +41,13 @@ where let buf = &mut [0u8; S::MAX_PAYLOAD_SIZE + U32_SIZE]; - let mut serializer = serde_cbor::ser::Serializer::new(serde_cbor::ser::SliceWrite::new( - &mut buf[U32_SIZE..], - )) - .packed_format(); + let mut serializer = minicbor_serde::Serializer::new(&mut buf[U32_SIZE..]); + state .serialize(&mut serializer) .map_err(|_| Error::InvalidPayload)?; - let len = serializer.into_inner().bytes_written(); + + let len = serializer.into_encoder().writer().len(); if len > S::MAX_PAYLOAD_SIZE { return Err(Error::Overflow); From 8656eb16fc2307424f2c0add71c6342d971b3480 Mon Sep 17 00:00:00 2001 From: Mathias Date: Wed, 6 Nov 2024 10:52:21 +0100 Subject: [PATCH 2/2] Add cursor around cbor writer to get write position back --- Cargo.toml | 7 ++++--- src/ota/encoding/cbor.rs | 5 +++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7fc3caa..862531f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ bitmaps = { version = "3.1", default-features = false } heapless = { version = "0.8", features = ["serde"] } serde = { version = "1.0", default-features = false, features = ["derive"] } +minicbor = { version = "0.25", optional = true } minicbor-serde = { version = "0.3.2", optional = true } @@ -72,13 +73,13 @@ hex = { version = "0.4.3", features = ["alloc"] } [features] default = ["ota_mqtt_data", "provision_cbor"] -provision_cbor = ["minicbor-serde"] +provision_cbor = ["dep:minicbor", "dep:minicbor-serde"] -ota_mqtt_data = ["minicbor-serde"] +ota_mqtt_data = ["dep:minicbor", "dep:minicbor-serde"] ota_http_data = [] -std = ["serde/std", "minicbor-serde/std"] +std = ["serde/std", "minicbor-serde?/std"] defmt = [ "dep:defmt", diff --git a/src/ota/encoding/cbor.rs b/src/ota/encoding/cbor.rs index da371d2..ad0f124 100644 --- a/src/ota/encoding/cbor.rs +++ b/src/ota/encoding/cbor.rs @@ -76,9 +76,10 @@ pub fn to_slice(value: &T, slice: &mut [u8]) -> Result where T: serde::ser::Serialize, { - let mut serializer = minicbor_serde::Serializer::new(slice); + let mut serializer = + minicbor_serde::Serializer::new(minicbor::encode::write::Cursor::new(slice)); value.serialize(&mut serializer).map_err(|_| ())?; - Ok(serializer.into_encoder().writer().len()) + Ok(serializer.into_encoder().writer().position()) } impl<'a> From> for FileBlock<'a> {