Skip to content

Commit

Permalink
Improve pairing reliability and speed
Browse files Browse the repository at this point in the history
  • Loading branch information
iranl committed Jun 18, 2024
1 parent 1c0ecf3 commit 1087910
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
13 changes: 8 additions & 5 deletions src/NukiBle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,17 @@ PairingResult NukiBle::pairNuki(AuthorizationIdType idType) {

if (retrieveCredentials()) {
#ifdef DEBUG_NUKI_CONNECT
log_d("Allready paired");
log_d("Already paired");
#endif
isPaired = true;
return PairingResult::Success;
}
PairingResult result = PairingResult::Pairing;

if (pairingLastSeen < millis() - 2000) pairingServiceAvailable = false;

if (pairingServiceAvailable && bleAddress != BLEAddress("")) {
pairingServiceAvailable = false;
#ifdef DEBUG_NUKI_CONNECT
log_d("Nuki in pairing mode found");
#endif
Expand All @@ -94,6 +97,7 @@ PairingResult NukiBle::pairNuki(AuthorizationIdType idType) {
PairingState nukiPairingState = PairingState::InitPairing;
do {
nukiPairingState = pairStateMachine(nukiPairingState);
extendDisconnectTimeout();
delay(50);
} while ((nukiPairingState != PairingState::Success) && (nukiPairingState != PairingState::Timeout));

Expand All @@ -104,7 +108,7 @@ PairingResult NukiBle::pairNuki(AuthorizationIdType idType) {
} else {
result = PairingResult::Timeout;
}
extendDisonnectTimeout();
extendDisconnectTimeout();
}
} else {
#ifdef DEBUG_NUKI_CONNECT
Expand Down Expand Up @@ -196,7 +200,7 @@ void NukiBle::setConnectRetries(uint8_t retries) {
connectRetries = retries;
}

void NukiBle::extendDisonnectTimeout() {
void NukiBle::extendDisconnectTimeout() {
lastStartTimeout = millis();
}

Expand Down Expand Up @@ -259,8 +263,7 @@ void NukiBle::onResult(BLEAdvertisedDevice* advertisedDevice) {
#endif
bleAddress = advertisedDevice->getAddress();
pairingServiceAvailable = true;
} else {
pairingServiceAvailable = false;
pairingLastSeen = millis();
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/NukiBle.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ class NukiBle : public BLEClientCallbacks, public BleScanner::Subscriber {

protected:
bool connectBle(const BLEAddress bleAddress);
void extendDisonnectTimeout();
void extendDisconnectTimeout();

template <typename TDeviceAction>
Nuki::CmdResult executeAction(const TDeviceAction action);
Expand Down Expand Up @@ -319,6 +319,7 @@ class NukiBle : public BLEClientCallbacks, public BleScanner::Subscriber {
uint16_t timeoutDuration = 1000;
uint8_t connectTimeoutSec = 1;
uint8_t connectRetries = 5;
unsigned long pairingLastSeen = 0;

void onConnect(BLEClient*) override;
void onDisconnect(BLEClient*) override;
Expand Down
8 changes: 4 additions & 4 deletions src/NukiBle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Nuki::CmdResult NukiBle::executeAction(const TDeviceAction action) {
Nuki::CmdResult result = cmdStateMachine(action);
if (result != Nuki::CmdResult::Working) {
giveNukiBleSemaphore();
extendDisonnectTimeout();
extendDisconnectTimeout();
return result;
}
esp_task_wdt_reset();
Expand All @@ -43,7 +43,7 @@ Nuki::CmdResult NukiBle::executeAction(const TDeviceAction action) {
Nuki::CmdResult result = cmdChallStateMachine(action);
if (result != Nuki::CmdResult::Working) {
giveNukiBleSemaphore();
extendDisonnectTimeout();
extendDisconnectTimeout();
return result;
}
esp_task_wdt_reset();
Expand All @@ -54,7 +54,7 @@ Nuki::CmdResult NukiBle::executeAction(const TDeviceAction action) {
Nuki::CmdResult result = cmdChallAccStateMachine(action);
if (result != Nuki::CmdResult::Working) {
giveNukiBleSemaphore();
extendDisonnectTimeout();
extendDisconnectTimeout();
return result;
}
esp_task_wdt_reset();
Expand All @@ -65,7 +65,7 @@ Nuki::CmdResult NukiBle::executeAction(const TDeviceAction action) {
Nuki::CmdResult result = cmdChallStateMachine(action, true);
if (result != Nuki::CmdResult::Working) {
giveNukiBleSemaphore();
extendDisonnectTimeout();
extendDisconnectTimeout();
return result;
}
esp_task_wdt_reset();
Expand Down
2 changes: 1 addition & 1 deletion src/NukiLock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ void NukiLock::createNewAdvancedConfig(const AdvancedConfig* oldConfig, NewAdvan
}

void NukiLock::handleReturnMessage(Command returnCode, unsigned char* data, uint16_t dataLen) {
extendDisonnectTimeout();
extendDisconnectTimeout();

switch (returnCode) {
case Command::KeyturnerStates : {
Expand Down
2 changes: 1 addition & 1 deletion src/NukiOpener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ void NukiOpener::createNewAdvancedConfig(const AdvancedConfig* oldConfig, NewAdv


void NukiOpener::handleReturnMessage(Command returnCode, unsigned char* data, uint16_t dataLen) {
extendDisonnectTimeout();
extendDisconnectTimeout();

switch (returnCode) {
case Command::KeyturnerStates : {
Expand Down

0 comments on commit 1087910

Please sign in to comment.