2018-10-31 19:43:23 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2023-06-07 02:56:50 -03:00
|
|
|
#include "AP_Scripting_config.h"
|
|
|
|
|
|
|
|
#if AP_SCRIPTING_ENABLED
|
|
|
|
|
2018-10-31 19:43:23 -03:00
|
|
|
#include <AP_Common/AP_Common.h>
|
|
|
|
#include <AP_Param/AP_Param.h>
|
2018-11-14 19:44:15 -04:00
|
|
|
#include <setjmp.h>
|
2018-10-31 19:43:23 -03:00
|
|
|
|
2019-08-01 07:55:03 -03:00
|
|
|
#include <AP_Filesystem/posix_compat.h>
|
2020-02-05 18:58:33 -04:00
|
|
|
#include <AP_Scripting/AP_Scripting.h>
|
2022-11-06 21:24:49 -04:00
|
|
|
#include <GCS_MAVLink/GCS_MAVLink.h>
|
2022-08-30 17:47:36 -03:00
|
|
|
#include <AP_HAL/Semaphores.h>
|
2022-12-21 18:36:11 -04:00
|
|
|
#include <AP_Common/MultiHeap.h>
|
2023-03-09 00:47:50 -04:00
|
|
|
#include "lua_common_defs.h"
|
2020-02-05 18:58:33 -04:00
|
|
|
|
2022-04-30 13:17:40 -03:00
|
|
|
#include "lua/src/lua.hpp"
|
|
|
|
|
2018-10-31 19:43:23 -03:00
|
|
|
class lua_scripts
|
|
|
|
{
|
|
|
|
public:
|
2024-05-23 18:00:45 -03:00
|
|
|
lua_scripts(const AP_Int32 &vm_steps, const AP_Int32 &heap_size, const AP_Int8 &debug_options);
|
2018-10-31 19:43:23 -03:00
|
|
|
|
2022-04-19 20:34:23 -03:00
|
|
|
~lua_scripts();
|
|
|
|
|
|
|
|
CLASS_NO_COPY(lua_scripts);
|
2018-10-31 19:43:23 -03:00
|
|
|
|
2019-11-26 01:33:14 -04:00
|
|
|
// return true if initialisation failed
|
2022-12-21 18:36:11 -04:00
|
|
|
bool heap_allocated() const { return _heap.available(); }
|
2019-11-26 01:33:14 -04:00
|
|
|
|
2018-10-31 19:43:23 -03:00
|
|
|
// run scripts, does not return unless an error occured
|
|
|
|
void run(void);
|
|
|
|
|
|
|
|
static bool overtime; // script exceeded it's execution slot, and we are bailing out
|
2021-11-13 13:09:09 -04:00
|
|
|
|
|
|
|
enum class DebugLevel {
|
|
|
|
NO_SCRIPTS_TO_RUN = 1U << 0,
|
|
|
|
RUNTIME_MSG = 1U << 1,
|
2021-11-13 13:42:49 -04:00
|
|
|
SUPPRESS_SCRIPT_LOG = 1U << 2,
|
|
|
|
LOG_RUNTIME = 1U << 3,
|
2022-08-28 16:31:20 -03:00
|
|
|
DISABLE_PRE_ARM = 1U << 4,
|
2023-11-25 13:18:37 -04:00
|
|
|
SAVE_CHECKSUM = 1U << 5,
|
2021-11-13 13:09:09 -04:00
|
|
|
};
|
|
|
|
|
2018-10-31 19:43:23 -03:00
|
|
|
private:
|
|
|
|
|
2020-02-05 18:58:33 -04:00
|
|
|
void create_sandbox(lua_State *L);
|
|
|
|
|
2018-10-31 19:43:23 -03:00
|
|
|
typedef struct script_info {
|
|
|
|
int lua_ref; // reference to the loaded script object
|
|
|
|
uint64_t next_run_ms; // time (in milliseconds) the script should next be run at
|
2023-11-25 13:18:37 -04:00
|
|
|
uint32_t crc; // crc32 checksum
|
2018-11-01 01:08:37 -03:00
|
|
|
char *name; // filename for the script // FIXME: This information should be available from Lua
|
2018-10-31 19:43:23 -03:00
|
|
|
script_info *next;
|
|
|
|
} script_info;
|
|
|
|
|
2018-11-01 01:08:37 -03:00
|
|
|
script_info *load_script(lua_State *L, char *filename);
|
|
|
|
|
2020-02-05 18:58:33 -04:00
|
|
|
void reset_loop_overtime(lua_State *L);
|
|
|
|
|
2018-11-01 01:08:37 -03:00
|
|
|
void load_all_scripts_in_dir(lua_State *L, const char *dirname);
|
2018-10-31 19:43:23 -03:00
|
|
|
|
|
|
|
void run_next_script(lua_State *L);
|
|
|
|
|
|
|
|
void remove_script(lua_State *L, script_info *script);
|
|
|
|
|
|
|
|
// reschedule the script for execution. It is assumed the script is not in the list already
|
|
|
|
void reschedule_script(script_info *script);
|
|
|
|
|
|
|
|
script_info *scripts; // linked list of scripts to be run, sorted by next run time (soonest first)
|
|
|
|
|
|
|
|
// hook will be run when CPU time for a script is exceeded
|
|
|
|
// it must be static to be passed to the C API
|
|
|
|
static void hook(lua_State *L, lua_Debug *ar);
|
|
|
|
|
2018-11-14 19:44:15 -04:00
|
|
|
// lua panic handler, will jump back to the start of run
|
|
|
|
static int atpanic(lua_State *L);
|
|
|
|
static jmp_buf panic_jmp;
|
|
|
|
|
|
|
|
lua_State *lua_state;
|
|
|
|
|
2018-10-31 19:43:23 -03:00
|
|
|
const AP_Int32 & _vm_steps;
|
2021-11-13 13:09:09 -04:00
|
|
|
const AP_Int8 & _debug_options;
|
2018-10-31 19:43:23 -03:00
|
|
|
|
2018-12-08 22:35:08 -04:00
|
|
|
static void *alloc(void *ud, void *ptr, size_t osize, size_t nsize);
|
|
|
|
|
2022-12-21 18:36:11 -04:00
|
|
|
static MultiHeap _heap;
|
2021-08-25 21:29:41 -03:00
|
|
|
|
2022-10-30 21:16:57 -03:00
|
|
|
// helper for print and log of runtime stats
|
|
|
|
void update_stats(const char *name, uint32_t run_time, int total_mem, int run_mem);
|
|
|
|
|
2021-08-25 21:29:41 -03:00
|
|
|
// must be static for use in atpanic
|
|
|
|
static void print_error(MAV_SEVERITY severity);
|
|
|
|
static char *error_msg_buf;
|
2022-08-30 17:47:36 -03:00
|
|
|
static HAL_Semaphore error_msg_buf_sem;
|
2021-08-25 21:29:41 -03:00
|
|
|
static uint8_t print_error_count;
|
|
|
|
static uint32_t last_print_ms;
|
2023-03-09 00:47:50 -04:00
|
|
|
int current_ref;
|
2022-04-26 14:38:02 -03:00
|
|
|
|
2023-11-25 13:18:37 -04:00
|
|
|
// XOR of crc32 of running scripts
|
|
|
|
static uint32_t loaded_checksum;
|
|
|
|
static uint32_t running_checksum;
|
|
|
|
static HAL_Semaphore crc_sem;
|
|
|
|
|
2022-04-26 14:38:02 -03:00
|
|
|
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);
|
|
|
|
|
2022-08-30 17:47:36 -03:00
|
|
|
// return last error message, nullptr if none, must use semaphore as this is updated in the scripting thread
|
2022-08-28 16:24:21 -03:00
|
|
|
static const char* get_last_error_message() { return error_msg_buf; }
|
|
|
|
|
2022-08-30 17:47:36 -03:00
|
|
|
// get semaphore for above error buffer
|
|
|
|
static AP_HAL::Semaphore* get_last_error_semaphore() { return &error_msg_buf_sem; }
|
|
|
|
|
2023-11-25 13:18:37 -04:00
|
|
|
// Return the file checksums of running and loaded scripts
|
|
|
|
static uint32_t get_loaded_checksum();
|
|
|
|
static uint32_t get_running_checksum();
|
|
|
|
|
2018-10-31 19:43:23 -03:00
|
|
|
};
|
2023-06-07 02:56:50 -03:00
|
|
|
|
|
|
|
#endif // AP_SCRIPTING_ENABLED
|