1 /* gap.c - Bluetooth GAP Tester */
2
3 /*
4 * Copyright (c) 2015-2016 Intel Corporation
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9 #include <zephyr/sys/atomic.h>
10 #include <zephyr/types.h>
11 #include <string.h>
12
13 #include <zephyr/toolchain.h>
14 #include <zephyr/bluetooth/bluetooth.h>
15 #include <zephyr/bluetooth/conn.h>
16 #include <zephyr/bluetooth/gatt.h>
17 #include <zephyr/bluetooth/hci.h>
18
19 #include <zephyr/sys/byteorder.h>
20 #include <zephyr/net/buf.h>
21
22 #include <hci_core.h>
23
24 #include <zephyr/logging/log.h>
25 #define LOG_MODULE_NAME bttester_gap
26 LOG_MODULE_REGISTER(LOG_MODULE_NAME, CONFIG_BTTESTER_LOG_LEVEL);
27
28 #include "btp/btp.h"
29
30 #define CONTROLLER_NAME "btp_tester"
31
32 #define BT_LE_AD_DISCOV_MASK (BT_LE_AD_LIMITED | BT_LE_AD_GENERAL)
33 #define ADV_BUF_LEN (sizeof(struct btp_gap_device_found_ev) + 2 * 31)
34
35 static atomic_t current_settings;
36 struct bt_conn_auth_cb cb;
37 static uint8_t oob_legacy_tk[16] = { 0 };
38
39 static bool filter_list_in_use;
40
41 #if !defined(CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY)
42 static struct bt_le_oob oob_sc_local = { 0 };
43 static struct bt_le_oob oob_sc_remote = { 0 };
44 #endif /* !defined(CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY) */
45
46 /* connection parameters for rejection test */
47 #define REJECT_INTERVAL_MIN 0x0C80
48 #define REJECT_INTERVAL_MAX 0x0C80
49 #define REJECT_LATENCY 0x0000
50 #define REJECT_SUPERVISION_TIMEOUT 0x0C80
51
52 #if defined(CONFIG_BT_PRIVACY)
53 static struct {
54 bt_addr_le_t addr;
55 bool supported;
56 } cars[CONFIG_BT_MAX_PAIRED];
57
58 static uint8_t read_car_cb(struct bt_conn *conn, uint8_t err,
59 struct bt_gatt_read_params *params, const void *data,
60 uint16_t length);
61
62 static struct bt_gatt_read_params read_car_params = {
63 .func = read_car_cb,
64 .by_uuid.uuid = BT_UUID_CENTRAL_ADDR_RES,
65 .by_uuid.start_handle = BT_ATT_FIRST_ATTRIBUTE_HANDLE,
66 .by_uuid.end_handle = BT_ATT_LAST_ATTRIBUTE_HANDLE,
67 };
68
read_car_cb(struct bt_conn * conn,uint8_t err,struct bt_gatt_read_params * params,const void * data,uint16_t length)69 static uint8_t read_car_cb(struct bt_conn *conn, uint8_t err,
70 struct bt_gatt_read_params *params, const void *data,
71 uint16_t length)
72 {
73 struct bt_conn_info info;
74 bool supported = false;
75
76 if (!err && data && length == 1) {
77 const uint8_t *tmp = data;
78
79 /* only 0 or 1 are valid values */
80 if (tmp[0] == 1) {
81 supported = true;
82 }
83 }
84
85 bt_conn_get_info(conn, &info);
86
87 for (int i = 0; i < CONFIG_BT_MAX_PAIRED; i++) {
88 if (bt_addr_le_eq(info.le.dst, &cars[i].addr)) {
89 cars[i].supported = supported;
90 break;
91 }
92 }
93
94 return BT_GATT_ITER_STOP;
95 }
96 #endif
97
le_connected(struct bt_conn * conn,uint8_t err)98 static void le_connected(struct bt_conn *conn, uint8_t err)
99 {
100 struct btp_gap_device_connected_ev ev;
101 struct bt_conn_info info;
102
103 if (err) {
104 return;
105 }
106
107 bt_conn_get_info(conn, &info);
108
109 bt_addr_le_copy(&ev.address, info.le.dst);
110 ev.interval = sys_cpu_to_le16(info.le.interval);
111 ev.latency = sys_cpu_to_le16(info.le.latency);
112 ev.timeout = sys_cpu_to_le16(info.le.timeout);
113
114 tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_CONNECTED, &ev, sizeof(ev));
115
116 if (info.role == BT_CONN_ROLE_PERIPHERAL) {
117 struct btp_gap_new_settings_ev sev;
118
119 atomic_clear_bit(¤t_settings, BTP_GAP_SETTINGS_ADVERTISING);
120 sev.current_settings = sys_cpu_to_le32(current_settings);
121 tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_NEW_SETTINGS, &sev, sizeof(sev));
122 }
123 }
124
le_disconnected(struct bt_conn * conn,uint8_t reason)125 static void le_disconnected(struct bt_conn *conn, uint8_t reason)
126 {
127 struct btp_gap_device_disconnected_ev ev;
128 const bt_addr_le_t *addr = bt_conn_get_dst(conn);
129
130 bt_addr_le_copy(&ev.address, addr);
131
132 tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_DISCONNECTED, &ev, sizeof(ev));
133 }
134
le_identity_resolved(struct bt_conn * conn,const bt_addr_le_t * rpa,const bt_addr_le_t * identity)135 static void le_identity_resolved(struct bt_conn *conn, const bt_addr_le_t *rpa,
136 const bt_addr_le_t *identity)
137 {
138 struct btp_gap_identity_resolved_ev ev;
139
140 bt_addr_le_copy(&ev.address, rpa);
141 bt_addr_le_copy(&ev.identity_address, identity);
142
143 tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_IDENTITY_RESOLVED, &ev, sizeof(ev));
144 }
145
le_param_updated(struct bt_conn * conn,uint16_t interval,uint16_t latency,uint16_t timeout)146 static void le_param_updated(struct bt_conn *conn, uint16_t interval,
147 uint16_t latency, uint16_t timeout)
148 {
149 struct btp_gap_conn_param_update_ev ev;
150 const bt_addr_le_t *addr = bt_conn_get_dst(conn);
151
152 bt_addr_le_copy(&ev.address, addr);
153 ev.interval = sys_cpu_to_le16(interval);
154 ev.latency = sys_cpu_to_le16(latency);
155 ev.timeout = sys_cpu_to_le16(timeout);
156
157 tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_CONN_PARAM_UPDATE, &ev, sizeof(ev));
158 }
159
le_param_req(struct bt_conn * conn,struct bt_le_conn_param * param)160 static bool le_param_req(struct bt_conn *conn, struct bt_le_conn_param *param)
161 {
162 /* reject update if all parameters match reject pattern */
163 if ((param->interval_min == REJECT_INTERVAL_MIN) &&
164 (param->interval_max == REJECT_INTERVAL_MAX) &&
165 (param->latency == REJECT_LATENCY) &&
166 (param->timeout == REJECT_SUPERVISION_TIMEOUT)) {
167 return false;
168 }
169
170 return true;
171 }
172
le_security_changed(struct bt_conn * conn,bt_security_t level,enum bt_security_err err)173 static void le_security_changed(struct bt_conn *conn, bt_security_t level,
174 enum bt_security_err err)
175 {
176 const bt_addr_le_t *addr = bt_conn_get_dst(conn);
177 struct btp_gap_sec_level_changed_ev sec_ev;
178 struct btp_gap_bond_lost_ev bond_ev;
179 struct bt_conn_info info;
180
181 switch (err) {
182 case BT_SECURITY_ERR_SUCCESS:
183 bt_addr_le_copy(&sec_ev.address, addr);
184 /* enum matches BTP values */
185 sec_ev.sec_level = level;
186
187 tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_SEC_LEVEL_CHANGED,
188 &sec_ev, sizeof(sec_ev));
189 break;
190 case BT_SECURITY_ERR_PIN_OR_KEY_MISSING:
191 /* for central role this means that peer have no LTK when we
192 * started encryption procedure
193 *
194 * This means bond is lost and we restart pairing to re-bond
195 */
196 if (bt_conn_get_info(conn, &info) == 0 &&
197 info.role == BT_CONN_ROLE_CENTRAL) {
198 LOG_DBG("Bond lost");
199
200 bt_addr_le_copy(&bond_ev.address, addr);
201
202 tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_BOND_LOST,
203 &bond_ev, sizeof(bond_ev));
204
205 (void)bt_conn_set_security(conn, BT_SECURITY_L2 | BT_SECURITY_FORCE_PAIR);
206 }
207 break;
208 default:
209 break;
210 }
211 }
212
213 static struct bt_conn_cb conn_callbacks = {
214 .connected = le_connected,
215 .disconnected = le_disconnected,
216 .identity_resolved = le_identity_resolved,
217 .le_param_updated = le_param_updated,
218 .le_param_req = le_param_req,
219 .security_changed = le_security_changed,
220 };
221
supported_commands(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)222 static uint8_t supported_commands(const void *cmd, uint16_t cmd_len,
223 void *rsp, uint16_t *rsp_len)
224 {
225 struct btp_gap_read_supported_commands_rp *rp = rsp;
226
227 /* octet 0 */
228 tester_set_bit(rp->data, BTP_GAP_READ_SUPPORTED_COMMANDS);
229 tester_set_bit(rp->data, BTP_GAP_READ_CONTROLLER_INDEX_LIST);
230 tester_set_bit(rp->data, BTP_GAP_READ_CONTROLLER_INFO);
231 tester_set_bit(rp->data, BTP_GAP_SET_CONNECTABLE);
232
233 /* octet 1 */
234 tester_set_bit(rp->data, BTP_GAP_SET_DISCOVERABLE);
235 tester_set_bit(rp->data, BTP_GAP_SET_BONDABLE);
236 tester_set_bit(rp->data, BTP_GAP_START_ADVERTISING);
237 tester_set_bit(rp->data, BTP_GAP_STOP_ADVERTISING);
238 tester_set_bit(rp->data, BTP_GAP_START_DISCOVERY);
239 tester_set_bit(rp->data, BTP_GAP_STOP_DISCOVERY);
240 tester_set_bit(rp->data, BTP_GAP_CONNECT);
241 tester_set_bit(rp->data, BTP_GAP_DISCONNECT);
242
243 /* octet 2 */
244 tester_set_bit(rp->data, BTP_GAP_SET_IO_CAP);
245 tester_set_bit(rp->data, BTP_GAP_PAIR);
246 tester_set_bit(rp->data, BTP_GAP_PASSKEY_ENTRY);
247 tester_set_bit(rp->data, BTP_GAP_PASSKEY_CONFIRM);
248 tester_set_bit(rp->data, BTP_GAP_START_DIRECTED_ADV);
249 tester_set_bit(rp->data, BTP_GAP_CONN_PARAM_UPDATE);
250
251 /* octet 3 */
252 tester_set_bit(rp->data, BTP_GAP_OOB_LEGACY_SET_DATA);
253 #if !defined(CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY)
254 tester_set_bit(rp->data, BTP_GAP_OOB_SC_GET_LOCAL_DATA);
255 tester_set_bit(rp->data, BTP_GAP_OOB_SC_SET_REMOTE_DATA);
256 #endif /* !defined(CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY) */
257 tester_set_bit(rp->data, BTP_GAP_SET_MITM);
258 tester_set_bit(rp->data, BTP_GAP_SET_FILTER_LIST);
259 #if defined(CONFIG_BT_EXT_ADV)
260 tester_set_bit(rp->data, BTP_GAP_SET_EXTENDED_ADVERTISING);
261 #endif
262
263 *rsp_len = sizeof(*rp) + 4;
264
265 return BTP_STATUS_SUCCESS;
266 }
267
controller_index_list(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)268 static uint8_t controller_index_list(const void *cmd, uint16_t cmd_len,
269 void *rsp, uint16_t *rsp_len)
270 {
271 struct btp_gap_read_controller_index_list_rp *rp = rsp;
272
273 rp->num = 1U;
274 rp->index[0] = BTP_INDEX;
275
276 *rsp_len = sizeof(*rp) + 1;
277
278 return BTP_STATUS_SUCCESS;
279 }
280
controller_info(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)281 static uint8_t controller_info(const void *cmd, uint16_t cmd_len,
282 void *rsp, uint16_t *rsp_len)
283 {
284 struct btp_gap_read_controller_info_rp *rp = rsp;
285 uint32_t supported_settings;
286 struct bt_le_oob oob_local = { 0 };
287
288 bt_le_oob_get_local(BT_ID_DEFAULT, &oob_local);
289
290 bt_addr_copy(&rp->address, &oob_local.addr.a);
291
292 /*
293 * Re-use the oob data read here in get_oob_sc_local_data()
294 */
295 #if !defined(CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY)
296 oob_sc_local = oob_local;
297 #endif /* !defined(CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY) */
298
299 /*
300 * If privacy is used, the device uses random type address, otherwise
301 * static random or public type address is used.
302 */
303 #if !defined(CONFIG_BT_PRIVACY)
304 if (oob_local.addr.type == BT_ADDR_LE_RANDOM) {
305 atomic_set_bit(¤t_settings, BTP_GAP_SETTINGS_STATIC_ADDRESS);
306 }
307 #endif /* CONFIG_BT_PRIVACY */
308
309 supported_settings = BIT(BTP_GAP_SETTINGS_POWERED);
310 supported_settings |= BIT(BTP_GAP_SETTINGS_CONNECTABLE);
311 supported_settings |= BIT(BTP_GAP_SETTINGS_BONDABLE);
312 supported_settings |= BIT(BTP_GAP_SETTINGS_LE);
313 supported_settings |= BIT(BTP_GAP_SETTINGS_ADVERTISING);
314 supported_settings |= BIT(BTP_GAP_SETTINGS_EXTENDED_ADVERTISING);
315
316 rp->supported_settings = sys_cpu_to_le32(supported_settings);
317 rp->current_settings = sys_cpu_to_le32(current_settings);
318
319 memcpy(rp->name, CONTROLLER_NAME, sizeof(CONTROLLER_NAME));
320
321 *rsp_len = sizeof(*rp);
322
323 return BTP_STATUS_SUCCESS;
324 }
325
326 #if !defined(CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY)
oob_config_str(int oob_config)327 static const char *oob_config_str(int oob_config)
328 {
329 switch (oob_config) {
330 case BT_CONN_OOB_LOCAL_ONLY:
331 return "Local";
332 case BT_CONN_OOB_REMOTE_ONLY:
333 return "Remote";
334 case BT_CONN_OOB_BOTH_PEERS:
335 return "Local and Remote";
336 case BT_CONN_OOB_NO_DATA:
337 default:
338 return "no";
339 }
340 }
341 #endif /* !defined(CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY) */
342
oob_data_request(struct bt_conn * conn,struct bt_conn_oob_info * oob_info)343 static void oob_data_request(struct bt_conn *conn,
344 struct bt_conn_oob_info *oob_info)
345 {
346 struct bt_conn_info info;
347 int err = bt_conn_get_info(conn, &info);
348
349 if (err) {
350 return;
351 }
352
353 char addr[BT_ADDR_LE_STR_LEN];
354
355 bt_addr_le_to_str(info.le.dst, addr, sizeof(addr));
356
357 switch (oob_info->type) {
358 #if !defined(CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY)
359 case BT_CONN_OOB_LE_SC:
360 {
361 LOG_DBG("Set %s OOB SC data for %s, ",
362 oob_config_str(oob_info->lesc.oob_config),
363 addr);
364
365 struct bt_le_oob_sc_data *oobd_local =
366 oob_info->lesc.oob_config != BT_CONN_OOB_REMOTE_ONLY ?
367 &oob_sc_local.le_sc_data :
368 NULL;
369
370 struct bt_le_oob_sc_data *oobd_remote =
371 oob_info->lesc.oob_config != BT_CONN_OOB_LOCAL_ONLY ?
372 &oob_sc_remote.le_sc_data :
373 NULL;
374
375 if (oobd_remote) {
376 /* Assume that oob_sc_remote
377 * corresponds to the currently connected peer
378 */
379 bt_addr_le_copy(&oob_sc_remote.addr, info.le.remote);
380 }
381
382 if (oobd_local &&
383 !bt_addr_le_eq(info.le.local, &oob_sc_local.addr)) {
384 bt_addr_le_to_str(info.le.local, addr, sizeof(addr));
385 LOG_DBG("No OOB data available for local %s",
386 addr);
387 bt_conn_auth_cancel(conn);
388 return;
389 }
390
391 err = bt_le_oob_set_sc_data(conn, oobd_local, oobd_remote);
392 if (err) {
393 LOG_DBG("bt_le_oob_set_sc_data failed with: %d", err);
394 }
395
396 break;
397 }
398 #endif /* !defined(CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY) */
399
400 #if !defined(CONFIG_BT_SMP_SC_PAIR_ONLY)
401 case BT_CONN_OOB_LE_LEGACY:
402 LOG_DBG("Legacy OOB TK requested from remote %s", addr);
403
404 err = bt_le_oob_set_legacy_tk(conn, oob_legacy_tk);
405 if (err < 0) {
406 LOG_ERR("Failed to set OOB TK: %d", err);
407 }
408
409 break;
410 #endif /* !defined(CONFIG_BT_SMP_SC_PAIR_ONLY) */
411 default:
412 LOG_ERR("Unhandled OOB type %d", oob_info->type);
413 break;
414 }
415 }
416
417 #if !defined(CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY)
get_oob_sc_local_data(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)418 static uint8_t get_oob_sc_local_data(const void *cmd, uint16_t cmd_len,
419 void *rsp, uint16_t *rsp_len)
420 {
421 struct btp_gap_oob_sc_get_local_data_rp *rp = rsp;
422
423 cb.oob_data_request = oob_data_request;
424
425 memcpy(rp->conf, &oob_sc_local.le_sc_data.c[0], sizeof(rp->conf));
426 memcpy(rp->rand, &oob_sc_local.le_sc_data.r[0], sizeof(rp->rand));
427
428 *rsp_len = sizeof(*rp);
429 return BTP_STATUS_SUCCESS;
430 }
431
set_oob_sc_remote_data(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)432 static uint8_t set_oob_sc_remote_data(const void *cmd, uint16_t cmd_len,
433 void *rsp, uint16_t *rsp_len)
434 {
435 const struct btp_gap_oob_sc_set_remote_data_cmd *cp = cmd;
436
437 cb.oob_data_request = oob_data_request;
438 bt_le_oob_set_sc_flag(true);
439
440 /* Note that the .addr field
441 * will be set by the oob_data_request callback
442 */
443 memcpy(&oob_sc_remote.le_sc_data.r[0], cp->rand,
444 sizeof(oob_sc_remote.le_sc_data.r));
445 memcpy(&oob_sc_remote.le_sc_data.c[0], cp->conf,
446 sizeof(oob_sc_remote.le_sc_data.c));
447
448 return BTP_STATUS_SUCCESS;
449 }
450 #endif /* !defined(CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY) */
451
set_connectable(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)452 static uint8_t set_connectable(const void *cmd, uint16_t cmd_len,
453 void *rsp, uint16_t *rsp_len)
454 {
455 const struct btp_gap_set_connectable_cmd *cp = cmd;
456 struct btp_gap_set_connectable_rp *rp = rsp;
457
458 if (cp->connectable) {
459 atomic_set_bit(¤t_settings, BTP_GAP_SETTINGS_CONNECTABLE);
460 } else {
461 atomic_clear_bit(¤t_settings, BTP_GAP_SETTINGS_CONNECTABLE);
462 }
463
464 rp->current_settings = sys_cpu_to_le32(current_settings);
465
466 *rsp_len = sizeof(*rp);
467
468 return BTP_STATUS_SUCCESS;
469 }
470
471 static uint8_t ad_flags = BT_LE_AD_NO_BREDR;
472 static struct bt_data ad[10] = {
473 BT_DATA(BT_DATA_FLAGS, &ad_flags, sizeof(ad_flags)),
474 };
475 static struct bt_data sd[10];
476
477 #if defined(CONFIG_BT_EXT_ADV)
478 static struct bt_le_ext_adv *ext_adv;
479
tester_gap_ext_adv_get(void)480 struct bt_le_ext_adv *tester_gap_ext_adv_get(void)
481 {
482 return ext_adv;
483 }
484
tester_gap_start_ext_adv(void)485 int tester_gap_start_ext_adv(void)
486 {
487 int err;
488
489 err = bt_le_ext_adv_start(ext_adv, BT_LE_EXT_ADV_START_DEFAULT);
490 if (err != 0) {
491 LOG_ERR("Failed to start advertising");
492
493 return -EINVAL;
494 }
495
496 atomic_set_bit(¤t_settings, BTP_GAP_SETTINGS_ADVERTISING);
497
498 return 0;
499 }
500
tester_gap_stop_ext_adv(void)501 int tester_gap_stop_ext_adv(void)
502 {
503 int err;
504
505 err = bt_le_ext_adv_stop(ext_adv);
506 if (err != 0) {
507 LOG_ERR("Failed to stop advertising");
508
509 return -EINVAL;
510 }
511
512 atomic_clear_bit(¤t_settings, BTP_GAP_SETTINGS_ADVERTISING);
513
514 return 0;
515 }
516 #endif /* defined(CONFIG_BT_EXT_ADV) */
517
set_discoverable(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)518 static uint8_t set_discoverable(const void *cmd, uint16_t cmd_len,
519 void *rsp, uint16_t *rsp_len)
520 {
521 const struct btp_gap_set_discoverable_cmd *cp = cmd;
522 struct btp_gap_set_discoverable_rp *rp = rsp;
523
524 switch (cp->discoverable) {
525 case BTP_GAP_NON_DISCOVERABLE:
526 ad_flags &= ~(BT_LE_AD_GENERAL | BT_LE_AD_LIMITED);
527 atomic_clear_bit(¤t_settings, BTP_GAP_SETTINGS_DISCOVERABLE);
528 break;
529 case BTP_GAP_GENERAL_DISCOVERABLE:
530 ad_flags &= ~BT_LE_AD_LIMITED;
531 ad_flags |= BT_LE_AD_GENERAL;
532 atomic_set_bit(¤t_settings, BTP_GAP_SETTINGS_DISCOVERABLE);
533 break;
534 case BTP_GAP_LIMITED_DISCOVERABLE:
535 ad_flags &= ~BT_LE_AD_GENERAL;
536 ad_flags |= BT_LE_AD_LIMITED;
537 atomic_set_bit(¤t_settings, BTP_GAP_SETTINGS_DISCOVERABLE);
538 break;
539 default:
540 return BTP_STATUS_FAILED;
541 }
542
543 rp->current_settings = sys_cpu_to_le32(current_settings);
544
545 *rsp_len = sizeof(*rp);
546 return BTP_STATUS_SUCCESS;
547 }
548
set_bondable(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)549 static uint8_t set_bondable(const void *cmd, uint16_t cmd_len,
550 void *rsp, uint16_t *rsp_len)
551 {
552 const struct btp_gap_set_bondable_cmd *cp = cmd;
553 struct btp_gap_set_bondable_rp *rp = rsp;
554
555 LOG_DBG("bondable: %d", cp->bondable);
556
557 if (cp->bondable) {
558 atomic_set_bit(¤t_settings, BTP_GAP_SETTINGS_BONDABLE);
559 } else {
560 atomic_clear_bit(¤t_settings, BTP_GAP_SETTINGS_BONDABLE);
561 }
562
563 bt_set_bondable(cp->bondable);
564
565 rp->current_settings = sys_cpu_to_le32(current_settings);
566 *rsp_len = sizeof(*rp);
567 return BTP_STATUS_SUCCESS;
568 }
569
tester_gap_create_adv_instance(struct bt_le_adv_param * param,uint8_t own_addr_type,const struct bt_data * ad,size_t ad_len,const struct bt_data * sd,size_t sd_len,uint32_t * settings)570 int tester_gap_create_adv_instance(struct bt_le_adv_param *param, uint8_t own_addr_type,
571 const struct bt_data *ad, size_t ad_len,
572 const struct bt_data *sd, size_t sd_len,
573 uint32_t *settings)
574 {
575 int err = 0;
576
577 if (settings != NULL) {
578 atomic_set(¤t_settings, *settings);
579 }
580
581 if (atomic_test_bit(¤t_settings, BTP_GAP_SETTINGS_CONNECTABLE)) {
582 param->options |= BT_LE_ADV_OPT_CONNECTABLE;
583
584 if (filter_list_in_use) {
585 param->options |= BT_LE_ADV_OPT_FILTER_CONN;
586 }
587 }
588
589 if (filter_list_in_use) {
590 param->options |= BT_LE_ADV_OPT_FILTER_SCAN_REQ;
591 }
592
593 switch (own_addr_type) {
594 case BTP_GAP_ADDR_TYPE_IDENTITY:
595 param->options |= BT_LE_ADV_OPT_USE_IDENTITY;
596 break;
597 #if defined(CONFIG_BT_PRIVACY)
598 case BTP_GAP_ADDR_TYPE_RESOLVABLE_PRIVATE:
599 /* RPA usage is is controlled via privacy settings */
600 if (!atomic_test_bit(¤t_settings, BTP_GAP_SETTINGS_PRIVACY)) {
601 return -EINVAL;
602 }
603 break;
604 case BTP_GAP_ADDR_TYPE_NON_RESOLVABLE_PRIVATE:
605 /* NRPA is used only for non-connectable advertising */
606 if (atomic_test_bit(¤t_settings, BTP_GAP_SETTINGS_CONNECTABLE)) {
607 return -EINVAL;
608 }
609 break;
610 #endif
611 default:
612 return -EINVAL;
613 }
614
615 if (IS_ENABLED(CONFIG_BT_EXT_ADV) && atomic_test_bit(¤t_settings,
616 BTP_GAP_SETTINGS_EXTENDED_ADVERTISING)) {
617 param->options |= BT_LE_ADV_OPT_EXT_ADV;
618 if (ext_adv != NULL) {
619 err = bt_le_ext_adv_stop(ext_adv);
620 if (err != 0) {
621 return err;
622 }
623
624 err = bt_le_ext_adv_delete(ext_adv);
625 if (err != 0) {
626 return err;
627 }
628
629 ext_adv = NULL;
630 }
631
632 err = bt_le_ext_adv_create(param, NULL, &ext_adv);
633 if (err != 0) {
634 return BTP_STATUS_FAILED;
635 }
636
637 err = bt_le_ext_adv_set_data(ext_adv, ad, ad_len, sd_len ? sd : NULL, sd_len);
638 }
639
640 return err;
641 }
642
start_advertising(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)643 static uint8_t start_advertising(const void *cmd, uint16_t cmd_len,
644 void *rsp, uint16_t *rsp_len)
645 {
646 const struct btp_gap_start_advertising_cmd *cp = cmd;
647 struct btp_gap_start_advertising_rp *rp = rsp;
648 struct bt_le_adv_param param = BT_LE_ADV_PARAM_INIT(BT_LE_ADV_OPT_ONE_TIME,
649 BT_GAP_ADV_FAST_INT_MIN_2,
650 BT_GAP_ADV_FAST_INT_MAX_2,
651 NULL);
652 uint8_t own_addr_type;
653 uint32_t duration;
654 uint8_t adv_len;
655 uint8_t sd_len;
656 int err;
657 int i;
658
659 /* This command is very unfortunate since after variable data there is
660 * additional 5 bytes (4 bytes for duration, 1 byte for own address
661 * type.
662 */
663 if ((cmd_len < sizeof(*cp)) ||
664 (cmd_len != sizeof(*cp) + cp->adv_data_len + cp->scan_rsp_len +
665 sizeof(duration) + sizeof(own_addr_type))) {
666 return BTP_STATUS_FAILED;
667 }
668
669 /* currently ignored */
670 duration = sys_get_le32(cp->adv_sr_data + cp->adv_data_len + cp->scan_rsp_len);
671 (void)duration;
672 own_addr_type = cp->adv_sr_data[cp->adv_data_len + cp->scan_rsp_len + sizeof(duration)];
673
674 for (i = 0, adv_len = 1U; i < cp->adv_data_len; adv_len++) {
675 if (adv_len >= ARRAY_SIZE(ad)) {
676 LOG_ERR("ad[] Out of memory");
677 return BTP_STATUS_FAILED;
678 }
679
680 ad[adv_len].type = cp->adv_sr_data[i++];
681 ad[adv_len].data_len = cp->adv_sr_data[i++];
682 ad[adv_len].data = &cp->adv_sr_data[i];
683 i += ad[adv_len].data_len;
684 }
685
686 for (sd_len = 0U; i < cp->adv_data_len + cp->scan_rsp_len; sd_len++) {
687 if (sd_len >= ARRAY_SIZE(sd)) {
688 LOG_ERR("sd[] Out of memory");
689 return BTP_STATUS_FAILED;
690 }
691
692 sd[sd_len].type = cp->adv_sr_data[i++];
693 sd[sd_len].data_len = cp->adv_sr_data[i++];
694 sd[sd_len].data = &cp->adv_sr_data[i];
695 i += sd[sd_len].data_len;
696 }
697
698 err = tester_gap_create_adv_instance(¶m, own_addr_type, ad, adv_len, sd, sd_len, NULL);
699 if (err != 0) {
700 return BTP_STATUS_FAILED;
701 }
702
703 #if defined(CONFIG_BT_EXT_ADV)
704 if (atomic_test_bit(¤t_settings, BTP_GAP_SETTINGS_EXTENDED_ADVERTISING)) {
705 err = bt_le_ext_adv_start(ext_adv, BT_LE_EXT_ADV_START_DEFAULT);
706 #else
707 if (0) {
708 #endif
709 } else {
710 err = bt_le_adv_start(¶m, ad, adv_len, sd_len ? sd : NULL, sd_len);
711 }
712
713 /* BTP API don't allow to set empty scan response data. */
714 if (err < 0) {
715 LOG_ERR("Failed to start advertising");
716
717 return BTP_STATUS_FAILED;
718 }
719
720 atomic_set_bit(¤t_settings, BTP_GAP_SETTINGS_ADVERTISING);
721 rp->current_settings = sys_cpu_to_le32(current_settings);
722
723 *rsp_len = sizeof(*rp);
724 return BTP_STATUS_SUCCESS;
725 }
726
727 static uint8_t start_directed_advertising(const void *cmd, uint16_t cmd_len,
728 void *rsp, uint16_t *rsp_len)
729 {
730 const struct btp_gap_start_directed_adv_cmd *cp = cmd;
731 struct btp_gap_start_directed_adv_rp *rp = rsp;
732 struct bt_le_adv_param adv_param;
733 uint16_t options = sys_le16_to_cpu(cp->options);
734
735 adv_param = *BT_LE_ADV_CONN_DIR(&cp->address);
736
737 if (!(options & BTP_GAP_START_DIRECTED_ADV_HD)) {
738 adv_param.options |= BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY;
739 adv_param.interval_max = BT_GAP_ADV_FAST_INT_MAX_2;
740 adv_param.interval_min = BT_GAP_ADV_FAST_INT_MIN_2;
741 }
742
743 if (options & BTP_GAP_START_DIRECTED_ADV_PEER_RPA) {
744 #if defined(CONFIG_BT_PRIVACY)
745 /* check if peer supports Central Address Resolution */
746 for (int i = 0; i < CONFIG_BT_MAX_PAIRED; i++) {
747 if (bt_addr_le_eq(&cp->address, &cars[i].addr)) {
748 if (cars[i].supported) {
749 adv_param.options |= BT_LE_ADV_OPT_DIR_ADDR_RPA;
750 }
751 }
752 }
753 #endif
754 }
755
756 if (bt_le_adv_start(&adv_param, NULL, 0, NULL, 0) < 0) {
757 LOG_ERR("Failed to start advertising");
758 return BTP_STATUS_FAILED;
759 }
760
761 atomic_set_bit(¤t_settings, BTP_GAP_SETTINGS_ADVERTISING);
762 rp->current_settings = sys_cpu_to_le32(current_settings);
763
764 *rsp_len = sizeof(*rp);
765 return BTP_STATUS_SUCCESS;
766 }
767
768 static uint8_t stop_advertising(const void *cmd, uint16_t cmd_len,
769 void *rsp, uint16_t *rsp_len)
770 {
771 struct btp_gap_stop_advertising_rp *rp = rsp;
772 int err;
773
774 err = bt_le_adv_stop();
775 if (err < 0) {
776 tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_STOP_ADVERTISING, BTP_STATUS_FAILED);
777 LOG_ERR("Failed to stop advertising: %d", err);
778 return BTP_STATUS_FAILED;
779 }
780
781 atomic_clear_bit(¤t_settings, BTP_GAP_SETTINGS_ADVERTISING);
782 rp->current_settings = sys_cpu_to_le32(current_settings);
783
784 *rsp_len = sizeof(*rp);
785 return BTP_STATUS_SUCCESS;
786 }
787
788 static uint8_t get_ad_flags(struct net_buf_simple *buf_ad)
789 {
790 uint8_t len, i;
791
792 /* Parse advertisement to get flags */
793 for (i = 0U; i < buf_ad->len; i += len - 1) {
794 len = buf_ad->data[i++];
795 if (!len) {
796 break;
797 }
798
799 /* Check if field length is correct */
800 if (len > (buf_ad->len - i) || (buf_ad->len - i) < 1) {
801 break;
802 }
803
804 switch (buf_ad->data[i++]) {
805 case BT_DATA_FLAGS:
806 return buf_ad->data[i];
807 default:
808 break;
809 }
810 }
811
812 return 0;
813 }
814
815 static uint8_t discovery_flags;
816 static struct net_buf_simple *adv_buf = NET_BUF_SIMPLE(ADV_BUF_LEN);
817
818 static void store_adv(const bt_addr_le_t *addr, int8_t rssi,
819 struct net_buf_simple *buf_ad)
820 {
821 struct btp_gap_device_found_ev *ev;
822
823 /* cleanup */
824 net_buf_simple_init(adv_buf, 0);
825
826 ev = net_buf_simple_add(adv_buf, sizeof(*ev));
827
828 bt_addr_le_copy(&ev->address, addr);
829 ev->rssi = rssi;
830 ev->flags = BTP_GAP_DEVICE_FOUND_FLAG_AD | BTP_GAP_DEVICE_FOUND_FLAG_RSSI;
831 ev->eir_data_len = buf_ad->len;
832 memcpy(net_buf_simple_add(adv_buf, buf_ad->len), buf_ad->data, buf_ad->len);
833 }
834
835 static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t evtype,
836 struct net_buf_simple *buf_ad)
837 {
838 /* if General/Limited Discovery - parse Advertising data to get flags */
839 if (!(discovery_flags & BTP_GAP_DISCOVERY_FLAG_LE_OBSERVE) &&
840 (evtype != BT_GAP_ADV_TYPE_SCAN_RSP)) {
841 uint8_t flags = get_ad_flags(buf_ad);
842
843 /* ignore non-discoverable devices */
844 if (!(flags & BT_LE_AD_DISCOV_MASK)) {
845 LOG_DBG("Non discoverable, skipping");
846 return;
847 }
848
849 /* if Limited Discovery - ignore general discoverable devices */
850 if ((discovery_flags & BTP_GAP_DISCOVERY_FLAG_LIMITED) &&
851 !(flags & BT_LE_AD_LIMITED)) {
852 LOG_DBG("General discoverable, skipping");
853 return;
854 }
855 }
856
857 /* attach Scan Response data */
858 if (evtype == BT_GAP_ADV_TYPE_SCAN_RSP) {
859 struct btp_gap_device_found_ev *ev;
860 bt_addr_le_t a;
861
862 /* skip if there is no pending advertisement */
863 if (!adv_buf->len) {
864 LOG_INF("No pending advertisement, skipping");
865 return;
866 }
867
868 ev = (void *) adv_buf->data;
869
870 bt_addr_le_copy(&a, &ev->address);
871
872 /*
873 * in general, the Scan Response comes right after the
874 * Advertisement, but if not if send stored event and ignore
875 * this one
876 */
877 if (!bt_addr_le_eq(addr, &a)) {
878 LOG_INF("Address does not match, skipping");
879 goto done;
880 }
881
882 ev->eir_data_len += buf_ad->len;
883 ev->flags |= BTP_GAP_DEVICE_FOUND_FLAG_SD;
884
885 memcpy(net_buf_simple_add(adv_buf, buf_ad->len), buf_ad->data, buf_ad->len);
886
887 goto done;
888 }
889
890 /*
891 * if there is another pending advertisement, send it and store the
892 * current one
893 */
894 if (adv_buf->len) {
895 tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_FOUND,
896 adv_buf->data, adv_buf->len);
897 net_buf_simple_reset(adv_buf);
898 }
899
900 store_adv(addr, rssi, buf_ad);
901
902 /* if Active Scan and scannable event - wait for Scan Response */
903 if ((discovery_flags & BTP_GAP_DISCOVERY_FLAG_LE_ACTIVE_SCAN) &&
904 (evtype == BT_GAP_ADV_TYPE_ADV_IND ||
905 evtype == BT_GAP_ADV_TYPE_ADV_SCAN_IND)) {
906 LOG_DBG("Waiting for scan response");
907 return;
908 }
909 done:
910 tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_FOUND,
911 adv_buf->data, adv_buf->len);
912 net_buf_simple_reset(adv_buf);
913 }
914
915 static uint8_t start_discovery(const void *cmd, uint16_t cmd_len,
916 void *rsp, uint16_t *rsp_len)
917 {
918 const struct btp_gap_start_discovery_cmd *cp = cmd;
919
920 /* only LE scan is supported */
921 if (cp->flags & BTP_GAP_DISCOVERY_FLAG_BREDR) {
922 LOG_WRN("BR/EDR not supported");
923 return BTP_STATUS_FAILED;
924 }
925
926 if (bt_le_scan_start(cp->flags & BTP_GAP_DISCOVERY_FLAG_LE_ACTIVE_SCAN ?
927 BT_LE_SCAN_ACTIVE : BT_LE_SCAN_PASSIVE,
928 device_found) < 0) {
929 LOG_ERR("Failed to start scanning");
930 return BTP_STATUS_FAILED;
931 }
932
933 net_buf_simple_init(adv_buf, 0);
934 discovery_flags = cp->flags;
935
936 return BTP_STATUS_SUCCESS;
937 }
938
939 static uint8_t stop_discovery(const void *cmd, uint16_t cmd_len,
940 void *rsp, uint16_t *rsp_len)
941 {
942 int err;
943
944 err = bt_le_scan_stop();
945 if (err < 0) {
946 LOG_ERR("Failed to stop scanning: %d", err);
947 return BTP_STATUS_FAILED;
948 }
949
950 return BTP_STATUS_SUCCESS;
951 }
952
953 static uint8_t connect(const void *cmd, uint16_t cmd_len,
954 void *rsp, uint16_t *rsp_len)
955 {
956 const struct btp_gap_connect_cmd *cp = cmd;
957 int err;
958
959 if (!bt_addr_le_eq(&cp->address, BT_ADDR_LE_ANY)) {
960 struct bt_conn *conn;
961
962 err = bt_conn_le_create(&cp->address, BT_CONN_LE_CREATE_CONN,
963 BT_LE_CONN_PARAM_DEFAULT, &conn);
964 if (err) {
965 LOG_ERR("Failed to create connection (%d)", err);
966 return BTP_STATUS_FAILED;
967 }
968
969 bt_conn_unref(conn);
970 } else {
971 err = bt_conn_le_create_auto(BT_CONN_LE_CREATE_CONN,
972 BT_LE_CONN_PARAM_DEFAULT);
973 if (err) {
974 LOG_ERR("Failed to create auto connection (%d)", err);
975 return BTP_STATUS_FAILED;
976 }
977 }
978
979 return BTP_STATUS_SUCCESS;
980 }
981
982 static uint8_t disconnect(const void *cmd, uint16_t cmd_len,
983 void *rsp, uint16_t *rsp_len)
984 {
985 const struct btp_gap_disconnect_cmd *cp = cmd;
986 struct bt_conn *conn;
987 uint8_t status;
988
989 conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &cp->address);
990 if (!conn) {
991 LOG_ERR("Unknown connection");
992 return BTP_STATUS_FAILED;
993 }
994
995 if (bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN)) {
996 LOG_ERR("Failed to disconnect");
997 status = BTP_STATUS_FAILED;
998 } else {
999 status = BTP_STATUS_SUCCESS;
1000 }
1001
1002 bt_conn_unref(conn);
1003
1004 return status;
1005 }
1006
1007 static void auth_passkey_display(struct bt_conn *conn, unsigned int passkey)
1008 {
1009 struct btp_gap_passkey_display_ev ev;
1010 const bt_addr_le_t *addr = bt_conn_get_dst(conn);
1011
1012 bt_addr_le_copy(&ev.address, addr);
1013 ev.passkey = sys_cpu_to_le32(passkey);
1014
1015 tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_PASSKEY_DISPLAY, &ev, sizeof(ev));
1016 }
1017
1018 static void auth_passkey_entry(struct bt_conn *conn)
1019 {
1020 struct btp_gap_passkey_entry_req_ev ev;
1021 const bt_addr_le_t *addr = bt_conn_get_dst(conn);
1022
1023 bt_addr_le_copy(&ev.address, addr);
1024
1025 tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_PASSKEY_ENTRY_REQ, &ev, sizeof(ev));
1026 }
1027
1028 static void auth_passkey_confirm(struct bt_conn *conn, unsigned int passkey)
1029 {
1030 struct btp_gap_passkey_confirm_req_ev ev;
1031 const bt_addr_le_t *addr = bt_conn_get_dst(conn);
1032
1033 bt_addr_le_copy(&ev.address, addr);
1034 ev.passkey = sys_cpu_to_le32(passkey);
1035
1036 tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_PASSKEY_CONFIRM_REQ, &ev, sizeof(ev));
1037 }
1038
1039 static void auth_cancel(struct bt_conn *conn)
1040 {
1041 /* TODO */
1042 }
1043
1044 enum bt_security_err auth_pairing_accept(struct bt_conn *conn,
1045 const struct bt_conn_pairing_feat *const feat)
1046 {
1047 struct btp_gap_bond_lost_ev ev;
1048 const bt_addr_le_t *addr = bt_conn_get_dst(conn);
1049
1050 if (!bt_addr_le_is_bonded(BT_ID_DEFAULT, addr)) {
1051 return BT_SECURITY_ERR_SUCCESS;
1052 }
1053
1054 /* If a peer is already bonded and tries to pair again then it means that
1055 * the it has lost its bond information.
1056 */
1057 LOG_DBG("Bond lost");
1058
1059 bt_addr_le_copy(&ev.address, addr);
1060
1061 tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_BOND_LOST, &ev, sizeof(ev));
1062
1063 return BT_SECURITY_ERR_SUCCESS;
1064 }
1065
1066 void auth_pairing_failed(struct bt_conn *conn, enum bt_security_err reason)
1067 {
1068 struct btp_gap_bond_pairing_failed_ev ev;
1069 const bt_addr_le_t *addr = bt_conn_get_dst(conn);
1070
1071 bt_addr_le_copy(&ev.address, addr);
1072 ev.reason = reason;
1073
1074 tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_PAIRING_FAILED, &ev, sizeof(ev));
1075 }
1076
1077 static void auth_pairing_complete(struct bt_conn *conn, bool bonded)
1078 {
1079 #if defined(CONFIG_BT_PRIVACY)
1080 /* Read peer's Central Address Resolution if bonded */
1081 if (bonded) {
1082 bt_gatt_read(conn, &read_car_params);
1083 }
1084 #endif
1085 }
1086
1087 static struct bt_conn_auth_info_cb auth_info_cb = {
1088 .pairing_failed = auth_pairing_failed,
1089 .pairing_complete = auth_pairing_complete,
1090 };
1091
1092 static uint8_t set_io_cap(const void *cmd, uint16_t cmd_len,
1093 void *rsp, uint16_t *rsp_len)
1094 {
1095 const struct btp_gap_set_io_cap_cmd *cp = cmd;
1096
1097 /* Reset io cap requirements */
1098 (void)memset(&cb, 0, sizeof(cb));
1099 bt_conn_auth_cb_register(NULL);
1100
1101 LOG_DBG("io_cap: %d", cp->io_cap);
1102
1103 switch (cp->io_cap) {
1104 case BTP_GAP_IO_CAP_DISPLAY_ONLY:
1105 cb.cancel = auth_cancel;
1106 cb.passkey_display = auth_passkey_display;
1107 break;
1108 case BTP_GAP_IO_CAP_KEYBOARD_DISPLAY:
1109 cb.cancel = auth_cancel;
1110 cb.passkey_display = auth_passkey_display;
1111 cb.passkey_entry = auth_passkey_entry;
1112 cb.passkey_confirm = auth_passkey_confirm;
1113 break;
1114 case BTP_GAP_IO_CAP_NO_INPUT_OUTPUT:
1115 cb.cancel = auth_cancel;
1116 break;
1117 case BTP_GAP_IO_CAP_KEYBOARD_ONLY:
1118 cb.cancel = auth_cancel;
1119 cb.passkey_entry = auth_passkey_entry;
1120 break;
1121 case BTP_GAP_IO_CAP_DISPLAY_YESNO:
1122 cb.cancel = auth_cancel;
1123 cb.passkey_display = auth_passkey_display;
1124 cb.passkey_confirm = auth_passkey_confirm;
1125 break;
1126 default:
1127 LOG_WRN("Unhandled io_cap: 0x%x", cp->io_cap);
1128 return BTP_STATUS_FAILED;
1129 }
1130
1131 cb.pairing_accept = auth_pairing_accept;
1132
1133 if (bt_conn_auth_cb_register(&cb)) {
1134 return BTP_STATUS_FAILED;
1135 }
1136
1137 return BTP_STATUS_SUCCESS;
1138 }
1139
1140 static uint8_t pair(const void *cmd, uint16_t cmd_len,
1141 void *rsp, uint16_t *rsp_len)
1142 {
1143 const struct btp_gap_pair_cmd *cp = cmd;
1144 struct bt_conn *conn;
1145 int err;
1146
1147 conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &cp->address);
1148 if (!conn) {
1149 LOG_ERR("Unknown connection");
1150 return BTP_STATUS_FAILED;
1151 }
1152
1153 err = bt_conn_set_security(conn, BT_SECURITY_L2);
1154 if (err < 0) {
1155 LOG_ERR("Failed to set security: %d", err);
1156 bt_conn_unref(conn);
1157 return BTP_STATUS_FAILED;
1158 }
1159
1160 bt_conn_unref(conn);
1161 return BTP_STATUS_SUCCESS;
1162 }
1163
1164 static uint8_t unpair(const void *cmd, uint16_t cmd_len,
1165 void *rsp, uint16_t *rsp_len)
1166 {
1167 const struct btp_gap_unpair_cmd *cp = cmd;
1168 struct bt_conn *conn;
1169 int err;
1170
1171 conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &cp->address);
1172 if (!conn) {
1173 LOG_INF("Unknown connection");
1174 goto keys;
1175 }
1176
1177 err = bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
1178
1179 bt_conn_unref(conn);
1180
1181 if (err < 0) {
1182 LOG_ERR("Failed to disconnect: %d", err);
1183 return BTP_STATUS_FAILED;
1184 }
1185 keys:
1186 err = bt_unpair(BT_ID_DEFAULT, &cp->address);
1187 if (err < 0) {
1188 return BTP_STATUS_FAILED;
1189 }
1190
1191 return BTP_STATUS_SUCCESS;
1192 }
1193
1194 static uint8_t passkey_entry(const void *cmd, uint16_t cmd_len,
1195 void *rsp, uint16_t *rsp_len)
1196 {
1197 const struct btp_gap_passkey_entry_cmd *cp = cmd;
1198 struct bt_conn *conn;
1199 int err;
1200
1201 conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &cp->address);
1202 if (!conn) {
1203 LOG_ERR("Unknown connection");
1204 return BTP_STATUS_FAILED;
1205 }
1206
1207 err = bt_conn_auth_passkey_entry(conn, sys_le32_to_cpu(cp->passkey));
1208 bt_conn_unref(conn);
1209
1210 if (err < 0) {
1211 LOG_ERR("Failed to enter passkey: %d", err);
1212 return BTP_STATUS_FAILED;
1213 }
1214
1215 return BTP_STATUS_SUCCESS;
1216 }
1217
1218 static uint8_t passkey_confirm(const void *cmd, uint16_t cmd_len,
1219 void *rsp, uint16_t *rsp_len)
1220 {
1221 const struct btp_gap_passkey_confirm_cmd *cp = cmd;
1222 struct bt_conn *conn;
1223 int err;
1224
1225 conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &cp->address);
1226 if (!conn) {
1227 LOG_ERR("Unknown connection");
1228 return BTP_STATUS_FAILED;
1229 }
1230
1231 if (cp->match) {
1232 err = bt_conn_auth_passkey_confirm(conn);
1233 if (err < 0) {
1234 LOG_ERR("Failed to confirm passkey: %d", err);
1235 }
1236 } else {
1237 err = bt_conn_auth_cancel(conn);
1238 if (err < 0) {
1239 LOG_ERR("Failed to cancel auth: %d", err);
1240 }
1241 }
1242
1243 bt_conn_unref(conn);
1244
1245 if (err < 0) {
1246 return BTP_STATUS_FAILED;
1247 }
1248
1249 return BTP_STATUS_SUCCESS;
1250 }
1251
1252 static uint8_t conn_param_update(const void *cmd, uint16_t cmd_len,
1253 void *rsp, uint16_t *rsp_len)
1254 {
1255 const struct btp_gap_conn_param_update_cmd *cp = cmd;
1256 struct bt_le_conn_param param = {
1257 .interval_min = sys_le16_to_cpu(cp->interval_min),
1258 .interval_max = sys_le16_to_cpu(cp->interval_max),
1259 .latency = sys_le16_to_cpu(cp->latency),
1260 .timeout = sys_le16_to_cpu(cp->timeout),
1261 };
1262 struct bt_conn *conn;
1263 int err;
1264
1265 conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &cp->address);
1266 if (!conn) {
1267 LOG_ERR("Unknown connection");
1268 return BTP_STATUS_FAILED;
1269 }
1270
1271 err = bt_conn_le_param_update(conn, ¶m);
1272 bt_conn_unref(conn);
1273
1274 if (err < 0) {
1275 LOG_ERR("Failed to update params: %d", err);
1276 return BTP_STATUS_FAILED;
1277 }
1278 return BTP_STATUS_SUCCESS;
1279 }
1280
1281 static uint8_t set_mitm(const void *cmd, uint16_t cmd_len,
1282 void *rsp, uint16_t *rsp_len)
1283 {
1284 /* TODO verify if can be done in runtime */
1285 LOG_WRN("Use CONFIG_BT_SMP_ENFORCE_MITM instead");
1286
1287 return BTP_STATUS_SUCCESS;
1288 }
1289
1290 static uint8_t set_oob_legacy_data(const void *cmd, uint16_t cmd_len,
1291 void *rsp, uint16_t *rsp_len)
1292 {
1293 const struct btp_gap_oob_legacy_set_data_cmd *cp = cmd;
1294
1295 memcpy(oob_legacy_tk, cp->oob_data, 16);
1296
1297 bt_le_oob_set_legacy_flag(true);
1298 cb.oob_data_request = oob_data_request;
1299
1300 return BTP_STATUS_SUCCESS;
1301 }
1302
1303 static uint8_t set_filter_list(const void *cmd, uint16_t cmd_len,
1304 void *rsp, uint16_t *rsp_len)
1305 {
1306 const struct btp_gap_set_filter_list *cp = cmd;
1307 int err;
1308
1309 if ((cmd_len < sizeof(*cp)) ||
1310 (cmd_len != sizeof(*cp) + (cp->cnt * sizeof(cp->addr[0])))) {
1311 return BTP_STATUS_FAILED;
1312 }
1313
1314 (void)bt_le_filter_accept_list_clear();
1315
1316 for (int i = 0; i < cp->cnt; i++) {
1317 err = bt_le_filter_accept_list_add(&cp->addr[i]);
1318 if (err < 0) {
1319 return BTP_STATUS_FAILED;
1320 }
1321 }
1322
1323 filter_list_in_use = cp->cnt != 0;
1324
1325 return BTP_STATUS_SUCCESS;
1326 }
1327
1328 static uint8_t set_extended_advertising(const void *cmd, uint16_t cmd_len,
1329 void *rsp, uint16_t *rsp_len)
1330 {
1331 const struct btp_gap_set_extended_advertising_cmd *cp = cmd;
1332 struct btp_gap_set_extended_advertising_rp *rp = rsp;
1333
1334 LOG_DBG("ext adv settings: %u", cp->settings);
1335
1336 if (cp->settings != 0) {
1337 atomic_set_bit(¤t_settings,
1338 BTP_GAP_SETTINGS_EXTENDED_ADVERTISING);
1339 } else {
1340 atomic_clear_bit(¤t_settings,
1341 BTP_GAP_SETTINGS_EXTENDED_ADVERTISING);
1342 }
1343
1344 rp->current_settings = sys_cpu_to_le32(current_settings);
1345
1346 *rsp_len = sizeof(*rp);
1347 return BTP_STATUS_SUCCESS;
1348 }
1349
1350 #if defined(CONFIG_BT_PER_ADV)
1351 static struct bt_data padv[10];
1352 static struct bt_le_per_adv_sync *pa_sync;
1353
1354 struct bt_le_per_adv_sync *tester_gap_padv_get(void)
1355 {
1356 return pa_sync;
1357 }
1358
1359 static void pa_sync_synced_cb(struct bt_le_per_adv_sync *sync,
1360 struct bt_le_per_adv_sync_synced_info *info)
1361 {
1362 LOG_DBG("");
1363
1364 if (sync == pa_sync) {
1365 struct btp_gap_ev_periodic_sync_established_ev ev;
1366
1367 bt_addr_le_copy(&ev.address, info->addr);
1368 ev.sync_handle = sys_cpu_to_le16(sync->handle);
1369 ev.status = 0;
1370
1371 tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_PERIODIC_SYNC_ESTABLISHED,
1372 &ev, sizeof(ev));
1373 }
1374 }
1375
1376 static void pa_sync_terminated_cb(struct bt_le_per_adv_sync *sync,
1377 const struct bt_le_per_adv_sync_term_info *info)
1378 {
1379 LOG_DBG("");
1380
1381 if (sync == pa_sync) {
1382 struct btp_gap_ev_periodic_sync_lost_ev ev;
1383
1384 LOG_DBG("PA sync lost with reason %u", info->reason);
1385 pa_sync = NULL;
1386
1387 ev.sync_handle = sys_cpu_to_le16(sync->handle);
1388 ev.reason = info->reason;
1389
1390 tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_PERIODIC_SYNC_LOST,
1391 &ev, sizeof(ev));
1392 }
1393 }
1394
1395 static struct bt_le_per_adv_sync_cb pa_sync_cb = {
1396 .synced = pa_sync_synced_cb,
1397 .term = pa_sync_terminated_cb,
1398 };
1399
1400 int tester_gap_padv_configure(const struct bt_le_per_adv_param *param)
1401 {
1402 int err;
1403 struct bt_le_adv_param ext_adv_param = BT_LE_ADV_PARAM_INIT(BT_LE_ADV_OPT_ONE_TIME,
1404 param->interval_min,
1405 param->interval_max,
1406 NULL);
1407
1408 if (ext_adv == NULL) {
1409 current_settings = BIT(BTP_GAP_SETTINGS_DISCOVERABLE) |
1410 BIT(BTP_GAP_SETTINGS_EXTENDED_ADVERTISING);
1411 err = tester_gap_create_adv_instance(&ext_adv_param, BTP_GAP_ADDR_TYPE_IDENTITY, ad,
1412 1, NULL, 0, NULL);
1413 if (err != 0) {
1414 return -EINVAL;
1415 }
1416 }
1417
1418 /* Set periodic advertising parameters and the required
1419 * bit in AD Flags of extended advertising.
1420 */
1421 err = bt_le_per_adv_set_param(ext_adv, param);
1422 if (err != 0) {
1423 LOG_DBG("Failed to set periodic advertising parameters (err %d)\n", err);
1424 }
1425
1426 return err;
1427 }
1428
1429 static uint8_t padv_configure(const void *cmd, uint16_t cmd_len,
1430 void *rsp, uint16_t *rsp_len)
1431 {
1432 int err;
1433 uint32_t options = BT_LE_PER_ADV_OPT_NONE;
1434 const struct btp_gap_padv_configure_cmd *cp = cmd;
1435 struct btp_gap_padv_configure_rp *rp = rsp;
1436
1437 if (cp->flags & BTP_GAP_PADV_INCLUDE_TX_POWER) {
1438 options |= BT_LE_PER_ADV_OPT_USE_TX_POWER;
1439 }
1440
1441 err = tester_gap_padv_configure(BT_LE_PER_ADV_PARAM(sys_le16_to_cpu(cp->interval_min),
1442 sys_le16_to_cpu(cp->interval_max),
1443 options));
1444 if (err) {
1445 return BTP_STATUS_FAILED;
1446 }
1447
1448 rp->current_settings = sys_cpu_to_le32(current_settings);
1449
1450 *rsp_len = sizeof(*rp);
1451
1452 return BTP_STATUS_SUCCESS;
1453 }
1454
1455 int tester_gap_padv_start(void)
1456 {
1457 int err;
1458
1459 if (ext_adv == NULL) {
1460 return -EINVAL;
1461 }
1462
1463 if (!atomic_test_bit(¤t_settings, BTP_GAP_SETTINGS_ADVERTISING)) {
1464 err = tester_gap_start_ext_adv();
1465 if (err != 0) {
1466 return -EINVAL;
1467 }
1468 }
1469
1470 /* Enable Periodic Advertising */
1471 err = bt_le_per_adv_start(ext_adv);
1472 if (err != 0) {
1473 LOG_DBG("Failed to start periodic advertising data: %d", err);
1474 }
1475
1476 return err;
1477 }
1478
1479 static uint8_t padv_start(const void *cmd, uint16_t cmd_len,
1480 void *rsp, uint16_t *rsp_len)
1481 {
1482 int err;
1483 struct btp_gap_padv_start_rp *rp = rsp;
1484
1485 err = tester_gap_padv_start();
1486
1487 if (err) {
1488 return BTP_STATUS_FAILED;
1489 }
1490
1491 rp->current_settings = sys_cpu_to_le32(current_settings);
1492
1493 *rsp_len = sizeof(*rp);
1494
1495 return BTP_STATUS_SUCCESS;
1496 }
1497
1498 int tester_gap_padv_stop(void)
1499 {
1500 int err;
1501
1502 if (ext_adv == NULL) {
1503 return -EINVAL;
1504 }
1505
1506 /* Enable Periodic Advertising */
1507 err = bt_le_per_adv_stop(ext_adv);
1508 if (err != 0) {
1509 LOG_DBG("Failed to stop periodic advertising data: %d", err);
1510 }
1511
1512 return err;
1513 }
1514
1515 static uint8_t padv_stop(const void *cmd, uint16_t cmd_len,
1516 void *rsp, uint16_t *rsp_len)
1517 {
1518 int err;
1519 struct btp_gap_padv_stop_rp *rp = rsp;
1520
1521 err = tester_gap_padv_stop();
1522
1523 if (err) {
1524 return BTP_STATUS_FAILED;
1525 }
1526
1527 rp->current_settings = sys_cpu_to_le32(current_settings);
1528
1529 *rsp_len = sizeof(*rp);
1530
1531 return BTP_STATUS_SUCCESS;
1532 }
1533
1534 int tester_gap_padv_set_data(struct bt_data *per_ad, uint8_t ad_len)
1535 {
1536 int err;
1537
1538 if (ext_adv == NULL) {
1539 return -EINVAL;
1540 }
1541
1542 /* Set Periodic Advertising data */
1543 err = bt_le_per_adv_set_data(ext_adv, per_ad, ad_len);
1544 if (err != 0) {
1545 LOG_DBG("Failed to set periodic advertising data: %d", err);
1546 }
1547
1548 return err;
1549 }
1550
1551 static uint8_t padv_set_data(const void *cmd, uint16_t cmd_len,
1552 void *rsp, uint16_t *rsp_len)
1553 {
1554 int err;
1555 uint8_t padv_len = 0U;
1556 const struct btp_gap_padv_set_data_cmd *cp = cmd;
1557
1558 for (uint8_t i = 0; i < cp->data_len; padv_len++) {
1559 if (padv_len >= ARRAY_SIZE(padv)) {
1560 LOG_ERR("padv[] Out of memory");
1561 return BTP_STATUS_FAILED;
1562 }
1563
1564 padv[padv_len].data_len = cp->data[i++] - 1;
1565 padv[padv_len].type = cp->data[i++];
1566 padv[padv_len].data = &cp->data[i];
1567 i += padv[padv_len].data_len;
1568 }
1569
1570 err = tester_gap_padv_set_data(padv, padv_len);
1571
1572 return BTP_STATUS_VAL(err);
1573 }
1574
1575 int tester_gap_padv_create_sync(struct bt_le_per_adv_sync_param *create_params)
1576 {
1577 int err;
1578
1579 if (pa_sync != NULL) {
1580 return -EBUSY;
1581 }
1582
1583 err = bt_le_per_adv_sync_create(create_params, &pa_sync);
1584
1585 if (err != 0) {
1586 LOG_DBG("Unable to sync to PA: %d", err);
1587 }
1588
1589 return err;
1590 }
1591
1592 int tester_gap_padv_stop_sync(void)
1593 {
1594 int err;
1595
1596 if (pa_sync == NULL) {
1597 return -EALREADY;
1598 }
1599
1600 err = bt_le_per_adv_sync_delete(pa_sync);
1601 if (err != 0) {
1602 LOG_DBG("Unable to stop sync to PA: %d", err);
1603 }
1604
1605 return err;
1606 }
1607
1608 static uint8_t padv_create_sync(const void *cmd, uint16_t cmd_len,
1609 void *rsp, uint16_t *rsp_len)
1610 {
1611 int err;
1612 const struct btp_gap_padv_create_sync_cmd *cp = cmd;
1613 struct bt_le_per_adv_sync_param create_params = {0};
1614
1615 bt_addr_le_copy(&create_params.addr, &cp->address);
1616 create_params.options = BT_LE_PER_ADV_SYNC_OPT_NONE;
1617 create_params.sid = cp->advertiser_sid;
1618 create_params.skip = sys_le16_to_cpu(cp->skip);
1619 create_params.timeout = sys_le16_to_cpu(cp->sync_timeout);
1620
1621 if (cp->flags & BTP_GAP_PADV_CREATE_SYNC_FLAG_REPORTS_DISABLED) {
1622 create_params.options |= BT_LE_PER_ADV_SYNC_OPT_REPORTING_INITIALLY_DISABLED;
1623 }
1624
1625 if (cp->flags & BTP_GAP_PADV_CREATE_SYNC_FLAG_FILTER_DUPLICATES) {
1626 create_params.options |= BT_LE_PER_ADV_SYNC_OPT_FILTER_DUPLICATE;
1627 }
1628
1629 err = tester_gap_padv_create_sync(&create_params);
1630
1631 return BTP_STATUS_VAL(err);
1632 }
1633
1634 static uint8_t padv_sync_transfer_set_info(const void *cmd, uint16_t cmd_len,
1635 void *rsp, uint16_t *rsp_len)
1636 {
1637 const struct btp_gap_padv_sync_transfer_set_info_cmd *cp = cmd;
1638 (void)cp;
1639
1640 /* TODO */
1641
1642 return BTP_STATUS_FAILED;
1643 }
1644
1645 static uint8_t padv_sync_transfer_start(const void *cmd, uint16_t cmd_len,
1646 void *rsp, uint16_t *rsp_len)
1647 {
1648 const struct btp_gap_padv_sync_transfer_start_cmd *cp = cmd;
1649 (void)cp;
1650
1651 /* TODO */
1652
1653 return BTP_STATUS_FAILED;
1654 }
1655
1656 static uint8_t padv_sync_transfer_recv(const void *cmd, uint16_t cmd_len,
1657 void *rsp, uint16_t *rsp_len)
1658 {
1659 const struct btp_gap_padv_sync_transfer_recv_cmd *cp = cmd;
1660 (void)cp;
1661
1662 /* TODO */
1663
1664 return BTP_STATUS_FAILED;
1665 }
1666 #endif /* defined(CONFIG_BT_PER_ADV) */
1667
1668 static const struct btp_handler handlers[] = {
1669 {
1670 .opcode = BTP_GAP_READ_SUPPORTED_COMMANDS,
1671 .index = BTP_INDEX_NONE,
1672 .expect_len = 0,
1673 .func = supported_commands,
1674 },
1675 {
1676 .opcode = BTP_GAP_READ_CONTROLLER_INDEX_LIST,
1677 .index = BTP_INDEX_NONE,
1678 .expect_len = 0,
1679 .func = controller_index_list,
1680 },
1681 {
1682 .opcode = BTP_GAP_READ_CONTROLLER_INFO,
1683 .expect_len = 0,
1684 .func = controller_info,
1685 },
1686 {
1687 .opcode = BTP_GAP_SET_CONNECTABLE,
1688 .expect_len = sizeof(struct btp_gap_set_connectable_cmd),
1689 .func = set_connectable,
1690 },
1691 {
1692 .opcode = BTP_GAP_SET_DISCOVERABLE,
1693 .expect_len = sizeof(struct btp_gap_set_discoverable_cmd),
1694 .func = set_discoverable,
1695 },
1696 {
1697 .opcode = BTP_GAP_SET_BONDABLE,
1698 .expect_len = sizeof(struct btp_gap_set_bondable_cmd),
1699 .func = set_bondable,
1700 },
1701 {
1702 .opcode = BTP_GAP_START_ADVERTISING,
1703 .expect_len = BTP_HANDLER_LENGTH_VARIABLE,
1704 .func = start_advertising,
1705 },
1706 {
1707 .opcode = BTP_GAP_START_DIRECTED_ADV,
1708 .expect_len = sizeof(struct btp_gap_start_directed_adv_cmd),
1709 .func = start_directed_advertising,
1710 },
1711 {
1712 .opcode = BTP_GAP_STOP_ADVERTISING,
1713 .expect_len = 0,
1714 .func = stop_advertising,
1715 },
1716 {
1717 .opcode = BTP_GAP_START_DISCOVERY,
1718 .expect_len = sizeof(struct btp_gap_start_discovery_cmd),
1719 .func = start_discovery,
1720 },
1721 {
1722 .opcode = BTP_GAP_STOP_DISCOVERY,
1723 .expect_len = 0,
1724 .func = stop_discovery,
1725 },
1726 {
1727 .opcode = BTP_GAP_CONNECT,
1728 .expect_len = sizeof(struct btp_gap_connect_cmd),
1729 .func = connect,
1730 },
1731 {
1732 .opcode = BTP_GAP_DISCONNECT,
1733 .expect_len = sizeof(struct btp_gap_disconnect_cmd),
1734 .func = disconnect,
1735 },
1736 {
1737 .opcode = BTP_GAP_SET_IO_CAP,
1738 .expect_len = sizeof(struct btp_gap_set_io_cap_cmd),
1739 .func = set_io_cap,
1740 },
1741 {
1742 .opcode = BTP_GAP_PAIR,
1743 .expect_len = sizeof(struct btp_gap_pair_cmd),
1744 .func = pair,
1745 },
1746 {
1747 .opcode = BTP_GAP_UNPAIR,
1748 .expect_len = sizeof(struct btp_gap_unpair_cmd),
1749 .func = unpair,
1750 },
1751 {
1752 .opcode = BTP_GAP_PASSKEY_ENTRY,
1753 .expect_len = sizeof(struct btp_gap_passkey_entry_cmd),
1754 .func = passkey_entry,
1755 },
1756 {
1757 .opcode = BTP_GAP_PASSKEY_CONFIRM,
1758 .expect_len = sizeof(struct btp_gap_passkey_confirm_cmd),
1759 .func = passkey_confirm,
1760 },
1761 {
1762 .opcode = BTP_GAP_CONN_PARAM_UPDATE,
1763 .expect_len = sizeof(struct btp_gap_conn_param_update_cmd),
1764 .func = conn_param_update,
1765 },
1766 {
1767 .opcode = BTP_GAP_SET_MITM,
1768 .expect_len = sizeof(struct btp_gap_set_mitm),
1769 .func = set_mitm,
1770 },
1771 {
1772 .opcode = BTP_GAP_OOB_LEGACY_SET_DATA,
1773 .expect_len = sizeof(struct btp_gap_oob_legacy_set_data_cmd),
1774 .func = set_oob_legacy_data,
1775 },
1776 #if !defined(CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY)
1777 {
1778 .opcode = BTP_GAP_OOB_SC_GET_LOCAL_DATA,
1779 .expect_len = 0,
1780 .func = get_oob_sc_local_data,
1781 },
1782 {
1783 .opcode = BTP_GAP_OOB_SC_SET_REMOTE_DATA,
1784 .expect_len = sizeof(struct btp_gap_oob_sc_set_remote_data_cmd),
1785 .func = set_oob_sc_remote_data,
1786 },
1787 #endif /* !defined(CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY) */
1788 {
1789 .opcode = BTP_GAP_SET_FILTER_LIST,
1790 .expect_len = BTP_HANDLER_LENGTH_VARIABLE,
1791 .func = set_filter_list,
1792 },
1793 #if defined(CONFIG_BT_EXT_ADV)
1794 {
1795 .opcode = BTP_GAP_SET_EXTENDED_ADVERTISING,
1796 .expect_len = sizeof(struct btp_gap_set_extended_advertising_cmd),
1797 .func = set_extended_advertising,
1798 },
1799 #if defined(CONFIG_BT_PER_ADV)
1800 {
1801 .opcode = BTP_GAP_PADV_CONFIGURE,
1802 .expect_len = sizeof(struct btp_gap_padv_configure_cmd),
1803 .func = padv_configure,
1804 },
1805 {
1806 .opcode = BTP_GAP_PADV_START,
1807 .expect_len = sizeof(struct btp_gap_padv_start_cmd),
1808 .func = padv_start,
1809 },
1810 {
1811 .opcode = BTP_GAP_PADV_STOP,
1812 .expect_len = sizeof(struct btp_gap_padv_stop_cmd),
1813 .func = padv_stop,
1814 },
1815 {
1816 .opcode = BTP_GAP_PADV_SET_DATA,
1817 .expect_len = BTP_HANDLER_LENGTH_VARIABLE,
1818 .func = padv_set_data,
1819 },
1820 {
1821 .opcode = BTP_GAP_PADV_CREATE_SYNC,
1822 .expect_len = sizeof(struct btp_gap_padv_create_sync_cmd),
1823 .func = padv_create_sync,
1824 },
1825 {
1826 .opcode = BTP_GAP_PADV_SYNC_TRANSFER_SET_INFO,
1827 .expect_len = sizeof(struct btp_gap_padv_sync_transfer_set_info_cmd),
1828 .func = padv_sync_transfer_set_info,
1829 },
1830 {
1831 .opcode = BTP_GAP_PADV_SYNC_TRANSFER_START,
1832 .expect_len = sizeof(struct btp_gap_padv_sync_transfer_start_cmd),
1833 .func = padv_sync_transfer_start,
1834 },
1835 {
1836 .opcode = BTP_GAP_PADV_SYNC_TRANSFER_RECV,
1837 .expect_len = sizeof(struct btp_gap_padv_sync_transfer_recv_cmd),
1838 .func = padv_sync_transfer_recv,
1839 },
1840 #endif /* defined(CONFIG_BT_PER_ADV) */
1841 #endif /* defined(CONFIG_BT_EXT_ADV) */
1842 };
1843
1844 uint8_t tester_init_gap(void)
1845 {
1846 int err;
1847
1848 (void)memset(&cb, 0, sizeof(cb));
1849 bt_conn_auth_cb_register(NULL);
1850 cb.pairing_accept = auth_pairing_accept;
1851 if (bt_conn_auth_cb_register(&cb)) {
1852 return BTP_STATUS_FAILED;
1853 }
1854
1855 err = bt_enable(NULL);
1856 if (err < 0) {
1857 LOG_ERR("Unable to enable Bluetooth: %d", err);
1858 return BTP_STATUS_FAILED;
1859 }
1860
1861 atomic_clear(¤t_settings);
1862 atomic_set_bit(¤t_settings, BTP_GAP_SETTINGS_POWERED);
1863 atomic_set_bit(¤t_settings, BTP_GAP_SETTINGS_CONNECTABLE);
1864 atomic_set_bit(¤t_settings, BTP_GAP_SETTINGS_BONDABLE);
1865 atomic_set_bit(¤t_settings, BTP_GAP_SETTINGS_LE);
1866 #if defined(CONFIG_BT_PRIVACY)
1867 atomic_set_bit(¤t_settings, BTP_GAP_SETTINGS_PRIVACY);
1868 #endif /* CONFIG_BT_PRIVACY */
1869
1870 bt_conn_cb_register(&conn_callbacks);
1871 bt_conn_auth_info_cb_register(&auth_info_cb);
1872
1873 #if defined(CONFIG_BT_PER_ADV)
1874 bt_le_per_adv_sync_cb_register(&pa_sync_cb);
1875 #endif /* defined(CONFIG_BT_PER_ADV) */
1876
1877 tester_register_command_handlers(BTP_SERVICE_ID_GAP, handlers,
1878 ARRAY_SIZE(handlers));
1879
1880 return BTP_STATUS_SUCCESS;
1881 }
1882
1883 uint8_t tester_unregister_gap(void)
1884 {
1885 return BTP_STATUS_SUCCESS;
1886 }
1887