Zephyr API Documentation 4.5.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
bluetooth.h
Go to the documentation of this file.
1
8
14
15#ifndef ZEPHYR_INCLUDE_DRIVERS_BLUETOOTH_H_
16#define ZEPHYR_INCLUDE_DRIVERS_BLUETOOTH_H_
17
28
29#include <stdbool.h>
30#include <stdint.h>
31#include <zephyr/net_buf.h>
35#include <zephyr/device.h>
36
37#ifdef __cplusplus
38extern "C" {
39#endif
40
54
55enum {
56 /* The host should never send HCI_Reset */
58 /* The controller does not auto-initiate a DLE procedure when the
59 * initial connection data length parameters are not equal to the
60 * default data length parameters. Therefore the host should initiate
61 * the DLE procedure after connection establishment.
62 *
63 * That requirement is stated in Core Spec v5.4 Vol 6 Part B. 4.5.10
64 * Data PDU length management:
65 *
66 * > For a new connection:
67 * > - ... If either value is not 27, then the Controller should
68 * > initiate the Data Length Update procedure at the earliest
69 * > practical opportunity.
70 */
92};
93
94#define BT_DT_HCI_QUIRK_OR(node_id, prop, idx) \
95 UTIL_CAT(BT_HCI_QUIRK_, DT_STRING_UPPER_TOKEN_BY_IDX(node_id, prop, idx))
96#define BT_DT_HCI_QUIRKS_GET(node_id) (COND_CODE_1(DT_NODE_HAS_PROP(node_id, bt_hci_quirks), \
97 (DT_FOREACH_PROP_ELEM_SEP(node_id, \
98 bt_hci_quirks, \
99 BT_DT_HCI_QUIRK_OR, \
100 (|))), \
101 (0)))
102#define BT_DT_HCI_QUIRKS_INST_GET(inst) BT_DT_HCI_QUIRKS_GET(DT_DRV_INST(inst))
103
104#define BT_DT_HCI_NAME_GET(node_id) DT_PROP_OR(node_id, bt_hci_name, "HCI")
105#define BT_DT_HCI_NAME_INST_GET(inst) BT_DT_HCI_NAME_GET(DT_DRV_INST(inst))
106
107/* Fallback default when there's no property, same as "virtual" */
108#define BT_PRIV_HCI_BUS_DEFAULT (0)
109#define BT_DT_HCI_BUS_GET(node_id) DT_ENUM_IDX_OR(node_id, bt_hci_bus, BT_PRIV_HCI_BUS_DEFAULT)
110
111#define BT_DT_HCI_BUS_INST_GET(inst) BT_DT_HCI_BUS_GET(DT_DRV_INST(inst))
112
124
130#define BT_DT_HCI_DRIVER_CONFIG_GET(node_id) \
131 { \
132 .quirks = (uint32_t)BT_DT_HCI_QUIRKS_GET(node_id), \
133 }
134
142
143#define BT_DT_HCI_DRIVER_CONFIG_INST_GET(inst) \
144 BT_DT_HCI_DRIVER_CONFIG_GET(DT_DRV_INST(inst))
145
150
158typedef int (*bt_hci_recv_t)(const struct device *dev, struct net_buf *buf);
159
169#if defined(CONFIG_BT_HCI_SET_PUBLIC_ADDR) || defined(__DOXYGEN__)
182#endif /* CONFIG_BT_HCI_SET_PUBLIC_ADDR */
183};
184
189typedef int (*bt_hci_api_open_t)(const struct device *dev);
190
195typedef int (*bt_hci_api_close_t)(const struct device *dev);
196
201typedef int (*bt_hci_api_send_t)(const struct device *dev, struct net_buf *buf);
202
209typedef int (*bt_hci_api_setup_t)(const struct device *dev,
210 const struct bt_hci_setup_params *param);
211
232__subsystem struct bt_hci_driver_api {
245#if defined(CONFIG_BT_HCI_SETUP) || defined(__DOXYGEN__)
253#endif /* CONFIG_BT_HCI_SETUP */
254};
255
258
277static inline int bt_hci_recv_err(const struct device *dev, struct net_buf *buf)
278{
279 struct bt_hci_driver_data *data = dev->data;
280
281 if (data->recv == NULL) {
282 return -ENOTCONN;
283 }
284
285 return data->recv(dev, buf);
286}
287
297static inline void bt_hci_recv(const struct device *dev, struct net_buf *buf)
298{
299 int err;
300
301 err = bt_hci_recv_err(dev, buf);
302 if (err != 0) {
303 net_buf_unref(buf);
304 }
305}
306
334static inline int bt_hci_open(const struct device *dev, bt_hci_recv_t recv)
335{
336 struct bt_hci_driver_data *data = dev->data;
337 int err;
338
339 if (data->recv != NULL) {
340 return -EALREADY;
341 }
342
343 data->recv = recv;
344
345 err = DEVICE_API_GET(bt_hci, dev)->open(dev);
346 if (err != 0) {
347 data->recv = NULL;
348 }
349
350 return err;
351}
352
365static inline bool bt_hci_can_close(const struct device *dev)
366{
367 return DEVICE_API_GET(bt_hci, dev)->close != NULL;
368}
369
391static inline int bt_hci_close(const struct device *dev)
392{
393 struct bt_hci_driver_data *data = dev->data;
394 int err = 0;
395
396 if (!bt_hci_can_close(dev)) {
397 return -ENOSYS;
398 }
399
400 err = DEVICE_API_GET(bt_hci, dev)->close(dev);
401 if (err == 0) {
402 data->recv = NULL;
403 }
404
405 return err;
406}
407
435static inline int bt_hci_send(const struct device *dev, struct net_buf *buf)
436{
437 return DEVICE_API_GET(bt_hci, dev)->send(dev, buf);
438}
439
440#if defined(CONFIG_BT_HCI_SETUP) || defined(__DOXYGEN__)
456static inline int bt_hci_setup(const struct device *dev, struct bt_hci_setup_params *params)
457{
458 const struct bt_hci_driver_api *api = DEVICE_API_GET(bt_hci, dev);
459
460 if (api->setup == NULL) {
461 return -ENOSYS;
462 }
463
464 return api->setup(dev, params);
465}
466#endif
467
486void bt_hci_set_public_addr(const struct device *dev, const bt_addr_t *addr);
487
515const bt_addr_t *bt_hci_get_public_addr(const struct device *dev);
516
520
521/* The following functions are not strictly part of the HCI driver API, in that
522 * they do not take as input a struct device which implements the HCI driver API.
523 */
524
536int bt_hci_transport_setup(const struct device *dev);
537
549int bt_hci_transport_teardown(const struct device *dev);
550
563
577
591
592#ifdef __cplusplus
593}
594#endif
595
596#endif /* ZEPHYR_INCLUDE_DRIVERS_BLUETOOTH_H_ */
Bluetooth device address definitions and utilities.
Bluetooth data buffer API.
APIs and macros for the Zephyr device model.
#define DEVICE_API_GET(_class, _dev)
Expands to the pointer of a device's API for a given class.
Definition device.h:1501
struct net_buf * bt_hci_evt_create(uint8_t evt, uint8_t len)
Allocate an HCI event buffer.
struct net_buf * bt_hci_cmd_complete_create(uint16_t op, uint8_t plen)
Allocate an HCI Command Complete event buffer.
struct net_buf * bt_hci_cmd_status_create(uint16_t op, uint8_t status)
Allocate an HCI Command Status event buffer.
int bt_hci_transport_setup(const struct device *dev)
Setup the HCI transport, which usually means to reset the Bluetooth IC.
int bt_hci_transport_teardown(const struct device *dev)
Teardown the HCI transport.
int(* bt_hci_api_send_t)(const struct device *dev, struct net_buf *buf)
Callback API to send an HCI buffer to the controller.
Definition bluetooth.h:201
int(* bt_hci_recv_t)(const struct device *dev, struct net_buf *buf)
Deliver HCI data from the controller to the host.
Definition bluetooth.h:158
int(* bt_hci_api_close_t)(const struct device *dev)
Callback API to close the HCI transport.
Definition bluetooth.h:195
int(* bt_hci_api_setup_t)(const struct device *dev, const struct bt_hci_setup_params *param)
Callback API for HCI vendor-specific setup.
Definition bluetooth.h:209
int(* bt_hci_api_open_t)(const struct device *dev)
Callback API to open the HCI transport.
Definition bluetooth.h:189
static void bt_hci_recv(const struct device *dev, struct net_buf *buf)
Deliver an HCI packet from the driver.
Definition bluetooth.h:297
static int bt_hci_setup(const struct device *dev, struct bt_hci_setup_params *params)
HCI vendor-specific setup.
Definition bluetooth.h:456
static bool bt_hci_can_close(const struct device *dev)
Check whether the HCI transport can be closed.
Definition bluetooth.h:365
static int bt_hci_send(const struct device *dev, struct net_buf *buf)
Send HCI buffer to controller.
Definition bluetooth.h:435
void bt_hci_set_public_addr(const struct device *dev, const bt_addr_t *addr)
Set the public identity address for the controller.
static int bt_hci_recv_err(const struct device *dev, struct net_buf *buf)
Deliver an HCI packet from the driver.
Definition bluetooth.h:277
static int bt_hci_open(const struct device *dev, bt_hci_recv_t recv)
Open the HCI transport.
Definition bluetooth.h:334
static int bt_hci_close(const struct device *dev)
Close the HCI transport.
Definition bluetooth.h:391
const bt_addr_t * bt_hci_get_public_addr(const struct device *dev)
Get the public identity address the driver is to configure.
@ BT_HCI_QUIRK_NO_ADV_TERM_ON_DIR_TIMEOUT
No LE Advertising Set Terminated event when high duty cycle advertising times out.
Definition bluetooth.h:91
@ BT_HCI_QUIRK_NO_FLOW_CONTROL
The controller advertises the controller to host flow control commands as supported but does not acce...
Definition bluetooth.h:76
@ BT_HCI_QUIRK_NO_AUTO_DLE
Definition bluetooth.h:71
@ BT_HCI_QUIRK_NO_RESET
Definition bluetooth.h:57
void net_buf_unref(struct net_buf *buf)
Decrements the reference count of a buffer.
#define BIT(n)
Unsigned integer with bit position n set (signed in assembly language).
Definition util_macro.h:44
#define ENOSYS
Function not implemented.
Definition errno.h:83
#define EALREADY
Operation already in progress.
Definition errno.h:105
#define ENOTCONN
Socket is not connected.
Definition errno.h:113
Buffer management.
ssize_t recv(int sock, void *buf, size_t max_len, int flags)
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
Bluetooth Device Address.
Definition addr.h:42
<span class="mlabel">Driver Operations</span> Bluetooth HCI driver operations
Definition bluetooth.h:232
bt_hci_api_close_t close
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition bluetooth.h:240
bt_hci_api_send_t send
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition bluetooth.h:244
bt_hci_api_open_t open
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition bluetooth.h:236
bt_hci_api_setup_t setup
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition bluetooth.h:252
Common Bluetooth HCI driver configuration.
Definition bluetooth.h:120
uint32_t quirks
Quirks for this HCI device instance.
Definition bluetooth.h:122
Common Bluetooth HCI driver data.
Definition bluetooth.h:166
bt_addr_t public_addr
Public identity address to configure in the controller.
Definition bluetooth.h:181
bt_hci_recv_t recv
Callback for the driver to deliver data received from the controller to the host.
Definition bluetooth.h:168
Parameters of the setup() driver API op.
Definition bluetooth.h:46
bt_addr_t public_addr
The public identity address to give to the controller.
Definition bluetooth.h:52
Runtime device structure (in ROM) per driver instance.
Definition device.h:543
void * data
Address of the device instance private data.
Definition device.h:553
Network buffer representation.
Definition net_buf.h:1062
uint16_t len
Length of the data behind the data pointer.
Definition net_buf.h:1154