lwm2m_path.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * Copyright (c) 2020 Endian Technologies AB
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef ZEPHYR_INCLUDE_NET_LWM2M_PATH_H_
  7. #define ZEPHYR_INCLUDE_NET_LWM2M_PATH_H_
  8. /**
  9. * @brief Generate LwM2M string paths using numeric components.
  10. *
  11. * Accepts at least one and up to four arguments. Each argument will be
  12. * stringified by the pre-processor, so calling this with non-literals will
  13. * likely not do what you want. For example,
  14. *
  15. * #define MY_OBJ_ID 3
  16. * LWM2M_PATH(MY_OBJ_ID, 0, 1)
  17. *
  18. * would evaluate to "3/0/1", while
  19. *
  20. * int x = 3;
  21. * LWM2M_PATH(x, 0, 1)
  22. *
  23. * evalutes to "x/0/1".
  24. */
  25. #define LWM2M_PATH(...) \
  26. LWM2M_PATH_MACRO(__VA_ARGS__, LWM2M_PATH4, LWM2M_PATH3, \
  27. LWM2M_PATH2, LWM2M_PATH1)(__VA_ARGS__)
  28. /* Internal helper macros for the LWM2M_PATH macro */
  29. #define LWM2M_PATH_VA_NUM_ARGS(...) \
  30. LWM2M_PATH_VA_NUM_ARGS_IMPL(__VA_ARGS__, 5, 4, 3, 2, 1)
  31. #define LWM2M_PATH_VA_NUM_ARGS_IMPL(_1, _2, _3, _4, N, ...) N
  32. #define LWM2M_PATH1(_x) #_x
  33. #define LWM2M_PATH2(_x, _y) #_x "/" #_y
  34. #define LWM2M_PATH3(_x, _y, _z) #_x "/" #_y "/" #_z
  35. #define LWM2M_PATH4(_a, _x, _y, _z) #_a "/" #_x "/" #_y "/" #_z
  36. #define LWM2M_PATH_MACRO(_1, _2, _3, _4, NAME, ...) NAME
  37. #endif /* ZEPHYR_INCLUDE_NET_LWM2M_PATH_H_ */