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