Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions include/zephyr/devicetree.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* SPDX-License-Identifier: Apache-2.0
* Copyright (c) 2020 Nordic Semiconductor
* Copyright (c) 2020, Linaro Ltd.
* Copyright (c) 2025 Alexander Kozhinov <[email protected]>
*
* Not a generated file. Feel free to modify.
*/
Expand Down Expand Up @@ -1322,6 +1323,18 @@
#define DT_STRING_TOKEN_BY_IDX(node_id, prop, idx) \
DT_CAT6(node_id, _P_, prop, _IDX_, idx, _STRING_TOKEN)

/**
* @brief Like DT_STRING_TOKEN_BY_IDX(), but with a fallback to default_value
* @param node_id node identifier
* @param prop lowercase-and-underscores property name
* @param idx the index to get
* @param default_value a fallback value to expand to
* @return the element in @p prop at index @p idx as a token
*/
#define DT_STRING_TOKEN_BY_IDX_OR(node_id, prop, idx, default_value) \
COND_CODE_1(DT_PROP_HAS_IDX(node_id, prop, idx), \
(DT_STRING_TOKEN_BY_IDX(node_id, prop, idx)), (default_value))

/**
* @brief Like DT_STRING_TOKEN_BY_IDX(), but uppercased.
*
Expand Down Expand Up @@ -4418,6 +4431,17 @@
#define DT_INST_STRING_TOKEN_BY_IDX(inst, prop, idx) \
DT_STRING_TOKEN_BY_IDX(DT_DRV_INST(inst), prop, idx)

/**
* @brief Like DT_INST_STRING_TOKEN_BY_IDX(), but with a fallback to default_value
* @param inst instance number
* @param prop lowercase-and-underscores property name
* @param idx the index to get
* @param default_value a fallback value to expand to
* @return the element in @p prop at index @p idx as a token
*/
#define DT_INST_STRING_TOKEN_BY_IDX_OR(inst, prop, idx, default_value) \
DT_STRING_TOKEN_BY_IDX_OR(DT_DRV_INST(inst), prop, idx, default_value)

/**
* @brief Like DT_INST_STRING_TOKEN_BY_IDX(), but uppercased.
* @param inst instance number
Expand Down
18 changes: 18 additions & 0 deletions tests/lib/devicetree/api/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3433,7 +3433,9 @@ ZTEST(devicetree_api, test_string_token)
#define DT_DRV_COMPAT vnd_string_array_token
ZTEST(devicetree_api, test_string_idx_token)
{
/* The enum has 7 values in total - thus invalid idx starts with 16 */
Copy link

Copilot AI Oct 12, 2025

Choose a reason for hiding this comment

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

The comment states invalid indices start with 16, but the test uses index 42 for out-of-range testing. Either update the comment to reflect the actual test values or explain why 42 was chosen instead of 16.

Suggested change
/* The enum has 7 values in total - thus invalid idx starts with 16 */
/* The enum has 16 values in total (indices 0-15), so invalid indices start at 16.
* The test uses index 42 for out-of-range testing to ensure it is well beyond the valid range.
*/

Copilot uses AI. Check for mistakes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The number 42 is famously known as "The Answer to the Ultimate Question of Life, the Universe, and Everything"

Copy link
Member

Choose a reason for hiding this comment

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

the robot has a point though that 16 is probably better for test to avoid off by one errors

enum token_string_idx {
token_idx_default,
/* Tokens */
token_first_idx_zero,
token_first_idx_one,
Expand Down Expand Up @@ -3468,6 +3470,15 @@ ZTEST(devicetree_api, test_string_idx_token)
zassert_equal(DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 3),
token_second_idx_three, "");

/* Index is in range */
zassert_equal(DT_STRING_TOKEN_BY_IDX_OR(DT_NODELABEL(test_str_array_token_1), val, 3,
token_idx_default),
token_second_idx_three, "");
/* Index is out of range */
zassert_equal(DT_STRING_TOKEN_BY_IDX_OR(DT_NODELABEL(test_str_array_token_1), val, 42,
token_idx_default),
token_idx_default, "");

zassert_equal(DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_0), val, 0),
TOKEN_FIRST_IDX_ZERO, "");
zassert_equal(DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_0), val, 1),
Expand Down Expand Up @@ -3506,6 +3517,13 @@ ZTEST(devicetree_api, test_string_idx_token)
zassert_equal(STRING_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_1))[2],
token_second_idx_two, "");

/* Index is in range instance #0 corresponds to test_str_array_token_0 */
zassert_equal(DT_INST_STRING_TOKEN_BY_IDX_OR(0, val, 2, token_idx_default),
token_first_idx_two, "");
/* Index is out of range */
zassert_equal(DT_INST_STRING_TOKEN_BY_IDX_OR(0, val, 21, token_idx_default),
token_idx_default, "");

#define STRING_UPPER_TOKEN_BY_IDX_VAR(node_id) _CONCAT(var_upper_token, node_id)
#define STRING_UPPER_TOKEN_BY_IDX_TEST_INST_EXPANSION(inst) \
enum token_string_idx STRING_UPPER_TOKEN_BY_IDX_VAR(DT_DRV_INST(inst))[] = { \
Expand Down