msg.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /** @file
  2. * @brief Message APIs.
  3. */
  4. /*
  5. * Copyright (c) 2021 Nordic Semiconductor ASA
  6. *
  7. * SPDX-License-Identifier: Apache-2.0
  8. */
  9. #ifndef ZEPHYR_INCLUDE_BLUETOOTH_MESH_MSG_H_
  10. #define ZEPHYR_INCLUDE_BLUETOOTH_MESH_MSG_H_
  11. /**
  12. * @brief Message
  13. * @defgroup bt_mesh_msg Message
  14. * @ingroup bt_mesh
  15. * @{
  16. */
  17. #include <zephyr.h>
  18. #include <net/buf.h>
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. /** Length of a short Mesh MIC. */
  23. #define BT_MESH_MIC_SHORT 4
  24. /** Length of a long Mesh MIC. */
  25. #define BT_MESH_MIC_LONG 8
  26. /** @def BT_MESH_MODEL_OP_LEN
  27. *
  28. * @brief Helper to determine the length of an opcode.
  29. *
  30. * @param _op Opcode.
  31. */
  32. #define BT_MESH_MODEL_OP_LEN(_op) ((_op) <= 0xff ? 1 : (_op) <= 0xffff ? 2 : 3)
  33. /** @def BT_MESH_MODEL_BUF_LEN
  34. *
  35. * @brief Helper for model message buffer length.
  36. *
  37. * Returns the length of a Mesh model message buffer, including the opcode
  38. * length and a short MIC.
  39. *
  40. * @param _op Opcode of the message.
  41. * @param _payload_len Length of the model payload.
  42. */
  43. #define BT_MESH_MODEL_BUF_LEN(_op, _payload_len) \
  44. (BT_MESH_MODEL_OP_LEN(_op) + (_payload_len) + BT_MESH_MIC_SHORT)
  45. /** @def BT_MESH_MODEL_BUF_LEN_LONG_MIC
  46. *
  47. * @brief Helper for model message buffer length.
  48. *
  49. * Returns the length of a Mesh model message buffer, including the opcode
  50. * length and a long MIC.
  51. *
  52. * @param _op Opcode of the message.
  53. * @param _payload_len Length of the model payload.
  54. */
  55. #define BT_MESH_MODEL_BUF_LEN_LONG_MIC(_op, _payload_len) \
  56. (BT_MESH_MODEL_OP_LEN(_op) + (_payload_len) + BT_MESH_MIC_LONG)
  57. /** @def BT_MESH_MODEL_BUF_DEFINE
  58. *
  59. * @brief Define a Mesh model message buffer using @ref NET_BUF_SIMPLE_DEFINE.
  60. *
  61. * @param _buf Buffer name.
  62. * @param _op Opcode of the message.
  63. * @param _payload_len Length of the model message payload.
  64. */
  65. #define BT_MESH_MODEL_BUF_DEFINE(_buf, _op, _payload_len) \
  66. NET_BUF_SIMPLE_DEFINE(_buf, BT_MESH_MODEL_BUF_LEN(_op, (_payload_len)))
  67. /** Message sending context. */
  68. struct bt_mesh_msg_ctx {
  69. /** NetKey Index of the subnet to send the message on. */
  70. uint16_t net_idx;
  71. /** AppKey Index to encrypt the message with. */
  72. uint16_t app_idx;
  73. /** Remote address. */
  74. uint16_t addr;
  75. /** Destination address of a received message. Not used for sending. */
  76. uint16_t recv_dst;
  77. /** RSSI of received packet. Not used for sending. */
  78. int8_t recv_rssi;
  79. /** Received TTL value. Not used for sending. */
  80. uint8_t recv_ttl;
  81. /** Force sending reliably by using segment acknowledgment */
  82. bool send_rel;
  83. /** TTL, or BT_MESH_TTL_DEFAULT for default TTL. */
  84. uint8_t send_ttl;
  85. };
  86. /** @brief Initialize a model message.
  87. *
  88. * Clears the message buffer contents, and encodes the given opcode.
  89. * The message buffer will be ready for filling in payload data.
  90. *
  91. * @param msg Message buffer.
  92. * @param opcode Opcode to encode.
  93. */
  94. void bt_mesh_model_msg_init(struct net_buf_simple *msg, uint32_t opcode);
  95. /**
  96. * Acknowledged message context for tracking the status of model messages pending a response.
  97. */
  98. struct bt_mesh_msg_ack_ctx {
  99. struct k_sem sem; /**< Sync semaphore. */
  100. uint32_t op; /**< Opcode we're waiting for. */
  101. uint16_t dst; /**< Address of the node that should respond. */
  102. void *user_data; /**< User specific parameter. */
  103. };
  104. /** @brief Initialize an acknowledged message context.
  105. *
  106. * Initializes semaphore used for synchronization between @ref bt_mesh_msg_ack_ctx_wait and
  107. * @ref bt_mesh_msg_ack_ctx_rx calls. Call this function before using @ref bt_mesh_msg_ack_ctx.
  108. *
  109. * @param ack Acknowledged message context to initialize.
  110. */
  111. static inline void bt_mesh_msg_ack_ctx_init(struct bt_mesh_msg_ack_ctx *ack)
  112. {
  113. k_sem_init(&ack->sem, 0, 1);
  114. }
  115. /** @brief Reset the synchronization semaphore in an acknowledged message context.
  116. *
  117. * This function aborts call to @ref bt_mesh_msg_ack_ctx_wait.
  118. *
  119. * @param ack Acknowledged message context to be reset.
  120. */
  121. static inline void bt_mesh_msg_ack_ctx_reset(struct bt_mesh_msg_ack_ctx *ack)
  122. {
  123. k_sem_reset(&ack->sem);
  124. }
  125. /** @brief Clear parameters of an acknowledged message context.
  126. *
  127. * This function clears the opcode, remote address and user data set
  128. * by @ref bt_mesh_msg_ack_ctx_prepare.
  129. *
  130. * @param ack Acknowledged message context to be cleared.
  131. */
  132. void bt_mesh_msg_ack_ctx_clear(struct bt_mesh_msg_ack_ctx *ack);
  133. /** @brief Prepare an acknowledged message context for the incoming message to wait.
  134. *
  135. * This function sets the opcode, remote address of the incoming message and stores the user data.
  136. * Use this function before calling @ref bt_mesh_msg_ack_ctx_wait.
  137. *
  138. * @param ack Acknowledged message context to prepare.
  139. * @param op The message OpCode.
  140. * @param dst Destination address of the message.
  141. * @param user_data User data for the acknowledged message context.
  142. *
  143. * @return 0 on success, or (negative) error code on failure.
  144. */
  145. int bt_mesh_msg_ack_ctx_prepare(struct bt_mesh_msg_ack_ctx *ack,
  146. uint32_t op, uint16_t dst, void *user_data);
  147. /** @brief Check if the acknowledged message context is initialized with an opcode.
  148. *
  149. * @param ack Acknowledged message context.
  150. *
  151. * @return true if the acknowledged message context is initialized with an opcode,
  152. * false otherwise.
  153. */
  154. static inline bool bt_mesh_msg_ack_ctx_busy(struct bt_mesh_msg_ack_ctx *ack)
  155. {
  156. return (ack->op != 0);
  157. }
  158. /** @brief Wait for a message acknowledge.
  159. *
  160. * This function blocks execution until @ref bt_mesh_msg_ack_ctx_rx is called or by timeout.
  161. *
  162. * @param ack Acknowledged message context of the message to wait for.
  163. * @param timeout Wait timeout.
  164. *
  165. * @return 0 on success, or (negative) error code on failure.
  166. */
  167. int bt_mesh_msg_ack_ctx_wait(struct bt_mesh_msg_ack_ctx *ack, k_timeout_t timeout);
  168. /** @brief Mark a message as acknowledged.
  169. *
  170. * This function unblocks call to @ref bt_mesh_msg_ack_ctx_wait.
  171. *
  172. * @param ack Context of a message to be acknowledged.
  173. */
  174. static inline void bt_mesh_msg_ack_ctx_rx(struct bt_mesh_msg_ack_ctx *ack)
  175. {
  176. k_sem_give(&ack->sem);
  177. }
  178. /** @brief Check if an opcode and address of a message matches the expected one.
  179. *
  180. * @param ack Acknowledged message context to be checked.
  181. * @param op OpCode of the incoming message.
  182. * @param addr Source address of the incoming message.
  183. * @param user_data If not NULL, returns a user data stored in the acknowledged message context
  184. * by @ref bt_mesh_msg_ack_ctx_prepare.
  185. *
  186. * @return true if the incoming message matches the expected one, false otherwise.
  187. */
  188. bool bt_mesh_msg_ack_ctx_match(const struct bt_mesh_msg_ack_ctx *ack,
  189. uint32_t op, uint16_t addr, void **user_data);
  190. #ifdef __cplusplus
  191. }
  192. #endif
  193. /**
  194. * @}
  195. */
  196. #endif /* ZEPHYR_INCLUDE_BLUETOOTH_MESH_MSG_H_ */