From 257e3fe35ab586a5404392976a50b039ddb627f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Tu=C4=8Dek?= Date: Thu, 28 Nov 2024 16:07:33 +0100 Subject: [PATCH 1/2] Fix NewPairings flag and add GetNewPairings paging. --- device/src/bt_pair.c | 3 ++ .../usb_command_get_device_property.c | 4 +- .../usb_command_get_new_pairings.c | 39 ++++++++++++++----- .../usb_command_get_new_pairings.h | 7 +++- 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/device/src/bt_pair.c b/device/src/bt_pair.c index dbb02ba71..b2ed3143b 100644 --- a/device/src/bt_pair.c +++ b/device/src/bt_pair.c @@ -13,6 +13,7 @@ #include "bt_advertise.h" #include "legacy/host_connection.h" #include "settings.h" +#include "legacy/usb_commands/usb_command_get_new_pairings.h" bool BtPair_LastPairingSucceeded = true; bool BtPair_OobPairingInProgress = false; @@ -172,6 +173,7 @@ void BtPair_Unpair(const bt_addr_le_t addr) { // Update settings Settings_Reload(); + UsbCommand_UpdateNewPairingsFlag(); } struct check_bonded_device_args_t { @@ -215,4 +217,5 @@ void deleteBondIfUnknown(const struct bt_bond_info *info, void *user_data) { void BtPair_ClearUnknownBonds() { bt_foreach_bond(BT_ID_DEFAULT, deleteBondIfUnknown, NULL); + UsbCommand_UpdateNewPairingsFlag(); } diff --git a/right/src/usb_commands/usb_command_get_device_property.c b/right/src/usb_commands/usb_command_get_device_property.c index efd76ae3e..1b42b68cc 100644 --- a/right/src/usb_commands/usb_command_get_device_property.c +++ b/right/src/usb_commands/usb_command_get_device_property.c @@ -15,6 +15,8 @@ #include "slave_drivers/kboot_driver.h" #endif +#include +#include #include "slave_protocol.h" #include "timer.h" #include "usb_commands/usb_command_get_new_pairings.h" @@ -105,7 +107,7 @@ void UsbCommand_GetDeviceProperty(void) } break; case DevicePropertyId_NewPairings: #ifdef __ZEPHYR__ - UsbCommand_GetNewPairings(); + UsbCommand_GetNewPairings(GetUsbRxBufferUint8(2)); #endif break; default: diff --git a/right/src/usb_commands/usb_command_get_new_pairings.c b/right/src/usb_commands/usb_command_get_new_pairings.c index d86d030dc..bb286bcc6 100644 --- a/right/src/usb_commands/usb_command_get_new_pairings.c +++ b/right/src/usb_commands/usb_command_get_new_pairings.c @@ -7,12 +7,17 @@ #include #include "host_connection.h" -static uint8_t idx; +#define ADDRESS_COUNT_PER_PAGE 10 + +static uint8_t pageIdxOffset; +static uint8_t writeOffset; static uint8_t count; +static bool dryRun = false; + static void bt_foreach_bond_cb(const struct bt_bond_info *info, void *user_data) { - if (idx + BLE_ADDR_LEN+1 >= USB_GENERIC_HID_IN_BUFFER_LENGTH) { + if (writeOffset + BLE_ADDR_LEN+1 >= USB_GENERIC_HID_IN_BUFFER_LENGTH) { return; } @@ -20,24 +25,38 @@ static void bt_foreach_bond_cb(const struct bt_bond_info *info, void *user_data) return; } + Bt_NewPairedDevice = true; + count++; - SetUsbTxBufferBleAddress(idx, &info->addr); - idx += BLE_ADDR_LEN; + if (!dryRun && count >= pageIdxOffset && count < pageIdxOffset+ADDRESS_COUNT_PER_PAGE) { + SetUsbTxBufferBleAddress(writeOffset, &info->addr); + writeOffset += BLE_ADDR_LEN; + } - // Name placeholder - SetUsbTxBufferUint8(idx++, 0); } -void UsbCommand_GetNewPairings(void) { +void UsbCommand_UpdateNewPairingsFlag() { + dryRun = true; + pageIdxOffset = 0; count = 0; - idx = 2; bt_foreach_bond(BT_ID_DEFAULT, bt_foreach_bond_cb, NULL); +} - SetUsbTxBufferUint8(1, count); +void UsbCommand_GetNewPairings(uint8_t page) { + dryRun = false; + pageIdxOffset = ADDRESS_COUNT_PER_PAGE*page; + count = 0; + writeOffset = 2; + + bt_foreach_bond(BT_ID_DEFAULT, bt_foreach_bond_cb, NULL); - Bt_NewPairedDevice = false; + if (count < pageIdxOffset) { + SetUsbTxBufferUint8(1, 0); + } else { + SetUsbTxBufferUint8(1, count-pageIdxOffset); + } } #endif diff --git a/right/src/usb_commands/usb_command_get_new_pairings.h b/right/src/usb_commands/usb_command_get_new_pairings.h index a62b0cb41..2f74b9dbe 100644 --- a/right/src/usb_commands/usb_command_get_new_pairings.h +++ b/right/src/usb_commands/usb_command_get_new_pairings.h @@ -3,11 +3,16 @@ #ifdef __ZEPHYR__ +// Includes: + + #include + // Typedefs: // Functions: - void UsbCommand_GetNewPairings(void); + void UsbCommand_UpdateNewPairingsFlag(); + void UsbCommand_GetNewPairings(uint8_t page); #endif From 56ba94d8af9326e502c730026428cdd3a6b05d07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Tu=C4=8Dek?= Date: Wed, 18 Dec 2024 16:36:24 +0100 Subject: [PATCH 2/2] Fix bond clearing. (Didn't work because of settings.) --- device/src/bt_pair.c | 4 ++++ device/src/main.c | 10 +++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/device/src/bt_pair.c b/device/src/bt_pair.c index b2ed3143b..464232763 100644 --- a/device/src/bt_pair.c +++ b/device/src/bt_pair.c @@ -210,12 +210,16 @@ bool BtPair_IsDeviceBonded(const bt_addr_le_t *addr) void deleteBondIfUnknown(const struct bt_bond_info *info, void *user_data) { if (!HostConnections_IsKnownBleAddress(&info->addr)) { + printk(" - Deleting an unknown bond!\n"); deleteBond(info); + } else { + printk(" - Keeping a known bond.\n"); } }; void BtPair_ClearUnknownBonds() { + printk("Clearing bonds\n"); bt_foreach_bond(BT_ID_DEFAULT, deleteBondIfUnknown, NULL); UsbCommand_UpdateNewPairingsFlag(); } diff --git a/device/src/main.c b/device/src/main.c index 08e31897e..7c4ef95a9 100644 --- a/device/src/main.c +++ b/device/src/main.c @@ -129,6 +129,11 @@ int main(void) { Ledmap_InitLedLayout(); } + bt_enable(NULL); + + // has to be after bt_enable, has to be before ApplyConfig + InitSettings(); + // read configurations { InitFlash(); @@ -155,11 +160,6 @@ int main(void) { USB_EnableHid(); // has to be after USB_SetSerialNumber - bt_enable(NULL); - - // has to be after bt_enable - InitSettings(); - // has to be after InitSettings BtManager_InitBt(); BtManager_StartBt();