Skip to content

Commit

Permalink
BLE: clean up registered connection callback
Browse files Browse the repository at this point in the history
KRKNWK-19512
Just clean up, the sidewalk handlers filters events by the connection,
but still in case of DFU, the old connection handler can be called.

Signed-off-by: Robert Gałat <[email protected]>
  • Loading branch information
RobertGalatNordic committed Oct 14, 2024
1 parent 4d57fc9 commit ba1964f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
21 changes: 19 additions & 2 deletions subsys/sal/sid_pal/src/sid_ble_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ static void ble_mtu_cb(struct bt_conn *conn, uint16_t tx_mtu, uint16_t rx_mtu);
static sid_ble_conn_params_t conn_params;
static sid_ble_conn_params_t *p_conn_params_out;

static bool bt_conn_registered = false;
static struct bt_conn_cb conn_callbacks = {
.connected = ble_connect_cb,
.disconnected = ble_disconnect_cb,
Expand Down Expand Up @@ -103,7 +104,6 @@ const sid_ble_conn_params_t *sid_ble_conn_params_get(void)
void sid_ble_conn_init(void)
{
p_conn_params_out = &conn_params;
static bool bt_conn_registered;

if (!bt_conn_registered) {
int e = bt_conn_cb_register(&conn_callbacks);
Expand All @@ -116,8 +116,14 @@ void sid_ble_conn_init(void)
return;
}
}
bt_gatt_cb_register(&gatt_callbacks);
bt_conn_registered = true;
} else {
LOG_WRN("The connection has already been registered, but init was called again");
}
static bool gatt_cb_registered = false;
if (!gatt_cb_registered) {
gatt_cb_registered = true;
bt_gatt_cb_register(&gatt_callbacks);
}
}

Expand All @@ -138,4 +144,15 @@ int sid_ble_conn_disconnect(void)
void sid_ble_conn_deinit(void)
{
p_conn_params_out = NULL;
if (bt_conn_registered) {
int unregister_result = bt_conn_cb_unregister(&conn_callbacks);
switch (unregister_result) {
case 0:
bt_conn_registered = false;
break;
default:
LOG_ERR("bt_conn_cb_unregister resulted in errno %d = %s",
unregister_result, strerror(unregister_result));
}
}
}
5 changes: 5 additions & 0 deletions tests/unit_tests/sid_ble_connection/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
DEFINE_FFF_GLOBALS;

FAKE_VALUE_FUNC(int, bt_conn_cb_register, struct bt_conn_cb *);
FAKE_VALUE_FUNC(int, bt_conn_cb_unregister, struct bt_conn_cb *);
FAKE_VOID_FUNC(bt_gatt_cb_register, struct bt_gatt_cb *);
FAKE_VALUE_FUNC(struct bt_conn *, bt_conn_ref, struct bt_conn *);
FAKE_VOID_FUNC(bt_conn_unref, struct bt_conn *);
Expand All @@ -27,6 +28,7 @@ FAKE_VALUE_FUNC(int, bt_conn_disconnect, struct bt_conn *, uint8_t);

#define FFF_FAKES_LIST(FAKE) \
FAKE(bt_conn_cb_register) \
FAKE(bt_conn_cb_unregister) \
FAKE(bt_gatt_cb_register) \
FAKE(bt_conn_ref) \
FAKE(bt_conn_unref) \
Expand Down Expand Up @@ -74,6 +76,7 @@ void test_sid_ble_conn_init(void)
{
sid_ble_conn_init();
TEST_ASSERT_EQUAL(1, bt_conn_cb_register_fake.call_count);
TEST_ASSERT_EQUAL(0, bt_conn_cb_unregister_fake.call_count);
TEST_ASSERT_EQUAL(1, bt_gatt_cb_register_fake.call_count);

sid_bt_conn_cb = bt_conn_cb_register_fake.arg0_history[0];
Expand All @@ -83,6 +86,8 @@ void test_sid_ble_conn_init(void)
sid_bt_gatt_cb = bt_gatt_cb_register_fake.arg0_history[0];
TEST_ASSERT_NOT_NULL(sid_bt_gatt_cb);
TEST_ASSERT_NOT_NULL(sid_bt_gatt_cb->att_mtu_updated);
sid_ble_conn_deinit();
TEST_ASSERT_EQUAL(1, bt_conn_cb_unregister_fake.call_count);
}

void test_sid_ble_conn_params_get(void)
Expand Down

0 comments on commit ba1964f

Please sign in to comment.