Skip to content
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

Struct Packing #12

Open
wizard97 opened this issue Jul 26, 2016 · 6 comments
Open

Struct Packing #12

wizard97 opened this issue Jul 26, 2016 · 6 comments

Comments

@wizard97
Copy link

It seems to me that this library is only portable if both the sender and receiver are on the same platform. To serialize a struct, the library is using memcpy(), which does not take into account how different platforms might pack the struct differently.

I'm not sure if this problem would exist between different Arduino boards, such as the Due (ARM) or the UNO (AVR), but perhaps you should at least warn people of this potential.

@bbbowden
Copy link

bbbowden commented Aug 9, 2018

Thank you for the heads up! I'm trying to adapt this library for UDP between an Atmel ATMega and an ARM based processor. Did a quick check of the size of the structure when compiled for the ATMega and compiled for the ARM processor. Sure enough, ATMega size was 7 and ARM size was 8. Adding "attribute ((packed))" to the definition of the structure ensured that the structures were the same.

@wizard97
Copy link
Author

wizard97 commented Aug 9, 2018

You also need to worry about endianess, if one end is big endian and the other is little endian, you will get garbage out.

The library needs to always serialize elements into the same byte ordering before sending over the wire. Memcpy is not the right thing to use.

@bbbowden
Copy link

bbbowden commented Aug 9, 2018

I can see how that could cause problems but not sure if this method could be modified. In the end, this library has no idea what is in the data structure / where the element boundaries are, only the size of the entire structures in bytes.

If memcpy isn't the right thing to use, how would you handle it while maintaining the flexibility of not needing to know what is in the structure?

@madsci1016
Copy link
Owner

how would you handle it while maintaining the flexibility of not needing to know what is in the structure?

Google Protobuff

@wizard97
Copy link
Author

wizard97 commented Aug 11, 2018

Google Protobuff

Attempting to use C++ Protobuff's on low memory micro-controllers with no MMU, especially 8-bit AVRs, is a terrible idea.

@madsci1016
Copy link
Owner

See https://github.com/nanopb/nanopb

I've used it on 32 bit fine but they claim it runs on 8 bit with only 2-10 kB ROM, <1 kB Ram required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants