1 /*  Bluetooth MICP - Microphone Control Profile - Microphone Controller */
2 
3 /*
4  * Copyright (c) 2020 Bose Corporation
5  * Copyright (c) 2020-2022 Nordic Semiconductor ASA
6  *
7  * SPDX-License-Identifier: Apache-2.0
8  */
9 
10 #include <zephyr/kernel.h>
11 #include <zephyr/types.h>
12 
13 #include <zephyr/sys/check.h>
14 
15 #include <zephyr/device.h>
16 #include <zephyr/init.h>
17 
18 #include <zephyr/bluetooth/bluetooth.h>
19 #include <zephyr/bluetooth/conn.h>
20 #include <zephyr/bluetooth/gatt.h>
21 #include <zephyr/bluetooth/audio/micp.h>
22 
23 #include <zephyr/logging/log.h>
24 
25 LOG_MODULE_REGISTER(bt_micp_mic_ctlr, CONFIG_BT_MICP_MIC_CTLR_LOG_LEVEL);
26 
27 #include "common/bt_str.h"
28 
29 /* Callback functions */
30 static struct bt_micp_mic_ctlr_cb *micp_mic_ctlr_cb;
31 
32 struct bt_micp_mic_ctlr {
33 	uint16_t start_handle;
34 	uint16_t end_handle;
35 	uint16_t mute_handle;
36 	struct bt_gatt_subscribe_params mute_sub_params;
37 	struct bt_gatt_discover_params mute_sub_disc_params;
38 
39 	bool busy;
40 	uint8_t mute_val_buf[1]; /* Mute value is a single octet */
41 	struct bt_gatt_write_params write_params;
42 	struct bt_gatt_read_params read_params;
43 	struct bt_gatt_discover_params discover_params;
44 	struct bt_conn *conn;
45 
46 	uint8_t aics_inst_cnt;
47 	struct bt_aics *aics[CONFIG_BT_MICP_MIC_CTLR_MAX_AICS_INST];
48 };
49 
50 static struct bt_micp_mic_ctlr mic_ctlrs[CONFIG_BT_MAX_CONN];
51 static struct bt_uuid *mics_uuid = BT_UUID_MICS;
52 
mute_notify_handler(struct bt_conn * conn,struct bt_gatt_subscribe_params * params,const void * data,uint16_t length)53 static uint8_t mute_notify_handler(struct bt_conn *conn,
54 				   struct bt_gatt_subscribe_params *params,
55 				   const void *data, uint16_t length)
56 {
57 	uint8_t *mute_val;
58 	struct bt_micp_mic_ctlr *mic_ctlr;
59 
60 	if (conn == NULL) {
61 		return BT_GATT_ITER_CONTINUE;
62 	}
63 
64 	mic_ctlr = &mic_ctlrs[bt_conn_index(conn)];
65 
66 	if (data != NULL) {
67 		if (length == sizeof(*mute_val)) {
68 			mute_val = (uint8_t *)data;
69 			LOG_DBG("Mute %u", *mute_val);
70 			if (micp_mic_ctlr_cb != NULL &&
71 			    micp_mic_ctlr_cb->mute != NULL) {
72 				micp_mic_ctlr_cb->mute(mic_ctlr, 0, *mute_val);
73 			}
74 		} else {
75 			LOG_DBG("Invalid length %u (expected %zu)", length, sizeof(*mute_val));
76 		}
77 	}
78 
79 	return BT_GATT_ITER_CONTINUE;
80 }
81 
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)82 static uint8_t micp_mic_ctlr_read_mute_cb(struct bt_conn *conn, uint8_t err,
83 					struct bt_gatt_read_params *params,
84 					const void *data, uint16_t length)
85 {
86 	uint8_t cb_err = err;
87 	uint8_t mute_val = 0;
88 	struct bt_micp_mic_ctlr *mic_ctlr = &mic_ctlrs[bt_conn_index(conn)];
89 
90 	mic_ctlr->busy = false;
91 
92 	if (err > 0) {
93 		LOG_DBG("err: 0x%02X", err);
94 	} else if (data != NULL) {
95 		if (length == sizeof(mute_val)) {
96 			mute_val = ((uint8_t *)data)[0];
97 			LOG_DBG("Mute %u", mute_val);
98 		} else {
99 			LOG_DBG("Invalid length %u (expected %zu)", length, sizeof(mute_val));
100 			cb_err = BT_ATT_ERR_INVALID_ATTRIBUTE_LEN;
101 		}
102 	}
103 
104 	if (micp_mic_ctlr_cb != NULL && micp_mic_ctlr_cb->mute != NULL) {
105 		micp_mic_ctlr_cb->mute(mic_ctlr, cb_err, mute_val);
106 	}
107 
108 	return BT_GATT_ITER_STOP;
109 }
110 
micp_mic_ctlr_write_mics_mute_cb(struct bt_conn * conn,uint8_t err,struct bt_gatt_write_params * params)111 static void micp_mic_ctlr_write_mics_mute_cb(struct bt_conn *conn, uint8_t err,
112 					   struct bt_gatt_write_params *params)
113 {
114 	struct bt_micp_mic_ctlr *mic_ctlr = &mic_ctlrs[bt_conn_index(conn)];
115 	uint8_t mute_val = mic_ctlr->mute_val_buf[0];
116 
117 	LOG_DBG("Write %s (0x%02X)", err ? "failed" : "successful", err);
118 
119 	mic_ctlr->busy = false;
120 
121 	if (mute_val == BT_MICP_MUTE_UNMUTED) {
122 		if (micp_mic_ctlr_cb != NULL &&
123 		    micp_mic_ctlr_cb->unmute_written != NULL) {
124 			micp_mic_ctlr_cb->unmute_written(mic_ctlr, err);
125 		}
126 
127 	} else {
128 		if (micp_mic_ctlr_cb != NULL &&
129 		    micp_mic_ctlr_cb->mute_written != NULL) {
130 			micp_mic_ctlr_cb->mute_written(mic_ctlr, err);
131 		}
132 	}
133 }
134 
135 #if defined(CONFIG_BT_MICP_MIC_CTLR_AICS)
lookup_micp_by_aics(const struct bt_aics * aics)136 static struct bt_micp_mic_ctlr *lookup_micp_by_aics(const struct bt_aics *aics)
137 {
138 	__ASSERT(aics != NULL, "AICS pointer cannot be NULL");
139 
140 	for (int i = 0; i < ARRAY_SIZE(mic_ctlrs); i++) {
141 		for (int j = 0; j < ARRAY_SIZE(mic_ctlrs[i].aics); j++) {
142 			if (mic_ctlrs[i].aics[j] == aics) {
143 				return &mic_ctlrs[i];
144 			}
145 		}
146 	}
147 
148 	return NULL;
149 }
150 
aics_discover_cb(struct bt_aics * inst,int err)151 static void aics_discover_cb(struct bt_aics *inst, int err)
152 {
153 	struct bt_micp_mic_ctlr *mic_ctlr = lookup_micp_by_aics(inst);
154 
155 	if (err == 0) {
156 		/* Continue discovery of included services */
157 		err = bt_gatt_discover(mic_ctlr->conn,
158 				       &mic_ctlr->discover_params);
159 	}
160 
161 	if (err != 0) {
162 		LOG_DBG("Discover failed (err %d)", err);
163 		if (micp_mic_ctlr_cb != NULL &&
164 		    micp_mic_ctlr_cb->discover != NULL) {
165 			micp_mic_ctlr_cb->discover(mic_ctlr, err, 0);
166 		}
167 	}
168 }
169 #endif /* CONFIG_BT_MICP_MIC_CTLR_AICS */
170 
micp_discover_include_func(struct bt_conn * conn,const struct bt_gatt_attr * attr,struct bt_gatt_discover_params * params)171 static uint8_t micp_discover_include_func(
172 	struct bt_conn *conn, const struct bt_gatt_attr *attr,
173 	struct bt_gatt_discover_params *params)
174 {
175 	struct bt_micp_mic_ctlr *mic_ctlr = &mic_ctlrs[bt_conn_index(conn)];
176 
177 	if (attr == NULL) {
178 		LOG_DBG("Discover include complete for MICS: %u AICS", mic_ctlr->aics_inst_cnt);
179 		(void)memset(params, 0, sizeof(*params));
180 
181 		if (micp_mic_ctlr_cb != NULL &&
182 		    micp_mic_ctlr_cb->discover != NULL) {
183 			micp_mic_ctlr_cb->discover(mic_ctlr, 0,
184 						   mic_ctlr->aics_inst_cnt);
185 		}
186 
187 		return BT_GATT_ITER_STOP;
188 	}
189 
190 	LOG_DBG("[ATTRIBUTE] handle 0x%04X", attr->handle);
191 
192 	if (params->type == BT_GATT_DISCOVER_INCLUDE) {
193 		struct bt_gatt_include *include = (struct bt_gatt_include *)attr->user_data;
194 
195 		LOG_DBG("Include UUID %s", bt_uuid_str(include->uuid));
196 
197 		if (bt_uuid_cmp(include->uuid, BT_UUID_AICS) == 0 &&
198 		    mic_ctlr->aics_inst_cnt < CONFIG_BT_MICP_MIC_CTLR_MAX_AICS_INST) {
199 			uint8_t inst_idx;
200 			int err;
201 			struct bt_aics_discover_param param = {
202 				.start_handle = include->start_handle,
203 				.end_handle = include->end_handle,
204 			};
205 
206 			/* Update discover params so we can continue where we
207 			 * left off after bt_aics_discover
208 			 */
209 			mic_ctlr->discover_params.start_handle = attr->handle + 1;
210 
211 			inst_idx = mic_ctlr->aics_inst_cnt++;
212 			err = bt_aics_discover(conn, mic_ctlr->aics[inst_idx],
213 					       &param);
214 			if (err != 0) {
215 				LOG_DBG("AICS Discover failed (err %d)", err);
216 				if (micp_mic_ctlr_cb != NULL &&
217 				    micp_mic_ctlr_cb->discover != NULL) {
218 					micp_mic_ctlr_cb->discover(mic_ctlr, err,
219 								 0);
220 				}
221 			}
222 			return BT_GATT_ITER_STOP;
223 		}
224 	}
225 
226 	return BT_GATT_ITER_CONTINUE;
227 }
228 
229 /**
230  * @brief This will discover all characteristics on the server, retrieving the
231  * handles of the writeable characteristics and subscribing to all notify and
232  * indicate characteristics.
233  */
micp_discover_func(struct bt_conn * conn,const struct bt_gatt_attr * attr,struct bt_gatt_discover_params * params)234 static uint8_t micp_discover_func(struct bt_conn *conn,
235 				  const struct bt_gatt_attr *attr,
236 				  struct bt_gatt_discover_params *params)
237 {
238 	struct bt_micp_mic_ctlr *mic_ctlr = &mic_ctlrs[bt_conn_index(conn)];
239 
240 	if (attr == NULL) {
241 		int err = 0;
242 
243 		LOG_DBG("Discovery complete");
244 		(void)memset(params, 0, sizeof(*params));
245 		if (CONFIG_BT_MICP_MIC_CTLR_MAX_AICS_INST > 0) {
246 			/* Discover included services */
247 			mic_ctlr->discover_params.start_handle = mic_ctlr->start_handle;
248 			mic_ctlr->discover_params.end_handle = mic_ctlr->end_handle;
249 			mic_ctlr->discover_params.type = BT_GATT_DISCOVER_INCLUDE;
250 			mic_ctlr->discover_params.func = micp_discover_include_func;
251 
252 			err = bt_gatt_discover(conn,
253 					       &mic_ctlr->discover_params);
254 			if (err != 0) {
255 				LOG_DBG("Discover AICS failed (err %d)", err);
256 				if (micp_mic_ctlr_cb != NULL &&
257 				    micp_mic_ctlr_cb->discover != NULL) {
258 					micp_mic_ctlr_cb->discover(mic_ctlr, err, 0);
259 				}
260 			}
261 		} else {
262 			if (micp_mic_ctlr_cb != NULL &&
263 			    micp_mic_ctlr_cb->discover != NULL) {
264 				micp_mic_ctlr_cb->discover(mic_ctlr, err, 0);
265 			}
266 		}
267 		return BT_GATT_ITER_STOP;
268 	}
269 
270 	LOG_DBG("[ATTRIBUTE] handle 0x%04X", attr->handle);
271 
272 	if (params->type == BT_GATT_DISCOVER_CHARACTERISTIC) {
273 		struct bt_gatt_chrc *chrc = (struct bt_gatt_chrc *)attr->user_data;
274 		struct bt_gatt_subscribe_params *sub_params = NULL;
275 
276 		if (bt_uuid_cmp(chrc->uuid, BT_UUID_MICS_MUTE) == 0) {
277 			LOG_DBG("Mute");
278 			mic_ctlr->mute_handle = chrc->value_handle;
279 			sub_params = &mic_ctlr->mute_sub_params;
280 			sub_params->disc_params = &mic_ctlr->mute_sub_disc_params;
281 		}
282 
283 		if (sub_params != NULL) {
284 			int err;
285 
286 			/* With ccc_handle == 0 it will use auto discovery */
287 			sub_params->ccc_handle = 0;
288 			sub_params->end_handle = mic_ctlr->end_handle;
289 			sub_params->value = BT_GATT_CCC_NOTIFY;
290 			sub_params->value_handle = chrc->value_handle;
291 			sub_params->notify = mute_notify_handler;
292 
293 			err = bt_gatt_subscribe(conn, sub_params);
294 			if (err == 0) {
295 				LOG_DBG("Subscribed to handle 0x%04X", attr->handle);
296 			} else {
297 				LOG_DBG("Could not subscribe to handle 0x%04X: %d", attr->handle,
298 					err);
299 			}
300 		}
301 	}
302 
303 	return BT_GATT_ITER_CONTINUE;
304 }
305 
primary_discover_func(struct bt_conn * conn,const struct bt_gatt_attr * attr,struct bt_gatt_discover_params * params)306 static uint8_t primary_discover_func(struct bt_conn *conn,
307 				     const struct bt_gatt_attr *attr,
308 				     struct bt_gatt_discover_params *params)
309 {
310 	struct bt_micp_mic_ctlr *mic_ctlr = &mic_ctlrs[bt_conn_index(conn)];
311 
312 	if (attr == NULL) {
313 		LOG_DBG("Could not find a MICS instance on the server");
314 		if (micp_mic_ctlr_cb != NULL &&
315 		    micp_mic_ctlr_cb->discover != NULL) {
316 			micp_mic_ctlr_cb->discover(mic_ctlr, -ENODATA, 0);
317 		}
318 		return BT_GATT_ITER_STOP;
319 	}
320 
321 	LOG_DBG("[ATTRIBUTE] handle 0x%04X", attr->handle);
322 
323 	if (params->type == BT_GATT_DISCOVER_PRIMARY) {
324 		struct bt_gatt_service_val *prim_service =
325 			(struct bt_gatt_service_val *)attr->user_data;
326 		int err;
327 
328 		LOG_DBG("Primary discover complete");
329 		mic_ctlr->start_handle = attr->handle + 1;
330 		mic_ctlr->end_handle = prim_service->end_handle;
331 
332 		/* Discover characteristics */
333 		mic_ctlr->discover_params.uuid = NULL;
334 		mic_ctlr->discover_params.start_handle = mic_ctlr->start_handle;
335 		mic_ctlr->discover_params.end_handle = mic_ctlr->end_handle;
336 		mic_ctlr->discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC;
337 		mic_ctlr->discover_params.func = micp_discover_func;
338 
339 		err = bt_gatt_discover(conn, &mic_ctlr->discover_params);
340 		if (err != 0) {
341 			LOG_DBG("Discover failed (err %d)", err);
342 			if (micp_mic_ctlr_cb != NULL &&
343 			    micp_mic_ctlr_cb->discover != NULL) {
344 				micp_mic_ctlr_cb->discover(mic_ctlr, err, 0);
345 			}
346 		}
347 
348 		return BT_GATT_ITER_STOP;
349 	}
350 
351 	return BT_GATT_ITER_CONTINUE;
352 }
353 
micp_mic_ctlr_reset(struct bt_micp_mic_ctlr * mic_ctlr)354 static void micp_mic_ctlr_reset(struct bt_micp_mic_ctlr *mic_ctlr)
355 {
356 	mic_ctlr->start_handle = 0;
357 	mic_ctlr->end_handle = 0;
358 	mic_ctlr->mute_handle = 0;
359 	mic_ctlr->aics_inst_cnt = 0;
360 
361 	if (mic_ctlr->conn != NULL) {
362 		struct bt_conn *conn = mic_ctlr->conn;
363 
364 		/* It's okay if this fails. In case of disconnect, we can't
365 		 * unsubscribe and it will just fail.
366 		 * In case that we reset due to another call of the discover
367 		 * function, we will unsubscribe (regardless of bonding state)
368 		 * to accommodate the new discovery values.
369 		 */
370 		(void)bt_gatt_unsubscribe(conn, &mic_ctlr->mute_sub_params);
371 
372 		bt_conn_unref(conn);
373 		mic_ctlr->conn = NULL;
374 	}
375 }
376 
disconnected(struct bt_conn * conn,uint8_t reason)377 static void disconnected(struct bt_conn *conn, uint8_t reason)
378 {
379 	struct bt_micp_mic_ctlr *mic_ctlr = &mic_ctlrs[bt_conn_index(conn)];
380 
381 	if (mic_ctlr->conn == conn) {
382 		micp_mic_ctlr_reset(mic_ctlr);
383 	}
384 }
385 
386 BT_CONN_CB_DEFINE(conn_callbacks) = {
387 	.disconnected = disconnected,
388 };
389 
bt_micp_mic_ctlr_discover(struct bt_conn * conn,struct bt_micp_mic_ctlr ** mic_ctlr_out)390 int bt_micp_mic_ctlr_discover(struct bt_conn *conn, struct bt_micp_mic_ctlr **mic_ctlr_out)
391 {
392 	struct bt_micp_mic_ctlr *mic_ctlr;
393 	int err;
394 
395 	/*
396 	 * This will initiate a discover procedure. The procedure will do the
397 	 * following sequence:
398 	 * 1) Primary discover for the MICS
399 	 * 2) Characteristic discover of the MICS
400 	 * 3) Discover services included in MICS (AICS)
401 	 * 4) For each included service found; discovery of the characteristics
402 	 * 5) When everything above have been discovered, the callback is called
403 	 */
404 
405 	CHECKIF(conn == NULL) {
406 		LOG_DBG("NULL conn");
407 		return -EINVAL;
408 	}
409 
410 	mic_ctlr = &mic_ctlrs[bt_conn_index(conn)];
411 
412 	(void)memset(&mic_ctlr->discover_params, 0,
413 		     sizeof(mic_ctlr->discover_params));
414 	micp_mic_ctlr_reset(mic_ctlr);
415 
416 #if defined(CONFIG_BT_MICP_MIC_CTLR_AICS)
417 	static bool initialized;
418 
419 	if (!initialized) {
420 		for (int i = 0; i < ARRAY_SIZE(mic_ctlr->aics); i++) {
421 			mic_ctlr->aics[i] = bt_aics_client_free_instance_get();
422 
423 			if (mic_ctlr->aics[i] == NULL) {
424 				return -ENOMEM;
425 			}
426 
427 			bt_aics_client_cb_register(mic_ctlr->aics[i],
428 						   &micp_mic_ctlr_cb->aics_cb);
429 		}
430 	}
431 
432 	initialized = true;
433 #endif /* CONFIG_BT_MICP_MIC_CTLR_AICS */
434 
435 	mic_ctlr->conn = bt_conn_ref(conn);
436 	mic_ctlr->discover_params.func = primary_discover_func;
437 	mic_ctlr->discover_params.uuid = mics_uuid;
438 	mic_ctlr->discover_params.type = BT_GATT_DISCOVER_PRIMARY;
439 	mic_ctlr->discover_params.start_handle = BT_ATT_FIRST_ATTRIBUTE_HANDLE;
440 	mic_ctlr->discover_params.end_handle = BT_ATT_LAST_ATTRIBUTE_HANDLE;
441 
442 	err = bt_gatt_discover(conn, &mic_ctlr->discover_params);
443 	if (err == 0) {
444 		*mic_ctlr_out = mic_ctlr;
445 	}
446 
447 	return err;
448 }
449 
bt_micp_mic_ctlr_cb_register(struct bt_micp_mic_ctlr_cb * cb)450 int bt_micp_mic_ctlr_cb_register(struct bt_micp_mic_ctlr_cb *cb)
451 {
452 #if defined(CONFIG_BT_MICP_MIC_CTLR_AICS)
453 	struct bt_aics_cb *aics_cb = NULL;
454 
455 	if (cb != NULL) {
456 		/* Ensure that the cb->aics_cb.discover is the aics_discover_cb */
457 		CHECKIF(cb->aics_cb.discover != NULL &&
458 			cb->aics_cb.discover != aics_discover_cb) {
459 			LOG_ERR("AICS discover callback shall not be set");
460 			return -EINVAL;
461 		}
462 		cb->aics_cb.discover = aics_discover_cb;
463 
464 		aics_cb = &cb->aics_cb;
465 	}
466 
467 	for (int i = 0; i < ARRAY_SIZE(mic_ctlrs); i++) {
468 		for (int j = 0; j < ARRAY_SIZE(mic_ctlrs[i].aics); j++) {
469 			struct bt_aics *aics = mic_ctlrs[i].aics[j];
470 
471 			if (aics != NULL) {
472 				bt_aics_client_cb_register(aics, aics_cb);
473 			}
474 		}
475 	}
476 #endif /* CONFIG_BT_MICP_MIC_CTLR_AICS */
477 
478 	micp_mic_ctlr_cb = cb;
479 
480 	return 0;
481 }
482 
bt_micp_mic_ctlr_included_get(struct bt_micp_mic_ctlr * mic_ctlr,struct bt_micp_included * included)483 int bt_micp_mic_ctlr_included_get(struct bt_micp_mic_ctlr *mic_ctlr,
484 				  struct bt_micp_included *included)
485 {
486 	CHECKIF(mic_ctlr == NULL) {
487 		LOG_DBG("NULL mic_ctlr");
488 		return -EINVAL;
489 	}
490 
491 	CHECKIF(included == NULL) {
492 		return -EINVAL;
493 	}
494 
495 	included->aics_cnt = mic_ctlr->aics_inst_cnt;
496 	included->aics = mic_ctlr->aics;
497 
498 	return 0;
499 }
500 
bt_micp_mic_ctlr_conn_get(const struct bt_micp_mic_ctlr * mic_ctlr,struct bt_conn ** conn)501 int bt_micp_mic_ctlr_conn_get(const struct bt_micp_mic_ctlr *mic_ctlr, struct bt_conn **conn)
502 {
503 	CHECKIF(mic_ctlr == NULL) {
504 		LOG_DBG("NULL mic_ctlr pointer");
505 		return -EINVAL;
506 	}
507 
508 	if (mic_ctlr->conn == NULL) {
509 		LOG_DBG("mic_ctlr pointer not associated with a connection. "
510 		       "Do discovery first");
511 		return -ENOTCONN;
512 	}
513 
514 	*conn = mic_ctlr->conn;
515 	return 0;
516 }
517 
bt_micp_mic_ctlr_mute_get(struct bt_micp_mic_ctlr * mic_ctlr)518 int bt_micp_mic_ctlr_mute_get(struct bt_micp_mic_ctlr *mic_ctlr)
519 {
520 	int err;
521 
522 	CHECKIF(mic_ctlr == NULL) {
523 		LOG_DBG("NULL mic_ctlr");
524 		return -EINVAL;
525 	}
526 
527 	if (mic_ctlr->mute_handle == 0) {
528 		LOG_DBG("Handle not set");
529 		return -EINVAL;
530 	} else if (mic_ctlr->busy) {
531 		return -EBUSY;
532 	}
533 
534 	mic_ctlr->read_params.func = micp_mic_ctlr_read_mute_cb;
535 	mic_ctlr->read_params.handle_count = 1;
536 	mic_ctlr->read_params.single.handle = mic_ctlr->mute_handle;
537 	mic_ctlr->read_params.single.offset = 0U;
538 
539 	err = bt_gatt_read(mic_ctlr->conn, &mic_ctlr->read_params);
540 	if (err == 0) {
541 		mic_ctlr->busy = true;
542 	}
543 
544 	return err;
545 }
546 
bt_micp_mic_ctlr_write_mute(struct bt_micp_mic_ctlr * mic_ctlr,bool mute)547 int bt_micp_mic_ctlr_write_mute(struct bt_micp_mic_ctlr *mic_ctlr, bool mute)
548 {
549 	int err;
550 
551 	CHECKIF(mic_ctlr == NULL) {
552 		LOG_DBG("NULL mic_ctlr");
553 		return -EINVAL;
554 	}
555 
556 	if (mic_ctlr->mute_handle == 0) {
557 		LOG_DBG("Handle not set");
558 		return -EINVAL;
559 	} else if (mic_ctlr->busy) {
560 		return -EBUSY;
561 	}
562 
563 	mic_ctlr->mute_val_buf[0] = mute;
564 	mic_ctlr->write_params.offset = 0;
565 	mic_ctlr->write_params.data = mic_ctlr->mute_val_buf;
566 	mic_ctlr->write_params.length = sizeof(mute);
567 	mic_ctlr->write_params.handle = mic_ctlr->mute_handle;
568 	mic_ctlr->write_params.func = micp_mic_ctlr_write_mics_mute_cb;
569 
570 	err = bt_gatt_write(mic_ctlr->conn, &mic_ctlr->write_params);
571 	if (err == 0) {
572 		mic_ctlr->busy = true;
573 	}
574 
575 	return err;
576 }
577 
bt_micp_mic_ctlr_mute(struct bt_micp_mic_ctlr * mic_ctlr)578 int bt_micp_mic_ctlr_mute(struct bt_micp_mic_ctlr *mic_ctlr)
579 {
580 	return bt_micp_mic_ctlr_write_mute(mic_ctlr, true);
581 }
582 
bt_micp_mic_ctlr_unmute(struct bt_micp_mic_ctlr * mic_ctlr)583 int bt_micp_mic_ctlr_unmute(struct bt_micp_mic_ctlr *mic_ctlr)
584 {
585 	return bt_micp_mic_ctlr_write_mute(mic_ctlr, false);
586 }
587