LCOV - code coverage report
Current view: top level - AP_Scripting - AP_Scripting.h (source / functions) Hit Total Coverage
Test: lcov.info Lines: 8 12 66.7 %
Date: 2026-09-09 11:02:08 Functions: 7 9 77.8 %

          Line data    Source code
       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             : 
       7             :    This program is distributed in the hope that it will be useful,
       8             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
       9             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      10             :    GNU General Public License for more details.
      11             : 
      12             :    You should have received a copy of the GNU General Public License
      13             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      14             :  */
      15             : #pragma once
      16             : 
      17             : #include "AP_Scripting/AP_Scripting_config.h"
      18             : 
      19             : #if AP_SCRIPTING_ENABLED
      20             : 
      21             : #include <GCS_MAVLink/GCS_config.h>
      22             : #include <AP_Common/AP_Common.h>
      23             : #include <AP_Param/AP_Param.h>
      24             : #include <GCS_MAVLink/GCS_MAVLink.h>
      25             : #include <AP_Mission/AP_Mission.h>
      26             : #include <AP_Filesystem/AP_Filesystem.h>
      27             : #include <AP_HAL/I2CDevice.h>
      28             : #include "AP_Scripting_CANSensor.h"
      29             : #include <AP_Networking/AP_Networking_Config.h>
      30             : 
      31             : #ifndef SCRIPTING_MAX_NUM_I2C_DEVICE
      32             :   #define SCRIPTING_MAX_NUM_I2C_DEVICE 4
      33             : #endif
      34             : 
      35             : #define SCRIPTING_MAX_NUM_PWM_SOURCE 4
      36             : 
      37             : #if AP_NETWORKING_ENABLED
      38             : #ifndef SCRIPTING_MAX_NUM_NET_SOCKET
      39             : #define SCRIPTING_MAX_NUM_NET_SOCKET 50
      40             : #endif
      41             : class SocketAPM;
      42             : #endif
      43             : 
      44             : #if AP_SCRIPTING_SERIALDEVICE_ENABLED
      45             : #include "AP_Scripting_SerialDevice.h"
      46             : #endif
      47             : 
      48             : class AP_Scripting
      49             : {
      50             : public:
      51             :     AP_Scripting();
      52             : 
      53             :     /* Do not allow copies */
      54             :     CLASS_NO_COPY(AP_Scripting);
      55             : 
      56             :     void init(void);
      57             : 
      58             : #if AP_SCRIPTING_SERIALDEVICE_ENABLED
      59             :     void init_serialdevice_ports(void);
      60             : #endif
      61             : 
      62             :     void update();
      63             : 
      64      158115 :     bool enabled(void) const { return _enable != 0; };
      65       91303 :     bool should_run(void) const { return enabled() && !_stop; }
      66             : 
      67             : #if HAL_GCS_ENABLED
      68             :     void handle_message(const mavlink_message_t &msg, const mavlink_channel_t chan);
      69             : 
      70             :     // Check if command ID is blocked
      71             :     bool is_handling_command(uint16_t cmd_id);
      72             : #endif
      73             : 
      74     3569057 :     static AP_Scripting * get_singleton(void) { return _singleton; }
      75             : 
      76             :     static const struct AP_Param::GroupInfo var_info[];
      77             : 
      78             : #if HAL_GCS_ENABLED
      79             :     MAV_RESULT handle_command_int_packet(const mavlink_command_int_t &packet);
      80             : #endif
      81             : 
      82             :     void handle_mission_command(const class AP_Mission::Mission_Command& cmd);
      83             : 
      84             :     bool arming_checks(size_t buflen, char *buffer) const;
      85             :     
      86             :     void restart_all(void);
      87           0 :     void stop(void) { _stop = true; }
      88             : 
      89             :    // User parameters for inputs into scripts 
      90             :    AP_Float _user[6];
      91             : 
      92             :     enum class SCR_DIR {
      93             :         ROMFS = 1 << 0,
      94             :         SCRIPTS = 1 << 1,
      95             :     };
      96         189 :     uint16_t get_disabled_dir() { return uint16_t(_dir_disable.get());}
      97             : 
      98             :     // the number of and storage for i2c devices
      99             :     uint8_t num_i2c_devices;
     100             :     AP_HAL::I2CDevice *_i2c_dev[SCRIPTING_MAX_NUM_I2C_DEVICE];
     101             : 
     102             : #if AP_SCRIPTING_CAN_SENSOR_ENABLED
     103             :     // Scripting CAN sensor
     104             :     ScriptingCANSensor *_CAN_dev;
     105             :     ScriptingCANSensor *_CAN_dev2;
     106             : #endif
     107             : 
     108             : #if AP_MISSION_ENABLED
     109             :     // mission item buffer
     110             :     static const int mission_cmd_queue_size = 5;
     111             :     struct scripting_mission_cmd {
     112             :         uint16_t p1;
     113             :         float content_p1;
     114             :         float content_p2;
     115             :         float content_p3;
     116             :         uint32_t time_ms;
     117             :     };
     118             :     ObjectBuffer<struct scripting_mission_cmd> * mission_data;
     119             : #endif
     120             : 
     121             :     // PWMSource storage
     122             :     uint8_t num_pwm_source;
     123             :     AP_HAL::PWMSource *_pwm_source[SCRIPTING_MAX_NUM_PWM_SOURCE];
     124          53 :     int get_current_env_ref() { return current_env_ref; }
     125       90393 :     void set_current_env_ref(int ref) { current_env_ref = ref; }
     126             : 
     127             : #if AP_NETWORKING_ENABLED
     128             :     // SocketAPM storage
     129             :     SocketAPM *_net_sockets[SCRIPTING_MAX_NUM_NET_SOCKET];
     130             : #endif
     131             : 
     132             :     struct mavlink_msg {
     133             :         mavlink_message_t msg;
     134             :         mavlink_channel_t chan;
     135             :         uint32_t timestamp_ms;
     136             :     };
     137             : 
     138             :     struct mavlink {
     139             :         ObjectBuffer<struct mavlink_msg> *rx_buffer;
     140             :         uint32_t *accept_msg_ids;
     141             :         uint16_t accept_msg_ids_size;
     142             :         HAL_Semaphore sem;
     143             :     } mavlink_data;
     144             : 
     145             :     struct command_block_list {
     146             :         uint16_t id;
     147             :         command_block_list *next;
     148             :     };
     149             :     command_block_list *mavlink_command_block_list;
     150             :     HAL_Semaphore mavlink_command_block_list_sem;
     151             : 
     152             :     #if AP_SCRIPTING_SERIALDEVICE_ENABLED
     153             :         AP_Scripting_SerialDevice _serialdevice;
     154             :     #endif
     155             : 
     156             :     enum class DebugOption : uint8_t {
     157             :         NO_SCRIPTS_TO_RUN = 1U << 0,
     158             :         RUNTIME_MSG = 1U << 1,
     159             :         SUPPRESS_SCRIPT_LOG = 1U << 2,
     160             :         LOG_RUNTIME = 1U << 3,
     161             :         DISABLE_PRE_ARM = 1U << 4,
     162             :         SAVE_CHECKSUM = 1U << 5,
     163             :         DISABLE_HEAP_EXPANSION = 1U << 6,
     164             :     };
     165             : 
     166             : private:
     167             : 
     168             :     void thread(void); // main script execution thread
     169             : 
     170             :     // Check if DEBUG_OPTS bit has been set to save current checksum values to params
     171             :     void save_checksum();
     172             : 
     173             :     // Mask down to 23 bits for comparison with parameters, this the length of the a float mantissa, to deal with the float transport of parameters over MAVLink
     174             :     // The full range of uint32 integers cannot be represented by a float.
     175             :     const uint32_t checksum_param_mask = 0x007FFFFF;
     176             : 
     177             :     enum class ThreadPriority : uint8_t {
     178             :         NORMAL = 0,
     179             :         IO = 1,
     180             :         STORAGE = 2,
     181             :         UART = 3,
     182             :         I2C = 4,
     183             :         SPI = 5,
     184             :         TIMER = 6,
     185             :         MAIN = 7,
     186             :         BOOST = 8
     187             :     };
     188             : 
     189             :     AP_Int8 _enable;
     190             :     AP_Int32 _script_vm_exec_count;
     191             :     AP_Int32 _script_heap_size;
     192             :     AP_Int8 _debug_options;
     193             :     AP_Int16 _dir_disable;
     194             :     AP_Int32 _required_loaded_checksum;
     195             :     AP_Int32 _required_running_checksum;
     196             : 
     197             :     AP_Enum<ThreadPriority> _thd_priority;
     198             : 
     199      153286 :     bool option_is_set(DebugOption option) const {
     200      153286 :         return (uint8_t(_debug_options.get()) & uint8_t(option)) != 0;
     201             :     }
     202             : 
     203           0 :     void option_clear(DebugOption option) {
     204           0 :         _debug_options.set_and_save(_debug_options.get() & ~uint8_t(option));
     205           0 :     }
     206             : 
     207             :     bool _thread_failed; // thread allocation failed
     208             :     bool _init_failed;  // true if memory allocation failed
     209             :     bool _restart; // true if scripts should be restarted
     210             :     bool _stop; // true if scripts should be stopped
     211             : 
     212             :     static AP_Scripting *_singleton;
     213             :     int current_env_ref;
     214             : };
     215             : 
     216             : namespace AP {
     217             :     AP_Scripting * scripting(void);
     218             : };
     219             : 
     220             : #endif // AP_SCRIPTING_ENABLED

Generated by: LCOV version 1.14