Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various enhancements and bug fixes #83

Merged
merged 8 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/NukiBle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,15 +464,16 @@ void NukiBle::updateConnectionState() {
#else
if (lastStartTimeout != 0 && ((esp_timer_get_time() / 1000) - lastStartTimeout > timeoutDuration)) {
#endif
if (debugNukiConnect) {
logMessage("disconnecting BLE on timeout");
}

if (altConnect) {
disconnect();
delay(200);
}
else if (pClient && pClient->isConnected()) {
pClient->disconnect();
if (debugNukiConnect) {
logMessage("disconnecting BLE on timeout");
}
pClient->disconnect();
}
}
}
Expand Down Expand Up @@ -501,7 +502,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 @@ -1562,7 +1565,7 @@ void NukiBle::handleReturnMessage(Command returnCode, unsigned char* data, uint1
case Command::AuthorizationEntry : {
printBuffer((byte*)data, dataLen, false, "authorizationEntry", debugNukiHexData, logger);
AuthorizationEntry authEntry;
memcpy(&authEntry, data, sizeof(authEntry));
memcpy(&authEntry, data, dataLen);
listOfAuthorizationEntries.push_back(authEntry);
if (debugNukiReadableData) {
NukiLock::logAuthorizationEntry(authEntry, true, logger);
Expand Down Expand Up @@ -1657,7 +1660,7 @@ void NukiBle::handleReturnMessage(Command returnCode, unsigned char* data, uint1
}
case Command::KeypadCode : {
KeypadEntry keypadEntry;
memcpy(&keypadEntry, data, sizeof(KeypadEntry));
memcpy(&keypadEntry, data, dataLen);
listOfKeyPadEntries.push_back(keypadEntry);
nrOfReceivedKeypadCodes++;

Expand Down
23 changes: 23 additions & 0 deletions src/NukiBle.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@
#define PAIRING_TIMEOUT 30000
#define HEARTBEAT_TIMEOUT 30000

#ifdef CONFIG_IDF_TARGET_ESP32P4
typedef enum {
ESP_PWR_LVL_N24 = 0, /*!< Corresponding to -24 dBm */
ESP_PWR_LVL_N21 = 1, /*!< Corresponding to -21 dBm */
ESP_PWR_LVL_N18 = 2, /*!< Corresponding to -18 dBm */
ESP_PWR_LVL_N15 = 3, /*!< Corresponding to -15 dBm */
ESP_PWR_LVL_N12 = 4, /*!< Corresponding to -12 dBm */
ESP_PWR_LVL_N9 = 5, /*!< Corresponding to -9 dBm */
ESP_PWR_LVL_N6 = 6, /*!< Corresponding to -6 dBm */
ESP_PWR_LVL_N3 = 7, /*!< Corresponding to -3 dBm */
ESP_PWR_LVL_N0 = 8, /*!< Corresponding to 0 dBm */
ESP_PWR_LVL_P3 = 9, /*!< Corresponding to +3 dBm */
ESP_PWR_LVL_P6 = 10, /*!< Corresponding to +6 dBm */
ESP_PWR_LVL_P9 = 11, /*!< Corresponding to +9 dBm */
ESP_PWR_LVL_P12 = 12, /*!< Corresponding to +12 dBm */
ESP_PWR_LVL_P15 = 13, /*!< Corresponding to +15 dBm */
ESP_PWR_LVL_P18 = 14, /*!< Corresponding to +18 dBm */
ESP_PWR_LVL_P20 = 15, /*!< Corresponding to +20 dBm */
ESP_PWR_LVL_P21 = 15, /*!< Corresponding to +20 dBm, this enum variable has been deprecated */
ESP_PWR_LVL_INVALID = 0xFF, /*!< Indicates an invalid value */
} esp_power_level_t;
#endif

namespace Nuki {
class NukiBle : public BLEClientCallbacks, public BleScanner::Subscriber {
public:
Expand Down
11 changes: 8 additions & 3 deletions src/NukiConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,25 @@ enum class DoorSensorState : uint8_t {
DoorClosed = 0x02,
DoorOpened = 0x03,
DoorStateUnknown = 0x04,
Calibrating = 0x05
Calibrating = 0x05,
Uncalibrated = 0x10,
Tampered = 0xF0,
Unknown = 0xFF
};

enum class BatteryType : uint8_t {
Alkali = 0x00,
Accumulators = 0x01,
Lithium = 0x02
Lithium = 0x02,
Unknown = 0xFF
};

enum class AdvertisingMode : uint8_t {
Automatic = 0x00,
Normal = 0x01,
Slow = 0x02,
Slowest = 0x03
Slowest = 0x03,
Unknown = 0xFF
};

enum class CommandType {
Expand Down
37 changes: 27 additions & 10 deletions src/NukiLock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,18 +623,35 @@ Nuki::CmdResult NukiLock::retrieveLogEntries(const uint32_t startIndex, const ui
}

bool NukiLock::isBatteryCritical() {
return ((keyTurnerState.criticalBatteryState & (1 << 0)) != 0);
if(keyTurnerState.criticalBatteryState != 255) {
return ((keyTurnerState.criticalBatteryState & 1) == 1);
}
return false;
}

bool NukiLock::isKeypadBatteryCritical() {
if ((keyTurnerState.accessoryBatteryState & (1 << 7)) != 0) {
return ((keyTurnerState.accessoryBatteryState & (1 << 6)) != 0);
if(keyTurnerState.accessoryBatteryState != 255) {
if ((keyTurnerState.accessoryBatteryState & 1) == 1) {
return ((keyTurnerState.accessoryBatteryState & 3) == 3);
}
}
return false;
}

bool NukiLock::isDoorSensorBatteryCritical() {
if(keyTurnerState.accessoryBatteryState != 255) {
if ((keyTurnerState.accessoryBatteryState & 4) == 4) {
return ((keyTurnerState.accessoryBatteryState & 12) == 12);
}
}
return false;
}

bool NukiLock::isBatteryCharging() {
return ((keyTurnerState.criticalBatteryState & (1 << 1)) != 0);
if(keyTurnerState.criticalBatteryState != 255) {
return ((keyTurnerState.criticalBatteryState & 2) == 2);
}
return false;
}

uint8_t NukiLock::getBatteryPerc() {
Expand Down Expand Up @@ -734,30 +751,30 @@ void NukiLock::handleReturnMessage(Command returnCode, unsigned char* data, uint
switch (returnCode) {
case Command::KeyturnerStates : {
printBuffer((byte*)data, dataLen, false, "keyturnerStates", debugNukiHexData, logger);
memcpy(&keyTurnerState, data, sizeof(keyTurnerState));
memcpy(&keyTurnerState, data, dataLen);
if (debugNukiReadableData) {
logKeyturnerState(keyTurnerState, true, logger);
}
break;
}
case Command::BatteryReport : {
printBuffer((byte*)data, dataLen, false, "batteryReport", debugNukiHexData, logger);
memcpy(&batteryReport, data, sizeof(batteryReport));
memcpy(&batteryReport, data, dataLen);
if (debugNukiReadableData) {
logBatteryReport(batteryReport, true, logger);
}
break;
}
case Command::Config : {
memcpy(&config, data, sizeof(config));
memcpy(&config, data, dataLen);
if (debugNukiReadableData) {
logConfig(config, true, logger);
}
printBuffer((byte*)data, dataLen, false, "config", debugNukiHexData, logger);
break;
}
case Command::AdvancedConfig : {
memcpy(&advancedConfig, data, sizeof(advancedConfig));
memcpy(&advancedConfig, data, dataLen);
if (debugNukiReadableData) {
logAdvancedConfig(advancedConfig, true, logger);
}
Expand All @@ -767,14 +784,14 @@ void NukiLock::handleReturnMessage(Command returnCode, unsigned char* data, uint
case Command::TimeControlEntry : {
printBuffer((byte*)data, dataLen, false, "timeControlEntry", debugNukiHexData, logger);
TimeControlEntry timeControlEntry;
memcpy(&timeControlEntry, data, sizeof(timeControlEntry));
memcpy(&timeControlEntry, data, dataLen);
listOfTimeControlEntries.push_back(timeControlEntry);
break;
}
case Command::LogEntry : {
printBuffer((byte*)data, dataLen, false, "logEntry", debugNukiHexData, logger);
LogEntry logEntry;
memcpy(&logEntry, data, sizeof(logEntry));
memcpy(&logEntry, data, dataLen);
listOfLogEntries.push_back(logEntry);
if (debugNukiReadableData) {
logLogEntry(logEntry, true, logger);
Expand Down
9 changes: 9 additions & 0 deletions src/NukiLock.h
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,15 @@ class NukiLock : public Nuki::NukiBle {
* @return true if critical
*/
bool isBatteryCritical();

/**
* @brief Returns door sensor battery critical state in case this is supported
*
* Note that `retrieveOpenerState()` needs to be called first to retrieve the needed data
*
* @return true if critical
*/
bool isDoorSensorBatteryCritical();

/**
* @brief Returns keypad battery critical state in case this is supported
Expand Down
Loading
Loading