1 /* btp_ccp.c - Bluetooth CCP Tester */
2 
3 /*
4  * Copyright (c) 2023 Oticon
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 #include "btp/btp.h"
9 #include "zephyr/sys/byteorder.h"
10 #include <stdint.h>
11 
12 #include <../../subsys/bluetooth/audio/tbs_internal.h>
13 
14 #include <zephyr/bluetooth/audio/tbs.h>
15 #include <zephyr/logging/log.h>
16 
17 #define LOG_MODULE_NAME bttester_ccp
18 LOG_MODULE_REGISTER(LOG_MODULE_NAME, CONFIG_BTTESTER_LOG_LEVEL);
19 
20 struct btp_ccp_chrc_handles_ev tbs_handles;
21 struct bt_tbs_instance *tbs_inst;
22 static uint8_t call_index;
23 static uint8_t inst_ccid;
24 static bool send_ev;
25 
ccp_supported_commands(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)26 static uint8_t ccp_supported_commands(const void *cmd, uint16_t cmd_len,
27 				      void *rsp, uint16_t *rsp_len)
28 {
29 	struct btp_ccp_read_supported_commands_rp *rp = rsp;
30 
31 	/* octet 0 */
32 	tester_set_bit(rp->data, BTP_CCP_READ_SUPPORTED_COMMANDS);
33 	tester_set_bit(rp->data, BTP_CCP_DISCOVER_TBS);
34 	tester_set_bit(rp->data, BTP_CCP_ACCEPT_CALL);
35 	tester_set_bit(rp->data, BTP_CCP_TERMINATE_CALL);
36 	tester_set_bit(rp->data, BTP_CCP_ORIGINATE_CALL);
37 	tester_set_bit(rp->data, BTP_CCP_READ_CALL_STATE);
38 	tester_set_bit(rp->data, BTP_CCP_READ_BEARER_NAME);
39 
40 	/* octet 1 */
41 	tester_set_bit(rp->data, BTP_CCP_READ_BEARER_UCI);
42 	tester_set_bit(rp->data, BTP_CCP_READ_BEARER_TECH);
43 	tester_set_bit(rp->data, BTP_CCP_READ_URI_LIST);
44 	tester_set_bit(rp->data, BTP_CCP_READ_SIGNAL_STRENGTH);
45 	tester_set_bit(rp->data, BTP_CCP_READ_SIGNAL_INTERVAL);
46 	tester_set_bit(rp->data, BTP_CCP_READ_CURRENT_CALLS);
47 	tester_set_bit(rp->data, BTP_CCP_READ_CCID);
48 
49 	/* octet 2 */
50 	tester_set_bit(rp->data, BTP_CCP_READ_CALL_URI);
51 	tester_set_bit(rp->data, BTP_CCP_READ_STATUS_FLAGS);
52 	tester_set_bit(rp->data, BTP_CCP_READ_OPTIONAL_OPCODES);
53 	tester_set_bit(rp->data, BTP_CCP_READ_FRIENDLY_NAME);
54 	tester_set_bit(rp->data, BTP_CCP_READ_REMOTE_URI);
55 	tester_set_bit(rp->data, BTP_CCP_SET_SIGNAL_INTERVAL);
56 	tester_set_bit(rp->data, BTP_CCP_HOLD_CALL);
57 
58 	/* octet 3 */
59 	tester_set_bit(rp->data, BTP_CCP_RETRIEVE_CALL);
60 	tester_set_bit(rp->data, BTP_CCP_JOIN_CALLS);
61 
62 	*rsp_len = sizeof(*rp) + 1;
63 
64 	return BTP_STATUS_SUCCESS;
65 }
66 
tbs_client_discovered_ev(int err,uint8_t tbs_count,bool gtbs_found)67 static void tbs_client_discovered_ev(int err, uint8_t tbs_count, bool gtbs_found)
68 {
69 	struct btp_ccp_discovered_ev ev;
70 
71 	ev.status = sys_cpu_to_le32(err);
72 	ev.tbs_count = tbs_count;
73 	ev.gtbs_found = gtbs_found;
74 
75 	tester_event(BTP_SERVICE_ID_CCP, BTP_CCP_EV_DISCOVERED, &ev, sizeof(ev));
76 }
77 
tbs_chrc_handles_ev(struct btp_ccp_chrc_handles_ev * tbs_handles)78 static void tbs_chrc_handles_ev(struct btp_ccp_chrc_handles_ev *tbs_handles)
79 {
80 	struct btp_ccp_chrc_handles_ev ev;
81 
82 	ev.provider_name = sys_cpu_to_le16(tbs_handles->provider_name);
83 	ev.bearer_uci = sys_cpu_to_le16(tbs_handles->bearer_uci);
84 	ev.bearer_technology = sys_cpu_to_le16(tbs_handles->bearer_technology);
85 	ev.uri_list = sys_cpu_to_le16(tbs_handles->uri_list);
86 	ev.signal_strength = sys_cpu_to_le16(tbs_handles->signal_strength);
87 	ev.signal_interval = sys_cpu_to_le16(tbs_handles->signal_interval);
88 	ev.current_calls = sys_cpu_to_le16(tbs_handles->current_calls);
89 	ev.ccid = sys_cpu_to_le16(tbs_handles->ccid);
90 	ev.status_flags = sys_cpu_to_le16(tbs_handles->status_flags);
91 	ev.bearer_uri = sys_cpu_to_le16(tbs_handles->bearer_uri);
92 	ev.call_state = sys_cpu_to_le16(tbs_handles->call_state);
93 	ev.control_point = sys_cpu_to_le16(tbs_handles->control_point);
94 	ev.optional_opcodes = sys_cpu_to_le16(tbs_handles->optional_opcodes);
95 	ev.termination_reason = sys_cpu_to_le16(tbs_handles->termination_reason);
96 	ev.incoming_call = sys_cpu_to_le16(tbs_handles->incoming_call);
97 	ev.friendly_name = sys_cpu_to_le16(tbs_handles->friendly_name);
98 
99 	tester_event(BTP_SERVICE_ID_CCP, BTP_CCP_EV_CHRC_HANDLES, &ev, sizeof(ev));
100 }
101 
tbs_client_chrc_val_ev(struct bt_conn * conn,uint8_t status,uint8_t inst_index,uint32_t value)102 static void tbs_client_chrc_val_ev(struct bt_conn *conn, uint8_t status, uint8_t inst_index,
103 				   uint32_t value)
104 {
105 	struct btp_ccp_chrc_val_ev ev;
106 
107 	bt_addr_le_copy(&ev.address, bt_conn_get_dst(conn));
108 
109 	ev.status = status;
110 	ev.inst_index = inst_index;
111 	ev.value = value;
112 
113 	tester_event(BTP_SERVICE_ID_CCP, BTP_CCP_EV_CHRC_VAL, &ev, sizeof(ev));
114 }
115 
tbs_client_chrc_str_ev(struct bt_conn * conn,uint8_t status,uint8_t inst_index,uint8_t data_len,const char * data)116 static void tbs_client_chrc_str_ev(struct bt_conn *conn, uint8_t status, uint8_t inst_index,
117 				   uint8_t data_len, const char *data)
118 {
119 	struct btp_ccp_chrc_str_ev *ev;
120 
121 	tester_rsp_buffer_lock();
122 	tester_rsp_buffer_allocate(sizeof(*ev) + data_len, (uint8_t **)&ev);
123 	bt_addr_le_copy(&ev->address, bt_conn_get_dst(conn));
124 	ev->status = status;
125 	ev->inst_index = inst_index;
126 	ev->data_len = data_len;
127 	memcpy(ev->data, data, data_len);
128 
129 	tester_event(BTP_SERVICE_ID_CCP, BTP_CCP_EV_CHRC_STR, ev, sizeof(*ev) + data_len);
130 
131 	tester_rsp_buffer_free();
132 	tester_rsp_buffer_unlock();
133 }
134 
tbs_client_cp_ev(struct bt_conn * conn,uint8_t status)135 static void tbs_client_cp_ev(struct bt_conn *conn, uint8_t status)
136 {
137 	struct btp_ccp_cp_ev ev;
138 
139 	bt_addr_le_copy(&ev.address, bt_conn_get_dst(conn));
140 
141 	ev.status = status;
142 
143 	tester_event(BTP_SERVICE_ID_CCP, BTP_CCP_EV_CP, &ev, sizeof(ev));
144 }
145 
tbs_client_current_calls_ev(struct bt_conn * conn,uint8_t status)146 static void tbs_client_current_calls_ev(struct bt_conn *conn, uint8_t status)
147 {
148 	struct btp_ccp_current_calls_ev ev;
149 
150 	bt_addr_le_copy(&ev.address, bt_conn_get_dst(conn));
151 
152 	ev.status = status;
153 
154 	tester_event(BTP_SERVICE_ID_CCP, BTP_CCP_EV_CURRENT_CALLS, &ev, sizeof(ev));
155 }
156 
tbs_client_discover_cb(struct bt_conn * conn,int err,uint8_t tbs_count,bool gtbs_found)157 static void tbs_client_discover_cb(struct bt_conn *conn, int err, uint8_t tbs_count,
158 				   bool gtbs_found)
159 {
160 	if (err) {
161 		LOG_DBG("Discovery Failed (%d)", err);
162 		return;
163 	}
164 
165 	LOG_DBG("Discovered TBS - err (%u) GTBS (%u)", err, gtbs_found);
166 
167 	bt_tbs_client_read_ccid(conn, 0xFF);
168 
169 	tbs_client_discovered_ev(err, tbs_count, gtbs_found);
170 
171 	send_ev = true;
172 }
173 
174 typedef struct bt_tbs_client_call_state bt_tbs_client_call_state_t;
175 
176 #define CALL_STATES_EV_SIZE sizeof(struct btp_ccp_call_states_ev) + \
177 			    sizeof(bt_tbs_client_call_state_t) * \
178 			    CONFIG_BT_TBS_CLIENT_MAX_CALLS
179 
tbs_client_call_states_ev(int err,uint8_t inst_index,uint8_t call_count,const bt_tbs_client_call_state_t * call_states)180 static void tbs_client_call_states_ev(int err,
181 				      uint8_t inst_index,
182 				      uint8_t call_count,
183 				      const bt_tbs_client_call_state_t *call_states)
184 {
185 	struct net_buf_simple *buf = NET_BUF_SIMPLE(CALL_STATES_EV_SIZE);
186 	struct btp_ccp_call_states_ev ev = {
187 		sys_cpu_to_le32(err), inst_index, call_count
188 	};
189 
190 	net_buf_simple_init(buf, 0);
191 	net_buf_simple_add_mem(buf, &ev, sizeof(ev));
192 
193 	for (uint8_t n = 0; n < call_count; n++, call_states++) {
194 		net_buf_simple_add_mem(buf, call_states, sizeof(bt_tbs_client_call_state_t));
195 	}
196 
197 	tester_event(BTP_SERVICE_ID_CCP, BTP_CCP_EV_CALL_STATES, buf->data, buf->len);
198 }
199 
tbs_client_call_states_cb(struct bt_conn * conn,int err,uint8_t inst_index,uint8_t call_count,const bt_tbs_client_call_state_t * call_states)200 static void tbs_client_call_states_cb(struct bt_conn *conn,
201 				      int err,
202 				      uint8_t inst_index,
203 				      uint8_t call_count,
204 				      const bt_tbs_client_call_state_t *call_states)
205 {
206 	LOG_DBG("Call states - err (%u) Call Count (%u)", err, call_count);
207 
208 	tbs_client_call_states_ev(err, inst_index, call_count, call_states);
209 }
210 
tbs_client_termination_reason_cb(struct bt_conn * conn,int err,uint8_t inst_index,uint8_t call_index,uint8_t reason)211 static void tbs_client_termination_reason_cb(struct bt_conn *conn,
212 					     int err,
213 					     uint8_t inst_index,
214 					     uint8_t call_index,
215 					     uint8_t reason)
216 {
217 	LOG_DBG("Termination reason - err (%u) Call Index (%u) Reason (%u)",
218 		err, call_index, reason);
219 }
220 
tbs_client_read_string_cb(struct bt_conn * conn,int err,uint8_t inst_index,const char * value)221 static void tbs_client_read_string_cb(struct bt_conn *conn, int err, uint8_t inst_index,
222 				      const char *value)
223 {
224 	LOG_DBG("TBS Client read string characteristic value cb");
225 
226 	uint8_t data_len = strlen(value);
227 
228 	tbs_client_chrc_str_ev(conn, err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS, inst_index,
229 			       data_len, value);
230 }
231 
tbs_client_read_val_cb(struct bt_conn * conn,int err,uint8_t inst_index,uint32_t value)232 static void tbs_client_read_val_cb(struct bt_conn *conn, int err, uint8_t inst_index,
233 				   uint32_t value)
234 {
235 	LOG_DBG("TBS Client read characteristic value cb");
236 
237 	tbs_client_chrc_val_ev(conn, err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS, inst_index,
238 			       value);
239 
240 	if (send_ev == true) {
241 		inst_ccid = value;
242 
243 		tbs_inst = bt_tbs_client_get_by_ccid(conn, inst_ccid);
244 
245 		tbs_handles.provider_name = tbs_inst->name_sub_params.value_handle;
246 		tbs_handles.bearer_uci = tbs_inst->bearer_uci_handle;
247 		tbs_handles.bearer_technology = tbs_inst->technology_sub_params.value_handle;
248 		tbs_handles.uri_list = tbs_inst->uri_list_handle;
249 		tbs_handles.signal_strength = tbs_inst->signal_strength_sub_params.value_handle;
250 		tbs_handles.signal_interval = tbs_inst->signal_interval_handle;
251 		tbs_handles.current_calls = tbs_inst->current_calls_sub_params.value_handle;
252 		tbs_handles.ccid = tbs_inst->ccid_handle;
253 		tbs_handles.status_flags = tbs_inst->status_flags_sub_params.value_handle;
254 		tbs_handles.bearer_uri = tbs_inst->in_target_uri_sub_params.value_handle;
255 		tbs_handles.call_state = tbs_inst->call_state_sub_params.value_handle;
256 		tbs_handles.control_point = tbs_inst->call_cp_sub_params.value_handle;
257 		tbs_handles.optional_opcodes = tbs_inst->optional_opcodes_handle;
258 		tbs_handles.termination_reason = tbs_inst->termination_reason_handle;
259 		tbs_handles.incoming_call = tbs_inst->incoming_call_sub_params.value_handle;
260 		tbs_handles.friendly_name = tbs_inst->friendly_name_sub_params.value_handle;
261 
262 		tbs_chrc_handles_ev(&tbs_handles);
263 		send_ev = false;
264 	}
265 }
266 
tbs_client_current_calls_cb(struct bt_conn * conn,int err,uint8_t inst_index,uint8_t call_count,const struct bt_tbs_client_call * calls)267 static void tbs_client_current_calls_cb(struct bt_conn *conn, int err, uint8_t inst_index,
268 					uint8_t call_count, const struct bt_tbs_client_call *calls)
269 {
270 	LOG_DBG("");
271 
272 	tbs_client_current_calls_ev(conn, err);
273 }
274 
tbs_client_cp_cb(struct bt_conn * conn,int err,uint8_t inst_index,uint8_t call_index)275 static void tbs_client_cp_cb(struct bt_conn *conn, int err, uint8_t inst_index, uint8_t call_index)
276 {
277 	LOG_DBG("");
278 
279 	tbs_client_cp_ev(conn, err);
280 }
281 
282 static struct bt_tbs_client_cb tbs_client_callbacks = {
283 	.discover = tbs_client_discover_cb,
284 	.originate_call = tbs_client_cp_cb,
285 	.terminate_call = tbs_client_cp_cb,
286 	.call_state = tbs_client_call_states_cb,
287 	.termination_reason = tbs_client_termination_reason_cb,
288 	.bearer_provider_name = tbs_client_read_string_cb,
289 	.bearer_uci = tbs_client_read_string_cb,
290 	.technology = tbs_client_read_val_cb,
291 	.uri_list = tbs_client_read_string_cb,
292 	.signal_strength = tbs_client_read_val_cb,
293 	.signal_interval = tbs_client_read_val_cb,
294 	.current_calls = tbs_client_current_calls_cb,
295 	.ccid = tbs_client_read_val_cb,
296 	.call_uri = tbs_client_read_string_cb,
297 	.status_flags = tbs_client_read_val_cb,
298 	.optional_opcodes = tbs_client_read_val_cb,
299 	.friendly_name = tbs_client_read_string_cb,
300 	.remote_uri = tbs_client_read_string_cb,
301 	.accept_call = tbs_client_cp_cb,
302 	.hold_call = tbs_client_cp_cb,
303 	.retrieve_call = tbs_client_cp_cb,
304 	.join_calls = tbs_client_cp_cb,
305 };
306 
ccp_discover_tbs(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)307 static uint8_t ccp_discover_tbs(const void *cmd, uint16_t cmd_len,
308 				void *rsp, uint16_t *rsp_len)
309 {
310 	const struct btp_ccp_discover_tbs_cmd *cp = cmd;
311 	struct bt_conn *conn;
312 	int err;
313 
314 	conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &cp->address);
315 	err = (conn) ? bt_tbs_client_discover(conn) : -ENOTCONN;
316 	if (conn) {
317 		bt_conn_unref(conn);
318 	}
319 
320 	return BTP_STATUS_VAL(err);
321 }
322 
ccp_accept_call(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)323 static uint8_t ccp_accept_call(const void *cmd, uint16_t cmd_len,
324 			       void *rsp, uint16_t *rsp_len)
325 {
326 	const struct btp_ccp_accept_call_cmd *cp = cmd;
327 	struct bt_conn *conn;
328 	int err;
329 
330 	conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &cp->address);
331 	err = (conn) ? bt_tbs_client_accept_call(conn, cp->inst_index, cp->call_id) : -ENOTCONN;
332 	if (conn) {
333 		bt_conn_unref(conn);
334 	}
335 
336 	return BTP_STATUS_VAL(err);
337 }
338 
ccp_terminate_call(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)339 static uint8_t ccp_terminate_call(const void *cmd, uint16_t cmd_len,
340 				  void *rsp, uint16_t *rsp_len)
341 {
342 	const struct btp_ccp_terminate_call_cmd *cp = cmd;
343 	struct bt_conn *conn;
344 	int err;
345 
346 	conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &cp->address);
347 	err = (conn) ? bt_tbs_client_terminate_call(conn, cp->inst_index, cp->call_id) :
348 		-ENOTCONN;
349 	if (conn) {
350 		bt_conn_unref(conn);
351 	}
352 
353 	return BTP_STATUS_VAL(err);
354 }
355 
ccp_originate_call(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)356 static uint8_t ccp_originate_call(const void *cmd, uint16_t cmd_len,
357 				  void *rsp, uint16_t *rsp_len)
358 {
359 	const struct btp_ccp_originate_call_cmd *cp = cmd;
360 	struct bt_conn *conn;
361 	int err;
362 
363 	conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &cp->address);
364 	err = (conn) ? bt_tbs_client_originate_call(conn, cp->inst_index, cp->uri) : -ENOTCONN;
365 	if (conn) {
366 		bt_conn_unref(conn);
367 	}
368 
369 	return BTP_STATUS_VAL(err);
370 }
371 
ccp_read_call_state(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)372 static uint8_t ccp_read_call_state(const void *cmd, uint16_t cmd_len,
373 				   void *rsp, uint16_t *rsp_len)
374 {
375 	const struct btp_ccp_read_call_state_cmd *cp = cmd;
376 	struct bt_conn *conn;
377 	int err;
378 
379 	conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &cp->address);
380 	err = (conn) ? bt_tbs_client_read_call_state(conn, cp->inst_index) : -ENOTCONN;
381 	if (conn) {
382 		bt_conn_unref(conn);
383 	}
384 
385 	return BTP_STATUS_VAL(err);
386 }
387 
ccp_read_bearer_name(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)388 static uint8_t ccp_read_bearer_name(const void *cmd, uint16_t cmd_len, void *rsp,
389 				    uint16_t *rsp_len)
390 {
391 	const struct btp_ccp_read_bearer_name_cmd *cp = cmd;
392 	struct bt_conn *conn;
393 	int err;
394 
395 	conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &cp->address);
396 	if (!conn) {
397 		LOG_ERR("Unknown connection");
398 		return BTP_STATUS_FAILED;
399 	}
400 
401 	err = bt_tbs_client_read_bearer_provider_name(conn, cp->inst_index);
402 	if (err) {
403 		return BTP_STATUS_FAILED;
404 	}
405 
406 	return BTP_STATUS_SUCCESS;
407 }
408 
ccp_read_bearer_uci(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)409 static uint8_t ccp_read_bearer_uci(const void *cmd, uint16_t cmd_len, void *rsp,
410 				    uint16_t *rsp_len)
411 {
412 	const struct btp_ccp_read_bearer_uci_cmd *cp = cmd;
413 	struct bt_conn *conn;
414 	int err;
415 
416 	conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &cp->address);
417 	if (!conn) {
418 		LOG_ERR("Unknown connection");
419 		return BTP_STATUS_FAILED;
420 	}
421 
422 	err = bt_tbs_client_read_bearer_uci(conn, cp->inst_index);
423 	if (err) {
424 		return BTP_STATUS_FAILED;
425 	}
426 
427 	return BTP_STATUS_SUCCESS;
428 }
429 
ccp_read_bearer_tech(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)430 static uint8_t ccp_read_bearer_tech(const void *cmd, uint16_t cmd_len, void *rsp,
431 				    uint16_t *rsp_len)
432 {
433 	const struct btp_ccp_read_bearer_technology_cmd *cp = cmd;
434 	struct bt_conn *conn;
435 	int err;
436 
437 	conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &cp->address);
438 	if (!conn) {
439 		LOG_ERR("Unknown connection");
440 		return BTP_STATUS_FAILED;
441 	}
442 
443 	err = bt_tbs_client_read_technology(conn, cp->inst_index);
444 	if (err) {
445 		return BTP_STATUS_FAILED;
446 	}
447 
448 	return BTP_STATUS_SUCCESS;
449 }
450 
ccp_read_uri_list(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)451 static uint8_t ccp_read_uri_list(const void *cmd, uint16_t cmd_len, void *rsp, uint16_t *rsp_len)
452 {
453 	const struct btp_ccp_read_uri_list_cmd *cp = cmd;
454 	struct bt_conn *conn;
455 	int err;
456 
457 	conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &cp->address);
458 	if (!conn) {
459 		LOG_ERR("Unknown connection");
460 		return BTP_STATUS_FAILED;
461 	}
462 
463 	err = bt_tbs_client_read_uri_list(conn, cp->inst_index);
464 	if (err) {
465 		return BTP_STATUS_FAILED;
466 	}
467 
468 	return BTP_STATUS_SUCCESS;
469 }
470 
ccp_read_signal_strength(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)471 static uint8_t ccp_read_signal_strength(const void *cmd, uint16_t cmd_len, void *rsp,
472 					uint16_t *rsp_len)
473 {
474 	const struct btp_ccp_read_signal_strength_cmd *cp = cmd;
475 	struct bt_conn *conn;
476 	int err;
477 
478 	conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &cp->address);
479 	if (!conn) {
480 		LOG_ERR("Unknown connection");
481 		return BTP_STATUS_FAILED;
482 	}
483 
484 	err = bt_tbs_client_read_signal_strength(conn, cp->inst_index);
485 	if (err) {
486 		return BTP_STATUS_FAILED;
487 	}
488 
489 	return BTP_STATUS_SUCCESS;
490 }
491 
ccp_read_signal_interval(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)492 static uint8_t ccp_read_signal_interval(const void *cmd, uint16_t cmd_len, void *rsp,
493 					uint16_t *rsp_len)
494 {
495 	const struct btp_ccp_read_signal_interval_cmd *cp = cmd;
496 	struct bt_conn *conn;
497 	int err;
498 
499 	conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &cp->address);
500 	if (!conn) {
501 		LOG_ERR("Unknown connection");
502 		return BTP_STATUS_FAILED;
503 	}
504 
505 	err = bt_tbs_client_read_signal_interval(conn, cp->inst_index);
506 	if (err) {
507 		return BTP_STATUS_FAILED;
508 	}
509 
510 	return BTP_STATUS_SUCCESS;
511 }
512 
ccp_read_current_calls(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)513 static uint8_t ccp_read_current_calls(const void *cmd, uint16_t cmd_len, void *rsp,
514 				      uint16_t *rsp_len)
515 {
516 	const struct btp_ccp_read_current_calls_cmd *cp = cmd;
517 	struct bt_conn *conn;
518 	int err;
519 
520 	conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &cp->address);
521 	if (!conn) {
522 		LOG_ERR("Unknown connection");
523 		return BTP_STATUS_FAILED;
524 	}
525 
526 	err = bt_tbs_client_read_current_calls(conn, cp->inst_index);
527 	if (err) {
528 		return BTP_STATUS_FAILED;
529 	}
530 
531 	return BTP_STATUS_SUCCESS;
532 }
533 
ccp_read_ccid(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)534 static uint8_t ccp_read_ccid(const void *cmd, uint16_t cmd_len, void *rsp,
535 			     uint16_t *rsp_len)
536 {
537 	const struct btp_ccp_read_ccid_cmd *cp = cmd;
538 	struct bt_conn *conn;
539 	int err;
540 
541 	conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &cp->address);
542 	if (!conn) {
543 		LOG_ERR("Unknown connection");
544 		return BTP_STATUS_FAILED;
545 	}
546 
547 	err = bt_tbs_client_read_ccid(conn, cp->inst_index);
548 	if (err) {
549 		return BTP_STATUS_FAILED;
550 	}
551 
552 	return BTP_STATUS_SUCCESS;
553 }
554 
ccp_read_call_uri(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)555 static uint8_t ccp_read_call_uri(const void *cmd, uint16_t cmd_len, void *rsp,
556 				 uint16_t *rsp_len)
557 {
558 	const struct btp_ccp_read_call_uri_cmd *cp = cmd;
559 	struct bt_conn *conn;
560 	int err;
561 
562 	conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &cp->address);
563 	if (!conn) {
564 		LOG_ERR("Unknown connection");
565 		return BTP_STATUS_FAILED;
566 	}
567 
568 	err = bt_tbs_client_read_call_uri(conn, cp->inst_index);
569 	if (err) {
570 		return BTP_STATUS_FAILED;
571 	}
572 
573 	return BTP_STATUS_SUCCESS;
574 }
575 
ccp_read_status_flags(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)576 static uint8_t ccp_read_status_flags(const void *cmd, uint16_t cmd_len, void *rsp,
577 				     uint16_t *rsp_len)
578 {
579 	const struct btp_ccp_read_status_flags_cmd *cp = cmd;
580 	struct bt_conn *conn;
581 	int err;
582 
583 	conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &cp->address);
584 	if (!conn) {
585 		LOG_ERR("Unknown connection");
586 		return BTP_STATUS_FAILED;
587 	}
588 
589 	err = bt_tbs_client_read_status_flags(conn, cp->inst_index);
590 	if (err) {
591 		return BTP_STATUS_FAILED;
592 	}
593 
594 	return BTP_STATUS_SUCCESS;
595 }
596 
ccp_read_optional_opcodes(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)597 static uint8_t ccp_read_optional_opcodes(const void *cmd, uint16_t cmd_len, void *rsp,
598 					 uint16_t *rsp_len)
599 {
600 	const struct btp_ccp_read_optional_opcodes_cmd *cp = cmd;
601 	struct bt_conn *conn;
602 	int err;
603 
604 	conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &cp->address);
605 	if (!conn) {
606 		LOG_ERR("Unknown connection");
607 		return BTP_STATUS_FAILED;
608 	}
609 
610 	err = bt_tbs_client_read_optional_opcodes(conn, cp->inst_index);
611 	if (err) {
612 		return BTP_STATUS_FAILED;
613 	}
614 
615 	return BTP_STATUS_SUCCESS;
616 }
617 
ccp_read_friendly_name(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)618 static uint8_t ccp_read_friendly_name(const void *cmd, uint16_t cmd_len, void *rsp,
619 					 uint16_t *rsp_len)
620 {
621 	const struct btp_ccp_read_friendly_name_cmd *cp = cmd;
622 	struct bt_conn *conn;
623 	int err;
624 
625 	conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &cp->address);
626 	if (!conn) {
627 		LOG_ERR("Unknown connection");
628 		return BTP_STATUS_FAILED;
629 	}
630 
631 	err = bt_tbs_client_read_friendly_name(conn, cp->inst_index);
632 	if (err) {
633 		return BTP_STATUS_FAILED;
634 	}
635 
636 	return BTP_STATUS_SUCCESS;
637 }
638 
ccp_read_remote_uri(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)639 static uint8_t ccp_read_remote_uri(const void *cmd, uint16_t cmd_len, void *rsp,
640 				   uint16_t *rsp_len)
641 {
642 	const struct btp_ccp_read_remote_uri_cmd *cp = cmd;
643 	struct bt_conn *conn;
644 	int err;
645 
646 	conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &cp->address);
647 	if (!conn) {
648 		LOG_ERR("Unknown connection");
649 		return BTP_STATUS_FAILED;
650 	}
651 
652 	err = bt_tbs_client_read_remote_uri(conn, cp->inst_index);
653 	if (err) {
654 		return BTP_STATUS_FAILED;
655 	}
656 
657 	return BTP_STATUS_SUCCESS;
658 }
659 
ccp_set_signal_interval(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)660 static uint8_t ccp_set_signal_interval(const void *cmd, uint16_t cmd_len, void *rsp,
661 				       uint16_t *rsp_len)
662 {
663 	const struct btp_ccp_set_signal_interval_cmd *cp = cmd;
664 	struct bt_conn *conn;
665 	int err;
666 
667 	conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &cp->address);
668 	if (!conn) {
669 		LOG_ERR("Unknown connection");
670 		return BTP_STATUS_FAILED;
671 	}
672 
673 	err = bt_tbs_client_set_signal_strength_interval(conn, cp->inst_index, cp->interval);
674 	if (err) {
675 		return BTP_STATUS_FAILED;
676 	}
677 
678 	return BTP_STATUS_SUCCESS;
679 }
680 
ccp_hold_call(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)681 static uint8_t ccp_hold_call(const void *cmd, uint16_t cmd_len, void *rsp, uint16_t *rsp_len)
682 {
683 	const struct btp_ccp_hold_call_cmd *cp = cmd;
684 	struct bt_conn *conn;
685 	int err;
686 
687 	conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &cp->address);
688 	if (!conn) {
689 		LOG_ERR("Unknown connection");
690 		return BTP_STATUS_FAILED;
691 	}
692 
693 	err = bt_tbs_client_hold_call(conn, cp->inst_index, cp->call_id);
694 	if (err) {
695 		return BTP_STATUS_FAILED;
696 	}
697 
698 	return BTP_STATUS_SUCCESS;
699 }
700 
ccp_retrieve_call(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)701 static uint8_t ccp_retrieve_call(const void *cmd, uint16_t cmd_len, void *rsp, uint16_t *rsp_len)
702 {
703 	const struct btp_ccp_retrieve_call_cmd *cp = cmd;
704 	struct bt_conn *conn;
705 	int err;
706 
707 	conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &cp->address);
708 	if (!conn) {
709 		LOG_ERR("Unknown connection");
710 		return BTP_STATUS_FAILED;
711 	}
712 
713 	err = bt_tbs_client_retrieve_call(conn, cp->inst_index, cp->call_id);
714 	if (err) {
715 		return BTP_STATUS_FAILED;
716 	}
717 
718 	return BTP_STATUS_SUCCESS;
719 }
720 
ccp_join_calls(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)721 static uint8_t ccp_join_calls(const void *cmd, uint16_t cmd_len, void *rsp, uint16_t *rsp_len)
722 {
723 	const struct btp_ccp_join_calls_cmd *cp = cmd;
724 	const uint8_t *call_index;
725 	struct bt_conn *conn;
726 	int err;
727 
728 	conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &cp->address);
729 	if (!conn) {
730 		LOG_ERR("Unknown connection");
731 		return BTP_STATUS_FAILED;
732 	}
733 
734 	call_index = cp->call_index;
735 
736 	err = bt_tbs_client_join_calls(conn, cp->inst_index, call_index, cp->count);
737 	if (err) {
738 		return BTP_STATUS_FAILED;
739 	}
740 
741 	return BTP_STATUS_SUCCESS;
742 }
743 
744 static const struct btp_handler ccp_handlers[] = {
745 	{
746 		.opcode = BTP_CCP_READ_SUPPORTED_COMMANDS,
747 		.index = BTP_INDEX_NONE,
748 		.expect_len = 0,
749 		.func = ccp_supported_commands
750 	},
751 	{
752 		.opcode = BTP_CCP_DISCOVER_TBS,
753 		.expect_len = sizeof(struct btp_ccp_discover_tbs_cmd),
754 		.func = ccp_discover_tbs
755 	},
756 	{
757 		.opcode = BTP_CCP_ACCEPT_CALL,
758 		.expect_len = sizeof(struct btp_ccp_accept_call_cmd),
759 		.func = ccp_accept_call
760 	},
761 	{
762 		.opcode = BTP_CCP_TERMINATE_CALL,
763 		.expect_len = sizeof(struct btp_ccp_terminate_call_cmd),
764 		.func = ccp_terminate_call
765 	},
766 	{
767 		.opcode = BTP_CCP_ORIGINATE_CALL,
768 		.expect_len = BTP_HANDLER_LENGTH_VARIABLE,
769 		.func = ccp_originate_call
770 	},
771 	{
772 		.opcode = BTP_CCP_READ_CALL_STATE,
773 		.expect_len = sizeof(struct btp_ccp_read_call_state_cmd),
774 		.func = ccp_read_call_state
775 	},
776 	{
777 		.opcode = BTP_CCP_READ_BEARER_NAME,
778 		.expect_len = sizeof(struct btp_ccp_read_bearer_name_cmd),
779 		.func = ccp_read_bearer_name
780 	},
781 	{
782 		.opcode = BTP_CCP_READ_BEARER_UCI,
783 		.expect_len = sizeof(struct btp_ccp_read_bearer_uci_cmd),
784 		.func = ccp_read_bearer_uci
785 	},
786 	{
787 		.opcode = BTP_CCP_READ_BEARER_TECH,
788 		.expect_len = sizeof(struct btp_ccp_read_bearer_technology_cmd),
789 		.func = ccp_read_bearer_tech
790 	},
791 	{
792 		.opcode = BTP_CCP_READ_URI_LIST,
793 		.expect_len = sizeof(struct btp_ccp_read_uri_list_cmd),
794 		.func = ccp_read_uri_list
795 	},
796 	{
797 		.opcode = BTP_CCP_READ_SIGNAL_STRENGTH,
798 		.expect_len = sizeof(struct btp_ccp_read_signal_strength_cmd),
799 		.func = ccp_read_signal_strength
800 	},
801 	{
802 		.opcode = BTP_CCP_READ_SIGNAL_INTERVAL,
803 		.expect_len = sizeof(struct btp_ccp_read_signal_interval_cmd),
804 		.func = ccp_read_signal_interval
805 	},
806 	{
807 		.opcode = BTP_CCP_READ_CURRENT_CALLS,
808 		.expect_len = sizeof(struct btp_ccp_read_current_calls_cmd),
809 		.func = ccp_read_current_calls
810 	},
811 	{
812 		.opcode = BTP_CCP_READ_CCID,
813 		.expect_len = sizeof(struct btp_ccp_read_ccid_cmd),
814 		.func = ccp_read_ccid
815 	},
816 	{
817 		.opcode = BTP_CCP_READ_CALL_URI,
818 		.expect_len = sizeof(struct btp_ccp_read_call_uri_cmd),
819 		.func = ccp_read_call_uri
820 	},
821 	{
822 		.opcode = BTP_CCP_READ_STATUS_FLAGS,
823 		.expect_len = sizeof(struct btp_ccp_read_status_flags_cmd),
824 		.func = ccp_read_status_flags
825 	},
826 	{
827 		.opcode = BTP_CCP_READ_OPTIONAL_OPCODES,
828 		.expect_len = sizeof(struct btp_ccp_read_optional_opcodes_cmd),
829 		.func = ccp_read_optional_opcodes
830 	},
831 	{
832 		.opcode = BTP_CCP_READ_FRIENDLY_NAME,
833 		.expect_len = sizeof(struct btp_ccp_read_friendly_name_cmd),
834 		.func = ccp_read_friendly_name
835 	},
836 	{
837 		.opcode = BTP_CCP_READ_REMOTE_URI,
838 		.expect_len = sizeof(struct btp_ccp_read_remote_uri_cmd),
839 		.func = ccp_read_remote_uri
840 	},
841 	{
842 		.opcode = BTP_CCP_SET_SIGNAL_INTERVAL,
843 		.expect_len = sizeof(struct btp_ccp_set_signal_interval_cmd),
844 		.func = ccp_set_signal_interval
845 	},
846 	{
847 		.opcode = BTP_CCP_HOLD_CALL,
848 		.expect_len = sizeof(struct btp_ccp_hold_call_cmd),
849 		.func = ccp_hold_call
850 	},
851 	{
852 		.opcode = BTP_CCP_RETRIEVE_CALL,
853 		.expect_len = sizeof(struct btp_ccp_retrieve_call_cmd),
854 		.func = ccp_retrieve_call
855 	},
856 	{
857 		.opcode = BTP_CCP_JOIN_CALLS,
858 		.expect_len = BTP_HANDLER_LENGTH_VARIABLE,
859 		.func = ccp_join_calls
860 	},
861 };
862 
tester_init_ccp(void)863 uint8_t tester_init_ccp(void)
864 {
865 	int err;
866 
867 	tester_register_command_handlers(BTP_SERVICE_ID_CCP, ccp_handlers,
868 					 ARRAY_SIZE(ccp_handlers));
869 
870 	err = bt_tbs_client_register_cb(&tbs_client_callbacks);
871 	if (err != 0) {
872 		return BTP_STATUS_FAILED;
873 	}
874 
875 	return BTP_STATUS_SUCCESS;
876 }
877 
tester_unregister_ccp(void)878 uint8_t tester_unregister_ccp(void)
879 {
880 	return BTP_STATUS_SUCCESS;
881 }
882 
883 /* Telephone Bearer Service */
tbs_supported_commands(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)884 static uint8_t tbs_supported_commands(const void *cmd, uint16_t cmd_len, void *rsp,
885 				       uint16_t *rsp_len)
886 {
887 	struct btp_tbs_read_supported_commands_rp *rp = rsp;
888 
889 	/* octet 0 */
890 	tester_set_bit(rp->data, BTP_TBS_READ_SUPPORTED_COMMANDS);
891 	tester_set_bit(rp->data, BTP_TBS_REMOTE_INCOMING);
892 	tester_set_bit(rp->data, BTP_TBS_HOLD);
893 	tester_set_bit(rp->data, BTP_TBS_SET_BEARER_NAME);
894 	tester_set_bit(rp->data, BTP_TBS_SET_TECHNOLOGY);
895 	tester_set_bit(rp->data, BTP_TBS_SET_URI_SCHEME);
896 	tester_set_bit(rp->data, BTP_TBS_SET_STATUS_FLAGS);
897 
898 	/* octet 1 */
899 	tester_set_bit(rp->data, BTP_TBS_REMOTE_HOLD);
900 	tester_set_bit(rp->data, BTP_TBS_ORIGINATE);
901 	tester_set_bit(rp->data, BTP_TBS_SET_SIGNAL_STRENGTH);
902 
903 	*rsp_len = sizeof(*rp) + 2;
904 
905 	return BTP_STATUS_SUCCESS;
906 }
907 
tbs_remote_incoming(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)908 static uint8_t tbs_remote_incoming(const void *cmd, uint16_t cmd_len, void *rsp, uint16_t *rsp_len)
909 {
910 	const struct btp_tbs_remote_incoming_cmd *cp = cmd;
911 	char friendly_name[CONFIG_BT_TBS_MAX_URI_LENGTH];
912 	char caller_uri[CONFIG_BT_TBS_MAX_URI_LENGTH];
913 	char recv_uri[CONFIG_BT_TBS_MAX_URI_LENGTH];
914 	int err;
915 
916 	LOG_DBG("");
917 
918 	if ((cp->recv_len >= sizeof(recv_uri) || cp->caller_len >= sizeof(caller_uri)) ||
919 	     cp->fn_len >= sizeof(friendly_name)) {
920 		return BTP_STATUS_FAILED;
921 	}
922 
923 	memcpy(recv_uri, cp->data, cp->recv_len);
924 	memcpy(caller_uri, cp->data + cp->recv_len, cp->caller_len);
925 	memcpy(friendly_name, cp->data + cp->recv_len + cp->caller_len, cp->fn_len);
926 
927 	recv_uri[cp->recv_len] = '\0';
928 	caller_uri[cp->caller_len] = '\0';
929 	friendly_name[cp->fn_len] = '\0';
930 
931 	err = bt_tbs_remote_incoming(cp->index, recv_uri, caller_uri, friendly_name);
932 	if (err < 0) {
933 		return BTP_STATUS_FAILED;
934 	}
935 
936 	return BTP_STATUS_SUCCESS;
937 }
938 
tbs_originate(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)939 static uint8_t tbs_originate(const void *cmd, uint16_t cmd_len, void *rsp, uint16_t *rsp_len)
940 {
941 	const struct btp_tbs_originate_cmd *cp = cmd;
942 	char uri[CONFIG_BT_TBS_MAX_URI_LENGTH];
943 	int err;
944 
945 	LOG_DBG("TBS Originate Call");
946 
947 	if (cp->uri_len >= sizeof(uri)) {
948 		return BTP_STATUS_FAILED;
949 	}
950 
951 	memcpy(uri, cp->uri, cp->uri_len);
952 	uri[cp->uri_len] = '\0';
953 
954 	err = bt_tbs_originate(cp->index, uri, &call_index);
955 	if (err) {
956 		return BTP_STATUS_FAILED;
957 	}
958 
959 	return BTP_STATUS_SUCCESS;
960 }
961 
tbs_hold(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)962 static uint8_t tbs_hold(const void *cmd, uint16_t cmd_len, void *rsp, uint16_t *rsp_len)
963 {
964 	const struct btp_tbs_hold_cmd *cp = cmd;
965 	int err;
966 
967 	LOG_DBG("TBS Hold Call");
968 
969 	err = bt_tbs_hold(cp->index);
970 	if (err) {
971 		return BTP_STATUS_FAILED;
972 	}
973 
974 	return BTP_STATUS_SUCCESS;
975 }
976 
tbs_remote_hold(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)977 static uint8_t tbs_remote_hold(const void *cmd, uint16_t cmd_len, void *rsp, uint16_t *rsp_len)
978 {
979 	const struct btp_tbs_remote_hold_cmd *cp = cmd;
980 	int err;
981 
982 	LOG_DBG("TBS Remote Hold Call");
983 
984 	err = bt_tbs_remote_hold(cp->index);
985 	if (err) {
986 		return BTP_STATUS_FAILED;
987 	}
988 
989 	return BTP_STATUS_SUCCESS;
990 }
991 
tbs_set_bearer_name(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)992 static uint8_t tbs_set_bearer_name(const void *cmd, uint16_t cmd_len, void *rsp, uint16_t *rsp_len)
993 {
994 	const struct btp_tbs_set_bearer_name_cmd *cp = cmd;
995 	char bearer_name[CONFIG_BT_TBS_MAX_PROVIDER_NAME_LENGTH];
996 	int err;
997 
998 	LOG_DBG("TBS Set Bearer Provider Name");
999 
1000 	if (cp->name_len >= sizeof(bearer_name)) {
1001 		return BTP_STATUS_FAILED;
1002 	}
1003 
1004 	memcpy(bearer_name, cp->name, cp->name_len);
1005 	bearer_name[cp->name_len] = '\0';
1006 
1007 	err = bt_tbs_set_bearer_provider_name(cp->index, bearer_name);
1008 	if (err) {
1009 		return BTP_STATUS_FAILED;
1010 	}
1011 
1012 	return BTP_STATUS_SUCCESS;
1013 }
1014 
tbs_set_bearer_technology(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1015 static uint8_t tbs_set_bearer_technology(const void *cmd, uint16_t cmd_len, void *rsp,
1016 					 uint16_t *rsp_len)
1017 {
1018 	const struct btp_tbs_set_technology_cmd *cp = cmd;
1019 	int err;
1020 
1021 	LOG_DBG("TBS Set bearer technology");
1022 
1023 	err = bt_tbs_set_bearer_technology(cp->index, cp->tech);
1024 	if (err) {
1025 		return BTP_STATUS_FAILED;
1026 	}
1027 
1028 	return BTP_STATUS_SUCCESS;
1029 }
1030 
tbs_set_uri_scheme_list(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1031 static uint8_t tbs_set_uri_scheme_list(const void *cmd, uint16_t cmd_len, void *rsp,
1032 				       uint16_t *rsp_len)
1033 {
1034 	const struct btp_tbs_set_uri_schemes_list_cmd *cp = cmd;
1035 	char uri_list[CONFIG_BT_TBS_MAX_SCHEME_LIST_LENGTH];
1036 	char *uri_ptr = (char *)&uri_list;
1037 	int err;
1038 
1039 	LOG_DBG("TBS Set Uri Scheme list");
1040 
1041 	if (cp->uri_len >= sizeof(uri_list)) {
1042 		return BTP_STATUS_FAILED;
1043 	}
1044 
1045 	memcpy(uri_list, cp->uri_list, cp->uri_len);
1046 	uri_list[cp->uri_len] = '\0';
1047 
1048 	if (cp->uri_count > 1) {
1049 		/* TODO: currently supporting only one uri*/
1050 		return BTP_STATUS_FAILED;
1051 	}
1052 
1053 	err = bt_tbs_set_uri_scheme_list(cp->index, (const char **)&uri_ptr, cp->uri_count);
1054 	if (err) {
1055 		return BTP_STATUS_FAILED;
1056 	}
1057 
1058 	return BTP_STATUS_SUCCESS;
1059 }
1060 
tbs_set_status_flags(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1061 static uint8_t tbs_set_status_flags(const void *cmd, uint16_t cmd_len, void *rsp,
1062 				    uint16_t *rsp_len)
1063 {
1064 	const struct btp_tbs_set_status_flags_cmd *cp = cmd;
1065 	uint16_t flags = sys_le16_to_cpu(cp->flags);
1066 	int err;
1067 
1068 	LOG_DBG("TBS Set Status Flags");
1069 
1070 	err = bt_tbs_set_status_flags(cp->index, flags);
1071 	if (err) {
1072 		return BTP_STATUS_FAILED;
1073 	}
1074 
1075 	return BTP_STATUS_SUCCESS;
1076 }
1077 
tbs_set_signal_strength(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1078 static uint8_t tbs_set_signal_strength(const void *cmd, uint16_t cmd_len, void *rsp,
1079 				       uint16_t *rsp_len)
1080 {
1081 	const struct btp_tbs_set_signal_strength_cmd *cp = cmd;
1082 	int err;
1083 
1084 	LOG_DBG("TBS Set Signal Strength");
1085 
1086 	err = bt_tbs_set_signal_strength(cp->index, cp->strength);
1087 	if (err) {
1088 		return BTP_STATUS_FAILED;
1089 	}
1090 
1091 	return BTP_STATUS_SUCCESS;
1092 }
1093 
btp_tbs_originate_call_cb(struct bt_conn * conn,uint8_t call_index,const char * uri)1094 static bool btp_tbs_originate_call_cb(struct bt_conn *conn, uint8_t call_index, const char *uri)
1095 {
1096 	LOG_DBG("TBS Originate Call cb");
1097 
1098 	return true;
1099 }
1100 
btp_tbs_call_change_cb(struct bt_conn * conn,uint8_t call_index)1101 static void btp_tbs_call_change_cb(struct bt_conn *conn, uint8_t call_index)
1102 {
1103 	LOG_DBG("TBS Call Status Changed cb");
1104 }
1105 
1106 static struct bt_tbs_cb tbs_cbs = {
1107 	.originate_call = btp_tbs_originate_call_cb,
1108 	.hold_call = btp_tbs_call_change_cb,
1109 };
1110 
1111 static const struct btp_handler tbs_handlers[] = {
1112 	{
1113 		.opcode = BTP_TBS_READ_SUPPORTED_COMMANDS,
1114 		.index = BTP_INDEX_NONE,
1115 		.expect_len = 0,
1116 		.func = tbs_supported_commands,
1117 	},
1118 	{
1119 		.opcode = BTP_TBS_REMOTE_INCOMING,
1120 		.expect_len = BTP_HANDLER_LENGTH_VARIABLE,
1121 		.func = tbs_remote_incoming,
1122 	},
1123 	{
1124 		.opcode = BTP_TBS_HOLD,
1125 		.expect_len = sizeof(struct btp_tbs_hold_cmd),
1126 		.func = tbs_hold,
1127 	},
1128 	{
1129 		.opcode = BTP_TBS_SET_BEARER_NAME,
1130 		.expect_len = BTP_HANDLER_LENGTH_VARIABLE,
1131 		.func = tbs_set_bearer_name,
1132 	},
1133 	{
1134 		.opcode = BTP_TBS_SET_TECHNOLOGY,
1135 		.expect_len = sizeof(struct btp_tbs_set_technology_cmd),
1136 		.func = tbs_set_bearer_technology,
1137 	},
1138 	{
1139 		.opcode = BTP_TBS_SET_URI_SCHEME,
1140 		.expect_len = BTP_HANDLER_LENGTH_VARIABLE,
1141 		.func = tbs_set_uri_scheme_list,
1142 	},
1143 	{
1144 		.opcode = BTP_TBS_SET_STATUS_FLAGS,
1145 		.expect_len = sizeof(struct btp_tbs_set_status_flags_cmd),
1146 		.func = tbs_set_status_flags,
1147 	},
1148 	{
1149 		.opcode = BTP_TBS_REMOTE_HOLD,
1150 		.expect_len = sizeof(struct btp_tbs_remote_hold_cmd),
1151 		.func = tbs_remote_hold,
1152 	},
1153 	{
1154 		.opcode = BTP_TBS_ORIGINATE,
1155 		.expect_len = BTP_HANDLER_LENGTH_VARIABLE,
1156 		.func = tbs_originate,
1157 	},
1158 	{
1159 		.opcode = BTP_TBS_SET_SIGNAL_STRENGTH,
1160 		.expect_len = sizeof(struct btp_tbs_set_signal_strength_cmd),
1161 		.func = tbs_set_signal_strength,
1162 	},
1163 };
1164 
tester_init_tbs(void)1165 uint8_t tester_init_tbs(void)
1166 {
1167 	const struct bt_tbs_register_param gtbs_param = {
1168 		.provider_name = "Generic TBS",
1169 		.uci = "un000",
1170 		.uri_schemes_supported = "tel,skype",
1171 		.gtbs = true,
1172 		.authorization_required = false,
1173 		.technology = BT_TBS_TECHNOLOGY_3G,
1174 		.supported_features = CONFIG_BT_TBS_SUPPORTED_FEATURES,
1175 	};
1176 	const struct bt_tbs_register_param tbs_param = {
1177 		.provider_name = "TBS",
1178 		.uci = "un000",
1179 		.uri_schemes_supported = "tel,skype",
1180 		.gtbs = false,
1181 		.authorization_required = false,
1182 		/* Set different technologies per bearer */
1183 		.technology = BT_TBS_TECHNOLOGY_4G,
1184 		.supported_features = CONFIG_BT_TBS_SUPPORTED_FEATURES,
1185 	};
1186 	int err;
1187 
1188 	bt_tbs_register_cb(&tbs_cbs);
1189 
1190 	tester_register_command_handlers(BTP_SERVICE_ID_TBS, tbs_handlers,
1191 					 ARRAY_SIZE(tbs_handlers));
1192 
1193 	err = bt_tbs_register_bearer(&gtbs_param);
1194 	if (err < 0) {
1195 		LOG_DBG("Failed to register GTBS: %d", err);
1196 
1197 		return BTP_STATUS_FAILED;
1198 	}
1199 
1200 	err = bt_tbs_register_bearer(&tbs_param);
1201 	if (err < 0) {
1202 		LOG_DBG("Failed to register TBS: %d", err);
1203 
1204 		return BTP_STATUS_FAILED;
1205 	}
1206 
1207 	return BTP_STATUS_SUCCESS;
1208 }
1209 
tester_unregister_tbs(void)1210 uint8_t tester_unregister_tbs(void)
1211 {
1212 	return BTP_STATUS_SUCCESS;
1213 }
1214