Skip to content

Commit 6e44adb

Browse files
paul-fornageflosse
authored andcommitted
add defmt feature
1 parent 7501aff commit 6e44adb

10 files changed

Lines changed: 65 additions & 5 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ all-features = true
1616
[dependencies]
1717
log = "0.4.26"
1818
byteorder = { version = "1.5.0", default-features = false }
19+
defmt = {version = "1.0.1", optional = true}
1920

2021
[features]
2122
default = ["tcp", "rtu"]
2223
tcp = []
2324
rtu = []
2425
std = ["byteorder/std"]
26+
defmt = ["dep:defmt"]
2527

2628
[badges]
2729
maintenance = { status = "actively-developed" }

src/codec/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub mod rtu;
55
pub mod tcp;
66

77
/// The type of decoding
8+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
89
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
910
pub enum DecoderType {
1011
Request,

src/codec/rtu/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ pub use crate::frame::rtu::*;
1212
const MAX_FRAME_LEN: usize = 256;
1313

1414
/// An extracted RTU PDU frame.
15+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
1516
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1617
pub struct DecodedFrame<'a> {
1718
pub slave: SlaveId,
1819
pub pdu: &'a [u8],
1920
}
2021

2122
/// The location of all bytes that belong to the frame.
23+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
2224
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2325
pub struct FrameLocation {
2426
/// The index where the frame starts

src/codec/tcp/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub use crate::frame::tcp::*;
1111
const MAX_FRAME_LEN: usize = 256;
1212

1313
/// An extracted TCP PDU frame.
14+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
1415
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1516
pub struct DecodedFrame<'a> {
1617
pub transaction_id: TransactionId,
@@ -19,6 +20,7 @@ pub struct DecodedFrame<'a> {
1920
}
2021

2122
/// The location of all bytes that belong to the frame.
23+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
2224
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2325
pub struct FrameLocation {
2426
/// The index where the frame starts

src/error.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,30 @@ impl fmt::Display for Error {
4848
}
4949
}
5050
}
51+
52+
#[cfg(feature = "defmt")]
53+
impl defmt::Format for Error {
54+
fn format(&self, f: defmt::Formatter) {
55+
match self {
56+
Self::CoilValue(v) => defmt::write!(f, "Invalid coil value: {}", v),
57+
Self::BufferSize => defmt::write!(f, "Invalid buffer size"),
58+
Self::FnCode(fn_code) => defmt::write!(f, "Invalid function code: {=u8:#04x}", fn_code),
59+
Self::ExceptionCode(code) => defmt::write!(f, "Invalid exception code: {=u8:#04x}", code),
60+
Self::ExceptionFnCode(code) => {
61+
defmt::write!(f, "Invalid exception function code: {=u8:#04x}", code)
62+
}
63+
Self::Crc(expected, actual) => defmt::write!(f,
64+
"Invalid CRC: expected = {=u16:#06x}, actual = {=u16:#06x}",
65+
expected, actual
66+
),
67+
Self::ByteCount(cnt) => defmt::write!(f, "Invalid byte count: {}", cnt),
68+
Self::LengthMismatch(length_field, pdu_len) => defmt::write!(f,
69+
"Length Mismatch: Length Field: {}, PDU Len + 1: {}",
70+
length_field, pdu_len
71+
),
72+
Self::ProtocolNotModbus(protocol_id) => {
73+
defmt::write!(f, "Protocol not Modbus(0), recieved {} instead", protocol_id)
74+
}
75+
}
76+
}
77+
}

src/frame/coils.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::*;
22
use crate::error::*;
33

44
/// Packed coils
5+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
56
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
67
pub struct Coils<'c> {
78
pub(crate) data: RawData<'c>,
@@ -60,6 +61,7 @@ impl<'c> Coils<'c> {
6061

6162
/// Coils iterator.
6263
// TODO: crate an generic iterator
64+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
6365
#[derive(Debug, Clone, PartialEq, Eq)]
6466
pub struct CoilsIter<'c> {
6567
cnt: usize,

src/frame/data.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::*;
22
use crate::error::*;
33

44
/// Modbus data (u16 values)
5+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
56
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
67
pub struct Data<'d> {
78
pub(crate) data: RawData<'d>,
@@ -57,6 +58,7 @@ impl<'d> Data<'d> {
5758

5859
/// Data iterator
5960
// TODO: crate a generic iterator
61+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
6062
#[derive(Debug, Clone, PartialEq, Eq)]
6163
pub struct DataIter<'d> {
6264
cnt: usize,

src/frame/mod.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use byteorder::{BigEndian, ByteOrder};
1111
/// A Modbus function code.
1212
///
1313
/// It is represented by an unsigned 8 bit integer.
14+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
1415
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1516
pub enum FunctionCode {
1617
/// Modbus Function Code: `01` (`0x01`).
@@ -157,6 +158,7 @@ pub(crate) type Quantity = u16;
157158
type RawData<'r> = &'r [u8];
158159

159160
/// A request represents a message from the client (master) to the server (slave).
161+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
160162
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
161163
pub enum Request<'r> {
162164
ReadCoils(Address, Quantity),
@@ -191,17 +193,20 @@ pub enum Request<'r> {
191193
}
192194

193195
/// A server (slave) exception response.
196+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
194197
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
195198
pub struct ExceptionResponse {
196199
pub function: FunctionCode,
197200
pub exception: Exception,
198201
}
199202

200203
/// Represents a message from the client (slave) to the server (master).
204+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
201205
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
202206
pub struct RequestPdu<'r>(pub Request<'r>);
203207

204208
/// Represents a message from the server (slave) to the client (master).
209+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
205210
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
206211
pub struct ResponsePdu<'r>(pub Result<Response<'r>, ExceptionResponse>);
207212

@@ -213,6 +218,7 @@ type EventCount = u16;
213218
type MessageCount = u16;
214219

215220
/// The response data of a successfull request.
221+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
216222
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
217223
pub enum Response<'r> {
218224
ReadCoils(Coils<'r>),
@@ -305,6 +311,7 @@ impl<'r> From<Response<'r>> for FunctionCode {
305311
}
306312

307313
/// A server (slave) exception.
314+
308315
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
309316
pub enum Exception {
310317
IllegalFunction = 0x01,
@@ -318,9 +325,9 @@ pub enum Exception {
318325
GatewayTargetDevice = 0x0B,
319326
}
320327

321-
impl fmt::Display for Exception {
322-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
323-
let desc = match *self {
328+
impl Exception{
329+
fn get_name(&self) -> &'static str {
330+
match *self {
324331
Self::IllegalFunction => "Illegal function",
325332
Self::IllegalDataAddress => "Illegal data address",
326333
Self::IllegalDataValue => "Illegal data value",
@@ -330,11 +337,20 @@ impl fmt::Display for Exception {
330337
Self::MemoryParityError => "Memory parity error",
331338
Self::GatewayPathUnavailable => "Gateway path unavailable",
332339
Self::GatewayTargetDevice => "Gateway target device failed to respond",
333-
};
334-
write!(f, "{desc}")
340+
}
335341
}
336342
}
337343

344+
impl fmt::Display for Exception {
345+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.get_name()) }
346+
}
347+
348+
#[cfg(feature = "defmt")]
349+
impl defmt::Format for Exception {
350+
fn format(&self, fmt: defmt::Formatter) { defmt::write!(fmt, "{}", self.get_name()) }
351+
}
352+
353+
338354
impl Request<'_> {
339355
/// Number of bytes required for a serialized PDU frame.
340356
#[must_use]

src/frame/rtu.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@ use super::*;
44
pub type SlaveId = u8;
55

66
/// RTU header
7+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
78
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
89
pub struct Header {
910
pub slave: SlaveId,
1011
}
1112

1213
/// RTU Request ADU
14+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
1315
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1416
pub struct RequestAdu<'r> {
1517
pub hdr: Header,
1618
pub pdu: RequestPdu<'r>,
1719
}
1820

1921
/// RTU Response ADU
22+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
2023
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2124
pub struct ResponseAdu<'r> {
2225
pub hdr: Header,

src/frame/tcp.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@ use super::*;
33
pub type TransactionId = u16;
44
pub type UnitId = u8;
55

6+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
67
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
78
pub struct Header {
89
pub transaction_id: TransactionId,
910
pub unit_id: UnitId,
1011
}
1112

13+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
1214
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1315
pub struct RequestAdu<'r> {
1416
pub hdr: Header,
1517
pub pdu: RequestPdu<'r>,
1618
}
1719

20+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
1821
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1922
pub struct ResponseAdu<'r> {
2023
pub hdr: Header,

0 commit comments

Comments
 (0)