access.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. /** @file
  2. * @brief Access layer APIs.
  3. */
  4. /*
  5. * Copyright (c) 2017 Intel Corporation
  6. *
  7. * SPDX-License-Identifier: Apache-2.0
  8. */
  9. #ifndef ZEPHYR_INCLUDE_BLUETOOTH_MESH_ACCESS_H_
  10. #define ZEPHYR_INCLUDE_BLUETOOTH_MESH_ACCESS_H_
  11. #include <settings/settings.h>
  12. #include <sys/util.h>
  13. #include <bluetooth/mesh/msg.h>
  14. /* Internal macros used to initialize array members */
  15. #define BT_MESH_KEY_UNUSED_ELT_(IDX, _) BT_MESH_KEY_UNUSED,
  16. #define BT_MESH_ADDR_UNASSIGNED_ELT_(IDX, _) BT_MESH_ADDR_UNASSIGNED,
  17. #define BT_MESH_MODEL_KEYS_UNUSED \
  18. { UTIL_LISTIFY(CONFIG_BT_MESH_MODEL_KEY_COUNT, \
  19. BT_MESH_KEY_UNUSED_ELT_) }
  20. #define BT_MESH_MODEL_GROUPS_UNASSIGNED \
  21. { UTIL_LISTIFY(CONFIG_BT_MESH_MODEL_GROUP_COUNT, \
  22. BT_MESH_ADDR_UNASSIGNED_ELT_) }
  23. /**
  24. * @brief Access layer
  25. * @defgroup bt_mesh_access Access layer
  26. * @ingroup bt_mesh
  27. * @{
  28. */
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. #define BT_MESH_ADDR_UNASSIGNED 0x0000
  33. #define BT_MESH_ADDR_ALL_NODES 0xffff
  34. #define BT_MESH_ADDR_PROXIES 0xfffc
  35. #define BT_MESH_ADDR_FRIENDS 0xfffd
  36. #define BT_MESH_ADDR_RELAYS 0xfffe
  37. #define BT_MESH_KEY_UNUSED 0xffff
  38. #define BT_MESH_KEY_ANY 0xffff
  39. #define BT_MESH_KEY_DEV 0xfffe
  40. #define BT_MESH_KEY_DEV_LOCAL BT_MESH_KEY_DEV
  41. #define BT_MESH_KEY_DEV_REMOTE 0xfffd
  42. #define BT_MESH_KEY_DEV_ANY 0xfffc
  43. #define BT_MESH_ADDR_IS_UNICAST(addr) ((addr) && (addr) < 0x8000)
  44. #define BT_MESH_ADDR_IS_GROUP(addr) ((addr) >= 0xc000 && (addr) <= 0xff00)
  45. #define BT_MESH_ADDR_IS_VIRTUAL(addr) ((addr) >= 0x8000 && (addr) < 0xc000)
  46. #define BT_MESH_ADDR_IS_RFU(addr) ((addr) >= 0xff00 && (addr) <= 0xfffb)
  47. #define BT_MESH_IS_DEV_KEY(key) (key == BT_MESH_KEY_DEV_LOCAL || \
  48. key == BT_MESH_KEY_DEV_REMOTE)
  49. /** Maximum payload size of an access message (in octets). */
  50. #define BT_MESH_APP_SEG_SDU_MAX 12
  51. /** Maximum possible payload size of an outgoing access message (in octets). */
  52. #define BT_MESH_TX_SDU_MAX (CONFIG_BT_MESH_TX_SEG_MAX * \
  53. BT_MESH_APP_SEG_SDU_MAX)
  54. /** Maximum possible payload size of an incoming access message (in octets). */
  55. #define BT_MESH_RX_SDU_MAX (CONFIG_BT_MESH_RX_SEG_MAX * \
  56. BT_MESH_APP_SEG_SDU_MAX)
  57. /** Helper to define a mesh element within an array.
  58. *
  59. * In case the element has no SIG or Vendor models the helper
  60. * macro BT_MESH_MODEL_NONE can be given instead.
  61. *
  62. * @param _loc Location Descriptor.
  63. * @param _mods Array of models.
  64. * @param _vnd_mods Array of vendor models.
  65. */
  66. #define BT_MESH_ELEM(_loc, _mods, _vnd_mods) \
  67. { \
  68. .loc = (_loc), \
  69. .model_count = ARRAY_SIZE(_mods), \
  70. .vnd_model_count = ARRAY_SIZE(_vnd_mods), \
  71. .models = (_mods), \
  72. .vnd_models = (_vnd_mods), \
  73. }
  74. /** Abstraction that describes a Mesh Element */
  75. struct bt_mesh_elem {
  76. /** Unicast Address. Set at runtime during provisioning. */
  77. uint16_t addr;
  78. /** Location Descriptor (GATT Bluetooth Namespace Descriptors) */
  79. const uint16_t loc;
  80. /** The number of SIG models in this element */
  81. const uint8_t model_count;
  82. /** The number of vendor models in this element */
  83. const uint8_t vnd_model_count;
  84. /** The list of SIG models in this element */
  85. struct bt_mesh_model * const models;
  86. /** The list of vendor models in this element */
  87. struct bt_mesh_model * const vnd_models;
  88. };
  89. /* Foundation Models */
  90. #define BT_MESH_MODEL_ID_CFG_SRV 0x0000
  91. #define BT_MESH_MODEL_ID_CFG_CLI 0x0001
  92. #define BT_MESH_MODEL_ID_HEALTH_SRV 0x0002
  93. #define BT_MESH_MODEL_ID_HEALTH_CLI 0x0003
  94. /* Models from the Mesh Model Specification */
  95. #define BT_MESH_MODEL_ID_GEN_ONOFF_SRV 0x1000
  96. #define BT_MESH_MODEL_ID_GEN_ONOFF_CLI 0x1001
  97. #define BT_MESH_MODEL_ID_GEN_LEVEL_SRV 0x1002
  98. #define BT_MESH_MODEL_ID_GEN_LEVEL_CLI 0x1003
  99. #define BT_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_SRV 0x1004
  100. #define BT_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_CLI 0x1005
  101. #define BT_MESH_MODEL_ID_GEN_POWER_ONOFF_SRV 0x1006
  102. #define BT_MESH_MODEL_ID_GEN_POWER_ONOFF_SETUP_SRV 0x1007
  103. #define BT_MESH_MODEL_ID_GEN_POWER_ONOFF_CLI 0x1008
  104. #define BT_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV 0x1009
  105. #define BT_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV 0x100a
  106. #define BT_MESH_MODEL_ID_GEN_POWER_LEVEL_CLI 0x100b
  107. #define BT_MESH_MODEL_ID_GEN_BATTERY_SRV 0x100c
  108. #define BT_MESH_MODEL_ID_GEN_BATTERY_CLI 0x100d
  109. #define BT_MESH_MODEL_ID_GEN_LOCATION_SRV 0x100e
  110. #define BT_MESH_MODEL_ID_GEN_LOCATION_SETUPSRV 0x100f
  111. #define BT_MESH_MODEL_ID_GEN_LOCATION_CLI 0x1010
  112. #define BT_MESH_MODEL_ID_GEN_ADMIN_PROP_SRV 0x1011
  113. #define BT_MESH_MODEL_ID_GEN_MANUFACTURER_PROP_SRV 0x1012
  114. #define BT_MESH_MODEL_ID_GEN_USER_PROP_SRV 0x1013
  115. #define BT_MESH_MODEL_ID_GEN_CLIENT_PROP_SRV 0x1014
  116. #define BT_MESH_MODEL_ID_GEN_PROP_CLI 0x1015
  117. #define BT_MESH_MODEL_ID_SENSOR_SRV 0x1100
  118. #define BT_MESH_MODEL_ID_SENSOR_SETUP_SRV 0x1101
  119. #define BT_MESH_MODEL_ID_SENSOR_CLI 0x1102
  120. #define BT_MESH_MODEL_ID_TIME_SRV 0x1200
  121. #define BT_MESH_MODEL_ID_TIME_SETUP_SRV 0x1201
  122. #define BT_MESH_MODEL_ID_TIME_CLI 0x1202
  123. #define BT_MESH_MODEL_ID_SCENE_SRV 0x1203
  124. #define BT_MESH_MODEL_ID_SCENE_SETUP_SRV 0x1204
  125. #define BT_MESH_MODEL_ID_SCENE_CLI 0x1205
  126. #define BT_MESH_MODEL_ID_SCHEDULER_SRV 0x1206
  127. #define BT_MESH_MODEL_ID_SCHEDULER_SETUP_SRV 0x1207
  128. #define BT_MESH_MODEL_ID_SCHEDULER_CLI 0x1208
  129. #define BT_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV 0x1300
  130. #define BT_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV 0x1301
  131. #define BT_MESH_MODEL_ID_LIGHT_LIGHTNESS_CLI 0x1302
  132. #define BT_MESH_MODEL_ID_LIGHT_CTL_SRV 0x1303
  133. #define BT_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV 0x1304
  134. #define BT_MESH_MODEL_ID_LIGHT_CTL_CLI 0x1305
  135. #define BT_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV 0x1306
  136. #define BT_MESH_MODEL_ID_LIGHT_HSL_SRV 0x1307
  137. #define BT_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV 0x1308
  138. #define BT_MESH_MODEL_ID_LIGHT_HSL_CLI 0x1309
  139. #define BT_MESH_MODEL_ID_LIGHT_HSL_HUE_SRV 0x130a
  140. #define BT_MESH_MODEL_ID_LIGHT_HSL_SAT_SRV 0x130b
  141. #define BT_MESH_MODEL_ID_LIGHT_XYL_SRV 0x130c
  142. #define BT_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV 0x130d
  143. #define BT_MESH_MODEL_ID_LIGHT_XYL_CLI 0x130e
  144. #define BT_MESH_MODEL_ID_LIGHT_LC_SRV 0x130f
  145. #define BT_MESH_MODEL_ID_LIGHT_LC_SETUPSRV 0x1310
  146. #define BT_MESH_MODEL_ID_LIGHT_LC_CLI 0x1311
  147. /** Model opcode handler. */
  148. struct bt_mesh_model_op {
  149. /** OpCode encoded using the BT_MESH_MODEL_OP_* macros */
  150. const uint32_t opcode;
  151. /** Message length. If the message has variable length then this value
  152. * indicates minimum message length and should be positive. Handler
  153. * function should verify precise length based on the contents of the
  154. * message. If the message has fixed length then this value should
  155. * be negative. Use BT_MESH_LEN_* macros when defining this value.
  156. */
  157. const ssize_t len;
  158. /** @brief Handler function for this opcode.
  159. *
  160. * @param model Model instance receiving the message.
  161. * @param ctx Message context for the message.
  162. * @param buf Message buffer containing the message payload, not
  163. * including the opcode.
  164. *
  165. * @return Zero on success or (negative) error code otherwise.
  166. */
  167. int (*const func)(struct bt_mesh_model *model,
  168. struct bt_mesh_msg_ctx *ctx,
  169. struct net_buf_simple *buf);
  170. };
  171. #define BT_MESH_MODEL_OP_1(b0) (b0)
  172. #define BT_MESH_MODEL_OP_2(b0, b1) (((b0) << 8) | (b1))
  173. #define BT_MESH_MODEL_OP_3(b0, cid) ((((b0) << 16) | 0xc00000) | (cid))
  174. /** Macro for encoding exact message length for fixed-length messages. */
  175. #define BT_MESH_LEN_EXACT(len) (-len)
  176. /** Macro for encoding minimum message length for variable-length messages. */
  177. #define BT_MESH_LEN_MIN(len) (len)
  178. /** End of the opcode list. Must always be present. */
  179. #define BT_MESH_MODEL_OP_END { 0, 0, NULL }
  180. /** Helper to define an empty opcode list. */
  181. #define BT_MESH_MODEL_NO_OPS ((struct bt_mesh_model_op []) \
  182. { BT_MESH_MODEL_OP_END })
  183. /** Helper to define an empty model array */
  184. #define BT_MESH_MODEL_NONE ((struct bt_mesh_model []){})
  185. /** @def BT_MESH_MODEL_CB
  186. *
  187. * @brief Composition data SIG model entry with callback functions.
  188. *
  189. * @param _id Model ID.
  190. * @param _op Array of model opcode handlers.
  191. * @param _pub Model publish parameters.
  192. * @param _user_data User data for the model.
  193. * @param _cb Callback structure, or NULL to keep no callbacks.
  194. */
  195. #define BT_MESH_MODEL_CB(_id, _op, _pub, _user_data, _cb) \
  196. { \
  197. .id = (_id), \
  198. .pub = _pub, \
  199. .keys = BT_MESH_MODEL_KEYS_UNUSED, \
  200. .groups = BT_MESH_MODEL_GROUPS_UNASSIGNED, \
  201. .op = _op, \
  202. .cb = _cb, \
  203. .user_data = _user_data, \
  204. }
  205. /** @def BT_MESH_MODEL_VND_CB
  206. *
  207. * @brief Composition data vendor model entry with callback functions.
  208. *
  209. * @param _company Company ID.
  210. * @param _id Model ID.
  211. * @param _op Array of model opcode handlers.
  212. * @param _pub Model publish parameters.
  213. * @param _user_data User data for the model.
  214. * @param _cb Callback structure, or NULL to keep no callbacks.
  215. */
  216. #define BT_MESH_MODEL_VND_CB(_company, _id, _op, _pub, _user_data, _cb) \
  217. { \
  218. .vnd.company = (_company), \
  219. .vnd.id = (_id), \
  220. .op = _op, \
  221. .pub = _pub, \
  222. .keys = BT_MESH_MODEL_KEYS_UNUSED, \
  223. .groups = BT_MESH_MODEL_GROUPS_UNASSIGNED, \
  224. .user_data = _user_data, \
  225. .cb = _cb, \
  226. }
  227. /** @def BT_MESH_MODEL
  228. *
  229. * @brief Composition data SIG model entry.
  230. *
  231. * @param _id Model ID.
  232. * @param _op Array of model opcode handlers.
  233. * @param _pub Model publish parameters.
  234. * @param _user_data User data for the model.
  235. */
  236. #define BT_MESH_MODEL(_id, _op, _pub, _user_data) \
  237. BT_MESH_MODEL_CB(_id, _op, _pub, _user_data, NULL)
  238. /** @def BT_MESH_MODEL_VND
  239. *
  240. * @brief Composition data vendor model entry.
  241. *
  242. * @param _company Company ID.
  243. * @param _id Model ID.
  244. * @param _op Array of model opcode handlers.
  245. * @param _pub Model publish parameters.
  246. * @param _user_data User data for the model.
  247. */
  248. #define BT_MESH_MODEL_VND(_company, _id, _op, _pub, _user_data) \
  249. BT_MESH_MODEL_VND_CB(_company, _id, _op, _pub, _user_data, NULL)
  250. /** @def BT_MESH_TRANSMIT
  251. *
  252. * @brief Encode transmission count & interval steps.
  253. *
  254. * @param count Number of retransmissions (first transmission is excluded).
  255. * @param int_ms Interval steps in milliseconds. Must be greater than 0,
  256. * less than or equal to 320, and a multiple of 10.
  257. *
  258. * @return Mesh transmit value that can be used e.g. for the default
  259. * values of the configuration model data.
  260. */
  261. #define BT_MESH_TRANSMIT(count, int_ms) ((count) | (((int_ms / 10) - 1) << 3))
  262. /** @def BT_MESH_TRANSMIT_COUNT
  263. *
  264. * @brief Decode transmit count from a transmit value.
  265. *
  266. * @param transmit Encoded transmit count & interval value.
  267. *
  268. * @return Transmission count (actual transmissions is N + 1).
  269. */
  270. #define BT_MESH_TRANSMIT_COUNT(transmit) (((transmit) & (uint8_t)BIT_MASK(3)))
  271. /** @def BT_MESH_TRANSMIT_INT
  272. *
  273. * @brief Decode transmit interval from a transmit value.
  274. *
  275. * @param transmit Encoded transmit count & interval value.
  276. *
  277. * @return Transmission interval in milliseconds.
  278. */
  279. #define BT_MESH_TRANSMIT_INT(transmit) ((((transmit) >> 3) + 1) * 10)
  280. /** @def BT_MESH_PUB_TRANSMIT
  281. *
  282. * @brief Encode Publish Retransmit count & interval steps.
  283. *
  284. * @param count Number of retransmissions (first transmission is excluded).
  285. * @param int_ms Interval steps in milliseconds. Must be greater than 0 and a
  286. * multiple of 50.
  287. *
  288. * @return Mesh transmit value that can be used e.g. for the default
  289. * values of the configuration model data.
  290. */
  291. #define BT_MESH_PUB_TRANSMIT(count, int_ms) BT_MESH_TRANSMIT(count, \
  292. (int_ms) / 5)
  293. /** @def BT_MESH_PUB_TRANSMIT_COUNT
  294. *
  295. * @brief Decode Publish Retransmit count from a given value.
  296. *
  297. * @param transmit Encoded Publish Retransmit count & interval value.
  298. *
  299. * @return Retransmission count (actual transmissions is N + 1).
  300. */
  301. #define BT_MESH_PUB_TRANSMIT_COUNT(transmit) BT_MESH_TRANSMIT_COUNT(transmit)
  302. /** @def BT_MESH_PUB_TRANSMIT_INT
  303. *
  304. * @brief Decode Publish Retransmit interval from a given value.
  305. *
  306. * @param transmit Encoded Publish Retransmit count & interval value.
  307. *
  308. * @return Transmission interval in milliseconds.
  309. */
  310. #define BT_MESH_PUB_TRANSMIT_INT(transmit) ((((transmit) >> 3) + 1) * 50)
  311. /** Model publication context.
  312. *
  313. * The context should primarily be created using the
  314. * BT_MESH_MODEL_PUB_DEFINE macro.
  315. */
  316. struct bt_mesh_model_pub {
  317. /** The model the context belongs to. Initialized by the stack. */
  318. struct bt_mesh_model *mod;
  319. uint16_t addr; /**< Publish Address. */
  320. uint16_t key:12, /**< Publish AppKey Index. */
  321. cred:1, /**< Friendship Credentials Flag. */
  322. send_rel:1, /**< Force reliable sending (segment acks) */
  323. fast_period:1; /**< Use FastPeriodDivisor */
  324. uint8_t ttl; /**< Publish Time to Live. */
  325. uint8_t retransmit; /**< Retransmit Count & Interval Steps. */
  326. uint8_t period; /**< Publish Period. */
  327. uint8_t period_div:4, /**< Divisor for the Period. */
  328. count:4; /**< Transmissions left. */
  329. uint32_t period_start; /**< Start of the current period. */
  330. /** @brief Publication buffer, containing the publication message.
  331. *
  332. * This will get correctly created when the publication context
  333. * has been defined using the BT_MESH_MODEL_PUB_DEFINE macro.
  334. *
  335. * BT_MESH_MODEL_PUB_DEFINE(name, update, size);
  336. */
  337. struct net_buf_simple *msg;
  338. /** @brief Callback for updating the publication buffer.
  339. *
  340. * When set to NULL, the model is assumed not to support
  341. * periodic publishing. When set to non-NULL the callback
  342. * will be called periodically and is expected to update
  343. * @ref bt_mesh_model_pub.msg with a valid publication
  344. * message.
  345. *
  346. * If the callback returns non-zero, the publication is skipped
  347. * and will resume on the next periodic publishing interval.
  348. *
  349. * @param mod The Model the Publication Context belogs to.
  350. *
  351. * @return Zero on success or (negative) error code otherwise.
  352. */
  353. int (*update)(struct bt_mesh_model *mod);
  354. /** Publish Period Timer. Only for stack-internal use. */
  355. struct k_work_delayable timer;
  356. };
  357. /** @def BT_MESH_MODEL_PUB_DEFINE
  358. *
  359. * Define a model publication context.
  360. *
  361. * @param _name Variable name given to the context.
  362. * @param _update Optional message update callback (may be NULL).
  363. * @param _msg_len Length of the publication message.
  364. */
  365. #define BT_MESH_MODEL_PUB_DEFINE(_name, _update, _msg_len) \
  366. NET_BUF_SIMPLE_DEFINE_STATIC(bt_mesh_pub_msg_##_name, _msg_len); \
  367. static struct bt_mesh_model_pub _name = { \
  368. .msg = &bt_mesh_pub_msg_##_name, \
  369. .update = _update, \
  370. }
  371. /** Model callback functions. */
  372. struct bt_mesh_model_cb {
  373. /** @brief Set value handler of user data tied to the model.
  374. *
  375. * @sa settings_handler::h_set
  376. *
  377. * @param model Model to set the persistent data of.
  378. * @param name Name/key of the settings item.
  379. * @param len_rd The size of the data found in the backend.
  380. * @param read_cb Function provided to read the data from the backend.
  381. * @param cb_arg Arguments for the read function provided by the
  382. * backend.
  383. *
  384. * @return 0 on success, error otherwise.
  385. */
  386. int (*const settings_set)(struct bt_mesh_model *model,
  387. const char *name, size_t len_rd,
  388. settings_read_cb read_cb, void *cb_arg);
  389. /** @brief Callback called when the mesh is started.
  390. *
  391. * This handler gets called after the node has been provisioned, or
  392. * after all mesh data has been loaded from persistent storage.
  393. *
  394. * When this callback fires, the mesh model may start its behavior,
  395. * and all Access APIs are ready for use.
  396. *
  397. * @param model Model this callback belongs to.
  398. *
  399. * @return 0 on success, error otherwise.
  400. */
  401. int (*const start)(struct bt_mesh_model *model);
  402. /** @brief Model init callback.
  403. *
  404. * Called on every model instance during mesh initialization.
  405. *
  406. * If any of the model init callbacks return an error, the Mesh
  407. * subsystem initialization will be aborted, and the error will be
  408. * returned to the caller of @ref bt_mesh_init.
  409. *
  410. * @param model Model to be initialized.
  411. *
  412. * @return 0 on success, error otherwise.
  413. */
  414. int (*const init)(struct bt_mesh_model *model);
  415. /** @brief Model reset callback.
  416. *
  417. * Called when the mesh node is reset. All model data is deleted on
  418. * reset, and the model should clear its state.
  419. *
  420. * @note If the model stores any persistent data, this needs to be
  421. * erased manually.
  422. *
  423. * @param model Model this callback belongs to.
  424. */
  425. void (*const reset)(struct bt_mesh_model *model);
  426. };
  427. /** Vendor model ID */
  428. struct bt_mesh_mod_id_vnd {
  429. /** Vendor's company ID */
  430. uint16_t company;
  431. /** Model ID */
  432. uint16_t id;
  433. };
  434. /** Abstraction that describes a Mesh Model instance */
  435. struct bt_mesh_model {
  436. union {
  437. /** SIG model ID */
  438. const uint16_t id;
  439. /** Vendor model ID */
  440. const struct bt_mesh_mod_id_vnd vnd;
  441. };
  442. /* Internal information, mainly for persistent storage */
  443. uint8_t elem_idx; /* Belongs to Nth element */
  444. uint8_t mod_idx; /* Is the Nth model in the element */
  445. uint16_t flags; /* Model flags for internal bookkeeping */
  446. /** Model Publication */
  447. struct bt_mesh_model_pub * const pub;
  448. /** AppKey List */
  449. uint16_t keys[CONFIG_BT_MESH_MODEL_KEY_COUNT];
  450. /** Subscription List (group or virtual addresses) */
  451. uint16_t groups[CONFIG_BT_MESH_MODEL_GROUP_COUNT];
  452. /** Opcode handler list */
  453. const struct bt_mesh_model_op * const op;
  454. /** Model callback structure. */
  455. const struct bt_mesh_model_cb * const cb;
  456. #ifdef CONFIG_BT_MESH_MODEL_EXTENSIONS
  457. /* Pointer to the next model in a model extension list. */
  458. struct bt_mesh_model *next;
  459. #endif
  460. /** Model-specific user data */
  461. void *user_data;
  462. };
  463. /** Callback structure for monitoring model message sending */
  464. struct bt_mesh_send_cb {
  465. /** @brief Handler called at the start of the transmission.
  466. *
  467. * @param duration The duration of the full transmission.
  468. * @param err Error occurring during sending.
  469. * @param cb_data Callback data, as passed to the send API.
  470. */
  471. void (*start)(uint16_t duration, int err, void *cb_data);
  472. /** @brief Handler called at the end of the transmission.
  473. *
  474. * @param err Error occurring during sending.
  475. * @param cb_data Callback data, as passed to the send API.
  476. */
  477. void (*end)(int err, void *cb_data);
  478. };
  479. /** Special TTL value to request using configured default TTL */
  480. #define BT_MESH_TTL_DEFAULT 0xff
  481. /** Maximum allowed TTL value */
  482. #define BT_MESH_TTL_MAX 0x7f
  483. /** @brief Send an Access Layer message.
  484. *
  485. * @param model Mesh (client) Model that the message belongs to.
  486. * @param ctx Message context, includes keys, TTL, etc.
  487. * @param msg Access Layer payload (the actual message to be sent).
  488. * @param cb Optional "message sent" callback.
  489. * @param cb_data User data to be passed to the callback.
  490. *
  491. * @return 0 on success, or (negative) error code on failure.
  492. */
  493. int bt_mesh_model_send(struct bt_mesh_model *model,
  494. struct bt_mesh_msg_ctx *ctx,
  495. struct net_buf_simple *msg,
  496. const struct bt_mesh_send_cb *cb,
  497. void *cb_data);
  498. /** @brief Send a model publication message.
  499. *
  500. * Before calling this function, the user needs to ensure that the model
  501. * publication message (@ref bt_mesh_model_pub.msg) contains a valid
  502. * message to be sent. Note that this API is only to be used for
  503. * non-period publishing. For periodic publishing the app only needs
  504. * to make sure that @ref bt_mesh_model_pub.msg contains a valid message
  505. * whenever the @ref bt_mesh_model_pub.update callback is called.
  506. *
  507. * @param model Mesh (client) Model that's publishing the message.
  508. *
  509. * @return 0 on success, or (negative) error code on failure.
  510. */
  511. int bt_mesh_model_publish(struct bt_mesh_model *model);
  512. /** @brief Get the element that a model belongs to.
  513. *
  514. * @param mod Mesh model.
  515. *
  516. * @return Pointer to the element that the given model belongs to.
  517. */
  518. struct bt_mesh_elem *bt_mesh_model_elem(struct bt_mesh_model *mod);
  519. /** @brief Find a SIG model.
  520. *
  521. * @param elem Element to search for the model in.
  522. * @param id Model ID of the model.
  523. *
  524. * @return A pointer to the Mesh model matching the given parameters, or NULL
  525. * if no SIG model with the given ID exists in the given element.
  526. */
  527. struct bt_mesh_model *bt_mesh_model_find(const struct bt_mesh_elem *elem,
  528. uint16_t id);
  529. /** @brief Find a vendor model.
  530. *
  531. * @param elem Element to search for the model in.
  532. * @param company Company ID of the model.
  533. * @param id Model ID of the model.
  534. *
  535. * @return A pointer to the Mesh model matching the given parameters, or NULL
  536. * if no vendor model with the given ID exists in the given element.
  537. */
  538. struct bt_mesh_model *bt_mesh_model_find_vnd(const struct bt_mesh_elem *elem,
  539. uint16_t company, uint16_t id);
  540. /** @brief Get whether the model is in the primary element of the device.
  541. *
  542. * @param mod Mesh model.
  543. *
  544. * @return true if the model is on the primary element, false otherwise.
  545. */
  546. static inline bool bt_mesh_model_in_primary(const struct bt_mesh_model *mod)
  547. {
  548. return (mod->elem_idx == 0);
  549. }
  550. /** @brief Immediately store the model's user data in persistent storage.
  551. *
  552. * @param mod Mesh model.
  553. * @param vnd This is a vendor model.
  554. * @param name Name/key of the settings item. Only
  555. * @ref SETTINGS_MAX_DIR_DEPTH bytes will be used at most.
  556. * @param data Model data to store, or NULL to delete any model data.
  557. * @param data_len Length of the model data.
  558. *
  559. * @return 0 on success, or (negative) error code on failure.
  560. */
  561. int bt_mesh_model_data_store(struct bt_mesh_model *mod, bool vnd,
  562. const char *name, const void *data,
  563. size_t data_len);
  564. /** @brief Let a model extend another.
  565. *
  566. * Mesh models may be extended to reuse their functionality, forming a more
  567. * complex model. A Mesh model may extend any number of models, in any element.
  568. * The extensions may also be nested, ie a model that extends another may
  569. * itself be extended.
  570. *
  571. * A set of models that extend each other form a model extension list.
  572. *
  573. * All models in an extension list share one subscription list per element. The
  574. * access layer will utilize the combined subscription list of all models in an
  575. * extension list and element, giving the models extended subscription list
  576. * capacity.
  577. *
  578. * @param extending_mod Mesh model that is extending the base model.
  579. * @param base_mod The model being extended.
  580. *
  581. * @retval 0 Successfully extended the base_mod model.
  582. */
  583. int bt_mesh_model_extend(struct bt_mesh_model *extending_mod,
  584. struct bt_mesh_model *base_mod);
  585. /** @brief Check if model is extended by another model.
  586. *
  587. * @param model The model to check.
  588. *
  589. * @retval true If model is extended by another model, otherwise false
  590. */
  591. bool bt_mesh_model_is_extended(struct bt_mesh_model *model);
  592. /** Node Composition */
  593. struct bt_mesh_comp {
  594. uint16_t cid; /**< Company ID */
  595. uint16_t pid; /**< Product ID */
  596. uint16_t vid; /**< Version ID */
  597. size_t elem_count; /**< The number of elements in this device. */
  598. struct bt_mesh_elem *elem; /**< List of elements. */
  599. };
  600. #ifdef __cplusplus
  601. }
  602. #endif
  603. /**
  604. * @}
  605. */
  606. #endif /* ZEPHYR_INCLUDE_BLUETOOTH_MESH_ACCESS_H_ */