From e2c29b09ab2db231b6cf09559efc9a60d41cbfb7 Mon Sep 17 00:00:00 2001 From: Iampete1 Date: Sun, 28 Aug 2022 20:24:21 +0100 Subject: [PATCH] AP_Scripting: add arming check for failed scripts --- libraries/AP_Scripting/AP_Scripting.cpp | 31 ++++++++++++++++++++++--- libraries/AP_Scripting/AP_Scripting.h | 4 +++- libraries/AP_Scripting/lua_scripts.cpp | 5 +--- libraries/AP_Scripting/lua_scripts.h | 3 +++ 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/libraries/AP_Scripting/AP_Scripting.cpp b/libraries/AP_Scripting/AP_Scripting.cpp index e27c5ae812..55686dd8d8 100644 --- a/libraries/AP_Scripting/AP_Scripting.cpp +++ b/libraries/AP_Scripting/AP_Scripting.cpp @@ -154,9 +154,8 @@ void AP_Scripting::init(void) { if (!hal.scheduler->thread_create(FUNCTOR_BIND_MEMBER(&AP_Scripting::thread, void), "Scripting", SCRIPTING_STACK_SIZE, AP_HAL::Scheduler::PRIORITY_SCRIPTING, 0)) { - gcs().send_text(MAV_SEVERITY_CRITICAL, "Could not create scripting stack (%d)", SCRIPTING_STACK_SIZE); - gcs().send_text(MAV_SEVERITY_ERROR, "Scripting failed to start"); - _init_failed = true; + gcs().send_text(MAV_SEVERITY_ERROR, "Scripting: %s", "failed to start"); + _thread_failed = true; } } @@ -222,6 +221,7 @@ void AP_Scripting::thread(void) { // reset flags _stop = false; _restart = false; + _init_failed = false; lua_scripts *lua = new lua_scripts(_script_vm_exec_count, _script_heap_size, _debug_options, terminal); if (lua == nullptr || !lua->heap_allocated()) { @@ -281,6 +281,31 @@ void AP_Scripting::handle_mission_command(const AP_Mission::Mission_Command& cmd mission_data->push(cmd); } +bool AP_Scripting::arming_checks(size_t buflen, char *buffer) const +{ + if (!enabled()) { + return true; + } + + if (_thread_failed) { + hal.util->snprintf(buffer, buflen, "Scripting: %s", "failed to start"); + return false; + } + + if (_init_failed) { + hal.util->snprintf(buffer, buflen, "Scripting: %s", "out of memory"); + return false; + } + + const char *error_buf = lua_scripts::get_last_error_message(); + if (error_buf != nullptr) { + hal.util->snprintf(buffer, buflen, "Scripting: %s", error_buf); + return false; + } + + return true; +} + AP_Scripting *AP_Scripting::_singleton = nullptr; namespace AP { diff --git a/libraries/AP_Scripting/AP_Scripting.h b/libraries/AP_Scripting/AP_Scripting.h index c663146699..1befe3b3ac 100644 --- a/libraries/AP_Scripting/AP_Scripting.h +++ b/libraries/AP_Scripting/AP_Scripting.h @@ -38,7 +38,6 @@ public: AP_Scripting &operator=(const AP_Scripting&) = delete; void init(void); - bool init_failed(void) const { return _init_failed; } bool enabled(void) const { return _enable != 0; }; bool should_run(void) const { return enabled() && !_stop; } @@ -51,6 +50,8 @@ public: void handle_mission_command(const class AP_Mission::Mission_Command& cmd); + bool arming_checks(size_t buflen, char *buffer) const; + // User parameters for inputs into scripts AP_Float _user[6]; @@ -102,6 +103,7 @@ private: AP_Int8 _debug_options; AP_Int16 _dir_disable; + bool _thread_failed; // thread allocation failed bool _init_failed; // true if memory allocation failed bool _restart; // true if scripts should be restarted bool _stop; // true if scripts should be stopped diff --git a/libraries/AP_Scripting/lua_scripts.cpp b/libraries/AP_Scripting/lua_scripts.cpp index 0ab44a76dc..969c8b05b5 100644 --- a/libraries/AP_Scripting/lua_scripts.cpp +++ b/libraries/AP_Scripting/lua_scripts.cpp @@ -531,12 +531,9 @@ void lua_scripts::run(void) { // re-print the latest error message every 10 seconds 10 times const uint8_t error_prints = 10; if ((print_error_count < error_prints) && (AP_HAL::millis() - last_print_ms > 10000)) { + // note that we do not clear the buffer after we have finished printing, this allows it to be used for a pre-arm check print_error(MAV_SEVERITY_DEBUG); print_error_count++; - if ((print_error_count >= error_prints) && (error_msg_buf != nullptr)) { - hal.util->heap_realloc(_heap, error_msg_buf, 0); - error_msg_buf = nullptr; - } } } diff --git a/libraries/AP_Scripting/lua_scripts.h b/libraries/AP_Scripting/lua_scripts.h index f8e39bd496..6238a6cc8f 100644 --- a/libraries/AP_Scripting/lua_scripts.h +++ b/libraries/AP_Scripting/lua_scripts.h @@ -141,4 +141,7 @@ public: // must be static for use in atpanic, public to allow bindings to issue none fatal warnings static void set_and_print_new_error_message(MAV_SEVERITY severity, const char *fmt, ...) FMT_PRINTF(2,3); + // return last error message, nullptr if none + static const char* get_last_error_message() { return error_msg_buf; } + };