-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
use the enumeration with an fixed underlying type #7266
use the enumeration with an fixed underlying type #7266
Conversation
6a39378
to
e178746
Compare
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 change seems error-prone, and it sounds like the issue is the "too much overloading that can cause unexpected results". Perhaps adding explicit PutInt32
, PutUint8
, etc, in TLVWriter
, where it really matters, would help.
Not having overloaded methods, and having explicitly named Put functions as @tcarmelveilleux suggested I don't think actually fixes the problem with enumerations. Fundamentally, you always have to either type-cast the enumeration to a particular stdint type (as is the case in this PR), OR use strongly-typed enumeration definitions as part of code-generation (available since C++11). I'd prefer the latter (and as part of changes we're making to code-gen, I hope to fix this too). |
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.
Agreed that having strongly-typed enums would be better. That said:
- If we have to cast, we should cast to the largest type with the right signedness, so we don't end up with accidental information dropping.
- For at least some of the callsites in this diff the signedness is wrong. For example, door locks
user->type
is presumably UserType, which is anenum8
in the spec, which should per spec be encoded as an unsigned integer.
I'm a little curious what the actual compile error here was. Was it failing to pick one of the overloads because none of them really matched better than any others?
@bzbarsky-apple Thanks for suggestion. The compile error was that when compiling with our riscv-esp-elf toolchain, there is not overloaded function |
e178746
to
2c7d5e8
Compare
Sounds like the compiler in this case is catching a pre-existing bug? We should fix that bug, not enforce it via casts... |
2c7d5e8
to
16c5c01
Compare
In particular, we should probably go through the relevant enums and explicitly declare the right storage types for them, with the right signedness to match the spec, instead of doing this casting at the callsites. |
16c5c01
to
934e48a
Compare
1583345
to
926aec1
Compare
@bzbarsky-apple I used the enumeration with an fixed underlying type and the compile will not fail. But I don't know whether it can be a solution. Also, I found the definition of
|
In practice yes, because we do min-size encoding; the resulting TLV just depends on the value passed in and whether it was signed or unsigned, not on the exact C++ type it was represented in. |
926aec1
to
d8f59e8
Compare
b57baf0
to
adcd0cd
Compare
Size increase report for "nrfconnect-example-build" from aa96ea0
Full report output
|
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.
LGTM
@andy31415 PTAL |
Hi @andy31415 Can this be reviewed and merged? #6345 is blocked on this PR |
Note that not properly sizing the enums is leading to the compile issue in #7601 and may well be leading to functional issues too, at least in the places where we are still using the ZCL encoding. |
@wqx6 was a followup issue filed to track sizing the enums correctly? |
This was broken by project-chip#7266
This was broken by project-chip#7266
This was broken by project-chip#7266
Yes this is a problem IMO , i would like to enforce enum width in my cluster declaration to later reuse them in the device code |
This was broken by project-chip#7266
Problem
We define function
chip::TLV::TLVWriter::Put
with some overloaded functions. The second parameter of thePut
function can beuint8_t
,uint16_t
,uint32_t
,uint64_t
,int8_t
,int16_t
,int32_t
,int64_t
. But in some toolchains (such as riscv32-esp-elf #7057 and arm-none-eabi-gcc),int32_t
is defined fromlong int
instead ofint
, which will cause compile errors when callingPut(uint64_t, enum types)
.Change overview
use the enumeration with an fixed underlying type
Testing
the compile success with the toochain riscv32-esp-elf.