-
Notifications
You must be signed in to change notification settings - Fork 27
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
How to discard the packet after hasData returns? #39
Comments
Hi Leo, you can call I hesitate to add a dataLength parameter to |
Thanks for your reply! I will try "init" to workaround this. Do you think adding a default value of 255 (i.e. "uint8_t dataLength = 255") can solve the back-compatibility problem? |
No kidding, that is a first for me as well! Thanks for the report and now I really wish I would have included dataLength as a required parameter to readData. I tried writing an update to readData with a default value for expectedDataLength = 0 back when I read your comment. It turned out as shown below.
I didn't like this because I had to make |
Sorry, I guess I didn't explain myself clearly. uint8_t readData(void *data, uint8_t maxDataLength=255); Just like the signature of the linux function ssize_t read(int fd, void *buf, size_t count);
The default value of _radio.readData(data); Everything in the packet will be written into But, if char name[16];
_radio.readData(name, 16); This means that if there are only 5 bytes in the packet, fine, 5 bytes are written into The problem of using the result of |
Addresses #39 when receiving packets of an unexpected length.
When a radio receives a packet with an unexpected data length, it means that portions of the packet were valid (preamble, address, and CRC) while an unknown number of portions of the packet are invalid. We know for certain that payload data length is wrong in the situation you ran into, but packet identity, the acknowledgement flag, and the data payload could also all be wrong as well. Due to this, we really cannot trust the data in the packet. So I made an improvement to the library to help with the situation you ran into, and updated the example to show its use (helping to make it more obvious what to do). How's this look? https://github.com/dparson55/NRFLite/compare/discardData |
Cool! Thanks! |
Hi,
First of all, thanks for the great library! It makes RF communication much easier!
When reading the code of readData, It seems to me that the "readData" function just writes everything from the packet into the parameter "data" disregarding of the actual size of the argument.
Here is the source code of the function:
This can be dangerous because the packet size can be larger than the actual size of the argument "data".
e.g. for code like this:
If a packet has more than 4 bytes in it, the memory will get corrupted.
I'm wondering whether I can somehow discard the packet when the packet data size is not expected?
Something like this:
Or better, can you please change the signature of the function "readData" and add a second parameter "uint8_t dataLength"? So that the memory will not be corrupted even if the packet has too many bytes in it?
Thanks!
Leo
The text was updated successfully, but these errors were encountered: