Zephyr Project API  3.2.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) >= 0xff00 && (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
240#define BT_MESH_MODEL_CB(_id, _op, _pub, _user_data, _cb) \
241{ \
242 .id = (_id), \
243 .pub = _pub, \
244 .keys = BT_MESH_MODEL_KEYS_UNUSED, \
245 .groups = BT_MESH_MODEL_GROUPS_UNASSIGNED, \
246 .op = _op, \
247 .cb = _cb, \
248 .user_data = _user_data, \
249}
250
261#define BT_MESH_MODEL_VND_CB(_company, _id, _op, _pub, _user_data, _cb) \
262{ \
263 .vnd.company = (_company), \
264 .vnd.id = (_id), \
265 .op = _op, \
266 .pub = _pub, \
267 .keys = BT_MESH_MODEL_KEYS_UNUSED, \
268 .groups = BT_MESH_MODEL_GROUPS_UNASSIGNED, \
269 .user_data = _user_data, \
270 .cb = _cb, \
271}
272
273
282#define BT_MESH_MODEL(_id, _op, _pub, _user_data) \
283 BT_MESH_MODEL_CB(_id, _op, _pub, _user_data, NULL)
284
294#define BT_MESH_MODEL_VND(_company, _id, _op, _pub, _user_data) \
295 BT_MESH_MODEL_VND_CB(_company, _id, _op, _pub, _user_data, NULL)
296
307#define BT_MESH_TRANSMIT(count, int_ms) ((count) | (((int_ms / 10) - 1) << 3))
308
316#define BT_MESH_TRANSMIT_COUNT(transmit) (((transmit) & (uint8_t)BIT_MASK(3)))
317
325#define BT_MESH_TRANSMIT_INT(transmit) ((((transmit) >> 3) + 1) * 10)
326
337#define BT_MESH_PUB_TRANSMIT(count, int_ms) BT_MESH_TRANSMIT(count, \
338 (int_ms) / 5)
339
347#define BT_MESH_PUB_TRANSMIT_COUNT(transmit) BT_MESH_TRANSMIT_COUNT(transmit)
348
356#define BT_MESH_PUB_TRANSMIT_INT(transmit) ((((transmit) >> 3) + 1) * 50)
357
366#define BT_MESH_PUB_MSG_TOTAL(pub) (BT_MESH_PUB_TRANSMIT_COUNT((pub)->retransmit) + 1)
367
377#define BT_MESH_PUB_MSG_NUM(pub) (BT_MESH_PUB_TRANSMIT_COUNT((pub)->retransmit) + 1 - (pub)->count)
378
387
411
430 int (*update)(struct bt_mesh_model *mod);
431
434};
435
443#define BT_MESH_MODEL_PUB_DEFINE(_name, _update, _msg_len) \
444 NET_BUF_SIMPLE_DEFINE_STATIC(bt_mesh_pub_msg_##_name, _msg_len); \
445 static struct bt_mesh_model_pub _name = { \
446 .msg = &bt_mesh_pub_msg_##_name, \
447 .update = _update, \
448 }
449
465 int (*const settings_set)(struct bt_mesh_model *model,
466 const char *name, size_t len_rd,
467 settings_read_cb read_cb, void *cb_arg);
468
481 int (*const start)(struct bt_mesh_model *model);
482
495 int (*const init)(struct bt_mesh_model *model);
496
507 void (*const reset)(struct bt_mesh_model *model);
508};
509
516};
517
520 union {
525 };
526
527 /* Internal information, mainly for persistent storage */
528 uint8_t elem_idx; /* Belongs to Nth element */
529 uint8_t mod_idx; /* Is the Nth model in the element */
530 uint16_t flags; /* Model flags for internal bookkeeping */
531
533 struct bt_mesh_model_pub * const pub;
534
536 uint16_t keys[CONFIG_BT_MESH_MODEL_KEY_COUNT];
537
539 uint16_t groups[CONFIG_BT_MESH_MODEL_GROUP_COUNT];
540
542 const struct bt_mesh_model_op * const op;
543
545 const struct bt_mesh_model_cb * const cb;
546
547#ifdef CONFIG_BT_MESH_MODEL_EXTENSIONS
548 /* Pointer to the next model in a model extension list. */
550#endif
551
554};
555
564 void (*start)(uint16_t duration, int err, void *cb_data);
570 void (*end)(int err, void *cb_data);
571};
572
573
575#define BT_MESH_TTL_DEFAULT 0xff
576
578#define BT_MESH_TTL_MAX 0x7f
579
591 struct bt_mesh_msg_ctx *ctx,
592 struct net_buf_simple *msg,
593 const struct bt_mesh_send_cb *cb,
594 void *cb_data);
595
610
619static inline bool bt_mesh_model_pub_is_retransmission(const struct bt_mesh_model *model)
620{
621 return model->pub->count != BT_MESH_PUB_TRANSMIT_COUNT(model->pub->retransmit);
622}
623
631
641 uint16_t id);
642
653 uint16_t company, uint16_t id);
654
661static inline bool bt_mesh_model_in_primary(const struct bt_mesh_model *mod)
662{
663 return (mod->elem_idx == 0);
664}
665
678 const char *name, const void *data,
679 size_t data_len);
680
700int bt_mesh_model_extend(struct bt_mesh_model *extending_mod,
701 struct bt_mesh_model *base_mod);
702
710
717 size_t elem_count;
719};
720
721#ifdef __cplusplus
722}
723#endif
724
729#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:347
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:619
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:661
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:712
uint16_t pid
Definition: access.h:714
uint16_t cid
Definition: access.h:713
uint16_t vid
Definition: access.h:715
struct bt_mesh_elem * elem
Definition: access.h:718
size_t elem_count
Definition: access.h:717
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:511
uint16_t company
Definition: access.h:513
uint16_t id
Definition: access.h:515
Definition: access.h:451
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:465
void(*const reset)(struct bt_mesh_model *model)
Model reset callback.
Definition: access.h:507
int(*const start)(struct bt_mesh_model *model)
Callback called when the mesh is started.
Definition: access.h:481
int(*const init)(struct bt_mesh_model *model)
Model init callback.
Definition: access.h:495
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:384
uint16_t key
Definition: access.h:389
uint16_t retr_update
Definition: access.h:393
int(* update)(struct bt_mesh_model *mod)
Callback for updating the publication buffer.
Definition: access.h:430
uint16_t cred
Definition: access.h:390
uint8_t retransmit
Definition: access.h:396
uint16_t addr
Definition: access.h:388
uint8_t count
Definition: access.h:399
uint8_t period_div
Definition: access.h:398
struct net_buf_simple * msg
Publication buffer, containing the publication message.
Definition: access.h:410
struct bt_mesh_model * mod
Definition: access.h:386
uint8_t ttl
Definition: access.h:395
uint32_t period_start
Definition: access.h:401
uint16_t send_rel
Definition: access.h:391
uint16_t fast_period
Definition: access.h:392
uint8_t period
Definition: access.h:397
struct k_work_delayable timer
Definition: access.h:433
Definition: access.h:519
uint8_t elem_idx
Definition: access.h:528
const struct bt_mesh_mod_id_vnd vnd
Definition: access.h:524
uint16_t keys[CONFIG_BT_MESH_MODEL_KEY_COUNT]
Definition: access.h:536
const struct bt_mesh_model_op *const op
Definition: access.h:542
uint16_t groups[CONFIG_BT_MESH_MODEL_GROUP_COUNT]
Definition: access.h:539
void * user_data
Definition: access.h:553
const struct bt_mesh_model_cb *const cb
Definition: access.h:545
uint16_t flags
Definition: access.h:530
const uint16_t id
Definition: access.h:522
uint8_t mod_idx
Definition: access.h:529
struct bt_mesh_model * next
Definition: access.h:549
struct bt_mesh_model_pub *const pub
Definition: access.h:533
Definition: msg.h:74
Definition: access.h:557
void(* start)(uint16_t duration, int err, void *cb_data)
Handler called at the start of the transmission.
Definition: access.h:564
void(* end)(int err, void *cb_data)
Handler called at the end of the transmission.
Definition: access.h:570
A structure used to submit work after a delay.
Definition: kernel.h:3692
Simple network buffer representation.
Definition: buf.h:82
static fdata_t data[2]
Definition: test_fifo_contexts.c:15
static void msg(uint64_t c64)
Definition: main.c:17
Misc utilities.