@@ -35,21 +35,75 @@ extern "C" {
35
35
#endif
36
36
37
37
/** Possible types of buffers passed around the Bluetooth stack in a form of bitmask. */
38
- enum __packed bt_buf_type {
38
+ enum bt_buf_type {
39
+ /** Invalid value, used for error checking */
40
+ BT_BUF_TYPE_INVALID = 0 ,
39
41
/** HCI command */
40
- BT_BUF_CMD = BT_HCI_H4_CMD ,
42
+ BT_BUF_CMD = BIT ( 0 ) ,
41
43
/** HCI event */
42
- BT_BUF_EVT = BT_HCI_H4_EVT ,
44
+ BT_BUF_EVT = BIT ( 1 ) ,
43
45
/** Outgoing ACL data */
44
- BT_BUF_ACL_OUT = BT_HCI_H4_ACL ,
46
+ BT_BUF_ACL_OUT = BIT ( 2 ) ,
45
47
/** Incoming ACL data */
46
- BT_BUF_ACL_IN = BT_HCI_H4_ACL ,
48
+ BT_BUF_ACL_IN = BIT ( 3 ) ,
47
49
/** Outgoing ISO data */
48
- BT_BUF_ISO_OUT = BT_HCI_H4_ISO ,
50
+ BT_BUF_ISO_OUT = BIT ( 4 ) ,
49
51
/** Incoming ISO data */
50
- BT_BUF_ISO_IN = BT_HCI_H4_ISO ,
52
+ BT_BUF_ISO_IN = BIT ( 5 ) ,
51
53
};
52
54
55
+ /** Direction of HCI packets */
56
+ enum bt_buf_dir {
57
+ BT_BUF_IN ,
58
+ BT_BUF_OUT ,
59
+ };
60
+
61
+ /** Convert from bt_buf_type to H:4 type.
62
+ *
63
+ * @param type The bt_buf_type to convert
64
+ * @return The H:4 type
65
+ */
66
+ static inline uint8_t bt_buf_type_to_h4 (enum bt_buf_type type )
67
+ {
68
+ switch (type ) {
69
+ case BT_BUF_CMD :
70
+ return BT_HCI_H4_CMD ;
71
+ case BT_BUF_ACL_IN :
72
+ case BT_BUF_ACL_OUT :
73
+ return BT_HCI_H4_ACL ;
74
+ case BT_BUF_ISO_IN :
75
+ case BT_BUF_ISO_OUT :
76
+ return BT_HCI_H4_ISO ;
77
+ case BT_BUF_EVT :
78
+ return BT_HCI_H4_EVT ;
79
+ default :
80
+ __ASSERT_NO_MSG (false);
81
+ return 0 ;
82
+ }
83
+ }
84
+
85
+ /** Convert from H:4 type to bt_buf_type.
86
+ *
87
+ * @param h4_type The H:4 type to convert
88
+ * @param dir The direction of the packet
89
+ * @return The bt_buf_type
90
+ */
91
+ static inline enum bt_buf_type bt_buf_type_from_h4 (uint8_t h4_type , enum bt_buf_dir dir )
92
+ {
93
+ switch (h4_type ) {
94
+ case BT_HCI_H4_CMD :
95
+ return BT_BUF_CMD ;
96
+ case BT_HCI_H4_ACL :
97
+ return dir == BT_BUF_OUT ? BT_BUF_ACL_OUT : BT_BUF_ACL_IN ;
98
+ case BT_HCI_H4_EVT :
99
+ return BT_BUF_EVT ;
100
+ case BT_HCI_H4_ISO :
101
+ return dir == BT_BUF_OUT ? BT_BUF_ISO_OUT : BT_BUF_ISO_IN ;
102
+ default :
103
+ return BT_BUF_TYPE_INVALID ;
104
+ }
105
+ }
106
+
53
107
/* Headroom reserved in buffers, primarily for HCI transport encoding purposes */
54
108
#define BT_BUF_RESERVE 1
55
109
@@ -151,7 +205,7 @@ struct net_buf *bt_buf_get_rx(enum bt_buf_type type, k_timeout_t timeout);
151
205
* any action that makes the current thread unready. This callback must only be used for very
152
206
* short non-blocking operation (e.g. submitting a work item).
153
207
*
154
- * @param type_mask A bit mask of buffer types (BIT(type)) that have been freed.
208
+ * @param type_mask A bit mask of buffer types that have been freed.
155
209
*/
156
210
typedef void (* bt_buf_rx_freed_cb_t )(enum bt_buf_type type_mask );
157
211
@@ -197,9 +251,10 @@ struct net_buf *bt_buf_get_evt(uint8_t evt, bool discardable, k_timeout_t timeou
197
251
static inline void __deprecated bt_buf_set_type (struct net_buf * buf , enum bt_buf_type type )
198
252
{
199
253
__ASSERT_NO_MSG (net_buf_headroom (buf ) >= 1 );
200
- net_buf_push_u8 (buf , type );
254
+ net_buf_push_u8 (buf , bt_buf_type_to_h4 ( type ) );
201
255
}
202
256
257
+
203
258
/** Get the buffer type. This pulls the H:4 byte prefix from the payload, which means
204
259
* that the call can be done only once per buffer.
205
260
*
@@ -209,7 +264,11 @@ static inline void __deprecated bt_buf_set_type(struct net_buf *buf, enum bt_buf
209
264
*/
210
265
static inline enum bt_buf_type __deprecated bt_buf_get_type (struct net_buf * buf )
211
266
{
212
- return net_buf_pull_u8 (buf );
267
+ /* We have to assume the direction since the H:4 type doesn't tell us
268
+ * if the buffer is incoming or outgoing. The common use case of this API is for outgoing
269
+ * buffers, so we assume that.
270
+ */
271
+ return bt_buf_type_from_h4 (net_buf_pull_u8 (buf ), BT_BUF_OUT );
213
272
}
214
273
215
274
/**
0 commit comments