NimBLE-Arduino 2.1.2
Loading...
Searching...
No Matches
glue.h
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#ifndef _MESH_GLUE_
21#define _MESH_GLUE_
22
23#include <assert.h>
24#include <errno.h>
25
26#include "nimble/porting/nimble/include/syscfg/syscfg.h"
27#include "nimble/porting/nimble/include/logcfg/logcfg.h"
28#include "nimble/porting/nimble/include/modlog/modlog.h"
29#include "nimble/nimble/include/nimble/nimble_npl.h"
30
31#include "nimble/porting/nimble/include/os/os_mbuf.h"
32#include "nimble/porting/nimble/include/os/queue.h"
33
34#include "nimble/nimble/include/nimble/ble.h"
35#include "nimble/nimble/host/include/host/ble_hs.h"
36#include "nimble/nimble/host/include/host/ble_uuid.h"
37#include "nimble/nimble/host/src/ble_sm_priv.h"
38#include "nimble/nimble/host/src/ble_hs_hci_priv.h"
39
40#if MYNEWT_VAL(BLE_CRYPTO_STACK_MBEDTLS)
41#include "mbedtls/aes.h"
42#include "mbedtls/cipher.h"
43#include "mbedtls/entropy.h"
44#include "mbedtls/ctr_drbg.h"
45#include "mbedtls/cmac.h"
46#include "mbedtls/ecdh.h"
47#include "mbedtls/ecp.h"
48
49#else
55#endif
56
57#if MYNEWT_VAL(BLE_MESH_SETTINGS)
58#include "config/config.h"
59#endif
60
61#ifdef __cplusplus
62extern "C" {
63#endif
64
65#define SETTINGS_MAX_DIR_DEPTH 8 /* max depth of settings tree */
66
68#define BT_ECC_KEY_SIZE 32
70#define BT_PUB_KEY_COORD_LEN (BT_ECC_KEY_SIZE)
72#define BT_PUB_KEY_LEN (2 * (BT_PUB_KEY_COORD_LEN))
74#define BT_PRIV_KEY_LEN (BT_ECC_KEY_SIZE)
76#define BT_DH_KEY_LEN (BT_ECC_KEY_SIZE)
77
87#define BT_DATA(_type, _data, _data_len) \
88 { \
89 .type = (_type), \
90 .data_len = (_data_len), \
91 .data = (const uint8_t *)(_data), \
92 }
93
102#define BT_DATA_BYTES(_type, _bytes...) \
103 BT_DATA(_type, ((uint8_t []) { _bytes }), \
104 sizeof((uint8_t []) { _bytes }))
105
106/* EIR/AD data type definitions */
107#define BT_DATA_FLAGS 0x01 /* AD flags */
108#define BT_DATA_UUID16_SOME 0x02 /* 16-bit UUID, more available */
109#define BT_DATA_UUID16_ALL 0x03 /* 16-bit UUID, all listed */
110#define BT_DATA_UUID32_SOME 0x04 /* 32-bit UUID, more available */
111#define BT_DATA_UUID32_ALL 0x05 /* 32-bit UUID, all listed */
112#define BT_DATA_UUID128_SOME 0x06 /* 128-bit UUID, more available */
113#define BT_DATA_UUID128_ALL 0x07 /* 128-bit UUID, all listed */
114#define BT_DATA_NAME_SHORTENED 0x08 /* Shortened name */
115#define BT_DATA_NAME_COMPLETE 0x09 /* Complete name */
116#define BT_DATA_TX_POWER 0x0a /* Tx Power */
117#define BT_DATA_SOLICIT16 0x14 /* Solicit UUIDs, 16-bit */
118#define BT_DATA_SOLICIT128 0x15 /* Solicit UUIDs, 128-bit */
119#define BT_DATA_SVC_DATA16 0x16 /* Service data, 16-bit UUID */
120#define BT_DATA_GAP_APPEARANCE 0x19 /* GAP appearance */
121#define BT_DATA_SOLICIT32 0x1f /* Solicit UUIDs, 32-bit */
122#define BT_DATA_SVC_DATA32 0x20 /* Service data, 32-bit UUID */
123#define BT_DATA_SVC_DATA128 0x21 /* Service data, 128-bit UUID */
124#define BT_DATA_URI 0x24 /* URI */
125#define BT_DATA_MESH_PROV 0x29 /* Mesh Provisioning PDU */
126#define BT_DATA_MESH_MESSAGE 0x2a /* Mesh Networking PDU */
127#define BT_DATA_MESH_BEACON 0x2b /* Mesh Beacon */
128
129#define BT_DATA_MANUFACTURER_DATA 0xff /* Manufacturer Specific Data */
130
131#define BT_LE_AD_LIMITED 0x01 /* Limited Discoverable */
132#define BT_LE_AD_GENERAL 0x02 /* General Discoverable */
133#define BT_LE_AD_NO_BREDR 0x04 /* BR/EDR not supported */
134
135#define sys_put_be16(a,b) put_be16(b, a)
136#define sys_put_le16(a,b) put_le16(b, a)
137#define sys_put_le24(a,b) put_le24(b, a)
138#define sys_put_be24(a,b) put_be24(b, a)
139#define sys_put_be32(a,b) put_be32(b, a)
140#define sys_get_be16(a) get_be16(a)
141#define sys_get_be24(a) get_be24(a)
142#define sys_get_le16(a) get_le16(a)
143#define sys_get_le24(a) get_le24(a)
144#define sys_get_be32(a) get_be32(a)
145#define sys_cpu_to_be16(a) htobe16(a)
146#define sys_cpu_to_be32(a) htobe32(a)
147#define sys_be32_to_cpu(a) be32toh(a)
148#define sys_be16_to_cpu(a) be16toh(a)
149#define sys_le16_to_cpu(a) le16toh(a)
150
151#ifndef ARRAY_SIZE
152#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
153#endif
154
155#define CODE_UNREACHABLE __builtin_unreachable()
156#define __ASSERT(code, str) \
157 do { \
158 if (!(code)) BT_ERR(str); \
159 assert(code); \
160 } while (0);
161
162#define __ASSERT_NO_MSG(test) __ASSERT(test, "")
163
164/* Mesh is designed to not use mbuf chains */
165#if BT_DBG_ENABLED
166#define ASSERT_NOT_CHAIN(om) assert(SLIST_NEXT(om, om_next) == NULL)
167#else
168#define ASSERT_NOT_CHAIN(om) (void)(om)
169#endif
170
171#define CHECKIF(expr) \
172 __ASSERT_NO_MSG(!(expr)); \
173 if (0)
174
175#define __packed __attribute__((__packed__))
176
177#define MSEC_PER_SEC (1000)
178#define K_MSEC(ms) (ms)
179#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
180#define K_MINUTES(m) K_SECONDS((m) * 60)
181#define K_HOURS(h) K_MINUTES((h) * 60)
182
183#ifndef BIT
184#define BIT(n) (1UL << (n))
185#endif
186
187#define BIT_MASK(n) (BIT(n) - 1)
188
189#define BT_GAP_ADV_FAST_INT_MIN_1 0x0030 /* 30 ms */
190#define BT_GAP_ADV_FAST_INT_MAX_1 0x0060 /* 60 ms */
191#define BT_GAP_ADV_FAST_INT_MIN_2 0x00a0 /* 100 ms */
192#define BT_GAP_ADV_FAST_INT_MAX_2 0x00f0 /* 150 ms */
193#define BT_GAP_ADV_SLOW_INT_MIN 0x0640 /* 1 s */
194#define BT_GAP_ADV_SLOW_INT_MAX 0x0780 /* 1.2 s */
195
196#ifndef MESH_LOG_MODULE
197#define MESH_LOG_MODULE BLE_MESH_LOG
198#endif
199
200#define CAT(a, ...) PRIMITIVE_CAT(a, __VA_ARGS__)
201#define PRIMITIVE_CAT(a, ...) a ## __VA_ARGS__
202
203#define BLE_MESH_LOG(lvl, ...) CAT(MESH_LOG_MODULE, CAT(_, lvl))(__VA_ARGS__)
204
205#define BT_DBG(fmt, ...) BLE_MESH_LOG(DEBUG, "%s: " fmt "\n", __func__, ## __VA_ARGS__);
206#define BT_INFO(fmt, ...) BLE_MESH_LOG(INFO, "%s: " fmt "\n", __func__, ## __VA_ARGS__);
207#define BT_WARN(fmt, ...) BLE_MESH_LOG(WARN, "%s: " fmt "\n", __func__, ## __VA_ARGS__);
208#define BT_ERR(fmt, ...) BLE_MESH_LOG(ERROR, "%s: " fmt "\n", __func__, ## __VA_ARGS__);
209#define BT_GATT_ERR(_att_err) (-(_att_err))
210
211typedef ble_addr_t bt_addr_le_t;
212
213#define k_fifo_init(queue) ble_npl_eventq_init(queue)
214#define net_buf_simple_tailroom(buf) OS_MBUF_TRAILINGSPACE(buf)
215#define net_buf_tailroom(buf) net_buf_simple_tailroom(buf)
216#define net_buf_headroom(buf) ((buf)->om_data - &(buf)->om_databuf[buf->om_pkthdr_len])
217#define net_buf_simple_headroom(buf) net_buf_headroom(buf)
218#define net_buf_simple_tail(buf) ((buf)->om_data + (buf)->om_len)
219
220struct net_buf_simple_state {
222 uint16_t offset;
224 uint16_t len;
225};
226
227static inline struct os_mbuf * NET_BUF_SIMPLE(uint16_t size)
228{
229 struct os_mbuf *buf;
230
231 buf = os_msys_get(size, 0);
232 assert(buf);
233
234 return buf;
235}
236
237#define K_NO_WAIT (0)
238#define K_FOREVER (-1)
239
240#if MYNEWT_VAL(BLE_EXT_ADV)
241#define BT_MESH_ADV_INST (MYNEWT_VAL(BLE_MULTI_ADV_INSTANCES))
242
243#if MYNEWT_VAL(BLE_MESH_PROXY)
244/* Note that BLE_MULTI_ADV_INSTANCES contains number of additional instances.
245 * Instance 0 is always there
246 */
247#if MYNEWT_VAL(BLE_MULTI_ADV_INSTANCES) < 1
248#error "Mesh needs at least BLE_MULTI_ADV_INSTANCES set to 1"
249#endif
250#define BT_MESH_ADV_GATT_INST (MYNEWT_VAL(BLE_MULTI_ADV_INSTANCES) - 1)
251#endif /* BLE_MESH_PROXY */
252#endif /* BLE_EXT_ADV */
253
254/* This is by purpose */
255static inline void net_buf_simple_init(struct os_mbuf *buf,
256 size_t reserve_head)
257{
258 /* This is called in Zephyr after init.
259 * Note in Mynewt case we don't care abour reserved head*/
260 buf->om_data = &buf->om_databuf[buf->om_pkthdr_len] + reserve_head;
261 buf->om_len = 0;
262}
263
264#define net_buf_simple_init_with_data(buf, data, size) \
265 os_mbuf_copyinto(buf, 0, data, size);
266
267static inline void net_buf_simple_reset(struct os_mbuf *om)
268{
269 net_buf_simple_init(om, 0);
270}
271
272struct bt_le_ext_adv_start_param {
273 uint16_t timeout;
274
275 uint8_t num_events;
276};
277
278void net_buf_put(struct ble_npl_eventq *fifo, struct os_mbuf *buf);
279void * net_buf_ref(struct os_mbuf *om);
280void net_buf_unref(struct os_mbuf *om);
281uint16_t net_buf_simple_pull_le16(struct os_mbuf *om);
282uint16_t net_buf_simple_pull_be16(struct os_mbuf *om);
283uint32_t net_buf_simple_pull_le24(struct os_mbuf *om);
284uint32_t net_buf_simple_pull_be32(struct os_mbuf *om);
285uint32_t net_buf_simple_pull_le32(struct os_mbuf *om);
286uint8_t net_buf_simple_pull_u8(struct os_mbuf *om);
287void net_buf_simple_add_le16(struct os_mbuf *om, uint16_t val);
288void net_buf_simple_add_be16(struct os_mbuf *om, uint16_t val);
289void net_buf_simple_add_le24(struct os_mbuf *om, uint32_t val);
290void net_buf_simple_add_u8(struct os_mbuf *om, uint8_t val);
291void net_buf_simple_add_be32(struct os_mbuf *om, uint32_t val);
292void net_buf_simple_add_le32(struct os_mbuf *om, uint32_t val);
293void net_buf_add_zeros(struct os_mbuf *om, uint8_t len);
294void net_buf_simple_push_le16(struct os_mbuf *om, uint16_t val);
295void net_buf_simple_push_be16(struct os_mbuf *om, uint16_t val);
296void net_buf_simple_push_be24(struct os_mbuf *om, uint32_t val);
297void net_buf_simple_push_u8(struct os_mbuf *om, uint8_t val);
298void *net_buf_simple_pull(struct os_mbuf *om, uint8_t len);
299void *net_buf_simple_pull_mem(struct os_mbuf *om, uint8_t len);
300void *net_buf_simple_add(struct os_mbuf *om, uint8_t len);
301bool k_fifo_is_empty(struct ble_npl_eventq *q);
302void *net_buf_get(struct ble_npl_eventq *fifo,int32_t t);
303uint8_t *net_buf_simple_push(struct os_mbuf *om, uint8_t len);
304void net_buf_reserve(struct os_mbuf *om, size_t reserve);
305
306#define net_buf_add_mem(a,b,c) os_mbuf_append(a,b,c)
307#define net_buf_simple_add_mem(a,b,c) os_mbuf_append(a,b,c)
308#define net_buf_add_u8(a,b) net_buf_simple_add_u8(a,b)
309#define net_buf_add(a,b) net_buf_simple_add(a,b)
310
311#define net_buf_clone(a, b) os_mbuf_dup(a)
312#define net_buf_add_be32(a, b) net_buf_simple_add_be32(a, b)
313#define net_buf_add_be16(a, b) net_buf_simple_add_be16(a, b)
314#define net_buf_pull(a, b) net_buf_simple_pull_mem(a, b)
315#define net_buf_pull_mem(a, b) net_buf_simple_pull_mem(a, b)
316#define net_buf_pull_u8(a) net_buf_simple_pull_u8(a)
317#define net_buf_pull_be16(a) net_buf_simple_pull_be16(a)
318#define net_buf_skip(a, b) net_buf_simple_pull_mem(a, b)
319
320#define BT_GATT_CCC_NOTIFY BLE_GATT_CHR_PROP_NOTIFY
321
322#ifndef MIN
323#define MIN(a, b) (((a) < (b)) ? (a) : (b))
324#endif
325
326#ifndef MAX
327#define MAX(a, b) (((a) > (b)) ? (a) : (b))
328#endif
329
334struct bt_data {
335 uint8_t type;
336 uint8_t data_len;
337 const uint8_t *data;
338};
339
340struct bt_pub_key_cb {
349 void (*func)(const uint8_t key[BT_PUB_KEY_LEN]);
350
351 struct bt_pub_key_cb *_next;
352};
353
364 uint8_t id;
365
371 uint8_t sid;
372
382
384 uint32_t options;
385
393 uint32_t interval_min;
394
402 uint32_t interval_max;
403
419 const bt_addr_le_t *peer;
420};
421
422typedef void (*bt_dh_key_cb_t)(const uint8_t key[BT_DH_KEY_LEN]);
423int bt_dh_key_gen(const uint8_t remote_pk[BT_PUB_KEY_LEN], bt_dh_key_cb_t cb);
424int bt_pub_key_gen(struct bt_pub_key_cb *new_cb);
425uint8_t *bt_pub_key_get(void);
426void bt_conn_get_info(struct ble_hs_conn *conn, struct ble_gap_conn_desc *desc);
427int bt_rand(void *buf, size_t len);
428const char * bt_hex(const void *buf, size_t len);
429int bt_encrypt_be(const uint8_t *key, const uint8_t *plaintext, uint8_t *enc_data);
430int bt_ccm_decrypt(const uint8_t key[16], uint8_t nonce[13], const uint8_t *enc_data,
431 size_t len, const uint8_t *aad, size_t aad_len,
432 uint8_t *plaintext, size_t mic_size);
433int bt_ccm_encrypt(const uint8_t key[16], uint8_t nonce[13], const uint8_t *enc_data,
434 size_t len, const uint8_t *aad, size_t aad_len,
435 uint8_t *plaintext, size_t mic_size);
436void bt_mesh_register_gatt(void);
437int bt_le_adv_start(const struct ble_gap_adv_params *param,
438 const struct bt_data *ad, size_t ad_len,
439 const struct bt_data *sd, size_t sd_len);
440
441int bt_le_adv_stop(void);
442
443struct k_work_delayable {
444 struct ble_npl_callout work;
445};
446
447void k_work_init(struct ble_npl_callout *work, ble_npl_event_fn handler);
448void k_work_init_delayable(struct k_work_delayable *w, ble_npl_event_fn *f);
449void k_work_cancel_delayable(struct k_work_delayable *w);
450bool k_work_delayable_is_pending(struct k_work_delayable *w);
451void k_work_reschedule(struct k_work_delayable *w, uint32_t ms);
452int64_t k_uptime_get(void);
453uint32_t k_uptime_get_32(void);
454int64_t k_uptime_delta(int64_t *reftime);
455void k_sleep(int32_t duration);
456void k_work_submit(struct ble_npl_callout *w);
457void k_work_add_arg(struct ble_npl_callout *w, void *arg);
458void k_work_add_arg_delayable(struct k_work_delayable *w, void *arg);
459ble_npl_time_t k_work_delayable_remaining_get(struct k_work_delayable *w);
460void k_work_schedule(struct k_work_delayable *w, uint32_t ms);
461uint32_t k_ticks_to_ms_floor32(ble_npl_time_t ticks);
462
463static inline void net_buf_simple_save(struct os_mbuf *buf,
464 struct net_buf_simple_state *state)
465{
466 state->offset = net_buf_simple_headroom(buf);
467 state->len = buf->om_len;
468}
469
470void net_buf_simple_clone(const struct os_mbuf *original, struct os_mbuf *clone);
471
472static inline void net_buf_simple_restore(struct os_mbuf *buf,
473 struct net_buf_simple_state *state)
474{
475 buf->om_data = &buf->om_databuf[buf->om_pkthdr_len] + state->offset;
476 buf->om_len = state->len;
477}
478
479static inline void sys_memcpy_swap(void *dst, const void *src, size_t length)
480{
481 __ASSERT(((src < dst && (src + length) <= dst) ||
482 (src > dst && (dst + length) <= src)),
483 "Source and destination buffers must not overlap");
484
485 src += length - 1;
486
487 for (; length > 0; length--) {
488 *((uint8_t *)dst++) = *((uint8_t *)src--);
489 }
490}
491
492#define popcount(x) __builtin_popcount(x)
493
494static inline unsigned int find_lsb_set(uint32_t op)
495{
496 return __builtin_ffs(op);
497}
498
499static inline unsigned int find_msb_set(uint32_t op)
500{
501 if (!op)
502 return 0;
503
504 return 32 - __builtin_clz(op);
505}
506
507#define CONFIG_BT_MESH_FRIEND BLE_MESH_FRIEND
508#define CONFIG_BT_MESH_GATT_PROXY BLE_MESH_GATT_PROXY
509#define CONFIG_BT_MESH_IV_UPDATE_TEST BLE_MESH_IV_UPDATE_TEST
510#define CONFIG_BT_MESH_LOW_POWER BLE_MESH_LOW_POWER
511#define CONFIG_BT_MESH_LPN_SUB_ALL_NODES_ADDR BLE_MESH_LPN_SUB_ALL_NODES_ADDR
512#define CONFIG_BT_MESH_LPN_AUTO BLE_MESH_LPN_AUTO
513#define CONFIG_BT_MESH_LPN_ESTABLISHMENT BLE_MESH_LPN_ESTABLISHMENT
514#define CONFIG_BT_MESH_PB_ADV BLE_MESH_PB_ADV
515#define CONFIG_BT_MESH_PB_GATT BLE_MESH_PB_GATT
516#define CONFIG_BT_MESH_PROV BLE_MESH_PROV
517#define CONFIG_BT_MESH_PROXY BLE_MESH_PROXY
518#define CONFIG_BT_TESTING BLE_MESH_TESTING
519#define CONFIG_BT_SETTINGS BLE_MESH_SETTINGS
520#define CONFIG_SETTINGS BLE_MESH_SETTINGS
521#define CONFIG_BT_MESH_PROVISIONER BLE_MESH_PROVISIONER
522#define CONFIG_BT_MESH_PROV_DEVICE BLE_MESH_PROV_DEVICE
523#define CONFIG_BT_MESH_CDB BLE_MESH_CDB
524#define CONFIG_BT_MESH_DEBUG_CFG BLE_MESH_DEBUG_CFG
525#define CONFIG_BT_MESH_DEBUG_ADV BLE_MESH_DEBUG_ADV
526
527/* Above flags are used with IS_ENABLED macro */
528#define IS_ENABLED(config) MYNEWT_VAL(config)
529
530#define CONFIG_BT_MESH_LPN_GROUPS MYNEWT_VAL(BLE_MESH_LPN_GROUPS)
531#define CONFIG_BT_MESH_ADV_BUF_COUNT MYNEWT_VAL(BLE_MESH_ADV_BUF_COUNT)
532#define CONFIG_BT_MESH_SEG_BUFS MYNEWT_VAL(BLE_MESH_SEG_BUFS)
533#define CONFIG_BT_MESH_FRIEND_QUEUE_SIZE MYNEWT_VAL(BLE_MESH_FRIEND_QUEUE_SIZE)
534#define CONFIG_BT_MESH_FRIEND_RECV_WIN MYNEWT_VAL(BLE_MESH_FRIEND_RECV_WIN)
535#define CONFIG_BT_MESH_LPN_POLL_TIMEOUT MYNEWT_VAL(BLE_MESH_LPN_POLL_TIMEOUT)
536#define CONFIG_BT_MESH_MODEL_GROUP_COUNT MYNEWT_VAL(BLE_MESH_MODEL_GROUP_COUNT)
537#define CONFIG_BT_MESH_MODEL_KEY_COUNT MYNEWT_VAL(BLE_MESH_MODEL_KEY_COUNT)
538#define CONFIG_BT_MESH_NODE_ID_TIMEOUT MYNEWT_VAL(BLE_MESH_NODE_ID_TIMEOUT)
539#define CONFIG_BT_MAX_CONN MYNEWT_VAL(BLE_MAX_CONNECTIONS)
540#define CONFIG_BT_MESH_SEQ_STORE_RATE MYNEWT_VAL(BLE_MESH_SEQ_STORE_RATE)
541#define CONFIG_BT_MESH_RPL_STORE_TIMEOUT MYNEWT_VAL(BLE_MESH_RPL_STORE_TIMEOUT)
542#define CONFIG_BT_MESH_APP_KEY_COUNT MYNEWT_VAL(BLE_MESH_APP_KEY_COUNT)
543#define CONFIG_BT_MESH_SUBNET_COUNT MYNEWT_VAL(BLE_MESH_SUBNET_COUNT)
544#define CONFIG_BT_MESH_STORE_TIMEOUT MYNEWT_VAL(BLE_MESH_STORE_TIMEOUT)
545#define CONFIG_BT_MESH_IV_UPDATE_SEQ_LIMIT MYNEWT_VAL(BLE_MESH_IV_UPDATE_SEQ_LIMIT)
546#define CONFIG_BT_MESH_IVU_DIVIDER MYNEWT_VAL(BLE_MESH_IVU_DIVIDER)
547#define CONFIG_BT_DEVICE_NAME MYNEWT_VAL(BLE_MESH_DEVICE_NAME)
548#define CONFIG_BT_RX_SEG_MAX MYNEWT_VAL(BLE_MESH_RX_SEG_MAX)
549#define CONFIG_BT_MESH_TX_SEG_MAX MYNEWT_VAL(BLE_MESH_TX_SEG_MAX)
550#define CONFIG_BT_MESH_RX_SEG_MAX MYNEWT_VAL(BLE_MESH_RX_SEG_MAX)
551#define CONFIG_BT_MESH_RX_SEG_MSG_COUNT MYNEWT_VAL(BLE_MESH_RX_SEG_MSG_COUNT)
552#define CONFIG_BT_MESH_LABEL_COUNT MYNEWT_VAL(BLE_MESH_LABEL_COUNT)
553#define CONFIG_BT_MESH_MODEL_VND_MSG_CID_FORCE MYNEWT_VAL(BLE_MESH_MODEL_VND_MSG_CID_FORCE)
554#define CONFIG_BT_MESH_NODE_COUNT MYNEWT_VAL(BLE_MESH_CDB_NODE_COUNT)
555#define CONFIG_BT_GATT_PROXY_ENABLED MYNEWT_VAL(BLE_MESH_GATT_PROXY_ENABLED)
556#define CONFIG_BT_MESH_DEFAULT_TTL MYNEWT_VAL(BLE_MESH_DEFAULT_TTL)
557#define CONFIG_BT_MESH_NETWORK_TRANSMIT_COUNT MYNEWT_VAL(BLE_MESH_NETWORK_TRANSMIT_COUNT)
558#define CONFIG_BT_MESH_NETWORK_TRANSMIT_INTERVAL MYNEWT_VAL(BLE_MESH_NETWORK_TRANSMIT_INTERVAL)
559#define CONFIG_BT_MESH_RELAY_ENABLED MYNEWT_VAL(BLE_MESH_RELAY_ENABLED)
560#define CONFIG_BT_MESH_RELAY_RETRANSMIT_INTERVAL MYNEWT_VAL(BLE_MESH_RELAY_RETRANSMIT_INTERVAL)
561#define CONFIG_BT_MESH_BEACON_ENABLED MYNEWT_VAL(BLE_MESH_BEACON_ENABLED)
562#define CONFIG_BT_MESH_FRIEND_ENABLED MYNEWT_VAL(BLE_MESH_FRIEND_ENABLED)
563#define CONFIG_BT_MESH_RELAY MYNEWT_VAL(BLE_MESH_RELAY)
564#define CONFIG_BT_MESH_RELAY_RETRANSMIT_COUNT MYNEWT_VAL(BLE_MESH_RELAY_RETRANSMIT_COUNT)
565#define CONFIG_BT_MESH_GATT_PROXY_ENABLED MYNEWT_VAL(BLE_MESH_GATT_PROXY_ENABLED)
566#define CONFIG_BT_MESH_PROXY_MSG_LEN MYNEWT_VAL(BLE_MESH_PROXY_MSG_LEN)
567
568#define printk console_printf
569
570#define CONTAINER_OF(ptr, type, field) \
571 ((type *)(((char *)(ptr)) - offsetof(type, field)))
572
573
574#define k_sem ble_npl_sem
575
576static inline void k_sem_init(struct k_sem *sem, unsigned int initial_count,
577 unsigned int limit)
578{
579 ble_npl_sem_init(sem, initial_count);
580}
581
582static inline int k_sem_take(struct k_sem *sem, int32_t timeout)
583{
584 uint32_t ticks;
585
586 ble_npl_time_ms_to_ticks(timeout, &ticks);
587 return - ble_npl_sem_pend(sem, ticks);
588}
589
590static inline void k_sem_give(struct k_sem *sem)
591{
592 ble_npl_sem_release(sem);
593}
594
595static inline void k_sem_reset(struct k_sem *sem)
596{
597 ble_npl_sem_init(sem, 0);
598}
599/* Helpers to access the storage array, since we don't have access to its
600 * type at this point anymore.
601 */
602
603#define BUF_SIZE(pool) (pool->omp_pool->mp_block_size)
604
605static inline int net_buf_id(struct os_mbuf *buf)
606{
607 struct os_mbuf_pool *pool = buf->om_omp;
608 uint8_t *pool_start = (uint8_t *)pool->omp_pool->mp_membuf_addr;
609 uint8_t *buf_ptr = (uint8_t *)buf;
610
611 return (buf_ptr - pool_start) / BUF_SIZE(pool);
612}
613
614/* XXX: We should not use os_mbuf_pkthdr chains to represent a list of
615 * packets, this is a hack. For now this is not an issue, because mesh
616 * does not use os_mbuf chains. We should change this in the future.
617 */
618STAILQ_HEAD(net_buf_slist_t, os_mbuf_pkthdr);
619
620void net_buf_slist_init(struct net_buf_slist_t *list);
621bool net_buf_slist_is_empty(struct net_buf_slist_t *list);
622struct os_mbuf *net_buf_slist_peek_head(struct net_buf_slist_t *list);
623struct os_mbuf *net_buf_slist_peek_next(struct os_mbuf *buf);
624struct os_mbuf *net_buf_slist_get(struct net_buf_slist_t *list);
625void net_buf_slist_put(struct net_buf_slist_t *list, struct os_mbuf *buf);
626void net_buf_slist_remove(struct net_buf_slist_t *list, struct os_mbuf *prev,
627 struct os_mbuf *cur);
628void net_buf_slist_merge_slist(struct net_buf_slist_t *list,
629 struct net_buf_slist_t *list_to_append);
630#define NET_BUF_SLIST_FOR_EACH_NODE(head, var) STAILQ_FOREACH(var, head, omp_next)
631
632#if MYNEWT_VAL(BLE_MESH_SETTINGS)
633
634#define settings_load conf_load
635int settings_bytes_from_str(char *val_str, void *vp, int *len);
636char *settings_str_from_bytes(const void *vp, int vp_len,
637 char *buf, int buf_len);
638
639#define snprintk snprintf
640#define BT_SETTINGS_SIZE(in_size) ((((((in_size) - 1) / 3) * 4) + 4) + 1)
641#define settings_save_one conf_save_one
642
643#else
644
645static inline int
646settings_load(void)
647{
648 return 0;
649}
650
651#endif /* MYNEWT_VAL(MYNEWT_VAL_BLE_MESH_SETTINGS) */
652
653#define BUILD_ASSERT(cond, msg) _Static_assert(cond, msg)
654
655
656/* Memory slabs/blocks */
657
664 uint32_t num_blocks;
665 size_t block_size;
670 char *buffer;
671 char *free_list;
672 uint32_t num_used;
673};
674
675struct k_mem_block_id {
676 uint32_t pool : 8;
677 uint32_t level : 4;
678 uint32_t block : 20;
679};
680
681struct k_mem_block {
682 void *data;
683 struct k_mem_block_id id;
684};
685
686extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
687extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem);
688static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
689{
690 return slab->num_blocks - slab->num_used;
691}
692
693int create_free_list(struct k_mem_slab *slab);
694
695#ifdef __cplusplus
696}
697#endif
698
699#endif /* _MESH_GLUE_ */
– Interface to an AES-128 implementation.
Interface to a CMAC implementation.
– Interface to constants.
– Interface to EC-DH implementation.
struct os_mbuf * os_msys_get(uint16_t dsize, uint16_t leadingspace)
Definition os_mbuf.c:176
Definition ble_gap.h:220
Connection descriptor.
Definition ble_gap.h:249
Definition glue.h:334
Definition glue.h:355
uint32_t options
Definition glue.h:384
const bt_addr_le_t * peer
Directed advertising to peer.
Definition glue.h:419
uint8_t sid
Advertising Set Identifier, valid range 0x00 - 0x0f.
Definition glue.h:371
uint8_t secondary_max_skip
Secondary channel maximum skip count.
Definition glue.h:381
uint32_t interval_min
Definition glue.h:393
uint8_t id
Local identity.
Definition glue.h:364
uint32_t interval_max
Definition glue.h:402
Definition glue.h:659
char * buffer
Definition glue.h:670
uint32_t num_blocks
Definition glue.h:664
uint32_t num_used
Definition glue.h:672
size_t block_size
Definition glue.h:665
Definition os_mbuf.h:70
Definition os_mbuf.h:52
struct os_mempool * omp_pool
Definition os_mbuf.h:61
Definition os_mbuf.h:86
struct os_mbuf_pool * om_omp
Definition os_mbuf.h:107
uint8_t * om_data
Definition os_mbuf.h:90
uint8_t om_databuf[0]
Definition os_mbuf.h:114
uint16_t om_len
Definition os_mbuf.h:102
uint8_t om_pkthdr_len
Definition os_mbuf.h:98
uint32_t mp_membuf_addr
Definition os_mempool.h:69
Interface to platform-dependent run-time operations.