Zephyr Project API  3.1.0
A Scalable Open Source RTOS
access.h
Go to the documentation of this file.
1
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
14#include <zephyr/sys/util.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_MODEL_KEYS_UNUSED \
21 { LISTIFY(CONFIG_BT_MESH_MODEL_KEY_COUNT, \
22 BT_MESH_KEY_UNUSED_ELT_, (,)) }
23#define BT_MESH_MODEL_GROUPS_UNASSIGNED \
24 { LISTIFY(CONFIG_BT_MESH_MODEL_GROUP_COUNT, \
25 BT_MESH_ADDR_UNASSIGNED_ELT_, (,)) }
26
34#ifdef __cplusplus
35extern "C" {
36#endif
37
38#define BT_MESH_ADDR_UNASSIGNED 0x0000
39#define BT_MESH_ADDR_ALL_NODES 0xffff
40#define BT_MESH_ADDR_PROXIES 0xfffc
41#define BT_MESH_ADDR_FRIENDS 0xfffd
42#define BT_MESH_ADDR_RELAYS 0xfffe
43
44#define BT_MESH_KEY_UNUSED 0xffff
45#define BT_MESH_KEY_ANY 0xffff
46#define BT_MESH_KEY_DEV 0xfffe
47#define BT_MESH_KEY_DEV_LOCAL BT_MESH_KEY_DEV
48#define BT_MESH_KEY_DEV_REMOTE 0xfffd
49#define BT_MESH_KEY_DEV_ANY 0xfffc
50
51#define BT_MESH_ADDR_IS_UNICAST(addr) ((addr) && (addr) < 0x8000)
52#define BT_MESH_ADDR_IS_GROUP(addr) ((addr) >= 0xc000 && (addr) < 0xff00)
53#define BT_MESH_ADDR_IS_FIXED_GROUP(addr) ((addr) >= 0xfffc && (addr) < 0xffff)
54#define BT_MESH_ADDR_IS_VIRTUAL(addr) ((addr) >= 0x8000 && (addr) < 0xc000)
55#define BT_MESH_ADDR_IS_RFU(addr) ((addr) >= 0xff00 && (addr) <= 0xfffb)
56
57#define BT_MESH_IS_DEV_KEY(key) (key == BT_MESH_KEY_DEV_LOCAL || \
58 key == BT_MESH_KEY_DEV_REMOTE)
59
61#define BT_MESH_APP_SEG_SDU_MAX 12
62
64#define BT_MESH_APP_UNSEG_SDU_MAX 15
65
67#if defined(CONFIG_BT_MESH_RX_SEG_MAX)
68#define BT_MESH_RX_SEG_MAX CONFIG_BT_MESH_RX_SEG_MAX
69#else
70#define BT_MESH_RX_SEG_MAX 0
71#endif
72
74#if defined(CONFIG_BT_MESH_TX_SEG_MAX)
75#define BT_MESH_TX_SEG_MAX CONFIG_BT_MESH_TX_SEG_MAX
76#else
77#define BT_MESH_TX_SEG_MAX 0
78#endif
79
81#define BT_MESH_TX_SDU_MAX MAX((BT_MESH_TX_SEG_MAX * \
82 BT_MESH_APP_SEG_SDU_MAX), \
83 BT_MESH_APP_UNSEG_SDU_MAX)
84
86#define BT_MESH_RX_SDU_MAX MAX((BT_MESH_RX_SEG_MAX * \
87 BT_MESH_APP_SEG_SDU_MAX), \
88 BT_MESH_APP_UNSEG_SDU_MAX)
89
99#define BT_MESH_ELEM(_loc, _mods, _vnd_mods) \
100{ \
101 .loc = (_loc), \
102 .model_count = ARRAY_SIZE(_mods), \
103 .vnd_model_count = ARRAY_SIZE(_vnd_mods), \
104 .models = (_mods), \
105 .vnd_models = (_vnd_mods), \
106}
107
112
119
121 struct bt_mesh_model * const models;
123 struct bt_mesh_model * const vnd_models;
124};
125
126/* Foundation Models */
127#define BT_MESH_MODEL_ID_CFG_SRV 0x0000
128#define BT_MESH_MODEL_ID_CFG_CLI 0x0001
129#define BT_MESH_MODEL_ID_HEALTH_SRV 0x0002
130#define BT_MESH_MODEL_ID_HEALTH_CLI 0x0003
131
132/* Models from the Mesh Model Specification */
133#define BT_MESH_MODEL_ID_GEN_ONOFF_SRV 0x1000
134#define BT_MESH_MODEL_ID_GEN_ONOFF_CLI 0x1001
135#define BT_MESH_MODEL_ID_GEN_LEVEL_SRV 0x1002
136#define BT_MESH_MODEL_ID_GEN_LEVEL_CLI 0x1003
137#define BT_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_SRV 0x1004
138#define BT_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_CLI 0x1005
139#define BT_MESH_MODEL_ID_GEN_POWER_ONOFF_SRV 0x1006
140#define BT_MESH_MODEL_ID_GEN_POWER_ONOFF_SETUP_SRV 0x1007
141#define BT_MESH_MODEL_ID_GEN_POWER_ONOFF_CLI 0x1008
142#define BT_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV 0x1009
143#define BT_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV 0x100a
144#define BT_MESH_MODEL_ID_GEN_POWER_LEVEL_CLI 0x100b
145#define BT_MESH_MODEL_ID_GEN_BATTERY_SRV 0x100c
146#define BT_MESH_MODEL_ID_GEN_BATTERY_CLI 0x100d
147#define BT_MESH_MODEL_ID_GEN_LOCATION_SRV 0x100e
148#define BT_MESH_MODEL_ID_GEN_LOCATION_SETUPSRV 0x100f
149#define BT_MESH_MODEL_ID_GEN_LOCATION_CLI 0x1010
150#define BT_MESH_MODEL_ID_GEN_ADMIN_PROP_SRV 0x1011
151#define BT_MESH_MODEL_ID_GEN_MANUFACTURER_PROP_SRV 0x1012
152#define BT_MESH_MODEL_ID_GEN_USER_PROP_SRV 0x1013
153#define BT_MESH_MODEL_ID_GEN_CLIENT_PROP_SRV 0x1014
154#define BT_MESH_MODEL_ID_GEN_PROP_CLI 0x1015
155#define BT_MESH_MODEL_ID_SENSOR_SRV 0x1100
156#define BT_MESH_MODEL_ID_SENSOR_SETUP_SRV 0x1101
157#define BT_MESH_MODEL_ID_SENSOR_CLI 0x1102
158#define BT_MESH_MODEL_ID_TIME_SRV 0x1200
159#define BT_MESH_MODEL_ID_TIME_SETUP_SRV 0x1201
160#define BT_MESH_MODEL_ID_TIME_CLI 0x1202
161#define BT_MESH_MODEL_ID_SCENE_SRV 0x1203
162#define BT_MESH_MODEL_ID_SCENE_SETUP_SRV 0x1204
163#define BT_MESH_MODEL_ID_SCENE_CLI 0x1205
164#define BT_MESH_MODEL_ID_SCHEDULER_SRV 0x1206
165#define BT_MESH_MODEL_ID_SCHEDULER_SETUP_SRV 0x1207
166#define BT_MESH_MODEL_ID_SCHEDULER_CLI 0x1208
167#define BT_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV 0x1300
168#define BT_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV 0x1301
169#define BT_MESH_MODEL_ID_LIGHT_LIGHTNESS_CLI 0x1302
170#define BT_MESH_MODEL_ID_LIGHT_CTL_SRV 0x1303
171#define BT_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV 0x1304
172#define BT_MESH_MODEL_ID_LIGHT_CTL_CLI 0x1305
173#define BT_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV 0x1306
174#define BT_MESH_MODEL_ID_LIGHT_HSL_SRV 0x1307
175#define BT_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV 0x1308
176#define BT_MESH_MODEL_ID_LIGHT_HSL_CLI 0x1309
177#define BT_MESH_MODEL_ID_LIGHT_HSL_HUE_SRV 0x130a
178#define BT_MESH_MODEL_ID_LIGHT_HSL_SAT_SRV 0x130b
179#define BT_MESH_MODEL_ID_LIGHT_XYL_SRV 0x130c
180#define BT_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV 0x130d
181#define BT_MESH_MODEL_ID_LIGHT_XYL_CLI 0x130e
182#define BT_MESH_MODEL_ID_LIGHT_LC_SRV 0x130f
183#define BT_MESH_MODEL_ID_LIGHT_LC_SETUPSRV 0x1310
184#define BT_MESH_MODEL_ID_LIGHT_LC_CLI 0x1311
185
190
198
208 int (*const func)(struct bt_mesh_model *model,
209 struct bt_mesh_msg_ctx *ctx,
210 struct net_buf_simple *buf);
211};
212
213#define BT_MESH_MODEL_OP_1(b0) (b0)
214#define BT_MESH_MODEL_OP_2(b0, b1) (((b0) << 8) | (b1))
215#define BT_MESH_MODEL_OP_3(b0, cid) ((((b0) << 16) | 0xc00000) | (cid))
216
218#define BT_MESH_LEN_EXACT(len) (-len)
220#define BT_MESH_LEN_MIN(len) (len)
221
223#define BT_MESH_MODEL_OP_END { 0, 0, NULL }
225#define BT_MESH_MODEL_NO_OPS ((struct bt_mesh_model_op []) \
226 { BT_MESH_MODEL_OP_END })
227
229#define BT_MESH_MODEL_NONE ((struct bt_mesh_model []){})
230
241#define BT_MESH_MODEL_CB(_id, _op, _pub, _user_data, _cb) \
242{ \
243 .id = (_id), \
244 .pub = _pub, \
245 .keys = BT_MESH_MODEL_KEYS_UNUSED, \
246 .groups = BT_MESH_MODEL_GROUPS_UNASSIGNED, \
247 .op = _op, \
248 .cb = _cb, \
249 .user_data = _user_data, \
250}
251
263#define BT_MESH_MODEL_VND_CB(_company, _id, _op, _pub, _user_data, _cb) \
264{ \
265 .vnd.company = (_company), \
266 .vnd.id = (_id), \
267 .op = _op, \
268 .pub = _pub, \
269 .keys = BT_MESH_MODEL_KEYS_UNUSED, \
270 .groups = BT_MESH_MODEL_GROUPS_UNASSIGNED, \
271 .user_data = _user_data, \
272 .cb = _cb, \
273}
274
275
285#define BT_MESH_MODEL(_id, _op, _pub, _user_data) \
286 BT_MESH_MODEL_CB(_id, _op, _pub, _user_data, NULL)
287
298#define BT_MESH_MODEL_VND(_company, _id, _op, _pub, _user_data) \
299 BT_MESH_MODEL_VND_CB(_company, _id, _op, _pub, _user_data, NULL)
300
312#define BT_MESH_TRANSMIT(count, int_ms) ((count) | (((int_ms / 10) - 1) << 3))
313
322#define BT_MESH_TRANSMIT_COUNT(transmit) (((transmit) & (uint8_t)BIT_MASK(3)))
323
332#define BT_MESH_TRANSMIT_INT(transmit) ((((transmit) >> 3) + 1) * 10)
333
345#define BT_MESH_PUB_TRANSMIT(count, int_ms) BT_MESH_TRANSMIT(count, \
346 (int_ms) / 5)
347
356#define BT_MESH_PUB_TRANSMIT_COUNT(transmit) BT_MESH_TRANSMIT_COUNT(transmit)
357
366#define BT_MESH_PUB_TRANSMIT_INT(transmit) ((((transmit) >> 3) + 1) * 50)
367
377#define BT_MESH_PUB_MSG_TOTAL(pub) (BT_MESH_PUB_TRANSMIT_COUNT((pub)->retransmit) + 1)
378
389#define BT_MESH_PUB_MSG_NUM(pub) (BT_MESH_PUB_TRANSMIT_COUNT((pub)->retransmit) + 1 - (pub)->count)
390
399
423
442 int (*update)(struct bt_mesh_model *mod);
443
446};
447
456#define BT_MESH_MODEL_PUB_DEFINE(_name, _update, _msg_len) \
457 NET_BUF_SIMPLE_DEFINE_STATIC(bt_mesh_pub_msg_##_name, _msg_len); \
458 static struct bt_mesh_model_pub _name = { \
459 .msg = &bt_mesh_pub_msg_##_name, \
460 .update = _update, \
461 }
462
478 int (*const settings_set)(struct bt_mesh_model *model,
479 const char *name, size_t len_rd,
480 settings_read_cb read_cb, void *cb_arg);
481
494 int (*const start)(struct bt_mesh_model *model);
495
508 int (*const init)(struct bt_mesh_model *model);
509
520 void (*const reset)(struct bt_mesh_model *model);
521};
522
529};
530
533 union {
538 };
539
540 /* Internal information, mainly for persistent storage */
541 uint8_t elem_idx; /* Belongs to Nth element */
542 uint8_t mod_idx; /* Is the Nth model in the element */
543 uint16_t flags; /* Model flags for internal bookkeeping */
544
546 struct bt_mesh_model_pub * const pub;
547
549 uint16_t keys[CONFIG_BT_MESH_MODEL_KEY_COUNT];
550
552 uint16_t groups[CONFIG_BT_MESH_MODEL_GROUP_COUNT];
553
555 const struct bt_mesh_model_op * const op;
556
558 const struct bt_mesh_model_cb * const cb;
559
560#ifdef CONFIG_BT_MESH_MODEL_EXTENSIONS
561 /* Pointer to the next model in a model extension list. */
563#endif
564
567};
568
577 void (*start)(uint16_t duration, int err, void *cb_data);
583 void (*end)(int err, void *cb_data);
584};
585
586
588#define BT_MESH_TTL_DEFAULT 0xff
589
591#define BT_MESH_TTL_MAX 0x7f
592
604 struct bt_mesh_msg_ctx *ctx,
605 struct net_buf_simple *msg,
606 const struct bt_mesh_send_cb *cb,
607 void *cb_data);
608
623
632static inline bool bt_mesh_model_pub_is_retransmission(const struct bt_mesh_model *model)
633{
634 return model->pub->count != BT_MESH_PUB_TRANSMIT_COUNT(model->pub->retransmit);
635}
636
644
654 uint16_t id);
655
666 uint16_t company, uint16_t id);
667
674static inline bool bt_mesh_model_in_primary(const struct bt_mesh_model *mod)
675{
676 return (mod->elem_idx == 0);
677}
678
691 const char *name, const void *data,
692 size_t data_len);
693
713int bt_mesh_model_extend(struct bt_mesh_model *extending_mod,
714 struct bt_mesh_model *base_mod);
715
723
730 size_t elem_count;
732};
733
734#ifdef __cplusplus
735}
736#endif
737
742#endif /* ZEPHYR_INCLUDE_BLUETOOTH_MESH_ACCESS_H_ */
bool bt_mesh_model_is_extended(struct bt_mesh_model *model)
Check if model is extended by another model.
#define BT_MESH_PUB_TRANSMIT_COUNT(transmit)
Decode Publish Retransmit count from a given value.
Definition: access.h:356
struct bt_mesh_elem * bt_mesh_model_elem(struct bt_mesh_model *mod)
Get the element that a model belongs to.
static bool bt_mesh_model_pub_is_retransmission(const struct bt_mesh_model *model)
Check if a message is being retransmitted.
Definition: access.h:632
struct bt_mesh_model * bt_mesh_model_find_vnd(const struct bt_mesh_elem *elem, uint16_t company, uint16_t id)
Find a vendor model.
int bt_mesh_model_publish(struct bt_mesh_model *model)
Send a model publication message.
struct bt_mesh_model * bt_mesh_model_find(const struct bt_mesh_elem *elem, uint16_t id)
Find a SIG model.
static bool bt_mesh_model_in_primary(const struct bt_mesh_model *mod)
Get whether the model is in the primary element of the device.
Definition: access.h:674
int bt_mesh_model_data_store(struct bt_mesh_model *mod, bool vnd, const char *name, const void *data, size_t data_len)
Immediately store the model's user data in persistent storage.
int bt_mesh_model_extend(struct bt_mesh_model *extending_mod, struct bt_mesh_model *base_mod)
Let a model extend another.
int bt_mesh_model_send(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *msg, const struct bt_mesh_send_cb *cb, void *cb_data)
Send an Access Layer message.
ssize_t(* settings_read_cb)(void *cb_arg, void *data, size_t len)
Definition: settings.h:57
__SIZE_TYPE__ ssize_t
Definition: types.h:28
Message APIs.
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
__UINT16_TYPE__ uint16_t
Definition: stdint.h:89
Definition: access.h:725
uint16_t pid
Definition: access.h:727
uint16_t cid
Definition: access.h:726
uint16_t vid
Definition: access.h:728
struct bt_mesh_elem * elem
Definition: access.h:731
size_t elem_count
Definition: access.h:730
Definition: access.h:109
struct bt_mesh_model *const models
Definition: access.h:121
const uint16_t loc
Definition: access.h:114
const uint8_t model_count
Definition: access.h:116
struct bt_mesh_model *const vnd_models
Definition: access.h:123
uint16_t addr
Definition: access.h:111
const uint8_t vnd_model_count
Definition: access.h:118
Definition: access.h:524
uint16_t company
Definition: access.h:526
uint16_t id
Definition: access.h:528
Definition: access.h:464
int(*const settings_set)(struct bt_mesh_model *model, const char *name, size_t len_rd, settings_read_cb read_cb, void *cb_arg)
Set value handler of user data tied to the model.
Definition: access.h:478
void(*const reset)(struct bt_mesh_model *model)
Model reset callback.
Definition: access.h:520
int(*const start)(struct bt_mesh_model *model)
Callback called when the mesh is started.
Definition: access.h:494
int(*const init)(struct bt_mesh_model *model)
Model init callback.
Definition: access.h:508
Definition: access.h:187
const uint32_t opcode
Definition: access.h:189
const ssize_t len
Definition: access.h:197
int(*const func)(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf)
Handler function for this opcode.
Definition: access.h:208
Definition: access.h:396
uint16_t key
Definition: access.h:401
uint16_t retr_update
Definition: access.h:405
int(* update)(struct bt_mesh_model *mod)
Callback for updating the publication buffer.
Definition: access.h:442
uint16_t cred
Definition: access.h:402
uint8_t retransmit
Definition: access.h:408
uint16_t addr
Definition: access.h:400
uint8_t count
Definition: access.h:411
uint8_t period_div
Definition: access.h:410
struct net_buf_simple * msg
Publication buffer, containing the publication message.
Definition: access.h:422
struct bt_mesh_model * mod
Definition: access.h:398
uint8_t ttl
Definition: access.h:407
uint32_t period_start
Definition: access.h:413
uint16_t send_rel
Definition: access.h:403
uint16_t fast_period
Definition: access.h:404
uint8_t period
Definition: access.h:409
struct k_work_delayable timer
Definition: access.h:445
Definition: access.h:532
uint8_t elem_idx
Definition: access.h:541
const struct bt_mesh_mod_id_vnd vnd
Definition: access.h:537
uint16_t keys[CONFIG_BT_MESH_MODEL_KEY_COUNT]
Definition: access.h:549
const struct bt_mesh_model_op *const op
Definition: access.h:555
uint16_t groups[CONFIG_BT_MESH_MODEL_GROUP_COUNT]
Definition: access.h:552
void * user_data
Definition: access.h:566
const struct bt_mesh_model_cb *const cb
Definition: access.h:558
uint16_t flags
Definition: access.h:543
const uint16_t id
Definition: access.h:535
uint8_t mod_idx
Definition: access.h:542
struct bt_mesh_model * next
Definition: access.h:562
struct bt_mesh_model_pub *const pub
Definition: access.h:546
Definition: msg.h:78
Definition: access.h:570
void(* start)(uint16_t duration, int err, void *cb_data)
Handler called at the start of the transmission.
Definition: access.h:577
void(* end)(int err, void *cb_data)
Handler called at the end of the transmission.
Definition: access.h:583
A structure used to submit work after a delay.
Definition: kernel.h:3670
Simple network buffer representation.
Definition: buf.h:83
static fdata_t data[2]
Definition: test_fifo_contexts.c:15
static void msg(uint64_t c64)
Definition: main.c:17
Misc utilities.