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