1 /*  Bluetooth MICP - Microphone Control Profile - Microphone Controller */
2 
3 /*
4  * Copyright (c) 2020 Bose Corporation
5  * Copyright (c) 2020-2023 Nordic Semiconductor ASA
6  *
7  * SPDX-License-Identifier: Apache-2.0
8  */
9 
10 #include <errno.h>
11 #include <stdbool.h>
12 #include <stddef.h>
13 #include <stdint.h>
14 #include <string.h>
15 
16 #include <zephyr/autoconf.h>
17 #include <zephyr/bluetooth/att.h>
18 #include <zephyr/bluetooth/audio/aics.h>
19 #include <zephyr/bluetooth/audio/micp.h>
20 #include <zephyr/bluetooth/bluetooth.h>
21 #include <zephyr/bluetooth/conn.h>
22 #include <zephyr/bluetooth/gatt.h>
23 #include <zephyr/bluetooth/uuid.h>
24 #include <zephyr/device.h>
25 #include <zephyr/init.h>
26 #include <zephyr/kernel.h>
27 #include <zephyr/logging/log.h>
28 #include <zephyr/sys/__assert.h>
29 #include <zephyr/sys/atomic.h>
30 #include <zephyr/sys/check.h>
31 #include <zephyr/sys/slist.h>
32 #include <zephyr/sys/util.h>
33 #include <zephyr/types.h>
34 
35 #include "common/bt_str.h"
36 #include "micp_internal.h"
37 
38 LOG_MODULE_REGISTER(bt_micp_mic_ctlr, CONFIG_BT_MICP_MIC_CTLR_LOG_LEVEL);
39 
40 /* Callback functions */
41 static sys_slist_t micp_mic_ctlr_cbs = SYS_SLIST_STATIC_INIT(&micp_mic_ctlr_cbs);
42 
43 static struct bt_micp_mic_ctlr mic_ctlrs[CONFIG_BT_MAX_CONN];
44 static const struct bt_uuid *mics_uuid = BT_UUID_MICS;
45 
mic_ctlr_get_by_conn(const struct bt_conn * conn)46 static struct bt_micp_mic_ctlr *mic_ctlr_get_by_conn(const struct bt_conn *conn)
47 {
48 	return &mic_ctlrs[bt_conn_index(conn)];
49 }
50 
micp_mic_ctlr_mute_changed(struct bt_micp_mic_ctlr * mic_ctlr,int err,uint8_t mute_val)51 static void micp_mic_ctlr_mute_changed(struct bt_micp_mic_ctlr *mic_ctlr, int err, uint8_t mute_val)
52 {
53 	struct bt_micp_mic_ctlr_cb *listener, *next;
54 
55 	SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&micp_mic_ctlr_cbs, listener, next, _node) {
56 		if (listener->mute) {
57 			listener->mute(mic_ctlr, err, mute_val);
58 		}
59 	}
60 }
61 
micp_mic_ctlr_mute_written(struct bt_micp_mic_ctlr * mic_ctlr,int err,uint8_t mute_val)62 static void micp_mic_ctlr_mute_written(struct bt_micp_mic_ctlr *mic_ctlr, int err, uint8_t mute_val)
63 {
64 	struct bt_micp_mic_ctlr_cb *listener, *next;
65 
66 	SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&micp_mic_ctlr_cbs, listener, next, _node) {
67 		if (mute_val == BT_MICP_MUTE_UNMUTED) {
68 			if (listener->unmute_written) {
69 				listener->unmute_written(mic_ctlr, err);
70 			}
71 		} else {
72 			if (listener->mute_written) {
73 				listener->mute_written(mic_ctlr, err);
74 			}
75 		}
76 	}
77 }
78 
micp_mic_ctlr_discover_complete(struct bt_micp_mic_ctlr * mic_ctlr,int err)79 static void micp_mic_ctlr_discover_complete(struct bt_micp_mic_ctlr *mic_ctlr, int err)
80 {
81 	struct bt_micp_mic_ctlr_cb *listener, *next;
82 
83 	atomic_clear_bit(mic_ctlr->flags, BT_MICP_MIC_CTLR_FLAG_BUSY);
84 
85 	SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&micp_mic_ctlr_cbs, listener, next, _node) {
86 		if (listener->discover) {
87 			uint8_t aics_cnt = 0U;
88 
89 #if defined(CONFIG_BT_MICP_MIC_CTLR_AICS)
90 			if (err == 0) {
91 				aics_cnt = mic_ctlr->aics_inst_cnt;
92 			}
93 #endif /* CONFIG_BT_MICP_MIC_CTLR_AICS */
94 
95 			listener->discover(mic_ctlr, err, aics_cnt);
96 		}
97 	}
98 }
99 
mute_notify_handler(struct bt_conn * conn,struct bt_gatt_subscribe_params * params,const void * data,uint16_t length)100 static uint8_t mute_notify_handler(struct bt_conn *conn, struct bt_gatt_subscribe_params *params,
101 				   const void *data, uint16_t length)
102 {
103 	uint8_t *mute_val;
104 	struct bt_micp_mic_ctlr *mic_ctlr;
105 
106 	if (conn == NULL) {
107 		return BT_GATT_ITER_CONTINUE;
108 	}
109 
110 	mic_ctlr = mic_ctlr_get_by_conn(conn);
111 
112 	if (data != NULL) {
113 		if (length == sizeof(*mute_val)) {
114 			mute_val = (uint8_t *)data;
115 			LOG_DBG("Mute %u", *mute_val);
116 			micp_mic_ctlr_mute_changed(mic_ctlr, 0, *mute_val);
117 		}
118 	} else {
119 		LOG_DBG("Invalid length %u (expected %zu)", length, sizeof(*mute_val));
120 	}
121 
122 	return BT_GATT_ITER_CONTINUE;
123 }
124 
micp_mic_ctlr_read_mute_cb(struct bt_conn * conn,uint8_t err,struct bt_gatt_read_params * params,const void * data,uint16_t length)125 static uint8_t micp_mic_ctlr_read_mute_cb(struct bt_conn *conn, uint8_t err,
126 					struct bt_gatt_read_params *params,
127 					const void *data, uint16_t length)
128 {
129 	struct bt_micp_mic_ctlr *mic_ctlr = mic_ctlr_get_by_conn(conn);
130 	uint8_t cb_err = err;
131 	uint8_t mute_val = 0;
132 
133 	atomic_clear_bit(mic_ctlr->flags, BT_MICP_MIC_CTLR_FLAG_BUSY);
134 
135 	if (err > 0) {
136 		LOG_DBG("err: 0x%02X", err);
137 	} else if (data != NULL) {
138 		if (length == sizeof(mute_val)) {
139 			mute_val = ((uint8_t *)data)[0];
140 			LOG_DBG("Mute %u", mute_val);
141 		} else {
142 			LOG_DBG("Invalid length %u (expected %zu)", length, sizeof(mute_val));
143 			cb_err = BT_ATT_ERR_INVALID_ATTRIBUTE_LEN;
144 		}
145 	}
146 
147 	micp_mic_ctlr_mute_changed(mic_ctlr, cb_err, mute_val);
148 
149 	return BT_GATT_ITER_STOP;
150 }
151 
micp_mic_ctlr_write_mics_mute_cb(struct bt_conn * conn,uint8_t err,struct bt_gatt_write_params * params)152 static void micp_mic_ctlr_write_mics_mute_cb(struct bt_conn *conn, uint8_t err,
153 					   struct bt_gatt_write_params *params)
154 {
155 	struct bt_micp_mic_ctlr *mic_ctlr = mic_ctlr_get_by_conn(conn);
156 	uint8_t mute_val = mic_ctlr->mute_val_buf[0];
157 
158 	LOG_DBG("Write %s (0x%02X)", err ? "failed" : "successful", err);
159 
160 	atomic_clear_bit(mic_ctlr->flags, BT_MICP_MIC_CTLR_FLAG_BUSY);
161 
162 	micp_mic_ctlr_mute_written(mic_ctlr, err, mute_val);
163 }
164 
165 #if defined(CONFIG_BT_MICP_MIC_CTLR_AICS)
lookup_micp_by_aics(const struct bt_aics * aics)166 static struct bt_micp_mic_ctlr *lookup_micp_by_aics(const struct bt_aics *aics)
167 {
168 	__ASSERT(aics != NULL, "AICS pointer cannot be NULL");
169 
170 	for (int i = 0; i < ARRAY_SIZE(mic_ctlrs); i++) {
171 		for (int j = 0; j < ARRAY_SIZE(mic_ctlrs[i].aics); j++) {
172 			if (mic_ctlrs[i].aics[j] == aics) {
173 				return &mic_ctlrs[i];
174 			}
175 		}
176 	}
177 
178 	return NULL;
179 }
180 
micp_mic_ctlr_aics_state_cb(struct bt_aics * inst,int err,int8_t gain,uint8_t mute,uint8_t mode)181 static void micp_mic_ctlr_aics_state_cb(struct bt_aics *inst, int err, int8_t gain, uint8_t mute,
182 					uint8_t mode)
183 {
184 	struct bt_micp_mic_ctlr_cb *listener, *next;
185 
186 	SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&micp_mic_ctlr_cbs, listener, next, _node) {
187 		if (listener->aics_cb.state) {
188 			listener->aics_cb.state(inst, err, gain, mute, mode);
189 		}
190 	}
191 }
192 
micp_mic_ctlr_aics_gain_setting_cb(struct bt_aics * inst,int err,uint8_t units,int8_t minimum,int8_t maximum)193 static void micp_mic_ctlr_aics_gain_setting_cb(struct bt_aics *inst, int err, uint8_t units,
194 					       int8_t minimum, int8_t maximum)
195 {
196 	struct bt_micp_mic_ctlr_cb *listener, *next;
197 
198 	SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&micp_mic_ctlr_cbs, listener, next, _node) {
199 		if (listener->aics_cb.gain_setting) {
200 			listener->aics_cb.gain_setting(inst, err, units, minimum, maximum);
201 		}
202 	}
203 }
204 
micp_mic_ctlr_aics_type_cb(struct bt_aics * inst,int err,uint8_t type)205 static void micp_mic_ctlr_aics_type_cb(struct bt_aics *inst, int err, uint8_t type)
206 {
207 	struct bt_micp_mic_ctlr_cb *listener, *next;
208 
209 	SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&micp_mic_ctlr_cbs, listener, next, _node) {
210 		if (listener->aics_cb.type) {
211 			listener->aics_cb.type(inst, err, type);
212 		}
213 	}
214 }
215 
micp_mic_ctlr_aics_status_cb(struct bt_aics * inst,int err,bool active)216 static void micp_mic_ctlr_aics_status_cb(struct bt_aics *inst, int err, bool active)
217 {
218 	struct bt_micp_mic_ctlr_cb *listener, *next;
219 
220 	SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&micp_mic_ctlr_cbs, listener, next, _node) {
221 		if (listener->aics_cb.status) {
222 			listener->aics_cb.status(inst, err, active);
223 		}
224 	}
225 }
226 
micp_mic_ctlr_aics_description_cb(struct bt_aics * inst,int err,char * description)227 static void micp_mic_ctlr_aics_description_cb(struct bt_aics *inst, int err, char *description)
228 {
229 	struct bt_micp_mic_ctlr_cb *listener, *next;
230 
231 	SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&micp_mic_ctlr_cbs, listener, next, _node) {
232 		if (listener->aics_cb.description) {
233 			listener->aics_cb.description(inst, err, description);
234 		}
235 	}
236 }
237 
micp_mic_ctlr_aics_discover_cb(struct bt_aics * inst,int err)238 static void micp_mic_ctlr_aics_discover_cb(struct bt_aics *inst, int err)
239 {
240 	struct bt_micp_mic_ctlr *mic_ctlr = lookup_micp_by_aics(inst);
241 	struct bt_micp_mic_ctlr_cb *listener, *next;
242 
243 	if (mic_ctlr == NULL) {
244 		LOG_ERR("Could not lookup mic_ctlr from aics");
245 		micp_mic_ctlr_discover_complete(mic_ctlr, BT_GATT_ERR(BT_ATT_ERR_UNLIKELY));
246 
247 		return;
248 	}
249 
250 	if (err == 0) {
251 		/* Continue discovery of included services */
252 		err = bt_gatt_discover(mic_ctlr->conn, &mic_ctlr->discover_params);
253 	}
254 
255 	if (err != 0) {
256 		LOG_DBG("Discover failed (err %d)", err);
257 		micp_mic_ctlr_discover_complete(mic_ctlr, err);
258 	}
259 
260 	SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&micp_mic_ctlr_cbs, listener, next, _node) {
261 		if (listener->aics_cb.discover) {
262 			listener->aics_cb.discover(inst, err);
263 		}
264 	}
265 }
266 
micp_mic_ctlr_aics_set_gain_cb(struct bt_aics * inst,int err)267 static void micp_mic_ctlr_aics_set_gain_cb(struct bt_aics *inst, int err)
268 {
269 	struct bt_micp_mic_ctlr_cb *listener, *next;
270 
271 	SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&micp_mic_ctlr_cbs, listener, next, _node) {
272 		if (listener->aics_cb.set_gain) {
273 			listener->aics_cb.set_gain(inst, err);
274 		}
275 	}
276 }
277 
micp_mic_ctlr_aics_unmute_cb(struct bt_aics * inst,int err)278 static void micp_mic_ctlr_aics_unmute_cb(struct bt_aics *inst, int err)
279 {
280 	struct bt_micp_mic_ctlr_cb *listener, *next;
281 
282 	SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&micp_mic_ctlr_cbs, listener, next, _node) {
283 		if (listener->aics_cb.unmute) {
284 			listener->aics_cb.unmute(inst, err);
285 		}
286 	}
287 }
288 
micp_mic_ctlr_aics_mute_cb(struct bt_aics * inst,int err)289 static void micp_mic_ctlr_aics_mute_cb(struct bt_aics *inst, int err)
290 {
291 	struct bt_micp_mic_ctlr_cb *listener, *next;
292 
293 	SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&micp_mic_ctlr_cbs, listener, next, _node) {
294 		if (listener->aics_cb.mute) {
295 			listener->aics_cb.mute(inst, err);
296 		}
297 	}
298 }
299 
micp_mic_ctlr_aics_set_manual_mode_cb(struct bt_aics * inst,int err)300 static void micp_mic_ctlr_aics_set_manual_mode_cb(struct bt_aics *inst, int err)
301 {
302 	struct bt_micp_mic_ctlr_cb *listener, *next;
303 
304 	SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&micp_mic_ctlr_cbs, listener, next, _node) {
305 		if (listener->aics_cb.set_manual_mode) {
306 			listener->aics_cb.set_manual_mode(inst, err);
307 		}
308 	}
309 }
310 
micp_mic_ctlr_aics_set_auto_mode_cb(struct bt_aics * inst,int err)311 static void micp_mic_ctlr_aics_set_auto_mode_cb(struct bt_aics *inst, int err)
312 {
313 	struct bt_micp_mic_ctlr_cb *listener, *next;
314 
315 	SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&micp_mic_ctlr_cbs, listener, next, _node) {
316 		if (listener->aics_cb.set_auto_mode) {
317 			listener->aics_cb.set_auto_mode(inst, err);
318 		}
319 	}
320 }
321 
micp_discover_include_func(struct bt_conn * conn,const struct bt_gatt_attr * attr,struct bt_gatt_discover_params * params)322 static uint8_t micp_discover_include_func(
323 	struct bt_conn *conn, const struct bt_gatt_attr *attr,
324 	struct bt_gatt_discover_params *params)
325 {
326 	struct bt_micp_mic_ctlr *mic_ctlr = mic_ctlr_get_by_conn(conn);
327 
328 	if (attr == NULL) {
329 		LOG_DBG("Discover include complete for MICS: %u AICS", mic_ctlr->aics_inst_cnt);
330 		(void)memset(params, 0, sizeof(*params));
331 
332 		micp_mic_ctlr_discover_complete(mic_ctlr, 0);
333 
334 		return BT_GATT_ITER_STOP;
335 	}
336 
337 	LOG_DBG("[ATTRIBUTE] handle 0x%04X", attr->handle);
338 
339 	if (params->type == BT_GATT_DISCOVER_INCLUDE) {
340 		struct bt_gatt_include *include = (struct bt_gatt_include *)attr->user_data;
341 
342 		LOG_DBG("Include UUID %s", bt_uuid_str(include->uuid));
343 
344 		if (bt_uuid_cmp(include->uuid, BT_UUID_AICS) == 0 &&
345 		    mic_ctlr->aics_inst_cnt < CONFIG_BT_MICP_MIC_CTLR_MAX_AICS_INST) {
346 			uint8_t inst_idx;
347 			int err;
348 			struct bt_aics_discover_param param = {
349 				.start_handle = include->start_handle,
350 				.end_handle = include->end_handle,
351 			};
352 
353 			/* Update discover params so we can continue where we
354 			 * left off after bt_aics_discover
355 			 */
356 			mic_ctlr->discover_params.start_handle = attr->handle + 1;
357 
358 			inst_idx = mic_ctlr->aics_inst_cnt++;
359 			err = bt_aics_discover(conn, mic_ctlr->aics[inst_idx],
360 					       &param);
361 			if (err != 0) {
362 				LOG_DBG("AICS Discover failed (err %d)", err);
363 				micp_mic_ctlr_discover_complete(mic_ctlr, err);
364 			}
365 
366 			return BT_GATT_ITER_STOP;
367 		}
368 	}
369 
370 	return BT_GATT_ITER_CONTINUE;
371 }
372 #endif /* CONFIG_BT_MICP_MIC_CTLR_AICS */
373 
374 /**
375  * @brief This will discover all characteristics on the server, retrieving the
376  * handles of the writeable characteristics and subscribing to all notify and
377  * indicate characteristics.
378  */
micp_discover_func(struct bt_conn * conn,const struct bt_gatt_attr * attr,struct bt_gatt_discover_params * params)379 static uint8_t micp_discover_func(struct bt_conn *conn,
380 				  const struct bt_gatt_attr *attr,
381 				  struct bt_gatt_discover_params *params)
382 {
383 	struct bt_micp_mic_ctlr *mic_ctlr = mic_ctlr_get_by_conn(conn);
384 
385 	if (attr == NULL) {
386 		int err = 0;
387 
388 		LOG_DBG("Discovery complete");
389 		(void)memset(params, 0, sizeof(*params));
390 
391 #if defined(CONFIG_BT_MICP_MIC_CTLR_AICS)
392 		/* Discover included services */
393 		mic_ctlr->discover_params.start_handle = mic_ctlr->start_handle;
394 		mic_ctlr->discover_params.end_handle = mic_ctlr->end_handle;
395 		mic_ctlr->discover_params.type = BT_GATT_DISCOVER_INCLUDE;
396 		mic_ctlr->discover_params.func = micp_discover_include_func;
397 
398 		err = bt_gatt_discover(conn, &mic_ctlr->discover_params);
399 		if (err != 0) {
400 			LOG_DBG("Discover AICS failed (err %d)", err);
401 			micp_mic_ctlr_discover_complete(mic_ctlr, err);
402 		}
403 #else
404 		micp_mic_ctlr_discover_complete(mic_ctlr, err);
405 #endif /* CONFIG_BT_MICP_MIC_CTLR_AICS */
406 
407 		return BT_GATT_ITER_STOP;
408 	}
409 
410 	LOG_DBG("[ATTRIBUTE] handle 0x%04X", attr->handle);
411 
412 	if (params->type == BT_GATT_DISCOVER_CHARACTERISTIC) {
413 		struct bt_gatt_chrc *chrc = (struct bt_gatt_chrc *)attr->user_data;
414 		struct bt_gatt_subscribe_params *sub_params = NULL;
415 
416 		if (bt_uuid_cmp(chrc->uuid, BT_UUID_MICS_MUTE) == 0) {
417 			LOG_DBG("Mute");
418 			mic_ctlr->mute_handle = chrc->value_handle;
419 			sub_params = &mic_ctlr->mute_sub_params;
420 			sub_params->disc_params = &mic_ctlr->mute_sub_disc_params;
421 		}
422 
423 		if (sub_params != NULL) {
424 			int err;
425 
426 			sub_params->ccc_handle = BT_GATT_AUTO_DISCOVER_CCC_HANDLE;
427 			sub_params->end_handle = mic_ctlr->end_handle;
428 			sub_params->value = BT_GATT_CCC_NOTIFY;
429 			sub_params->value_handle = chrc->value_handle;
430 			sub_params->notify = mute_notify_handler;
431 			atomic_set_bit(sub_params->flags, BT_GATT_SUBSCRIBE_FLAG_VOLATILE);
432 
433 			err = bt_gatt_subscribe(conn, sub_params);
434 			if (err == 0 || err == -EALREADY) {
435 				LOG_DBG("Subscribed to handle 0x%04X", attr->handle);
436 			} else {
437 				LOG_DBG("Could not subscribe to handle 0x%04X: %d", attr->handle,
438 					err);
439 
440 				micp_mic_ctlr_discover_complete(mic_ctlr, err);
441 
442 				return BT_GATT_ITER_STOP;
443 			}
444 		}
445 	}
446 
447 	return BT_GATT_ITER_CONTINUE;
448 }
449 
primary_discover_func(struct bt_conn * conn,const struct bt_gatt_attr * attr,struct bt_gatt_discover_params * params)450 static uint8_t primary_discover_func(struct bt_conn *conn,
451 				     const struct bt_gatt_attr *attr,
452 				     struct bt_gatt_discover_params *params)
453 {
454 	struct bt_micp_mic_ctlr *mic_ctlr = mic_ctlr_get_by_conn(conn);
455 
456 	if (attr == NULL) {
457 		LOG_DBG("Could not find a MICS instance on the server");
458 		micp_mic_ctlr_discover_complete(mic_ctlr, -ENODATA);
459 
460 		return BT_GATT_ITER_STOP;
461 	}
462 
463 	LOG_DBG("[ATTRIBUTE] handle 0x%04X", attr->handle);
464 
465 	if (params->type == BT_GATT_DISCOVER_PRIMARY) {
466 		struct bt_gatt_service_val *prim_service =
467 			(struct bt_gatt_service_val *)attr->user_data;
468 		int err;
469 
470 		LOG_DBG("Primary discover complete");
471 		mic_ctlr->start_handle = attr->handle + 1;
472 		mic_ctlr->end_handle = prim_service->end_handle;
473 
474 		/* Discover characteristics */
475 		mic_ctlr->discover_params.uuid = NULL;
476 		mic_ctlr->discover_params.start_handle = mic_ctlr->start_handle;
477 		mic_ctlr->discover_params.end_handle = mic_ctlr->end_handle;
478 		mic_ctlr->discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC;
479 		mic_ctlr->discover_params.func = micp_discover_func;
480 
481 		err = bt_gatt_discover(conn, &mic_ctlr->discover_params);
482 		if (err != 0) {
483 			LOG_DBG("Discover failed (err %d)", err);
484 			micp_mic_ctlr_discover_complete(mic_ctlr, err);
485 		}
486 
487 		return BT_GATT_ITER_STOP;
488 	}
489 
490 	return BT_GATT_ITER_CONTINUE;
491 }
492 
micp_mic_ctlr_reset(struct bt_micp_mic_ctlr * mic_ctlr)493 static void micp_mic_ctlr_reset(struct bt_micp_mic_ctlr *mic_ctlr)
494 {
495 	mic_ctlr->start_handle = 0;
496 	mic_ctlr->end_handle = 0;
497 	mic_ctlr->mute_handle = 0;
498 #if defined(CONFIG_BT_MICP_MIC_CTLR_AICS)
499 	mic_ctlr->aics_inst_cnt = 0;
500 #endif /* CONFIG_BT_MICP_MIC_CTLR_AICS */
501 
502 	if (mic_ctlr->conn != NULL) {
503 		struct bt_conn *conn = mic_ctlr->conn;
504 
505 		bt_conn_unref(conn);
506 		mic_ctlr->conn = NULL;
507 	}
508 }
509 
disconnected(struct bt_conn * conn,uint8_t reason)510 static void disconnected(struct bt_conn *conn, uint8_t reason)
511 {
512 	struct bt_micp_mic_ctlr *mic_ctlr = mic_ctlr_get_by_conn(conn);
513 
514 	if (mic_ctlr->conn == conn) {
515 		micp_mic_ctlr_reset(mic_ctlr);
516 	}
517 }
518 
519 BT_CONN_CB_DEFINE(conn_callbacks) = {
520 	.disconnected = disconnected,
521 };
522 
bt_micp_mic_ctlr_discover(struct bt_conn * conn,struct bt_micp_mic_ctlr ** mic_ctlr_out)523 int bt_micp_mic_ctlr_discover(struct bt_conn *conn, struct bt_micp_mic_ctlr **mic_ctlr_out)
524 {
525 	struct bt_micp_mic_ctlr *mic_ctlr;
526 	int err;
527 
528 	/*
529 	 * This will initiate a discover procedure. The procedure will do the
530 	 * following sequence:
531 	 * 1) Primary discover for the MICS
532 	 * 2) Characteristic discover of the MICS
533 	 * 3) Discover services included in MICS (AICS)
534 	 * 4) For each included service found; discovery of the characteristics
535 	 * 5) When everything above have been discovered, the callback is called
536 	 */
537 
538 	CHECKIF(conn == NULL) {
539 		LOG_DBG("NULL conn");
540 		return -EINVAL;
541 	}
542 
543 	mic_ctlr = mic_ctlr_get_by_conn(conn);
544 	if (atomic_test_and_set_bit(mic_ctlr->flags, BT_MICP_MIC_CTLR_FLAG_BUSY)) {
545 		LOG_DBG("Instance is busy");
546 
547 		return -EBUSY;
548 	}
549 
550 	(void)memset(&mic_ctlr->discover_params, 0,
551 		     sizeof(mic_ctlr->discover_params));
552 	micp_mic_ctlr_reset(mic_ctlr);
553 
554 #if defined(CONFIG_BT_MICP_MIC_CTLR_AICS)
555 	static bool initialized;
556 
557 	if (!initialized) {
558 		for (size_t i = 0U; i < ARRAY_SIZE(mic_ctlrs); i++) {
559 			for (size_t j = 0U; j < ARRAY_SIZE(mic_ctlr->aics); j++) {
560 				static struct bt_aics_cb aics_cb = {
561 					.state = micp_mic_ctlr_aics_state_cb,
562 					.gain_setting = micp_mic_ctlr_aics_gain_setting_cb,
563 					.type = micp_mic_ctlr_aics_type_cb,
564 					.status = micp_mic_ctlr_aics_status_cb,
565 					.description = micp_mic_ctlr_aics_description_cb,
566 					.discover = micp_mic_ctlr_aics_discover_cb,
567 					.set_gain = micp_mic_ctlr_aics_set_gain_cb,
568 					.unmute = micp_mic_ctlr_aics_unmute_cb,
569 					.mute = micp_mic_ctlr_aics_mute_cb,
570 					.set_manual_mode = micp_mic_ctlr_aics_set_manual_mode_cb,
571 					.set_auto_mode = micp_mic_ctlr_aics_set_auto_mode_cb,
572 				};
573 
574 				mic_ctlrs[i].aics[j] = bt_aics_client_free_instance_get();
575 
576 				if (mic_ctlrs[i].aics[j] == NULL) {
577 					return -ENOMEM;
578 				}
579 
580 				bt_aics_client_cb_register(mic_ctlrs[i].aics[j], &aics_cb);
581 			}
582 		}
583 
584 		initialized = true;
585 	}
586 #endif /* CONFIG_BT_MICP_MIC_CTLR_AICS */
587 
588 	mic_ctlr->conn = bt_conn_ref(conn);
589 	mic_ctlr->discover_params.func = primary_discover_func;
590 	mic_ctlr->discover_params.uuid = mics_uuid;
591 	mic_ctlr->discover_params.type = BT_GATT_DISCOVER_PRIMARY;
592 	mic_ctlr->discover_params.start_handle = BT_ATT_FIRST_ATTRIBUTE_HANDLE;
593 	mic_ctlr->discover_params.end_handle = BT_ATT_LAST_ATTRIBUTE_HANDLE;
594 
595 	err = bt_gatt_discover(conn, &mic_ctlr->discover_params);
596 	if (err == 0) {
597 		*mic_ctlr_out = mic_ctlr;
598 	} else {
599 		atomic_clear_bit(mic_ctlr->flags, BT_MICP_MIC_CTLR_FLAG_BUSY);
600 	}
601 
602 	return err;
603 }
604 
bt_micp_mic_ctlr_cb_register(struct bt_micp_mic_ctlr_cb * cb)605 int bt_micp_mic_ctlr_cb_register(struct bt_micp_mic_ctlr_cb *cb)
606 {
607 	struct bt_micp_mic_ctlr_cb *tmp;
608 
609 	CHECKIF(cb == NULL) {
610 		return -EINVAL;
611 	}
612 
613 	SYS_SLIST_FOR_EACH_CONTAINER(&micp_mic_ctlr_cbs, tmp, _node) {
614 		if (tmp == cb) {
615 			LOG_DBG("Already registered");
616 			return -EALREADY;
617 		}
618 	}
619 
620 	sys_slist_append(&micp_mic_ctlr_cbs, &cb->_node);
621 
622 	return 0;
623 }
624 
625 #if defined(CONFIG_BT_MICP_MIC_CTLR_AICS)
bt_micp_mic_ctlr_included_get(struct bt_micp_mic_ctlr * mic_ctlr,struct bt_micp_included * included)626 int bt_micp_mic_ctlr_included_get(struct bt_micp_mic_ctlr *mic_ctlr,
627 				  struct bt_micp_included *included)
628 {
629 	CHECKIF(mic_ctlr == NULL) {
630 		LOG_DBG("NULL mic_ctlr");
631 		return -EINVAL;
632 	}
633 
634 	CHECKIF(included == NULL) {
635 		return -EINVAL;
636 	}
637 
638 	included->aics_cnt = mic_ctlr->aics_inst_cnt;
639 	included->aics = mic_ctlr->aics;
640 
641 	return 0;
642 }
643 #endif /* CONFIG_BT_MICP_MIC_CTLR_AICS */
644 
bt_micp_mic_ctlr_get_by_conn(const struct bt_conn * conn)645 struct bt_micp_mic_ctlr *bt_micp_mic_ctlr_get_by_conn(const struct bt_conn *conn)
646 {
647 	struct bt_micp_mic_ctlr *mic_ctlr;
648 
649 	CHECKIF(conn == NULL) {
650 		LOG_DBG("NULL conn pointer");
651 		return NULL;
652 	}
653 
654 	mic_ctlr = mic_ctlr_get_by_conn(conn);
655 	if (mic_ctlr->conn == NULL) {
656 		LOG_DBG("conn %p is not associated with microphone controller. Do discovery first",
657 			(void *)conn);
658 		return NULL;
659 	}
660 
661 	return mic_ctlr;
662 }
663 
bt_micp_mic_ctlr_conn_get(const struct bt_micp_mic_ctlr * mic_ctlr,struct bt_conn ** conn)664 int bt_micp_mic_ctlr_conn_get(const struct bt_micp_mic_ctlr *mic_ctlr, struct bt_conn **conn)
665 {
666 	CHECKIF(mic_ctlr == NULL) {
667 		LOG_DBG("NULL mic_ctlr pointer");
668 		return -EINVAL;
669 	}
670 
671 	if (mic_ctlr->conn == NULL) {
672 		LOG_DBG("mic_ctlr pointer not associated with a connection. "
673 		       "Do discovery first");
674 		return -ENOTCONN;
675 	}
676 
677 	*conn = mic_ctlr->conn;
678 	return 0;
679 }
680 
bt_micp_mic_ctlr_mute_get(struct bt_micp_mic_ctlr * mic_ctlr)681 int bt_micp_mic_ctlr_mute_get(struct bt_micp_mic_ctlr *mic_ctlr)
682 {
683 	int err;
684 
685 	CHECKIF(mic_ctlr == NULL) {
686 		LOG_DBG("NULL mic_ctlr");
687 		return -EINVAL;
688 	}
689 
690 	if (mic_ctlr->mute_handle == 0) {
691 		LOG_DBG("Handle not set");
692 		return -EINVAL;
693 	} else if (atomic_test_and_set_bit(mic_ctlr->flags, BT_MICP_MIC_CTLR_FLAG_BUSY)) {
694 		LOG_DBG("Instance is busy");
695 
696 		return -EBUSY;
697 	}
698 
699 	mic_ctlr->read_params.func = micp_mic_ctlr_read_mute_cb;
700 	mic_ctlr->read_params.handle_count = 1;
701 	mic_ctlr->read_params.single.handle = mic_ctlr->mute_handle;
702 	mic_ctlr->read_params.single.offset = 0U;
703 
704 	err = bt_gatt_read(mic_ctlr->conn, &mic_ctlr->read_params);
705 	if (err != 0) {
706 		atomic_clear_bit(mic_ctlr->flags, BT_MICP_MIC_CTLR_FLAG_BUSY);
707 	}
708 
709 	return err;
710 }
711 
bt_micp_mic_ctlr_write_mute(struct bt_micp_mic_ctlr * mic_ctlr,bool mute)712 static int bt_micp_mic_ctlr_write_mute(struct bt_micp_mic_ctlr *mic_ctlr, bool mute)
713 {
714 	int err;
715 
716 	CHECKIF(mic_ctlr == NULL) {
717 		LOG_DBG("NULL mic_ctlr");
718 		return -EINVAL;
719 	}
720 
721 	if (mic_ctlr->mute_handle == 0) {
722 		LOG_DBG("Handle not set");
723 		return -EINVAL;
724 	} else if (atomic_test_and_set_bit(mic_ctlr->flags, BT_MICP_MIC_CTLR_FLAG_BUSY)) {
725 		LOG_DBG("Instance is busy");
726 
727 		return -EBUSY;
728 	}
729 
730 	mic_ctlr->mute_val_buf[0] = mute;
731 	mic_ctlr->write_params.offset = 0;
732 	mic_ctlr->write_params.data = mic_ctlr->mute_val_buf;
733 	mic_ctlr->write_params.length = sizeof(mute);
734 	mic_ctlr->write_params.handle = mic_ctlr->mute_handle;
735 	mic_ctlr->write_params.func = micp_mic_ctlr_write_mics_mute_cb;
736 
737 	err = bt_gatt_write(mic_ctlr->conn, &mic_ctlr->write_params);
738 	if (err != 0) {
739 		atomic_clear_bit(mic_ctlr->flags, BT_MICP_MIC_CTLR_FLAG_BUSY);
740 	}
741 
742 	return err;
743 }
744 
bt_micp_mic_ctlr_mute(struct bt_micp_mic_ctlr * mic_ctlr)745 int bt_micp_mic_ctlr_mute(struct bt_micp_mic_ctlr *mic_ctlr)
746 {
747 	return bt_micp_mic_ctlr_write_mute(mic_ctlr, true);
748 }
749 
bt_micp_mic_ctlr_unmute(struct bt_micp_mic_ctlr * mic_ctlr)750 int bt_micp_mic_ctlr_unmute(struct bt_micp_mic_ctlr *mic_ctlr)
751 {
752 	return bt_micp_mic_ctlr_write_mute(mic_ctlr, false);
753 }
754