aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock38
-rw-r--r--Cargo.toml9
-rw-r--r--src/commands.rs77
3 files changed, 61 insertions, 63 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 5437476..a729af6 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -60,6 +60,11 @@ dependencies = [
]
[[package]]
+name = "encode_unicode"
+version = "0.3.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
name = "nb"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -72,6 +77,8 @@ dependencies = [
"cortex-m-rt 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)",
"embedded-hal 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"panic-halt 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)",
+ "ssmarshal 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"stm32f103xx-usb 0.1.0 (git+https://github.com/mvirkkunen/stm32f103xx-usb?rev=57d23751367461bec5f39322727bdd65e5c2aa30)",
"stm32f1xx-hal 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"usb-device 0.2.0 (git+https://github.com/mvirkkunen/usb-device?rev=e58e30f3b9c9bf4aab00ea039d129b964a3fd2d3)",
@@ -146,6 +153,33 @@ version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
+name = "serde"
+version = "1.0.88"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "serde_derive 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "serde_derive"
+version = "1.0.88"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
+ "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
+ "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "ssmarshal"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "encode_unicode 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "stm32f1"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -228,6 +262,7 @@ dependencies = [
"checksum cortex-m-rt 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)" = "f69d2beca37acc3776c17201c9d1f8904fb9139fa3a4d2cf28c8436a07b21a88"
"checksum cortex-m-rt-macros 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "d7ae692573e0acccb1579fef1abf5a5bf1d2f3f0149a22b16870ec9309aee25f"
"checksum embedded-hal 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9880e55238830314d41d88f1ac7a819d495799c3cc3bc392cc172bab26428c33"
+"checksum encode_unicode 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "90b2c9496c001e8cb61827acdefad780795c42264c137744cae6f7d9e3450abd"
"checksum nb 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "69f380b5fe9fab8c0d7a6a99cda23e2cc0463bedb2cbc3aada0813b98496ecdc"
"checksum panic-halt 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "de96540e0ebde571dc55c73d60ef407c653844e6f9a1e2fdbd40c07b9252d812"
"checksum proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)" = "4d317f9caece796be1980837fd5cb3dfec5613ebdb04ad0956deea83ce168915"
@@ -239,6 +274,9 @@ dependencies = [
"checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
"checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
"checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
+"checksum serde 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)" = "9f301d728f2b94c9a7691c90f07b0b4e8a4517181d9461be94c04bddeb4bd850"
+"checksum serde_derive 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)" = "beed18e6f5175aef3ba670e57c60ef3b1b74d250d962a26604bff4c80e970dd4"
+"checksum ssmarshal 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f3e6ad23b128192ed337dfa4f1b8099ced0c2bf30d61e551b65fda5916dbb850"
"checksum stm32f1 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "48763f6f093674476399efbd8d2e52349dc32c3bbf57657234cbb15d309cd8a3"
"checksum stm32f103xx-usb 0.1.0 (git+https://github.com/mvirkkunen/stm32f103xx-usb?rev=57d23751367461bec5f39322727bdd65e5c2aa30)" = "<none>"
"checksum stm32f1xx-hal 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f410543fc282d51e181e3cf5c8adff303d9e5ae8b9eb731e6a07f58a4cec4265"
diff --git a/Cargo.toml b/Cargo.toml
index e3614d5..a11296e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -22,6 +22,15 @@ package = "stm32f1xx-hal"
version = "0.1.1"
features = ["stm32f103", "rt"]
+[dependencies.serde]
+version = "1.0"
+default-features = false
+features = ["derive"]
+
+[dependencies.ssmarshal]
+version = "1.0"
+default-features = false
+
[profile.release]
codegen-units = 1
debug = true
diff --git a/src/commands.rs b/src/commands.rs
index 62e2983..d2769ed 100644
--- a/src/commands.rs
+++ b/src/commands.rs
@@ -2,7 +2,9 @@
// SPDX-License-Identifier: GPL-3.0-or-later
use core::default::Default;
-use core::marker::Sized;
+
+use serde::{Deserialize, Serialize};
+use serde::de::DeserializeOwned;
use crate::device::CommandStatus;
@@ -16,32 +18,21 @@ enum_cmd! {
}
}
-trait RequestData: Sized {
- fn from_bytes(data: &[u8]) -> Option<Self>;
-}
-
-impl RequestData for () {
- fn from_bytes(_data: &[u8]) -> Option<Self> {
- Some(())
- }
-}
-
-trait ResponseData {
- fn write_bytes(&self, buf: &mut [u8]);
-}
-
trait Command {
- type Request: RequestData;
- type Response: ResponseData;
+ type Request: DeserializeOwned;
+ type Response: Serialize;
fn execute(data: Self::Request) -> Result<Self::Response, CommandStatus>;
fn execute_raw(data: &[u8], buf: &mut [u8]) -> CommandStatus {
- if let Some(request) = Self::Request::from_bytes(data) {
+ // TODO: better error if (de-)serialization fails
+ if let Ok((request, _)) = ssmarshal::deserialize::<Self::Request>(data) {
match Self::execute(request) {
Ok(response) => {
- response.write_bytes(buf);
- CommandStatus::Ok
+ match ssmarshal::serialize(buf, &response) {
+ Ok(_) => CommandStatus::Ok,
+ Err(_) => CommandStatus::NotSupported,
+ }
}
Err(status) => status,
}
@@ -51,7 +42,7 @@ trait Command {
}
}
-#[derive(Debug, Default)]
+#[derive(Debug, Default, Serialize)]
struct GetStatusResponse {
firmware_version_minor: u8,
firmware_version_major: u8,
@@ -60,27 +51,6 @@ struct GetStatusResponse {
config_capslock: u8,
config_enable_user_password: u8,
config_delete_user_password: u8,
- buf: [u8; 10],
-}
-
-impl ResponseData for GetStatusResponse {
- fn write_bytes(&self, buf: &mut [u8]) {
- let card_serial = self.card_serial.to_le_bytes();
- let data = &[
- self.firmware_version_minor,
- self.firmware_version_major,
- card_serial[0],
- card_serial[1],
- card_serial[2],
- card_serial[3],
- self.config_numlock,
- self.config_capslock,
- self.config_enable_user_password,
- self.config_delete_user_password,
- ];
- assert!(buf.len() >= data.len());
- buf[..data.len()].copy_from_slice(data);
- }
}
#[derive(Debug, Default)]
@@ -97,35 +67,16 @@ impl Command for GetStatusCommand {
}
}
-#[derive(Debug, Default)]
+#[derive(Debug, Default, Deserialize)]
struct ReadSlotNameRequest {
internal_slot_number: u8,
}
-impl RequestData for ReadSlotNameRequest {
- fn from_bytes(data: &[u8]) -> Option<Self> {
- if data.is_empty() {
- None
- } else {
- Some(ReadSlotNameRequest {
- internal_slot_number: data[0],
- })
- }
- }
-}
-
-#[derive(Debug, Default)]
+#[derive(Debug, Default, Serialize)]
struct ReadSlotNameResponse {
slot_name: [u8; 15],
}
-impl ResponseData for ReadSlotNameResponse {
- fn write_bytes(&self, buf: &mut [u8]) {
- assert!(buf.len() >= self.slot_name.len());
- buf[..self.slot_name.len()].copy_from_slice(&self.slot_name)
- }
-}
-
#[derive(Debug, Default)]
struct ReadSlotNameCommand {}