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