@@ -2,24 +2,25 @@ use std::sync::mpsc::{Receiver, Sender};
22use std:: path:: PathBuf ;
33use configparser:: ini:: Ini ;
44use std:: collections:: { BTreeMap , HashMap } ;
5- use rand:: Rng ;
6- use tokio:: { sync:: { mpsc as tokio_mpsc, oneshot} , task:: JoinHandle } ;
5+ use tokio:: task:: JoinHandle ;
76use std:: time:: Duration ;
87use crate :: canopen:: {
98 CANopenConnection , CANopenNodeHandle ,
10- SdoRequest , SdoDataType , SdoError
9+ SdoRequest , SdoDataType
1110} ;
1211
1312
1413#[ derive( Debug , Clone ) ]
1514pub struct SdoSubObject {
15+ #[ allow( dead_code) ] // Stored from EDS for reference
1616 pub sub_index : u8 ,
1717 pub name : String ,
1818 pub data_type : String ,
1919}
2020
2121#[ derive( Debug , Clone ) ]
2222pub struct SdoObject {
23+ #[ allow( dead_code) ] // Used internally by BTreeMap, needed for EDS parsing
2324 pub index : u16 ,
2425 pub name : String ,
2526 /// We use a BTreeMap to keep sub-objects automatically sorted by their sub_index (the u8 key).
@@ -29,6 +30,7 @@ pub struct SdoObject {
2930#[ derive( Debug , Clone , Eq , Hash , PartialEq ) ]
3031pub struct SdoAddress {
3132 pub index : u16 ,
33+ #[ allow( dead_code) ] // Used in HashMap key, accessed via pattern matching
3234 pub sub_index : u8 ,
3335}
3436
@@ -39,37 +41,24 @@ pub enum Command {
3941 Subscribe {
4042 address : SdoAddress ,
4143 interval_ms : u64 ,
44+ data_type : SdoDataType ,
4245 } ,
4346 Unsubscribe ( SdoAddress ) ,
4447}
4548
4649#[ derive( Debug ) ]
4750pub enum Update {
4851 SdoList ( BTreeMap < u16 , SdoObject > ) ,
52+ #[ allow( dead_code) ] // TODO: Will be used in Priority 1 fixes for connection status
4953 ConnectionSuccess ( BTreeMap < u16 , SdoObject > ) ,
54+ #[ allow( dead_code) ] // TODO: Will be used in Priority 1 fixes for error reporting
5055 ConnectionFailed ( String ) ,
5156 SdoData {
5257 address : SdoAddress ,
5358 value : String ,
5459 } ,
5560}
5661
57- async fn simulation_task ( address : SdoAddress , interval_ms : u64 , update_tx : Sender < Update > ) {
58- println ! ( "Starting simulation task for address {:?} with interval {} ms" , & address, interval_ms) ;
59- let mut interval = tokio:: time:: interval ( std:: time:: Duration :: from_millis ( interval_ms) ) ;
60-
61-
62- loop {
63- interval. tick ( ) . await ;
64- let mut rng = rand:: thread_rng ( ) ;
65- let random_value = rng. gen_range ( 0 ..100 ) ;
66- let _ = update_tx. send ( Update :: SdoData {
67- address : address. clone ( ) ,
68- value : format ! ( "{}" , random_value) ,
69- } ) ;
70- }
71- }
72-
7362async fn sdo_polling_task (
7463 address : SdoAddress ,
7564 interval_ms : u64 ,
@@ -113,7 +102,8 @@ pub fn communication_thread_main(
113102
114103 let rt = tokio:: runtime:: Runtime :: new ( ) . unwrap ( ) ;
115104 let mut subscription_handles: HashMap < SdoAddress , JoinHandle < ( ) > > = HashMap :: new ( ) ;
116- let mut connection: Option < CANopenConnection > = None ;
105+ // Keep connection alive - it owns the background CAN reader task
106+ let mut _connection_handle: Option < CANopenConnection > = None ;
117107 let mut node_handle: Option < CANopenNodeHandle > = None ;
118108
119109
@@ -126,7 +116,7 @@ pub fn communication_thread_main(
126116 Ok :: < ( CANopenConnection , CANopenNodeHandle ) , Box < dyn std:: error:: Error > > ( ( conn, handle) )
127117 } ) {
128118 Ok ( ( conn, handle) ) => {
129- connection = Some ( conn) ;
119+ _connection_handle = Some ( conn) ;
130120 node_handle = Some ( handle) ;
131121 } ,
132122 Err ( err) => {
@@ -149,7 +139,7 @@ pub fn communication_thread_main(
149139 let _ = update_tx. send ( Update :: SdoList ( BTreeMap :: new ( ) ) ) ;
150140 }
151141 } ,
152- Command :: Subscribe { address, interval_ms } => {
142+ Command :: Subscribe { address, interval_ms, data_type } => {
153143 if let Some ( ref handle) = node_handle {
154144 println ! ( "Subscribing to address {:?} with interval {} ms" , & address, interval_ms) ;
155145
@@ -161,7 +151,7 @@ pub fn communication_thread_main(
161151 interval_ms,
162152 update_tx_clone,
163153 handle_clone,
164- SdoDataType :: Real32 ,
154+ data_type ,
165155 ) ) ;
166156
167157 subscription_handles. insert ( address, subscription_handle) ;
@@ -177,7 +167,6 @@ pub fn communication_thread_main(
177167 subscription_handle. abort ( ) ;
178168 }
179169 }
180- _ => { }
181170 }
182171 }
183172}
0 commit comments