AP_Scripting.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. #include <AP_Scripting/AP_Scripting.h>
  14. #include <AP_HAL/AP_HAL.h>
  15. #include <GCS_MAVLink/GCS.h>
  16. #include "lua_scripts.h"
  17. // ensure that we have a set of stack sizes, and enforce constraints around it
  18. // except for the minimum size, these are allowed to be defined by the build system
  19. #undef SCRIPTING_STACK_MIN_SIZE
  20. #define SCRIPTING_STACK_MIN_SIZE (8 * 1024)
  21. #if !defined(SCRIPTING_STACK_SIZE)
  22. #define SCRIPTING_STACK_SIZE (17 * 1024) // Linux experiences stack corruption at ~16.25KB when handed bad scripts
  23. #endif // !defined(SCRIPTING_STACK_SIZE)
  24. #if !defined(SCRIPTING_STACK_MAX_SIZE)
  25. #define SCRIPTING_STACK_MAX_SIZE (64 * 1024)
  26. #endif // !defined(SCRIPTING_STACK_MAX_SIZE)
  27. static_assert(SCRIPTING_STACK_SIZE >= SCRIPTING_STACK_MIN_SIZE, "Scripting requires a larger minimum stack size");
  28. static_assert(SCRIPTING_STACK_SIZE <= SCRIPTING_STACK_MAX_SIZE, "Scripting requires a smaller stack size");
  29. extern const AP_HAL::HAL& hal;
  30. const AP_Param::GroupInfo AP_Scripting::var_info[] = {
  31. // @Param: ENABLE
  32. // @DisplayName: Enable Scripting
  33. // @Description: Controls if scripting is enabled
  34. // @Values: 0:None,1:Lua Scripts
  35. // @RebootRequired: True
  36. // @User: Advanced
  37. AP_GROUPINFO_FLAGS("ENABLE", 1, AP_Scripting, _enable, 0, AP_PARAM_FLAG_ENABLE),
  38. // @Param: VM_I_COUNT
  39. // @DisplayName: Scripting Virtual Machine Instruction Count
  40. // @Description: The number virtual machine instructions that can be run before considering a script to have taken an excessive amount of time
  41. // @Range: 1000 1000000
  42. // @Increment: 10000
  43. // @User: Advanced
  44. AP_GROUPINFO("VM_I_COUNT", 2, AP_Scripting, _script_vm_exec_count, 10000),
  45. // @Param: HEAP_SIZE
  46. // @DisplayName: Scripting Heap Size
  47. // @Description: Amount of memory available for scripting
  48. // @Range: 1024 1048576
  49. // @Increment: 1024
  50. // @User: Advanced
  51. // @RebootRequired: True
  52. AP_GROUPINFO("HEAP_SIZE", 3, AP_Scripting, _script_heap_size, 32*1024),
  53. AP_GROUPINFO("DEBUG_LVL", 4, AP_Scripting, _debug_level, 1),
  54. AP_GROUPEND
  55. };
  56. AP_Scripting::AP_Scripting() {
  57. AP_Param::setup_object_defaults(this, var_info);
  58. #if CONFIG_HAL_BOARD == HAL_BOARD_SITL
  59. if (_singleton != nullptr) {
  60. AP_HAL::panic("Scripting must be a singleton");
  61. }
  62. #endif // CONFIG_HAL_BOARD == HAL_BOARD_SITL
  63. _singleton = this;
  64. }
  65. bool AP_Scripting::init(void) {
  66. if (!_enable) {
  67. return true;
  68. }
  69. if (!hal.scheduler->thread_create(FUNCTOR_BIND_MEMBER(&AP_Scripting::thread, void),
  70. "Scripting", SCRIPTING_STACK_SIZE, AP_HAL::Scheduler::PRIORITY_SCRIPTING, 0)) {
  71. gcs().send_text(MAV_SEVERITY_CRITICAL, "Could not create scripting stack (%d)", SCRIPTING_STACK_SIZE);
  72. return false;
  73. }
  74. return true;
  75. }
  76. void AP_Scripting::thread(void) {
  77. lua_scripts *lua = new lua_scripts(_script_vm_exec_count, _script_heap_size, _debug_level);
  78. if (lua == nullptr) {
  79. gcs().send_text(MAV_SEVERITY_CRITICAL, "Unable to allocate scripting memory");
  80. return;
  81. }
  82. lua->run();
  83. // only reachable if the lua backend has died for any reason
  84. gcs().send_text(MAV_SEVERITY_CRITICAL, "Scripting has died");
  85. }
  86. AP_Scripting *AP_Scripting::_singleton = nullptr;
  87. namespace AP {
  88. AP_Scripting *scripting() {
  89. return AP_Scripting::get_singleton();
  90. }
  91. }