forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuf.h
141 lines (124 loc) · 4.1 KB
/
buf.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/** @file
* @brief Bluetooth data buffer API
*/
/*
* Copyright (c) 2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_INCLUDE_BLUETOOTH_BUF_H_
#define ZEPHYR_INCLUDE_BLUETOOTH_BUF_H_
/**
* @brief Data buffers
* @defgroup bt_buf Data buffers
* @ingroup bluetooth
* @{
*/
#include <zephyr/types.h>
#include <net/buf.h>
#include <bluetooth/hci.h>
/** Possible types of buffers passed around the Bluetooth stack */
enum bt_buf_type {
/** HCI command */
BT_BUF_CMD,
/** HCI event */
BT_BUF_EVT,
/** Outgoing ACL data */
BT_BUF_ACL_OUT,
/** Incoming ACL data */
BT_BUF_ACL_IN,
/** Outgoing ISO data */
BT_BUF_ISO_OUT,
/** Incoming ISO data */
BT_BUF_ISO_IN,
/** H:4 data */
BT_BUF_H4,
};
/** Minimum amount of user data size for buffers passed to the stack. */
#define BT_BUF_USER_DATA_MIN __DEPRECATED_MACRO 4
#if defined(CONFIG_BT_HCI_RAW)
#define BT_BUF_RESERVE MAX(CONFIG_BT_HCI_RESERVE, CONFIG_BT_HCI_RAW_RESERVE)
#else
#define BT_BUF_RESERVE CONFIG_BT_HCI_RESERVE
#endif
#define BT_BUF_SIZE(size) (BT_BUF_RESERVE + (size))
/** Data size neeed for HCI RX buffers */
#define BT_BUF_RX_SIZE (BT_BUF_SIZE(CONFIG_BT_RX_BUF_LEN))
/** Allocate a buffer for incoming data
*
* This will set the buffer type so bt_buf_set_type() does not need to
* be explicitly called before bt_recv_prio().
*
* @param type Type of buffer. Only BT_BUF_EVT and BT_BUF_ACL_IN are
* allowed.
* @param timeout Non-negative waiting period to obtain a buffer or one of the
* special values K_NO_WAIT and K_FOREVER.
* @return A new buffer.
*/
struct net_buf *bt_buf_get_rx(enum bt_buf_type type, k_timeout_t timeout);
/** Allocate a buffer for outgoing data
*
* This will set the buffer type so bt_buf_set_type() does not need to
* be explicitly called before bt_send().
*
* @param type Type of buffer. Only BT_BUF_CMD, BT_BUF_ACL_OUT or
* BT_BUF_H4, when operating on H:4 mode, are allowed.
* @param timeout Non-negative waiting period to obtain a buffer or one of the
* special values K_NO_WAIT and K_FOREVER.
* @param data Initial data to append to buffer.
* @param size Initial data size.
* @return A new buffer.
*/
struct net_buf *bt_buf_get_tx(enum bt_buf_type type, k_timeout_t timeout,
const void *data, size_t size);
/** Allocate a buffer for an HCI Command Complete/Status Event
*
* This will set the buffer type so bt_buf_set_type() does not need to
* be explicitly called before bt_recv_prio().
*
* @param timeout Non-negative waiting period to obtain a buffer or one of the
* special values K_NO_WAIT and K_FOREVER.
* @return A new buffer.
*/
struct net_buf *bt_buf_get_cmd_complete(k_timeout_t timeout);
/** Allocate a buffer for an HCI Event
*
* This will set the buffer type so bt_buf_set_type() does not need to
* be explicitly called before bt_recv_prio() or bt_recv().
*
* @param evt HCI event code
* @param discardable Whether the driver considers the event discardable.
* @param timeout Non-negative waiting period to obtain a buffer or one of
* the special values K_NO_WAIT and K_FOREVER.
* @return A new buffer.
*/
struct net_buf *bt_buf_get_evt(uint8_t evt, bool discardable, k_timeout_t timeout);
/** Set the buffer type
*
* @param buf Bluetooth buffer
* @param type The BT_* type to set the buffer to
*/
static inline void bt_buf_set_type(struct net_buf *buf, enum bt_buf_type type)
{
*(uint8_t *)net_buf_user_data(buf) = type;
}
/** Get the buffer type
*
* @param buf Bluetooth buffer
*
* @return The BT_* type to of the buffer
*/
static inline enum bt_buf_type bt_buf_get_type(struct net_buf *buf)
{
/* De-referencing the pointer from net_buf_user_data(buf) as a
* pointer to an enum causes issues on qemu_x86 because the true
* size is 8-bit, but the enum is 32-bit on qemu_x86. So we put in
* a temporary cast to 8-bit to ensure only 8 bits are read from
* the pointer.
*/
return (enum bt_buf_type)(*(uint8_t *)net_buf_user_data(buf));
}
/**
* @}
*/
#endif /* ZEPHYR_INCLUDE_BLUETOOTH_BUF_H_ */