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