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