mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 15:59:52 +03:00
Fix crash on unknown commands (cf==nullptr)
This commit is contained in:
@@ -38,13 +38,13 @@ uint8_t Command::call(const uint8_t device_type, const char * cmd, const char *
|
||||
auto cf = find_command(device_type, cmd_new, id_new);
|
||||
if ((cf == nullptr) || (cf->cmdfunction_json_)) {
|
||||
LOG_WARNING(F("Command %s on %s not found"), cmd, EMSdevice::device_type_2_device_name(device_type).c_str());
|
||||
return 2; // command not found
|
||||
return CommandRet::NOT_FOUND;
|
||||
}
|
||||
|
||||
// check if we're allowed to call it
|
||||
if (cf->has_flags(CommandFlag::ADMIN_ONLY) && !authenticated) {
|
||||
LOG_WARNING(F("Command %s on %s not permitted. requires admin."), cmd, EMSdevice::device_type_2_device_name(device_type).c_str());
|
||||
return 4; // command not allowed
|
||||
return CommandRet::NOT_ALLOWED;
|
||||
}
|
||||
|
||||
std::string dname = EMSdevice::device_type_2_device_name(device_type);
|
||||
@@ -56,7 +56,7 @@ uint8_t Command::call(const uint8_t device_type, const char * cmd, const char *
|
||||
LOG_INFO(F("Calling %s command '%s', value %s, id is %d"), dname.c_str(), cmd, value, id);
|
||||
}
|
||||
|
||||
return ((cf->cmdfunction_)(value, id_new));
|
||||
return ((cf->cmdfunction_)(value, id_new)) ? CommandRet::OK : CommandRet::ERROR;
|
||||
}
|
||||
|
||||
// calls a command. Takes a json object for output.
|
||||
@@ -70,9 +70,11 @@ uint8_t Command::call(const uint8_t device_type, const char * cmd, const char *
|
||||
auto cf = find_command(device_type, cmd_new, id_new);
|
||||
|
||||
// check if we're allowed to call it
|
||||
if (cf->has_flags(CommandFlag::ADMIN_ONLY) && !authenticated) {
|
||||
LOG_WARNING(F("Command %s on %s not permitted. requires admin."), cmd, EMSdevice::device_type_2_device_name(device_type).c_str());
|
||||
return 4; // command not allowed
|
||||
if (cf != nullptr) {
|
||||
if (cf->has_flags(CommandFlag::ADMIN_ONLY) && !authenticated && value != nullptr) {
|
||||
LOG_WARNING(F("Command %s on %s not permitted. requires admin."), cmd, EMSdevice::device_type_2_device_name(device_type).c_str());
|
||||
return CommandRet::NOT_ALLOWED; // command not allowed
|
||||
}
|
||||
}
|
||||
|
||||
std::string dname = EMSdevice::device_type_2_device_name(device_type);
|
||||
@@ -87,21 +89,21 @@ uint8_t Command::call(const uint8_t device_type, const char * cmd, const char *
|
||||
// check if json object is empty, if so quit
|
||||
if (json.isNull()) {
|
||||
LOG_WARNING(F("Ignore call for command %s in %s because it has no json body"), cmd, EMSdevice::device_type_2_device_name(device_type).c_str());
|
||||
return 3;
|
||||
return CommandRet::ERROR;
|
||||
}
|
||||
|
||||
// this is for endpoints that don't have commands, i.e not writable (e.g. boiler/syspress)
|
||||
if (cf == nullptr) {
|
||||
return EMSESP::get_device_value_info(json, cmd_new, id_new, device_type);
|
||||
return EMSESP::get_device_value_info(json, cmd_new, id_new, device_type) ? CommandRet::OK : CommandRet::ERROR;
|
||||
}
|
||||
|
||||
if (cf->cmdfunction_json_) {
|
||||
return ((cf->cmdfunction_json_)(value, id_new, json));
|
||||
return ((cf->cmdfunction_json_)(value, id_new, json)) ? CommandRet::OK : CommandRet::ERROR;
|
||||
} else {
|
||||
if ((device_type != EMSdevice::DeviceType::SYSTEM) && (value == nullptr || strlen(value) == 0 || strcmp(value, "?") == 0 || strcmp(value, "*") == 0)) {
|
||||
return EMSESP::get_device_value_info(json, cmd_new, id_new, device_type);
|
||||
return EMSESP::get_device_value_info(json, cmd_new, id_new, device_type) ? CommandRet::OK : CommandRet::ERROR;
|
||||
}
|
||||
return ((cf->cmdfunction_)(value, id_new));
|
||||
return ((cf->cmdfunction_)(value, id_new)) ? CommandRet::OK : CommandRet::ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user