55// Modules and Imports
66// ============================================================================
77
8+ #[ cfg( feature = "defmt" ) ]
89use defmt:: Format ;
910
1011mod crc;
@@ -34,7 +35,8 @@ pub trait Receivable<'a>: Sized {
3435// ============================================================================
3536
3637/// The ways this API can fail
37- #[ derive( Debug , Copy , Clone , Format , PartialEq , Eq ) ]
38+ #[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
39+ #[ cfg_attr( feature = "defmt" , derive( Format ) ) ]
3840pub enum Error {
3941 BadCrc ,
4042 BadLength ,
@@ -44,8 +46,11 @@ pub enum Error {
4446}
4547
4648/// The kinds of [`Request`] the *Host* can make to the NBMC
49+ #[ derive(
50+ Debug , Copy , Clone , PartialEq , Eq , num_enum:: IntoPrimitive , num_enum:: TryFromPrimitive ,
51+ ) ]
52+ #[ cfg_attr( feature = "defmt" , derive( Format ) ) ]
4753#[ repr( u8 ) ]
48- #[ derive( Debug , Copy , Clone , Format , PartialEq , Eq ) ]
4954pub enum RequestType {
5055 Read = 0xC0 ,
5156 ReadAlt = 0xC1 ,
@@ -57,7 +62,11 @@ pub enum RequestType {
5762
5863/// The NBMC returns this code to indicate whether the previous [`Request`] was
5964/// succesful or not.
60- #[ derive( Debug , Copy , Clone , Format , PartialEq , Eq ) ]
65+ #[ derive(
66+ Debug , Copy , Clone , PartialEq , Eq , num_enum:: IntoPrimitive , num_enum:: TryFromPrimitive ,
67+ ) ]
68+ #[ cfg_attr( feature = "defmt" , derive( Format ) ) ]
69+ #[ repr( u8 ) ]
6170pub enum ResponseResult {
6271 /// The [`Request`] was correctly understood and actioned.
6372 Ok = 0xA0 ,
@@ -85,7 +94,8 @@ pub enum ResponseResult {
8594// ============================================================================
8695
8796/// A *Request* made by the *Host* to the *NBMC*
88- #[ derive( Debug , Clone , Format , PartialEq , Eq ) ]
97+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
98+ #[ cfg_attr( feature = "defmt" , derive( Format ) ) ]
8999pub struct Request {
90100 pub request_type : RequestType ,
91101 pub register : u8 ,
@@ -94,7 +104,8 @@ pub struct Request {
94104}
95105
96106/// A *Response* sent by the *NBMC* in reply to a [`Request`] from a *Host*
97- #[ derive( Debug , Clone , Format , PartialEq , Eq ) ]
107+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
108+ #[ cfg_attr( feature = "defmt" , derive( Format ) ) ]
98109pub struct Response < ' a > {
99110 pub result : ResponseResult ,
100111 pub data : & ' a [ u8 ] ,
@@ -103,7 +114,8 @@ pub struct Response<'a> {
103114
104115/// Describes the [semantic version](https://semver.org) of this implementation
105116/// of the NBMC interface.
106- #[ derive( Debug , Copy , Clone , Format , PartialEq , Eq ) ]
117+ #[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
118+ #[ cfg_attr( feature = "defmt" , derive( Format ) ) ]
107119pub struct ProtocolVersion {
108120 major : u8 ,
109121 minor : u8 ,
@@ -114,22 +126,6 @@ pub struct ProtocolVersion {
114126// Impls
115127// ============================================================================
116128
117- impl TryFrom < u8 > for RequestType {
118- type Error = Error ;
119-
120- fn try_from ( byte : u8 ) -> Result < Self , Error > {
121- match byte {
122- 0xC0 => Ok ( RequestType :: Read ) ,
123- 0xC1 => Ok ( RequestType :: ReadAlt ) ,
124- 0xC2 => Ok ( RequestType :: ShortWrite ) ,
125- 0xC3 => Ok ( RequestType :: ShortWriteAlt ) ,
126- 0xC4 => Ok ( RequestType :: LongWrite ) ,
127- 0xC5 => Ok ( RequestType :: LongWriteAlt ) ,
128- _ => Err ( Error :: BadRequestType ) ,
129- }
130- }
131- }
132-
133129impl Request {
134130 /// Make a new Read Request, requesting the given register and number of
135131 /// bytes.
@@ -208,6 +204,7 @@ impl Request {
208204 ]
209205 }
210206}
207+
211208impl Sendable for Request {
212209 /// Convert to bytes for transmission.
213210 ///
@@ -223,6 +220,7 @@ impl Sendable for Request {
223220 Ok ( bytes. len ( ) )
224221 }
225222}
223+
226224impl < ' a > Receivable < ' a > for Request {
227225 /// Convert from received bytes.
228226 ///
@@ -244,29 +242,14 @@ impl<'a> Receivable<'a> for Request {
244242 return Err ( Error :: BadCrc ) ;
245243 }
246244 Ok ( Request {
247- request_type : data[ 0 ] . try_into ( ) ?,
245+ request_type : data[ 0 ] . try_into ( ) . map_err ( |_| Error :: BadRequestType ) ?,
248246 register : data[ 1 ] ,
249247 length_or_data : data[ 2 ] ,
250248 crc : data[ 3 ] ,
251249 } )
252250 }
253251}
254252
255- impl TryFrom < u8 > for ResponseResult {
256- type Error = Error ;
257-
258- fn try_from ( byte : u8 ) -> Result < Self , Error > {
259- match byte {
260- 0xA0 => Ok ( ResponseResult :: Ok ) ,
261- 0xA1 => Ok ( ResponseResult :: CrcFailure ) ,
262- 0xA2 => Ok ( ResponseResult :: BadRequestType ) ,
263- 0xA3 => Ok ( ResponseResult :: BadRegister ) ,
264- 0xA4 => Ok ( ResponseResult :: BadLength ) ,
265- _ => Err ( Error :: BadResponseResult ) ,
266- }
267- }
268- }
269-
270253impl < ' a > Response < ' a > {
271254 /// Make a new OK response, with some optional data
272255 pub fn new_ok_with_data ( data : & ' a [ u8 ] ) -> Response < ' a > {
@@ -346,7 +329,7 @@ impl<'a> Receivable<'a> for Response<'a> {
346329 return Err ( Error :: BadCrc ) ;
347330 }
348331 Ok ( Response {
349- result : data[ 0 ] . try_into ( ) ?,
332+ result : data[ 0 ] . try_into ( ) . map_err ( |_| Error :: BadResponseResult ) ?,
350333 data : & data[ 1 ..=( data. len ( ) - 2 ) ] ,
351334 crc : data[ data. len ( ) - 1 ] ,
352335 } )
0 commit comments