Skip to content

Commit

Permalink
Replace raw u32 can identifier with embedded_can types
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelbuesing committed Aug 27, 2021
1 parent 6c58db4 commit 28da084
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 23 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Change Log
## [1.0.0]
- Breaking: src and dst identifier are now based on embedded_can::Id.
## [0.1.1](https://github.com/marcelbuesing/socketcan-isotp/tree/1.0.1) (2020-02-18)
- Critical FIX: Source and destination CAN identifiers were mixed up
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ maintenance = { status = "actively-developed" }

[dependencies]
bitflags = "1.2"
embedded-can = "0.3"
libc = "0.2"
nix = "0.20"
thiserror = "1.0"
21 changes: 10 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,27 @@ care of handling the ISO-TP protocol. Instructions on how the can-isotp kernel m
at [https://github.com/hartkopp/can-isotp](https://github.com/hartkopp/can-isotp).

```rust,no_run
use socketcan_isotp::{self, IsoTpSocket};
use socketcan_isotp::{self, IsoTpSocket, StandardId};
fn main() -> Result<(), socketcan_isotp::Error> {
let mut tp_socket = IsoTpSocket::open(
"vcan0",
0x123,
0x321
StandardId::new(0x123).expect("Invalid src id"),
StandardId::new(0x321).expect("Invalid dst id"),
)?;
loop {
let buffer = tp_socket.read()?;
println!("read {} bytes", buffer.len());
let buffer = tp_socket.read()?;
println!("read {} bytes", buffer.len());
// print TP frame data
for x in buffer {
print!("{:X?} ", x);
}
println!("");
for x in buffer {
print!("{:X?} ", x);
}
println!("");
Ok(())
}
```

# Dev Setup
Expand Down
8 changes: 6 additions & 2 deletions examples/isotprecv.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use socketcan_isotp::{self, IsoTpSocket};
use socketcan_isotp::{self, IsoTpSocket, StandardId};

fn main() -> Result<(), socketcan_isotp::Error> {
let mut tp_socket = IsoTpSocket::open("vcan0", 0x123, 0x321)?;
let mut tp_socket = IsoTpSocket::open(
"vcan0",
StandardId::new(0x123).expect("Invalid src id"),
StandardId::new(0x321).expect("Invalid dst id"),
)?;

let buffer = tp_socket.read()?;
println!("read {} bytes", buffer.len());
Expand Down
8 changes: 6 additions & 2 deletions examples/isotpsend.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use socketcan_isotp::{self, IsoTpSocket};
use socketcan_isotp::{self, IsoTpSocket, StandardId};
use std::time::Duration;

fn main() -> Result<(), socketcan_isotp::Error> {
let tp_socket = IsoTpSocket::open("vcan0", 0x321, 0x123)?;
let tp_socket = IsoTpSocket::open(
"vcan0",
StandardId::new(0x321).expect("Invalid dst id"),
StandardId::new(0x123).expect("Invalid src id"),
)?;

loop {
tp_socket.write(&[0xAA, 0x11, 0x22, 0x33, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF])?;
Expand Down
25 changes: 17 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
//!

use bitflags::bitflags;
pub use embedded_can::{ExtendedId, Id, StandardId};
use libc::{
bind, c_int, c_short, c_void, close, fcntl, read, setsockopt, sockaddr, socket, write, F_GETFL,
F_SETFL, O_NONBLOCK, SOCK_DGRAM,
Expand Down Expand Up @@ -398,7 +399,7 @@ impl IsoTpSocket {
///
/// Usually the more common case, opens a socket can device by name, such
/// as "vcan0" or "socan0".
pub fn open(ifname: &str, src: u32, dst: u32) -> Result<Self, Error> {
pub fn open(ifname: &str, src: impl Into<Id>, dst: impl Into<Id>) -> Result<Self, Error> {
Self::open_with_opts(
ifname,
src,
Expand All @@ -415,8 +416,8 @@ impl IsoTpSocket {
/// as "vcan0" or "socan0".
pub fn open_with_opts(
ifname: &str,
src: u32,
dst: u32,
src: impl Into<Id>,
dst: impl Into<Id>,
isotp_options: Option<IsoTpOptions>,
rx_flow_control_options: Option<FlowControlOptions>,
link_layer_options: Option<LinkLayerOptions>,
Expand All @@ -435,7 +436,7 @@ impl IsoTpSocket {
/// Open CAN ISO-TP device device by interface number.
///
/// Opens a CAN device by kernel interface number.
pub fn open_if(if_index: c_int, src: u32, dst: u32) -> Result<Self, Error> {
pub fn open_if(if_index: c_int, src: impl Into<Id>, dst: impl Into<Id>) -> Result<Self, Error> {
Self::open_if_with_opts(
if_index,
src,
Expand All @@ -451,17 +452,25 @@ impl IsoTpSocket {
/// Opens a CAN device by kernel interface number.
pub fn open_if_with_opts(
if_index: c_int,
src: u32,
dst: u32,
src: impl Into<Id>,
dst: impl Into<Id>,
isotp_options: Option<IsoTpOptions>,
rx_flow_control_options: Option<FlowControlOptions>,
link_layer_options: Option<LinkLayerOptions>,
) -> Result<Self, Error> {
let rx_id = match src.into() {
Id::Standard(standard_id) => standard_id.as_raw() as u32,
Id::Extended(extended_id) => extended_id.as_raw() | EFF_FLAG,
};
let tx_id = match dst.into() {
Id::Standard(standard_id) => standard_id.as_raw() as u32,
Id::Extended(extended_id) => extended_id.as_raw() | EFF_FLAG,
};
let addr = CanAddr {
_af_can: AF_CAN,
if_index,
rx_id: src,
tx_id: dst,
rx_id,
tx_id,
_pgn: 0,
_addr: 0,
};
Expand Down

0 comments on commit 28da084

Please sign in to comment.