config: switch to postcard encoding

This commit is contained in:
Astro 2020-09-24 02:06:53 +02:00
parent 5d0d75d395
commit d40a038c2f
4 changed files with 48 additions and 27 deletions

51
Cargo.lock generated
View File

@ -174,10 +174,26 @@ dependencies = [
]
[[package]]
name = "half"
version = "1.6.0"
name = "hash32"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d36fab90f82edc3c747f9d438e06cf0a491055896f2a279638bb5beed6c40177"
checksum = "d4041af86e63ac4298ce40e5cca669066e75b6f1aa3390fe2561ffa5e1d9f4cc"
dependencies = [
"byteorder",
]
[[package]]
name = "heapless"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74911a68a1658cfcfb61bc0ccfbd536e3b6e906f8c2f7883ee50157e3e2184f1"
dependencies = [
"as-slice",
"generic-array 0.13.2",
"hash32",
"serde",
"stable_deref_trait",
]
[[package]]
name = "libm"
@ -257,6 +273,23 @@ dependencies = [
"cortex-m-semihosting",
]
[[package]]
name = "postcard"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3e3f5c2e9a91383c6594ec68aa2dfdfe19a3c86f34b088ba7203f2483d2682f"
dependencies = [
"heapless",
"postcard-cobs",
"serde",
]
[[package]]
name = "postcard-cobs"
version = "0.1.5-pre"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c68cb38ed13fd7bc9dd5db8f165b7c8d9c1a315104083a2b10f11354c2af97f"
[[package]]
name = "proc-macro2"
version = "1.0.20"
@ -320,16 +353,6 @@ dependencies = [
"serde_derive",
]
[[package]]
name = "serde_cbor"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e18acfa2f90e8b735b2836ab8d538de304cbb6729a7360729ea5a895d15a622"
dependencies = [
"half",
"serde",
]
[[package]]
name = "serde_derive"
version = "1.0.116"
@ -440,8 +463,8 @@ dependencies = [
"num-traits",
"panic-abort",
"panic-semihosting",
"postcard",
"serde",
"serde_cbor",
"smoltcp",
"stm32-eth",
"stm32f4xx-hal",

View File

@ -34,7 +34,7 @@ nb = "0.1"
uom = { version = "0.29", default-features = false, features = ["autoconvert", "si", "f64", "use_serde"] }
eeprom24x = "0.3"
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde_cbor = { version = "0.11.1", default-features = false }
postcard = "0.5.1"
[patch.crates-io]
stm32f4xx-hal = { git = "https://github.com/stm32-rs/stm32f4xx-hal.git" }

View File

@ -1,6 +1,5 @@
use serde::{Serialize, Deserialize};
use serde_cbor::Serializer;
use serde_cbor::ser::SliceWrite;
use postcard::to_slice;
use crate::{
channel_state::ChannelState,
channels::{CHANNELS, Channels},
@ -25,13 +24,12 @@ impl Config {
}
}
pub fn encode(&self, buffer: &mut [u8]) -> Result<usize, serde_cbor::Error> {
let writer = SliceWrite::new(buffer);
let mut ser = Serializer::new(writer);
self.serialize(&mut ser)?;
let writer = ser.into_inner();
let size = writer.bytes_written();
Ok(size)
pub fn encode<'a>(&self, buffer: &'a mut [u8]) -> Result<&'a mut [u8], postcard::Error> {
to_slice(self, buffer)
}
pub fn decode(buffer: &[u8]) -> Result<Self, postcard::Error> {
from_bytes(buffer)
}
}

View File

@ -432,10 +432,10 @@ fn main() -> ! {
Command::Load => {}
Command::Save => {
let config = Config::new(&mut channels);
let mut buf = [0; 0x200];
let mut buf = [0; 128];
match config.encode(&mut buf) {
Ok(len) => {
let _ = writeln!(socket, "Encoded: {:?}", &buf[..len]);
Ok(buf) => {
let _ = writeln!(socket, "Encoded {}: {:?}", buf.len(), buf);
}
Err(e) => {
let _ = writeln!(socket, "Error encoding configuration: {}", e);