add more sanity checks

This commit is contained in:
technyon
2022-08-12 23:28:18 +02:00
parent 0060acefce
commit 3de0802419

View File

@@ -366,21 +366,27 @@ void NukiWrapper::onKeypadCommandReceived(const char *command, const uint &id, c
} }
bool idExists = std::find(_keypadCodeIds.begin(), _keypadCodeIds.end(), id) != _keypadCodeIds.end(); bool idExists = std::find(_keypadCodeIds.begin(), _keypadCodeIds.end(), id) != _keypadCodeIds.end();
int codeInt = code.toInt();
NukiLock::CmdResult result = (NukiLock::CmdResult)-1; NukiLock::CmdResult result = (NukiLock::CmdResult)-1;
if(strcmp(command, "add") == 0) if(strcmp(command, "add") == 0)
{ {
if(name == "") if(name == "" || name == "--")
{ {
_network->publishKeypadCommandResult("MissingParameterName"); _network->publishKeypadCommandResult("MissingParameterName");
return; return;
} }
if(codeInt == 0)
{
_network->publishKeypadCommandResult("MissingParameterCode");
return;
}
NukiLock::NewKeypadEntry entry; NukiLock::NewKeypadEntry entry;
memset(&entry, 0, sizeof(entry)); memset(&entry, 0, sizeof(entry));
size_t nameLen = name.length(); size_t nameLen = name.length();
memcpy(&entry.name, name.c_str(), nameLen > 20 ? 20 : nameLen); memcpy(&entry.name, name.c_str(), nameLen > 20 ? 20 : nameLen);
entry.code = code.toInt(); entry.code = codeInt;
result = _nukiLock.addKeypadEntry(entry); result = _nukiLock.addKeypadEntry(entry);
Serial.print("Add keypad code: "); Serial.println((int)result); Serial.print("Add keypad code: "); Serial.println((int)result);
updateKeypad(); updateKeypad();
@@ -398,11 +404,16 @@ void NukiWrapper::onKeypadCommandReceived(const char *command, const uint &id, c
} }
else if(strcmp(command, "update") == 0) else if(strcmp(command, "update") == 0)
{ {
if(name == "") if(name == "" || name == "--")
{ {
_network->publishKeypadCommandResult("MissingParameterName"); _network->publishKeypadCommandResult("MissingParameterName");
return; return;
} }
if(codeInt == 0)
{
_network->publishKeypadCommandResult("MissingParameterCode");
return;
}
if(!idExists) if(!idExists)
{ {
_network->publishKeypadCommandResult("UnknownId"); _network->publishKeypadCommandResult("UnknownId");
@@ -414,7 +425,7 @@ void NukiWrapper::onKeypadCommandReceived(const char *command, const uint &id, c
entry.codeId = id; entry.codeId = id;
size_t nameLen = name.length(); size_t nameLen = name.length();
memcpy(&entry.name, name.c_str(), nameLen > 20 ? 20 : nameLen); memcpy(&entry.name, name.c_str(), nameLen > 20 ? 20 : nameLen);
entry.code = code.toInt(); entry.code = codeInt;
entry.enabled = enabled == 0 ? 0 : 1; entry.enabled = enabled == 0 ? 0 : 1;
result = _nukiLock.updateKeypadEntry(entry); result = _nukiLock.updateKeypadEntry(entry);
Serial.print("Update keypad code: "); Serial.println((int)result); Serial.print("Update keypad code: "); Serial.println((int)result);