1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
// Copyright 2019 Robin Krahl <robin.krahl@ireas.org>
// SPDX-License-Identifier: GPL-3.0-or-later
use core::default::Default;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use crate::device::CommandStatus;
pub const COMMAND_LEN: usize = 53;
enum_cmd! {
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum CommandId {
GetStatus(GetStatusCommand) = 0,
ReadSlotName(ReadSlotNameCommand) = 2,
}
}
trait Command {
type Request: DeserializeOwned;
type Response: Serialize;
fn execute(data: Self::Request) -> Result<Self::Response, CommandStatus>;
}
#[derive(Debug, Default, Serialize)]
struct GetStatusResponse {
firmware_version_minor: u8,
firmware_version_major: u8,
card_serial: u32,
config_numlock: u8,
config_capslock: u8,
config_enable_user_password: u8,
config_delete_user_password: u8,
}
#[derive(Debug, Default)]
struct GetStatusCommand {}
impl Command for GetStatusCommand {
type Request = ();
type Response = GetStatusResponse;
fn execute(_data: Self::Request) -> Result<Self::Response, CommandStatus> {
let mut response: Self::Response = Default::default();
response.firmware_version_minor = 1;
Ok(response)
}
}
#[derive(Debug, Default, Deserialize)]
struct ReadSlotNameRequest {
internal_slot_number: u8,
}
#[derive(Debug, Default, Serialize)]
struct ReadSlotNameResponse {
slot_name: [u8; 15],
}
#[derive(Debug, Default)]
struct ReadSlotNameCommand {}
impl Command for ReadSlotNameCommand {
type Request = ReadSlotNameRequest;
type Response = ReadSlotNameResponse;
fn execute(data: Self::Request) -> Result<Self::Response, CommandStatus> {
if data.internal_slot_number != 0x20 {
Err(CommandStatus::SlotNotProgrammed)
} else {
let mut response: Self::Response = Default::default();
response.slot_name[0] = 0x74; // t
response.slot_name[1] = 0x65; // e
response.slot_name[2] = 0x73; // s
response.slot_name[3] = 0x74; // t
Ok(response)
}
}
}
fn execute<C: Command>(data: &[u8], buf: &mut [u8]) -> CommandStatus {
// TODO: better error if (de-)serialization fails
if let Ok((request, _)) = ssmarshal::deserialize::<C::Request>(data) {
match C::execute(request) {
Ok(response) => match ssmarshal::serialize(buf, &response) {
Ok(_) => CommandStatus::Ok,
Err(_) => CommandStatus::NotSupported,
},
Err(status) => status,
}
} else {
CommandStatus::NotSupported
}
}
|