Skip to content

Commit

Permalink
Fix address matching.
Browse files Browse the repository at this point in the history
  • Loading branch information
kareltucek committed Dec 22, 2024
1 parent b14f163 commit 6b17356
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 12 deletions.
4 changes: 2 additions & 2 deletions device/src/bt_conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ peer_t Peers[PeerCount] = {

peer_t *getPeerByAddr(const bt_addr_le_t *addr) {
for (uint8_t i = 0; i < PeerCount; i++) {
if (bt_addr_le_eq(addr, &Peers[i].addr)) {
if (BtAddrEq(addr, &Peers[i].addr)) {
return &Peers[i];
}
}
Expand Down Expand Up @@ -214,7 +214,7 @@ static void connected(struct bt_conn *conn, uint8_t err) {
connectionType != ConnectionType_NusLeft &&
BtConn_UnusedPeripheralConnectionCount() <= 1 &&
SelectedHostConnectionId != ConnectionId_Invalid &&
bt_addr_le_cmp(bt_conn_get_dst(conn), &HostConnection(SelectedHostConnectionId)->bleAddress) != 0
!BtAddrEq(bt_conn_get_dst(conn), &HostConnection(SelectedHostConnectionId)->bleAddress)
) {
printk("Refusing connenction %d (this is not a selected connection)\n", connectionId);
err = bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
Expand Down
4 changes: 4 additions & 0 deletions device/src/bt_conn.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,8 @@
void BtConn_ListCurrentConnections();
void BtConn_ListAllBonds();

static inline bool BtAddrEq(const bt_addr_le_t *a, const bt_addr_le_t *b) {
return 0 == memcmp(a->a.val, b->a.val, sizeof(a->a.val));
}

#endif // __BT_CONN_H__
8 changes: 4 additions & 4 deletions device/src/bt_pair.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ static void deleteBond(const struct bt_bond_info *info) {

struct bt_conn* conn;

if (bt_addr_le_cmp(&Peers[PeerIdLeft].addr, &info->addr) == 0) {
if (BtAddrEq(&Peers[PeerIdLeft].addr, &info->addr)) {
settings_delete("uhk/addr/left");
}
if (bt_addr_le_cmp(&Peers[PeerIdRight].addr, &info->addr) == 0) {
if (BtAddrEq(&Peers[PeerIdRight].addr, &info->addr)) {
settings_delete("uhk/addr/right");
}

Expand Down Expand Up @@ -141,7 +141,7 @@ static void bt_foreach_bond_cb_delete(const struct bt_bond_info *info, void *use
{
struct delete_args_t* args = (struct delete_args_t*)user_data;

if (!args->all && bt_addr_le_cmp(args->addr, &info->addr) != 0) {
if (!args->all && !BtAddrEq(args->addr, &info->addr) ) {
char addr[32];
bt_addr_le_to_str(&info->addr, addr, sizeof(addr));
printk("Not deleting bond for %s\n", addr);
Expand Down Expand Up @@ -192,7 +192,7 @@ void checkBondedDevice(const struct bt_bond_info *info, void *user_data) {
bt_addr_le_to_str(&info->addr, addr, sizeof(addr));
char ref[32];
bt_addr_le_to_str(args->addr, ref, sizeof(ref));
if (bt_addr_le_cmp(&info->addr, args->addr) == 0) {
if (BtAddrEq(&info->addr, args->addr)) {
*args->bonded = true;
printk("Device %s is bonded, ref %s\n", addr, ref);
}
Expand Down
4 changes: 2 additions & 2 deletions device/src/connections.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ connection_id_t Connections_GetConnectionIdByBtAddr(const bt_addr_le_t *addr) {
uint8_t newBtConnection = ConnectionId_Invalid;

for (uint8_t peerId = 0; peerId < PeerCount; peerId++) {
if (bt_addr_le_cmp(addr, &Peers[peerId].addr) == 0) {
if (BtAddrEq(addr, &Peers[peerId].addr)) {
return Peers[peerId].connectionId;
}
}
Expand All @@ -197,7 +197,7 @@ connection_id_t Connections_GetConnectionIdByBtAddr(const bt_addr_le_t *addr) {
break;
case HostConnectionType_Dongle:
case HostConnectionType_BtHid:
if (bt_addr_le_cmp(addr, &hostConnection->bleAddress) == 0) {
if (BtAddrEq(addr, &hostConnection->bleAddress)) {
return connectionId;
}
break;
Expand Down
2 changes: 1 addition & 1 deletion right/src/config_parser/parse_host_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ static parser_error_t parseHostConnection(config_buffer_t* buffer, host_connecti
}

if (hostConnection->type == HostConnectionType_BtHid || hostConnection->type == HostConnectionType_Dongle) {
hostConnection->bleAddress.type = 1;
for (uint8_t i = 0; i < BLE_ADDRESS_LENGTH; i++) {
hostConnection->bleAddress.a.val[i] = ReadUInt8(buffer);
}
hostConnection->bleAddress.type = hostConnection->bleAddress.a.val[0] & 0x01;
}

if (hostConnection->type != HostConnectionType_Empty) {
Expand Down
4 changes: 2 additions & 2 deletions right/src/host_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ bool HostConnections_IsKnownBleAddress(const bt_addr_le_t *address) {
break;
case HostConnectionType_Dongle:
case HostConnectionType_BtHid:
if (bt_addr_le_cmp(address, &HostConnections[i].bleAddress) == 0) {
if (BtAddrEq(address, &HostConnections[i].bleAddress)) {
return true;
}
break;
Expand All @@ -35,7 +35,7 @@ bool HostConnections_IsKnownBleAddress(const bt_addr_le_t *address) {
// Don't count new ble connections
// Do check devices that are paired via settings - left, right, dongle
for (int peerIdx = 0; peerIdx < PeerIdFirstHost; peerIdx++) {
if (bt_addr_le_cmp(address, &Peers[peerIdx].addr) == 0) {
if (BtAddrEq(address, &Peers[peerIdx].addr)) {
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion right/src/usb_protocol_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ void UsbProtocolHandler(const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidI
#ifdef __ZEPHYR__
bt_addr_le_t GetBufferBleAddress(const uint8_t *GenericHidOutBuffer, uint32_t offset) {
bt_addr_le_t addr;
addr.type = 1;
for (uint8_t i = 0; i < BLE_ADDR_LEN; i++) {
addr.a.val[i] = GenericHidOutBuffer[offset + i];
}
addr.type = addr.a.val[0] & 0x01;
return addr;
}

Expand Down

0 comments on commit 6b17356

Please sign in to comment.