Skip to content

Commit

Permalink
Add optional serde derives on most public types (#39)
Browse files Browse the repository at this point in the history
* add serde derives

* fmt
  • Loading branch information
pd0wm authored Mar 18, 2024
1 parent fb650f5 commit 6d7fed3
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ repository = "https://github.com/I-CAN-hack/automotive"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
all = []
test_panda = []
test_socketcan = []
serde = ["dep:serde"]

[dependencies]
async-stream = "^0.3.5"
Expand All @@ -21,6 +23,7 @@ futures-core = "^0.3.30"
futures-util = "^0.3.30"
hex = "^0.4"
rusb = "^0.9"
serde = {version = "^1.0.197", features = ["derive"], optional = true}
serial_test = "^3.0.0"
strum = "^0.26"
strum_macros = "^0.26"
Expand Down
2 changes: 2 additions & 0 deletions src/can.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::fmt;

/// Identifier for a CAN frame
#[derive(Copy, Clone, PartialOrd, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Identifier {
Standard(u32),
Extended(u32),
Expand Down Expand Up @@ -51,6 +52,7 @@ impl From<Identifier> for u32 {

/// A CAN frame
#[derive(Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Frame {
/// The bus index for adapters supporting multiple CAN busses
pub bus: u8,
Expand Down
2 changes: 2 additions & 0 deletions src/isotp/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub static FRAME_TYPE_MASK: u8 = 0xf0;
pub static FLOW_SATUS_MASK: u8 = 0x0f;

#[derive(Debug, PartialEq, Copy, Clone, FromRepr)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(u8)]
pub enum FrameType {
Single = 0x00,
Expand All @@ -13,6 +14,7 @@ pub enum FrameType {
}

#[derive(Debug, PartialEq, Copy, Clone, FromRepr)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(u8)]
pub enum FlowStatus {
ContinueToSend = 0x0,
Expand Down
2 changes: 1 addition & 1 deletion src/isotp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const DEFAULT_TIMEOUT_MS: u64 = 100;

/// Configuring passed to the IsoTPAdapter.
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct IsoTPConfig {
pub bus: u8,
/// Transmit ID
Expand Down Expand Up @@ -243,7 +244,6 @@ impl<'a> IsoTPAdapter<'a> {
unimplemented!("CAN FD escape sequence for first frame not supported");
}


buf.extend(&frame.data[2..]);

// Send Flow Control
Expand Down
1 change: 1 addition & 0 deletions src/isotp/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::can::Frame;

#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FlowControlConfig {
pub block_size: u8,
pub separation_time_min: std::time::Duration,
Expand Down
6 changes: 6 additions & 0 deletions src/uds/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub static ZERO_SUB_FUNCTION: u8 = 0x00;

/// Service Identifiers (SIDs) as defined in ISO 14229
#[derive(Debug, PartialEq, Copy, Clone, EnumIter)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(u8)]
pub enum ServiceIdentifier {
// Diagnostic and Communication Management
Expand Down Expand Up @@ -50,6 +51,7 @@ pub enum ServiceIdentifier {

/// Standard Data Identifiers (DIDs) as defined in ISO 14229
#[derive(Debug, PartialEq, Copy, Clone, EnumIter)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(u16)]
pub enum DataIdentifier {
BootSoftwareIdentification = 0xf180,
Expand Down Expand Up @@ -87,6 +89,7 @@ pub enum DataIdentifier {

/// Diagnostic Session Type Sub-Function ID as defined in ISO 14229
#[derive(Debug, PartialEq, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(u8)]
pub enum SessionType {
Default = 0x01,
Expand All @@ -97,6 +100,7 @@ pub enum SessionType {

/// ECU Reset Sub-Function ID as defined in ISO 14229
#[derive(Debug, PartialEq, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(u8)]
pub enum ResetType {
/// Simulates power-on/start-up sequence after a power loss
Expand All @@ -111,6 +115,7 @@ pub enum ResetType {

/// Security Access Type Sub-Function ID as defined in ISO 14229
#[derive(Debug, PartialEq, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(u8)]
pub enum SecurityAccessType {
RequestSeed = 0x01,
Expand All @@ -119,6 +124,7 @@ pub enum SecurityAccessType {

/// ROutine Control Sub-Function ID as defined in ISO 14229
#[derive(Debug, PartialEq, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(u8)]
pub enum RoutineControlType {
Start = 0x01,
Expand Down
1 change: 1 addition & 0 deletions src/uds/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use thiserror::Error;

/// Negative Response Codes returned by ECU as defined in ISO 14229
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(u8)]
pub enum NegativeResponseCode {
GeneralReject = 0x10,
Expand Down
1 change: 1 addition & 0 deletions src/uds/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::time::Duration;

/// Struct returned by DiagnosticSessionControl (0x10)
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SessionParameterRecord {
/// Performance requirement for the server (i.e. the ECU) to start with th response message after the reception of a request message.
pub p2_server_max: Duration,
Expand Down

0 comments on commit 6d7fed3

Please sign in to comment.