LCOV - code coverage report
Current view: top level - AP_HAL - Util.h (source / functions) Hit Total Coverage
Test: lcov.info Lines: 15 38 39.5 %
Date: 2026-09-09 11:02:08 Functions: 15 35 42.9 %

          Line data    Source code
       1             : #pragma once
       2             : 
       3             : #include <stdarg.h>
       4             : #include <AP_Common/AP_Common.h> // for FMT_PRINTF
       5             : #include "AP_HAL_Namespace.h"
       6             : #include <AP_Logger/AP_Logger_config.h>
       7             : 
       8             : class ExpandingString;
       9             : 
      10             : class AP_HAL::Util {
      11             : public:
      12             :     int snprintf(char* str, size_t size,
      13             :                  const char *format, ...) FMT_PRINTF(4, 5);
      14             : 
      15             :     int vsnprintf(char* str, size_t size,
      16             :                   const char *format, va_list ap);
      17             : 
      18             :     virtual void set_soft_armed(const bool b);
      19  1042374311 :     bool get_soft_armed() const { return soft_armed; }
      20             : 
      21             :     // return the time that the armed state last changed
      22    93252810 :     uint32_t get_last_armed_change() const { return last_armed_change_ms; };
      23             : 
      24             :     // return true if the reason for the reboot was a watchdog reset
      25           2 :     virtual bool was_watchdog_reset() const { return false; }
      26             : 
      27             : #if AP_REBOOT_MASS_STORAGE_ENABLED
      28             :     // support an early application mode which exports the SD card over USB
      29             :     virtual bool request_usb_msd() { return false; }
      30             : #endif
      31             : 
      32             :     // return true if safety was off and this was a watchdog reset
      33           0 :     bool was_watchdog_safety_off() const {
      34           0 :         return was_watchdog_reset() && persistent_data.safety_state == SAFETY_ARMED;
      35             :     }
      36             : 
      37             :     // return true if this is a watchdog reset boot and we were armed
      38       31502 :     bool was_watchdog_armed() const { return was_watchdog_reset() && persistent_data.armed; }
      39             : 
      40        2088 :     virtual const char* get_custom_log_directory() const { return nullptr; }
      41        1690 :     virtual const char* get_custom_terrain_directory() const { return nullptr;  }
      42           0 :     virtual const char *get_custom_storage_directory() const { return nullptr;  }
      43             : 
      44             :     // get path to custom defaults file for AP_Param
      45           0 :     virtual const char* get_custom_defaults_file() const {
      46           0 :         return HAL_PARAM_DEFAULTS_PATH;
      47             :     }
      48             : 
      49             :     // set command line parameters to the eeprom on start
      50           0 :     virtual void set_cmdline_parameters() {};
      51             : 
      52             :     enum safety_state : uint8_t {
      53             :         SAFETY_NONE,
      54             :         SAFETY_DISARMED,
      55             :         SAFETY_ARMED,
      56             :     };
      57             : 
      58             :     /*
      59             :       persistent data structure. This data is restored on boot if
      60             :       there has been a watchdog reset.  The data in this structure
      61             :       should only be read if was_watchdog_reset() is true
      62             :       Note that on STM32 this structure is limited to 76 bytes
      63             :      */
      64             :     struct PersistentData {
      65             :         float roll_rad, pitch_rad, yaw_rad; // attitude
      66             :         int32_t home_lat, home_lon, home_alt_cm; // home position
      67             :         uint32_t fault_addr;
      68             :         uint32_t fault_icsr;
      69             :         uint32_t fault_lr;
      70             :         uint32_t internal_errors;
      71             :         uint16_t internal_error_count;
      72             :         uint16_t internal_error_last_line;
      73             :         uint32_t spi_count;
      74             :         uint32_t i2c_count;
      75             :         uint32_t i2c_isr_count;
      76             :         uint16_t waypoint_num;
      77             :         uint16_t last_mavlink_msgid;
      78             :         uint16_t last_mavlink_cmd;
      79             :         uint16_t semaphore_line;
      80             :         uint16_t fault_line;
      81             :         uint8_t fault_type;
      82             :         uint8_t fault_thd_prio;
      83             :         char thread_name4[4];
      84             :         int8_t scheduler_task;
      85             :         bool armed : 1; // true if vehicle was armed
      86             :         bool boot_to_mass_storage : 1;
      87             :         enum safety_state safety_state;
      88             :         bool boot_to_dfu; // true if we should reboot to DFU on boot
      89             :     };
      90             :     struct PersistentData persistent_data;
      91             :     // last_persistent_data is only filled in if we've suffered a watchdog reset
      92             :     struct PersistentData last_persistent_data;
      93             : 
      94             :     /*
      95             :       return state of safety switch, if applicable
      96             :      */
      97           0 :     virtual enum safety_state safety_switch_state(void) { return SAFETY_NONE; }
      98             : 
      99             :     /*
     100             :       set HW RTC in UTC microseconds
     101             :      */
     102             :     virtual void set_hw_rtc(uint64_t time_utc_usec) = 0;
     103             : 
     104             :     /*
     105             :       get system clock in UTC microseconds
     106             :      */
     107             :     virtual uint64_t get_hw_rtc() const = 0;
     108             : 
     109             :     enum class FlashBootloader {
     110             :         OK=0,
     111             :         NO_CHANGE=1,
     112             :         FAIL=2,
     113             :         NOT_AVAILABLE=3,
     114             :         NOT_SIGNED=4,
     115             :     };
     116             : 
     117             :     // overwrite bootloader (probably with one from ROMFS)
     118           0 :     virtual FlashBootloader flash_bootloader() { return FlashBootloader::NOT_AVAILABLE; }
     119             : 
     120             :     /*
     121             :       get system identifier (eg. serial number)
     122             :       return false if a system identifier is not available
     123             : 
     124             :       Buf should be filled with a printable string and must be null
     125             :       terminated
     126             :      */
     127           0 :     virtual bool get_system_id(char buf[50]) { return false; }
     128           0 :     virtual bool get_system_id_unformatted(uint8_t buf[], uint8_t &len) { return false; }
     129             : 
     130             :     /**
     131             :        return commandline arguments, if available
     132             :      */
     133           0 :     virtual void commandline_arguments(uint8_t &argc, char * const *&argv) { argc = 0; }
     134             : 
     135        2094 :     virtual bool toneAlarm_init(uint8_t types) { return false;}
     136           0 :     virtual void toneAlarm_set_buzzer_tone(float frequency, float volume, uint32_t duration_ms) {}
     137             : 
     138             :     /* Support for an imu heating system */
     139           0 :     virtual void set_imu_temp(float current) {}
     140             : 
     141             :     /* Support for an imu heating system */
     142           0 :     virtual void set_imu_target_temp(int8_t *target) {}
     143             :     
     144             :     // allocate and free DMA-capable memory if possible. Otherwise return normal memory
     145             :     enum Memory_Type {
     146             :         MEM_DMA_SAFE,
     147             :         MEM_FAST,
     148             :         MEM_FILESYSTEM
     149             :     };
     150        2603 :     virtual void *malloc_type(size_t size, Memory_Type mem_type) { return calloc(1, size); }
     151           2 :     virtual void free_type(void *ptr, size_t size, Memory_Type mem_type) { return free(ptr); }
     152             : 
     153             :     /**
     154             :        how much free memory do we have in bytes. If unknown return 4096
     155             :      */
     156           0 :     virtual uint32_t available_memory(void) { return 4096; }
     157             : 
     158             :     // attempt to trap the processor, presumably to enter an attached debugger
     159           0 :     virtual bool trap() const { return false; }
     160             : 
     161             :     // request information on running threads
     162        1263 :     virtual void thread_info(ExpandingString &str) {}
     163             : 
     164             :     // request information on dma contention
     165        1264 :     virtual void dma_info(ExpandingString &str) {}
     166             : 
     167             :     // request information on memory allocation
     168        1263 :     virtual void mem_info(ExpandingString &str) {}
     169             : 
     170             :     // load persistent parameters from bootloader sector
     171           0 :     virtual bool load_persistent_params(ExpandingString &str) const { return false; }
     172             : 
     173           0 :     virtual bool get_persistent_param_by_name(const char *name, char* value, size_t& len) const {
     174           0 :         return false;
     175             :     }
     176             : 
     177             : #if HAL_UART_STATS_ENABLED
     178             :     // request information on uart I/O
     179           0 :     virtual void uart_info(ExpandingString &str) {}
     180             : 
     181             : #if HAL_LOGGING_ENABLED
     182             :     // Log UART message for each serial port
     183           0 :     virtual void uart_log() {};
     184             : #endif
     185             : #endif // HAL_UART_STATS_ENABLED
     186             : 
     187             :     // request information on timer frequencies
     188        1262 :     virtual void timer_info(ExpandingString &str) {}
     189             : 
     190             :     // generate Random values
     191           0 :     virtual bool get_random_vals(uint8_t* data, size_t size) { return false; }
     192             : 
     193             :     // generate Random values, will block until enough entropy is available
     194           0 :     virtual bool get_true_random_vals(uint8_t* data, size_t size, uint32_t timeout_us) { return false; }
     195             : 
     196             :     // log info on stack usage
     197     1520757 :     virtual void log_stack_info(void) {}
     198             : 
     199             : #if AP_CRASHDUMP_ENABLED
     200             :     virtual size_t last_crash_dump_size() const { return 0; }
     201             :     virtual void* last_crash_dump_ptr() const { return nullptr; }
     202             : #endif
     203             : 
     204             :     // get the system load
     205        4675 :     virtual bool get_system_load(float& avg_load, float& peak_load) const { return false; }
     206             : 
     207             : #if HAL_ENABLE_DFU_BOOT
     208             :     virtual void boot_to_dfu(void) {}
     209             : #endif
     210             : protected:
     211             :     // we start soft_armed false, so that actuators don't send any
     212             :     // values until the vehicle code has fully started
     213             :     bool soft_armed = false;
     214             :     uint32_t last_armed_change_ms;
     215             : };
     216             : 
     217             : extern "C" {
     218             :     void AP_stack_overflow(const char *thread_name);
     219             :     void AP_memory_guard_error(uint32_t size);
     220             : }

Generated by: LCOV version 1.14