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