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

Enforce the device-serial length #325

Open
psi-4ward opened this issue May 8, 2024 · 4 comments
Open

Enforce the device-serial length #325

psi-4ward opened this issue May 8, 2024 · 4 comments

Comments

@psi-4ward
Copy link
Contributor

AS we know, the device-serial must be exactly 10 chars.

I've accidentally used only 9 chars which results in substantial problems after paring:

  • The CCU allows to pair but the device does not show up in devices-list
  • The CCU produces huge amount of errors in the logs
  • Node-RED (redmatic and the fork from @ptweety) crashes because of invalid-chars in device-name when unmarshalling the response XML

Should we enforce the serial-length by code? Ideally on compile-time but I did not find a working solution.

@pa-pa
Copy link
Owner

pa-pa commented May 8, 2024

Would be good. But I have no idea how to do that at compile time.

@stan23
Copy link
Contributor

stan23 commented May 8, 2024

Something like:

#define ASSERT_IF_NOT_EQUAL(a, b)   char compile_time_assert[((a) == (b)) ? 1 : -1];

ASSERT_IF_NOT_EQUAL (sizeof(serial)/sizeof(char), 11);

The trick is to create an array of negative size.
Untested as I’m typing on the phone.

Edit: must be 11 due to termination character.

@stan23
Copy link
Contributor

stan23 commented May 8, 2024

If you name the array serial_is_not_10_characters it gets pretty obvious in the compiler error message.

@TomMajor
Copy link
Contributor

TomMajor commented May 8, 2024

das funktioniert in der IDE mit der ich gerade arbeite (ist aber nicht Arduino) :

#define SERIAL "1234567890"
_Static_assert((sizeof(SERIAL) - 1) == 10, "SERIAL length must be 10 characters!");
-> alles Bestens

bei SERIAL "123456789" oder SERIAL "1234567890a" :
-> [C++ Error] static_assert failed "SERIAL length must be 10 characters!"

-1 da hier der Null-Terminator mitgezählt wird im Gegensatz zu strlen().

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

4 participants