AP_Scripting: add error message buffer semaphore

This commit is contained in:
Iampete1 2022-08-30 21:47:36 +01:00 committed by Andrew Tridgell
parent e3931a6f56
commit da5ec973c5
3 changed files with 21 additions and 1 deletions

View File

@ -297,11 +297,14 @@ bool AP_Scripting::arming_checks(size_t buflen, char *buffer) const
return false;
}
lua_scripts::get_last_error_semaphore()->take_blocking();
const char *error_buf = lua_scripts::get_last_error_message();
if (error_buf != nullptr) {
hal.util->snprintf(buffer, buflen, "Scripting: %s", error_buf);
lua_scripts::get_last_error_semaphore()->give();
return false;
}
lua_scripts::get_last_error_semaphore()->give();
return true;
}

View File

@ -27,6 +27,7 @@ extern const AP_HAL::HAL& hal;
bool lua_scripts::overtime;
jmp_buf lua_scripts::panic_jmp;
char *lua_scripts::error_msg_buf;
HAL_Semaphore lua_scripts::error_msg_buf_sem;
uint8_t lua_scripts::print_error_count;
uint32_t lua_scripts::last_print_ms;
@ -52,14 +53,19 @@ void lua_scripts::hook(lua_State *L, lua_Debug *ar) {
}
void lua_scripts::print_error(MAV_SEVERITY severity) {
error_msg_buf_sem.take_blocking();
if (error_msg_buf == nullptr) {
error_msg_buf_sem.give();
return;
}
last_print_ms = AP_HAL::millis();
GCS_SEND_TEXT(severity, "Lua: %s", error_msg_buf);
error_msg_buf_sem.give();
}
void lua_scripts::set_and_print_new_error_message(MAV_SEVERITY severity, const char *fmt, ...) {
error_msg_buf_sem.take_blocking();
// reset buffer and print count
print_error_count = 0;
if (error_msg_buf) {
@ -81,6 +87,7 @@ void lua_scripts::set_and_print_new_error_message(MAV_SEVERITY severity, const c
if (len <= 0) {
// nothing to print, something has gone wrong
va_end(arg_list);
error_msg_buf_sem.give();
return;
}
@ -89,6 +96,7 @@ void lua_scripts::set_and_print_new_error_message(MAV_SEVERITY severity, const c
if (!error_msg_buf) {
// allocation failed
va_end(arg_list);
error_msg_buf_sem.give();
return;
}
@ -98,6 +106,8 @@ void lua_scripts::set_and_print_new_error_message(MAV_SEVERITY severity, const c
// print to cosole and GCS
DEV_PRINTF("Lua: %s\n", error_msg_buf);
error_msg_buf_sem.give();
print_error(severity);
}
@ -547,8 +557,10 @@ void lua_scripts::run(void) {
lua_state = nullptr;
}
error_msg_buf_sem.take_blocking();
if (error_msg_buf != nullptr) {
hal.util->heap_realloc(_heap, error_msg_buf, 0);
error_msg_buf = nullptr;
}
error_msg_buf_sem.give();
}

View File

@ -21,6 +21,7 @@
#include <AP_Filesystem/posix_compat.h>
#include <AP_Scripting/AP_Scripting.h>
#include <GCS_MAVLink/GCS.h>
#include <AP_HAL/Semaphores.h>
#include "lua/src/lua.hpp"
@ -135,6 +136,7 @@ private:
// must be static for use in atpanic
static void print_error(MAV_SEVERITY severity);
static char *error_msg_buf;
static HAL_Semaphore error_msg_buf_sem;
static uint8_t print_error_count;
static uint32_t last_print_ms;
@ -142,7 +144,10 @@ 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
// return last error message, nullptr if none, must use semaphore as this is updated in the scripting thread
static const char* get_last_error_message() { return error_msg_buf; }
// get semaphore for above error buffer
static AP_HAL::Semaphore* get_last_error_semaphore() { return &error_msg_buf_sem; }
};