1 /** @file
2  *  @brief Access layer APIs.
3  */
4 
5 /*
6  * Copyright (c) 2017 Intel Corporation
7  *
8  * SPDX-License-Identifier: Apache-2.0
9  */
10 #ifndef ZEPHYR_INCLUDE_BLUETOOTH_MESH_ACCESS_H_
11 #define ZEPHYR_INCLUDE_BLUETOOTH_MESH_ACCESS_H_
12 
13 #include <zephyr/sys/util.h>
14 #include <zephyr/settings/settings.h>
15 #include <zephyr/bluetooth/mesh/msg.h>
16 
17 /* Internal macros used to initialize array members */
18 #define BT_MESH_KEY_UNUSED_ELT_(IDX, _) BT_MESH_KEY_UNUSED
19 #define BT_MESH_ADDR_UNASSIGNED_ELT_(IDX, _) BT_MESH_ADDR_UNASSIGNED
20 #define BT_MESH_UUID_UNASSIGNED_ELT_(IDX, _) NULL
21 #define BT_MESH_MODEL_KEYS_UNUSED(_keys)			\
22 	{ LISTIFY(_keys, BT_MESH_KEY_UNUSED_ELT_, (,)) }
23 #define BT_MESH_MODEL_GROUPS_UNASSIGNED(_grps)			\
24 	{ LISTIFY(_grps, BT_MESH_ADDR_UNASSIGNED_ELT_, (,)) }
25 #if CONFIG_BT_MESH_LABEL_COUNT > 0
26 #define BT_MESH_MODEL_UUIDS_UNASSIGNED()			\
27 	.uuids = (const uint8_t *[]){ LISTIFY(CONFIG_BT_MESH_LABEL_COUNT, \
28 				     BT_MESH_UUID_UNASSIGNED_ELT_, (,)) },
29 #else
30 #define BT_MESH_MODEL_UUIDS_UNASSIGNED()
31 #endif
32 
33 #define BT_MESH_MODEL_RUNTIME_INIT(_user_data)			\
34 	.rt = &(struct bt_mesh_model_rt_ctx){ .user_data = (_user_data) },
35 
36 /**
37  * @brief Access layer
38  * @defgroup bt_mesh_access Access layer
39  * @ingroup bt_mesh
40  * @{
41  */
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 /**
48  * @name Group addresses
49  * @{
50  */
51 #define BT_MESH_ADDR_UNASSIGNED    0x0000   /**< unassigned */
52 #define BT_MESH_ADDR_ALL_NODES     0xffff   /**< all-nodes */
53 #define BT_MESH_ADDR_RELAYS        0xfffe   /**< all-relays */
54 #define BT_MESH_ADDR_FRIENDS       0xfffd   /**< all-friends */
55 #define BT_MESH_ADDR_PROXIES       0xfffc   /**< all-proxies */
56 #define BT_MESH_ADDR_DFW_NODES     0xfffb   /**< all-directed-forwarding-nodes */
57 #define BT_MESH_ADDR_IP_NODES      0xfffa   /**< all-ipt-nodes */
58 #define BT_MESH_ADDR_IP_BR_ROUTERS 0xfff9   /**< all-ipt-border-routers */
59 /**
60  * @}
61  */
62 
63 /**
64  * @name Predefined key indexes
65  * @{
66  */
67 #define BT_MESH_KEY_UNUSED        0xffff          /**< Key unused */
68 #define BT_MESH_KEY_ANY           0xffff          /**< Any key index */
69 #define BT_MESH_KEY_DEV           0xfffe          /**< Device key */
70 #define BT_MESH_KEY_DEV_LOCAL     BT_MESH_KEY_DEV /**< Local device key */
71 #define BT_MESH_KEY_DEV_REMOTE    0xfffd          /**< Remote device key */
72 #define BT_MESH_KEY_DEV_ANY       0xfffc          /**< Any device key */
73 /**
74  * @}
75  */
76 
77 /**
78  * Check if a Bluetooth Mesh address is a unicast address.
79  */
80 #define BT_MESH_ADDR_IS_UNICAST(addr) ((addr) && (addr) < 0x8000)
81 /**
82  * Check if a Bluetooth Mesh address is a group address.
83  */
84 #define BT_MESH_ADDR_IS_GROUP(addr) ((addr) >= 0xc000 && (addr) < 0xff00)
85 /**
86  * Check if a Bluetooth Mesh address is a fixed group address.
87  */
88 #define BT_MESH_ADDR_IS_FIXED_GROUP(addr) ((addr) >= 0xff00 && (addr) < 0xffff)
89 /**
90  * Check if a Bluetooth Mesh address is a virtual address.
91  */
92 #define BT_MESH_ADDR_IS_VIRTUAL(addr) ((addr) >= 0x8000 && (addr) < 0xc000)
93 /**
94  * Check if a Bluetooth Mesh address is an RFU address.
95  */
96 #define BT_MESH_ADDR_IS_RFU(addr) ((addr) >= 0xff00 && (addr) <= 0xfff8)
97 
98 /**
99  * Check if a Bluetooth Mesh key is a device key.
100  */
101 #define BT_MESH_IS_DEV_KEY(key) (key == BT_MESH_KEY_DEV_LOCAL || \
102 				 key == BT_MESH_KEY_DEV_REMOTE)
103 
104 /** Maximum size of an access message segment (in octets). */
105 #define BT_MESH_APP_SEG_SDU_MAX   12
106 
107 /** Maximum payload size of an unsegmented access message (in octets). */
108 #define BT_MESH_APP_UNSEG_SDU_MAX 15
109 
110 /** Maximum number of segments supported for incoming messages. */
111 #if defined(CONFIG_BT_MESH_RX_SEG_MAX)
112 #define BT_MESH_RX_SEG_MAX CONFIG_BT_MESH_RX_SEG_MAX
113 #else
114 #define BT_MESH_RX_SEG_MAX 0
115 #endif
116 
117 /** Maximum number of segments supported for outgoing messages. */
118 #if defined(CONFIG_BT_MESH_TX_SEG_MAX)
119 #define BT_MESH_TX_SEG_MAX CONFIG_BT_MESH_TX_SEG_MAX
120 #else
121 #define BT_MESH_TX_SEG_MAX 0
122 #endif
123 
124 /** Maximum possible payload size of an outgoing access message (in octets). */
125 #define BT_MESH_TX_SDU_MAX        MAX((BT_MESH_TX_SEG_MAX *		\
126 				       BT_MESH_APP_SEG_SDU_MAX),	\
127 				      BT_MESH_APP_UNSEG_SDU_MAX)
128 
129 /** Maximum possible payload size of an incoming access message (in octets). */
130 #define BT_MESH_RX_SDU_MAX        MAX((BT_MESH_RX_SEG_MAX *		\
131 				       BT_MESH_APP_SEG_SDU_MAX),	\
132 				      BT_MESH_APP_UNSEG_SDU_MAX)
133 
134 /** Helper to define a mesh element within an array.
135  *
136  *  In case the element has no SIG or Vendor models the helper
137  *  macro BT_MESH_MODEL_NONE can be given instead.
138  *
139  *  @param _loc       Location Descriptor.
140  *  @param _mods      Array of models.
141  *  @param _vnd_mods  Array of vendor models.
142  */
143 #define BT_MESH_ELEM(_loc, _mods, _vnd_mods)				\
144 {									\
145 	.rt		  = &(struct bt_mesh_elem_rt_ctx) { 0 },	\
146 	.loc              = (_loc),					\
147 	.model_count      = ARRAY_SIZE(_mods),				\
148 	.vnd_model_count  = ARRAY_SIZE(_vnd_mods),			\
149 	.models           = (_mods),					\
150 	.vnd_models       = (_vnd_mods),				\
151 }
152 
153 /** Abstraction that describes a Mesh Element */
154 struct bt_mesh_elem {
155 	/** Mesh Element runtime information */
156 	struct bt_mesh_elem_rt_ctx {
157 		/** Unicast Address. Set at runtime during provisioning. */
158 		uint16_t addr;
159 	} * const rt;
160 
161 	/** Location Descriptor (GATT Bluetooth Namespace Descriptors) */
162 	const uint16_t loc;
163 	/** The number of SIG models in this element */
164 	const uint8_t model_count;
165 	/** The number of vendor models in this element */
166 	const uint8_t vnd_model_count;
167 
168 	/** The list of SIG models in this element */
169 	const struct bt_mesh_model * const models;
170 	/** The list of vendor models in this element */
171 	const struct bt_mesh_model * const vnd_models;
172 };
173 
174 /**
175  * @name Foundation Models
176  * @{
177  */
178 /** Configuration Server */
179 #define BT_MESH_MODEL_ID_CFG_SRV                   0x0000
180 /** Configuration Client */
181 #define BT_MESH_MODEL_ID_CFG_CLI                   0x0001
182 /** Health Server */
183 #define BT_MESH_MODEL_ID_HEALTH_SRV                0x0002
184 /** Health Client */
185 #define BT_MESH_MODEL_ID_HEALTH_CLI                0x0003
186 /** Remote Provisioning Server */
187 #define BT_MESH_MODEL_ID_REMOTE_PROV_SRV           0x0004
188 /** Remote Provisioning Client */
189 #define BT_MESH_MODEL_ID_REMOTE_PROV_CLI           0x0005
190 /** Private Beacon Server */
191 #define BT_MESH_MODEL_ID_PRIV_BEACON_SRV           0x000a
192 /** Private Beacon Client */
193 #define BT_MESH_MODEL_ID_PRIV_BEACON_CLI           0x000b
194 /** SAR Configuration Server */
195 #define BT_MESH_MODEL_ID_SAR_CFG_SRV               0x000e
196 /** SAR Configuration Client */
197 #define BT_MESH_MODEL_ID_SAR_CFG_CLI               0x000f
198 /** Opcodes Aggregator Server */
199 #define BT_MESH_MODEL_ID_OP_AGG_SRV                0x0010
200 /** Opcodes Aggregator Client */
201 #define BT_MESH_MODEL_ID_OP_AGG_CLI                0x0011
202 /** Large Composition Data Server */
203 #define BT_MESH_MODEL_ID_LARGE_COMP_DATA_SRV       0x0012
204 /** Large Composition Data Client */
205 #define BT_MESH_MODEL_ID_LARGE_COMP_DATA_CLI       0x0013
206 /** Solicitation PDU RPL Configuration Client */
207 #define BT_MESH_MODEL_ID_SOL_PDU_RPL_SRV	   0x0014
208 /** Solicitation PDU RPL Configuration Server */
209 #define BT_MESH_MODEL_ID_SOL_PDU_RPL_CLI	   0x0015
210 /** Private Proxy Server */
211 #define BT_MESH_MODEL_ID_ON_DEMAND_PROXY_SRV	   0x000c
212 /** Private Proxy Client */
213 #define BT_MESH_MODEL_ID_ON_DEMAND_PROXY_CLI	   0x000d
214 /**
215  * @}
216  */
217 
218 /**
219  * @name Models from the Mesh Model Specification
220  * @{
221  */
222 /** Generic OnOff Server */
223 #define BT_MESH_MODEL_ID_GEN_ONOFF_SRV             0x1000
224 /** Generic OnOff Client */
225 #define BT_MESH_MODEL_ID_GEN_ONOFF_CLI             0x1001
226 /** Generic Level Server */
227 #define BT_MESH_MODEL_ID_GEN_LEVEL_SRV             0x1002
228 /** Generic Level Client */
229 #define BT_MESH_MODEL_ID_GEN_LEVEL_CLI             0x1003
230 /** Generic Default Transition Time Server */
231 #define BT_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_SRV    0x1004
232 /** Generic Default Transition Time Client */
233 #define BT_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_CLI    0x1005
234 /** Generic Power OnOff Server */
235 #define BT_MESH_MODEL_ID_GEN_POWER_ONOFF_SRV       0x1006
236 /** Generic Power OnOff Setup Server */
237 #define BT_MESH_MODEL_ID_GEN_POWER_ONOFF_SETUP_SRV 0x1007
238 /** Generic Power OnOff Client */
239 #define BT_MESH_MODEL_ID_GEN_POWER_ONOFF_CLI       0x1008
240 /** Generic Power Level Server */
241 #define BT_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV       0x1009
242 /** Generic Power Level Setup Server */
243 #define BT_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV 0x100a
244 /** Generic Power Level Client */
245 #define BT_MESH_MODEL_ID_GEN_POWER_LEVEL_CLI       0x100b
246 /** Generic Battery Server */
247 #define BT_MESH_MODEL_ID_GEN_BATTERY_SRV           0x100c
248 /** Generic Battery Client */
249 #define BT_MESH_MODEL_ID_GEN_BATTERY_CLI           0x100d
250 /** Generic Location Server */
251 #define BT_MESH_MODEL_ID_GEN_LOCATION_SRV          0x100e
252 /** Generic Location Setup Server */
253 #define BT_MESH_MODEL_ID_GEN_LOCATION_SETUPSRV     0x100f
254 /** Generic Location Client */
255 #define BT_MESH_MODEL_ID_GEN_LOCATION_CLI          0x1010
256 /** Generic Admin Property Server */
257 #define BT_MESH_MODEL_ID_GEN_ADMIN_PROP_SRV        0x1011
258 /** Generic Manufacturer Property Server */
259 #define BT_MESH_MODEL_ID_GEN_MANUFACTURER_PROP_SRV 0x1012
260 /** Generic User Property Server */
261 #define BT_MESH_MODEL_ID_GEN_USER_PROP_SRV         0x1013
262 /** Generic Client Property Server */
263 #define BT_MESH_MODEL_ID_GEN_CLIENT_PROP_SRV       0x1014
264 /** Generic Property Client */
265 #define BT_MESH_MODEL_ID_GEN_PROP_CLI              0x1015
266 /** Sensor Server */
267 #define BT_MESH_MODEL_ID_SENSOR_SRV                0x1100
268 /** Sensor Setup Server */
269 #define BT_MESH_MODEL_ID_SENSOR_SETUP_SRV          0x1101
270 /** Sensor Client */
271 #define BT_MESH_MODEL_ID_SENSOR_CLI                0x1102
272 /** Time Server */
273 #define BT_MESH_MODEL_ID_TIME_SRV                  0x1200
274 /** Time Setup Server */
275 #define BT_MESH_MODEL_ID_TIME_SETUP_SRV            0x1201
276 /** Time Client */
277 #define BT_MESH_MODEL_ID_TIME_CLI                  0x1202
278 /** Scene Server */
279 #define BT_MESH_MODEL_ID_SCENE_SRV                 0x1203
280 /** Scene Setup Server */
281 #define BT_MESH_MODEL_ID_SCENE_SETUP_SRV           0x1204
282 /** Scene Client */
283 #define BT_MESH_MODEL_ID_SCENE_CLI                 0x1205
284 /** Scheduler Server */
285 #define BT_MESH_MODEL_ID_SCHEDULER_SRV             0x1206
286 /** Scheduler Setup Server */
287 #define BT_MESH_MODEL_ID_SCHEDULER_SETUP_SRV       0x1207
288 /** Scheduler Client */
289 #define BT_MESH_MODEL_ID_SCHEDULER_CLI             0x1208
290 /** Light Lightness Server */
291 #define BT_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV       0x1300
292 /** Light Lightness Setup Server */
293 #define BT_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV 0x1301
294 /** Light Lightness Client */
295 #define BT_MESH_MODEL_ID_LIGHT_LIGHTNESS_CLI       0x1302
296 /** Light CTL Server */
297 #define BT_MESH_MODEL_ID_LIGHT_CTL_SRV             0x1303
298 /** Light CTL Setup Server */
299 #define BT_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV       0x1304
300 /** Light CTL Client */
301 #define BT_MESH_MODEL_ID_LIGHT_CTL_CLI             0x1305
302 /** Light CTL Temperature Server */
303 #define BT_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV        0x1306
304 /** Light HSL Server */
305 #define BT_MESH_MODEL_ID_LIGHT_HSL_SRV             0x1307
306 /** Light HSL Setup Server */
307 #define BT_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV       0x1308
308 /** Light HSL Client */
309 #define BT_MESH_MODEL_ID_LIGHT_HSL_CLI             0x1309
310 /** Light HSL Hue Server */
311 #define BT_MESH_MODEL_ID_LIGHT_HSL_HUE_SRV         0x130a
312 /** Light HSL Saturation Server */
313 #define BT_MESH_MODEL_ID_LIGHT_HSL_SAT_SRV         0x130b
314 /** Light xyL Server */
315 #define BT_MESH_MODEL_ID_LIGHT_XYL_SRV             0x130c
316 /** Light xyL Setup Server */
317 #define BT_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV       0x130d
318 /** Light xyL Client */
319 #define BT_MESH_MODEL_ID_LIGHT_XYL_CLI             0x130e
320 /** Light LC Server */
321 #define BT_MESH_MODEL_ID_LIGHT_LC_SRV              0x130f
322 /** Light LC Setup Server */
323 #define BT_MESH_MODEL_ID_LIGHT_LC_SETUPSRV         0x1310
324 /** Light LC Client */
325 #define BT_MESH_MODEL_ID_LIGHT_LC_CLI              0x1311
326 /**
327  * @}
328  */
329 
330 /**
331  * @name Models from the Mesh Binary Large Object Transfer Model Specification
332  * @{
333  */
334 /** BLOB Transfer Server */
335 #define BT_MESH_MODEL_ID_BLOB_SRV                  0x1400
336 /** BLOB Transfer Client */
337 #define BT_MESH_MODEL_ID_BLOB_CLI                  0x1401
338 /**
339  * @}
340  */
341 
342 /**
343  *  @name Models from the Mesh Device Firmware Update Model Specification
344  * @{
345  */
346 /** Firmware Update Server */
347 #define BT_MESH_MODEL_ID_DFU_SRV                   0x1402
348 /** Firmware Update Client */
349 #define BT_MESH_MODEL_ID_DFU_CLI                   0x1403
350 /** Firmware Distribution Server */
351 #define BT_MESH_MODEL_ID_DFD_SRV                   0x1404
352 /** Firmware Distribution Client */
353 #define BT_MESH_MODEL_ID_DFD_CLI                   0x1405
354 /**
355  * @}
356  */
357 
358 /** Model opcode handler. */
359 struct bt_mesh_model_op {
360 	/** OpCode encoded using the BT_MESH_MODEL_OP_* macros */
361 	const uint32_t  opcode;
362 
363 	/** Message length. If the message has variable length then this value
364 	 *  indicates minimum message length and should be positive. Handler
365 	 *  function should verify precise length based on the contents of the
366 	 *  message. If the message has fixed length then this value should
367 	 *  be negative. Use BT_MESH_LEN_* macros when defining this value.
368 	 */
369 	const ssize_t len;
370 
371 	/** @brief Handler function for this opcode.
372 	 *
373 	 *  @param model Model instance receiving the message.
374 	 *  @param ctx   Message context for the message.
375 	 *  @param buf   Message buffer containing the message payload, not
376 	 *               including the opcode.
377 	 *
378 	 *  @return Zero on success or (negative) error code otherwise.
379 	 */
380 	int (*const func)(const struct bt_mesh_model *model,
381 			  struct bt_mesh_msg_ctx *ctx,
382 			  struct net_buf_simple *buf);
383 };
384 
385 #define BT_MESH_MODEL_OP_1(b0) (b0)
386 #define BT_MESH_MODEL_OP_2(b0, b1) (((b0) << 8) | (b1))
387 #define BT_MESH_MODEL_OP_3(b0, cid) ((((b0) << 16) | 0xc00000) | (cid))
388 
389 /** Macro for encoding exact message length for fixed-length messages.  */
390 #define BT_MESH_LEN_EXACT(len) (-len)
391 /** Macro for encoding minimum message length for variable-length messages.  */
392 #define BT_MESH_LEN_MIN(len) (len)
393 
394 /** End of the opcode list. Must always be present. */
395 #define BT_MESH_MODEL_OP_END { 0, 0, NULL }
396 
397 #if !defined(__cplusplus) || defined(__DOXYGEN__)
398 /**
399  * @brief Helper to define an empty opcode list.
400  *
401  * This macro uses compound literal feature of C99 standard and thus is available only from C,
402  * not C++.
403  */
404 #define BT_MESH_MODEL_NO_OPS ((struct bt_mesh_model_op []) \
405 			      { BT_MESH_MODEL_OP_END })
406 
407 /**
408  *  @brief Helper to define an empty model array
409  *
410  * This macro uses compound literal feature of C99 standard and thus is available only from C,
411  * not C++.
412  */
413 #define BT_MESH_MODEL_NONE ((const struct bt_mesh_model []){})
414 
415 /**
416  *  @brief Composition data SIG model entry with callback functions
417  *	   with specific number of keys & groups.
418  *
419  * This macro uses compound literal feature of C99 standard and thus is available only from C,
420  * not C++.
421  *
422  *  @param _id        Model ID.
423  *  @param _op        Array of model opcode handlers.
424  *  @param _pub       Model publish parameters.
425  *  @param _user_data User data for the model.
426  *  @param _keys      Number of keys that can be bound to the model.
427  *		      Shall not exceed @kconfig{CONFIG_BT_MESH_MODEL_KEY_COUNT}.
428  *  @param _grps      Number of addresses that the model can be subscribed to.
429  *		      Shall not exceed @kconfig{CONFIG_BT_MESH_MODEL_GROUP_COUNT}.
430  *  @param _cb        Callback structure, or NULL to keep no callbacks.
431  */
432 #define BT_MESH_MODEL_CNT_CB(_id, _op, _pub, _user_data, _keys, _grps, _cb)	\
433 {										\
434 	.id = (_id),								\
435 	BT_MESH_MODEL_RUNTIME_INIT(_user_data)					\
436 	.pub = _pub,								\
437 	.keys = (uint16_t []) BT_MESH_MODEL_KEYS_UNUSED(_keys),			\
438 	.keys_cnt = _keys,							\
439 	.groups = (uint16_t []) BT_MESH_MODEL_GROUPS_UNASSIGNED(_grps),		\
440 	.groups_cnt = _grps,							\
441 	BT_MESH_MODEL_UUIDS_UNASSIGNED()					\
442 	.op = _op,								\
443 	.cb = _cb,								\
444 }
445 
446 /**
447  *  @brief Composition data vendor model entry with callback functions
448  *	   with specific number of keys & groups.
449  *
450  * This macro uses compound literal feature of C99 standard and thus is available only from C,
451  * not C++.
452  *
453  *  @param _company   Company ID.
454  *  @param _id        Model ID.
455  *  @param _op        Array of model opcode handlers.
456  *  @param _pub       Model publish parameters.
457  *  @param _user_data User data for the model.
458  *  @param _keys      Number of keys that can be bound to the model.
459  *		      Shall not exceed @kconfig{CONFIG_BT_MESH_MODEL_KEY_COUNT}.
460  *  @param _grps      Number of addresses that the model can be subscribed to.
461  *		      Shall not exceed @kconfig{CONFIG_BT_MESH_MODEL_GROUP_COUNT}.
462  *  @param _cb        Callback structure, or NULL to keep no callbacks.
463  */
464 #define BT_MESH_MODEL_CNT_VND_CB(_company, _id, _op, _pub, _user_data, _keys, _grps, _cb)	\
465 {												\
466 	.vnd.company = (_company),								\
467 	.vnd.id = (_id),									\
468 	BT_MESH_MODEL_RUNTIME_INIT(_user_data)							\
469 	.op = _op,										\
470 	.pub = _pub,										\
471 	.keys = (uint16_t []) BT_MESH_MODEL_KEYS_UNUSED(_keys),					\
472 	.keys_cnt = _keys,									\
473 	.groups = (uint16_t []) BT_MESH_MODEL_GROUPS_UNASSIGNED(_grps),				\
474 	.groups_cnt = _grps,									\
475 	BT_MESH_MODEL_UUIDS_UNASSIGNED()							\
476 	.cb = _cb,										\
477 }
478 
479 /**
480  *  @brief Composition data SIG model entry with callback functions.
481  *
482  * This macro uses compound literal feature of C99 standard and thus is available only from C,
483  * not C++.
484  *
485  *  @param _id        Model ID.
486  *  @param _op        Array of model opcode handlers.
487  *  @param _pub       Model publish parameters.
488  *  @param _user_data User data for the model.
489  *  @param _cb        Callback structure, or NULL to keep no callbacks.
490  */
491 #define BT_MESH_MODEL_CB(_id, _op, _pub, _user_data, _cb)	\
492 	BT_MESH_MODEL_CNT_CB(_id, _op, _pub, _user_data,	\
493 			     CONFIG_BT_MESH_MODEL_KEY_COUNT,	\
494 			     CONFIG_BT_MESH_MODEL_GROUP_COUNT, _cb)
495 
496 
497 /**
498  *
499  *  @brief Composition data SIG model entry with callback functions and metadata.
500  *
501  * This macro uses compound literal feature of C99 standard and thus is available only from C,
502  * not C++.
503  *
504  *  @param _id        Model ID.
505  *  @param _op        Array of model opcode handlers.
506  *  @param _pub       Model publish parameters.
507  *  @param _user_data User data for the model.
508  *  @param _cb        Callback structure, or NULL to keep no callbacks.
509  *  @param _metadata  Metadata structure. Used if @kconfig{CONFIG_BT_MESH_LARGE_COMP_DATA_SRV}
510  *		      is enabled.
511  */
512 #if defined(CONFIG_BT_MESH_LARGE_COMP_DATA_SRV)
513 #define BT_MESH_MODEL_METADATA_CB(_id, _op, _pub, _user_data, _cb, _metadata)                    \
514 {                                                                            \
515 	.id = (_id),                                                         \
516 	BT_MESH_MODEL_RUNTIME_INIT(_user_data)				     \
517 	.pub = _pub,                                                         \
518 	.keys = (uint16_t []) BT_MESH_MODEL_KEYS_UNUSED(CONFIG_BT_MESH_MODEL_KEY_COUNT), \
519 	.keys_cnt = CONFIG_BT_MESH_MODEL_KEY_COUNT,                          \
520 	.groups = (uint16_t []) BT_MESH_MODEL_GROUPS_UNASSIGNED(CONFIG_BT_MESH_MODEL_GROUP_COUNT), \
521 	.groups_cnt = CONFIG_BT_MESH_MODEL_GROUP_COUNT,                      \
522 	BT_MESH_MODEL_UUIDS_UNASSIGNED()                                     \
523 	.op = _op,                                                           \
524 	.cb = _cb,                                                           \
525 	.metadata = _metadata,                                               \
526 }
527 #else
528 #define BT_MESH_MODEL_METADATA_CB(_id, _op, _pub, _user_data, _cb, _metadata)  \
529 	BT_MESH_MODEL_CB(_id, _op, _pub, _user_data, _cb)
530 #endif
531 
532 /**
533  *
534  *  @brief Composition data vendor model entry with callback functions.
535  *
536  * This macro uses compound literal feature of C99 standard and thus is available only from C,
537  * not C++.
538  *
539  *  @param _company   Company ID.
540  *  @param _id        Model ID.
541  *  @param _op        Array of model opcode handlers.
542  *  @param _pub       Model publish parameters.
543  *  @param _user_data User data for the model.
544  *  @param _cb        Callback structure, or NULL to keep no callbacks.
545  */
546 #define BT_MESH_MODEL_VND_CB(_company, _id, _op, _pub, _user_data, _cb)	\
547 	BT_MESH_MODEL_CNT_VND_CB(_company, _id, _op, _pub, _user_data,	\
548 				 CONFIG_BT_MESH_MODEL_KEY_COUNT,	\
549 				 CONFIG_BT_MESH_MODEL_GROUP_COUNT, _cb)
550 
551 /**
552  *
553  *  @brief Composition data vendor model entry with callback functions and metadata.
554  *
555  * This macro uses compound literal feature of C99 standard and thus is available only from C,
556  * not C++.
557  *
558  *  @param _company   Company ID.
559  *  @param _id        Model ID.
560  *  @param _op        Array of model opcode handlers.
561  *  @param _pub       Model publish parameters.
562  *  @param _user_data User data for the model.
563  *  @param _cb        Callback structure, or NULL to keep no callbacks.
564  *  @param _metadata  Metadata structure. Used if @kconfig{CONFIG_BT_MESH_LARGE_COMP_DATA_SRV}
565  *		      is enabled.
566  */
567 #if defined(CONFIG_BT_MESH_LARGE_COMP_DATA_SRV)
568 #define BT_MESH_MODEL_VND_METADATA_CB(_company, _id, _op, _pub, _user_data, _cb, _metadata)      \
569 {                                                                            \
570 	.vnd.company = (_company),                                           \
571 	.vnd.id = (_id),                                                     \
572 	BT_MESH_MODEL_RUNTIME_INIT(_user_data)				     \
573 	.op = _op,                                                           \
574 	.pub = _pub,                                                         \
575 	.keys = (uint16_t []) BT_MESH_MODEL_KEYS_UNUSED(CONFIG_BT_MESH_MODEL_KEY_COUNT), \
576 	.keys_cnt = CONFIG_BT_MESH_MODEL_KEY_COUNT,                          \
577 	.groups = (uint16_t []) BT_MESH_MODEL_GROUPS_UNASSIGNED(CONFIG_BT_MESH_MODEL_GROUP_COUNT), \
578 	.groups_cnt = CONFIG_BT_MESH_MODEL_GROUP_COUNT,                      \
579 	BT_MESH_MODEL_UUIDS_UNASSIGNED()                                     \
580 	.cb = _cb,                                                           \
581 	.metadata = _metadata,                                               \
582 }
583 #else
584 #define BT_MESH_MODEL_VND_METADATA_CB(_company, _id, _op, _pub, _user_data, _cb, _metadata)      \
585 	BT_MESH_MODEL_VND_CB(_company, _id, _op, _pub, _user_data, _cb)
586 #endif
587 /**
588  *  @brief Composition data SIG model entry.
589  *
590  * This macro uses compound literal feature of C99 standard and thus is available only from C,
591  * not C++.
592  *
593  *  @param _id        Model ID.
594  *  @param _op        Array of model opcode handlers.
595  *  @param _pub       Model publish parameters.
596  *  @param _user_data User data for the model.
597  */
598 #define BT_MESH_MODEL(_id, _op, _pub, _user_data)                              \
599 	BT_MESH_MODEL_CB(_id, _op, _pub, _user_data, NULL)
600 
601 /**
602  *  @brief Composition data vendor model entry.
603  *
604  * This macro uses compound literal feature of C99 standard and thus is available only from C,
605  * not C++.
606  *
607  *  @param _company   Company ID.
608  *  @param _id        Model ID.
609  *  @param _op        Array of model opcode handlers.
610  *  @param _pub       Model publish parameters.
611  *  @param _user_data User data for the model.
612  */
613 #define BT_MESH_MODEL_VND(_company, _id, _op, _pub, _user_data)                \
614 	BT_MESH_MODEL_VND_CB(_company, _id, _op, _pub, _user_data, NULL)
615 #endif /* !defined(__cplusplus) || defined(__DOXYGEN__) */
616 
617 /**
618  *  @brief Encode transmission count & interval steps.
619  *
620  *  @param count   Number of retransmissions (first transmission is excluded).
621  *  @param int_ms  Interval steps in milliseconds. Must be greater than 0,
622  *                 less than or equal to 320, and a multiple of 10.
623  *
624  *  @return Mesh transmit value that can be used e.g. for the default
625  *          values of the configuration model data.
626  */
627 #define BT_MESH_TRANSMIT(count, int_ms) ((count) | (((int_ms / 10) - 1) << 3))
628 
629 /**
630  *  @brief Decode transmit count from a transmit value.
631  *
632  *  @param transmit Encoded transmit count & interval value.
633  *
634  *  @return Transmission count (actual transmissions is N + 1).
635  */
636 #define BT_MESH_TRANSMIT_COUNT(transmit) (((transmit) & (uint8_t)BIT_MASK(3)))
637 
638 /**
639  *  @brief Decode transmit interval from a transmit value.
640  *
641  *  @param transmit Encoded transmit count & interval value.
642  *
643  *  @return Transmission interval in milliseconds.
644  */
645 #define BT_MESH_TRANSMIT_INT(transmit) ((((transmit) >> 3) + 1) * 10)
646 
647 /**
648  *  @brief Encode Publish Retransmit count & interval steps.
649  *
650  *  @param count  Number of retransmissions (first transmission is excluded).
651  *  @param int_ms Interval steps in milliseconds. Must be greater than 0 and a
652  *                multiple of 50.
653  *
654  *  @return Mesh transmit value that can be used e.g. for the default
655  *          values of the configuration model data.
656  */
657 #define BT_MESH_PUB_TRANSMIT(count, int_ms) BT_MESH_TRANSMIT(count,           \
658 							     (int_ms) / 5)
659 
660 /**
661  *  @brief Decode Publish Retransmit count from a given value.
662  *
663  *  @param transmit Encoded Publish Retransmit count & interval value.
664  *
665  *  @return Retransmission count (actual transmissions is N + 1).
666  */
667 #define BT_MESH_PUB_TRANSMIT_COUNT(transmit) BT_MESH_TRANSMIT_COUNT(transmit)
668 
669 /**
670  *  @brief Decode Publish Retransmit interval from a given value.
671  *
672  *  @param transmit Encoded Publish Retransmit count & interval value.
673  *
674  *  @return Transmission interval in milliseconds.
675  */
676 #define BT_MESH_PUB_TRANSMIT_INT(transmit) ((((transmit) >> 3) + 1) * 50)
677 
678 /**
679  * @brief Get total number of messages within one publication interval including initial
680  * publication.
681  *
682  * @param pub Model publication context.
683  *
684  * @return total number of messages.
685  */
686 #define BT_MESH_PUB_MSG_TOTAL(pub) (BT_MESH_PUB_TRANSMIT_COUNT((pub)->retransmit) + 1)
687 
688 /**
689  * @brief Get message number within one publication interval.
690  *
691  * Meant to be used inside @ref bt_mesh_model_pub.update.
692  *
693  * @param pub Model publication context.
694  *
695  * @return message number starting from 1.
696  */
697 #define BT_MESH_PUB_MSG_NUM(pub) (BT_MESH_PUB_TRANSMIT_COUNT((pub)->retransmit) + 1 - (pub)->count)
698 
699 /** Model publication context.
700  *
701  *  The context should primarily be created using the
702  *  BT_MESH_MODEL_PUB_DEFINE macro.
703  */
704 struct bt_mesh_model_pub {
705 	/** The model the context belongs to. Initialized by the stack. */
706 	const struct bt_mesh_model *mod;
707 
708 	uint16_t addr;          /**< Publish Address. */
709 	const uint8_t *uuid;    /**< Label UUID if Publish Address is Virtual Address. */
710 	uint16_t key:12,        /**< Publish AppKey Index. */
711 		 cred:1,        /**< Friendship Credentials Flag. */
712 		 send_rel:1,    /**< Force reliable sending (segment acks) */
713 		 fast_period:1, /**< Use FastPeriodDivisor */
714 		 retr_update:1; /**< Call update callback on every retransmission. */
715 
716 	uint8_t  ttl;          /**< Publish Time to Live. */
717 	uint8_t  retransmit;   /**< Retransmit Count & Interval Steps. */
718 	uint8_t  period;       /**< Publish Period. */
719 	uint8_t  period_div:4, /**< Divisor for the Period. */
720 		 count:4;      /**< Transmissions left. */
721 
722 	uint8_t delayable:1;   /**< Use random delay for publishing. */
723 
724 	uint32_t period_start; /**< Start of the current period. */
725 
726 	/** @brief Publication buffer, containing the publication message.
727 	 *
728 	 *  This will get correctly created when the publication context
729 	 *  has been defined using the BT_MESH_MODEL_PUB_DEFINE macro.
730 	 *
731 	 *	BT_MESH_MODEL_PUB_DEFINE(name, update, size);
732 	 */
733 	struct net_buf_simple *msg;
734 
735 	/** @brief Callback for updating the publication buffer.
736 	 *
737 	 *  When set to NULL, the model is assumed not to support
738 	 *  periodic publishing. When set to non-NULL the callback
739 	 *  will be called periodically and is expected to update
740 	 *  @ref bt_mesh_model_pub.msg with a valid publication
741 	 *  message.
742 	 *
743 	 *  If the callback returns non-zero, the publication is skipped
744 	 *  and will resume on the next periodic publishing interval.
745 	 *
746 	 *  When @ref bt_mesh_model_pub.retr_update is set to 1,
747 	 *  the callback will be called on every retransmission.
748 	 *
749 	 *  @param mod The Model the Publication Context belongs to.
750 	 *
751 	 *  @return Zero on success or (negative) error code otherwise.
752 	 */
753 	int (*update)(const struct bt_mesh_model *mod);
754 
755 	/** Publish Period Timer. Only for stack-internal use. */
756 	struct k_work_delayable timer;
757 };
758 
759 /**
760  *  Define a model publication context.
761  *
762  *  @param _name Variable name given to the context.
763  *  @param _update Optional message update callback (may be NULL).
764  *  @param _msg_len Length of the publication message.
765  */
766 #define BT_MESH_MODEL_PUB_DEFINE(_name, _update, _msg_len) \
767 	NET_BUF_SIMPLE_DEFINE_STATIC(bt_mesh_pub_msg_##_name, _msg_len); \
768 	static struct bt_mesh_model_pub _name = { \
769 		.msg = &bt_mesh_pub_msg_##_name, \
770 		.update = _update, \
771 	}
772 
773 /** Models Metadata Entry struct
774  *
775  *  The struct should primarily be created using the
776  *  BT_MESH_MODELS_METADATA_ENTRY macro.
777  */
778 struct bt_mesh_models_metadata_entry {
779 	/* Length of the metadata */
780 	const uint16_t len;
781 
782 	/* ID of the metadata */
783 	const uint16_t id;
784 
785 	/* Pointer to raw data */
786 	const void * const data;
787 };
788 
789 /**
790  *
791  *  Initialize a Models Metadata entry structure in a list.
792  *
793  *  @param _len Length of the metadata entry.
794  *  @param _id ID of the Models Metadata entry.
795  *  @param _data Pointer to a contiguous memory that contains the metadata.
796  */
797 #define BT_MESH_MODELS_METADATA_ENTRY(_len, _id, _data)                         \
798 	{                                                                      \
799 		.len = (_len), .id = _id, .data = _data,                       \
800 	}
801 
802 /** Helper to define an empty Models metadata array */
803 #define BT_MESH_MODELS_METADATA_NONE NULL
804 
805 /** End of the Models Metadata list. Must always be present. */
806 #define BT_MESH_MODELS_METADATA_END { 0, 0, NULL }
807 
808 /** Model callback functions. */
809 struct bt_mesh_model_cb {
810 	/** @brief Set value handler of user data tied to the model.
811 	 *
812 	 *  @sa settings_handler::h_set
813 	 *
814 	 *  @param model   Model to set the persistent data of.
815 	 *  @param name    Name/key of the settings item.
816 	 *  @param len_rd  The size of the data found in the backend.
817 	 *  @param read_cb Function provided to read the data from the backend.
818 	 *  @param cb_arg  Arguments for the read function provided by the
819 	 *                 backend.
820 	 *
821 	 *  @return 0 on success, error otherwise.
822 	 */
823 	int (*const settings_set)(const struct bt_mesh_model *model,
824 				  const char *name, size_t len_rd,
825 				  settings_read_cb read_cb, void *cb_arg);
826 
827 	/** @brief Callback called when the mesh is started.
828 	 *
829 	 *  This handler gets called after the node has been provisioned, or
830 	 *  after all mesh data has been loaded from persistent storage.
831 	 *
832 	 *  When this callback fires, the mesh model may start its behavior,
833 	 *  and all Access APIs are ready for use.
834 	 *
835 	 *  @param model      Model this callback belongs to.
836 	 *
837 	 *  @return 0 on success, error otherwise.
838 	 */
839 	int (*const start)(const struct bt_mesh_model *model);
840 
841 	/** @brief Model init callback.
842 	 *
843 	 *  Called on every model instance during mesh initialization.
844 	 *
845 	 *  If any of the model init callbacks return an error, the Mesh
846 	 *  subsystem initialization will be aborted, and the error will be
847 	 *  returned to the caller of @ref bt_mesh_init.
848 	 *
849 	 *  @param model Model to be initialized.
850 	 *
851 	 *  @return 0 on success, error otherwise.
852 	 */
853 	int (*const init)(const struct bt_mesh_model *model);
854 
855 	/** @brief Model reset callback.
856 	 *
857 	 *  Called when the mesh node is reset. All model data is deleted on
858 	 *  reset, and the model should clear its state.
859 	 *
860 	 *  @note If the model stores any persistent data, this needs to be
861 	 *  erased manually.
862 	 *
863 	 *  @param model Model this callback belongs to.
864 	 */
865 	void (*const reset)(const struct bt_mesh_model *model);
866 
867 	/** @brief Callback used to store pending model's user data.
868 	 *
869 	 *  Triggered by @ref bt_mesh_model_data_store_schedule.
870 	 *
871 	 *  To store the user data, call @ref bt_mesh_model_data_store.
872 	 *
873 	 *  @param model Model this callback belongs to.
874 	 */
875 	void (*const pending_store)(const struct bt_mesh_model *model);
876 };
877 
878 /** Vendor model ID */
879 struct bt_mesh_mod_id_vnd {
880 	/** Vendor's company ID */
881 	uint16_t company;
882 	/** Model ID */
883 	uint16_t id;
884 };
885 
886 /** Abstraction that describes a Mesh Model instance */
887 struct bt_mesh_model {
888 	union {
889 		/** SIG model ID */
890 		const uint16_t id;
891 		/** Vendor model ID */
892 		const struct bt_mesh_mod_id_vnd vnd;
893 	};
894 
895 	/* Model runtime information */
896 	struct bt_mesh_model_rt_ctx {
897 		uint8_t  elem_idx;   /* Belongs to Nth element */
898 		uint8_t  mod_idx;    /* Is the Nth model in the element */
899 		uint16_t flags;      /* Model flags for internal bookkeeping */
900 
901 #ifdef CONFIG_BT_MESH_MODEL_EXTENSIONS
902 		/* Pointer to the next model in a model extension list. */
903 		const struct bt_mesh_model *next;
904 #endif
905 		/** Model-specific user data */
906 		void *user_data;
907 	} * const rt;
908 
909 	/** Model Publication */
910 	struct bt_mesh_model_pub * const pub;
911 
912 	/** AppKey List */
913 	uint16_t * const keys;
914 	const uint16_t keys_cnt;
915 
916 	/** Subscription List (group or virtual addresses) */
917 	uint16_t * const groups;
918 	const uint16_t groups_cnt;
919 
920 #if (CONFIG_BT_MESH_LABEL_COUNT > 0) || defined(__DOXYGEN__)
921 	/** List of Label UUIDs the model is subscribed to. */
922 	const uint8_t ** const uuids;
923 #endif
924 
925 	/** Opcode handler list */
926 	const struct bt_mesh_model_op * const op;
927 
928 	/** Model callback structure. */
929 	const struct bt_mesh_model_cb * const cb;
930 
931 #if defined(CONFIG_BT_MESH_LARGE_COMP_DATA_SRV) || defined(__DOXYGEN__)
932 	/* Pointer to the array of model metadata entries. */
933 	const struct bt_mesh_models_metadata_entry * const metadata;
934 #endif
935 };
936 
937 /** Callback structure for monitoring model message sending */
938 struct bt_mesh_send_cb {
939 	/** @brief Handler called at the start of the transmission.
940 	 *
941 	 *  @param duration The duration of the full transmission.
942 	 *  @param err      Error occurring during sending.
943 	 *  @param cb_data  Callback data, as passed to the send API.
944 	 */
945 	void (*start)(uint16_t duration, int err, void *cb_data);
946 	/** @brief Handler called at the end of the transmission.
947 	 *
948 	 *  @param err     Error occurring during sending.
949 	 *  @param cb_data Callback data, as passed to the send API.
950 	 */
951 	void (*end)(int err, void *cb_data);
952 };
953 
954 
955 /** Special TTL value to request using configured default TTL */
956 #define BT_MESH_TTL_DEFAULT 0xff
957 
958 /** Maximum allowed TTL value */
959 #define BT_MESH_TTL_MAX     0x7f
960 
961 /** @brief Send an Access Layer message.
962  *
963  *  @param model   Mesh (client) Model that the message belongs to.
964  *  @param ctx     Message context, includes keys, TTL, etc.
965  *  @param msg     Access Layer payload (the actual message to be sent).
966  *  @param cb      Optional "message sent" callback.
967  *  @param cb_data User data to be passed to the callback.
968  *
969  *  @return 0 on success, or (negative) error code on failure.
970  */
971 int bt_mesh_model_send(const struct bt_mesh_model *model,
972 		       struct bt_mesh_msg_ctx *ctx,
973 		       struct net_buf_simple *msg,
974 		       const struct bt_mesh_send_cb *cb,
975 		       void *cb_data);
976 
977 /** @brief Send a model publication message.
978  *
979  *  Before calling this function, the user needs to ensure that the model
980  *  publication message (@ref bt_mesh_model_pub.msg) contains a valid
981  *  message to be sent. Note that this API is only to be used for
982  *  non-period publishing. For periodic publishing the app only needs
983  *  to make sure that @ref bt_mesh_model_pub.msg contains a valid message
984  *  whenever the @ref bt_mesh_model_pub.update callback is called.
985  *
986  *  @param model Mesh (client) Model that's publishing the message.
987  *
988  *  @return 0 on success, or (negative) error code on failure.
989  */
990 int bt_mesh_model_publish(const struct bt_mesh_model *model);
991 
992 /** @brief Check if a message is being retransmitted.
993  *
994  * Meant to be used inside the @ref bt_mesh_model_pub.update callback.
995  *
996  * @param model Mesh Model that supports publication.
997  *
998  * @return true if this is a retransmission, false if this is a first publication.
999  */
bt_mesh_model_pub_is_retransmission(const struct bt_mesh_model * model)1000 static inline bool bt_mesh_model_pub_is_retransmission(const struct bt_mesh_model *model)
1001 {
1002 	return model->pub->count != BT_MESH_PUB_TRANSMIT_COUNT(model->pub->retransmit);
1003 }
1004 
1005 /** @brief Get the element that a model belongs to.
1006  *
1007  *  @param mod Mesh model.
1008  *
1009  *  @return Pointer to the element that the given model belongs to.
1010  */
1011 const struct bt_mesh_elem *bt_mesh_model_elem(const struct bt_mesh_model *mod);
1012 
1013 /** @brief Find a SIG model.
1014  *
1015  *  @param elem Element to search for the model in.
1016  *  @param id   Model ID of the model.
1017  *
1018  *  @return A pointer to the Mesh model matching the given parameters, or NULL
1019  *          if no SIG model with the given ID exists in the given element.
1020  */
1021 const struct bt_mesh_model *bt_mesh_model_find(const struct bt_mesh_elem *elem,
1022 					       uint16_t id);
1023 
1024 /** @brief Find a vendor model.
1025  *
1026  *  @param elem    Element to search for the model in.
1027  *  @param company Company ID of the model.
1028  *  @param id      Model ID of the model.
1029  *
1030  *  @return A pointer to the Mesh model matching the given parameters, or NULL
1031  *          if no vendor model with the given ID exists in the given element.
1032  */
1033 const struct bt_mesh_model *bt_mesh_model_find_vnd(const struct bt_mesh_elem *elem,
1034 						   uint16_t company, uint16_t id);
1035 
1036 /** @brief Get whether the model is in the primary element of the device.
1037  *
1038  *  @param mod Mesh model.
1039  *
1040  *  @return true if the model is on the primary element, false otherwise.
1041  */
bt_mesh_model_in_primary(const struct bt_mesh_model * mod)1042 static inline bool bt_mesh_model_in_primary(const struct bt_mesh_model *mod)
1043 {
1044 	return (mod->rt->elem_idx == 0);
1045 }
1046 
1047 /** @brief Immediately store the model's user data in persistent storage.
1048  *
1049  *  @param mod      Mesh model.
1050  *  @param vnd      This is a vendor model.
1051  *  @param name     Name/key of the settings item. Only
1052  *                  @ref SETTINGS_MAX_DIR_DEPTH bytes will be used at most.
1053  *  @param data     Model data to store, or NULL to delete any model data.
1054  *  @param data_len Length of the model data.
1055  *
1056  *  @return 0 on success, or (negative) error code on failure.
1057  */
1058 int bt_mesh_model_data_store(const struct bt_mesh_model *mod, bool vnd,
1059 			     const char *name, const void *data,
1060 			     size_t data_len);
1061 
1062 /** @brief Schedule the model's user data store in persistent storage.
1063  *
1064  *  This function triggers the @ref bt_mesh_model_cb.pending_store callback
1065  *  for the corresponding model after delay defined by
1066  *  @kconfig{CONFIG_BT_MESH_STORE_TIMEOUT}.
1067  *
1068  *  The delay is global for all models. Once scheduled, the callback can
1069  *  not be re-scheduled until previous schedule completes.
1070  *
1071  *  @param mod      Mesh model.
1072  */
1073 void bt_mesh_model_data_store_schedule(const struct bt_mesh_model *mod);
1074 
1075 /** @brief Let a model extend another.
1076  *
1077  *  Mesh models may be extended to reuse their functionality, forming a more
1078  *  complex model. A Mesh model may extend any number of models, in any element.
1079  *  The extensions may also be nested, ie a model that extends another may
1080  *  itself be extended.
1081  *
1082  *  A set of models that extend each other form a model extension list.
1083  *
1084  *  All models in an extension list share one subscription list per element. The
1085  *  access layer will utilize the combined subscription list of all models in an
1086  *  extension list and element, giving the models extended subscription list
1087  *  capacity.
1088  *
1089  * If @kconfig{CONFIG_BT_MESH_COMP_PAGE_1} is enabled, it is not allowed to call
1090  * this function before the @ref bt_mesh_model_cb.init callback is called
1091  * for both models, except if it is called as part of the final callback.
1092  *
1093  *  @param extending_mod      Mesh model that is extending the base model.
1094  *  @param base_mod           The model being extended.
1095  *
1096  *  @retval 0 Successfully extended the base_mod model.
1097  */
1098 int bt_mesh_model_extend(const struct bt_mesh_model *extending_mod,
1099 			 const struct bt_mesh_model *base_mod);
1100 
1101 /** @brief Let a model correspond to another.
1102  *
1103  *  Mesh models may correspond to each other, which means that if one is present,
1104  *  other must be present too. A Mesh model may correspond to any number of models,
1105  *  in any element. All models connected together via correspondence form single
1106  *  Correspondence Group, which has it's unique Correspondence ID. Information about
1107  *  Correspondence is used to construct Composition Data Page 1.
1108  *
1109  *  This function must be called on already initialized base_mod. Because this function
1110  *  is designed to be called in corresponding_mod initializer, this means that base_mod
1111  *  shall be initialized before corresponding_mod is.
1112  *
1113  *  @param corresponding_mod  Mesh model that is corresponding to the base model.
1114  *  @param base_mod           The model being corresponded to.
1115  *
1116  *  @retval 0 Successfully saved correspondence to the base_mod model.
1117  *  @retval -ENOMEM   There is no more space to save this relation.
1118  *  @retval -ENOTSUP  Composition Data Page 1 is not supported.
1119  */
1120 
1121 int bt_mesh_model_correspond(const struct bt_mesh_model *corresponding_mod,
1122 			     const struct bt_mesh_model *base_mod);
1123 
1124 /** @brief Check if model is extended by another model.
1125  *
1126  *  @param model The model to check.
1127  *
1128  *  @retval true If model is extended by another model, otherwise false
1129  */
1130 bool bt_mesh_model_is_extended(const struct bt_mesh_model *model);
1131 
1132 /** @brief Indicate that the composition data will change on next bootup.
1133  *
1134  *  Tell the config server that the composition data is expected to change on
1135  *  the next bootup, and the current composition data should be backed up.
1136  *
1137  *  @return Zero on success or (negative) error code otherwise.
1138  */
1139 int bt_mesh_comp_change_prepare(void);
1140 
1141 /** @brief Indicate that the metadata will change on next bootup.
1142  *
1143  *  Tell the config server that the models metadata is expected to change on
1144  *  the next bootup, and the current models metadata should be backed up.
1145  *
1146  *  @return Zero on success or (negative) error code otherwise.
1147  */
1148 int bt_mesh_models_metadata_change_prepare(void);
1149 
1150 /** Node Composition */
1151 struct bt_mesh_comp {
1152 	uint16_t cid; /**< Company ID */
1153 	uint16_t pid; /**< Product ID */
1154 	uint16_t vid; /**< Version ID */
1155 
1156 	size_t elem_count; /**< The number of elements in this device. */
1157 	const struct bt_mesh_elem *elem; /**< List of elements. */
1158 };
1159 
1160 /** Composition data page 2 record. */
1161 struct bt_mesh_comp2_record {
1162 	/** Mesh profile ID. */
1163 	uint16_t id;
1164 	/** Mesh Profile Version. */
1165 	struct {
1166 		/** Major version. */
1167 		uint8_t x;
1168 		/** Minor version. */
1169 		uint8_t y;
1170 		/** Z version. */
1171 		uint8_t z;
1172 	} version;
1173 	/** Element offset count. */
1174 	uint8_t elem_offset_cnt;
1175 	/** Element offset list. */
1176 	const uint8_t *elem_offset;
1177 	/** Length of additional data. */
1178 	uint16_t data_len;
1179 	/** Additional data. */
1180 	const void *data;
1181 };
1182 
1183 /** Node Composition data page 2 */
1184 struct bt_mesh_comp2 {
1185 	/** The number of Mesh Profile records on a device. */
1186 	size_t record_cnt;
1187 	/** List of records. */
1188 	const struct bt_mesh_comp2_record *record;
1189 };
1190 
1191 /** @brief Register composition data page 2 of the device.
1192  *
1193  *  Register Mesh Profiles information (Ref section 3.12 in
1194  *  Bluetooth SIG Assigned Numbers) for composition data
1195  *  page 2 of the device.
1196  *
1197  *  @note There must be at least one record present in @c comp2
1198  *
1199  *  @param comp2 Pointer to composition data page 2.
1200  *
1201  *  @return Zero on success or (negative) error code otherwise.
1202  */
1203 int bt_mesh_comp2_register(const struct bt_mesh_comp2 *comp2);
1204 
1205 #ifdef __cplusplus
1206 }
1207 #endif
1208 
1209 /**
1210  * @}
1211  */
1212 
1213 #endif /* ZEPHYR_INCLUDE_BLUETOOTH_MESH_ACCESS_H_ */
1214