2018-09-27 19:30:55 -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/>.
|
|
|
|
*/
|
|
|
|
|
2023-06-07 02:56:50 -03:00
|
|
|
#include "AP_Scripting_config.h"
|
|
|
|
|
|
|
|
#if AP_SCRIPTING_ENABLED
|
|
|
|
|
2018-09-27 19:30:55 -03:00
|
|
|
#include <AP_Scripting/AP_Scripting.h>
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2018-09-27 20:04:37 -03:00
|
|
|
#include <GCS_MAVLink/GCS.h>
|
|
|
|
|
2018-10-31 19:43:23 -03:00
|
|
|
#include "lua_scripts.h"
|
2018-10-03 17:37:43 -03:00
|
|
|
|
2018-09-27 20:04:37 -03:00
|
|
|
// ensure that we have a set of stack sizes, and enforce constraints around it
|
|
|
|
// except for the minimum size, these are allowed to be defined by the build system
|
|
|
|
#undef SCRIPTING_STACK_MIN_SIZE
|
2018-10-31 19:43:23 -03:00
|
|
|
#define SCRIPTING_STACK_MIN_SIZE (8 * 1024)
|
2018-09-27 20:04:37 -03:00
|
|
|
|
|
|
|
#if !defined(SCRIPTING_STACK_SIZE)
|
2019-07-17 01:27:25 -03:00
|
|
|
#define SCRIPTING_STACK_SIZE (17 * 1024) // Linux experiences stack corruption at ~16.25KB when handed bad scripts
|
2018-09-27 20:04:37 -03:00
|
|
|
#endif // !defined(SCRIPTING_STACK_SIZE)
|
|
|
|
|
|
|
|
#if !defined(SCRIPTING_STACK_MAX_SIZE)
|
2018-10-31 19:43:23 -03:00
|
|
|
#define SCRIPTING_STACK_MAX_SIZE (64 * 1024)
|
2018-09-27 20:04:37 -03:00
|
|
|
#endif // !defined(SCRIPTING_STACK_MAX_SIZE)
|
|
|
|
|
2020-01-08 18:10:18 -04:00
|
|
|
#if !defined(SCRIPTING_HEAP_SIZE)
|
2023-03-07 10:45:02 -04:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL || CONFIG_HAL_BOARD == HAL_BOARD_LINUX || HAL_MEM_CLASS >= HAL_MEM_CLASS_1000
|
|
|
|
#define SCRIPTING_HEAP_SIZE (200 * 1024)
|
|
|
|
#elif HAL_MEM_CLASS >= HAL_MEM_CLASS_500
|
2022-04-10 23:59:49 -03:00
|
|
|
#define SCRIPTING_HEAP_SIZE (100 * 1024)
|
2020-01-08 18:10:18 -04:00
|
|
|
#else
|
|
|
|
#define SCRIPTING_HEAP_SIZE (43 * 1024)
|
|
|
|
#endif
|
|
|
|
#endif // !defined(SCRIPTING_HEAP_SIZE)
|
|
|
|
|
2018-09-27 20:04:37 -03:00
|
|
|
static_assert(SCRIPTING_STACK_SIZE >= SCRIPTING_STACK_MIN_SIZE, "Scripting requires a larger minimum stack size");
|
|
|
|
static_assert(SCRIPTING_STACK_SIZE <= SCRIPTING_STACK_MAX_SIZE, "Scripting requires a smaller stack size");
|
|
|
|
|
2021-06-14 13:01:09 -03:00
|
|
|
#ifndef SCRIPTING_ENABLE_DEFAULT
|
|
|
|
#define SCRIPTING_ENABLE_DEFAULT 0
|
|
|
|
#endif
|
|
|
|
|
2023-12-03 01:43:29 -04:00
|
|
|
#if AP_NETWORKING_ENABLED
|
|
|
|
#include <AP_HAL/utility/Socket.h>
|
|
|
|
#endif
|
|
|
|
|
2018-09-27 20:04:37 -03:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
const AP_Param::GroupInfo AP_Scripting::var_info[] = {
|
|
|
|
// @Param: ENABLE
|
|
|
|
// @DisplayName: Enable Scripting
|
|
|
|
// @Description: Controls if scripting is enabled
|
|
|
|
// @Values: 0:None,1:Lua Scripts
|
|
|
|
// @RebootRequired: True
|
|
|
|
// @User: Advanced
|
2021-06-14 13:01:09 -03:00
|
|
|
AP_GROUPINFO_FLAGS("ENABLE", 1, AP_Scripting, _enable, SCRIPTING_ENABLE_DEFAULT, AP_PARAM_FLAG_ENABLE),
|
2018-09-27 20:04:37 -03:00
|
|
|
|
2018-10-30 23:06:32 -03:00
|
|
|
// @Param: VM_I_COUNT
|
|
|
|
// @DisplayName: Scripting Virtual Machine Instruction Count
|
|
|
|
// @Description: The number virtual machine instructions that can be run before considering a script to have taken an excessive amount of time
|
|
|
|
// @Range: 1000 1000000
|
|
|
|
// @Increment: 10000
|
|
|
|
// @User: Advanced
|
|
|
|
AP_GROUPINFO("VM_I_COUNT", 2, AP_Scripting, _script_vm_exec_count, 10000),
|
|
|
|
|
2018-12-08 22:35:08 -04:00
|
|
|
// @Param: HEAP_SIZE
|
|
|
|
// @DisplayName: Scripting Heap Size
|
|
|
|
// @Description: Amount of memory available for scripting
|
|
|
|
// @Range: 1024 1048576
|
|
|
|
// @Increment: 1024
|
|
|
|
// @User: Advanced
|
|
|
|
// @RebootRequired: True
|
2020-01-08 18:10:18 -04:00
|
|
|
AP_GROUPINFO("HEAP_SIZE", 3, AP_Scripting, _script_heap_size, SCRIPTING_HEAP_SIZE),
|
2018-12-08 22:35:08 -04:00
|
|
|
|
2021-11-13 13:09:09 -04:00
|
|
|
// @Param: DEBUG_OPTS
|
2020-02-18 21:10:24 -04:00
|
|
|
// @DisplayName: Scripting Debug Level
|
2021-11-13 13:09:09 -04:00
|
|
|
// @Description: Debugging options
|
2023-11-25 13:18:37 -04:00
|
|
|
// @Bitmask: 0: No Scripts to run message if all scripts have stopped
|
|
|
|
// @Bitmask: 1: Runtime messages for memory usage and execution time
|
|
|
|
// @Bitmask: 2: Suppress logging scripts to dataflash
|
|
|
|
// @Bitmask: 3: log runtime memory usage and execution time
|
|
|
|
// @Bitmask: 4: Disable pre-arm check
|
|
|
|
// @Bitmask: 5: Save CRC of current scripts to loaded and running checksum parameters enabling pre-arm
|
2020-02-17 22:39:17 -04:00
|
|
|
// @User: Advanced
|
2021-11-13 13:09:09 -04:00
|
|
|
AP_GROUPINFO("DEBUG_OPTS", 4, AP_Scripting, _debug_options, 0),
|
2019-03-22 12:19:59 -03:00
|
|
|
|
2020-07-09 09:36:45 -03:00
|
|
|
// @Param: USER1
|
|
|
|
// @DisplayName: Scripting User Parameter1
|
|
|
|
// @Description: General purpose user variable input for scripts
|
|
|
|
// @User: Standard
|
|
|
|
AP_GROUPINFO("USER1", 5, AP_Scripting, _user[0], 0.0),
|
|
|
|
|
|
|
|
// @Param: USER2
|
|
|
|
// @DisplayName: Scripting User Parameter2
|
|
|
|
// @Description: General purpose user variable input for scripts
|
|
|
|
// @User: Standard
|
|
|
|
AP_GROUPINFO("USER2", 6, AP_Scripting, _user[1], 0.0),
|
|
|
|
|
|
|
|
// @Param: USER3
|
|
|
|
// @DisplayName: Scripting User Parameter3
|
|
|
|
// @Description: General purpose user variable input for scripts
|
|
|
|
// @User: Standard
|
|
|
|
AP_GROUPINFO("USER3", 7, AP_Scripting, _user[2], 0.0),
|
|
|
|
|
|
|
|
// @Param: USER4
|
|
|
|
// @DisplayName: Scripting User Parameter4
|
|
|
|
// @Description: General purpose user variable input for scripts
|
|
|
|
// @User: Standard
|
|
|
|
AP_GROUPINFO("USER4", 8, AP_Scripting, _user[3], 0.0),
|
|
|
|
|
2021-11-30 02:09:20 -04:00
|
|
|
// @Param: USER5
|
|
|
|
// @DisplayName: Scripting User Parameter5
|
|
|
|
// @Description: General purpose user variable input for scripts
|
|
|
|
// @User: Standard
|
|
|
|
AP_GROUPINFO("USER5", 10, AP_Scripting, _user[4], 0.0),
|
|
|
|
|
|
|
|
// @Param: USER6
|
|
|
|
// @DisplayName: Scripting User Parameter6
|
|
|
|
// @Description: General purpose user variable input for scripts
|
|
|
|
// @User: Standard
|
|
|
|
AP_GROUPINFO("USER6", 11, AP_Scripting, _user[5], 0.0),
|
|
|
|
|
2020-10-26 22:08:14 -03:00
|
|
|
// @Param: DIR_DISABLE
|
|
|
|
// @DisplayName: Directory disable
|
|
|
|
// @Description: This will stop scripts being loaded from the given locations
|
|
|
|
// @Bitmask: 0:ROMFS, 1:APM/scripts
|
|
|
|
// @RebootRequired: True
|
|
|
|
// @User: Advanced
|
|
|
|
AP_GROUPINFO("DIR_DISABLE", 9, AP_Scripting, _dir_disable, 0),
|
|
|
|
|
2023-11-25 13:18:37 -04:00
|
|
|
// @Param: LD_CHECKSUM
|
|
|
|
// @DisplayName: Loaded script checksum
|
|
|
|
// @Description: Required XOR of CRC32 checksum of loaded scripts, vehicle will not arm with incorrect scripts loaded, -1 disables
|
|
|
|
// @User: Advanced
|
|
|
|
AP_GROUPINFO("LD_CHECKSUM", 12, AP_Scripting, _required_loaded_checksum, -1),
|
|
|
|
|
|
|
|
// @Param: RUN_CHECKSUM
|
|
|
|
// @DisplayName: Running script checksum
|
|
|
|
// @Description: Required XOR of CRC32 checksum of running scripts, vehicle will not arm with incorrect scripts running, -1 disables
|
|
|
|
// @User: Advanced
|
|
|
|
AP_GROUPINFO("RUN_CHECKSUM", 13, AP_Scripting, _required_running_checksum, -1),
|
|
|
|
|
2018-09-27 20:04:37 -03:00
|
|
|
AP_GROUPEND
|
|
|
|
};
|
2018-09-27 19:30:55 -03:00
|
|
|
|
2018-10-31 19:43:23 -03:00
|
|
|
AP_Scripting::AP_Scripting() {
|
2018-09-27 20:04:37 -03:00
|
|
|
AP_Param::setup_object_defaults(this, var_info);
|
|
|
|
|
2018-09-27 19:30:55 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
|
|
|
if (_singleton != nullptr) {
|
|
|
|
AP_HAL::panic("Scripting must be a singleton");
|
|
|
|
}
|
|
|
|
#endif // CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
|
|
|
_singleton = this;
|
|
|
|
}
|
|
|
|
|
2019-11-26 01:39:17 -04:00
|
|
|
void AP_Scripting::init(void) {
|
2018-09-27 20:04:37 -03:00
|
|
|
if (!_enable) {
|
2019-11-26 01:39:17 -04:00
|
|
|
return;
|
2018-09-27 20:04:37 -03:00
|
|
|
}
|
2018-09-27 19:30:55 -03:00
|
|
|
|
2020-04-17 15:46:27 -03:00
|
|
|
const char *dir_name = SCRIPTING_DIRECTORY;
|
2020-06-20 09:35:38 -03:00
|
|
|
if (AP::FS().mkdir(dir_name)) {
|
|
|
|
if (errno != EEXIST) {
|
2023-09-21 22:24:30 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "Scripting: failed to create (%s)", dir_name);
|
2020-06-20 09:35:38 -03:00
|
|
|
}
|
2020-04-17 15:46:27 -03:00
|
|
|
}
|
|
|
|
|
2018-09-27 20:04:37 -03:00
|
|
|
if (!hal.scheduler->thread_create(FUNCTOR_BIND_MEMBER(&AP_Scripting::thread, void),
|
|
|
|
"Scripting", SCRIPTING_STACK_SIZE, AP_HAL::Scheduler::PRIORITY_SCRIPTING, 0)) {
|
2023-09-21 22:24:30 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_ERROR, "Scripting: %s", "failed to start");
|
2022-08-28 16:24:21 -03:00
|
|
|
_thread_failed = true;
|
2018-09-27 20:04:37 -03:00
|
|
|
}
|
|
|
|
}
|
2018-09-27 19:30:55 -03:00
|
|
|
|
2023-09-21 22:24:30 -03:00
|
|
|
#if HAL_GCS_ENABLED
|
2020-02-05 18:58:33 -04:00
|
|
|
MAV_RESULT AP_Scripting::handle_command_int_packet(const mavlink_command_int_t &packet) {
|
|
|
|
switch ((SCRIPTING_CMD)packet.param1) {
|
|
|
|
case SCRIPTING_CMD_REPL_START:
|
|
|
|
return repl_start() ? MAV_RESULT_ACCEPTED : MAV_RESULT_FAILED;
|
|
|
|
case SCRIPTING_CMD_REPL_STOP:
|
|
|
|
repl_stop();
|
|
|
|
return MAV_RESULT_ACCEPTED;
|
2021-08-04 13:49:17 -03:00
|
|
|
case SCRIPTING_CMD_STOP:
|
|
|
|
_restart = false;
|
|
|
|
_stop = true;
|
|
|
|
return MAV_RESULT_ACCEPTED;
|
|
|
|
case SCRIPTING_CMD_STOP_AND_RESTART:
|
|
|
|
_restart = true;
|
|
|
|
_stop = true;
|
|
|
|
return MAV_RESULT_ACCEPTED;
|
2020-02-05 18:58:33 -04:00
|
|
|
case SCRIPTING_CMD_ENUM_END: // cope with MAVLink generator appending to our enum
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MAV_RESULT_UNSUPPORTED;
|
|
|
|
}
|
2023-09-21 22:24:30 -03:00
|
|
|
#endif
|
2020-02-05 18:58:33 -04:00
|
|
|
|
|
|
|
bool AP_Scripting::repl_start(void) {
|
|
|
|
if (terminal.session) { // it's already running, this is fine
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nuke the old folder and all contents
|
|
|
|
struct stat st;
|
|
|
|
if ((AP::FS().stat(REPL_DIRECTORY, &st) == -1) &&
|
|
|
|
(AP::FS().unlink(REPL_DIRECTORY) == -1) &&
|
|
|
|
(errno != EEXIST)) {
|
2023-09-21 22:24:30 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "Scripting: Unable to delete old REPL %s", strerror(errno));
|
2020-02-05 18:58:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// create a new folder
|
|
|
|
AP::FS().mkdir(REPL_DIRECTORY);
|
|
|
|
// delete old files in case we couldn't
|
|
|
|
AP::FS().unlink(REPL_DIRECTORY "/in");
|
|
|
|
AP::FS().unlink(REPL_DIRECTORY "/out");
|
|
|
|
|
|
|
|
// make the output pointer
|
|
|
|
terminal.output_fd = AP::FS().open(REPL_OUT, O_WRONLY|O_CREAT|O_TRUNC);
|
|
|
|
if (terminal.output_fd == -1) {
|
2023-09-21 22:24:30 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "Scripting: %s", "Unable to make new REPL");
|
2020-02-05 18:58:33 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
terminal.session = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AP_Scripting::repl_stop(void) {
|
|
|
|
terminal.session = false;
|
|
|
|
// can't do any more cleanup here, closing the open FD's is the REPL's responsibility
|
|
|
|
}
|
|
|
|
|
2023-07-05 11:54:52 -03:00
|
|
|
/*
|
|
|
|
avoid optimisation of the thread function. This avoids nasty traps
|
|
|
|
where setjmp/longjmp does not properly handle save/restore of
|
|
|
|
floating point registers on exceptions. This is an extra protection
|
|
|
|
over the top of the fix in luaD_rawrunprotected() for the same issue
|
|
|
|
*/
|
|
|
|
#pragma GCC push_options
|
|
|
|
#pragma GCC optimize ("O0")
|
|
|
|
|
2018-09-27 20:04:37 -03:00
|
|
|
void AP_Scripting::thread(void) {
|
2021-08-04 13:49:17 -03:00
|
|
|
while (true) {
|
|
|
|
// reset flags
|
|
|
|
_stop = false;
|
|
|
|
_restart = false;
|
2022-08-28 16:24:21 -03:00
|
|
|
_init_failed = false;
|
2021-08-04 13:49:17 -03:00
|
|
|
|
2021-11-13 13:09:09 -04:00
|
|
|
lua_scripts *lua = new lua_scripts(_script_vm_exec_count, _script_heap_size, _debug_options, terminal);
|
2021-08-04 13:49:17 -03:00
|
|
|
if (lua == nullptr || !lua->heap_allocated()) {
|
2023-09-21 22:24:30 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_CRITICAL, "Scripting: %s", "Unable to allocate memory");
|
2021-08-04 13:49:17 -03:00
|
|
|
_init_failed = true;
|
|
|
|
} else {
|
|
|
|
// run won't return while scripting is still active
|
|
|
|
lua->run();
|
|
|
|
|
|
|
|
// only reachable if the lua backend has died for any reason
|
2023-09-21 22:24:30 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_CRITICAL, "Scripting: %s", "stopped");
|
2021-08-04 13:49:17 -03:00
|
|
|
}
|
2020-03-28 00:11:09 -03:00
|
|
|
delete lua;
|
2022-10-12 15:33:54 -03:00
|
|
|
lua = nullptr;
|
2018-10-29 22:05:36 -03:00
|
|
|
|
2022-09-10 13:22:08 -03:00
|
|
|
// clear allocated i2c devices
|
|
|
|
for (uint8_t i=0; i<SCRIPTING_MAX_NUM_I2C_DEVICE; i++) {
|
|
|
|
delete _i2c_dev[i];
|
|
|
|
_i2c_dev[i] = nullptr;
|
|
|
|
}
|
|
|
|
num_i2c_devices = 0;
|
|
|
|
|
2023-02-20 09:59:22 -04:00
|
|
|
// clear allocated PWM sources
|
|
|
|
for (uint8_t i=0; i<SCRIPTING_MAX_NUM_PWM_SOURCE; i++) {
|
|
|
|
if (_pwm_source[i] != nullptr) {
|
|
|
|
delete _pwm_source[i];
|
|
|
|
_pwm_source[i] = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
num_pwm_source = 0;
|
|
|
|
|
2023-12-03 01:43:29 -04:00
|
|
|
#if AP_NETWORKING_ENABLED
|
|
|
|
// clear allocated sockets
|
|
|
|
for (uint8_t i=0; i<SCRIPTING_MAX_NUM_NET_SOCKET; i++) {
|
|
|
|
if (_net_sockets[i] != nullptr) {
|
|
|
|
delete _net_sockets[i];
|
|
|
|
_net_sockets[i] = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
num_net_sockets = 0;
|
|
|
|
#endif // AP_NETWORKING_ENABLED
|
|
|
|
|
2023-06-18 09:59:45 -03:00
|
|
|
// Clear blocked commands
|
|
|
|
{
|
|
|
|
WITH_SEMAPHORE(mavlink_command_block_list_sem);
|
|
|
|
while (mavlink_command_block_list != nullptr) {
|
|
|
|
command_block_list *next_item = mavlink_command_block_list->next;
|
|
|
|
delete mavlink_command_block_list;
|
|
|
|
mavlink_command_block_list = next_item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-04 13:49:17 -03:00
|
|
|
bool cleared = false;
|
|
|
|
while(true) {
|
|
|
|
// 1hz check if we should restart
|
|
|
|
hal.scheduler->delay(1000);
|
|
|
|
if (!enabled()) {
|
|
|
|
// enable must be put to 0 and back to 1 to restart from params
|
|
|
|
cleared = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// must be enabled to get this far
|
|
|
|
if (cleared || _restart) {
|
2023-09-21 22:24:30 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_CRITICAL, "Scripting: %s", "restarted");
|
2021-08-04 13:49:17 -03:00
|
|
|
break;
|
|
|
|
}
|
2021-11-13 13:09:09 -04:00
|
|
|
if ((_debug_options.get() & uint8_t(lua_scripts::DebugLevel::NO_SCRIPTS_TO_RUN)) != 0) {
|
2023-09-21 22:24:30 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_DEBUG, "Scripting: %s", "stopped");
|
2021-08-04 13:49:17 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-27 19:30:55 -03:00
|
|
|
}
|
2023-07-05 11:54:52 -03:00
|
|
|
#pragma GCC pop_options
|
2018-09-27 19:30:55 -03:00
|
|
|
|
2021-02-25 21:09:17 -04:00
|
|
|
void AP_Scripting::handle_mission_command(const AP_Mission::Mission_Command& cmd_in)
|
|
|
|
{
|
2023-10-04 14:01:20 -03:00
|
|
|
#if AP_MISSION_ENABLED
|
2021-02-25 21:09:17 -04:00
|
|
|
if (!_enable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mission_data == nullptr) {
|
|
|
|
// load buffer
|
|
|
|
mission_data = new ObjectBuffer<struct AP_Scripting::scripting_mission_cmd>(mission_cmd_queue_size);
|
2023-01-01 17:33:22 -04:00
|
|
|
if (mission_data != nullptr && mission_data->get_size() == 0) {
|
|
|
|
delete mission_data;
|
|
|
|
mission_data = nullptr;
|
|
|
|
}
|
2021-02-25 21:09:17 -04:00
|
|
|
if (mission_data == nullptr) {
|
2023-09-22 21:29:05 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_ERROR, "Scripting: %s", "unable to receive mission command");
|
2021-02-25 21:09:17 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct scripting_mission_cmd cmd {cmd_in.p1,
|
|
|
|
cmd_in.content.scripting.p1,
|
|
|
|
cmd_in.content.scripting.p2,
|
|
|
|
cmd_in.content.scripting.p3,
|
|
|
|
AP_HAL::millis()};
|
|
|
|
|
|
|
|
mission_data->push(cmd);
|
2023-10-04 14:01:20 -03:00
|
|
|
#endif
|
2021-02-25 21:09:17 -04:00
|
|
|
}
|
|
|
|
|
2022-08-28 16:24:21 -03:00
|
|
|
bool AP_Scripting::arming_checks(size_t buflen, char *buffer) const
|
|
|
|
{
|
2022-08-28 16:31:20 -03:00
|
|
|
if (!enabled() || ((_debug_options.get() & uint8_t(lua_scripts::DebugLevel::DISABLE_PRE_ARM)) != 0)) {
|
2022-08-28 16:24:21 -03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-08-30 17:47:36 -03:00
|
|
|
lua_scripts::get_last_error_semaphore()->take_blocking();
|
2022-08-28 16:24:21 -03:00
|
|
|
const char *error_buf = lua_scripts::get_last_error_message();
|
|
|
|
if (error_buf != nullptr) {
|
|
|
|
hal.util->snprintf(buffer, buflen, "Scripting: %s", error_buf);
|
2022-08-30 17:47:36 -03:00
|
|
|
lua_scripts::get_last_error_semaphore()->give();
|
2022-08-28 16:24:21 -03:00
|
|
|
return false;
|
|
|
|
}
|
2022-08-30 17:47:36 -03:00
|
|
|
lua_scripts::get_last_error_semaphore()->give();
|
2022-08-28 16:24:21 -03:00
|
|
|
|
2023-11-25 13:18:37 -04:00
|
|
|
// Use -1 for disabled, this means we don't have to avoid 0 in the CRC, the sign bit is masked off anyway
|
|
|
|
if (_required_loaded_checksum != -1) {
|
|
|
|
const uint32_t expected_loaded = (uint32_t)_required_loaded_checksum.get() & checksum_param_mask;
|
|
|
|
const uint32_t loaded = lua_scripts::get_loaded_checksum() & checksum_param_mask;
|
|
|
|
if (expected_loaded != loaded) {
|
|
|
|
hal.util->snprintf(buffer, buflen, "Scripting: loaded CRC incorrect want: 0x%x", (unsigned int)loaded);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_required_running_checksum != -1) {
|
|
|
|
const uint32_t expected_running = (uint32_t)_required_running_checksum.get() & checksum_param_mask;
|
|
|
|
const uint32_t running = lua_scripts::get_running_checksum() & checksum_param_mask;
|
|
|
|
if (expected_running != running) {
|
|
|
|
hal.util->snprintf(buffer, buflen, "Scripting: running CRC incorrect want: 0x%x", (unsigned int)running);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-28 16:24:21 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-12-21 17:41:15 -04:00
|
|
|
void AP_Scripting::restart_all()
|
2022-12-18 20:58:35 -04:00
|
|
|
{
|
|
|
|
_restart = true;
|
2022-12-21 17:41:15 -04:00
|
|
|
_stop = true;
|
2022-12-18 20:58:35 -04:00
|
|
|
}
|
|
|
|
|
2023-09-21 22:24:30 -03:00
|
|
|
#if HAL_GCS_ENABLED
|
2020-02-24 20:22:10 -04:00
|
|
|
void AP_Scripting::handle_message(const mavlink_message_t &msg, const mavlink_channel_t chan) {
|
2023-03-09 18:45:30 -04:00
|
|
|
if (mavlink_data.rx_buffer == nullptr) {
|
2020-02-24 20:22:10 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-14 03:26:43 -03:00
|
|
|
struct mavlink_msg data {msg, chan, AP_HAL::millis()};
|
2020-02-24 20:22:10 -04:00
|
|
|
|
|
|
|
WITH_SEMAPHORE(mavlink_data.sem);
|
2023-03-09 18:45:30 -04:00
|
|
|
for (uint16_t i = 0; i < mavlink_data.accept_msg_ids_size; i++) {
|
2023-05-15 20:42:39 -03:00
|
|
|
if (mavlink_data.accept_msg_ids[i] == UINT32_MAX) {
|
2020-02-24 20:22:10 -04:00
|
|
|
return;
|
|
|
|
}
|
2023-03-09 18:45:30 -04:00
|
|
|
if (mavlink_data.accept_msg_ids[i] == msg.msgid) {
|
|
|
|
mavlink_data.rx_buffer->push(data);
|
2020-02-24 20:22:10 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-18 09:59:45 -03:00
|
|
|
bool AP_Scripting::is_handling_command(uint16_t cmd_id)
|
|
|
|
{
|
|
|
|
WITH_SEMAPHORE(mavlink_command_block_list_sem);
|
|
|
|
|
|
|
|
// Look in linked list to see if id is registered
|
|
|
|
if (mavlink_command_block_list != nullptr) {
|
|
|
|
for (command_block_list *item = mavlink_command_block_list; item; item = item->next) {
|
|
|
|
if (item->id == cmd_id) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2023-09-21 22:24:30 -03:00
|
|
|
#endif // HAL_GCS_ENABLED
|
2023-06-18 09:59:45 -03:00
|
|
|
|
2023-11-25 13:18:37 -04:00
|
|
|
// Update called at 1Hz from AP_Vehicle
|
|
|
|
void AP_Scripting::update() {
|
|
|
|
|
|
|
|
save_checksum();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if DEBUG_OPTS bit has been set to save current checksum values to params
|
|
|
|
void AP_Scripting::save_checksum() {
|
|
|
|
|
|
|
|
const uint8_t opts = _debug_options.get();
|
|
|
|
const uint8_t save_bit = uint8_t(lua_scripts::DebugLevel::SAVE_CHECKSUM);
|
|
|
|
if ((opts & save_bit) == 0) {
|
|
|
|
// Bit not set, nothing to do
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save two checksum parameters to there current values
|
|
|
|
_required_loaded_checksum.set_and_save(lua_scripts::get_loaded_checksum() & checksum_param_mask);
|
|
|
|
_required_running_checksum.set_and_save(lua_scripts::get_running_checksum() & checksum_param_mask);
|
|
|
|
|
|
|
|
// Un-set debug option bit
|
|
|
|
_debug_options.set_and_save(opts & ~save_bit);
|
|
|
|
|
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_ERROR, "Scripting: %s", "saved checksums");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-27 20:04:37 -03:00
|
|
|
AP_Scripting *AP_Scripting::_singleton = nullptr;
|
2018-09-27 19:30:55 -03:00
|
|
|
|
2018-09-27 20:04:37 -03:00
|
|
|
namespace AP {
|
|
|
|
AP_Scripting *scripting() {
|
|
|
|
return AP_Scripting::get_singleton();
|
|
|
|
}
|
|
|
|
}
|
2023-06-07 02:56:50 -03:00
|
|
|
|
|
|
|
#endif // AP_SCRIPTING_ENABLED
|