Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions device/src/bt_conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,14 @@ struct bt_conn *auth_conn;
#define BLE_KEY_LEN 16
#define BLE_ADDR_LEN 6

#define BT_REASON_TEMPORARY BT_HCI_ERR_REMOTE_USER_TERM_CONN
#define BT_REASON_PERMANENT BT_HCI_ERR_REMOTE_USER_TERM_CONN
// BT_HCI_ERR_REMOTE_LOW_RESOURCES - it is *their* resources that are low, so we probably shouldn't use this
// BT_HCI_ERR_LOCALHOST_TERM_CONN - this is our decision, bond should remain valid
// BT_HCI_ERR_REMOTE_USER_TERM_CONN - this is user's decision, bond should remain valid
// BT_HCI_ERR_AUTH_FAIL - permanent failure, bond should be removed

#define BT_REASON_HID_GIVE_US_BREAK BT_HCI_ERR_LOCALHOST_TERM_CONN
#define BT_REASON_NOT_SELECTED BT_HCI_ERR_LOCALHOST_TERM_CONN
#define BT_REASON_PERMANENT BT_HCI_ERR_AUTH_FAIL
#define BT_REASON_UNSPECIFIED BT_HCI_ERR_UNSPECIFIED

static void disconnectAllHids();
Expand Down Expand Up @@ -270,12 +276,12 @@ static void youAreNotWanted(struct bt_conn *conn) {
static bt_addr_le_t lastAddr = {0};
uint32_t currentTime = k_uptime_get_32();

if (currentTime - lastAttemptTime < 2000 && BtAddrEq(bt_conn_get_dst(conn), &lastAddr)) {
if (currentTime - lastAttemptTime < 1000 && BtAddrEq(bt_conn_get_dst(conn), &lastAddr)) {
LOG_WRN("Refusing connenction %s (this is not a selected connection)(this is repeated attempt!)\n", GetPeerStringByConn(conn));
safeDisconnect(conn, BT_REASON_TEMPORARY);
safeDisconnect(conn, BT_REASON_HID_GIVE_US_BREAK);
} else {
LOG_WRN("Refusing connenction %s (this is not a selected connection (%d))\n", GetPeerStringByConn(conn), SelectedHostConnectionId);
safeDisconnect(conn, BT_REASON_TEMPORARY);
safeDisconnect(conn, BT_REASON_NOT_SELECTED);
}
LOG_INF(" Free peripheral slots: %d, Peripheral conn count: %d, bt pari mode: %d",
BtConn_UnusedPeripheralConnectionCount(),
Expand Down Expand Up @@ -665,7 +671,7 @@ static void connectAuthenticatedConnection(struct bt_conn *conn, connection_id_t
break;
case ConnectionType_Unknown:
default:
LOG_WRN("Authenticated connection is not known. Disconnecting %s", GetPeerStringByConn(conn));
LOG_ERR("Authenticated connection is not known. Disconnecting %s", GetPeerStringByConn(conn));
safeDisconnect(conn, BT_HCI_ERR_AUTH_FAIL);
BtManager_StartScanningAndAdvertisingAsync(true, "StartScanningAndAdvertisingAsync in authenticatedConnection - is unknown");
break;
Expand Down Expand Up @@ -852,7 +858,7 @@ static void pairing_complete(struct bt_conn *conn, bool bonded) {
}

static void bt_foreach_conn_cb(struct bt_conn *conn, void *user_data) {
safeDisconnect(conn, BT_REASON_TEMPORARY);
safeDisconnect(conn, BT_REASON_NOT_SELECTED);
// gpt says you should unref here. Don't believe it!
}

Expand All @@ -870,14 +876,14 @@ void BtConn_DisconnectOne(connection_id_t connectionId) {

if (!conn) { return; }

safeDisconnect(conn, BT_REASON_TEMPORARY);
safeDisconnect(conn, BT_REASON_NOT_SELECTED);
}

static void bt_foreach_conn_cb_disconnect_unidentified(struct bt_conn *conn, void *user_data) {
peer_t* peer = getPeerByConn(conn);
if (!peer) {
LOG_INF(" disconnecting unassigned connection %s\n", GetPeerStringByConn(conn));
safeDisconnect(conn, BT_REASON_TEMPORARY);
safeDisconnect(conn, BT_REASON_NOT_SELECTED);
}
}

Expand Down Expand Up @@ -979,7 +985,7 @@ ATTR_UNUSED static void disconnectOldestHost() {

if (oldestPeerId != PeerIdUnknown) {
LOG_INF("Disconnecting oldest host %d\n", oldestPeerId);
safeDisconnect(Peers[oldestPeerId].conn, BT_REASON_TEMPORARY);
safeDisconnect(Peers[oldestPeerId].conn, BT_REASON_NOT_SELECTED);
}
}

Expand Down
8 changes: 8 additions & 0 deletions doc-dev/other/testing/bluetooth.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Things to test:

- Pair ble hid. Unpair. Pair again.
- When two ble hid devices are paired, test that we can `switchHost` between them.
- When a device is paired, check that we can disconnect it and connect it again - i.e., that the bonding information didn't get corrupted. (Optionally, throw in uhk restart, bluetooth restart, test this with multiple devices.)
- Test that we can switch between two dongles and that their leds light up accordingly.
- Issue `switchHost device` against a device that is not present. `switchHost` to current usb. See that uhk now advertises hid (pairing icon) and that a ble hid host can connect.
- Test that we can add a paired device into the host connections. Try pair and add multiple devices within one Agent session.