Skip to content

Commit 355b6fb

Browse files
committed
bt: scan: filter discovered devices by bonding status
Add support for filtering scanned BLE devices based on their bonding status. When POUCH_GATEWAY_GATT_SCAN_FILTER_BONDED is enabled, the gateway only connects to devices that are already bonded or when bonding is explicitly enabled via the bonding API. This enables authenticated connections where devices must be paired before they can communicate with the gateway. Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
1 parent d774a0c commit 355b6fb

2 files changed

Lines changed: 78 additions & 20 deletions

File tree

Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ module = POUCH_GATEWAY
4343
module-str = Pouch Gateway Library
4444
source "subsys/logging/Kconfig.template.log_config"
4545

46+
config POUCH_GATEWAY_GATT_SCAN_FILTER_BONDED
47+
bool "Scan filter by bonded status"
48+
help
49+
Filter scanned devices by bonded status. Use it for authenticated
50+
connections, so that discovered devices will be ignored when not bonded
51+
already. Bonding can be triggered explicitly by calling
52+
pouch_gateway_bonding_enable() API.
53+
4654
module = POUCH_GATEWAY_GATT
4755
module-str = Pouch Gateway GATT Library
4856
source "subsys/logging/Kconfig.template.log_config"

lib/bt/scan.c

Lines changed: 70 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <zephyr/logging/log.h>
1515
LOG_MODULE_REGISTER(scan, CONFIG_POUCH_GATEWAY_GATT_LOG_LEVEL);
1616

17+
#include <pouch_gateway/bt/bond.h>
1718
#include <pouch_gateway/bt/scan.h>
1819

1920
static inline bool version_is_compatible(const struct pouch_gatt_adv_data *adv_data)
@@ -31,7 +32,9 @@ static inline bool sync_requested(const struct pouch_gatt_adv_data *adv_data)
3132

3233
struct tf_data
3334
{
35+
const bt_addr_le_t *addr;
3436
bool is_tf;
37+
bool is_bonded;
3538
struct pouch_gatt_adv_data adv_data;
3639
};
3740

@@ -86,14 +89,27 @@ static bool data_cb(struct bt_data *data, void *user_data)
8689
}
8790
}
8891

92+
static void bond_filter(const struct bt_bond_info *info, void *user_data)
93+
{
94+
struct tf_data *tf = user_data;
95+
96+
if (memcmp(&info->addr, tf->addr, sizeof(info->addr)) == 0)
97+
{
98+
tf->is_bonded = true;
99+
}
100+
}
101+
89102
static void device_found(const bt_addr_le_t *addr,
90103
int8_t rssi,
91104
uint8_t type,
92105
struct net_buf_simple *ad)
93106
{
94107
char addr_str[BT_ADDR_LE_STR_LEN];
95108
struct tf_data tf = {
109+
.addr = addr,
96110
.is_tf = false,
111+
/* When filtering bonded devices is disabled, treat all devices as bonded */
112+
.is_bonded = IS_ENABLED(CONFIG_POUCH_GATEWAY_GATT_SCAN_FILTER_BONDED) ? false : true,
97113
};
98114
int err;
99115

@@ -106,30 +122,64 @@ static void device_found(const bt_addr_le_t *addr,
106122

107123
bt_data_parse(ad, data_cb, &tf);
108124

109-
if (tf.is_tf)
125+
if (!tf.is_tf)
126+
{
127+
return;
128+
}
129+
130+
bt_addr_le_to_str(addr, addr_str, sizeof(addr_str));
131+
132+
if (IS_ENABLED(CONFIG_POUCH_GATEWAY_GATT_SCAN_FILTER_BONDED))
133+
{
134+
bt_foreach_bond(BT_ID_DEFAULT, bond_filter, &tf);
135+
136+
LOG_DBG("Pouch device found: %s, (RSSI %d) (bonded %d)",
137+
addr_str,
138+
rssi,
139+
(int) tf.is_bonded);
140+
}
141+
else
110142
{
111-
bt_addr_le_to_str(addr, addr_str, sizeof(addr_str));
112143
LOG_DBG("Pouch device found: %s, (RSSI %d)", addr_str, rssi);
113-
LOG_DBG("version=0x%0x flags=0%0x", tf.adv_data.version, tf.adv_data.flags);
144+
}
114145

115-
if (version_is_compatible(&tf.adv_data) && sync_requested(&tf.adv_data))
116-
{
117-
err = bt_le_scan_stop();
118-
if (err)
119-
{
120-
LOG_ERR("Failed to stop scanning");
121-
return;
122-
}
146+
LOG_DBG("version=0x%0x flags=0%0x", tf.adv_data.version, tf.adv_data.flags);
123147

124-
struct bt_conn *conn = NULL;
125-
err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, BT_LE_CONN_PARAM_DEFAULT, &conn);
126-
if (err)
127-
{
128-
LOG_ERR("Create auto conn failed (%d)", err);
129-
pouch_gateway_scan_start();
130-
return;
131-
}
132-
}
148+
if (!version_is_compatible(&tf.adv_data))
149+
{
150+
return;
151+
}
152+
153+
if (!tf.is_bonded && !pouch_gateway_bonding_is_enabled())
154+
{
155+
return;
156+
}
157+
158+
if (tf.is_bonded && !sync_requested(&tf.adv_data))
159+
{
160+
return;
161+
}
162+
163+
err = bt_le_scan_stop();
164+
if (err)
165+
{
166+
LOG_ERR("Failed to stop scanning");
167+
return;
168+
}
169+
170+
struct bt_conn *conn = NULL;
171+
err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, BT_LE_CONN_PARAM_DEFAULT, &conn);
172+
if (err)
173+
{
174+
LOG_ERR("Create auto conn failed (%d)", err);
175+
pouch_gateway_scan_start();
176+
return;
177+
}
178+
179+
/* Disable bonding after first connect attempt */
180+
if (!tf.is_bonded)
181+
{
182+
pouch_gateway_bonding_disable();
133183
}
134184
}
135185

0 commit comments

Comments
 (0)