1 /* btp_cas.c - Bluetooth CAS Tester */
2 
3 /*
4  * Copyright (c) 2023 Oticon
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 #include <zephyr/bluetooth/audio/cap.h>
9 
10 #include "btp/btp.h"
11 #include "zephyr/sys/byteorder.h"
12 #include <stdint.h>
13 
14 #include <zephyr/logging/log.h>
15 #define LOG_MODULE_NAME bttester_cas
16 LOG_MODULE_REGISTER(LOG_MODULE_NAME, CONFIG_BTTESTER_LOG_LEVEL);
17 
18 static struct bt_csip_set_member_svc_inst *csis_svc_inst;
19 
cas_supported_commands(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)20 static uint8_t cas_supported_commands(const void *cmd, uint16_t cmd_len,
21 				      void *rsp, uint16_t *rsp_len)
22 {
23 	struct btp_cas_read_supported_commands_rp *rp = rsp;
24 
25 	/* octet 0 */
26 	tester_set_bit(rp->data, BTP_CAS_READ_SUPPORTED_COMMANDS);
27 	tester_set_bit(rp->data, BTP_CAS_SET_MEMBER_LOCK);
28 	tester_set_bit(rp->data, BTP_CAS_GET_MEMBER_RSI);
29 
30 	*rsp_len = sizeof(*rp) + 1;
31 
32 	return BTP_STATUS_SUCCESS;
33 }
34 
cas_set_member_lock(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)35 static uint8_t cas_set_member_lock(const void *cmd, uint16_t cmd_len,
36 				   void *rsp, uint16_t *rsp_len)
37 {
38 	const struct btp_cas_set_member_lock_cmd *cp = cmd;
39 	int err = -EINVAL;
40 
41 	if (csis_svc_inst) {
42 		err = bt_csip_set_member_lock(csis_svc_inst, cp->lock, cp->force);
43 	}
44 
45 	return BTP_STATUS_VAL(err);
46 }
47 
cas_get_member_rsi(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)48 static uint8_t cas_get_member_rsi(const void *cmd, uint16_t cmd_len,
49 				  void *rsp, uint16_t *rsp_len)
50 {
51 	struct btp_cas_get_member_rsi_rp *rp = rsp;
52 	int err = -EINVAL;
53 
54 	if (csis_svc_inst) {
55 		err = bt_csip_set_member_generate_rsi(csis_svc_inst, rp->rsi);
56 	}
57 	*rsp_len = sizeof(*rp);
58 
59 	return BTP_STATUS_VAL(err);
60 }
61 
62 static const struct btp_handler cas_handlers[] = {
63 	{
64 		.opcode = BTP_CAS_READ_SUPPORTED_COMMANDS,
65 		.index = BTP_INDEX_NONE,
66 		.expect_len = 0,
67 		.func = cas_supported_commands
68 	},
69 	{
70 		.opcode = BTP_CAS_SET_MEMBER_LOCK,
71 		.expect_len = sizeof(struct btp_cas_set_member_lock_cmd),
72 		.func = cas_set_member_lock
73 	},
74 	{
75 		.opcode = BTP_CAS_GET_MEMBER_RSI,
76 		.expect_len = sizeof(struct btp_cas_get_member_rsi_cmd),
77 		.func = cas_get_member_rsi
78 	}
79 };
80 
tester_init_cas(void)81 uint8_t tester_init_cas(void)
82 {
83 	tester_register_command_handlers(BTP_SERVICE_ID_CAS, cas_handlers,
84 					 ARRAY_SIZE(cas_handlers));
85 
86 #if defined(CONFIG_BT_CAP_ACCEPTOR) && defined(CONFIG_BT_CAP_ACCEPTOR_SET_MEMBER)
87 	struct bt_csip_set_member_register_param register_params = {
88 		.set_size = 2,
89 		.set_sirk = { 0xB8, 0x03, 0xEA, 0xC6, 0xAF, 0xBB, 0x65, 0xA2,
90 			      0x5A, 0x41, 0xF1, 0x53, 0x05, 0x68, 0x8E, 0x83 },
91 		.lockable = true,
92 		.rank = 1,
93 		.cb = NULL
94 	};
95 	int err = bt_cap_acceptor_register(&register_params, &csis_svc_inst);
96 #else
97 	int err = 0;
98 #endif /* CONFIG_BT_CAP_ACCEPTOR && CONFIG_BT_CAP_ACCEPTOR_SET_MEMBER */
99 
100 	return BTP_STATUS_VAL(err);
101 }
102 
tester_unregister_cas(void)103 uint8_t tester_unregister_cas(void)
104 {
105 	return BTP_STATUS_SUCCESS;
106 }
107