1 /*
2  * Copyright 2024 NXP
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <errno.h>
9 #include <zephyr/sys/atomic.h>
10 #include <zephyr/sys/byteorder.h>
11 #include <zephyr/sys/util.h>
12 #include <zephyr/sys/printk.h>
13 
14 #include <zephyr/bluetooth/conn.h>
15 
16 #include "common/assert.h"
17 
18 #include <zephyr/bluetooth/classic/rfcomm.h>
19 #include <zephyr/bluetooth/classic/hfp_ag.h>
20 
21 #include "host/hci_core.h"
22 #include "host/conn_internal.h"
23 #include "l2cap_br_internal.h"
24 #include "rfcomm_internal.h"
25 #include "at.h"
26 #include "sco_internal.h"
27 
28 #include "hfp_internal.h"
29 #include "hfp_ag_internal.h"
30 
31 #define LOG_LEVEL CONFIG_BT_HFP_AG_LOG_LEVEL
32 #include <zephyr/logging/log.h>
33 LOG_MODULE_REGISTER(bt_hfp_ag);
34 
35 typedef int (*bt_hfp_ag_parse_command_t)(struct bt_hfp_ag *ag, struct net_buf *buf);
36 
37 struct bt_hfp_ag_at_cmd_handler {
38 	const char *cmd;
39 	bt_hfp_ag_parse_command_t handler;
40 };
41 
42 static const struct {
43 	const char *name;
44 	const char *connector;
45 	uint32_t min;
46 	uint32_t max;
47 } ag_ind[] = {
48 	{"service", "-", 0, 1},   /* BT_HFP_AG_SERVICE_IND */
49 	{"call", ",", 0, 1},      /* BT_HFP_AG_CALL_IND */
50 	{"callsetup", "-", 0, 3}, /* BT_HFP_AG_CALL_SETUP_IND */
51 	{"callheld", "-", 0, 2},  /* BT_HFP_AG_CALL_HELD_IND */
52 	{"signal", "-", 0, 5},    /* BT_HFP_AG_SIGNAL_IND */
53 	{"roam", ",", 0, 1},      /* BT_HFP_AG_ROAM_IND */
54 	{"battchg", "-", 0, 5}    /* BT_HFP_AG_BATTERY_IND */
55 };
56 
57 typedef void (*bt_hfp_ag_tx_cb_t)(struct bt_hfp_ag *ag, void *user_data);
58 
59 struct bt_ag_tx {
60 	sys_snode_t node;
61 
62 	struct bt_hfp_ag *ag;
63 	struct net_buf *buf;
64 	bt_hfp_ag_tx_cb_t cb;
65 	void *user_data;
66 	int err;
67 };
68 
69 NET_BUF_POOL_FIXED_DEFINE(ag_pool, CONFIG_BT_HFP_AG_TX_BUF_COUNT,
70 			  BT_RFCOMM_BUF_SIZE(BT_HF_CLIENT_MAX_PDU),
71 			  CONFIG_BT_CONN_TX_USER_DATA_SIZE, NULL);
72 
73 static struct bt_hfp_ag bt_hfp_ag_pool[CONFIG_BT_MAX_CONN];
74 
75 static struct bt_hfp_ag_cb *bt_ag;
76 
77 /* Sent but not acknowledged TX packets with a callback */
78 static struct bt_ag_tx ag_tx[CONFIG_BT_HFP_AG_TX_BUF_COUNT * 2];
79 static K_FIFO_DEFINE(ag_tx_free);
80 static K_FIFO_DEFINE(ag_tx_notify);
81 
82 struct k_thread ag_thread;
83 static K_KERNEL_STACK_MEMBER(ag_thread_stack, CONFIG_BT_HFP_AG_THREAD_STACK_SIZE);
84 static k_tid_t ag_thread_id;
85 
bt_hfp_ag_get_cme_err(int err)86 static enum at_cme bt_hfp_ag_get_cme_err(int err)
87 {
88 	enum at_cme cme_err;
89 
90 	switch (err) {
91 	case -EOPNOTSUPP:
92 		cme_err = CME_ERROR_OPERATION_NOT_SUPPORTED;
93 		break;
94 	case -EFAULT:
95 		cme_err = CME_ERROR_AG_FAILURE;
96 		break;
97 	case -ENOSR:
98 		cme_err = CME_ERROR_MEMORY_FAILURE;
99 		break;
100 	case -ENOMEM:
101 		__fallthrough;
102 	case -ENOBUFS:
103 		cme_err = CME_ERROR_MEMORY_FULL;
104 		break;
105 	case -ENAMETOOLONG:
106 		cme_err = CME_ERROR_DIAL_STRING_TO_LONG;
107 		break;
108 	case -EINVAL:
109 		cme_err = CME_ERROR_INVALID_INDEX;
110 		break;
111 	case -ENOTSUP:
112 		cme_err = CME_ERROR_OPERATION_NOT_ALLOWED;
113 		break;
114 	case -ENOTCONN:
115 		cme_err = CME_ERROR_NO_CONNECTION_TO_PHONE;
116 		break;
117 	default:
118 		cme_err = CME_ERROR_AG_FAILURE;
119 		break;
120 	}
121 
122 	return cme_err;
123 }
124 
hfp_ag_lock(struct bt_hfp_ag * ag)125 static void hfp_ag_lock(struct bt_hfp_ag *ag)
126 {
127 	k_sem_take(&ag->lock, K_FOREVER);
128 }
129 
hfp_ag_unlock(struct bt_hfp_ag * ag)130 static void hfp_ag_unlock(struct bt_hfp_ag *ag)
131 {
132 	k_sem_give(&ag->lock);
133 }
134 
bt_hfp_ag_set_state(struct bt_hfp_ag * ag,bt_hfp_state_t state)135 static void bt_hfp_ag_set_state(struct bt_hfp_ag *ag, bt_hfp_state_t state)
136 {
137 	LOG_DBG("update state %p, old %d -> new %d", ag, ag->state, state);
138 
139 	hfp_ag_lock(ag);
140 	ag->state = state;
141 	hfp_ag_unlock(ag);
142 
143 	switch (state) {
144 	case BT_HFP_DISCONNECTED:
145 		if (bt_ag && bt_ag->disconnected) {
146 			bt_ag->disconnected(ag);
147 		}
148 		break;
149 	case BT_HFP_CONNECTING:
150 		break;
151 	case BT_HFP_CONFIG:
152 		break;
153 	case BT_HFP_CONNECTED:
154 		if (bt_ag && bt_ag->connected) {
155 			bt_ag->connected(ag);
156 		}
157 		break;
158 	case BT_HFP_DISCONNECTING:
159 		break;
160 	default:
161 		LOG_WRN("no valid (%u) state was set", state);
162 		break;
163 	}
164 }
165 
bt_hfp_ag_set_call_state(struct bt_hfp_ag * ag,bt_hfp_call_state_t call_state)166 static void bt_hfp_ag_set_call_state(struct bt_hfp_ag *ag, bt_hfp_call_state_t call_state)
167 {
168 	bt_hfp_state_t state;
169 
170 	LOG_DBG("update call state %p, old %d -> new %d", ag, ag->call_state, call_state);
171 
172 	hfp_ag_lock(ag);
173 	ag->call_state = call_state;
174 	state = ag->state;
175 	hfp_ag_unlock(ag);
176 
177 	switch (call_state) {
178 	case BT_HFP_CALL_TERMINATE:
179 		atomic_clear(ag->flags);
180 		k_work_cancel_delayable(&ag->deferred_work);
181 		break;
182 	case BT_HFP_CALL_OUTGOING:
183 		k_work_reschedule(&ag->deferred_work, K_SECONDS(CONFIG_BT_HFP_AG_OUTGOING_TIMEOUT));
184 		break;
185 	case BT_HFP_CALL_INCOMING:
186 		k_work_reschedule(&ag->deferred_work, K_SECONDS(CONFIG_BT_HFP_AG_INCOMING_TIMEOUT));
187 		break;
188 	case BT_HFP_CALL_ALERTING:
189 		k_work_reschedule(&ag->ringing_work, K_NO_WAIT);
190 		k_work_reschedule(&ag->deferred_work, K_SECONDS(CONFIG_BT_HFP_AG_ALERTING_TIMEOUT));
191 		break;
192 	case BT_HFP_CALL_ACTIVE:
193 		k_work_cancel_delayable(&ag->ringing_work);
194 		k_work_cancel_delayable(&ag->deferred_work);
195 		break;
196 	case BT_HFP_CALL_HOLD:
197 		break;
198 	default:
199 		/* Invalid state */
200 		break;
201 	}
202 
203 	if (state == BT_HFP_DISCONNECTING) {
204 		int err = bt_rfcomm_dlc_disconnect(&ag->rfcomm_dlc);
205 
206 		if (err) {
207 			LOG_ERR("Fail to disconnect DLC %p", &ag->rfcomm_dlc);
208 		}
209 	}
210 }
211 
bt_ag_tx_alloc(void)212 static struct bt_ag_tx *bt_ag_tx_alloc(void)
213 {
214 	/* The TX context always get freed in the system workqueue,
215 	 * so if we're in the same workqueue but there are no immediate
216 	 * contexts available, there's no chance we'll get one by waiting.
217 	 */
218 	if (k_current_get() == &k_sys_work_q.thread) {
219 		return k_fifo_get(&ag_tx_free, K_NO_WAIT);
220 	}
221 
222 	if (IS_ENABLED(CONFIG_BT_HFP_AG_LOG_LEVEL_DBG)) {
223 		struct bt_ag_tx *tx = k_fifo_get(&ag_tx_free, K_NO_WAIT);
224 
225 		if (tx) {
226 			return tx;
227 		}
228 
229 		LOG_WRN("Unable to get an immediate free bt_ag_tx");
230 	}
231 
232 	return k_fifo_get(&ag_tx_free, K_FOREVER);
233 }
234 
bt_ag_tx_free(struct bt_ag_tx * tx)235 static void bt_ag_tx_free(struct bt_ag_tx *tx)
236 {
237 	LOG_DBG("Free tx buffer %p", tx);
238 
239 	(void)memset(tx, 0, sizeof(*tx));
240 
241 	k_fifo_put(&ag_tx_free, tx);
242 }
243 
hfp_ag_next_step(struct bt_hfp_ag * ag,bt_hfp_ag_tx_cb_t cb,void * user_data)244 static int hfp_ag_next_step(struct bt_hfp_ag *ag, bt_hfp_ag_tx_cb_t cb, void *user_data)
245 {
246 	struct bt_ag_tx *tx;
247 
248 	LOG_DBG("cb %p user_data %p", cb, user_data);
249 
250 	tx = bt_ag_tx_alloc();
251 	if (tx == NULL) {
252 		LOG_ERR("No tx buffers!");
253 		return -ENOMEM;
254 	}
255 
256 	LOG_DBG("Alloc tx buffer %p", tx);
257 
258 	tx->ag = ag;
259 	tx->buf = NULL;
260 	tx->cb = cb;
261 	tx->user_data = user_data;
262 	tx->err = 0;
263 
264 	k_fifo_put(&ag_tx_notify, tx);
265 
266 	return 0;
267 }
268 
hfp_ag_send(struct bt_hfp_ag * ag,struct bt_ag_tx * tx)269 static int hfp_ag_send(struct bt_hfp_ag *ag, struct bt_ag_tx *tx)
270 {
271 	int err = bt_rfcomm_dlc_send(&ag->rfcomm_dlc, tx->buf);
272 
273 	if (err < 0) {
274 		net_buf_unref(tx->buf);
275 	}
276 
277 	return err;
278 }
279 
bt_ag_tx_work(struct k_work * work)280 static void bt_ag_tx_work(struct k_work *work)
281 {
282 	struct k_work_delayable *dwork = k_work_delayable_from_work(work);
283 	struct bt_hfp_ag *ag = CONTAINER_OF(dwork, struct bt_hfp_ag, tx_work);
284 	sys_snode_t *node;
285 	struct bt_ag_tx *tx;
286 	bt_hfp_state_t state;
287 
288 	hfp_ag_lock(ag);
289 	state = ag->state;
290 	if ((state == BT_HFP_DISCONNECTED) || (state == BT_HFP_DISCONNECTING)) {
291 		LOG_ERR("AG %p is not connected", ag);
292 		goto unlock;
293 	}
294 
295 	node = sys_slist_peek_head(&ag->tx_pending);
296 	if (!node) {
297 		LOG_DBG("No pending tx");
298 		goto unlock;
299 	}
300 
301 	tx = CONTAINER_OF(node, struct bt_ag_tx, node);
302 
303 	if (!atomic_test_and_set_bit(ag->flags, BT_HFP_AG_TX_ONGOING)) {
304 		LOG_DBG("AG %p sending tx %p", ag, tx);
305 		int err = hfp_ag_send(ag, tx);
306 
307 		if (err < 0) {
308 			LOG_ERR("Rfcomm send error :(%d)", err);
309 			sys_slist_find_and_remove(&ag->tx_pending, &tx->node);
310 			tx->err = err;
311 			k_fifo_put(&ag_tx_notify, tx);
312 			/* Clear the tx ongoing flag */
313 			if (!atomic_test_and_clear_bit(ag->flags, BT_HFP_AG_TX_ONGOING)) {
314 				LOG_WRN("tx ongoing flag is not set");
315 			}
316 			/* Due to the work is failed, restart the tx work */
317 			k_work_reschedule(&ag->tx_work, K_NO_WAIT);
318 		}
319 	}
320 
321 unlock:
322 	hfp_ag_unlock(ag);
323 }
324 
hfp_ag_send_data(struct bt_hfp_ag * ag,bt_hfp_ag_tx_cb_t cb,void * user_data,const char * format,...)325 static int hfp_ag_send_data(struct bt_hfp_ag *ag, bt_hfp_ag_tx_cb_t cb, void *user_data,
326 			    const char *format, ...)
327 {
328 	struct net_buf *buf;
329 	struct bt_ag_tx *tx;
330 	va_list vargs;
331 	int err;
332 	bt_hfp_state_t state;
333 
334 	LOG_DBG("AG %p sending data cb %p user_data %p", ag, cb, user_data);
335 
336 	hfp_ag_lock(ag);
337 	state = ag->state;
338 	hfp_ag_unlock(ag);
339 	if ((state == BT_HFP_DISCONNECTED) || (state == BT_HFP_DISCONNECTING)) {
340 		LOG_ERR("AG %p is not connected", ag);
341 		return -ENOTCONN;
342 	}
343 
344 	buf = bt_rfcomm_create_pdu(&ag_pool);
345 	if (!buf) {
346 		LOG_ERR("No Buffers!");
347 		return -ENOMEM;
348 	}
349 
350 	tx = bt_ag_tx_alloc();
351 	if (tx == NULL) {
352 		LOG_ERR("No tx buffers!");
353 		net_buf_unref(buf);
354 		return -ENOMEM;
355 	}
356 
357 	LOG_DBG("buf %p tx %p", buf, tx);
358 
359 	tx->ag = ag;
360 	tx->buf = buf;
361 	tx->cb = cb;
362 	tx->user_data = user_data;
363 
364 	va_start(vargs, format);
365 	err = vsnprintk((char *)buf->data, (net_buf_tailroom(buf) - 1), format, vargs);
366 	va_end(vargs);
367 
368 	if (err < 0) {
369 		LOG_ERR("Unable to format variable arguments");
370 		net_buf_unref(buf);
371 		bt_ag_tx_free(tx);
372 		return err;
373 	}
374 
375 	net_buf_add(buf, err);
376 
377 	LOG_HEXDUMP_DBG(buf->data, buf->len, "Sending:");
378 
379 	hfp_ag_lock(ag);
380 	sys_slist_append(&ag->tx_pending, &tx->node);
381 	hfp_ag_unlock(ag);
382 
383 	/* Always active tx work */
384 	k_work_reschedule(&ag->tx_work, K_NO_WAIT);
385 
386 	return 0;
387 }
388 
skip_space(struct net_buf * buf)389 static void skip_space(struct net_buf *buf)
390 {
391 	while ((buf->len > 0) && (buf->data[0] == ' ')) {
392 		(void)net_buf_pull(buf, 1);
393 	}
394 }
395 
get_number(struct net_buf * buf,uint32_t * number)396 static int get_number(struct net_buf *buf, uint32_t *number)
397 {
398 	int err = -EINVAL;
399 
400 	*number = 0;
401 
402 	skip_space(buf);
403 	while (buf->len > 0) {
404 		if ((buf->data[0] >= '0') && (buf->data[0] <= '9')) {
405 			*number = *number * 10 + buf->data[0] - '0';
406 			(void)net_buf_pull(buf, 1);
407 			err = 0;
408 		} else {
409 			break;
410 		}
411 	}
412 	skip_space(buf);
413 
414 	return err;
415 }
416 
is_char(struct net_buf * buf,uint8_t c)417 static bool is_char(struct net_buf *buf, uint8_t c)
418 {
419 	bool found = false;
420 
421 	skip_space(buf);
422 	if (buf->len > 0) {
423 		if (buf->data[0] == c) {
424 			(void)net_buf_pull(buf, 1);
425 			found = true;
426 		}
427 	}
428 	skip_space(buf);
429 
430 	return found;
431 }
432 
bt_hfp_ag_brsf_handler(struct bt_hfp_ag * ag,struct net_buf * buf)433 static int bt_hfp_ag_brsf_handler(struct bt_hfp_ag *ag, struct net_buf *buf)
434 {
435 	uint32_t hf_features;
436 	int err;
437 
438 	if (!is_char(buf, '=')) {
439 		return -ENOTSUP;
440 	}
441 
442 	err = get_number(buf, &hf_features);
443 	if (err != 0) {
444 		return -ENOTSUP;
445 	}
446 
447 	if (!is_char(buf, '\r')) {
448 		return -ENOTSUP;
449 	}
450 
451 	hfp_ag_lock(ag);
452 	ag->hf_features = hf_features;
453 	hfp_ag_unlock(ag);
454 
455 	return hfp_ag_send_data(ag, NULL, NULL, "\r\n+BRSF:%d\r\n", ag->ag_features);
456 }
457 
bt_hfp_ag_bac_handler(struct bt_hfp_ag * ag,struct net_buf * buf)458 static int bt_hfp_ag_bac_handler(struct bt_hfp_ag *ag, struct net_buf *buf)
459 {
460 	uint32_t codec;
461 	uint32_t codec_ids = 0U;
462 	int err = 0;
463 
464 	if (!is_char(buf, '=')) {
465 		return -ENOTSUP;
466 	}
467 
468 	hfp_ag_lock(ag);
469 	if (!(ag->hf_features & BT_HFP_HF_FEATURE_CODEC_NEG) ||
470 	    !(ag->ag_features & BT_HFP_AG_FEATURE_CODEC_NEG)) {
471 		hfp_ag_unlock(ag);
472 		return -EOPNOTSUPP;
473 	}
474 	hfp_ag_unlock(ag);
475 
476 	while (buf->len > 0) {
477 		err = get_number(buf, &codec);
478 		if (err != 0) {
479 			return -ENOTSUP;
480 		}
481 
482 		if (!is_char(buf, ',')) {
483 			if (!is_char(buf, '\r')) {
484 				return -ENOTSUP;
485 			}
486 		}
487 
488 		if (codec < (sizeof(codec_ids) * 8)) {
489 			codec_ids |= BIT(codec);
490 		}
491 	}
492 
493 	hfp_ag_lock(ag);
494 	ag->hf_codec_ids = codec_ids;
495 	hfp_ag_unlock(ag);
496 
497 	if (bt_ag && bt_ag->codec) {
498 		bt_ag->codec(ag, ag->hf_codec_ids);
499 	}
500 
501 	return 0;
502 }
503 
bt_hfp_ag_cind_handler(struct bt_hfp_ag * ag,struct net_buf * buf)504 static int bt_hfp_ag_cind_handler(struct bt_hfp_ag *ag, struct net_buf *buf)
505 {
506 	int err;
507 	bool inquiry_status = true;
508 
509 	if (is_char(buf, '=')) {
510 		inquiry_status = false;
511 	}
512 
513 	if (!is_char(buf, '?')) {
514 		return -ENOTSUP;
515 	}
516 
517 	if (!is_char(buf, '\r')) {
518 		return -ENOTSUP;
519 	}
520 
521 	if (!inquiry_status) {
522 		err = hfp_ag_send_data(
523 			ag, NULL, NULL,
524 			"\r\n+CIND:(\"%s\",(%d%s%d)),(\"%s\",(%d%s%d)),(\"%s\",(%d%s%d)),(\"%s\",(%"
525 			"d%s%d)),(\"%s\",(%d%s%d)),(\"%s\",(%d%s%d)),(\"%s\",(%d%s%d))\r\n",
526 			ag_ind[BT_HFP_AG_SERVICE_IND].name, ag_ind[BT_HFP_AG_SERVICE_IND].min,
527 			ag_ind[BT_HFP_AG_SERVICE_IND].connector, ag_ind[BT_HFP_AG_SERVICE_IND].max,
528 			ag_ind[BT_HFP_AG_CALL_IND].name, ag_ind[BT_HFP_AG_CALL_IND].min,
529 			ag_ind[BT_HFP_AG_CALL_IND].connector, ag_ind[BT_HFP_AG_CALL_IND].max,
530 			ag_ind[BT_HFP_AG_CALL_SETUP_IND].name, ag_ind[BT_HFP_AG_CALL_SETUP_IND].min,
531 			ag_ind[BT_HFP_AG_CALL_SETUP_IND].connector,
532 			ag_ind[BT_HFP_AG_CALL_SETUP_IND].max, ag_ind[BT_HFP_AG_CALL_HELD_IND].name,
533 			ag_ind[BT_HFP_AG_CALL_HELD_IND].min,
534 			ag_ind[BT_HFP_AG_CALL_HELD_IND].connector,
535 			ag_ind[BT_HFP_AG_CALL_HELD_IND].max, ag_ind[BT_HFP_AG_SIGNAL_IND].name,
536 			ag_ind[BT_HFP_AG_SIGNAL_IND].min, ag_ind[BT_HFP_AG_SIGNAL_IND].connector,
537 			ag_ind[BT_HFP_AG_SIGNAL_IND].max, ag_ind[BT_HFP_AG_ROAM_IND].name,
538 			ag_ind[BT_HFP_AG_ROAM_IND].min, ag_ind[BT_HFP_AG_ROAM_IND].connector,
539 			ag_ind[BT_HFP_AG_ROAM_IND].max, ag_ind[BT_HFP_AG_BATTERY_IND].name,
540 			ag_ind[BT_HFP_AG_BATTERY_IND].min, ag_ind[BT_HFP_AG_BATTERY_IND].connector,
541 			ag_ind[BT_HFP_AG_BATTERY_IND].max);
542 	} else {
543 		err = hfp_ag_send_data(ag, NULL, NULL, "\r\n+CIND:%d,%d,%d,%d,%d,%d,%d\r\n",
544 					 ag->indicator_value[BT_HFP_AG_SERVICE_IND],
545 					 ag->indicator_value[BT_HFP_AG_CALL_IND],
546 					 ag->indicator_value[BT_HFP_AG_CALL_SETUP_IND],
547 					 ag->indicator_value[BT_HFP_AG_CALL_HELD_IND],
548 					 ag->indicator_value[BT_HFP_AG_SIGNAL_IND],
549 					 ag->indicator_value[BT_HFP_AG_ROAM_IND],
550 					 ag->indicator_value[BT_HFP_AG_BATTERY_IND]);
551 	}
552 
553 	return err;
554 }
555 
bt_hfp_ag_set_in_band_ring(struct bt_hfp_ag * ag,void * user_data)556 static void bt_hfp_ag_set_in_band_ring(struct bt_hfp_ag *ag, void *user_data)
557 {
558 	bool is_inband_ringtone;
559 
560 	hfp_ag_lock(ag);
561 	is_inband_ringtone = (ag->ag_features & BT_HFP_AG_FEATURE_INBAND_RINGTONE) ? true : false;
562 	hfp_ag_unlock(ag);
563 
564 	if (is_inband_ringtone) {
565 		int err = hfp_ag_send_data(ag, NULL, NULL, "\r\n+BSIR:1\r\n");
566 
567 		atomic_set_bit_to(ag->flags, BT_HFP_AG_INBAND_RING, err == 0);
568 	}
569 }
570 
bt_hfp_ag_cmer_handler(struct bt_hfp_ag * ag,struct net_buf * buf)571 static int bt_hfp_ag_cmer_handler(struct bt_hfp_ag *ag, struct net_buf *buf)
572 {
573 	uint32_t number;
574 	int err;
575 	static const uint32_t command_line_prefix[] = {3, 0, 0};
576 
577 	if (!is_char(buf, '=')) {
578 		return -ENOTSUP;
579 	}
580 
581 	for (int i = 0; i < ARRAY_SIZE(command_line_prefix); i++) {
582 		err = get_number(buf, &number);
583 		if (err != 0) {
584 			return -ENOTSUP;
585 		}
586 
587 		if (!is_char(buf, ',')) {
588 			return -ENOTSUP;
589 		}
590 
591 		if (command_line_prefix[i] != number) {
592 			return -ENOTSUP;
593 		}
594 	}
595 
596 	err = get_number(buf, &number);
597 	if (err != 0) {
598 		return -ENOTSUP;
599 	}
600 
601 	if (!is_char(buf, '\r')) {
602 		return -ENOTSUP;
603 	}
604 
605 	if (number == 1) {
606 		atomic_set_bit(ag->flags, BT_HFP_AG_CMER_ENABLE);
607 		bt_hfp_ag_set_state(ag, BT_HFP_CONNECTED);
608 		err = hfp_ag_next_step(ag, bt_hfp_ag_set_in_band_ring, NULL);
609 		return err;
610 	} else if (number == 0) {
611 		atomic_clear_bit(ag->flags, BT_HFP_AG_CMER_ENABLE);
612 	} else {
613 		return -ENOTSUP;
614 	}
615 
616 	return 0;
617 }
618 
bt_hfp_ag_chld_handler(struct bt_hfp_ag * ag,struct net_buf * buf)619 static int bt_hfp_ag_chld_handler(struct bt_hfp_ag *ag, struct net_buf *buf)
620 {
621 	return -EOPNOTSUPP;
622 }
623 
bt_hfp_ag_bind_handler(struct bt_hfp_ag * ag,struct net_buf * buf)624 static int bt_hfp_ag_bind_handler(struct bt_hfp_ag *ag, struct net_buf *buf)
625 {
626 	uint32_t indicator;
627 	uint32_t hf_indicators = 0U;
628 	int err;
629 	char *data;
630 	uint32_t len;
631 
632 	hfp_ag_lock(ag);
633 	if (!((ag->ag_features & BT_HFP_AG_FEATURE_HF_IND) &&
634 	      (ag->hf_features & BT_HFP_HF_FEATURE_HF_IND))) {
635 		hfp_ag_unlock(ag);
636 		return -EOPNOTSUPP;
637 	}
638 	hfp_ag_unlock(ag);
639 
640 	if (is_char(buf, '?')) {
641 		if (!is_char(buf, '\r')) {
642 			return -ENOTSUP;
643 		}
644 
645 		hfp_ag_lock(ag);
646 		hf_indicators = ag->hf_indicators_of_hf & ag->hf_indicators_of_ag;
647 		hfp_ag_unlock(ag);
648 		len = (sizeof(hf_indicators) * 8) > HFP_HF_IND_MAX ? HFP_HF_IND_MAX
649 								   : (sizeof(hf_indicators) * 8);
650 		for (int i = 1; i < len; i++) {
651 			if (BIT(i) & hf_indicators) {
652 				err = hfp_ag_send_data(ag, NULL, NULL, "\r\n+BIND:%d,%d\r\n", i, 1);
653 			} else {
654 				err = hfp_ag_send_data(ag, NULL, NULL, "\r\n+BIND:%d,%d\r\n", i, 0);
655 			}
656 			if (err < 0) {
657 				return err;
658 			}
659 			if (hf_indicators == 0) {
660 				break;
661 			}
662 		}
663 		return 0;
664 	}
665 
666 	if (!is_char(buf, '=')) {
667 		return -ENOTSUP;
668 	}
669 
670 	if (is_char(buf, '?')) {
671 		if (!is_char(buf, '\r')) {
672 			return -ENOTSUP;
673 		}
674 
675 		data = &ag->buffer[0];
676 		*data = '(';
677 		data++;
678 		hfp_ag_lock(ag);
679 		hf_indicators = ag->hf_indicators_of_ag;
680 		hfp_ag_unlock(ag);
681 		len = (sizeof(hf_indicators) * 8) > HFP_HF_IND_MAX ? HFP_HF_IND_MAX
682 								   : (sizeof(hf_indicators) * 8);
683 		for (int i = 1; (i < len) && (hf_indicators != 0); i++) {
684 			if (BIT(i) & hf_indicators) {
685 				int length = snprintk(
686 					data, (char *)&ag->buffer[HF_MAX_BUF_LEN - 1] - data - 3,
687 					"%d", i);
688 				data += length;
689 				hf_indicators &= ~BIT(i);
690 			}
691 			if (hf_indicators != 0) {
692 				*data = ',';
693 				data++;
694 			}
695 		}
696 		*data = ')';
697 		data++;
698 		*data = '\r';
699 		data++;
700 		*data = '\0';
701 		data++;
702 
703 		err = hfp_ag_send_data(ag, NULL, NULL, "\r\n+BIND:%s\r\n", &ag->buffer[0]);
704 		return err;
705 	}
706 
707 	while (buf->len > 0) {
708 		err = get_number(buf, &indicator);
709 		if (err != 0) {
710 			return -ENOTSUP;
711 		}
712 
713 		if (!is_char(buf, ',')) {
714 			if (!is_char(buf, '\r')) {
715 				return -ENOTSUP;
716 			}
717 		}
718 
719 		if (indicator < (sizeof(hf_indicators) * 8)) {
720 			hf_indicators |= BIT(indicator);
721 		}
722 	}
723 
724 	hfp_ag_lock(ag);
725 	ag->hf_indicators_of_hf = hf_indicators;
726 	hfp_ag_unlock(ag);
727 
728 	return 0;
729 }
730 
bt_hfp_ag_cmee_handler(struct bt_hfp_ag * ag,struct net_buf * buf)731 static int bt_hfp_ag_cmee_handler(struct bt_hfp_ag *ag, struct net_buf *buf)
732 {
733 	uint32_t cmee;
734 	int err;
735 
736 	if (!is_char(buf, '=')) {
737 		return -ENOTSUP;
738 	}
739 
740 	err = get_number(buf, &cmee);
741 	if (err != 0) {
742 		return -ENOTSUP;
743 	}
744 
745 	if (!is_char(buf, '\r')) {
746 		return -ENOTSUP;
747 	}
748 
749 	if (cmee > 1) {
750 		return -ENOTSUP;
751 	}
752 
753 	atomic_set_bit_to(ag->flags, BT_HFP_AG_CMEE_ENABLE, cmee == 1);
754 
755 	return 0;
756 }
757 
hfp_ag_update_indicator(struct bt_hfp_ag * ag,enum bt_hfp_ag_indicator index,uint8_t value,bt_hfp_ag_tx_cb_t cb,void * user_data)758 static int hfp_ag_update_indicator(struct bt_hfp_ag *ag, enum bt_hfp_ag_indicator index,
759 				   uint8_t value, bt_hfp_ag_tx_cb_t cb, void *user_data)
760 {
761 	int err;
762 	uint8_t old_value;
763 
764 	hfp_ag_lock(ag);
765 	old_value = ag->indicator_value[index];
766 	if (value == old_value) {
767 		LOG_ERR("Duplicate value setting, indicator %d, old %d -> new %d", index, old_value,
768 			value);
769 		hfp_ag_unlock(ag);
770 		return -EINVAL;
771 	}
772 
773 	ag->indicator_value[index] = value;
774 	hfp_ag_unlock(ag);
775 
776 	LOG_DBG("indicator %d, old %d -> new %d", index, old_value, value);
777 
778 	err = hfp_ag_send_data(ag, cb, user_data, "\r\n+CIEV:%d,%d\r\n", (uint8_t)index + 1, value);
779 	if (err) {
780 		hfp_ag_lock(ag);
781 		ag->indicator_value[index] = old_value;
782 		hfp_ag_unlock(ag);
783 		LOG_ERR("Fail to update indicator %d, current %d", index, old_value);
784 	}
785 
786 	return err;
787 }
788 
hfp_ag_close_sco(struct bt_hfp_ag * ag)789 static void hfp_ag_close_sco(struct bt_hfp_ag *ag)
790 {
791 	struct bt_conn *sco;
792 
793 	LOG_DBG("");
794 
795 	hfp_ag_lock(ag);
796 	sco = ag->sco_chan.sco;
797 	ag->sco_chan.sco = NULL;
798 	hfp_ag_unlock(ag);
799 	if (sco != NULL) {
800 		LOG_DBG("Disconnect sco %p", sco);
801 		bt_conn_disconnect(sco, BT_HCI_ERR_LOCALHOST_TERM_CONN);
802 	}
803 }
804 
bt_hfp_ag_reject_cb(struct bt_hfp_ag * ag,void * user_data)805 static void bt_hfp_ag_reject_cb(struct bt_hfp_ag *ag, void *user_data)
806 {
807 	hfp_ag_close_sco(ag);
808 	bt_hfp_ag_set_call_state(ag, BT_HFP_CALL_TERMINATE);
809 
810 	if (bt_ag && bt_ag->reject) {
811 		bt_ag->reject(ag);
812 	}
813 }
814 
bt_hfp_ag_call_reject(struct bt_hfp_ag * ag,void * user_data)815 static void bt_hfp_ag_call_reject(struct bt_hfp_ag *ag, void *user_data)
816 {
817 	int err = hfp_ag_update_indicator(ag, BT_HFP_AG_CALL_SETUP_IND, BT_HFP_CALL_SETUP_NONE,
818 					    bt_hfp_ag_reject_cb, user_data);
819 	if (err != 0) {
820 		LOG_ERR("Fail to send err :(%d)", err);
821 	}
822 }
823 
bt_hfp_ag_terminate_cb(struct bt_hfp_ag * ag,void * user_data)824 static void bt_hfp_ag_terminate_cb(struct bt_hfp_ag *ag, void *user_data)
825 {
826 	hfp_ag_close_sco(ag);
827 	bt_hfp_ag_set_call_state(ag, BT_HFP_CALL_TERMINATE);
828 
829 	if (bt_ag && bt_ag->terminate) {
830 		bt_ag->terminate(ag);
831 	}
832 }
833 
bt_hfp_ag_unit_call_terminate(struct bt_hfp_ag * ag,void * user_data)834 static void bt_hfp_ag_unit_call_terminate(struct bt_hfp_ag *ag, void *user_data)
835 {
836 	int err = hfp_ag_update_indicator(ag, BT_HFP_AG_CALL_IND, 0, bt_hfp_ag_terminate_cb,
837 					    user_data);
838 	if (err != 0) {
839 		LOG_ERR("Fail to send err :(%d)", err);
840 	}
841 }
842 
bt_hfp_ag_chup_handler(struct bt_hfp_ag * ag,struct net_buf * buf)843 static int bt_hfp_ag_chup_handler(struct bt_hfp_ag *ag, struct net_buf *buf)
844 {
845 	int err;
846 	bt_hfp_call_state_t call_state;
847 
848 	if (!is_char(buf, '\r')) {
849 		return -ENOTSUP;
850 	}
851 
852 	hfp_ag_lock(ag);
853 	call_state = ag->call_state;
854 	hfp_ag_unlock(ag);
855 
856 	if (call_state == BT_HFP_CALL_ALERTING) {
857 		if (!atomic_test_bit(ag->flags, BT_HFP_AG_INCOMING_CALL)) {
858 			return -ENOTSUP;
859 		}
860 		err = hfp_ag_next_step(ag, bt_hfp_ag_call_reject, NULL);
861 	} else if ((call_state == BT_HFP_CALL_ACTIVE) || (call_state == BT_HFP_CALL_HOLD)) {
862 		err = hfp_ag_next_step(ag, bt_hfp_ag_unit_call_terminate, NULL);
863 	} else {
864 		return -ENOTSUP;
865 	}
866 	return err;
867 }
868 
bt_hfp_get_call_state(struct bt_hfp_ag * ag)869 static uint8_t bt_hfp_get_call_state(struct bt_hfp_ag *ag)
870 {
871 	uint8_t status = HFP_AG_CLCC_STATUS_INVALID;
872 
873 	hfp_ag_lock(ag);
874 	switch (ag->call_state) {
875 	case BT_HFP_CALL_TERMINATE:
876 		break;
877 	case BT_HFP_CALL_OUTGOING:
878 		status = HFP_AG_CLCC_STATUS_DIALING;
879 		break;
880 	case BT_HFP_CALL_INCOMING:
881 		status = HFP_AG_CLCC_STATUS_INCOMING;
882 		break;
883 	case BT_HFP_CALL_ALERTING:
884 		if (atomic_test_bit(ag->flags, BT_HFP_AG_INCOMING_CALL)) {
885 			status = HFP_AG_CLCC_STATUS_WAITING;
886 		} else {
887 			status = HFP_AG_CLCC_STATUS_ALERTING;
888 		}
889 		break;
890 	case BT_HFP_CALL_ACTIVE:
891 		status = HFP_AG_CLCC_STATUS_ACTIVE;
892 		break;
893 	case BT_HFP_CALL_HOLD:
894 		status = HFP_AG_CLCC_STATUS_HELD;
895 		break;
896 	default:
897 		break;
898 	}
899 	hfp_ag_unlock(ag);
900 
901 	return status;
902 }
903 
bt_hfp_ag_clcc_handler(struct bt_hfp_ag * ag,struct net_buf * buf)904 static int bt_hfp_ag_clcc_handler(struct bt_hfp_ag *ag, struct net_buf *buf)
905 {
906 	int err;
907 	uint8_t dir;
908 	uint8_t status;
909 	uint8_t mode;
910 	uint8_t mpty;
911 
912 	if (!is_char(buf, '\r')) {
913 		return -ENOTSUP;
914 	}
915 
916 	hfp_ag_lock(ag);
917 	if (ag->call_state == BT_HFP_CALL_TERMINATE) {
918 		/* AG shall always send OK response to HF */
919 		hfp_ag_unlock(ag);
920 		return 0;
921 	}
922 	hfp_ag_unlock(ag);
923 
924 	dir = atomic_test_bit(ag->flags, BT_HFP_AG_INCOMING_CALL) ? 1 : 0;
925 	status = bt_hfp_get_call_state(ag);
926 	mode = 0;
927 	mpty = 0;
928 	err = hfp_ag_send_data(ag, NULL, NULL, "\r\n+CLCC:%d,%d,%d,%d,%d\r\n", 1, dir, status, mode,
929 			       mpty);
930 	if (err != 0) {
931 		LOG_ERR("Fail to send err :(%d)", err);
932 	}
933 
934 	/* AG shall always send OK response to HF */
935 	return 0;
936 }
937 
bt_hfp_ag_bia_handler(struct bt_hfp_ag * ag,struct net_buf * buf)938 static int bt_hfp_ag_bia_handler(struct bt_hfp_ag *ag, struct net_buf *buf)
939 {
940 	uint32_t number;
941 	int err;
942 	int index = 0;
943 	uint32_t indicator;
944 
945 	if (!is_char(buf, '=')) {
946 		return -ENOTSUP;
947 	}
948 
949 	hfp_ag_lock(ag);
950 	indicator = ag->indicator;
951 	hfp_ag_unlock(ag);
952 
953 	while (buf->len > 0) {
954 		err = get_number(buf, &number);
955 		if (err == 0) {
956 			/* Valid number */
957 			if (number) {
958 				indicator |= BIT(index);
959 			} else {
960 				indicator &= ~BIT(index);
961 			}
962 		}
963 
964 		if (is_char(buf, ',')) {
965 			index++;
966 		} else {
967 			if (!is_char(buf, '\r')) {
968 				return -ENOTSUP;
969 			}
970 			if (buf->len != 0) {
971 				return -ENOTSUP;
972 			}
973 		}
974 	}
975 
976 	/* Force call, call setup and held call indicators are enabled. */
977 	indicator = BIT(BT_HFP_AG_CALL_IND) | BIT(BT_HFP_AG_CALL_SETUP_IND) |
978 		    BIT(BT_HFP_AG_CALL_HELD_IND);
979 
980 	hfp_ag_lock(ag);
981 	ag->indicator = indicator;
982 	hfp_ag_unlock(ag);
983 
984 	return 0;
985 }
986 
bt_hfp_ag_accept_cb(struct bt_hfp_ag * ag,void * user_data)987 static void bt_hfp_ag_accept_cb(struct bt_hfp_ag *ag, void *user_data)
988 {
989 	bt_hfp_ag_set_call_state(ag, BT_HFP_CALL_ACTIVE);
990 
991 	if (bt_ag && bt_ag->accept) {
992 		bt_ag->accept(ag);
993 	}
994 }
995 
bt_hfp_ag_call_ringing_cb(struct bt_hfp_ag * ag,bool in_bond)996 static void bt_hfp_ag_call_ringing_cb(struct bt_hfp_ag *ag, bool in_bond)
997 {
998 	if (bt_ag && bt_ag->ringing) {
999 		bt_ag->ringing(ag, in_bond);
1000 	}
1001 }
1002 
hfp_ag_sco_connected(struct bt_sco_chan * chan)1003 static void hfp_ag_sco_connected(struct bt_sco_chan *chan)
1004 {
1005 	struct bt_hfp_ag *ag = CONTAINER_OF(chan, struct bt_hfp_ag, sco_chan);
1006 	bt_hfp_call_state_t call_state;
1007 
1008 	hfp_ag_lock(ag);
1009 	call_state = ag->call_state;
1010 	hfp_ag_unlock(ag);
1011 	if (call_state == BT_HFP_CALL_INCOMING) {
1012 		bt_hfp_ag_set_call_state(ag, BT_HFP_CALL_ALERTING);
1013 		bt_hfp_ag_call_ringing_cb(ag, true);
1014 	}
1015 
1016 	if ((bt_ag) && bt_ag->sco_connected) {
1017 		bt_ag->sco_connected(ag, chan->sco);
1018 	}
1019 }
1020 
hfp_ag_sco_disconnected(struct bt_sco_chan * chan,uint8_t reason)1021 static void hfp_ag_sco_disconnected(struct bt_sco_chan *chan, uint8_t reason)
1022 {
1023 	struct bt_hfp_ag *ag = CONTAINER_OF(chan, struct bt_hfp_ag, sco_chan);
1024 	bt_hfp_call_state_t call_state;
1025 
1026 	if ((bt_ag) && bt_ag->sco_disconnected) {
1027 		bt_ag->sco_disconnected(ag);
1028 	}
1029 
1030 	hfp_ag_lock(ag);
1031 	call_state = ag->call_state;
1032 	hfp_ag_unlock(ag);
1033 	if ((call_state == BT_HFP_CALL_INCOMING) || (call_state == BT_HFP_CALL_OUTGOING)) {
1034 		bt_hfp_ag_call_reject(ag, NULL);
1035 	}
1036 }
1037 
bt_hfp_ag_create_sco(struct bt_hfp_ag * ag)1038 static struct bt_conn *bt_hfp_ag_create_sco(struct bt_hfp_ag *ag)
1039 {
1040 	struct bt_conn *sco_conn;
1041 
1042 	static struct bt_sco_chan_ops ops = {
1043 		.connected = hfp_ag_sco_connected,
1044 		.disconnected = hfp_ag_sco_disconnected,
1045 	};
1046 
1047 	LOG_DBG("");
1048 
1049 	if (ag->sco_chan.sco == NULL) {
1050 		ag->sco_chan.ops = &ops;
1051 
1052 		/* create SCO connection*/
1053 		sco_conn = bt_conn_create_sco(&ag->acl_conn->br.dst, &ag->sco_chan);
1054 		if (sco_conn != NULL) {
1055 			LOG_DBG("Created sco %p", sco_conn);
1056 			bt_conn_unref(sco_conn);
1057 		}
1058 	} else {
1059 		sco_conn = ag->sco_chan.sco;
1060 	}
1061 
1062 	return sco_conn;
1063 }
1064 
hfp_ag_open_sco(struct bt_hfp_ag * ag)1065 static int hfp_ag_open_sco(struct bt_hfp_ag *ag)
1066 {
1067 	bool create_sco;
1068 
1069 	if (atomic_test_bit(ag->flags, BT_HFP_AG_CREATING_SCO)) {
1070 		LOG_WRN("SCO connection is creating!");
1071 		return 0;
1072 	}
1073 
1074 	hfp_ag_lock(ag);
1075 	create_sco = (ag->sco_chan.sco == NULL) ? true : false;
1076 	if (create_sco) {
1077 		atomic_set_bit(ag->flags, BT_HFP_AG_CREATING_SCO);
1078 	}
1079 	hfp_ag_unlock(ag);
1080 
1081 	if (create_sco) {
1082 		struct bt_conn *sco_conn = bt_hfp_ag_create_sco(ag);
1083 
1084 		atomic_clear_bit(ag->flags, BT_HFP_AG_CREATING_SCO);
1085 
1086 		if (sco_conn == NULL) {
1087 			LOG_ERR("Fail to create sco connection!");
1088 			return -ENOTCONN;
1089 		}
1090 
1091 		LOG_DBG("SCO connection created (%p)", sco_conn);
1092 	}
1093 
1094 	return 0;
1095 }
1096 
bt_hfp_ag_codec_select(struct bt_hfp_ag * ag)1097 static int bt_hfp_ag_codec_select(struct bt_hfp_ag *ag)
1098 {
1099 	int err;
1100 
1101 	LOG_DBG("");
1102 
1103 	hfp_ag_lock(ag);
1104 	if (ag->selected_codec_id == 0) {
1105 		LOG_ERR("Codec is invalid");
1106 		hfp_ag_unlock(ag);
1107 		return -EINVAL;
1108 	}
1109 
1110 	if (!(ag->hf_codec_ids & BIT(ag->selected_codec_id))) {
1111 		LOG_ERR("Codec is unsupported (codec id %d)", ag->selected_codec_id);
1112 		hfp_ag_unlock(ag);
1113 		return -EINVAL;
1114 	}
1115 	hfp_ag_unlock(ag);
1116 
1117 	err = hfp_ag_send_data(ag, NULL, NULL, "\r\n+BCS:%d\r\n", ag->selected_codec_id);
1118 	if (err != 0) {
1119 		LOG_ERR("Fail to send err :(%d)", err);
1120 	}
1121 	return err;
1122 }
1123 
bt_hfp_ag_create_audio_connection(struct bt_hfp_ag * ag)1124 static int bt_hfp_ag_create_audio_connection(struct bt_hfp_ag *ag)
1125 {
1126 	int err;
1127 	uint32_t hf_codec_ids;
1128 
1129 	hfp_ag_lock(ag);
1130 	hf_codec_ids = ag->hf_codec_ids;
1131 	hfp_ag_unlock(ag);
1132 
1133 	if ((hf_codec_ids != 0) && atomic_test_bit(ag->flags, BT_HFP_AG_CODEC_CHANGED)) {
1134 		atomic_set_bit(ag->flags, BT_HFP_AG_CODEC_CONN);
1135 		err = bt_hfp_ag_codec_select(ag);
1136 	} else {
1137 		err = hfp_ag_open_sco(ag);
1138 	}
1139 
1140 	return err;
1141 }
1142 
bt_hfp_ag_audio_connection(struct bt_hfp_ag * ag,void * user_data)1143 static void bt_hfp_ag_audio_connection(struct bt_hfp_ag *ag, void *user_data)
1144 {
1145 	int err;
1146 
1147 	err = bt_hfp_ag_create_audio_connection(ag);
1148 	if (err) {
1149 		bt_hfp_ag_unit_call_terminate(ag, user_data);
1150 	}
1151 }
1152 
bt_hfp_ag_unit_call_accept(struct bt_hfp_ag * ag,void * user_data)1153 static void bt_hfp_ag_unit_call_accept(struct bt_hfp_ag *ag, void *user_data)
1154 {
1155 	int err;
1156 
1157 	err = hfp_ag_update_indicator(ag, BT_HFP_AG_CALL_IND, 1, bt_hfp_ag_accept_cb, user_data);
1158 	if (err != 0) {
1159 		LOG_ERR("Fail to send err :(%d)", err);
1160 	}
1161 
1162 	err = hfp_ag_update_indicator(ag, BT_HFP_AG_CALL_SETUP_IND, BT_HFP_CALL_SETUP_NONE,
1163 					bt_hfp_ag_audio_connection, user_data);
1164 	if (err != 0) {
1165 		LOG_ERR("Fail to send err :(%d)", err);
1166 	}
1167 }
1168 
bt_hfp_ag_ata_handler(struct bt_hfp_ag * ag,struct net_buf * buf)1169 static int bt_hfp_ag_ata_handler(struct bt_hfp_ag *ag, struct net_buf *buf)
1170 {
1171 	int err;
1172 
1173 	if (!is_char(buf, '\r')) {
1174 		return -ENOTSUP;
1175 	}
1176 
1177 	hfp_ag_lock(ag);
1178 	if (ag->call_state != BT_HFP_CALL_ALERTING) {
1179 		hfp_ag_unlock(ag);
1180 		return -ENOTSUP;
1181 	}
1182 	hfp_ag_unlock(ag);
1183 
1184 	if (!atomic_test_bit(ag->flags, BT_HFP_AG_INCOMING_CALL)) {
1185 		return -ENOTSUP;
1186 	}
1187 
1188 	err = hfp_ag_next_step(ag, bt_hfp_ag_unit_call_accept, NULL);
1189 
1190 	return err;
1191 }
1192 
bt_hfp_ag_cops_handler(struct bt_hfp_ag * ag,struct net_buf * buf)1193 static int bt_hfp_ag_cops_handler(struct bt_hfp_ag *ag, struct net_buf *buf)
1194 {
1195 	int err;
1196 	uint32_t number;
1197 
1198 	if (is_char(buf, '=')) {
1199 		static const uint32_t command_line_prefix[] = {3, 0};
1200 
1201 		for (int i = 0; i < ARRAY_SIZE(command_line_prefix); i++) {
1202 			err = get_number(buf, &number);
1203 			if (err != 0) {
1204 				return -ENOTSUP;
1205 			}
1206 
1207 			if (command_line_prefix[i] != number) {
1208 				return -ENOTSUP;
1209 			}
1210 
1211 			if (!is_char(buf, ',')) {
1212 				if (!is_char(buf, '\r')) {
1213 					return -ENOTSUP;
1214 				}
1215 			}
1216 		}
1217 
1218 		atomic_set_bit(ag->flags, BT_HFP_AG_COPS_SET);
1219 		return 0;
1220 	}
1221 
1222 	if (!atomic_test_bit(ag->flags, BT_HFP_AG_COPS_SET)) {
1223 		return -ENOTSUP;
1224 	}
1225 
1226 	if (!is_char(buf, '?')) {
1227 		return -ENOTSUP;
1228 	}
1229 
1230 	if (!is_char(buf, '\r')) {
1231 		return -ENOTSUP;
1232 	}
1233 
1234 	err = hfp_ag_send_data(ag, NULL, NULL, "\r\n+COPS:%d,%d,\"%s\"\r\n", 0, 0, ag->operator);
1235 	if (err != 0) {
1236 		LOG_ERR("Fail to send err :(%d)", err);
1237 	}
1238 
1239 	return err;
1240 }
1241 
bt_hfp_ag_bcc_handler(struct bt_hfp_ag * ag,struct net_buf * buf)1242 static int bt_hfp_ag_bcc_handler(struct bt_hfp_ag *ag, struct net_buf *buf)
1243 {
1244 	int err;
1245 
1246 	if (!is_char(buf, '\r')) {
1247 		return -ENOTSUP;
1248 	}
1249 
1250 	hfp_ag_lock(ag);
1251 	if ((ag->selected_codec_id == 0) ||
1252 		(!(ag->hf_codec_ids & BIT(ag->selected_codec_id))) ||
1253 		(ag->call_state == BT_HFP_CALL_TERMINATE) ||
1254 		(ag->sco_chan.sco != NULL)) {
1255 		hfp_ag_unlock(ag);
1256 		return -ENOTSUP;
1257 	}
1258 	hfp_ag_unlock(ag);
1259 
1260 	err = hfp_ag_next_step(ag, bt_hfp_ag_audio_connection, NULL);
1261 
1262 	return 0;
1263 }
1264 
bt_hfp_ag_unit_codec_conn_setup(struct bt_hfp_ag * ag,void * user_data)1265 static void bt_hfp_ag_unit_codec_conn_setup(struct bt_hfp_ag *ag, void *user_data)
1266 {
1267 	int err = hfp_ag_open_sco(ag);
1268 
1269 	if (err) {
1270 		bt_hfp_ag_call_reject(ag, user_data);
1271 	}
1272 }
1273 
bt_hfp_ag_bcs_handler(struct bt_hfp_ag * ag,struct net_buf * buf)1274 static int bt_hfp_ag_bcs_handler(struct bt_hfp_ag *ag, struct net_buf *buf)
1275 {
1276 	int err;
1277 	uint32_t number;
1278 
1279 	if (!is_char(buf, '=')) {
1280 		return -ENOTSUP;
1281 	}
1282 
1283 	err = get_number(buf, &number);
1284 	if (err != 0) {
1285 		return -ENOTSUP;
1286 	}
1287 
1288 	if (!is_char(buf, '\r')) {
1289 		return -ENOTSUP;
1290 	}
1291 
1292 	if (!atomic_test_bit(ag->flags, BT_HFP_AG_CODEC_CONN)) {
1293 		return -ESRCH;
1294 	}
1295 
1296 	hfp_ag_lock(ag);
1297 	if (ag->selected_codec_id != number) {
1298 		LOG_ERR("Received codec id %d is not aligned with selected %d",
1299 				number, ag->selected_codec_id);
1300 		err = -ENOTSUP;
1301 	} else if (!(ag->hf_codec_ids & BIT(ag->selected_codec_id))) {
1302 		LOG_ERR("Selected codec id %d is unsupported %d",
1303 				ag->selected_codec_id, ag->hf_codec_ids);
1304 		err = -ENOTSUP;
1305 	}
1306 	hfp_ag_unlock(ag);
1307 
1308 	atomic_clear_bit(ag->flags, BT_HFP_AG_CODEC_CONN);
1309 	atomic_clear_bit(ag->flags, BT_HFP_AG_CODEC_CHANGED);
1310 
1311 	if (err == 0) {
1312 		err = hfp_ag_next_step(ag, bt_hfp_ag_unit_codec_conn_setup, NULL);
1313 	} else {
1314 		bt_hfp_call_state_t call_state;
1315 
1316 		hfp_ag_lock(ag);
1317 		call_state = ag->call_state;
1318 		hfp_ag_unlock(ag);
1319 		if (call_state != BT_HFP_CALL_TERMINATE) {
1320 			(void)hfp_ag_next_step(ag, bt_hfp_ag_unit_call_terminate, NULL);
1321 		}
1322 	}
1323 
1324 	return err;
1325 }
1326 
bt_hfp_ag_unit_call_outgoing(struct bt_hfp_ag * ag,void * user_data)1327 static void bt_hfp_ag_unit_call_outgoing(struct bt_hfp_ag *ag, void *user_data)
1328 {
1329 	int err = bt_hfp_ag_outgoing(ag, ag->number);
1330 
1331 	if (err != 0) {
1332 		LOG_ERR("Fail to send err :(%d)", err);
1333 	}
1334 }
1335 
bt_hfp_ag_atd_handler(struct bt_hfp_ag * ag,struct net_buf * buf)1336 static int bt_hfp_ag_atd_handler(struct bt_hfp_ag *ag, struct net_buf *buf)
1337 {
1338 	int err;
1339 	char *number = NULL;
1340 	bool is_memory_dial = false;
1341 	size_t len;
1342 
1343 	if (buf->data[buf->len - 1] != '\r') {
1344 		return -ENOTSUP;
1345 	}
1346 
1347 	if (is_char(buf, '>')) {
1348 		is_memory_dial = true;
1349 	}
1350 
1351 	if ((buf->len - 1) > CONFIG_BT_HFP_AG_PHONE_NUMBER_MAX_LEN) {
1352 		return -ENAMETOOLONG;
1353 	}
1354 
1355 	buf->data[buf->len - 1] = '\0';
1356 
1357 	if (is_memory_dial) {
1358 		if (bt_ag && bt_ag->memory_dial) {
1359 			err = bt_ag->memory_dial(ag, &buf->data[0], &number);
1360 			if ((err != 0) || (number == NULL)) {
1361 				return -ENOTSUP;
1362 			}
1363 		} else {
1364 			return -ENOTSUP;
1365 		}
1366 	} else {
1367 		number = &buf->data[0];
1368 	}
1369 
1370 	len = strlen(number);
1371 	if (len == 0) {
1372 		return -ENOTSUP;
1373 	}
1374 
1375 	if (len > CONFIG_BT_HFP_AG_PHONE_NUMBER_MAX_LEN) {
1376 		return -ENAMETOOLONG;
1377 	}
1378 
1379 	hfp_ag_lock(ag);
1380 	if (ag->call_state != BT_HFP_CALL_TERMINATE) {
1381 		hfp_ag_unlock(ag);
1382 		return -EBUSY;
1383 	}
1384 
1385 	/* Copy number to ag->number including null-character */
1386 	memcpy(ag->number, number, len + 1);
1387 	hfp_ag_unlock(ag);
1388 
1389 	err = hfp_ag_next_step(ag, bt_hfp_ag_unit_call_outgoing, NULL);
1390 
1391 	return err;
1392 }
1393 
bt_hfp_ag_bldn_handler(struct bt_hfp_ag * ag,struct net_buf * buf)1394 static int bt_hfp_ag_bldn_handler(struct bt_hfp_ag *ag, struct net_buf *buf)
1395 {
1396 	int err;
1397 
1398 	if (!is_char(buf, '\r')) {
1399 		return -ENOTSUP;
1400 	}
1401 
1402 	hfp_ag_lock(ag);
1403 	if (strlen(ag->number) == 0) {
1404 		hfp_ag_unlock(ag);
1405 		return -ENOSR;
1406 	}
1407 
1408 	if (ag->call_state != BT_HFP_CALL_TERMINATE) {
1409 		hfp_ag_unlock(ag);
1410 		return -EBUSY;
1411 	}
1412 	hfp_ag_unlock(ag);
1413 
1414 	err = hfp_ag_next_step(ag, bt_hfp_ag_unit_call_outgoing, NULL);
1415 
1416 	return err;
1417 }
1418 
bt_hfp_ag_clip_handler(struct bt_hfp_ag * ag,struct net_buf * buf)1419 static int bt_hfp_ag_clip_handler(struct bt_hfp_ag *ag, struct net_buf *buf)
1420 {
1421 	int err;
1422 	uint32_t clip;
1423 
1424 	err = get_number(buf, &clip);
1425 	if (err != 0) {
1426 		return -ENOTSUP;
1427 	}
1428 
1429 	if (!is_char(buf, '\r')) {
1430 		return -ENOTSUP;
1431 	}
1432 
1433 	if (clip > 1) {
1434 		return -ENOTSUP;
1435 	}
1436 
1437 	atomic_set_bit_to(ag->flags, BT_HFP_AG_CLIP_ENABLE, clip == 1);
1438 
1439 	return err;
1440 }
1441 
1442 static struct bt_hfp_ag_at_cmd_handler cmd_handlers[] = {
1443 	{"AT+BRSF", bt_hfp_ag_brsf_handler}, {"AT+BAC", bt_hfp_ag_bac_handler},
1444 	{"AT+CIND", bt_hfp_ag_cind_handler}, {"AT+CMER", bt_hfp_ag_cmer_handler},
1445 	{"AT+CHLD", bt_hfp_ag_chld_handler}, {"AT+BIND", bt_hfp_ag_bind_handler},
1446 	{"AT+CMEE", bt_hfp_ag_cmee_handler}, {"AT+CHUP", bt_hfp_ag_chup_handler},
1447 	{"AT+CLCC", bt_hfp_ag_clcc_handler}, {"AT+BIA", bt_hfp_ag_bia_handler},
1448 	{"ATA", bt_hfp_ag_ata_handler},      {"AT+COPS", bt_hfp_ag_cops_handler},
1449 	{"AT+BCC", bt_hfp_ag_bcc_handler},   {"AT+BCS", bt_hfp_ag_bcs_handler},
1450 	{"ATD", bt_hfp_ag_atd_handler},      {"AT+BLDN", bt_hfp_ag_bldn_handler},
1451 	{"AT+CLIP", bt_hfp_ag_clip_handler},
1452 };
1453 
hfp_ag_connected(struct bt_rfcomm_dlc * dlc)1454 static void hfp_ag_connected(struct bt_rfcomm_dlc *dlc)
1455 {
1456 	struct bt_hfp_ag *ag = CONTAINER_OF(dlc, struct bt_hfp_ag, rfcomm_dlc);
1457 
1458 	bt_hfp_ag_set_state(ag, BT_HFP_CONFIG);
1459 
1460 	LOG_DBG("AG %p", ag);
1461 }
1462 
hfp_ag_disconnected(struct bt_rfcomm_dlc * dlc)1463 static void hfp_ag_disconnected(struct bt_rfcomm_dlc *dlc)
1464 {
1465 	struct bt_hfp_ag *ag = CONTAINER_OF(dlc, struct bt_hfp_ag, rfcomm_dlc);
1466 	bt_hfp_call_state_t call_state;
1467 	sys_snode_t *node;
1468 	struct bt_ag_tx *tx;
1469 
1470 	k_work_cancel_delayable(&ag->tx_work);
1471 
1472 	hfp_ag_lock(ag);
1473 	node = sys_slist_get(&ag->tx_pending);
1474 	hfp_ag_unlock(ag);
1475 	tx = CONTAINER_OF(node, struct bt_ag_tx, node);
1476 	while (tx) {
1477 		if (tx->buf && !atomic_test_and_clear_bit(ag->flags, BT_HFP_AG_TX_ONGOING)) {
1478 			net_buf_unref(tx->buf);
1479 		}
1480 		tx->err = -ESHUTDOWN;
1481 		k_fifo_put(&ag_tx_notify, tx);
1482 		hfp_ag_lock(ag);
1483 		node = sys_slist_get(&ag->tx_pending);
1484 		hfp_ag_unlock(ag);
1485 		tx = CONTAINER_OF(node, struct bt_ag_tx, node);
1486 	}
1487 
1488 	bt_hfp_ag_set_state(ag, BT_HFP_DISCONNECTED);
1489 
1490 	hfp_ag_lock(ag);
1491 	call_state = ag->call_state;
1492 	hfp_ag_unlock(ag);
1493 	if ((call_state == BT_HFP_CALL_ALERTING) || (call_state == BT_HFP_CALL_ACTIVE) ||
1494 	    (call_state == BT_HFP_CALL_HOLD)) {
1495 		bt_hfp_ag_terminate_cb(ag, NULL);
1496 	}
1497 
1498 	LOG_DBG("AG %p", ag);
1499 }
1500 
hfp_ag_recv(struct bt_rfcomm_dlc * dlc,struct net_buf * buf)1501 static void hfp_ag_recv(struct bt_rfcomm_dlc *dlc, struct net_buf *buf)
1502 {
1503 	struct bt_hfp_ag *ag = CONTAINER_OF(dlc, struct bt_hfp_ag, rfcomm_dlc);
1504 	uint8_t *data = buf->data;
1505 	uint16_t len = buf->len;
1506 	enum at_cme cme_err;
1507 	int err = -EOPNOTSUPP;
1508 
1509 	LOG_HEXDUMP_DBG(data, len, "Received:");
1510 
1511 	for (uint32_t index = 0; index < ARRAY_SIZE(cmd_handlers); index++) {
1512 		if (strlen(cmd_handlers[index].cmd) > len) {
1513 			continue;
1514 		}
1515 		if (strncmp((char *)data, cmd_handlers[index].cmd,
1516 				 strlen(cmd_handlers[index].cmd)) != 0) {
1517 			continue;
1518 		}
1519 		if (NULL != cmd_handlers[index].handler) {
1520 			(void)net_buf_pull(buf, strlen(cmd_handlers[index].cmd));
1521 			err = cmd_handlers[index].handler(ag, buf);
1522 			LOG_DBG("AT commander is handled (err %d)", err);
1523 			break;
1524 		}
1525 	}
1526 
1527 	if ((err != 0) && atomic_test_bit(ag->flags, BT_HFP_AG_CMEE_ENABLE)) {
1528 		cme_err = bt_hfp_ag_get_cme_err(err);
1529 		err = hfp_ag_send_data(ag, NULL, NULL, "\r\n+CME ERROR:%d\r\n", (uint32_t)cme_err);
1530 	} else {
1531 		err = hfp_ag_send_data(ag, NULL, NULL, "\r\n%s\r\n", (err == 0) ? "OK" : "ERROR");
1532 	}
1533 
1534 	if (err != 0) {
1535 		LOG_ERR("HFP AG send response err :(%d)", err);
1536 	}
1537 }
1538 
bt_hfp_ag_thread(void * p1,void * p2,void * p3)1539 static void bt_hfp_ag_thread(void *p1, void *p2, void *p3)
1540 {
1541 	struct bt_ag_tx *tx;
1542 	bt_hfp_ag_tx_cb_t cb;
1543 	struct bt_hfp_ag *ag;
1544 	void *user_data;
1545 	bt_hfp_state_t state;
1546 	int err;
1547 
1548 	while (true) {
1549 		tx = (struct bt_ag_tx *)k_fifo_get(&ag_tx_notify, K_FOREVER);
1550 
1551 		if (tx == NULL) {
1552 			continue;
1553 		}
1554 
1555 		cb = tx->cb;
1556 		ag = tx->ag;
1557 		user_data = tx->user_data;
1558 		err = tx->err;
1559 
1560 		bt_ag_tx_free(tx);
1561 
1562 		if (err < 0) {
1563 			hfp_ag_lock(ag);
1564 			state = ag->state;
1565 			hfp_ag_unlock(ag);
1566 			if ((state != BT_HFP_DISCONNECTED) && (state != BT_HFP_DISCONNECTING)) {
1567 				bt_hfp_ag_set_state(ag, BT_HFP_DISCONNECTING);
1568 				bt_rfcomm_dlc_disconnect(&ag->rfcomm_dlc);
1569 			}
1570 		}
1571 
1572 		if (cb) {
1573 			cb(ag, user_data);
1574 		}
1575 	}
1576 }
1577 
hfp_ag_sent(struct bt_rfcomm_dlc * dlc,int err)1578 static void hfp_ag_sent(struct bt_rfcomm_dlc *dlc, int err)
1579 {
1580 	struct bt_hfp_ag *ag = CONTAINER_OF(dlc, struct bt_hfp_ag, rfcomm_dlc);
1581 	sys_snode_t *node;
1582 	struct bt_ag_tx *tx;
1583 
1584 	hfp_ag_lock(ag);
1585 	/* Clear the tx ongoing flag */
1586 	if (!atomic_test_and_clear_bit(ag->flags, BT_HFP_AG_TX_ONGOING)) {
1587 		LOG_WRN("tx ongoing flag is not set");
1588 		hfp_ag_unlock(ag);
1589 		return;
1590 	}
1591 
1592 	node = sys_slist_get(&ag->tx_pending);
1593 	hfp_ag_unlock(ag);
1594 	if (!node) {
1595 		LOG_ERR("No pending tx");
1596 		return;
1597 	}
1598 
1599 	tx = CONTAINER_OF(node, struct bt_ag_tx, node);
1600 	LOG_ERR("Completed pending tx %p", tx);
1601 
1602 	/* Restart the tx work */
1603 	k_work_reschedule(&ag->tx_work, K_NO_WAIT);
1604 
1605 	tx->err = err;
1606 	k_fifo_put(&ag_tx_notify, tx);
1607 }
1608 
bt_ag_deferred_work_cb(struct bt_hfp_ag * ag,void * user_data)1609 static void bt_ag_deferred_work_cb(struct bt_hfp_ag *ag, void *user_data)
1610 {
1611 	int err;
1612 	bt_hfp_call_state_t call_state;
1613 
1614 	LOG_DBG("");
1615 
1616 	hfp_ag_lock(ag);
1617 	call_state = ag->call_state;
1618 	hfp_ag_unlock(ag);
1619 
1620 	switch (call_state) {
1621 	case BT_HFP_CALL_TERMINATE:
1622 		break;
1623 	case BT_HFP_CALL_ACTIVE:
1624 		break;
1625 	case BT_HFP_CALL_HOLD:
1626 		break;
1627 	case BT_HFP_CALL_OUTGOING:
1628 		__fallthrough;
1629 	case BT_HFP_CALL_INCOMING:
1630 		__fallthrough;
1631 	case BT_HFP_CALL_ALERTING:
1632 		__fallthrough;
1633 	default:
1634 		if (ag->indicator_value[BT_HFP_AG_CALL_SETUP_IND] &&
1635 		    ag->indicator_value[BT_HFP_AG_CALL_IND]) {
1636 			err = hfp_ag_update_indicator(ag, BT_HFP_AG_CALL_SETUP_IND,
1637 							BT_HFP_CALL_SETUP_NONE, NULL, NULL);
1638 			if (err) {
1639 				LOG_ERR("Fail to send indicator");
1640 				bt_hfp_ag_terminate_cb(ag, NULL);
1641 			} else {
1642 				err = hfp_ag_update_indicator(ag, BT_HFP_AG_CALL_IND, 0,
1643 								bt_hfp_ag_terminate_cb, NULL);
1644 				if (err) {
1645 					LOG_ERR("Fail to send indicator");
1646 					bt_hfp_ag_terminate_cb(ag, NULL);
1647 				}
1648 			}
1649 		} else if (ag->indicator_value[BT_HFP_AG_CALL_SETUP_IND]) {
1650 			err = hfp_ag_update_indicator(ag, BT_HFP_AG_CALL_SETUP_IND,
1651 							BT_HFP_CALL_SETUP_NONE, bt_hfp_ag_reject_cb,
1652 							NULL);
1653 			if (err) {
1654 				LOG_ERR("Fail to send indicator");
1655 				bt_hfp_ag_terminate_cb(ag, NULL);
1656 			}
1657 		} else if (ag->indicator_value[BT_HFP_AG_CALL_IND]) {
1658 			err = hfp_ag_update_indicator(ag, BT_HFP_AG_CALL_IND, 0,
1659 							bt_hfp_ag_terminate_cb, NULL);
1660 			if (err) {
1661 				LOG_ERR("Fail to send indicator");
1662 				bt_hfp_ag_terminate_cb(ag, NULL);
1663 			}
1664 		}
1665 		break;
1666 	}
1667 }
1668 
bt_ag_deferred_work(struct k_work * work)1669 static void bt_ag_deferred_work(struct k_work *work)
1670 {
1671 	struct k_work_delayable *dwork = k_work_delayable_from_work(work);
1672 	struct bt_hfp_ag *ag = CONTAINER_OF(dwork, struct bt_hfp_ag, deferred_work);
1673 
1674 	(void)hfp_ag_next_step(ag, bt_ag_deferred_work_cb, NULL);
1675 }
1676 
bt_ag_ringing_work_cb(struct bt_hfp_ag * ag,void * user_data)1677 static void bt_ag_ringing_work_cb(struct bt_hfp_ag *ag, void *user_data)
1678 {
1679 	int err;
1680 	bt_hfp_call_state_t call_state;
1681 
1682 	LOG_DBG("");
1683 
1684 	hfp_ag_lock(ag);
1685 	call_state = ag->call_state;
1686 	hfp_ag_unlock(ag);
1687 	if (call_state == BT_HFP_CALL_ALERTING) {
1688 
1689 		if (!atomic_test_bit(ag->flags, BT_HFP_AG_INCOMING_CALL)) {
1690 			return;
1691 		}
1692 
1693 		k_work_reschedule(&ag->ringing_work,
1694 				  K_SECONDS(CONFIG_BT_HFP_AG_RING_NOTIFY_INTERVAL));
1695 
1696 		err = hfp_ag_send_data(ag, NULL, NULL, "\r\nRING\r\n");
1697 		if (err) {
1698 			LOG_ERR("Fail to send RING %d", err);
1699 		} else {
1700 			if (atomic_test_bit(ag->flags, BT_HFP_AG_CLIP_ENABLE)) {
1701 				err = hfp_ag_send_data(ag, NULL, NULL, "\r\n+CLIP:\"%s\",%d\r\n",
1702 							 ag->number, 0);
1703 				if (err) {
1704 					LOG_ERR("Fail to send CLIP %d", err);
1705 				}
1706 			}
1707 		}
1708 	}
1709 }
1710 
bt_ag_ringing_work(struct k_work * work)1711 static void bt_ag_ringing_work(struct k_work *work)
1712 {
1713 	struct k_work_delayable *dwork = k_work_delayable_from_work(work);
1714 	struct bt_hfp_ag *ag = CONTAINER_OF(dwork, struct bt_hfp_ag, ringing_work);
1715 
1716 	(void)hfp_ag_next_step(ag, bt_ag_ringing_work_cb, NULL);
1717 }
1718 
bt_hfp_ag_connect(struct bt_conn * conn,struct bt_hfp_ag ** ag,uint8_t channel)1719 int bt_hfp_ag_connect(struct bt_conn *conn, struct bt_hfp_ag **ag, uint8_t channel)
1720 {
1721 	int i;
1722 	int err;
1723 
1724 	static struct bt_rfcomm_dlc_ops ops = {
1725 		.connected = hfp_ag_connected,
1726 		.disconnected = hfp_ag_disconnected,
1727 		.recv = hfp_ag_recv,
1728 		.sent = hfp_ag_sent,
1729 	};
1730 
1731 	LOG_DBG("");
1732 
1733 	if (ag == NULL) {
1734 		return -EINVAL;
1735 	}
1736 
1737 	*ag = NULL;
1738 
1739 	if (ag_thread_id == NULL) {
1740 
1741 		k_fifo_init(&ag_tx_free);
1742 		k_fifo_init(&ag_tx_notify);
1743 
1744 		for (i = 0; i < ARRAY_SIZE(ag_tx); i++) {
1745 			k_fifo_put(&ag_tx_free, &ag_tx[i]);
1746 		}
1747 
1748 		ag_thread_id = k_thread_create(
1749 			&ag_thread, ag_thread_stack, K_KERNEL_STACK_SIZEOF(ag_thread_stack),
1750 			bt_hfp_ag_thread, NULL, NULL, NULL,
1751 			K_PRIO_COOP(CONFIG_BT_HFP_AG_THREAD_PRIO), 0, K_NO_WAIT);
1752 		if (ag_thread_id == NULL) {
1753 			return -ENOMEM;
1754 		}
1755 		k_thread_name_set(ag_thread_id, "HFP AG");
1756 	}
1757 
1758 	for (i = 0; i < ARRAY_SIZE(bt_hfp_ag_pool); i++) {
1759 		struct bt_hfp_ag *_ag = &bt_hfp_ag_pool[i];
1760 
1761 		if (_ag->rfcomm_dlc.session) {
1762 			continue;
1763 		}
1764 
1765 		(void)memset(_ag, 0, sizeof(struct bt_hfp_ag));
1766 
1767 		sys_slist_init(&_ag->tx_pending);
1768 
1769 		k_sem_init(&_ag->lock, 1, 1);
1770 
1771 		_ag->rfcomm_dlc.ops = &ops;
1772 		_ag->rfcomm_dlc.mtu = BT_HFP_MAX_MTU;
1773 
1774 		/* Set the supported features*/
1775 		_ag->ag_features = BT_HFP_AG_SUPPORTED_FEATURES;
1776 
1777 		/* If supported codec ids cannot be notified, disable codec negotiation. */
1778 		if (!(bt_ag && bt_ag->codec)) {
1779 			_ag->ag_features &= ~BT_HFP_AG_FEATURE_CODEC_NEG;
1780 		}
1781 
1782 		_ag->hf_features = 0;
1783 		_ag->hf_codec_ids = 0;
1784 
1785 		_ag->acl_conn = conn;
1786 
1787 		/* Set AG indicator value */
1788 		_ag->indicator_value[BT_HFP_AG_SERVICE_IND] = 0;
1789 		_ag->indicator_value[BT_HFP_AG_CALL_IND] = 0;
1790 		_ag->indicator_value[BT_HFP_AG_CALL_SETUP_IND] = 0;
1791 		_ag->indicator_value[BT_HFP_AG_CALL_HELD_IND] = 0;
1792 		_ag->indicator_value[BT_HFP_AG_SIGNAL_IND] = 0;
1793 		_ag->indicator_value[BT_HFP_AG_ROAM_IND] = 0;
1794 		_ag->indicator_value[BT_HFP_AG_BATTERY_IND] = 0;
1795 
1796 		/* Set AG indicator status */
1797 		_ag->indicator = BIT(BT_HFP_AG_SERVICE_IND) | BIT(BT_HFP_AG_CALL_IND) |
1798 				 BIT(BT_HFP_AG_CALL_SETUP_IND) | BIT(BT_HFP_AG_CALL_HELD_IND) |
1799 				 BIT(BT_HFP_AG_SIGNAL_IND) | BIT(BT_HFP_AG_ROAM_IND) |
1800 				 BIT(BT_HFP_AG_BATTERY_IND);
1801 
1802 		/* Set AG operator */
1803 		memcpy(_ag->operator, "UNKNOWN", sizeof("UNKNOWN"));
1804 
1805 		/* Set Codec ID*/
1806 		_ag->selected_codec_id = BT_HFP_AG_CODEC_CVSD;
1807 
1808 		/* Init delay work */
1809 		k_work_init_delayable(&_ag->deferred_work, bt_ag_deferred_work);
1810 
1811 		k_work_init_delayable(&_ag->ringing_work, bt_ag_ringing_work);
1812 
1813 		k_work_init_delayable(&_ag->tx_work, bt_ag_tx_work);
1814 
1815 		atomic_set_bit(_ag->flags, BT_HFP_AG_CODEC_CHANGED);
1816 
1817 		*ag = _ag;
1818 	}
1819 
1820 	if (*ag == NULL) {
1821 		return -ENOMEM;
1822 	}
1823 
1824 	err = bt_rfcomm_dlc_connect(conn, &(*ag)->rfcomm_dlc, channel);
1825 	if (err != 0) {
1826 		(void)memset(*ag, 0, sizeof(struct bt_hfp_ag));
1827 		*ag = NULL;
1828 	} else {
1829 		bt_hfp_ag_set_state(*ag, BT_HFP_CONNECTING);
1830 	}
1831 
1832 	return err;
1833 }
1834 
bt_hfp_ag_disconnect(struct bt_hfp_ag * ag)1835 int bt_hfp_ag_disconnect(struct bt_hfp_ag *ag)
1836 {
1837 	int err;
1838 	bt_hfp_call_state_t call_state;
1839 
1840 	LOG_DBG("");
1841 
1842 	if (ag == NULL) {
1843 		return -EINVAL;
1844 	}
1845 
1846 	hfp_ag_lock(ag);
1847 	call_state = ag->call_state;
1848 	hfp_ag_unlock(ag);
1849 
1850 	bt_hfp_ag_set_state(ag, BT_HFP_DISCONNECTING);
1851 
1852 	if ((call_state == BT_HFP_CALL_ACTIVE) || (call_state == BT_HFP_CALL_HOLD)) {
1853 		err = hfp_ag_update_indicator(ag, BT_HFP_AG_CALL_IND, 0, bt_hfp_ag_terminate_cb,
1854 					      NULL);
1855 		if (err != 0) {
1856 			LOG_ERR("HFP AG send response err :(%d)", err);
1857 		}
1858 		return err;
1859 	} else if (call_state != BT_HFP_CALL_TERMINATE) {
1860 		err = hfp_ag_update_indicator(ag, BT_HFP_AG_CALL_SETUP_IND, BT_HFP_CALL_SETUP_NONE,
1861 					      bt_hfp_ag_reject_cb, NULL);
1862 		if (err != 0) {
1863 			LOG_ERR("HFP AG send response err :(%d)", err);
1864 		}
1865 		return err;
1866 	}
1867 
1868 	return bt_rfcomm_dlc_disconnect(&ag->rfcomm_dlc);
1869 }
1870 
bt_hfp_ag_register(struct bt_hfp_ag_cb * cb)1871 int bt_hfp_ag_register(struct bt_hfp_ag_cb *cb)
1872 {
1873 	if (!cb) {
1874 		return -EINVAL;
1875 	}
1876 
1877 	if (bt_ag) {
1878 		return -EALREADY;
1879 	}
1880 
1881 	bt_ag = cb;
1882 
1883 	return 0;
1884 }
1885 
bt_hfp_ag_incoming_cb(struct bt_hfp_ag * ag,void * user_data)1886 static void bt_hfp_ag_incoming_cb(struct bt_hfp_ag *ag, void *user_data)
1887 {
1888 	bt_hfp_ag_set_call_state(ag, BT_HFP_CALL_INCOMING);
1889 
1890 	if (bt_ag && bt_ag->incoming) {
1891 		bt_ag->incoming(ag, ag->number);
1892 	}
1893 
1894 	if (atomic_test_bit(ag->flags, BT_HFP_AG_INBAND_RING)) {
1895 		int err;
1896 
1897 		err = bt_hfp_ag_create_audio_connection(ag);
1898 		if (err) {
1899 			bt_hfp_ag_call_reject(ag, NULL);
1900 		}
1901 	} else {
1902 		bt_hfp_ag_set_call_state(ag, BT_HFP_CALL_ALERTING);
1903 		bt_hfp_ag_call_ringing_cb(ag, false);
1904 	}
1905 }
1906 
bt_hfp_ag_remote_incoming(struct bt_hfp_ag * ag,const char * number)1907 int bt_hfp_ag_remote_incoming(struct bt_hfp_ag *ag, const char *number)
1908 {
1909 	int err = 0;
1910 	size_t len;
1911 
1912 	LOG_DBG("");
1913 
1914 	if (ag == NULL) {
1915 		return -EINVAL;
1916 	}
1917 
1918 	hfp_ag_lock(ag);
1919 	if (ag->state != BT_HFP_CONNECTED) {
1920 		hfp_ag_unlock(ag);
1921 		return -ENOTCONN;
1922 	}
1923 
1924 	if (ag->call_state != BT_HFP_CALL_TERMINATE) {
1925 		hfp_ag_unlock(ag);
1926 		return -EBUSY;
1927 	}
1928 	hfp_ag_unlock(ag);
1929 
1930 	len = strlen(number);
1931 	if ((len == 0) || (len > CONFIG_BT_HFP_AG_PHONE_NUMBER_MAX_LEN)) {
1932 		return -EINVAL;
1933 	}
1934 
1935 	hfp_ag_lock(ag);
1936 	/* Copy number to ag->number including null-character */
1937 	memcpy(ag->number, number, len + 1);
1938 	hfp_ag_unlock(ag);
1939 
1940 	atomic_set_bit(ag->flags, BT_HFP_AG_INCOMING_CALL);
1941 
1942 	err = hfp_ag_update_indicator(ag, BT_HFP_AG_CALL_SETUP_IND, BT_HFP_CALL_SETUP_INCOMING,
1943 					bt_hfp_ag_incoming_cb, NULL);
1944 	if (err != 0) {
1945 		atomic_clear_bit(ag->flags, BT_HFP_AG_INCOMING_CALL);
1946 	}
1947 
1948 	return err;
1949 }
1950 
bt_hfp_ag_reject(struct bt_hfp_ag * ag)1951 int bt_hfp_ag_reject(struct bt_hfp_ag *ag)
1952 {
1953 	int err;
1954 
1955 	LOG_DBG("");
1956 
1957 	if (ag == NULL) {
1958 		return -EINVAL;
1959 	}
1960 
1961 	hfp_ag_lock(ag);
1962 	if (ag->state != BT_HFP_CONNECTED) {
1963 		hfp_ag_unlock(ag);
1964 		return -ENOTCONN;
1965 	}
1966 
1967 	if ((ag->call_state != BT_HFP_CALL_ALERTING) && (ag->call_state != BT_HFP_CALL_INCOMING)) {
1968 		hfp_ag_unlock(ag);
1969 		return -EINVAL;
1970 	}
1971 	hfp_ag_unlock(ag);
1972 
1973 	if (!atomic_test_bit(ag->flags, BT_HFP_AG_INCOMING_CALL)) {
1974 		return -EINVAL;
1975 	}
1976 
1977 	err = hfp_ag_update_indicator(ag, BT_HFP_AG_CALL_SETUP_IND, BT_HFP_CALL_SETUP_NONE,
1978 					bt_hfp_ag_reject_cb, NULL);
1979 
1980 	return err;
1981 }
1982 
bt_hfp_ag_accept(struct bt_hfp_ag * ag)1983 int bt_hfp_ag_accept(struct bt_hfp_ag *ag)
1984 {
1985 	int err;
1986 
1987 	LOG_DBG("");
1988 
1989 	if (ag == NULL) {
1990 		return -EINVAL;
1991 	}
1992 
1993 	hfp_ag_lock(ag);
1994 	if (ag->state != BT_HFP_CONNECTED) {
1995 		hfp_ag_unlock(ag);
1996 		return -ENOTCONN;
1997 	}
1998 
1999 	if (ag->call_state != BT_HFP_CALL_ALERTING) {
2000 		hfp_ag_unlock(ag);
2001 		return -EINVAL;
2002 	}
2003 	hfp_ag_unlock(ag);
2004 
2005 	if (!atomic_test_bit(ag->flags, BT_HFP_AG_INCOMING_CALL)) {
2006 		return -EINVAL;
2007 	}
2008 
2009 	err = hfp_ag_update_indicator(ag, BT_HFP_AG_CALL_IND, 1, bt_hfp_ag_accept_cb, NULL);
2010 	if (err != 0) {
2011 		LOG_ERR("Fail to send err :(%d)", err);
2012 	}
2013 
2014 	err = hfp_ag_update_indicator(ag, BT_HFP_AG_CALL_SETUP_IND, BT_HFP_CALL_SETUP_NONE,
2015 					bt_hfp_ag_audio_connection, NULL);
2016 	if (err != 0) {
2017 		LOG_ERR("Fail to send err :(%d)", err);
2018 	}
2019 
2020 	return err;
2021 }
2022 
bt_hfp_ag_terminate(struct bt_hfp_ag * ag)2023 int bt_hfp_ag_terminate(struct bt_hfp_ag *ag)
2024 {
2025 	int err;
2026 
2027 	LOG_DBG("");
2028 
2029 	if (ag == NULL) {
2030 		return -EINVAL;
2031 	}
2032 
2033 	hfp_ag_lock(ag);
2034 	if (ag->state != BT_HFP_CONNECTED) {
2035 		hfp_ag_unlock(ag);
2036 		return -ENOTCONN;
2037 	}
2038 
2039 	if ((ag->call_state != BT_HFP_CALL_ACTIVE) && (ag->call_state != BT_HFP_CALL_HOLD)) {
2040 		hfp_ag_unlock(ag);
2041 		return -EINVAL;
2042 	}
2043 	hfp_ag_unlock(ag);
2044 
2045 	err = hfp_ag_update_indicator(ag, BT_HFP_AG_CALL_IND, 0, bt_hfp_ag_terminate_cb, NULL);
2046 
2047 	return err;
2048 }
2049 
bt_hfp_ag_outgoing_cb(struct bt_hfp_ag * ag,void * user_data)2050 static void bt_hfp_ag_outgoing_cb(struct bt_hfp_ag *ag, void *user_data)
2051 {
2052 	bt_hfp_ag_set_call_state(ag, BT_HFP_CALL_OUTGOING);
2053 
2054 	if (bt_ag && bt_ag->outgoing) {
2055 		bt_ag->outgoing(ag, ag->number);
2056 	}
2057 
2058 	if (atomic_test_bit(ag->flags, BT_HFP_AG_INBAND_RING)) {
2059 		int err;
2060 
2061 		err = bt_hfp_ag_create_audio_connection(ag);
2062 		if (err) {
2063 			bt_hfp_ag_call_reject(ag, NULL);
2064 		}
2065 	}
2066 }
2067 
bt_hfp_ag_outgoing(struct bt_hfp_ag * ag,const char * number)2068 int bt_hfp_ag_outgoing(struct bt_hfp_ag *ag, const char *number)
2069 {
2070 	int err = 0;
2071 	size_t len;
2072 
2073 	LOG_DBG("");
2074 
2075 	if (ag == NULL) {
2076 		return -EINVAL;
2077 	}
2078 
2079 	hfp_ag_lock(ag);
2080 	if (ag->state != BT_HFP_CONNECTED) {
2081 		hfp_ag_unlock(ag);
2082 		return -ENOTCONN;
2083 	}
2084 
2085 	if (ag->call_state != BT_HFP_CALL_TERMINATE) {
2086 		hfp_ag_unlock(ag);
2087 		return -EBUSY;
2088 	}
2089 	hfp_ag_unlock(ag);
2090 
2091 	len = strlen(number);
2092 	if ((len == 0) || (len > CONFIG_BT_HFP_AG_PHONE_NUMBER_MAX_LEN)) {
2093 		return -EINVAL;
2094 	}
2095 
2096 	hfp_ag_lock(ag);
2097 	/* Copy number to ag->number including null-character */
2098 	memcpy(ag->number, number, len + 1);
2099 	hfp_ag_unlock(ag);
2100 
2101 	atomic_clear_bit(ag->flags, BT_HFP_AG_INCOMING_CALL);
2102 
2103 	err = hfp_ag_update_indicator(ag, BT_HFP_AG_CALL_SETUP_IND, BT_HFP_CALL_SETUP_OUTGOING,
2104 					bt_hfp_ag_outgoing_cb, NULL);
2105 
2106 	return err;
2107 }
2108 
bt_hfp_ag_ringing_cb(struct bt_hfp_ag * ag,void * user_data)2109 static void bt_hfp_ag_ringing_cb(struct bt_hfp_ag *ag, void *user_data)
2110 {
2111 	bt_hfp_ag_set_call_state(ag, BT_HFP_CALL_ALERTING);
2112 
2113 	if (atomic_test_bit(ag->flags, BT_HFP_AG_INBAND_RING)) {
2114 		bt_hfp_ag_call_ringing_cb(ag, true);
2115 	} else {
2116 		bt_hfp_ag_call_ringing_cb(ag, false);
2117 	}
2118 }
2119 
bt_hfp_ag_remote_ringing(struct bt_hfp_ag * ag)2120 int bt_hfp_ag_remote_ringing(struct bt_hfp_ag *ag)
2121 {
2122 	int err = 0;
2123 
2124 	LOG_DBG("");
2125 
2126 	if (ag == NULL) {
2127 		return -EINVAL;
2128 	}
2129 
2130 	hfp_ag_lock(ag);
2131 	if (ag->state != BT_HFP_CONNECTED) {
2132 		hfp_ag_unlock(ag);
2133 		return -ENOTCONN;
2134 	}
2135 
2136 	if (ag->call_state != BT_HFP_CALL_OUTGOING) {
2137 		hfp_ag_unlock(ag);
2138 		return -EBUSY;
2139 	}
2140 
2141 	if (atomic_test_bit(ag->flags, BT_HFP_AG_INBAND_RING)) {
2142 		if (ag->sco_chan.sco == NULL) {
2143 			hfp_ag_unlock(ag);
2144 			return -ENOTCONN;
2145 		}
2146 	}
2147 	hfp_ag_unlock(ag);
2148 
2149 	err = hfp_ag_update_indicator(ag, BT_HFP_AG_CALL_SETUP_IND,
2150 					BT_HFP_CALL_SETUP_REMOTE_ALERTING, bt_hfp_ag_ringing_cb,
2151 					NULL);
2152 
2153 	return err;
2154 }
2155 
bt_hfp_ag_remote_reject(struct bt_hfp_ag * ag)2156 int bt_hfp_ag_remote_reject(struct bt_hfp_ag *ag)
2157 {
2158 	int err;
2159 
2160 	LOG_DBG("");
2161 
2162 	if (ag == NULL) {
2163 		return -EINVAL;
2164 	}
2165 
2166 	hfp_ag_lock(ag);
2167 	if (ag->state != BT_HFP_CONNECTED) {
2168 		hfp_ag_unlock(ag);
2169 		return -ENOTCONN;
2170 	}
2171 
2172 	if ((ag->call_state != BT_HFP_CALL_ALERTING) && (ag->call_state != BT_HFP_CALL_OUTGOING)) {
2173 		hfp_ag_unlock(ag);
2174 		return -EINVAL;
2175 	}
2176 	hfp_ag_unlock(ag);
2177 
2178 	if (atomic_test_bit(ag->flags, BT_HFP_AG_INCOMING_CALL)) {
2179 		return -EINVAL;
2180 	}
2181 
2182 	err = hfp_ag_update_indicator(ag, BT_HFP_AG_CALL_SETUP_IND, BT_HFP_CALL_SETUP_NONE,
2183 					bt_hfp_ag_reject_cb, NULL);
2184 
2185 	return err;
2186 }
2187 
bt_hfp_ag_remote_accept(struct bt_hfp_ag * ag)2188 int bt_hfp_ag_remote_accept(struct bt_hfp_ag *ag)
2189 {
2190 	int err;
2191 
2192 	LOG_DBG("");
2193 
2194 	if (ag == NULL) {
2195 		return -EINVAL;
2196 	}
2197 
2198 	hfp_ag_lock(ag);
2199 	if (ag->state != BT_HFP_CONNECTED) {
2200 		hfp_ag_unlock(ag);
2201 		return -ENOTCONN;
2202 	}
2203 
2204 	if (ag->call_state != BT_HFP_CALL_ALERTING) {
2205 		hfp_ag_unlock(ag);
2206 		return -EINVAL;
2207 	}
2208 	hfp_ag_unlock(ag);
2209 
2210 	if (atomic_test_bit(ag->flags, BT_HFP_AG_INCOMING_CALL)) {
2211 		return -EINVAL;
2212 	}
2213 
2214 	err = hfp_ag_update_indicator(ag, BT_HFP_AG_CALL_IND, 1, bt_hfp_ag_accept_cb, NULL);
2215 	if (err != 0) {
2216 		LOG_ERR("Fail to send err :(%d)", err);
2217 	}
2218 
2219 	err = hfp_ag_update_indicator(ag, BT_HFP_AG_CALL_SETUP_IND, BT_HFP_CALL_SETUP_NONE,
2220 					bt_hfp_ag_audio_connection, NULL);
2221 	if (err != 0) {
2222 		LOG_ERR("Fail to send err :(%d)", err);
2223 	}
2224 
2225 	return err;
2226 }
2227 
bt_hfp_ag_remote_terminate(struct bt_hfp_ag * ag)2228 int bt_hfp_ag_remote_terminate(struct bt_hfp_ag *ag)
2229 {
2230 	int err;
2231 
2232 	LOG_DBG("");
2233 
2234 	if (ag == NULL) {
2235 		return -EINVAL;
2236 	}
2237 
2238 	hfp_ag_lock(ag);
2239 	if (ag->state != BT_HFP_CONNECTED) {
2240 		hfp_ag_unlock(ag);
2241 		return -ENOTCONN;
2242 	}
2243 
2244 	if ((ag->call_state != BT_HFP_CALL_ACTIVE) && (ag->call_state != BT_HFP_CALL_HOLD)) {
2245 		hfp_ag_unlock(ag);
2246 		return -EINVAL;
2247 	}
2248 	hfp_ag_unlock(ag);
2249 
2250 	err = hfp_ag_update_indicator(ag, BT_HFP_AG_CALL_IND, 0, bt_hfp_ag_terminate_cb, NULL);
2251 
2252 	return err;
2253 }
2254 
bt_hfp_ag_set_indicator(struct bt_hfp_ag * ag,enum bt_hfp_ag_indicator index,uint8_t value)2255 int bt_hfp_ag_set_indicator(struct bt_hfp_ag *ag, enum bt_hfp_ag_indicator index, uint8_t value)
2256 {
2257 	int err;
2258 
2259 	LOG_DBG("");
2260 
2261 	if (ag == NULL) {
2262 		return -EINVAL;
2263 	}
2264 
2265 	hfp_ag_lock(ag);
2266 	if (ag->state != BT_HFP_CONNECTED) {
2267 		hfp_ag_unlock(ag);
2268 		return -ENOTCONN;
2269 	}
2270 	hfp_ag_unlock(ag);
2271 
2272 	switch (index) {
2273 	case BT_HFP_AG_SERVICE_IND:
2274 		__fallthrough;
2275 	case BT_HFP_AG_SIGNAL_IND:
2276 		__fallthrough;
2277 	case BT_HFP_AG_ROAM_IND:
2278 		__fallthrough;
2279 	case BT_HFP_AG_BATTERY_IND:
2280 		if ((ag_ind[(uint8_t)index].min > value) || (ag_ind[(uint8_t)index].max < value)) {
2281 			return -EINVAL;
2282 		}
2283 		break;
2284 	case BT_HFP_AG_CALL_IND:
2285 		__fallthrough;
2286 	case BT_HFP_AG_CALL_SETUP_IND:
2287 		__fallthrough;
2288 	case BT_HFP_AG_CALL_HELD_IND:
2289 		__fallthrough;
2290 	default:
2291 		return -EINVAL;
2292 	}
2293 
2294 	err = hfp_ag_update_indicator(ag, index, value, NULL, NULL);
2295 
2296 	return err;
2297 }
2298 
bt_hfp_ag_set_operator(struct bt_hfp_ag * ag,char * name)2299 int bt_hfp_ag_set_operator(struct bt_hfp_ag *ag, char *name)
2300 {
2301 	int len;
2302 
2303 	LOG_DBG("");
2304 
2305 	if (ag == NULL) {
2306 		return -EINVAL;
2307 	}
2308 
2309 	hfp_ag_lock(ag);
2310 	if (ag->state != BT_HFP_CONNECTED) {
2311 		hfp_ag_unlock(ag);
2312 		return -ENOTCONN;
2313 	}
2314 
2315 	len = strlen(name);
2316 	len = MIN(sizeof(ag->operator) - 1, len);
2317 	memcpy(ag->operator, name, len);
2318 	ag->operator[len] = '\0';
2319 	hfp_ag_unlock(ag);
2320 
2321 	return 0;
2322 }
2323 
bt_hfp_ag_select_codec(struct bt_hfp_ag * ag,uint8_t id)2324 int bt_hfp_ag_select_codec(struct bt_hfp_ag *ag, uint8_t id)
2325 {
2326 	int err;
2327 
2328 	LOG_DBG("");
2329 
2330 	if (ag == NULL) {
2331 		return -EINVAL;
2332 	}
2333 
2334 	hfp_ag_lock(ag);
2335 	if (ag->state != BT_HFP_CONNECTED) {
2336 		hfp_ag_unlock(ag);
2337 		return -ENOTCONN;
2338 	}
2339 
2340 	if (!(ag->hf_codec_ids && BIT(id))) {
2341 		hfp_ag_unlock(ag);
2342 		return -ENOTSUP;
2343 	}
2344 	hfp_ag_unlock(ag);
2345 
2346 	if (atomic_test_bit(ag->flags, BT_HFP_AG_CODEC_CONN)) {
2347 		return -EBUSY;
2348 	}
2349 
2350 	hfp_ag_lock(ag);
2351 	ag->selected_codec_id = id;
2352 	hfp_ag_unlock(ag);
2353 
2354 	atomic_set_bit(ag->flags, BT_HFP_AG_CODEC_CHANGED);
2355 
2356 	err = bt_hfp_ag_create_audio_connection(ag);
2357 
2358 	return err;
2359 }
2360