Skip to content

Commit

Permalink
Merge pull request #40 from Kelesys62/FixIssue39
Browse files Browse the repository at this point in the history
Set the correct payload size when using parameter 'nameSuffix' for lo…
  • Loading branch information
Bascy authored Mar 12, 2024
2 parents 707b327 + c18ce85 commit a69a979
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
9 changes: 6 additions & 3 deletions src/NukiLock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ NukiLock::NukiLock(const std::string& deviceName, const uint32_t deviceId)

Nuki::CmdResult NukiLock::lockAction(const LockAction lockAction, const uint32_t nukiAppId, const uint8_t flags, const char* nameSuffix, const uint8_t nameSuffixLen) {
Action action;
unsigned char payload[5 + nameSuffixLen] = {0};
unsigned char payload[sizeof(LockAction) + 4 + 1 + 20] = {0};
memcpy(payload, &lockAction, sizeof(LockAction));
memcpy(&payload[sizeof(LockAction)], &nukiAppId, 4);
memcpy(&payload[sizeof(LockAction) + 4], &flags, 1);
uint8_t payloadLen = 0;
if (nameSuffix) {
memcpy(&payload[sizeof(LockAction) + 4 + 1], nameSuffix, nameSuffixLen);
payloadLen = sizeof(LockAction) + 4 + 1 + nameSuffixLen;
//If nameSuffixLen is between 1 & 18, use it, else use 19 (keep 1 for ending '\0')
uint8_t len = nameSuffixLen>0 && nameSuffixLen<19 ? nameSuffixLen : 19;
strncpy((char*)&payload[sizeof(LockAction) + 4 + 1], nameSuffix, len);
payload[sizeof(LockAction) + 4 + 1 + len] = '\0'; //In any case, add '\0' at end of string
payloadLen = sizeof(LockAction) + 4 + 1 + 20;
} else {
payloadLen = sizeof(LockAction) + 4 + 1;
}
Expand Down
3 changes: 1 addition & 2 deletions src/NukiLock.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ class NukiLock : public Nuki::NukiBle {
* @param nukiAppId 0 = App, 1 = Bridge, 2 = Fob, 3 = Keypad
* @param flags optional
* @param nameSuffix optional
* @param nameSuffixLen len of nameSuffix if used
* @param nameSuffixLen len of nameSuffix if used ('\0' included, maximum 19)
* @return Nuki::CmdResult
*/
Nuki::CmdResult lockAction(const LockAction lockAction, const uint32_t nukiAppId = 1, const uint8_t flags = 0,
const char* nameSuffix = nullptr, const uint8_t nameSuffixLen = 0);


/**
* @brief Requests keyturner state from Lock via BLE
*
Expand Down

0 comments on commit a69a979

Please sign in to comment.