Line data Source code
1 : #pragma once 2 : 3 : #include <AP_HAL/AP_HAL.h> 4 : #include "AP_HAL_SITL_Namespace.h" 5 : #include "AP_HAL_SITL.h" 6 : #include "Semaphores.h" 7 : #include "ToneAlarm_SF.h" 8 : #include <AP_Logger/AP_Logger_config.h> 9 : 10 : #if !defined(__CYGWIN__) && !defined(__CYGWIN64__) 11 : #include <sys/types.h> 12 : #include <signal.h> 13 : #endif 14 : 15 : class HALSITL::Util : public AP_HAL::Util { 16 : public: 17 2145 : Util(SITL_State *_sitlState) : 18 2145 : sitlState(_sitlState) {} 19 : 20 : /** 21 : how much free memory do we have in bytes. 22 : */ 23 42294729 : uint32_t available_memory(void) override { 24 : // SITL is assumed to always have plenty of memory. Return 512k for now 25 42294729 : return 512*1024; 26 : } 27 : 28 : // get path to custom defaults file for AP_Param 29 3863 : const char* get_custom_defaults_file() const override { 30 3863 : return sitlState->defaults_path; 31 : } 32 : 33 : /** 34 : return commandline arguments, if available 35 : */ 36 : void commandline_arguments(uint8_t &argc, char * const *&argv) override; 37 : 38 : uint64_t get_hw_rtc() const override; 39 1352847 : void set_hw_rtc(uint64_t time_utc_usec) override { /* fail silently */ } 40 : 41 : 42 : bool get_system_id(char buf[50]) override; 43 : bool get_system_id_unformatted(uint8_t buf[], uint8_t &len) override; 44 : void dump_stack_trace(); 45 : 46 : #ifdef WITH_SITL_TONEALARM 47 : bool toneAlarm_init(uint8_t types) override { return _toneAlarm.init(); } 48 : void toneAlarm_set_buzzer_tone(float frequency, float volume, uint32_t duration_ms) override { 49 : _toneAlarm.set_buzzer_tone(frequency, volume, duration_ms); 50 : } 51 : #endif 52 : 53 : // return true if the reason for the reboot was a watchdog reset 54 42032324 : bool was_watchdog_reset() const override { return getenv("SITL_WATCHDOG_RESET") != nullptr; } 55 : 56 : #if !defined(HAL_BUILD_AP_PERIPH) 57 : enum safety_state safety_switch_state(void) override; 58 : void set_cmdline_parameters() override; 59 : #endif 60 : 61 0 : bool trap() const override { 62 : #if defined(__CYGWIN__) || defined(__CYGWIN64__) 63 : return false; 64 : #else 65 0 : if (kill(0, SIGTRAP) == -1) { 66 0 : return false; 67 : } 68 0 : return true; 69 : #endif 70 : } 71 : 72 2094 : void init(int argc, char *const *argv) { 73 2094 : saved_argc = argc; 74 2094 : saved_argv = argv; 75 2094 : } 76 : 77 : // fills data with random values of requested size 78 : bool get_random_vals(uint8_t* data, size_t size) override; 79 : 80 : private: 81 : SITL_State *sitlState; 82 : 83 : #ifdef WITH_SITL_TONEALARM 84 : static ToneAlarm_SF _toneAlarm; 85 : #endif 86 : 87 : int saved_argc; 88 : char *const *saved_argv; 89 : 90 : #if HAL_UART_STATS_ENABLED 91 : // request information on uart I/O 92 : void uart_info(ExpandingString &str) override; 93 : 94 : #if HAL_LOGGING_ENABLED 95 : // Log UART message for each serial port 96 : void uart_log() override; 97 : #endif 98 : #endif // HAL_UART_STATS_ENABLED 99 : 100 : private: 101 : #if HAL_UART_STATS_ENABLED 102 : // UART stats tracking helper 103 : struct uart_stats { 104 : AP_HAL::UARTDriver::StatsTracker serial[AP_HAL::HAL::num_serial]; 105 : uint32_t last_ms; 106 : }; 107 : uart_stats sys_uart_stats; 108 : #if HAL_LOGGING_ENABLED 109 : uart_stats log_uart_stats; 110 : #endif 111 : #endif // HAL_UART_STATS_ENABLED 112 : };