1 /* Copyright (c) 2023 Nordic Semiconductor ASA
2  * SPDX-License-Identifier: Apache-2.0
3  */
4 
5 #include "common.h"
6 
7 extern const struct test_sample_data *sample_data;
8 
9 extern int data_set;
10 
create_adv(struct bt_le_ext_adv ** adv)11 static void create_adv(struct bt_le_ext_adv **adv)
12 {
13 	int err;
14 	struct bt_le_adv_param params;
15 
16 	memset(&params, 0, sizeof(struct bt_le_adv_param));
17 
18 	params.options |= BT_LE_ADV_OPT_CONN;
19 	params.options |= BT_LE_ADV_OPT_EXT_ADV;
20 
21 	params.id = BT_ID_DEFAULT;
22 	params.sid = 0;
23 	params.interval_min = BT_GAP_ADV_SLOW_INT_MIN;
24 	params.interval_max = BT_GAP_ADV_SLOW_INT_MAX;
25 
26 	err = bt_le_ext_adv_create(&params, NULL, adv);
27 	if (err) {
28 		FAIL("Failed to create advertiser (%d)\n", err);
29 	}
30 }
31 
start_adv(struct bt_le_ext_adv * adv)32 static void start_adv(struct bt_le_ext_adv *adv)
33 {
34 	int err;
35 	int32_t timeout = 0;
36 	uint8_t num_events = 0;
37 
38 	struct bt_le_ext_adv_start_param start_params;
39 
40 	start_params.timeout = timeout;
41 	start_params.num_events = num_events;
42 
43 	err = bt_le_ext_adv_start(adv, &start_params);
44 	if (err) {
45 		FAIL("Failed to start advertiser (%d)\n", err);
46 	}
47 
48 	LOG_DBG("Advertiser started");
49 }
50 
set_ad_data(struct bt_le_ext_adv * adv)51 static void set_ad_data(struct bt_le_ext_adv *adv)
52 {
53 	int err;
54 
55 	uint8_t ead[sample_data->size_ead];
56 	struct bt_data ead_struct;
57 	size_t size_ad_data = sample_data->size_ad_data;
58 	size_t size_ead = BT_EAD_ENCRYPTED_PAYLOAD_SIZE(size_ad_data);
59 
60 	if (size_ead != sample_data->size_ead) {
61 		LOG_ERR("Size of ead: %zu\n", size_ead);
62 		LOG_ERR("Size of sample_ead: %zu", sample_data->size_ead);
63 		FAIL("Computed size of encrypted data does not match the size of the encrypted "
64 		     "data from the sample. (data set %d)\n",
65 		     data_set);
66 	}
67 
68 	err = bt_test_ead_encrypt(sample_data->session_key, sample_data->iv,
69 				  sample_data->randomizer_little_endian, sample_data->ad_data,
70 				  size_ad_data, ead);
71 	if (err != 0) {
72 		FAIL("Error during encryption.\n");
73 	} else if (memcmp(ead, sample_data->ead, sample_data->size_ead) != 0) {
74 		LOG_HEXDUMP_ERR(ead, size_ead, "Encrypted data from bt_ead_encrypt:");
75 		LOG_HEXDUMP_ERR(sample_data->ead, sample_data->size_ead,
76 				"Encrypted data from sample:");
77 		FAIL("Encrypted AD data does not match the ones provided in the sample. (data set "
78 		     "%d)\n",
79 		     data_set);
80 	}
81 
82 	LOG_HEXDUMP_DBG(ead, size_ead, "Encrypted data:");
83 
84 	ead_struct.data_len = size_ead;
85 	ead_struct.type = BT_DATA_ENCRYPTED_AD_DATA;
86 	ead_struct.data = ead;
87 
88 	err = bt_le_ext_adv_set_data(adv, &ead_struct, 1, NULL, 0);
89 	if (err) {
90 		FAIL("Failed to set advertising data (%d)\n", err);
91 	}
92 
93 	PASS("Peripheral test passed. (data set %d)\n", data_set);
94 }
95 
test_peripheral(void)96 void test_peripheral(void)
97 {
98 	int err;
99 	struct bt_le_ext_adv *adv = NULL;
100 
101 	LOG_DBG("Peripheral device. (data set %d)", data_set);
102 
103 	err = bt_enable(NULL);
104 	if (err) {
105 		FAIL("Bluetooth init failed (err %d)\n", err);
106 	}
107 
108 	LOG_DBG("Bluetooth initialized");
109 
110 	create_adv(&adv);
111 	start_adv(adv);
112 
113 	set_ad_data(adv);
114 }
115