Skip to content

Commit 871db2d

Browse files
MariuszSkamracarlescufi
authored andcommitted
Bluetooth: Move bt_data_parse to dedicated source file
This moves bt_data_parse function outside of hci_core.c. Having it in separate file makes unit testing easier as the function do not use kernel objects, thus can be used in unit tests without a need for adding any mocks. Signed-off-by: Mariusz Skamra <[email protected]>
1 parent 61559bf commit 871db2d

File tree

3 files changed

+46
-32
lines changed

3 files changed

+46
-32
lines changed

subsys/bluetooth/host/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ if(CONFIG_BT_HCI_HOST)
3434
uuid.c
3535
addr.c
3636
buf.c
37+
data.c
3738
hci_core.c
3839
hci_common.c
3940
id.c

subsys/bluetooth/host/data.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2017-2021 Nordic Semiconductor ASA
3+
* Copyright (c) 2015-2016 Intel Corporation
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
#include <zephyr/kernel.h>
9+
#include <zephyr/bluetooth/bluetooth.h>
10+
11+
#define LOG_LEVEL CONFIG_BT_HCI_CORE_LOG_LEVEL
12+
#include <zephyr/logging/log.h>
13+
LOG_MODULE_REGISTER(bt_data);
14+
15+
void bt_data_parse(struct net_buf_simple *ad,
16+
bool (*func)(struct bt_data *data, void *user_data),
17+
void *user_data)
18+
{
19+
while (ad->len > 1) {
20+
struct bt_data data;
21+
uint8_t len;
22+
23+
len = net_buf_simple_pull_u8(ad);
24+
if (len == 0U) {
25+
/* Early termination */
26+
return;
27+
}
28+
29+
if (len > ad->len) {
30+
LOG_WRN("malformed advertising data %u / %u",
31+
len, ad->len);
32+
return;
33+
}
34+
35+
data.type = net_buf_simple_pull_u8(ad);
36+
data.data_len = len - 1;
37+
data.data = ad->data;
38+
39+
if (!func(&data, user_data)) {
40+
return;
41+
}
42+
43+
net_buf_simple_pull(ad, len - 1);
44+
}
45+
}

subsys/bluetooth/host/hci_core.c

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4059,38 +4059,6 @@ int bt_le_set_rpa_timeout(uint16_t new_rpa_timeout)
40594059
}
40604060
#endif
40614061

4062-
void bt_data_parse(struct net_buf_simple *ad,
4063-
bool (*func)(struct bt_data *data, void *user_data),
4064-
void *user_data)
4065-
{
4066-
while (ad->len > 1) {
4067-
struct bt_data data;
4068-
uint8_t len;
4069-
4070-
len = net_buf_simple_pull_u8(ad);
4071-
if (len == 0U) {
4072-
/* Early termination */
4073-
return;
4074-
}
4075-
4076-
if (len > ad->len) {
4077-
LOG_WRN("malformed advertising data %u / %u",
4078-
len, ad->len);
4079-
return;
4080-
}
4081-
4082-
data.type = net_buf_simple_pull_u8(ad);
4083-
data.data_len = len - 1;
4084-
data.data = ad->data;
4085-
4086-
if (!func(&data, user_data)) {
4087-
return;
4088-
}
4089-
4090-
net_buf_simple_pull(ad, len - 1);
4091-
}
4092-
}
4093-
40944062
int bt_configure_data_path(uint8_t dir, uint8_t id, uint8_t vs_config_len,
40954063
const uint8_t *vs_config)
40964064
{

0 commit comments

Comments
 (0)