Skip to content

Commit

Permalink
implement basic can network layer
Browse files Browse the repository at this point in the history
commit a8bcaae
Author: kistenklaus <karlsasssie@gmail.com>
Date:   Sat Nov 18 14:39:05 2023 +0100

    add feature mock-can

commit 89e91f6
Author: kistenklaus <karlsasssie@gmail.com>
Date:   Wed Nov 15 14:58:46 2023 +0100

    add parsers for TypeFrames

commit 477db29
Author: kistenklaus <karlsasssie@gmail.com>
Date:   Sat Nov 11 04:09:36 2023 +0100

    trace example

commit 0b76bbb
Author: kistenklaus <karlsasssie@gmail.com>
Date:   Fri Nov 10 23:58:28 2023 +0100

    event example
  • Loading branch information
kistenklaus committed Nov 18, 2023
1 parent 214d884 commit de3aed4
Show file tree
Hide file tree
Showing 23 changed files with 2,096 additions and 1,408 deletions.
1,686 changes: 300 additions & 1,386 deletions package-lock.json

Large diffs are not rendered by default.

100 changes: 79 additions & 21 deletions src-tauri/Cargo.lock

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

8 changes: 8 additions & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,16 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio = { version = "1.34.0", features = ["time"] }
rand = "0.8.5"
libc = {version = "0.2.149", optional = true}
nix = {version = "0.27.1", features = ["net"], optional = true }
can-config-rs = { git = "https://github.com/mu-zero-HYPERLOOP/can-config-rs.git" }
can-yaml-config-rs = { git = "https://github.com/mu-zero-HYPERLOOP/can-yaml-config-rs.git" }
sorted-vec = "0.8.3"

[features]
default = ["mock-can"]
# this feature is used for production builds or when `devPath` points to the filesystem
# DO NOT REMOVE!!
custom-protocol = ["tauri/custom-protocol"]
mock-can = []
socket-can = ["dep:libc", "dep:nix"]
85 changes: 85 additions & 0 deletions src-tauri/src/can/can/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@

use tokio::{sync::{mpsc::Receiver, Mutex}, sync::mpsc::Sender, runtime::Handle};

use self::{
socket::OwnedCanSocket,
};

use super::can_frame::{CanFrame, CanError};

mod socket;

pub enum CanModule {
CAN0,
CAN1,
}

pub struct CAN {
socket: OwnedCanSocket,
rx: Mutex<Receiver<CanFrame>>,
err_rx: Mutex<Receiver<CanError>>,
tx : Sender<CanFrame>,
}

impl CAN {
pub fn create(module: CanModule, recv_errors: bool) -> Result<CAN, std::io::Error> {
let ifname = match module {
CanModule::CAN0 => "can0",
CanModule::CAN1 => "can1",
};
let socket = OwnedCanSocket::open(ifname)?;

let (tx, rx) = tokio::sync::mpsc::channel::<CanFrame>(16);
let (err_tx, err_rx) = tokio::sync::mpsc::channel::<CanError>(16);

let (txtx, mut txrx) = tokio::sync::mpsc::channel::<CanFrame>(16);

let socket_ref = socket.as_ref();
tokio::task::spawn_blocking(move|| loop {
let frame = socket_ref.receive();
Handle::current().block_on(async {
match frame {
Ok(frame) => {
tx.send(frame).await.expect("failed to forward canframe receiver closed early");
}
Err(err) => {
err_tx.send(err).await.expect("failed to forward canerror receiver closed early");
}
}
});
});

let socket_ref = socket.as_ref();
tokio::task::spawn_blocking(move|| loop {
let frame = Handle::current().block_on(async {
txrx.recv().await.expect("failed to transmit canframe sender closed early")
});
socket_ref.transmit(&frame).expect("failed to write to socket");
});

Ok(CAN { tx : txtx, socket, rx : Mutex::new(rx), err_rx : Mutex::new(err_rx) })
}

pub async fn send(&self, frame: CanFrame) {
self.tx.send(frame).await.expect("failed to forward canframe to can module for transmission");
}

pub async fn receive(&self) -> Result<CanFrame, CanError> {
let frame = self.rx.lock().await
.recv().await;
match frame {
Some(frame) => Ok(frame),
None => Err(CanError::Disconnect(format!("can receive thread closed rx channel")))
}
}

pub async fn receive_err(&mut self) -> CanError {
let error = self.err_rx.lock().await.recv().await;
match error {
Some(error) => error,
None => CanError::Disconnect(format!("can receive thread closed rx channel")),
}
}
}
unsafe impl Send for CAN {}
unsafe impl Sync for CAN {}
Loading

0 comments on commit de3aed4

Please sign in to comment.