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
data_parse_cb(struct bt_data * data,void * user_data)11 static bool data_parse_cb(struct bt_data *data, void *user_data)
12 {
13 if (data->type == BT_DATA_ENCRYPTED_AD_DATA) {
14 int err;
15 uint8_t decrypted_payload[sample_data->size_ad_data];
16 struct net_buf_simple decrypted_buf;
17 size_t decrypted_data_size = BT_EAD_DECRYPTED_PAYLOAD_SIZE(data->data_len);
18
19 if (decrypted_data_size != sample_data->size_ad_data) {
20 LOG_ERR("Size of decrypted data: %d", decrypted_data_size);
21 LOG_ERR("Size of sample data: %d", sample_data->size_ad_data);
22 FAIL("Computed size of data does not match the size of the data from the "
23 "sample. (data set %d)\n",
24 data_set);
25 }
26
27 if (memcmp(sample_data->randomizer_little_endian, data->data,
28 BT_EAD_RANDOMIZER_SIZE) != 0) {
29 LOG_ERR("Received Randomizer: %s",
30 bt_hex(data->data, BT_EAD_RANDOMIZER_SIZE));
31 LOG_ERR("Expected Randomizer from sample: %s",
32 bt_hex(sample_data->randomizer_little_endian,
33 BT_EAD_RANDOMIZER_SIZE));
34 FAIL("Received Randomizer does not match the expected one.\n");
35 }
36
37 net_buf_simple_init_with_data(&decrypted_buf, decrypted_payload,
38 decrypted_data_size);
39
40 err = bt_ead_decrypt(sample_data->session_key, sample_data->iv, data->data,
41 data->data_len, decrypted_buf.data);
42 if (err != 0) {
43 FAIL("Error during decryption.\n");
44 } else if (memcmp(decrypted_buf.data, sample_data->ad_data, decrypted_data_size)) {
45 LOG_HEXDUMP_ERR(decrypted_buf.data, decrypted_data_size,
46 "Decrypted data from bt_ead_decrypt:");
47 LOG_HEXDUMP_ERR(sample_data->ad_data, sample_data->size_ad_data,
48 "Expected data from sample:");
49 FAIL("Decrypted AD data does not match expected sample data. (data set "
50 "%d)\n",
51 data_set);
52 }
53
54 LOG_HEXDUMP_DBG(decrypted_buf.data, decrypted_data_size, "Raw decrypted data: ");
55
56 bt_data_parse(&decrypted_buf, &data_parse_cb, NULL);
57
58 PASS("Central test passed. (data set %d)\n", data_set);
59
60 return false;
61 }
62
63 LOG_DBG("Parsed data:");
64 LOG_DBG("len : %d", data->data_len);
65 LOG_DBG("type: 0x%02x", data->type);
66 LOG_HEXDUMP_DBG(data->data, data->data_len, "data:");
67
68 return true;
69 }
70
device_found(const bt_addr_le_t * addr,int8_t rssi,uint8_t type,struct net_buf_simple * ad)71 static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type,
72 struct net_buf_simple *ad)
73 {
74 char addr_str[BT_ADDR_LE_STR_LEN];
75
76 bt_addr_le_to_str(addr, addr_str, sizeof(addr_str));
77
78 LOG_DBG("Device found: %s (RSSI %d)", addr_str, rssi);
79
80 bt_data_parse(ad, &data_parse_cb, NULL);
81 }
82
start_scan(void)83 static void start_scan(void)
84 {
85 int err;
86
87 err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found);
88 if (err) {
89 FAIL("Scanning failed to start (err %d)\n", err);
90 }
91
92 LOG_DBG("Scanning successfully started");
93 }
94
test_central(void)95 void test_central(void)
96 {
97 int err;
98
99 LOG_DBG("Central device. (data set %d)", data_set);
100
101 err = bt_enable(NULL);
102 if (err) {
103 FAIL("Bluetooth init failed (err %d)\n", err);
104 }
105
106 LOG_DBG("Bluetooth initialized");
107
108 start_scan();
109 }
110