-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add: Device module and example for Ping1D #23
Add: Device module and example for Ping1D #23
Conversation
67d02f2
to
db893ec
Compare
b4cb7aa
to
c614a09
Compare
src/device.rs
Outdated
async fn stream( | ||
mut serial_stream: SplitStream<Framed<SerialStream, PingCodec>>, | ||
broadcast_tx: Sender<ProtocolMessage>, | ||
) { | ||
'serial_stream: loop { | ||
while let Some(msg) = serial_stream.next().await { | ||
match msg { | ||
Ok(msg) => { | ||
if let Err(_e) = broadcast_tx.send(msg) { | ||
dbg! {_e}; | ||
break 'serial_stream; | ||
}; | ||
} | ||
Err(e) => { | ||
dbg!(e); | ||
} | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This task is likely to leave forever, as the only way out of the loop is if the broadcast_tx fails to send (by being closed), but who closes it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a master "panic!" for this use case?
I believe if it get some faltal error on serial bus it should stop.
We can create a manager which retry stabilish the port, but I not but the port creation inside the device yet.
examples/ping_1d.rs
Outdated
Ok(()) | ||
} | ||
|
||
mod cli_args { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use structs over clap builder pattern, it's simpler and easier to read/maintain.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
k, please check the newer version
examples/ping_1d.rs
Outdated
|
||
let mut port = tokio_serial::new(port_name, baud_rate).open_native_async()?; | ||
#[cfg(unix)] | ||
port.set_exclusive(false)?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the reason for this ? Is missing a comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it's to allow reuse the ports in the same scope,
https://docs.rs/tokio-serial/latest/tokio_serial/struct.SerialStream.html#method.set_exclusive
It was into example tokio_serial, still works without it, let remove and keep it to future if it's required
examples/ping_1d.rs
Outdated
} | ||
Err(_e) => break, | ||
} | ||
if ProfileStructVector.len() == 30 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
>=30
src/device.rs
Outdated
if !(common::AckStruct::id() == answer.message_id | ||
|| common::NackStruct::id() == answer.message_id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
common::AckStruct::id() != answer.message_id && common::NackStruct::id() != answer.message_id
src/device.rs
Outdated
if answer.nacked_id != message_id { | ||
continue; | ||
}; | ||
return Err(PingError::NackError(answer)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The answer here is unnecessary, there is not useful inside
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For logging it could be usefull, since it have the request id inside it.
Should we log and return error? I think a generic error will be harder to track
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since Nack contains the error contains nack_message https://docs.bluerobotics.com/ping-protocol/pingmessage-common/#2-nack it's better to return the message itself or the struct.
Err(PingError::NackError(String))
}; | ||
return Err(PingError::NackError(answer)); | ||
} | ||
_ => return Err(PingError::TryFromError(answer)), // Almost unreachable, but raises error ProtocolMessage |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shoudnt this be a continue ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we never reach this condition. If it's an Ack or Nack, it verifies the ID.
This is just in case any error happens during conversion (which will be almost impossible, as we do CRC during the message reception.)
src/device.rs
Outdated
}; | ||
|
||
match tokio::time::timeout(tokio::time::Duration::from_secs(15), future).await { | ||
Ok(_result) => Ok(()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should return the future result, not an empty ok
src/device.rs
Outdated
match msg { | ||
Ok(msg) => { | ||
if let Err(_e) = broadcast_tx.send(msg) { | ||
dbg! {_e}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should use parentheses not braces
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed to tracing, since we merged last related PR
c614a09
to
fbb3147
Compare
b70f6df
to
07d42bf
Compare
src/device.rs
Outdated
if answer.acked_id != message_id { | ||
continue; | ||
}; | ||
return Ok(answer); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, we should return an empty Ok since ack will always contain message_id
https://docs.bluerobotics.com/ping-protocol/pingmessage-common/#1-ack
src/device.rs
Outdated
if answer.nacked_id != message_id { | ||
continue; | ||
}; | ||
return Err(PingError::NackError(answer)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since Nack contains the error contains nack_message https://docs.bluerobotics.com/ping-protocol/pingmessage-common/#2-nack it's better to return the message itself or the struct.
Err(PingError::NackError(String))
07d42bf
to
3c7b360
Compare
d2af646
to
9f9861b
Compare
9f9861b
to
373a6ec
Compare
@patrickelectric with you |
No description provided.