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);
|
auto cf = find_command(device_type, cmd_new, id_new);
|
||||||
if ((cf == nullptr) || (cf->cmdfunction_json_)) {
|
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());
|
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
|
// check if we're allowed to call it
|
||||||
if (cf->has_flags(CommandFlag::ADMIN_ONLY) && !authenticated) {
|
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());
|
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);
|
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);
|
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.
|
// 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);
|
auto cf = find_command(device_type, cmd_new, id_new);
|
||||||
|
|
||||||
// check if we're allowed to call it
|
// check if we're allowed to call it
|
||||||
if (cf->has_flags(CommandFlag::ADMIN_ONLY) && !authenticated) {
|
if (cf != nullptr) {
|
||||||
LOG_WARNING(F("Command %s on %s not permitted. requires admin."), cmd, EMSdevice::device_type_2_device_name(device_type).c_str());
|
if (cf->has_flags(CommandFlag::ADMIN_ONLY) && !authenticated && value != nullptr) {
|
||||||
return 4; // command not allowed
|
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);
|
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
|
// check if json object is empty, if so quit
|
||||||
if (json.isNull()) {
|
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());
|
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)
|
// this is for endpoints that don't have commands, i.e not writable (e.g. boiler/syspress)
|
||||||
if (cf == nullptr) {
|
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_) {
|
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 {
|
} else {
|
||||||
if ((device_type != EMSdevice::DeviceType::SYSTEM) && (value == nullptr || strlen(value) == 0 || strcmp(value, "?") == 0 || strcmp(value, "*") == 0)) {
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -413,7 +413,7 @@ void EMSESPShell::add_console_commands() {
|
|||||||
shell.println(F("Unknown command"));
|
shell.println(F("Unknown command"));
|
||||||
shell.print(F("Available commands are: "));
|
shell.print(F("Available commands are: "));
|
||||||
Command::show(shell, device_type, false); // non-verbose mode
|
Command::show(shell, device_type, false); // non-verbose mode
|
||||||
} else if (cmd_return == CommandRet::ERROR) {
|
} else if (cmd_return != CommandRet::OK) {
|
||||||
shell.println(F("Bad syntax"));
|
shell.println(F("Bad syntax"));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -351,7 +351,7 @@ void Mqtt::on_message(const char * fulltopic, const char * payload, size_t len)
|
|||||||
if (cmd_return == CommandRet::NOT_FOUND) {
|
if (cmd_return == CommandRet::NOT_FOUND) {
|
||||||
LOG_ERROR(F("No matching cmd (%s) in topic %s"), cmd_only, topic);
|
LOG_ERROR(F("No matching cmd (%s) in topic %s"), cmd_only, topic);
|
||||||
Mqtt::publish(F_(response), "unknown");
|
Mqtt::publish(F_(response), "unknown");
|
||||||
} else if (cmd_return == CommandRet::ERROR) {
|
} else if (cmd_return != CommandRet::OK) {
|
||||||
LOG_ERROR(F("Invalid data with cmd (%s) in topic %s"), cmd_only, topic);
|
LOG_ERROR(F("Invalid data with cmd (%s) in topic %s"), cmd_only, topic);
|
||||||
Mqtt::publish(F_(response), "unknown");
|
Mqtt::publish(F_(response), "unknown");
|
||||||
}
|
}
|
||||||
@@ -405,7 +405,7 @@ void Mqtt::on_message(const char * fulltopic, const char * payload, size_t len)
|
|||||||
if (cmd_return == CommandRet::NOT_FOUND) {
|
if (cmd_return == CommandRet::NOT_FOUND) {
|
||||||
LOG_ERROR(F("No matching cmd (%s)"), command);
|
LOG_ERROR(F("No matching cmd (%s)"), command);
|
||||||
Mqtt::publish(F_(response), "unknown");
|
Mqtt::publish(F_(response), "unknown");
|
||||||
} else if (cmd_return == CommandRet::ERROR) {
|
} else if (cmd_return != CommandRet::OK) {
|
||||||
LOG_ERROR(F("Invalid data for cmd (%s)"), command);
|
LOG_ERROR(F("Invalid data for cmd (%s)"), command);
|
||||||
Mqtt::publish(F_(response), "unknown");
|
Mqtt::publish(F_(response), "unknown");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -186,14 +186,14 @@ void WebAPIService::parse(AsyncWebServerRequest * request, std::string & device_
|
|||||||
delete response;
|
delete response;
|
||||||
send_message_response(request, 400, "Command not found"); // Bad Request
|
send_message_response(request, 400, "Command not found"); // Bad Request
|
||||||
return;
|
return;
|
||||||
} else if (cmd_reply == CommandRet::ERROR) {
|
|
||||||
delete response;
|
|
||||||
send_message_response(request, 400, "Problems parsing elements"); // Bad Request
|
|
||||||
return;
|
|
||||||
} else if (cmd_reply == CommandRet::NOT_ALLOWED) {
|
} else if (cmd_reply == CommandRet::NOT_ALLOWED) {
|
||||||
delete response;
|
delete response;
|
||||||
send_message_response(request, 401, "Bad credentials"); // Unauthorized
|
send_message_response(request, 401, "Bad credentials"); // Unauthorized
|
||||||
return;
|
return;
|
||||||
|
} else if (cmd_reply != CommandRet::OK) {
|
||||||
|
delete response;
|
||||||
|
send_message_response(request, 400, "Problems parsing elements"); // Bad Request
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!json.size()) {
|
if (!json.size()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user