nRF Connect SDK API 0.1.0
Loading...
Searching...
No Matches
conn_ctx.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5 */
6
7#ifndef BT_CONN_CTX_H_
8#define BT_CONN_CTX_H_
9
17#include <zephyr/kernel.h>
18#include <zephyr/sys/__assert.h>
19#include <zephyr/bluetooth/conn.h>
20
21#ifdef __cplusplus
22extern "C" {
23#endif
24
31#define BT_CONN_CTX_DEF(_name, _max_clients, _ctx_sz) \
32 K_MEM_SLAB_DEFINE(_name##_mem_slab, \
33 ROUND_UP(_ctx_sz, CONFIG_BT_CONN_CTX_MEM_BUF_ALIGN), \
34 (_max_clients), \
35 CONFIG_BT_CONN_CTX_MEM_BUF_ALIGN); \
36 K_MUTEX_DEFINE(_name##_mutex); \
37 static struct bt_conn_ctx_lib _CONCAT(_name, _ctx_lib) = \
38 { \
39 .mem_slab = &_CONCAT(_name, _mem_slab), \
40 .mutex = &_name##_mutex \
41 }
42
46 void *data;
47
49 struct bt_conn *conn;
50};
51
55 struct bt_conn_ctx ctx[CONFIG_BT_MAX_CONN];
56
59 struct k_mutex * const mutex;
60
62 struct k_mem_slab * const mem_slab;
63};
64
72static inline size_t bt_conn_ctx_block_size_get(struct bt_conn_ctx_lib *ctx_lib)
73{
74 return ctx_lib->mem_slab->info.block_size;
75}
76
84static inline size_t bt_conn_ctx_count(struct bt_conn_ctx_lib *ctx_lib)
85{
86 return ARRAY_SIZE(ctx_lib->ctx);
87}
88
103void *bt_conn_ctx_alloc(struct bt_conn_ctx_lib *ctx_lib, struct bt_conn *conn);
104
114int bt_conn_ctx_free(struct bt_conn_ctx_lib *ctx_lib, struct bt_conn *conn);
115
122
138void *bt_conn_ctx_get(struct bt_conn_ctx_lib *ctx_lib, struct bt_conn *conn);
139
156const struct bt_conn_ctx *bt_conn_ctx_get_by_id(struct bt_conn_ctx_lib *ctx_lib, uint8_t id);
157
171void bt_conn_ctx_release(struct bt_conn_ctx_lib *ctx_lib, void *data);
172
173#ifdef __cplusplus
174}
175#endif
176
181#endif /* BT_CONN_CTX_H_ */
static size_t bt_conn_ctx_block_size_get(struct bt_conn_ctx_lib *ctx_lib)
Get the block size.
Definition: conn_ctx.h:72
const struct bt_conn_ctx * bt_conn_ctx_get_by_id(struct bt_conn_ctx_lib *ctx_lib, uint8_t id)
Get a specific connection context from the memory pool.
void * bt_conn_ctx_alloc(struct bt_conn_ctx_lib *ctx_lib, struct bt_conn *conn)
Allocate memory for the connection context data.
void bt_conn_ctx_release(struct bt_conn_ctx_lib *ctx_lib, void *data)
Release a connection context from the memory pool.
void * bt_conn_ctx_get(struct bt_conn_ctx_lib *ctx_lib, struct bt_conn *conn)
Get the context data of a connection from the memory pool.
static size_t bt_conn_ctx_count(struct bt_conn_ctx_lib *ctx_lib)
Get the number of connection contexts that are managed by the library.
Definition: conn_ctx.h:84
void bt_conn_ctx_free_all(struct bt_conn_ctx_lib *ctx_lib)
Free all allocated context data.
int bt_conn_ctx_free(struct bt_conn_ctx_lib *ctx_lib, struct bt_conn *conn)
Free the allocated memory for a connection.
struct bt_conn_ctx ctx[CONFIG_BT_MAX_CONN]
Definition: conn_ctx.h:55
struct k_mem_slab *const mem_slab
Definition: conn_ctx.h:62
struct k_mutex *const mutex
Definition: conn_ctx.h:59
Bluetooth connection context library structure.
Definition: conn_ctx.h:53
struct bt_conn * conn
Definition: conn_ctx.h:49
void * data
Definition: conn_ctx.h:46
Context data for a connection.
Definition: conn_ctx.h:44