diff --git a/src/command.h b/src/command.h index daee30e80..c68a6fd7b 100644 --- a/src/command.h +++ b/src/command.h @@ -45,6 +45,16 @@ enum CommandFlag : uint8_t { }; +// returns 0 if the command errored, 1 (TRUE) if ok, 2 if not found, 3 if error or 4 if not allowed +enum CommandRet: uint8_t { + ERRORED = 0, + OK, + NOT_FOUND, + ERROR, + NOT_ALLOWED + +}; + using cmdfunction_p = std::function; using cmdfunction_json_p = std::function; diff --git a/src/console.cpp b/src/console.cpp index cc654e63e..cd35f8aa7 100644 --- a/src/console.cpp +++ b/src/console.cpp @@ -385,7 +385,7 @@ void EMSESPShell::add_console_commands() { const char * cmd = arguments[1].c_str(); - uint8_t cmd_return = 1; // OK + uint8_t cmd_return = CommandRet::OK; if (arguments.size() == 2) { // no value specified, just the cmd @@ -403,17 +403,17 @@ void EMSESPShell::add_console_commands() { cmd_return = Command::call(device_type, cmd, arguments[2].c_str(), true, atoi(arguments[3].c_str()), json); } - if (cmd_return == 1 && json.size()) { + if (cmd_return == CommandRet::OK && json.size()) { serializeJsonPretty(doc, shell); shell.println(); return; } - if (cmd_return == 2) { + if (cmd_return == CommandRet::NOT_FOUND) { shell.println(F("Unknown command")); shell.print(F("Available commands are: ")); Command::show(shell, device_type, false); // non-verbose mode - } else if (cmd_return == 3) { + } else if (cmd_return == CommandRet::ERROR) { shell.println(F("Bad syntax")); } }, diff --git a/src/mqtt.cpp b/src/mqtt.cpp index da307095f..238b6f1ac 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -348,10 +348,10 @@ void Mqtt::on_message(const char * fulltopic, const char * payload, size_t len) // LOG_INFO(F("devicetype= %d, topic = %s, cmd = %s, message = %s), mf.device_type_, topic, cmd_only, message); // call command, assume admin authentication is allowed uint8_t cmd_return = Command::call(mf.device_type_, cmd_only, message, true); - if (cmd_return == 2) { + if (cmd_return == CommandRet::NOT_FOUND) { LOG_ERROR(F("No matching cmd (%s) in topic %s"), cmd_only, topic); Mqtt::publish(F_(response), "unknown"); - } else if (cmd_return == 3) { + } else if (cmd_return == CommandRet::ERROR) { LOG_ERROR(F("Invalid data with cmd (%s) in topic %s"), cmd_only, topic); Mqtt::publish(F_(response), "unknown"); } @@ -381,7 +381,7 @@ void Mqtt::on_message(const char * fulltopic, const char * payload, size_t len) n = doc["id"]; } - uint8_t cmd_return = 1; // OK + uint8_t cmd_return = CommandRet::OK; JsonVariant data = doc["data"]; if (data.is()) { @@ -402,10 +402,10 @@ void Mqtt::on_message(const char * fulltopic, const char * payload, size_t len) } } - if (cmd_return == 2) { + if (cmd_return == CommandRet::NOT_FOUND) { LOG_ERROR(F("No matching cmd (%s)"), command); Mqtt::publish(F_(response), "unknown"); - } else if (cmd_return == 3) { + } else if (cmd_return == CommandRet::ERROR) { LOG_ERROR(F("Invalid data for cmd (%s)"), command); Mqtt::publish(F_(response), "unknown"); } diff --git a/src/web/WebAPIService.cpp b/src/web/WebAPIService.cpp index 20f06110f..8fd60f05b 100644 --- a/src/web/WebAPIService.cpp +++ b/src/web/WebAPIService.cpp @@ -182,15 +182,15 @@ void WebAPIService::parse(AsyncWebServerRequest * request, std::string & device_ uint8_t cmd_reply = Command::call(device_type, cmd_s.c_str(), (have_data ? value_s.c_str() : nullptr), authenticated, id_n, json); // check for errors - if (cmd_reply == 2) { + if (cmd_reply == CommandRet::NOT_FOUND) { delete response; send_message_response(request, 400, "Command not found"); // Bad Request return; - } else if (cmd_reply == 3) { + } else if (cmd_reply == CommandRet::ERROR) { delete response; send_message_response(request, 400, "Problems parsing elements"); // Bad Request return; - } else if (cmd_reply == 4) { + } else if (cmd_reply == CommandRet::NOT_ALLOWED) { delete response; send_message_response(request, 401, "Bad credentials"); // Unauthorized return; diff --git a/src/web/WebDataService.cpp b/src/web/WebDataService.cpp index 1bf492944..631bc8984 100644 --- a/src/web/WebDataService.cpp +++ b/src/web/WebDataService.cpp @@ -135,7 +135,7 @@ void WebDataService::write_value(AsyncWebServerRequest * request, JsonVariant & if (emsdevice->unique_id() == id) { const char * cmd = dv["c"]; uint8_t device_type = emsdevice->device_type(); - uint8_t cmd_return = 1; // OK + uint8_t cmd_return = CommandRet::OK; char s[10]; // the data could be in any format, but we need string JsonVariant data = dv["v"]; @@ -150,7 +150,7 @@ void WebDataService::write_value(AsyncWebServerRequest * request, JsonVariant & } // send "Write command sent to device" or "Write command failed" - AsyncWebServerResponse * response = request->beginResponse((cmd_return == 1) ? 200 : 204); + AsyncWebServerResponse * response = request->beginResponse((cmd_return == CommandRet::OK) ? 200 : 204); request->send(response); return; }