trussed_se050_manage/
lib.rs

1#![no_std]
2
3use serde::{Deserialize, Serialize};
4use trussed_core::{
5    serde_extensions::{Extension, ExtensionClient, ExtensionResult},
6    types::Bytes,
7    Error,
8};
9
10#[derive(Debug, Default)]
11pub struct Se050ManageExtension;
12
13/// Request information regarding the SE050
14#[derive(Debug, Deserialize, Serialize, Copy, Clone)]
15pub struct InfoRequest;
16
17/// Test SE050 functionality
18///
19/// This is now a placeholder for the previous test. It is kept to return available space on the SE050
20#[derive(Debug, Deserialize, Serialize, Copy, Clone)]
21pub struct TestSe050Request;
22
23#[allow(clippy::large_enum_variant)]
24#[derive(Debug, Deserialize, Serialize)]
25pub enum Se050ManageRequest {
26    Info(InfoRequest),
27    TestSe050(TestSe050Request),
28}
29
30impl TryFrom<Se050ManageRequest> for InfoRequest {
31    type Error = Error;
32    fn try_from(request: Se050ManageRequest) -> Result<Self, Self::Error> {
33        match request {
34            Se050ManageRequest::Info(request) => Ok(request),
35            _ => Err(Error::InternalError),
36        }
37    }
38}
39
40impl From<InfoRequest> for Se050ManageRequest {
41    fn from(request: InfoRequest) -> Self {
42        Self::Info(request)
43    }
44}
45
46impl TryFrom<Se050ManageRequest> for TestSe050Request {
47    type Error = Error;
48    fn try_from(request: Se050ManageRequest) -> Result<Self, Self::Error> {
49        match request {
50            Se050ManageRequest::TestSe050(request) => Ok(request),
51            _ => Err(Error::InternalError),
52        }
53    }
54}
55
56impl From<TestSe050Request> for Se050ManageRequest {
57    fn from(request: TestSe050Request) -> Self {
58        Self::TestSe050(request)
59    }
60}
61
62#[derive(Debug, Deserialize, Serialize, Copy, Clone)]
63pub struct InfoReply {
64    pub major: u8,
65    pub minor: u8,
66    pub patch: u8,
67    pub sb_major: u8,
68    pub sb_minor: u8,
69    pub persistent: u16,
70    pub transient_deselect: u16,
71    pub transient_reset: u16,
72}
73
74#[allow(clippy::large_enum_variant)]
75#[derive(Debug, Deserialize, Serialize)]
76pub enum Se050ManageReply {
77    Info(InfoReply),
78    TestSe050(TestSe050Reply),
79}
80
81impl TryFrom<Se050ManageReply> for InfoReply {
82    type Error = Error;
83    fn try_from(request: Se050ManageReply) -> Result<Self, Self::Error> {
84        match request {
85            Se050ManageReply::Info(request) => Ok(request),
86            _ => Err(Error::InternalError),
87        }
88    }
89}
90
91impl From<InfoReply> for Se050ManageReply {
92    fn from(request: InfoReply) -> Self {
93        Self::Info(request)
94    }
95}
96
97#[derive(Debug, Deserialize, Serialize, Clone)]
98pub struct TestSe050Reply {
99    pub reply: Bytes<1024>,
100}
101
102impl TryFrom<Se050ManageReply> for TestSe050Reply {
103    type Error = Error;
104    fn try_from(request: Se050ManageReply) -> Result<Self, Self::Error> {
105        match request {
106            Se050ManageReply::TestSe050(request) => Ok(request),
107            _ => Err(Error::InternalError),
108        }
109    }
110}
111
112impl From<TestSe050Reply> for Se050ManageReply {
113    fn from(request: TestSe050Reply) -> Self {
114        Self::TestSe050(request)
115    }
116}
117
118impl Extension for Se050ManageExtension {
119    type Request = Se050ManageRequest;
120    type Reply = Se050ManageReply;
121}
122
123pub type Se050ManageResult<'a, R, C> = ExtensionResult<'a, Se050ManageExtension, R, C>;
124
125pub trait Se050ManageClient: ExtensionClient<Se050ManageExtension> {
126    /// Get info on the SE050
127    fn get_info(&mut self) -> Se050ManageResult<'_, InfoReply, Self> {
128        self.extension(InfoRequest)
129    }
130
131    /// Test the se050 device and driver
132    ///
133    /// This will fake the results of the tests from v0.1.0-test-driver for compatibility but
134    /// return correct metadata header to be shown in the test result
135    fn test_se050(&mut self) -> Se050ManageResult<'_, TestSe050Reply, Self> {
136        self.extension(TestSe050Request)
137    }
138}
139
140impl<C: ExtensionClient<Se050ManageExtension>> Se050ManageClient for C {}