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