Skip to content

Commit d8f5932

Browse files
committed
Implement exponential backoff when aggresive devices turn up.
1 parent c333f0a commit d8f5932

7 files changed

Lines changed: 29 additions & 19 deletions

File tree

device/src/bt_conn.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ static void connectHid(struct bt_conn *conn, connection_id_t connectionId, conne
482482
// Assume that HOGP is ready
483483
LOG_INF("Established HID connection with %s\n", GetPeerStringByConn(conn));
484484
Connections_SetState(connectionId, ConnectionState_Ready);
485-
BtManager_StartScanningAndAdvertisingAsync("StartScanningAndAdvertisingAsync in connectHid");
485+
BtManager_StartScanningAndAdvertisingAsync(false, "StartScanningAndAdvertisingAsync in connectHid");
486486
}
487487

488488
#define BT_UUID_NUS_VAL BT_UUID_128_ENCODE(0x6e400001, 0xb5a3, 0xf393, 0xe0a9, 0xe50e24dcca9e)
@@ -566,15 +566,15 @@ static void connected(struct bt_conn *conn, uint8_t err) {
566566

567567
if (connectionId == ConnectionId_Invalid) {
568568
connectUnknown(conn);
569-
BtManager_StartScanningAndAdvertisingAsync("StartScanningAndAdvertisingAsync in connected - invalid connection");
569+
BtManager_StartScanningAndAdvertisingAsync(true, "StartScanningAndAdvertisingAsync in connected - invalid connection");
570570
} else {
571571

572572
if (isWanted(conn, false, connectionId, connectionType)) {
573573
bt_conn_set_security(conn, BT_SECURITY_L4);
574574
// advertising/scanning needs to be started only after peers are assigned :-/
575575
} else {
576576
youAreNotWanted(conn);
577-
BtManager_StartScanningAndAdvertisingAsync("StartScanningAndAdvertisingAsync in connected - they are not wanted");
577+
BtManager_StartScanningAndAdvertisingAsync(true, "StartScanningAndAdvertisingAsync in connected - they are not wanted");
578578
}
579579
}
580580

@@ -618,7 +618,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason) {
618618
}
619619

620620
if (!BtManager_Restarting) {
621-
BtManager_StartScanningAndAdvertisingAsync("StartScanningAndAdvertisingAsync in disconnected");
621+
BtManager_StartScanningAndAdvertisingAsync(true, "StartScanningAndAdvertisingAsync in disconnected");
622622
}
623623

624624
if (conn == auth_conn) {
@@ -631,7 +631,7 @@ void Bt_SetConnectionConfigured(struct bt_conn* conn) {
631631
uint8_t peerId = GetPeerIdByConn(conn);
632632
if (Connections[Peers[peerId].connectionId].state != ConnectionState_Ready) {
633633
Connections_SetState(Peers[peerId].connectionId, ConnectionState_Ready);
634-
BtManager_StartScanningAndAdvertisingAsync("StartScanningAndAdvertisingAsync in SetConnectionConfigured");
634+
BtManager_StartScanningAndAdvertisingAsync(false, "StartScanningAndAdvertisingAsync in SetConnectionConfigured");
635635
}
636636
}
637637

@@ -667,7 +667,7 @@ static void connectAuthenticatedConnection(struct bt_conn *conn, connection_id_t
667667
default:
668668
LOG_WRN("Authenticated connection is not known. Disconnecting %s", GetPeerStringByConn(conn));
669669
safeDisconnect(conn, BT_HCI_ERR_AUTH_FAIL);
670-
BtManager_StartScanningAndAdvertisingAsync("StartScanningAndAdvertisingAsync in authenticatedConnection - is unknown");
670+
BtManager_StartScanningAndAdvertisingAsync(true, "StartScanningAndAdvertisingAsync in authenticatedConnection - is unknown");
671671
break;
672672
}
673673
}
@@ -848,7 +848,7 @@ static void pairing_complete(struct bt_conn *conn, bool bonded) {
848848
PairingScreen_Feedback(true);
849849
}
850850

851-
BtManager_StartScanningAndAdvertisingAsync("StartScanningAndAdvertisingAsync in pairing_complete");
851+
BtManager_StartScanningAndAdvertisingAsync(false, "StartScanningAndAdvertisingAsync in pairing_complete");
852852
}
853853

854854
static void bt_foreach_conn_cb(struct bt_conn *conn, void *user_data) {
@@ -1014,7 +1014,7 @@ void BtConn_ReserveConnections() {
10141014
disconnectOldestHost();
10151015
// Advertising will get started when the host actually gets disconnected
10161016
} else {
1017-
BtManager_StartScanningAndAdvertisingAsync("StartScanningAndAdvertisingAsync in ReserveConnections");
1017+
BtManager_StartScanningAndAdvertisingAsync(false, "StartScanningAndAdvertisingAsync in ReserveConnections");
10181018
}
10191019
WIDGET_REFRESH(&TargetWidget);
10201020
} else {

device/src/bt_manager.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,21 @@ void BtManager_StopBt() {
8888
}
8989

9090

91-
void BtManager_StartScanningAndAdvertisingAsync(const char* eventLabel) {
91+
void BtManager_StartScanningAndAdvertisingAsync(bool wasAggresive, const char* eventLabel) {
9292
BT_TRACE_AND_ASSERT("bm4");
93-
uint32_t delay = 50;
94-
LOG_INF("btManager: BtManager_StartScanningAndAdvertisingAsync because %s\n", eventLabel);
95-
EventScheduler_Reschedule(Timer_GetCurrentTime() + delay, EventSchedulerEvent_BtStartScanningAndAdvertising, eventLabel);
93+
uint32_t maxDelay = 5000;
94+
uint32_t minDelay = 20;
95+
static int8_t aggressiveTries = 0;
96+
97+
aggressiveTries = wasAggresive ? aggressiveTries + 1 : 0;
98+
aggressiveTries = MAX(0, aggressiveTries);
99+
aggressiveTries = MIN(aggressiveTries, 16);
100+
101+
uint32_t expDelay = MIN(maxDelay, minDelay << aggressiveTries);
102+
103+
104+
LOG_INF("btManager: BtManager_StartScanningAndAdvertisingAsync because %s, delay %d\n", eventLabel, expDelay);
105+
EventScheduler_Reschedule(Timer_GetCurrentTime() + expDelay, EventSchedulerEvent_BtStartScanningAndAdvertising, eventLabel);
96106
}
97107

98108
/*

device/src/bt_manager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
void BtManager_StopBt();
2525
void BtManager_RestartBt();
2626
void BtManager_StartScanningAndAdvertising();
27-
void BtManager_StartScanningAndAdvertisingAsync(const char* eventLabel);
27+
void BtManager_StartScanningAndAdvertisingAsync(bool wasAggresive, const char* eventLabel);
2828
void BtManager_EnterMode(pairing_mode_t mode, bool toggle);
2929

3030
#endif // __BT_MANAGER_H__

device/src/bt_pair.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void BtManager_EnterMode(pairing_mode_t mode, bool toggle) {
8787
if (mode == PairingMode_PairHid) {
8888
BtConn_MakeSpaceForHid();
8989
}
90-
BtManager_StartScanningAndAdvertisingAsync("StartScanningAndAdvertisingAsync in BtManager_EnterMode - start advertising");
90+
BtManager_StartScanningAndAdvertisingAsync(false, "StartScanningAndAdvertisingAsync in BtManager_EnterMode - start advertising");
9191
if (mode != defaultMode) {
9292
EventScheduler_Reschedule(k_uptime_get_32() + USER_PAIRING_TIMEOUT, EventSchedulerEvent_EndBtPairing, "User pairing mode timeout.");
9393
}
@@ -156,7 +156,7 @@ void BtPair_EndPairing(bool success, const char* msg) {
156156
BtAdvertise_Stop();
157157
#endif
158158

159-
BtManager_StartScanningAndAdvertisingAsync("StartScanningAndAdvertisingAsync in BtPair_EndPairing");
159+
BtManager_StartScanningAndAdvertisingAsync(false, "StartScanningAndAdvertisingAsync in BtPair_EndPairing");
160160
}
161161

162162
struct delete_args_t {

device/src/device_state.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void handleStateTransition(connection_target_t remote, connection_id_t connectio
5454
EventVector_WakeMain();
5555
}
5656

57-
BtManager_StartScanningAndAdvertisingAsync("StartScanningAndAdvertisingAsync in handleStateTransition - left was disconnected");
57+
BtManager_StartScanningAndAdvertisingAsync(false, "StartScanningAndAdvertisingAsync in handleStateTransition - left was disconnected");
5858
}
5959
break;
6060
case ConnectionTarget_Host:

right/src/host_connection.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void HostConnection_SetSelectedConnection(uint8_t connectionId) {
8383

8484
void HostConnection_Unselect() {
8585
HostConnection_SetSelectedConnection(ConnectionId_Invalid);
86-
BtManager_StartScanningAndAdvertisingAsync("StartScanningAndAdvertisingAsync in HostConnection_Unselect");
86+
BtManager_StartScanningAndAdvertisingAsync(false, "StartScanningAndAdvertisingAsync in HostConnection_Unselect");
8787
}
8888

8989
static void selectConnection(uint8_t connectionId) {

right/src/macros/set_command.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,12 +376,12 @@ static macro_variable_t bluetooth(parser_context_t* ctx, set_command_action_t ac
376376
ASSIGN_BOOL(Cfg.Bt_AlwaysAdvertiseHid);
377377
#if DEVICE_IS_UHK80_RIGHT
378378
BtManager_EnterMode(PairingMode_Default, false);
379-
BtManager_StartScanningAndAdvertisingAsync("StartScanningAndAdvertisingAsync in set_command - alwaysAdvertiseHid changed");
379+
BtManager_StartScanningAndAdvertisingAsync(false, "StartScanningAndAdvertisingAsync in set_command - alwaysAdvertiseHid changed");
380380
#endif
381381
} else if (ConsumeToken(ctx, "directedAdvertisingAllowed")) {
382382
ASSIGN_BOOL(Cfg.Bt_DirectedAdvertisingAllowed);
383383
#ifdef __ZEPHYR__
384-
BtManager_StartScanningAndAdvertisingAsync("StartScanningAndAdvertisingAsync in set_command - directedAdvertisingAllowed changed");
384+
BtManager_StartScanningAndAdvertisingAsync(false, "StartScanningAndAdvertisingAsync in set_command - directedAdvertisingAllowed changed");
385385
#endif
386386
} else {
387387
Macros_ReportErrorTok(ctx, "Parameter not recognized:");

0 commit comments

Comments
 (0)