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