net: lwm2m: Add path generation macro
Currently, the Zephyr LwM2M code base is littered with hard-coded
object- and resource IDs, such as
/* Mark 1st instance of security object as a bootstrap server */
lwm2m_engine_set_u8("0/0/1", 1);
The purpose of this LoC is extremely opaque without the accompanying
comment. Each use of a resource path requires constantly checking
headers or the object registry to ensure you're not misremembering IDs.
To alleviate this, this commit introduces a variadic LwM2M path
expansion macro which performs preprocessor stringification on each
argument. For instance, "0/0/1" can now be written as
LWM2M_PATH(LWM2M_OBJECT_SECURITY_ID, 0, SECURITY_BOOTSTRAP_FLAG_ID)
which, while albeit lengthier, avoids the use of hard-coded string
literals, thus making the code much more immediately understandable.
Signed-off-by: Benjamin Lindqvist <benjamin.lindqvist@endian.se>
2020-10-22 13:28:35 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Endian Technologies AB
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ZEPHYR_INCLUDE_NET_LWM2M_PATH_H_
|
|
|
|
#define ZEPHYR_INCLUDE_NET_LWM2M_PATH_H_
|
|
|
|
|
2023-08-09 15:51:20 +02:00
|
|
|
/**
|
2024-05-20 06:41:15 +02:00
|
|
|
* @file lwm2m.h
|
|
|
|
*
|
2023-08-09 15:51:20 +02:00
|
|
|
* @brief LwM2M path helper macros
|
2024-05-20 06:41:15 +02:00
|
|
|
*
|
2023-08-09 15:51:20 +02:00
|
|
|
* @defgroup lwm2m_path_helpers LwM2M path helper macros
|
|
|
|
* @ingroup lwm2m_api
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
net: lwm2m: Add path generation macro
Currently, the Zephyr LwM2M code base is littered with hard-coded
object- and resource IDs, such as
/* Mark 1st instance of security object as a bootstrap server */
lwm2m_engine_set_u8("0/0/1", 1);
The purpose of this LoC is extremely opaque without the accompanying
comment. Each use of a resource path requires constantly checking
headers or the object registry to ensure you're not misremembering IDs.
To alleviate this, this commit introduces a variadic LwM2M path
expansion macro which performs preprocessor stringification on each
argument. For instance, "0/0/1" can now be written as
LWM2M_PATH(LWM2M_OBJECT_SECURITY_ID, 0, SECURITY_BOOTSTRAP_FLAG_ID)
which, while albeit lengthier, avoids the use of hard-coded string
literals, thus making the code much more immediately understandable.
Signed-off-by: Benjamin Lindqvist <benjamin.lindqvist@endian.se>
2020-10-22 13:28:35 +02:00
|
|
|
/**
|
|
|
|
* @brief Generate LwM2M string paths using numeric components.
|
|
|
|
*
|
|
|
|
* Accepts at least one and up to four arguments. Each argument will be
|
|
|
|
* stringified by the pre-processor, so calling this with non-literals will
|
2023-08-09 15:51:20 +02:00
|
|
|
* likely not do what you want.
|
|
|
|
*
|
|
|
|
* For example:
|
net: lwm2m: Add path generation macro
Currently, the Zephyr LwM2M code base is littered with hard-coded
object- and resource IDs, such as
/* Mark 1st instance of security object as a bootstrap server */
lwm2m_engine_set_u8("0/0/1", 1);
The purpose of this LoC is extremely opaque without the accompanying
comment. Each use of a resource path requires constantly checking
headers or the object registry to ensure you're not misremembering IDs.
To alleviate this, this commit introduces a variadic LwM2M path
expansion macro which performs preprocessor stringification on each
argument. For instance, "0/0/1" can now be written as
LWM2M_PATH(LWM2M_OBJECT_SECURITY_ID, 0, SECURITY_BOOTSTRAP_FLAG_ID)
which, while albeit lengthier, avoids the use of hard-coded string
literals, thus making the code much more immediately understandable.
Signed-off-by: Benjamin Lindqvist <benjamin.lindqvist@endian.se>
2020-10-22 13:28:35 +02:00
|
|
|
*
|
2023-08-09 15:51:20 +02:00
|
|
|
* @code{c}
|
|
|
|
* #define MY_OBJ_ID 3
|
|
|
|
* LWM2M_PATH(MY_OBJ_ID, 0, 1)
|
|
|
|
* @endcode
|
net: lwm2m: Add path generation macro
Currently, the Zephyr LwM2M code base is littered with hard-coded
object- and resource IDs, such as
/* Mark 1st instance of security object as a bootstrap server */
lwm2m_engine_set_u8("0/0/1", 1);
The purpose of this LoC is extremely opaque without the accompanying
comment. Each use of a resource path requires constantly checking
headers or the object registry to ensure you're not misremembering IDs.
To alleviate this, this commit introduces a variadic LwM2M path
expansion macro which performs preprocessor stringification on each
argument. For instance, "0/0/1" can now be written as
LWM2M_PATH(LWM2M_OBJECT_SECURITY_ID, 0, SECURITY_BOOTSTRAP_FLAG_ID)
which, while albeit lengthier, avoids the use of hard-coded string
literals, thus making the code much more immediately understandable.
Signed-off-by: Benjamin Lindqvist <benjamin.lindqvist@endian.se>
2020-10-22 13:28:35 +02:00
|
|
|
*
|
|
|
|
* would evaluate to "3/0/1", while
|
|
|
|
*
|
2023-08-09 15:51:20 +02:00
|
|
|
* @code{c}
|
|
|
|
* int x = 3;
|
|
|
|
* LWM2M_PATH(x, 0, 1)
|
|
|
|
* @endcode
|
net: lwm2m: Add path generation macro
Currently, the Zephyr LwM2M code base is littered with hard-coded
object- and resource IDs, such as
/* Mark 1st instance of security object as a bootstrap server */
lwm2m_engine_set_u8("0/0/1", 1);
The purpose of this LoC is extremely opaque without the accompanying
comment. Each use of a resource path requires constantly checking
headers or the object registry to ensure you're not misremembering IDs.
To alleviate this, this commit introduces a variadic LwM2M path
expansion macro which performs preprocessor stringification on each
argument. For instance, "0/0/1" can now be written as
LWM2M_PATH(LWM2M_OBJECT_SECURITY_ID, 0, SECURITY_BOOTSTRAP_FLAG_ID)
which, while albeit lengthier, avoids the use of hard-coded string
literals, thus making the code much more immediately understandable.
Signed-off-by: Benjamin Lindqvist <benjamin.lindqvist@endian.se>
2020-10-22 13:28:35 +02:00
|
|
|
*
|
2022-02-24 13:00:55 +01:00
|
|
|
* evaluates to "x/0/1".
|
net: lwm2m: Add path generation macro
Currently, the Zephyr LwM2M code base is littered with hard-coded
object- and resource IDs, such as
/* Mark 1st instance of security object as a bootstrap server */
lwm2m_engine_set_u8("0/0/1", 1);
The purpose of this LoC is extremely opaque without the accompanying
comment. Each use of a resource path requires constantly checking
headers or the object registry to ensure you're not misremembering IDs.
To alleviate this, this commit introduces a variadic LwM2M path
expansion macro which performs preprocessor stringification on each
argument. For instance, "0/0/1" can now be written as
LWM2M_PATH(LWM2M_OBJECT_SECURITY_ID, 0, SECURITY_BOOTSTRAP_FLAG_ID)
which, while albeit lengthier, avoids the use of hard-coded string
literals, thus making the code much more immediately understandable.
Signed-off-by: Benjamin Lindqvist <benjamin.lindqvist@endian.se>
2020-10-22 13:28:35 +02:00
|
|
|
*/
|
|
|
|
#define LWM2M_PATH(...) \
|
|
|
|
LWM2M_PATH_MACRO(__VA_ARGS__, LWM2M_PATH4, LWM2M_PATH3, \
|
|
|
|
LWM2M_PATH2, LWM2M_PATH1)(__VA_ARGS__)
|
|
|
|
|
|
|
|
|
2023-08-09 15:51:20 +02:00
|
|
|
/** @cond INTERNAL_HIDDEN */
|
net: lwm2m: Add path generation macro
Currently, the Zephyr LwM2M code base is littered with hard-coded
object- and resource IDs, such as
/* Mark 1st instance of security object as a bootstrap server */
lwm2m_engine_set_u8("0/0/1", 1);
The purpose of this LoC is extremely opaque without the accompanying
comment. Each use of a resource path requires constantly checking
headers or the object registry to ensure you're not misremembering IDs.
To alleviate this, this commit introduces a variadic LwM2M path
expansion macro which performs preprocessor stringification on each
argument. For instance, "0/0/1" can now be written as
LWM2M_PATH(LWM2M_OBJECT_SECURITY_ID, 0, SECURITY_BOOTSTRAP_FLAG_ID)
which, while albeit lengthier, avoids the use of hard-coded string
literals, thus making the code much more immediately understandable.
Signed-off-by: Benjamin Lindqvist <benjamin.lindqvist@endian.se>
2020-10-22 13:28:35 +02:00
|
|
|
/* Internal helper macros for the LWM2M_PATH macro */
|
|
|
|
#define LWM2M_PATH_VA_NUM_ARGS(...) \
|
|
|
|
LWM2M_PATH_VA_NUM_ARGS_IMPL(__VA_ARGS__, 5, 4, 3, 2, 1)
|
|
|
|
#define LWM2M_PATH_VA_NUM_ARGS_IMPL(_1, _2, _3, _4, N, ...) N
|
|
|
|
|
|
|
|
#define LWM2M_PATH1(_x) #_x
|
|
|
|
#define LWM2M_PATH2(_x, _y) #_x "/" #_y
|
|
|
|
#define LWM2M_PATH3(_x, _y, _z) #_x "/" #_y "/" #_z
|
|
|
|
#define LWM2M_PATH4(_a, _x, _y, _z) #_a "/" #_x "/" #_y "/" #_z
|
|
|
|
|
|
|
|
#define LWM2M_PATH_MACRO(_1, _2, _3, _4, NAME, ...) NAME
|
2023-08-09 15:51:20 +02:00
|
|
|
/** @endcond */
|
net: lwm2m: Add path generation macro
Currently, the Zephyr LwM2M code base is littered with hard-coded
object- and resource IDs, such as
/* Mark 1st instance of security object as a bootstrap server */
lwm2m_engine_set_u8("0/0/1", 1);
The purpose of this LoC is extremely opaque without the accompanying
comment. Each use of a resource path requires constantly checking
headers or the object registry to ensure you're not misremembering IDs.
To alleviate this, this commit introduces a variadic LwM2M path
expansion macro which performs preprocessor stringification on each
argument. For instance, "0/0/1" can now be written as
LWM2M_PATH(LWM2M_OBJECT_SECURITY_ID, 0, SECURITY_BOOTSTRAP_FLAG_ID)
which, while albeit lengthier, avoids the use of hard-coded string
literals, thus making the code much more immediately understandable.
Signed-off-by: Benjamin Lindqvist <benjamin.lindqvist@endian.se>
2020-10-22 13:28:35 +02:00
|
|
|
|
2022-09-27 15:29:26 +02:00
|
|
|
/**
|
|
|
|
* @brief Initialize LwM2M object structure
|
|
|
|
*
|
|
|
|
* Accepts at least one and up to four arguments. Fill up @ref lwm2m_obj_path structure
|
2023-08-09 15:51:20 +02:00
|
|
|
* and sets the level.
|
2022-09-27 15:29:26 +02:00
|
|
|
*
|
2023-08-09 15:51:20 +02:00
|
|
|
* For example:
|
|
|
|
*
|
|
|
|
* @code{c}
|
|
|
|
* struct lwm2m_obj_path p = LWM2M_OBJ(MY_OBJ, 0, RESOURCE);
|
|
|
|
* @endcode
|
2022-09-27 15:29:26 +02:00
|
|
|
*
|
|
|
|
* Can also be used in place of function argument to return the structure allocated from stack
|
|
|
|
*
|
2023-08-09 15:51:20 +02:00
|
|
|
* @code{c}
|
|
|
|
* lwm2m_notify_observer_path(&LWM2M_OBJ(MY_OBJ, inst_id, RESOURCE));
|
|
|
|
* @endcode
|
2022-09-27 15:29:26 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
#define LWM2M_OBJ(...) \
|
|
|
|
GET_OBJ_MACRO(__VA_ARGS__, LWM2M_OBJ4, LWM2M_OBJ3, LWM2M_OBJ2, LWM2M_OBJ1)(__VA_ARGS__)
|
2023-08-09 15:51:20 +02:00
|
|
|
|
|
|
|
/** @cond INTERNAL_HIDDEN */
|
|
|
|
/* Internal helper macros for the LWM2M_OBJ macro */
|
2022-09-27 15:29:26 +02:00
|
|
|
#define GET_OBJ_MACRO(_1, _2, _3, _4, NAME, ...) NAME
|
|
|
|
#define LWM2M_OBJ1(oi) (struct lwm2m_obj_path) {.obj_id = oi, .level = 1}
|
|
|
|
#define LWM2M_OBJ2(oi, oii) (struct lwm2m_obj_path) {.obj_id = oi, .obj_inst_id = oii, .level = 2}
|
|
|
|
#define LWM2M_OBJ3(oi, oii, ri) (struct lwm2m_obj_path) \
|
|
|
|
{.obj_id = oi, .obj_inst_id = oii, .res_id = ri, .level = 3}
|
|
|
|
#define LWM2M_OBJ4(oi, oii, ri, rii) (struct lwm2m_obj_path) \
|
|
|
|
{.obj_id = oi, .obj_inst_id = oii, .res_id = ri, .res_inst_id = rii, .level = 4}
|
2023-08-09 15:51:20 +02:00
|
|
|
/** @endcond */
|
|
|
|
|
|
|
|
/** @} */
|
net: lwm2m: Add path generation macro
Currently, the Zephyr LwM2M code base is littered with hard-coded
object- and resource IDs, such as
/* Mark 1st instance of security object as a bootstrap server */
lwm2m_engine_set_u8("0/0/1", 1);
The purpose of this LoC is extremely opaque without the accompanying
comment. Each use of a resource path requires constantly checking
headers or the object registry to ensure you're not misremembering IDs.
To alleviate this, this commit introduces a variadic LwM2M path
expansion macro which performs preprocessor stringification on each
argument. For instance, "0/0/1" can now be written as
LWM2M_PATH(LWM2M_OBJECT_SECURITY_ID, 0, SECURITY_BOOTSTRAP_FLAG_ID)
which, while albeit lengthier, avoids the use of hard-coded string
literals, thus making the code much more immediately understandable.
Signed-off-by: Benjamin Lindqvist <benjamin.lindqvist@endian.se>
2020-10-22 13:28:35 +02:00
|
|
|
|
|
|
|
#endif /* ZEPHYR_INCLUDE_NET_LWM2M_PATH_H_ */
|