From 875ac6280a5115147e014cd4a5d1ffb9e531cbb4 Mon Sep 17 00:00:00 2001 From: lkoester Date: Tue, 24 Jan 2023 11:10:34 +0100 Subject: [PATCH] added can device handler implementation --- src/can.rs | 6 +- src/device_handler.rs | 149 +++++++++++++++++++++++++++++++++++------- 2 files changed, 131 insertions(+), 24 deletions(-) diff --git a/src/can.rs b/src/can.rs index 5d0d5f1..3e5b384 100644 --- a/src/can.rs +++ b/src/can.rs @@ -151,7 +151,7 @@ impl CanTxHandler { if self.package_map.contains_key(&package_id) { let value = self.package_map.get(&package_id).unwrap(); if value.get_sender() == DeviceId::OBC { - if value.get_thread() == self.thread_id { + //if value.get_thread() == self.thread_id { if data.len() <= 8 { let frame_id = package_id_to_can_id(&package_id); let frame = CanFrame::new(frame_id, data); @@ -166,12 +166,14 @@ impl CanTxHandler { data.len() ); } - } else { + /*} else { warn!( "Message dismissed, mismatched thread id: {:?}", value.get_thread() ); } + + */ } else { warn!( "Message dismissed, wrong sender id: {:?}", diff --git a/src/device_handler.rs b/src/device_handler.rs index b8ea961..b997216 100644 --- a/src/device_handler.rs +++ b/src/device_handler.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::hash::Hash; -use std::sync::mpsc::{Receiver, Sender}; -use log::info; +use std::sync::mpsc::{Receiver, RecvError, Sender}; +use log::{info, warn}; use crate::can_ids::{DeviceId, PackageId, PackageModel, ThreadId}; use socketcan::{errors, frame, socket, CanFrame, Socket}; @@ -19,54 +19,145 @@ pub enum DeviceState { Unknown, } - - -pub struct DeviceHandler { +pub struct CanDeviceHandler { device_state_map: HashMap, can_tx_handler: CanTxHandler, can_rx_receiver: Receiver, } -impl DeviceHandler { - pub fn new(can_tx_handler: CanTxHandler, can_rx_receiver: Receiver) -> DeviceHandler { +impl CanDeviceHandler { + pub fn new(can_tx_handler: CanTxHandler, can_rx_receiver: Receiver) -> CanDeviceHandler { let mut device_state_map:HashMap = HashMap::new(); for id in DeviceId::iter() { device_state_map.insert(id, DeviceState::Unknown); } - DeviceHandler{ device_state_map, can_tx_handler, can_rx_receiver } - } - - pub fn power_up_seq(&mut self) { - for id in DeviceId::iter() { - let power_on_success = self.power_on(id); - match power_on_success{ - Ok(_) => {info!("{:?} powered up successfully.", id)} - Err(_) => {info!("{:?} failed to power up.", id)} - } - } + CanDeviceHandler { device_state_map, can_tx_handler, can_rx_receiver } } pub fn power_on(&mut self, id: DeviceId) -> Result<(),()> { if !self.device_state_map.contains_key(&id) { return Err(()); } + info!("Powering on device {:?}", id); let msg_data: [u8; 1] = [id as u8]; self.can_tx_handler.tx_socket(PackageId::DevicePowerOnRequest, &msg_data); - match self.get_power_state(id) { - None => {return Err(());} - Some(power_state) => { - if *power_state != DeviceState::On { + let request_confirmation = self.can_rx_receiver.recv(); + match request_confirmation { + Ok(confirmation) => { + if confirmation.package_id() != PackageId::DevicePowerOnRequestConfirmation { + warn!("Wrong package ID."); return Err(()); } + if confirmation.data()[0] != id as u8 { + warn!("Wrong device ID."); + return Err(()); + } + if confirmation.data()[1] != 1 { + warn!("Request unsuccessful."); + return Err(()); + } + info!("Power on request confirmation received."); + } + Err(_) => { + warn!("Error receiving package."); + return Err(()); + } + } + + let request_success = self.can_rx_receiver.recv(); + match request_success { + Ok(success) => { + if success.package_id() != PackageId::DevicePowerOnConfirmation { + warn!("Wrong package ID."); + return Err(()); + } + if success.data()[0] != id as u8 { + warn!("Wrong device ID."); + return Err(()); + } + if success.data()[1] != 1 { + warn!("Power on unsuccessful."); + return Err(()); + } + info!("Power on confirmation received."); + } + Err(_) => { + warn!("Error receiving package."); + return Err(()); } } Ok(()) } + pub fn power_off(&mut self, id: DeviceId) -> Result<(),()> { + if !self.device_state_map.contains_key(&id) { + return Err(()); + } + info!("Powering on device {:?}", id); + + let msg_data: [u8; 1] = [id as u8]; + self.can_tx_handler.tx_socket(PackageId::DevicePowerOffRequest, &msg_data); + + let request_confirmation = self.can_rx_receiver.recv(); + match request_confirmation { + Ok(confirmation) => { + if confirmation.package_id() != PackageId::DevicePowerOffRequestConfirmation { + warn!("Wrong package ID."); + return Err(()); + } + if confirmation.data()[0] != id as u8 { + warn!("Wrong device ID."); + return Err(()); + } + if confirmation.data()[1] != 1 { + warn!("Request unsuccessful."); + return Err(()); + } + info!("Power off request confirmation received."); + } + Err(_) => { + warn!("Error receiving package."); + return Err(()); + } + } + + let request_success = self.can_rx_receiver.recv(); + match request_success { + Ok(success) => { + if success.package_id() != PackageId::DevicePowerOffConfirmation { + warn!("Wrong package ID."); + return Err(()); + } + if success.data()[0] != id as u8 { + warn!("Wrong device ID."); + return Err(()); + } + if success.data()[1] != 1 { + warn!("Power off unsuccessful."); + return Err(()); + } + info!("Power off confirmation received."); + } + Err(_) => { + warn!("Error receiving package."); + return Err(()); + } + } + + Ok(()) + } + + pub fn get_power_states(&mut self) -> HashMap { + for id in DeviceId::iter() { + self.update_power_state(id).expect("Error updating power state."); + } + self.device_state_map.clone() + } + pub fn get_power_state(&mut self, id: DeviceId) -> Option<&DeviceState> { self.update_power_state(id).expect("Error updating power state."); self.device_state_map.get(&id) @@ -100,3 +191,17 @@ impl DeviceHandler { } } +pub fn power_up_sequence(mut device_handler: CanDeviceHandler) -> HashMap { + for id in DeviceId::iter() { + device_handler.power_on(id).expect("Error powering on device."); + } + device_handler.get_power_states() +} + +pub fn power_down_sequence(mut device_handler: CanDeviceHandler) -> HashMap { + for id in DeviceId::iter() { + device_handler.power_off(id).expect("Error powering on device."); + } + device_handler.get_power_states() +} +