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

Use RadioLibTime_t (aka unsigned long) when dealing with millis() and micros() #1075

Merged
merged 3 commits into from
Apr 25, 2024

Conversation

Elizafox
Copy link
Contributor

Although sizeof(uint32_t) == sizeof(unsigned long) on Arduino, this is not the case on 64-bit Linux, where sizeof(unsigned long) == sizeof(uint64_t).

Most timestamp arithmetic and comparisons have been left alone, to reduce code churn. This is fine, as uint32_t is perfectly wide to store most timestamp deltas this library will deal with, and C will promote the integer rather than do a narrowing conversion. The real problem arises with narrowing conversions being done by assuming timestamps are 32-bit.

No functional changes intended for platforms where sizeof(uint32_t) == sizeof(unsigned long) (so most 8/16/32-bit platforms).

Although sizeof(uint32_t) == sizeof(unsigned long) on Arduino, this is
not the case on 64-bit Linux, where sizeof(unsigned long) ==
sizeof(uint64_t).

Most timestamp arithmetic and comparisons have been left alone, to
reduce code churn. This is fine, as uint32_t is perfectly wide to store
most timestamp deltas this library will deal with, and C will promote
the integer rather than do a narrowing conversion. The real problem
arises with narrowing conversions being done by assuming timestamps are
32-bit.

No functional changes intended for platforms where sizeof(uint32_t) ==
sizeof(unsigned long) (so most 8/16/32-bit platforms).

Signed-off-by: Elizabeth Myers <[email protected]>
@jgromes
Copy link
Owner

jgromes commented Apr 24, 2024

Thank you for the contribution!

One question/suggestion, would it be possible to use a custom type for this, to make the intent clear? For example typedef RadioLibTimestamp_t unsigned long somewhere in TypeDef.h. This would also have the added benefit of making things a bit more future-proof (since it would be a lot easier to keep this in mind when writing more code/checking pull requests).

@Elizafox
Copy link
Contributor Author

That's a great idea. I thought about doing it like this in fact. I will do it today after work.

@Elizafox
Copy link
Contributor Author

That's a great idea. I thought about doing it like this in fact. I will do it today after work.

In that case it may also be beneficial to change more uses where timestamps are involved to use this type. I already had to update some of the API, it might be beneficial to just rip the bandaid off and just change it all for clarity.

@jgromes
Copy link
Owner

jgromes commented Apr 24, 2024

In that case it may also be beneficial to change more uses where timestamps are involved to use this type

Definitely, that would be better to keep everything consistent.

This makes it obvious what is and isn't a timestamp.

Not everything has been converted; anything dealing with protocol and
chip-level timestamps has been left alone on purpose, to make it clear
that these functions do require 32-bit timestamps.

No functional changes intended on platforms where sizeof(uint32_t) ==
sizeof(unsigned long).

Signed-off-by: Elizabeth Myers <[email protected]>
@Elizafox
Copy link
Contributor Author

In that case it may also be beneficial to change more uses where timestamps are involved to use this type

Definitely, that would be better to keep everything consistent.

I have mostly done so. I have kept some timestamps 32-bit, mostly stuff dealing with chips and protocol stuff. This is to make it clear what values are expected.

@Elizafox Elizafox changed the title Use unsigned long when dealing with millis() and micros() Use RadioLibTime_t (aka unsigned long) when dealing with millis() and micros() Apr 25, 2024
Copy link
Owner

@jgromes jgromes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I left one minor(/bit nitpicky) comment.

src/modules/LR11x0/LR11x0.cpp Outdated Show resolved Hide resolved
We need to not overflow the integers with the shifts and
multiplications, so this is correct behaviour.

Signed-off-by: Elizabeth Myers <[email protected]>
@Elizafox Elizafox requested a review from jgromes April 25, 2024 06:22
@jgromes jgromes merged commit 2050315 into jgromes:master Apr 25, 2024
30 checks passed
@jgromes
Copy link
Owner

jgromes commented Apr 25, 2024

All looks good now - merged, thank you for the contribution!

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

Successfully merging this pull request may close these issues.

2 participants