Skip to content

sys/bcd: add bcd_buf_from_u32() #21308

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

Merged
merged 4 commits into from
Apr 3, 2025
Merged

Conversation

benpicco
Copy link
Contributor

@benpicco benpicco commented Mar 19, 2025

Contribution description

This adds two helper functions to the string_utils library:

  • a reverse_buf() that inverts the order of bytes in an array
  • a bcd_buf_from_u32() that takes a number a and calculates a number a' so that the hex string representation of a' is the same as the decimal string representation of a. e.g. bcd_buf_from_u32(123) would produce 0x123.

Testing procedure

This can be used to to generate pretty MAC / IP addresses from decimal serial numbers:

static inline int _get_eui64(uint8_t index, eui64_t *addr)
{
    memset(addr, 0, sizeof(*addr));
    int res = bcd_buf_from_u32(device_get_serial(), addr, sizeof(*addr));
    if (res < 0) {
        return res;
    }
    addr->uint8[res] = index;
    reverse_buf(addr, sizeof(*addr));

    eui64_set_local(addr);
    return 0;
}
2025-03-20 10:43:11,323 # Iface  8  HWaddr: 17:12  Channel: 0  NID: 0x23  PHY: MR-O-QPSK 
2025-03-20 10:43:11,327 #            chip rate: 1000  rate mode: 2 
2025-03-20 10:43:11,331 #           Long HWaddr: 02:00:00:00:00:00:97:12 
2025-03-20 10:43:11,338 #            TX-Power: 3dBm  State: IDLE  max. Retrans.: 3  CSMA Retries: 4 
2025-03-20 10:43:11,344 #           AUTOACK  ACK_REQ  CSMA  L2-PDU:2022  MTU:1280  HL:64  RTR  
2025-03-20 10:43:11,346 #           6LO  IPHC  
2025-03-20 10:43:11,349 #           Source address length: 8
2025-03-20 10:43:11,352 #           Link type: wireless
2025-03-20 10:43:11,357 #           inet6 addr: fe80::9712  scope: link  VAL
2025-03-20 10:43:11,361 #           inet6 addr: fd40::9712  scope: global  VAL
2025-03-20 10:43:11,364 #           inet6 group: ff02::2
2025-03-20 10:43:11,367 #           inet6 group: ff02::1
2025-03-20 10:43:11,370 #           inet6 group: ff02::1:ff00:9712

Issues/PRs references

@github-actions github-actions bot added the Area: sys Area: System label Mar 19, 2025
@github-actions github-actions bot added the Area: tests Area: tests and testing framework label Mar 20, 2025
@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Mar 20, 2025
@riot-ci
Copy link

riot-ci commented Mar 20, 2025

Murdock results

✔️ PASSED

8f4dec6 tests/unittests: add test for bcd_buf_from_u32()

Success Failures Total Runtime
10280 0 10280 10m:03s

Artifacts

@benpicco benpicco requested a review from maribu March 20, 2025 09:50
@benpicco benpicco marked this pull request as ready for review March 20, 2025 09:51
@benpicco benpicco requested a review from miri64 as a code owner March 20, 2025 09:51
@benpicco benpicco requested a review from fabian18 March 20, 2025 09:53
@maribu
Copy link
Member

maribu commented Mar 20, 2025

Isn't the dec_as_hex the same as binary coded decimals? I think there is already a helper for that.

@benpicco
Copy link
Contributor Author

hah you are right!
But I don't think we have a helper for that yet - should I just change the name to dec_to_bcd() then?

@maribu
Copy link
Member

maribu commented Mar 20, 2025

Sounds good to me. I'd personally search such a function in the bcd module, though. How about moving it there?

@OlegHahm
Copy link
Member

I also thing that such a function in a string header is somewhat misleading.

@benpicco
Copy link
Contributor Author

Ok, will move it to a new bcd module, but I think the reverse buffer function can remain as it's more in the spirit of memcpy()/memchk()

@mguetschow
Copy link
Contributor

Ok, will move it to a new bcd module

There already is https://doc.riot-os.org/group__sys__bcd.html

@benpicco
Copy link
Contributor Author

Yes but that's just a header file 😉

@benpicco benpicco changed the title sys/string_utils: add dec_as_hex() sys/string_utils: add bcd_buf_from_u32() Apr 2, 2025
@benpicco benpicco changed the title sys/string_utils: add bcd_buf_from_u32() sys/bcd: add bcd_buf_from_u32() Apr 2, 2025
@benpicco
Copy link
Contributor Author

benpicco commented Apr 2, 2025

I renamed the function and moved it to the bcd module.

Copy link
Contributor

@Teufelchen1 Teufelchen1 left a comment

Choose a reason for hiding this comment

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

I can't shake the feeling off that there's a off-by-one error in it. Oh well, can't find it nor proof it. So looks good to me! :D

@mguetschow mguetschow added this pull request to the merge queue Apr 3, 2025
Merged via the queue into RIOT-OS:master with commit 57de45c Apr 3, 2025
26 checks passed
@benpicco benpicco deleted the dec_as_hex branch April 3, 2025 12:36
@mguetschow mguetschow added this to the Release 2025.04 milestone Apr 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: sys Area: System Area: tests Area: tests and testing framework CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants