Skip to content

Commit

Permalink
Add (back) some delays to allow task yielding
Browse files Browse the repository at this point in the history
  • Loading branch information
iranl committed Jan 2, 2025
1 parent 198482d commit 84865a9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
28 changes: 19 additions & 9 deletions src/NukiBle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ PairingResult NukiBle::pairNuki(AuthorizationIdType idType) {
do {
nukiPairingState = pairStateMachine(nukiPairingState);
extendDisconnectTimeout();
delay(50);
delay(10);
} while ((nukiPairingState != PairingState::Success) && (nukiPairingState != PairingState::Timeout));

if (nukiPairingState == PairingState::Success) {
Expand All @@ -197,6 +197,7 @@ PairingResult NukiBle::pairNuki(AuthorizationIdType idType) {
result = PairingResult::Timeout;
}
extendDisconnectTimeout();
delay(10);
}
} else {
if (debugNukiConnect) {
Expand Down Expand Up @@ -224,15 +225,17 @@ void NukiBle::resetHost() {
if (debugNukiConnect) {
logMessageVar("[%s] Resetting BLE host", deviceName.c_str());
}

ble_hs_sched_reset(0);
delay(10);
}

bool NukiBle::connectBle(const BLEAddress bleAddress, bool pairing) {
if (altConnect) {
connecting = true;
bleScanner->enableScanning(false);
pClient = nullptr;
delay(10);

if (debugNukiConnect) {
#if (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0))
Expand Down Expand Up @@ -315,11 +318,12 @@ bool NukiBle::connectBle(const BLEAddress bleAddress, bool pairing) {
pClient->setConnectTimeout(connectTimeoutSec * 1000);
#endif

delay(300);

int loopCreateClient = 0;

while(!pClient && loopCreateClient < 50) {
if (debugNukiConnect) {
logMessage(".");
}
delay(100);
loopCreateClient++;
}
Expand Down Expand Up @@ -464,15 +468,17 @@ void NukiBle::updateConnectionState() {
#else
if (lastStartTimeout != 0 && ((esp_timer_get_time() / 1000) - lastStartTimeout > timeoutDuration)) {
#endif
if (debugNukiConnect) {
logMessage("disconnecting BLE on timeout");
}

if (altConnect) {
delay(100);
disconnect();
delay(200);
delay(100);
}
else if (pClient && pClient->isConnected()) {
pClient->disconnect();
if (debugNukiConnect) {
logMessage("disconnecting BLE on timeout");
}
}
}
}
Expand Down Expand Up @@ -501,7 +507,9 @@ void NukiBle::disconnect()
int loop = 0;

while ((countDisconnects > 0 || pClient->isConnected()) && loop < 50) {
logMessage(".");
if (debugNukiConnect) {
logMessage(".");
}
loop++;
delay(100);
}
Expand Down Expand Up @@ -1524,6 +1532,8 @@ void NukiBle::notifyCallback(BLERemoteCharacteristic* pBLERemoteCharacteristic,
}

void NukiBle::handleReturnMessage(Command returnCode, unsigned char* data, uint16_t dataLen) {
delay(10);

switch (returnCode) {
case Command::RequestData : {
if (debugNukiCommunication) {
Expand Down
10 changes: 8 additions & 2 deletions src/NukiBle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Nuki::CmdResult NukiBle::executeAction(const TDeviceAction action) {

while (1) {
extendDisconnectTimeout();

Nuki::CmdResult result;
if (action.cmdType == Nuki::CommandType::Command) {
result = cmdStateMachine(action);
Expand Down Expand Up @@ -74,7 +74,9 @@ Nuki::CmdResult NukiBle::executeAction(const TDeviceAction action) {

template <typename TDeviceAction>
Nuki::CmdResult NukiBle::cmdStateMachine(const TDeviceAction action) {
extendDisconnectTimeout();
extendDisconnectTimeout();
delay(10);

switch (nukiCommandState) {
case CommandState::Idle: {
if (debugNukiCommunication) {
Expand Down Expand Up @@ -159,6 +161,8 @@ Nuki::CmdResult NukiBle::cmdStateMachine(const TDeviceAction action) {
template <typename TDeviceAction>
Nuki::CmdResult NukiBle::cmdChallStateMachine(const TDeviceAction action, const bool sendPinCode) {
extendDisconnectTimeout();
delay(10);

switch (nukiCommandState) {
case CommandState::Idle: {
if (debugNukiCommunication) {
Expand Down Expand Up @@ -304,6 +308,8 @@ Nuki::CmdResult NukiBle::cmdChallStateMachine(const TDeviceAction action, const
template <typename TDeviceAction>
Nuki::CmdResult NukiBle::cmdChallAccStateMachine(const TDeviceAction action) {
extendDisconnectTimeout();
delay(10);

switch (nukiCommandState) {
case CommandState::Idle: {
if (debugNukiCommunication) {
Expand Down

0 comments on commit 84865a9

Please sign in to comment.