Skip to content

Commit

Permalink
Merge pull request #822 from UltimateHackingKeyboard/fix-new-pairings
Browse files Browse the repository at this point in the history
Fix NewPairings flag and add GetNewPairings paging.
  • Loading branch information
kareltucek authored Dec 20, 2024
2 parents 7337f9a + e7bf227 commit fba425d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 22 deletions.
7 changes: 7 additions & 0 deletions device/src/bt_pair.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "bt_advertise.h"
#include "host_connection.h"
#include "settings.h"
#include "usb_commands/usb_command_get_new_pairings.h"

bool BtPair_LastPairingSucceeded = true;
bool BtPair_OobPairingInProgress = false;
Expand Down Expand Up @@ -180,6 +181,7 @@ void BtPair_Unpair(const bt_addr_le_t addr) {

// Update settings
Settings_Reload();
UsbCommand_UpdateNewPairingsFlag();
}

struct check_bonded_device_args_t {
Expand Down Expand Up @@ -216,11 +218,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();
}
10 changes: 5 additions & 5 deletions device/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion right/src/usb_commands/usb_command_get_device_property.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "slave_drivers/kboot_driver.h"
#endif

#include <stdbool.h>
#include <stdint.h>
#include "slave_protocol.h"
#include "timer.h"
#include "usb_commands/usb_command_get_new_pairings.h"
Expand Down Expand Up @@ -105,7 +107,7 @@ void UsbCommand_GetDeviceProperty(const uint8_t *GenericHidOutBuffer, uint8_t *G
} break;
case DevicePropertyId_NewPairings:
#ifdef __ZEPHYR__
UsbCommand_GetNewPairings(GenericHidOutBuffer, GenericHidInBuffer);
UsbCommand_GetNewPairings(GetUsbRxBufferUint8(2), GenericHidOutBuffer, GenericHidInBuffer);
#endif
break;
default:
Expand Down
53 changes: 38 additions & 15 deletions right/src/usb_commands/usb_command_get_new_pairings.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,72 @@
#include <zephyr/bluetooth/addr.h>
#include "host_connection.h"

#define ADDRESS_COUNT_PER_PAGE 10

typedef struct {
const uint8_t *OutBuffer;
uint8_t *InBuffer;
uint8_t idx;
uint8_t count;
uint8_t pageIdxOffset;
uint8_t writeOffset;
uint8_t addressCount;
bool dryRun;
} CommandUserData;


static void bt_foreach_bond_cb(const struct bt_bond_info *info, void *user_data)
{
CommandUserData *data = (CommandUserData *)user_data;
uint8_t *GenericHidInBuffer = data->InBuffer;

if ((data->idx + BLE_ADDR_LEN + 1) >= USB_GENERIC_HID_IN_BUFFER_LENGTH) {
if ((data->writeOffset + BLE_ADDR_LEN + 1) >= USB_GENERIC_HID_IN_BUFFER_LENGTH) {
return;
}

if (HostConnections_IsKnownBleAddress(&info->addr)) {
return;
}

data->count++;
Bt_NewPairedDevice = true;

SetUsbTxBufferBleAddress(data->idx, &info->addr);
data->idx += BLE_ADDR_LEN;
data->addressCount++;

if (!data->dryRun && data->addressCount >= data->pageIdxOffset && data->addressCount < data->pageIdxOffset+ADDRESS_COUNT_PER_PAGE) {
SetUsbTxBufferBleAddress(data->writeOffset, &info->addr);
data->writeOffset += BLE_ADDR_LEN;
}

// Name placeholder
SetUsbTxBufferUint8(data->idx++, 0);
}

void UsbCommand_GetNewPairings(const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidInBuffer) {
void UsbCommand_UpdateNewPairingsFlag() {

CommandUserData data = {
.OutBuffer = NULL,
.InBuffer = NULL,
.pageIdxOffset = 0,
.writeOffset = 2,
.addressCount = 0,
.dryRun = true,
};

bt_foreach_bond(BT_ID_DEFAULT, bt_foreach_bond_cb, &data);
}

void UsbCommand_GetNewPairings(uint8_t page, const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidInBuffer) {
CommandUserData data = {
.OutBuffer = GenericHidOutBuffer,
.InBuffer = GenericHidInBuffer,
.idx = 2,
.count = 0
.pageIdxOffset = ADDRESS_COUNT_PER_PAGE*page,
.writeOffset = 2,
.addressCount = 0,
.dryRun = false,
};

bt_foreach_bond(BT_ID_DEFAULT, bt_foreach_bond_cb, &data);

SetUsbTxBufferUint8(1, data.count);

Bt_NewPairedDevice = false;
if (data.addressCount < data.pageIdxOffset) {
SetUsbTxBufferUint8(1, 0);
} else {
SetUsbTxBufferUint8(1, data.addressCount-data.pageIdxOffset);
}
}

#endif
Expand Down
7 changes: 6 additions & 1 deletion right/src/usb_commands/usb_command_get_new_pairings.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@

#ifdef __ZEPHYR__

// Includes:

#include <stdint.h>

// Typedefs:

// Functions:

void UsbCommand_GetNewPairings(const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidInBuffer);
void UsbCommand_UpdateNewPairingsFlag();
void UsbCommand_GetNewPairings(uint8_t page, const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidInBuffer);

#endif

Expand Down

0 comments on commit fba425d

Please sign in to comment.