From 2775f3287e03586b4a110ba60809928836df4022 Mon Sep 17 00:00:00 2001 From: Karel Tucek Date: Wed, 5 Aug 2026 15:19:04 +0200 Subject: [PATCH] Switchovers: move advertising controls into native UserConfig --- right/src/config_parser/parse_config.c | 19 +++++++ right/src/host_connection.c | 16 +++++- right/src/host_connection.h | 1 + .../usb_command_get_device_property.c | 7 ++- .../usb_command_get_device_property.h | 1 + .../usb_command_get_new_pairings.c | 51 ++++++++++++++----- .../usb_command_get_new_pairings.h | 3 +- scripts/package.json | 4 +- 8 files changed, 85 insertions(+), 17 deletions(-) diff --git a/right/src/config_parser/parse_config.c b/right/src/config_parser/parse_config.c index 4456be6df..9d89e76df 100644 --- a/right/src/config_parser/parse_config.c +++ b/right/src/config_parser/parse_config.c @@ -36,6 +36,7 @@ #ifdef __ZEPHYR__ #include "usb_commands/usb_command_get_new_pairings.h" +#include "bt_manager.h" #include "bt_pair.h" #include "state_sync.h" #else @@ -257,6 +258,16 @@ parser_error_t parseConfig(config_buffer_t *buffer) secondaryRoles_AdvancedStrategyTimeoutType = ReadUInt8(buffer); } + // Version 15: + + bool bt_AlwaysAdvertise = Cfg.Bt_AlwaysAdvertise; + bool bt_KeepConnectionsAlive = Cfg.Bt_KeepConnectionsAlive; + + if (DataModelVersion.major >= 15) { + bt_AlwaysAdvertise = ReadBool(buffer); + bt_KeepConnectionsAlive = ReadBool(buffer); + } + // HostConnection configuration if (VERSION_AT_LEAST(DataModelVersion, 8, 1, 0)) { @@ -418,6 +429,13 @@ parser_error_t parseConfig(config_buffer_t *buffer) } } + // Version 15 + + if (DataModelVersion.major >= 15) { + Cfg.Bt_AlwaysAdvertise = bt_AlwaysAdvertise; + Cfg.Bt_KeepConnectionsAlive = bt_KeepConnectionsAlive; + } + // Version 8 @@ -439,6 +457,7 @@ parser_error_t parseConfig(config_buffer_t *buffer) BtPair_AllocateUnregisteredBonds(); BtConn_UpdateHostConnectionPeerAllocations(); UsbCommand_UpdateNewPairingsFlag(); + BtManager_StartScanningAndAdvertisingAsync(false, "parse_config - bluetooth settings applied"); #endif WormCfg->devMode = Cfg.DevMode; LedManager_FullUpdate(); diff --git a/right/src/host_connection.c b/right/src/host_connection.c index a4e880d9e..0d741176b 100644 --- a/right/src/host_connection.c +++ b/right/src/host_connection.c @@ -31,7 +31,11 @@ host_connection_t HostConnections[HOST_CONNECTION_COUNT_MAX] = { }, }; -host_known_t HostConnections_IsKnownBleAddress(const bt_addr_le_t *address) { +host_known_t HostConnections_LookupBleAddress(const bt_addr_le_t *address, uint8_t *outConnectionId) { + if (outConnectionId) { + *outConnectionId = ConnectionId_Invalid; + } + for (int i = 0; i < HOST_CONNECTION_COUNT_MAX; i++) { host_connection_type_t type = HostConnections[i].type; switch (type) { @@ -43,12 +47,18 @@ host_known_t HostConnections_IsKnownBleAddress(const bt_addr_le_t *address) { break; case HostConnectionType_UnregisteredBtHid: if (BtAddrEq(address, &HostConnections[i].bleAddress)) { + if (outConnectionId) { + *outConnectionId = ConnectionId_HostConnectionFirst + i; + } return HostKnown_Unregistered; } break; case HostConnectionType_Dongle: case HostConnectionType_BtHid: if (BtAddrEq(address, &HostConnections[i].bleAddress)) { + if (outConnectionId) { + *outConnectionId = ConnectionId_HostConnectionFirst + i; + } return HostKnown_Registered; } break; @@ -66,6 +76,10 @@ host_known_t HostConnections_IsKnownBleAddress(const bt_addr_le_t *address) { return HostKnown_Unknown; } +host_known_t HostConnections_IsKnownBleAddress(const bt_addr_le_t *address) { + return HostConnections_LookupBleAddress(address, NULL); +} + host_connection_t* HostConnection(uint8_t connectionId) { if (connectionId < ConnectionId_HostConnectionFirst || connectionId > ConnectionId_HostConnectionLast) { return NULL; diff --git a/right/src/host_connection.h b/right/src/host_connection.h index 15f6113b6..bda24bbc2 100644 --- a/right/src/host_connection.h +++ b/right/src/host_connection.h @@ -67,6 +67,7 @@ // Functions: host_known_t HostConnections_IsKnownBleAddress(const bt_addr_le_t *address); + host_known_t HostConnections_LookupBleAddress(const bt_addr_le_t *address, uint8_t *outConnectionId); host_connection_t* HostConnection(uint8_t connectionId); void HostConnections_ListKnownBleConnections(); 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 4011cb43f..7b0b1371d 100644 --- a/right/src/usb_commands/usb_command_get_device_property.c +++ b/right/src/usb_commands/usb_command_get_device_property.c @@ -144,7 +144,12 @@ void UsbCommand_GetDeviceProperty(const uint8_t *GenericHidOutBuffer, uint8_t *G } break; case DevicePropertyId_NewPairings: #ifdef __ZEPHYR__ - UsbCommand_GetNewPairings(GetUsbRxBufferUint8(2), GenericHidOutBuffer, GenericHidInBuffer); + UsbCommand_GetNewPairings(GetUsbRxBufferUint8(2), false, GenericHidOutBuffer, GenericHidInBuffer); +#endif + break; + case DevicePropertyId_NewPairingsWithSlots: +#ifdef __ZEPHYR__ + UsbCommand_GetNewPairings(GetUsbRxBufferUint8(2), true, GenericHidOutBuffer, GenericHidInBuffer); #endif break; default: diff --git a/right/src/usb_commands/usb_command_get_device_property.h b/right/src/usb_commands/usb_command_get_device_property.h index 4ea049d4e..92f05a933 100644 --- a/right/src/usb_commands/usb_command_get_device_property.h +++ b/right/src/usb_commands/usb_command_get_device_property.h @@ -21,6 +21,7 @@ DevicePropertyId_PairedRightPeerBleAddress = 10, DevicePropertyId_PairingStatus = 11, DevicePropertyId_NewPairings = 12, + DevicePropertyId_NewPairingsWithSlots = 13, } device_property_t; typedef enum { 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 583e1b2ad..5d96d8265 100644 --- a/right/src/usb_commands/usb_command_get_new_pairings.c +++ b/right/src/usb_commands/usb_command_get_new_pairings.c @@ -2,9 +2,13 @@ #include "usb_protocol_handler.h" #include "bt_conn.h" #include +#include "connections.h" #include "host_connection.h" #define ADDRESS_COUNT_PER_PAGE 10 +#define ADDRESS_AND_SLOT_COUNT_PER_PAGE 8 + +#define HEADER_LENGTH 2 typedef struct { const uint8_t *OutBuffer; @@ -12,31 +16,52 @@ typedef struct { uint8_t pageIdxOffset; uint8_t writeOffset; uint8_t addressCount; + bool withSlots; bool dryRun; } CommandUserData; +static uint8_t entryLength(bool withSlots) { + return withSlots ? BLE_ADDR_LEN + 1 : BLE_ADDR_LEN; +} + +static uint8_t entriesPerPage(bool withSlots) { + return withSlots ? ADDRESS_AND_SLOT_COUNT_PER_PAGE : ADDRESS_COUNT_PER_PAGE; +} + 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->writeOffset + BLE_ADDR_LEN + 1) >= USB_COMMAND_BUFFER_LENGTH) { - return; - } + uint8_t connectionId; - if (HostConnections_IsKnownBleAddress(&info->addr) != HostKnown_Unregistered) { + if (HostConnections_LookupBleAddress(&info->addr, &connectionId) != HostKnown_Unregistered) { return; } Bt_NewPairedDevice = true; - data->addressCount++; + uint8_t addressIdx = data->addressCount++; + + if (data->dryRun) { + return; + } + + if (addressIdx < data->pageIdxOffset || addressIdx >= data->pageIdxOffset + entriesPerPage(data->withSlots)) { + return; + } - 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; + if (data->writeOffset + entryLength(data->withSlots) > USB_COMMAND_BUFFER_LENGTH) { + return; } + SetUsbTxBufferBleAddress(data->writeOffset, &info->addr); + data->writeOffset += BLE_ADDR_LEN; + + if (data->withSlots) { + SetUsbTxBufferUint8(data->writeOffset, connectionId - ConnectionId_HostConnectionFirst); + data->writeOffset += 1; + } } void UsbCommand_UpdateNewPairingsFlag() { @@ -46,21 +71,23 @@ void UsbCommand_UpdateNewPairingsFlag() { .OutBuffer = NULL, .InBuffer = NULL, .pageIdxOffset = 0, - .writeOffset = 2, + .writeOffset = HEADER_LENGTH, .addressCount = 0, + .withSlots = false, .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) { +void UsbCommand_GetNewPairings(uint8_t page, bool withSlots, const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidInBuffer) { CommandUserData data = { .OutBuffer = GenericHidOutBuffer, .InBuffer = GenericHidInBuffer, - .pageIdxOffset = ADDRESS_COUNT_PER_PAGE*page, - .writeOffset = 2, + .pageIdxOffset = entriesPerPage(withSlots)*page, + .writeOffset = HEADER_LENGTH, .addressCount = 0, + .withSlots = withSlots, .dryRun = false, }; 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 3da598a1d..1f2031999 100644 --- a/right/src/usb_commands/usb_command_get_new_pairings.h +++ b/right/src/usb_commands/usb_command_get_new_pairings.h @@ -10,13 +10,14 @@ // Includes: #include + #include // Typedefs: // Functions: void UsbCommand_UpdateNewPairingsFlag(); - void UsbCommand_GetNewPairings(uint8_t page, const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidInBuffer); + void UsbCommand_GetNewPairings(uint8_t page, bool withSlots, const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidInBuffer); #endif diff --git a/scripts/package.json b/scripts/package.json index 7aefa5883..ef1e1db77 100644 --- a/scripts/package.json +++ b/scripts/package.json @@ -19,10 +19,10 @@ "shelljs": "^0.8.4" }, "firmwareVersion": "17.2.0", - "deviceProtocolVersion": "4.18.1", + "deviceProtocolVersion": "4.19.0", "moduleProtocolVersion": "4.3.0", "dongleProtocolVersion": "2.0.0", - "userConfigVersion": "14.0.0", + "userConfigVersion": "15.0.0", "hardwareConfigVersion": "1.0.0", "smartMacrosVersion": "3.15.0", "devices": [