LCOV - code coverage report
Current view: top level - AP_HAL_SITL - UARTDriver.h (source / functions) Hit Total Coverage
Test: lcov.info Lines: 10 14 71.4 %
Date: 2026-09-09 11:02:08 Functions: 4 6 66.7 %

          Line data    Source code
       1             : #pragma once
       2             : 
       3             : #include <AP_HAL/AP_HAL.h>
       4             : #if CONFIG_HAL_BOARD == HAL_BOARD_SITL
       5             : 
       6             : #include <stdint.h>
       7             : #include <stdarg.h>
       8             : #include "AP_HAL_SITL_Namespace.h"
       9             : #include <AP_HAL/utility/Socket_native.h>
      10             : #include <AP_HAL/utility/RingBuffer.h>
      11             : #include <AP_CSVReader/AP_CSVReader.h>
      12             : #include <AP_HAL/utility/DataRateLimit.h>
      13             : 
      14             : #include <SITL/SIM_SerialDevice.h>
      15             : 
      16             : class HALSITL::UARTDriver : public AP_HAL::UARTDriver {
      17             : public:
      18             :     friend class HALSITL::SITL_State;
      19             : 
      20       21450 :     UARTDriver(const uint8_t portNumber, SITL_State *sitlState) {
      21       21450 :         _portNumber = portNumber;
      22       21450 :         _sitlState = sitlState;
      23             : 
      24       21450 :         _fd = -1;
      25       21450 :         _mc_fd = -1;
      26       21450 :         _listen_fd = -1;
      27       21450 :     }
      28             : 
      29           0 :     bool is_initialized() override {
      30           0 :         return true;
      31             :     }
      32             : 
      33             :     ssize_t get_system_outqueue_length() const;
      34             :     ssize_t get_system_outqueue_limit() const;
      35             : 
      36           0 :     bool tx_pending() override {
      37           0 :         return false;
      38             :     }
      39             : 
      40             :     /* Implementations of Stream virtual methods */
      41             :     uint32_t txspace() override;
      42             : 
      43             :     bool _unbuffered_writes;
      44             : 
      45             :     void set_flow_control(enum flow_control flow_control_setting) override;
      46             :     enum flow_control get_flow_control(void) override;
      47             : 
      48             :     void configure_parity(uint8_t v) override;
      49             :     void set_stop_bits(int n) override;
      50             :     bool set_unbuffered_writes(bool on) override;
      51             : 
      52             :     uint32_t bw_in_bytes_per_second() const override;
      53             : 
      54             :     void _timer_tick(void) override;
      55             : 
      56             :     /*
      57             :       return timestamp estimate in microseconds for when the start of
      58             :       a nbytes packet arrived on the uart. This should be treated as a
      59             :       time constraint, not an exact time. It is guaranteed that the
      60             :       packet did not start being received after this time, but it
      61             :       could have been in a system buffer before the returned time.
      62             : 
      63             :       This takes account of the baudrate of the link. For transports
      64             :       that have no baudrate (such as USB) the time estimate may be
      65             :       less accurate.
      66             : 
      67             :       A return value of zero means the HAL does not support this API
      68             :      */
      69             :     uint64_t receive_time_constraint_us(uint16_t nbytes) override;
      70             : 
      71        2031 :     uint32_t get_baud_rate() const override { return _uart_baudrate; }
      72             : 
      73             : #if HAL_UART_STATS_ENABLED
      74             :     // request information on uart I/O
      75             :     void uart_info(ExpandingString &str, StatsTracker &stats, const uint32_t dt_ms) override;
      76             : #endif
      77             : 
      78             : private:
      79             : 
      80             :     int _fd;
      81             : 
      82             :     // file descriptor for reading multicast packets
      83             :     int _mc_fd;
      84             : 
      85             :     uint8_t _portNumber;
      86             :     bool _connected = false; // true if a client has connected
      87             :     bool _use_send_recv = false;
      88             :     bool _is_unix_socket = false;
      89             :     int _listen_fd;  // socket we are listening on
      90             :     int _serial_port;
      91             :     static bool _console;
      92             :     ByteBuffer _readbuffer{16384};
      93             :     ByteBuffer _writebuffer{16384};
      94             : 
      95             :     // default multicast IP and port
      96             :     const char *mcast_ip_default = "239.255.145.50";
      97             :     const uint16_t mcast_port_default = 14550;
      98             : 
      99             :     const char *_uart_path;
     100             :     uint32_t _uart_baudrate;
     101             :     enum flow_control _flow_control = FLOW_CONTROL_DISABLE;
     102             :     uint32_t _auto_flow_start_ms;   // time AUTO mode first had pending data to write
     103             :     bool _auto_flow_detected;   // true once CTS confirmed active in AUTO mode
     104             : 
     105             :     void _tcp_start_connection(uint16_t port, bool wait_for_connection);
     106             :     void _unix_start_connection(const char *path, bool wait_for_connection);
     107             :     void _uart_start_connection(void);
     108             :     void _check_reconnect();
     109             :     void _tcp_start_client(const char *address, uint16_t port);
     110             :     void _udp_start_client(const char *address, uint16_t port);
     111             :     void _udp_start_multicast(const char *address, uint16_t port);
     112             :     void _check_connection(void);
     113             :     static bool _select_check(int );
     114             : 
     115             :     bool set_speed(int speed) const;
     116             : 
     117             :     SITL_State *_sitlState;
     118             :     uint64_t _receive_timestamp;
     119             :     bool _is_udp;
     120             :     bool _packetise;
     121             :     uint16_t _mc_myport;
     122             : 
     123             :     // for baud-rate limiting:
     124             :     struct {
     125             :         DataRateLimit write;
     126             :         DataRateLimit read;
     127             :     } baud_limits;
     128             : 
     129             :     HAL_Semaphore write_mtx;
     130             : 
     131             :     SITL::SerialDevice *_sim_serial_device;
     132             : 
     133             :     struct {
     134             :         bool active;
     135             :         uint8_t term[20];
     136             :         AP_CSVReader csvreader{term, sizeof(term), ','};
     137             :         struct {
     138             :             uint32_t timestamp_us;
     139             :             uint8_t b;  // the byte
     140             :         } loaded_data;
     141             :         bool loaded;  // true if data is all valid
     142             :         bool done_first_line = false;
     143             :         uint8_t terms_seen;
     144             :         uint32_t first_timestamp_us;
     145             :         uint32_t first_emit_micros_us;
     146             :     } logic_async_csv;
     147             :     uint16_t read_from_async_csv(uint8_t *buffer, uint16_t space);
     148             : 
     149             : protected:
     150             :     void _begin(uint32_t b, uint16_t rxS, uint16_t txS) override;
     151             :     size_t _write(const uint8_t *buffer, size_t size) override;
     152             :     ssize_t _read(uint8_t *buffer, uint16_t count) override;
     153             :     uint32_t _available() override;
     154             :     void _end() override;
     155             :     void _flush() override;
     156             :     bool _discard_input() override;
     157             : 
     158             : #if HAL_UART_STATS_ENABLED
     159             :     // Getters for cumulative tx and rx counts
     160     1492170 :     uint32_t get_total_tx_bytes() const override { return _tx_stats_bytes; }
     161     1492170 :     uint32_t get_total_rx_bytes() const override { return _rx_stats_bytes; }
     162             : #endif
     163             : 
     164             : private:
     165             :     void handle_writing_from_writebuffer_to_device();
     166             :     void handle_reading_from_device_to_readbuffer();
     167             : 
     168             :     // statistics
     169             :     uint32_t _tx_stats_bytes;
     170             :     uint32_t _rx_stats_bytes;
     171             : 
     172             : };
     173             : 
     174             : #endif

Generated by: LCOV version 1.14