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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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)
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
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
|
2019-06-17 19:24:06 -03:00
|
|
|
AP_GROUPINFO_FLAGS("ENABLE", 1, AP_Scripting, _enable, 0, 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
|
2019-10-31 19:04:36 -03:00
|
|
|
AP_GROUPINFO("HEAP_SIZE", 3, AP_Scripting, _script_heap_size, 42*1024),
|
2018-12-08 22:35:08 -04:00
|
|
|
|
2019-03-22 12:19:59 -03:00
|
|
|
AP_GROUPINFO("DEBUG_LVL", 4, AP_Scripting, _debug_level, 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;
|
|
|
|
}
|
|
|
|
|
2018-09-27 20:04:37 -03:00
|
|
|
bool AP_Scripting::init(void) {
|
|
|
|
if (!_enable) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-09-27 19:30:55 -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)) {
|
2018-10-31 19:43:23 -03:00
|
|
|
gcs().send_text(MAV_SEVERITY_CRITICAL, "Could not create scripting stack (%d)", SCRIPTING_STACK_SIZE);
|
2019-11-26 01:33:14 -04:00
|
|
|
_init_failed = true;
|
2018-09-27 20:04:37 -03:00
|
|
|
return false;
|
|
|
|
}
|
2018-09-27 19:30:55 -03:00
|
|
|
|
2018-09-27 20:04:37 -03:00
|
|
|
return true;
|
|
|
|
}
|
2018-09-27 19:30:55 -03:00
|
|
|
|
2018-09-27 20:04:37 -03:00
|
|
|
void AP_Scripting::thread(void) {
|
2019-03-22 12:19:59 -03:00
|
|
|
lua_scripts *lua = new lua_scripts(_script_vm_exec_count, _script_heap_size, _debug_level);
|
2019-11-26 01:33:14 -04:00
|
|
|
if (lua == nullptr || !lua->heap_allocated()) {
|
2018-10-31 19:43:23 -03:00
|
|
|
gcs().send_text(MAV_SEVERITY_CRITICAL, "Unable to allocate scripting memory");
|
2019-11-26 01:33:14 -04:00
|
|
|
_init_failed = true;
|
2018-10-29 22:05:36 -03:00
|
|
|
return;
|
|
|
|
}
|
2018-10-31 19:43:23 -03:00
|
|
|
lua->run();
|
2018-10-29 22:05:36 -03:00
|
|
|
|
2018-10-31 19:43:23 -03:00
|
|
|
// only reachable if the lua backend has died for any reason
|
2019-10-15 04:02:08 -03:00
|
|
|
gcs().send_text(MAV_SEVERITY_CRITICAL, "Scripting has stopped");
|
2018-09-27 19:30:55 -03:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|