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