1 /* @file
2 * @brief Internal APIs for LE Audio
3 *
4 * Copyright (c) 2022 Codecoup
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8 #include <stdbool.h>
9 #include <stddef.h>
10 #include <stdint.h>
11 #include <sys/types.h>
12
13 #include <zephyr/autoconf.h>
14 #include <zephyr/bluetooth/audio/audio.h>
15 #include <zephyr/bluetooth/conn.h>
16 #include <zephyr/bluetooth/gatt.h>
17
18 #define BT_AUDIO_NOTIFY_RETRY_DELAY_US ((CONFIG_BT_AUDIO_NOTIFY_RETRY_DELAY) * 1250U)
19
20 /** @brief LE Audio Attribute User Data. */
21 struct bt_audio_attr_user_data {
22 /** Attribute read callback */
23 ssize_t (*read)(struct bt_conn *conn, const struct bt_gatt_attr *attr,
24 void *buf, uint16_t len, uint16_t offset);
25
26 /** Attribute write callback */
27 ssize_t (*write)(struct bt_conn *conn, const struct bt_gatt_attr *attr,
28 const void *buf, uint16_t len, uint16_t offset,
29 uint8_t flags);
30
31 /** Attribute user data */
32 void *user_data;
33 };
34
35 /** LE Audio Read Characteristic value helper. */
36 ssize_t bt_audio_read_chrc(struct bt_conn *conn, const struct bt_gatt_attr *attr,
37 void *buf, uint16_t len, uint16_t offset);
38
39 /** LE Audio Write Characteristic value helper. */
40 ssize_t bt_audio_write_chrc(struct bt_conn *conn, const struct bt_gatt_attr *attr,
41 const void *buf, uint16_t len, uint16_t offset, uint8_t flags);
42
43 #define BT_AUDIO_ATTR_USER_DATA_INIT(_read, _write, _user_data) \
44 { \
45 .read = _read, \
46 .write = _write, \
47 .user_data = _user_data, \
48 }
49
50 /** Helper to define LE Audio characteristic. */
51 #define BT_AUDIO_CHRC(_uuid, _props, _perm, _read, _write, _user_data) \
52 BT_GATT_CHARACTERISTIC(_uuid, _props, _perm, bt_audio_read_chrc, bt_audio_write_chrc, \
53 ((struct bt_audio_attr_user_data[]) { \
54 BT_AUDIO_ATTR_USER_DATA_INIT(_read, _write, _user_data), \
55 }))
56
57 #define BT_AUDIO_CHRC_USER_DATA(_attr) \
58 (((struct bt_audio_attr_user_data *)(_attr)->user_data)->user_data)
59
60 /** LE Audio Write CCCD value helper. */
61 ssize_t bt_audio_ccc_cfg_write(struct bt_conn *conn, const struct bt_gatt_attr *attr,
62 uint16_t value);
63
64 /** Helper to define LE Audio CCC descriptor. */
65 #define BT_AUDIO_CCC(_changed) \
66 BT_GATT_CCC_MANAGED( \
67 ((struct bt_gatt_ccc_managed_user_data[]){BT_GATT_CCC_MANAGED_USER_DATA_INIT( \
68 _changed, bt_audio_ccc_cfg_write, NULL)}), \
69 (BT_GATT_PERM_READ | BT_GATT_PERM_WRITE_ENCRYPT))
70
bt_audio_dir_str(enum bt_audio_dir dir)71 static inline const char *bt_audio_dir_str(enum bt_audio_dir dir)
72 {
73 switch (dir) {
74 case BT_AUDIO_DIR_SINK:
75 return "sink";
76 case BT_AUDIO_DIR_SOURCE:
77 return "source";
78 default:
79 return "Unknown";
80 }
81 }
82
83 bool bt_audio_valid_ltv(const uint8_t *data, uint8_t data_len);
84 uint16_t bt_audio_get_max_ntf_size(struct bt_conn *conn);
85