1 /* csip.c - CAP Commander specific MICP mocks */
2 
3 /*
4  * Copyright (c) 2023 Nordic Semiconductor ASA
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include <zephyr/bluetooth/audio/micp.h>
10 
11 static struct bt_micp_mic_ctlr_cb *micp_cb;
12 
13 static struct bt_micp_mic_ctlr {
14 	struct bt_conn *conn;
15 	struct bt_aics *aics[CONFIG_BT_MICP_MIC_CTLR_MAX_AICS_INST];
16 } mic_ctlrs[CONFIG_BT_MAX_CONN];
17 
bt_micp_mic_ctlr_get_by_conn(const struct bt_conn * conn)18 struct bt_micp_mic_ctlr *bt_micp_mic_ctlr_get_by_conn(const struct bt_conn *conn)
19 {
20 	for (size_t i = 0; i < ARRAY_SIZE(mic_ctlrs); i++) {
21 		if (mic_ctlrs[i].conn == conn) {
22 			return &mic_ctlrs[i];
23 		}
24 	}
25 
26 	return NULL;
27 }
28 
bt_micp_mic_ctlr_conn_get(const struct bt_micp_mic_ctlr * mic_ctlr,struct bt_conn ** conn)29 int bt_micp_mic_ctlr_conn_get(const struct bt_micp_mic_ctlr *mic_ctlr, struct bt_conn **conn)
30 {
31 	*conn = mic_ctlr->conn;
32 
33 	return 0;
34 }
35 
bt_micp_mic_ctlr_mute(struct bt_micp_mic_ctlr * mic_ctlr)36 int bt_micp_mic_ctlr_mute(struct bt_micp_mic_ctlr *mic_ctlr)
37 {
38 	if (micp_cb != NULL && micp_cb->mute_written != NULL) {
39 		micp_cb->mute_written(mic_ctlr, 0);
40 	}
41 
42 	return 0;
43 }
44 
bt_micp_mic_ctlr_unmute(struct bt_micp_mic_ctlr * mic_ctlr)45 int bt_micp_mic_ctlr_unmute(struct bt_micp_mic_ctlr *mic_ctlr)
46 {
47 	if (micp_cb != NULL && micp_cb->unmute_written != NULL) {
48 		micp_cb->unmute_written(mic_ctlr, 0);
49 	}
50 
51 	return 0;
52 }
53 
bt_micp_mic_ctlr_discover(struct bt_conn * conn,struct bt_micp_mic_ctlr ** mic_ctlr)54 int bt_micp_mic_ctlr_discover(struct bt_conn *conn, struct bt_micp_mic_ctlr **mic_ctlr)
55 {
56 	for (size_t i = 0; i < ARRAY_SIZE(mic_ctlrs); i++) {
57 		if (mic_ctlrs[i].conn == NULL) {
58 			for (size_t j = 0U; j < ARRAY_SIZE(mic_ctlrs[i].aics); j++) {
59 				const int err = bt_aics_discover(conn, mic_ctlrs[i].aics[j], NULL);
60 
61 				if (err != 0) {
62 					return err;
63 				}
64 			}
65 
66 			mic_ctlrs[i].conn = conn;
67 			*mic_ctlr = &mic_ctlrs[i];
68 
69 			return 0;
70 		}
71 	}
72 
73 	return -ENOMEM;
74 }
75 
bt_micp_mic_ctlr_cb_register(struct bt_micp_mic_ctlr_cb * cb)76 int bt_micp_mic_ctlr_cb_register(struct bt_micp_mic_ctlr_cb *cb)
77 {
78 	micp_cb = cb;
79 
80 	if (IS_ENABLED(CONFIG_BT_MICP_MIC_CTLR_AICS)) {
81 		for (size_t i = 0U; i < ARRAY_SIZE(mic_ctlrs); i++) {
82 			for (size_t j = 0U; j < ARRAY_SIZE(mic_ctlrs[i].aics); j++) {
83 				bt_aics_client_cb_register(mic_ctlrs[i].aics[j], &cb->aics_cb);
84 			}
85 		}
86 	}
87 
88 	return 0;
89 }
90 
bt_micp_mic_ctlr_included_get(struct bt_micp_mic_ctlr * mic_ctlr,struct bt_micp_included * included)91 int bt_micp_mic_ctlr_included_get(struct bt_micp_mic_ctlr *mic_ctlr,
92 				  struct bt_micp_included *included)
93 {
94 	included->aics_cnt = ARRAY_SIZE(mic_ctlr->aics);
95 	included->aics = mic_ctlr->aics;
96 
97 	return 0;
98 }
99 
mock_bt_micp_init(void)100 void mock_bt_micp_init(void)
101 {
102 	if (IS_ENABLED(CONFIG_BT_MICP_MIC_CTLR_AICS)) {
103 		for (size_t i = 0U; i < ARRAY_SIZE(mic_ctlrs); i++) {
104 			for (size_t j = 0U; j < ARRAY_SIZE(mic_ctlrs[i].aics); j++) {
105 				mic_ctlrs[i].aics[j] = bt_aics_client_free_instance_get();
106 
107 				__ASSERT(mic_ctlrs[i].aics[j],
108 					 "Could not allocate AICS client instance");
109 			}
110 		}
111 	}
112 }
113 
mock_bt_micp_cleanup(void)114 void mock_bt_micp_cleanup(void)
115 {
116 	memset(mic_ctlrs, 0, sizeof(mic_ctlrs));
117 }
118