1 /* btp_core.c - Bluetooth Core service */
2
3 /*
4 * Copyright (c) 2015-2016 Intel Corporation
5 * Copyright (c) 2023 Codecoup
6 *
7 * SPDX-License-Identifier: Apache-2.0
8 */
9
10 #include <zephyr/sys/atomic.h>
11 #include <zephyr/types.h>
12 #include <string.h>
13
14 #include <zephyr/toolchain.h>
15 #include <zephyr/bluetooth/bluetooth.h>
16 #include <zephyr/bluetooth/conn.h>
17 #include <zephyr/bluetooth/gatt.h>
18 #include <zephyr/bluetooth/hci.h>
19
20 #include <zephyr/sys/byteorder.h>
21 #include <zephyr/net/buf.h>
22
23 #include <hci_core.h>
24
25 #include <zephyr/logging/log.h>
26 #define LOG_MODULE_NAME bttester_core
27 LOG_MODULE_REGISTER(LOG_MODULE_NAME, CONFIG_BTTESTER_LOG_LEVEL);
28
29 #include "btp/btp.h"
30
31 static ATOMIC_DEFINE(registered_services, BTP_SERVICE_ID_MAX);
32
supported_commands(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)33 static uint8_t supported_commands(const void *cmd, uint16_t cmd_len,
34 void *rsp, uint16_t *rsp_len)
35 {
36 struct btp_core_read_supported_commands_rp *rp = rsp;
37
38 tester_set_bit(rp->data, BTP_CORE_READ_SUPPORTED_COMMANDS);
39 tester_set_bit(rp->data, BTP_CORE_READ_SUPPORTED_SERVICES);
40 tester_set_bit(rp->data, BTP_CORE_REGISTER_SERVICE);
41 tester_set_bit(rp->data, BTP_CORE_UNREGISTER_SERVICE);
42
43 *rsp_len = sizeof(*rp) + 1;
44
45 return BTP_STATUS_SUCCESS;
46 }
47
supported_services(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)48 static uint8_t supported_services(const void *cmd, uint16_t cmd_len,
49 void *rsp, uint16_t *rsp_len)
50 {
51 struct btp_core_read_supported_services_rp *rp = rsp;
52
53 /* octet 0 */
54 tester_set_bit(rp->data, BTP_SERVICE_ID_CORE);
55 tester_set_bit(rp->data, BTP_SERVICE_ID_GAP);
56 tester_set_bit(rp->data, BTP_SERVICE_ID_GATT);
57 #if defined(CONFIG_BT_L2CAP_DYNAMIC_CHANNEL)
58 tester_set_bit(rp->data, BTP_SERVICE_ID_L2CAP);
59 #endif /* CONFIG_BT_L2CAP_DYNAMIC_CHANNEL */
60 #if defined(CONFIG_BT_MESH)
61 tester_set_bit(rp->data, BTP_SERVICE_ID_MESH);
62 #endif /* CONFIG_BT_MESH */
63
64 /* octet 1 */
65 #if defined(CONFIG_BT_VCP_VOL_REND)
66 tester_set_bit(rp->data, BTP_SERVICE_ID_VCS);
67 #endif /* CONFIG_BT_VCP_VOL_REND */
68 #if defined(CONFIG_BT_IAS) || defined(CONFIG_BT_IAS_CLIENT)
69 tester_set_bit(rp->data, BTP_SERVICE_ID_IAS);
70 #endif /* CONFIG_BT_IAS */
71 #if defined(CONFIG_BT_AICS) || defined(CONFIG_BT_AICS_CLIENT)
72 tester_set_bit(rp->data, BTP_SERVICE_ID_AICS);
73 #endif /*CONFIG_BT_AICS */
74 #if defined(CONFIG_BT_VOCS) || defined(CONFIG_BT_VOCS_CLIENT)
75 tester_set_bit(rp->data, BTP_SERVICE_ID_VOCS);
76 #endif /* CONFIG_BT_VOCS */
77 #if defined(CONFIG_BT_HAS) || defined(CONFIG_BT_HAS_CLIENT)
78 tester_set_bit(rp->data, BTP_SERVICE_ID_HAS);
79 #endif /* CONFIG_BT_HAS */
80 #if defined(CONFIG_BT_CSIP_SET_MEMBER)
81 tester_set_bit(rp->data, BTP_SERVICE_ID_CSIS);
82 #endif /* CONFIG_BT_CSIP_SET_MEMBER */
83 #if defined(CONFIG_BT_MICP_MIC_DEV) || defined(CONFIG_BT_MICP_MIC_CTLR)
84 tester_set_bit(rp->data, BTP_SERVICE_ID_MICP);
85 #endif /* CONFIG_BT_MICP_MIC_DEV */
86 #if defined(CONFIG_BT_TBS_CLIENT)
87 tester_set_bit(rp->data, BTP_SERVICE_ID_CCP);
88 #endif /* CONFIG_BT_TBS_CLIENT */
89 #if defined(CONFIG_BT_VCP_VOL_CTLR)
90 tester_set_bit(rp->data, BTP_SERVICE_ID_VCP);
91 #endif /* CONFIG_BT_VCP_VOL_CTLR */
92 #if defined(CONFIG_BT_CAP_ACCEPTOR)
93 tester_set_bit(rp->data, BTP_SERVICE_ID_CAS);
94 #endif /* CONFIG_BT_CAP_ACCEPTOR */
95 #if defined(CONFIG_BT_MCC)
96 tester_set_bit(rp->data, BTP_SERVICE_ID_MCP);
97 #endif /* CONFIG_BT_MCC */
98 #if defined(CONFIG_BT_MCS)
99 tester_set_bit(rp->data, BTP_SERVICE_ID_GMCS);
100 #endif /* CONFIG_BT_MCS */
101 #if defined(CONFIG_BT_HAS)
102 tester_set_bit(rp->data, BTP_SERVICE_ID_HAP);
103 #endif /* CONFIG_BT_HAS */
104
105 *rsp_len = sizeof(*rp) + 2;
106
107 return BTP_STATUS_SUCCESS;
108 }
109
register_service(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)110 static uint8_t register_service(const void *cmd, uint16_t cmd_len,
111 void *rsp, uint16_t *rsp_len)
112 {
113 const struct btp_core_register_service_cmd *cp = cmd;
114 uint8_t status;
115
116 /* invalid service */
117 if ((cp->id == BTP_SERVICE_ID_CORE) || (cp->id > BTP_SERVICE_ID_MAX)) {
118 return BTP_STATUS_FAILED;
119 }
120
121 /* already registered */
122 if (atomic_test_bit(registered_services, cp->id)) {
123 return BTP_STATUS_FAILED;
124 }
125
126 switch (cp->id) {
127 case BTP_SERVICE_ID_GAP:
128 status = tester_init_gap();
129 break;
130 case BTP_SERVICE_ID_GATT:
131 status = tester_init_gatt();
132 break;
133 #if defined(CONFIG_BT_L2CAP_DYNAMIC_CHANNEL)
134 case BTP_SERVICE_ID_L2CAP:
135 status = tester_init_l2cap();
136 #endif /* CONFIG_BT_L2CAP_DYNAMIC_CHANNEL */
137 break;
138 #if defined(CONFIG_BT_MESH)
139 case BTP_SERVICE_ID_MESH:
140 status = tester_init_mesh();
141 break;
142 case BTP_SERVICE_ID_MESH_MDL:
143 status = tester_init_mmdl();
144 break;
145 #endif /* CONFIG_BT_MESH */
146 #if defined(CONFIG_BT_VCP_VOL_REND) || defined(CONFIG_BT_VCP_VOL_CTLR)
147 case BTP_SERVICE_ID_VCS:
148 status = tester_init_vcs();
149 break;
150 case BTP_SERVICE_ID_VOCS:
151 status = tester_init_vocs();
152 break;
153 case BTP_SERVICE_ID_AICS:
154 status = tester_init_aics();
155 break;
156 case BTP_SERVICE_ID_VCP:
157 status = tester_init_vcp();
158 break;
159 #endif /* CONFIG_BT_VCP_VOL_REND */
160 #if defined(CONFIG_BT_IAS)
161 case BTP_SERVICE_ID_IAS:
162 status = tester_init_ias();
163 break;
164 #endif /* CONFIG_BT_IAS */
165 #if defined(CONFIG_BT_BAP_UNICAST_CLIENT) || defined(CONFIG_BT_BAP_UNICAST_SERVER) || \
166 defined(CONFIG_BT_BAP_BROADCAST_SOURCE) || defined(CONFIG_BT_BAP_BROADCAST_SINK)
167 case BTP_SERVICE_ID_PACS:
168 status = tester_init_pacs();
169 break;
170 case BTP_SERVICE_ID_ASCS:
171 status = tester_init_ascs();
172 break;
173 case BTP_SERVICE_ID_BAP:
174 status = tester_init_bap();
175 break;
176 #endif /* CONFIG_BT_BAP_UNICAST_CLIENT || CONFIG_BT_BAP_UNICAST_SERVER || \
177 * CONFIG_BT_BAP_BROADCAST_SOURCE || CONFIG_BT_BAP_BROADCAST_SINK
178 */
179 #if defined(CONFIG_BT_MICP_MIC_DEV) || defined(CONFIG_BT_MICP_MIC_CTLR)
180 case BTP_SERVICE_ID_MICP:
181 status = tester_init_micp();
182 break;
183 case BTP_SERVICE_ID_MICS:
184 status = tester_init_mics();
185 break;
186 #endif /* CONFIG_BT_MICP_MIC_DEV or CONFIG_BT_MICP_MIC_CTLR */
187 #if defined(CONFIG_BT_HAS)
188 case BTP_SERVICE_ID_HAS:
189 status = tester_init_has();
190 break;
191 #endif /* CONFIG_BT_HAS */
192 #if defined(CONFIG_BT_CSIP_SET_MEMBER)
193 case BTP_SERVICE_ID_CSIS:
194 status = tester_init_csis();
195 break;
196 #endif /* CONFIG_BT_CSIP_SET_MEMBER */
197 #if defined(CONFIG_BT_CSIP_SET_COORDINATOR)
198 case BTP_SERVICE_ID_CSIP:
199 status = tester_init_csip();
200 break;
201 #endif /* CONFIG_BT_CSIP_SET_COORDINATOR */
202 #if defined(CONFIG_BT_TBS_CLIENT)
203 case BTP_SERVICE_ID_CCP:
204 status = tester_init_ccp();
205 break;
206 #endif /* CONFIG_BT_TBS_CLIENT */
207 #if defined(CONFIG_BT_CAP_ACCEPTOR)
208 case BTP_SERVICE_ID_CAS:
209 status = tester_init_cas();
210 break;
211 #endif /* CONFIG_BT_CAP_ACCEPTOR */
212 #if defined(CONFIG_BT_CAP_INITIATOR)
213 case BTP_SERVICE_ID_CAP:
214 status = tester_init_cap();
215 break;
216 #endif /* CONFIG_BT_CAP_INITIATOR */
217 #if defined(CONFIG_BT_MCC)
218 case BTP_SERVICE_ID_MCP:
219 status = tester_init_mcp();
220 break;
221 #endif /* CONFIG_BT_MCC */
222 #if defined(CONFIG_BT_MCS)
223 case BTP_SERVICE_ID_GMCS:
224 status = tester_init_mcs();
225 break;
226 #endif /* CONFIG_BT_MCS */
227 #if defined(CONFIG_BT_HAS)
228 case BTP_SERVICE_ID_HAP:
229 status = tester_init_hap();
230 break;
231 #endif /* CONFIG_BT_HAS */
232 default:
233 LOG_WRN("unknown id: 0x%02x", cp->id);
234 status = BTP_STATUS_FAILED;
235 break;
236 }
237
238 if (status == BTP_STATUS_SUCCESS) {
239 atomic_set_bit(registered_services, cp->id);
240 }
241
242 return status;
243 }
244
unregister_service(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)245 static uint8_t unregister_service(const void *cmd, uint16_t cmd_len,
246 void *rsp, uint16_t *rsp_len)
247 {
248 const struct btp_core_unregister_service_cmd *cp = cmd;
249 uint8_t status;
250
251 /* invalid service ID */
252 if ((cp->id == BTP_SERVICE_ID_CORE) || (cp->id > BTP_SERVICE_ID_MAX)) {
253 return BTP_STATUS_FAILED;
254 }
255
256 /* not registered */
257 if (!atomic_test_bit(registered_services, cp->id)) {
258 return BTP_STATUS_FAILED;
259 }
260
261 switch (cp->id) {
262 case BTP_SERVICE_ID_GAP:
263 status = tester_unregister_gap();
264 break;
265 case BTP_SERVICE_ID_GATT:
266 status = tester_unregister_gatt();
267 break;
268 #if defined(CONFIG_BT_L2CAP_DYNAMIC_CHANNEL)
269 case BTP_SERVICE_ID_L2CAP:
270 status = tester_unregister_l2cap();
271 break;
272 #endif /* CONFIG_BT_L2CAP_DYNAMIC_CHANNEL */
273 #if defined(CONFIG_BT_MESH)
274 case BTP_SERVICE_ID_MESH:
275 status = tester_unregister_mesh();
276 break;
277 case BTP_SERVICE_ID_MESH_MDL:
278 status = tester_unregister_mmdl();
279 break;
280 #endif /* CONFIG_BT_MESH */
281 #if defined(CONFIG_BT_VCP_VOL_REND)
282 case BTP_SERVICE_ID_VCS:
283 status = tester_unregister_vcs();
284 break;
285 case BTP_SERVICE_ID_AICS:
286 status = tester_unregister_aics();
287 break;
288 case BTP_SERVICE_ID_VOCS:
289 status = tester_unregister_vocs();
290 break;
291 #endif /* CONFIG_BT_VCP_VOL_REND */
292 #if defined(CONFIG_BT_IAS)
293 case BTP_SERVICE_ID_IAS:
294 status = tester_unregister_ias();
295 break;
296 #endif /* CONFIG_BT_IAS */
297 #if defined(CONFIG_BT_BAP_UNICAST_CLIENT) || defined(CONFIG_BT_BAP_UNICAST_SERVER) || \
298 defined(CONFIG_BT_BAP_BROADCAST_SOURCE) || defined(CONFIG_BT_BAP_BROADCAST_SINK)
299 case BTP_SERVICE_ID_PACS:
300 status = tester_unregister_pacs();
301 break;
302 case BTP_SERVICE_ID_ASCS:
303 status = tester_unregister_ascs();
304 break;
305 case BTP_SERVICE_ID_BAP:
306 status = tester_unregister_bap();
307 break;
308 #endif /* CONFIG_BT_BAP_UNICAST_CLIENT || CONFIG_BT_BAP_UNICAST_SERVER || \
309 * CONFIG_BT_BAP_BROADCAST_SOURCE || CONFIG_BT_BAP_BROADCAST_SINK
310 */
311 #if defined(CONFIG_BT_MICP_MIC_DEV) || defined(CONFIG_BT_MICP_MIC_CTLR)
312 case BTP_SERVICE_ID_MICP:
313 status = tester_unregister_micp();
314 break;
315 case BTP_SERVICE_ID_MICS:
316 status = tester_unregister_mics();
317 break;
318 #endif /* CONFIG_BT_MICP_MIC_DEV or CONFIG_BT_MICP_MIC_CTLR */
319 #if defined(CONFIG_BT_HAS)
320 case BTP_SERVICE_ID_HAS:
321 status = tester_unregister_has();
322 break;
323 #endif /* CONFIG_BT_HAS */
324 #if defined(CONFIG_BT_CSIP_SET_MEMBER)
325 case BTP_SERVICE_ID_CSIS:
326 status = tester_unregister_csis();
327 break;
328 #endif /* CONFIG_BT_CSIP_SET_MEMBER */
329 #if defined(CONFIG_BT_CSIP_SET_COORDINATOR)
330 case BTP_SERVICE_ID_CSIP:
331 status = tester_unregister_csip();
332 break;
333 #endif /* CONFIG_BT_CSIP_SET_COORDINATOR */
334 #if defined(CONFIG_BT_TBS_CLIENT)
335 case BTP_SERVICE_ID_CCP:
336 status = tester_unregister_ccp();
337 break;
338 #endif /* CONFIG_BT_TBS_CLIENT */
339 #if defined(CONFIG_BT_CAP_ACCEPTOR)
340 case BTP_SERVICE_ID_CAS:
341 status = tester_unregister_cas();
342 break;
343 #endif /* CONFIG_BT_CAP_ACCEPTOR */
344 #if defined(CONFIG_BT_CAP_INITIATOR)
345 case BTP_SERVICE_ID_CAP:
346 status = tester_unregister_cap();
347 break;
348 #endif /* CONFIG_BT_CAP_INITIATOR */
349 #if defined(CONFIG_BT_MCC)
350 case BTP_SERVICE_ID_MCP:
351 status = tester_unregister_mcp();
352 break;
353 #endif /* CONFIG_BT_MCC */
354 #if defined(CONFIG_BT_MCS)
355 case BTP_SERVICE_ID_GMCS:
356 status = tester_unregister_mcs();
357 break;
358 #endif /* CONFIG_BT_MCS */
359 #if defined(CONFIG_BT_HAS)
360 case BTP_SERVICE_ID_HAP:
361 status = tester_unregister_hap();
362 break;
363 #endif /* CONFIG_BT_HAS */
364 default:
365 LOG_WRN("unknown id: 0x%x", cp->id);
366 status = BTP_STATUS_FAILED;
367 break;
368 }
369
370 if (status == BTP_STATUS_SUCCESS) {
371 atomic_clear_bit(registered_services, cp->id);
372 }
373
374 return BTP_STATUS_FAILED;
375 }
376
377 static const struct btp_handler handlers[] = {
378 {
379 .opcode = BTP_CORE_READ_SUPPORTED_COMMANDS,
380 .index = BTP_INDEX_NONE,
381 .expect_len = 0,
382 .func = supported_commands,
383 },
384 {
385 .opcode = BTP_CORE_READ_SUPPORTED_SERVICES,
386 .index = BTP_INDEX_NONE,
387 .expect_len = 0,
388 .func = supported_services,
389 },
390 {
391 .opcode = BTP_CORE_REGISTER_SERVICE,
392 .index = BTP_INDEX_NONE,
393 .expect_len = sizeof(struct btp_core_register_service_cmd),
394 .func = register_service,
395 },
396 {
397 .opcode = BTP_CORE_UNREGISTER_SERVICE,
398 .index = BTP_INDEX_NONE,
399 .expect_len = sizeof(struct btp_core_unregister_service_cmd),
400 .func = unregister_service,
401 },
402 };
403
tester_init_core(void)404 void tester_init_core(void)
405 {
406 tester_register_command_handlers(BTP_SERVICE_ID_CORE, handlers,
407 ARRAY_SIZE(handlers));
408 atomic_set_bit(registered_services, BTP_SERVICE_ID_CORE);
409 }
410