1 /** @file
2  *  @brief Internal APIs for Bluetooth AICS.
3  */
4 
5 /*
6  * Copyright (c) 2020 Bose Corporation
7  * Copyright (c) 2020 Nordic Semiconductor ASA
8  *
9  * SPDX-License-Identifier: Apache-2.0
10  */
11 
12 #ifndef ZEPHYR_INCLUDE_BLUETOOTH_AUDIO_AICS_INTERNAL_
13 #define ZEPHYR_INCLUDE_BLUETOOTH_AUDIO_AICS_INTERNAL_
14 #include <zephyr/types.h>
15 #include <zephyr/bluetooth/gatt.h>
16 
17 #if defined(CONFIG_BT_AICS)
18 #define BT_AICS_MAX_DESC_SIZE CONFIG_BT_AICS_MAX_INPUT_DESCRIPTION_SIZE
19 #else
20 #define BT_AICS_MAX_DESC_SIZE 1
21 #endif /* CONFIG_BT_AICS */
22 
23 /* AICS opcodes */
24 #define BT_AICS_OPCODE_SET_GAIN                    0x01
25 #define BT_AICS_OPCODE_UNMUTE                      0x02
26 #define BT_AICS_OPCODE_MUTE                        0x03
27 #define BT_AICS_OPCODE_SET_MANUAL                  0x04
28 #define BT_AICS_OPCODE_SET_AUTO                    0x05
29 
30 /* AICS status */
31 #define BT_AICS_STATUS_INACTIVE                    0x00
32 #define BT_AICS_STATUS_ACTIVE                      0x01
33 
34 #define BT_AICS_INPUT_MODE_IMMUTABLE(gain_mode) \
35 	((gain_mode) == BT_AICS_MODE_MANUAL_ONLY || (gain_mode) == BT_AICS_MODE_AUTO_ONLY)
36 
37 #define BT_AICS_INPUT_MODE_SETTABLE(gain_mode) \
38 	((gain_mode) == BT_AICS_MODE_AUTO || (gain_mode) == BT_AICS_MODE_MANUAL)
39 
40 struct bt_aics_control {
41 	uint8_t opcode;
42 	uint8_t counter;
43 } __packed;
44 
45 struct bt_aics_gain_control {
46 	struct bt_aics_control cp;
47 	int8_t gain_setting;
48 } __packed;
49 
50 struct bt_aics_client {
51 	uint8_t change_counter;
52 	uint8_t gain_mode;
53 	bool desc_writable;
54 	bool active;
55 
56 	uint16_t start_handle;
57 	uint16_t end_handle;
58 	uint16_t state_handle;
59 	uint16_t gain_handle;
60 	uint16_t type_handle;
61 	uint16_t status_handle;
62 	uint16_t control_handle;
63 	uint16_t desc_handle;
64 	struct bt_gatt_subscribe_params state_sub_params;
65 	struct bt_gatt_subscribe_params status_sub_params;
66 	struct bt_gatt_subscribe_params desc_sub_params;
67 	bool cp_retried;
68 
69 	bool busy;
70 	struct bt_aics_gain_control cp_val;
71 	struct bt_gatt_write_params write_params;
72 	struct bt_gatt_read_params read_params;
73 	struct bt_gatt_discover_params discover_params;
74 	struct bt_aics_cb *cb;
75 	struct bt_conn *conn;
76 };
77 
78 struct bt_aics_state {
79 	int8_t gain;
80 	uint8_t mute;
81 	uint8_t gain_mode;
82 	uint8_t change_counter;
83 } __packed;
84 
85 struct bt_aics_gain_settings {
86 	uint8_t units;
87 	int8_t minimum;
88 	int8_t maximum;
89 } __packed;
90 
91 enum bt_aics_notify {
92 	AICS_NOTIFY_STATE,
93 	AICS_NOTIFY_DESCRIPTION,
94 	AICS_NOTIFY_STATUS,
95 	AICS_NOTIFY_NUM,
96 };
97 
98 struct bt_aics_server {
99 	struct bt_aics_state state;
100 	struct bt_aics_gain_settings gain_settings;
101 	bool initialized;
102 	uint8_t type;
103 	uint8_t status;
104 	struct bt_aics *inst;
105 	char description[BT_AICS_MAX_DESC_SIZE];
106 	struct bt_aics_cb *cb;
107 
108 	struct bt_gatt_service *service_p;
109 
110 	ATOMIC_DEFINE(notify, AICS_NOTIFY_NUM);
111 	struct k_work_delayable notify_work;
112 };
113 
114 /* Struct used as a common type for the api */
115 struct bt_aics {
116 	bool client_instance;
117 	union {
118 		struct bt_aics_server srv;
119 		struct bt_aics_client cli;
120 	};
121 
122 };
123 
124 uint8_t aics_client_notify_handler(struct bt_conn *conn,
125 				   struct bt_gatt_subscribe_params *params,
126 				   const void *data, uint16_t length);
127 int bt_aics_client_register(struct bt_aics *inst);
128 int bt_aics_client_unregister(struct bt_aics *inst);
129 int bt_aics_client_state_get(struct bt_aics *inst);
130 int bt_aics_client_gain_setting_get(struct bt_aics *inst);
131 int bt_aics_client_type_get(struct bt_aics *inst);
132 int bt_aics_client_status_get(struct bt_aics *inst);
133 int bt_aics_client_unmute(struct bt_aics *inst);
134 int bt_aics_client_mute(struct bt_aics *inst);
135 int bt_aics_client_manual_gain_set(struct bt_aics *inst);
136 int bt_aics_client_automatic_gain_set(struct bt_aics *inst);
137 int bt_aics_client_gain_set(struct bt_aics *inst, int8_t gain);
138 int bt_aics_client_description_get(struct bt_aics *inst);
139 int bt_aics_client_description_set(struct bt_aics *inst,
140 				   const char *description);
141 
142 #endif /* ZEPHYR_INCLUDE_BLUETOOTH_AUDIO_AICS_INTERNAL_ */
143