ant-rs
ant-rs copied to clipboard
Is there a way to get a &[u8] with the raw data from AntMessage?
I'm just starting out with Rust, so this may be super trivial to do, but I can't seem to figure out a way to do this.
I'm trying to filter certain ant+ messages and then send them over the network "unmodified", and a component on the other end is expecting them to be in the ant+ format. Is there some method I can call to get a &[u8] to the underlying data, so I can send it over a stream?
Btw awesome work on this library. I've written an open source library for antplus in go: https://github.com/half2me/antgo but I want to have a play around with rust to see what its like.
Thanks
Maybe it would be something like implementing .into()?
No worries, this project is my self intro to rust as well :)
So you have a couple of options for where to get the array from (assuming you are just wanting ANT+ data). One is to just grab the underlying ANT message and inspect the raw array internally.
The other is you can just repack the message type to get the 8 byte array back. I rely on packed_struct a lot to do the underlying bit packing so you can call the functions as well to do the conversion.
Hope that helps.
@cujomalainey thanks for the reply. I actually need the entire ant+ message, from the 0xA4 sync byte at the beginning all the way to the checksum at the end. Basically the exact same message as it came down the wire
Ah for Rx the answer is not really in the current design. The serialize_message trait i use is only implemented for Tx. You could modify the library to dump the bytes right after it validates the checksum in the driver (see code).
@cujomalainey ah okay. Yes, I did see that the trait was only for Tx. I'll see if I can make some minor changes to be able to access the slice.
PS: I've fallen in love with rust's pattern matching. You did such a good job showcasing how useful it is in this library.
@cujomalainey ah okay. Yes, I did see that the trait was only for Tx. I'll see if I can make some minor changes to be able to access the slice.
Sounds good 😊
PS: I've fallen in love with rust's pattern matching. You did such a good job showcasing how useful it is in this library.
Haha thanks, I can't take all the credit, I have a number of fairly skilled rust developers in my social circles I consulted with my design before I built it. Also packed_struct.rs does a lot of the heavy lifting
@cujomalainey this is the best I could do atm, but it seems to work okay for my needs. (I'm only decoding broadcast messages, and I always set the extended flag to 0x80)
fn to_vec(msg: &AntMessage, out: &mut Vec<u8>) -> Result<(), Box<dyn Error>> {
match msg.message {
RxMessage::BroadcastData(msg2) => {
out.extend(msg.header.pack()?);
out.extend(msg2.payload.pack()?);
let ext_info = msg2.extended_info.ok_or("missing extended info")?;
out.extend(ext_info.flag_byte.pack()?);
out.extend(
ext_info
.channel_id_output
.ok_or("missing channel id")?
.pack()?,
);
out.push(msg.checksum);
Ok(())
}
_ => Err("Only broadcast data is supported for now".into()),
}
}
Since I know the exact size the output array will be, is there some better way I could be doing this? Extending a vec seems strange, but it's the only way I knew how to get it working.
Btw the project that uses it: https://github.com/half2me/antdump
If you are just focusing on broadcast then this is probably the best solution. The generic solution would be to build the serialization back into the type where it recursively builds the buffer through the encapsulated types (similar to how Tx works). Not a simple solution but would allow serialization across all types. Nice work.