2016-02-17 21:25:28 -04:00
|
|
|
#pragma once
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2015-05-04 03:15:12 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
2012-12-12 18:04:27 -04:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdarg.h>
|
2015-05-04 03:15:12 -03:00
|
|
|
#include "AP_HAL_SITL_Namespace.h"
|
2023-12-25 22:21:11 -04:00
|
|
|
#include <AP_HAL/utility/Socket_native.h>
|
2016-01-10 02:17:32 -04:00
|
|
|
#include <AP_HAL/utility/RingBuffer.h>
|
2023-01-12 19:52:08 -04:00
|
|
|
#include <AP_CSVReader/AP_CSVReader.h>
|
2024-04-01 17:11:40 -03:00
|
|
|
#include <AP_HAL/utility/DataRateLimit.h>
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2021-10-26 09:31:22 -03:00
|
|
|
#include <SITL/SIM_SerialDevice.h>
|
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
class HALSITL::UARTDriver : public AP_HAL::UARTDriver {
|
2012-12-12 18:04:27 -04:00
|
|
|
public:
|
2015-05-04 03:15:12 -03:00
|
|
|
friend class HALSITL::SITL_State;
|
2012-12-13 18:57:01 -04:00
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
UARTDriver(const uint8_t portNumber, SITL_State *sitlState) {
|
2015-05-04 21:59:07 -03:00
|
|
|
_portNumber = portNumber;
|
2012-12-17 22:34:13 -04:00
|
|
|
_sitlState = sitlState;
|
2015-05-04 21:59:07 -03:00
|
|
|
|
2012-12-17 22:34:13 -04:00
|
|
|
_fd = -1;
|
2018-12-05 03:32:43 -04:00
|
|
|
_mc_fd = -1;
|
2012-12-17 22:34:13 -04:00
|
|
|
_listen_fd = -1;
|
2015-05-04 21:59:07 -03:00
|
|
|
}
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2018-11-07 06:59:48 -04:00
|
|
|
bool is_initialized() override {
|
2015-05-04 21:59:07 -03:00
|
|
|
return true;
|
|
|
|
}
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2020-04-02 21:47:06 -03:00
|
|
|
ssize_t get_system_outqueue_length() const;
|
|
|
|
|
2018-11-07 06:59:48 -04:00
|
|
|
bool tx_pending() override {
|
2015-05-04 21:59:07 -03:00
|
|
|
return false;
|
2012-12-12 18:04:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Implementations of Stream virtual methods */
|
2016-08-02 10:42:50 -03:00
|
|
|
uint32_t txspace() override;
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2017-11-22 18:21:50 -04:00
|
|
|
bool _unbuffered_writes;
|
|
|
|
|
2018-11-07 06:59:48 -04:00
|
|
|
enum flow_control get_flow_control(void) override { return FLOW_CONTROL_ENABLE; }
|
2016-01-10 02:17:32 -04:00
|
|
|
|
2017-11-30 16:36:51 -04:00
|
|
|
void configure_parity(uint8_t v) override;
|
|
|
|
void set_stop_bits(int n) override;
|
|
|
|
bool set_unbuffered_writes(bool on) override;
|
2017-11-22 18:21:50 -04:00
|
|
|
|
2023-10-24 19:32:42 -03:00
|
|
|
uint32_t bw_in_bytes_per_second() const override;
|
|
|
|
|
2018-11-07 06:59:48 -04:00
|
|
|
void _timer_tick(void) override;
|
2018-05-15 21:52:08 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
return timestamp estimate in microseconds for when the start of
|
|
|
|
a nbytes packet arrived on the uart. This should be treated as a
|
|
|
|
time constraint, not an exact time. It is guaranteed that the
|
|
|
|
packet did not start being received after this time, but it
|
|
|
|
could have been in a system buffer before the returned time.
|
|
|
|
|
|
|
|
This takes account of the baudrate of the link. For transports
|
|
|
|
that have no baudrate (such as USB) the time estimate may be
|
|
|
|
less accurate.
|
|
|
|
|
|
|
|
A return value of zero means the HAL does not support this API
|
|
|
|
*/
|
2018-05-16 18:01:14 -03:00
|
|
|
uint64_t receive_time_constraint_us(uint16_t nbytes) override;
|
2023-06-26 06:52:07 -03:00
|
|
|
|
2024-02-28 02:11:31 -04:00
|
|
|
uint32_t get_baud_rate() const override { return _uart_baudrate; }
|
|
|
|
|
2024-03-15 11:25:18 -03:00
|
|
|
#if HAL_UART_STATS_ENABLED
|
|
|
|
// request information on uart I/O
|
|
|
|
void uart_info(ExpandingString &str, StatsTracker &stats, const uint32_t dt_ms) override;
|
|
|
|
#endif
|
|
|
|
|
2012-12-12 18:04:27 -04:00
|
|
|
private:
|
2021-10-26 21:48:23 -03:00
|
|
|
|
|
|
|
int _fd;
|
|
|
|
|
|
|
|
// file descriptor for reading multicast packets
|
|
|
|
int _mc_fd;
|
|
|
|
|
2012-12-12 18:04:27 -04:00
|
|
|
uint8_t _portNumber;
|
2015-11-03 16:59:43 -04:00
|
|
|
bool _connected = false; // true if a client has connected
|
|
|
|
bool _use_send_recv = false;
|
2015-05-04 21:59:07 -03:00
|
|
|
int _listen_fd; // socket we are listening on
|
|
|
|
int _serial_port;
|
|
|
|
static bool _console;
|
2016-01-10 02:17:32 -04:00
|
|
|
ByteBuffer _readbuffer{16384};
|
|
|
|
ByteBuffer _writebuffer{16384};
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2018-12-05 03:32:43 -04:00
|
|
|
// default multicast IP and port
|
|
|
|
const char *mcast_ip_default = "239.255.145.50";
|
|
|
|
const uint16_t mcast_port_default = 14550;
|
|
|
|
|
2016-08-22 04:11:48 -03:00
|
|
|
const char *_uart_path;
|
|
|
|
uint32_t _uart_baudrate;
|
|
|
|
|
2015-11-03 16:59:43 -04:00
|
|
|
void _tcp_start_connection(uint16_t port, bool wait_for_connection);
|
2016-08-22 04:11:48 -03:00
|
|
|
void _uart_start_connection(void);
|
|
|
|
void _check_reconnect();
|
2015-11-03 16:59:43 -04:00
|
|
|
void _tcp_start_client(const char *address, uint16_t port);
|
2018-12-05 01:32:49 -04:00
|
|
|
void _udp_start_client(const char *address, uint16_t port);
|
2018-12-05 03:32:43 -04:00
|
|
|
void _udp_start_multicast(const char *address, uint16_t port);
|
2012-12-12 18:04:27 -04:00
|
|
|
void _check_connection(void);
|
|
|
|
static bool _select_check(int );
|
|
|
|
static void _set_nonblocking(int );
|
2021-02-01 12:26:28 -04:00
|
|
|
bool set_speed(int speed) const;
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2012-12-17 22:34:13 -04:00
|
|
|
SITL_State *_sitlState;
|
2018-05-15 21:52:08 -03:00
|
|
|
uint64_t _receive_timestamp;
|
2018-12-05 01:32:49 -04:00
|
|
|
bool _is_udp;
|
2018-12-05 01:58:37 -04:00
|
|
|
bool _packetise;
|
2018-12-05 03:32:43 -04:00
|
|
|
uint16_t _mc_myport;
|
2023-07-17 23:04:09 -03:00
|
|
|
|
|
|
|
// for baud-rate limiting:
|
2024-04-01 17:11:40 -03:00
|
|
|
struct {
|
|
|
|
DataRateLimit write;
|
|
|
|
DataRateLimit read;
|
|
|
|
} baud_limits;
|
2019-10-28 23:52:19 -03:00
|
|
|
|
2023-12-29 22:34:57 -04:00
|
|
|
HAL_Semaphore write_mtx;
|
|
|
|
|
2021-10-26 09:31:22 -03:00
|
|
|
SITL::SerialDevice *_sim_serial_device;
|
2023-01-12 19:52:08 -04:00
|
|
|
|
|
|
|
struct {
|
|
|
|
bool active;
|
|
|
|
uint8_t term[20];
|
|
|
|
AP_CSVReader csvreader{term, sizeof(term), ','};
|
|
|
|
struct {
|
|
|
|
uint32_t timestamp_us;
|
|
|
|
uint8_t b; // the byte
|
|
|
|
} loaded_data;
|
|
|
|
bool loaded; // true if data is all valid
|
|
|
|
bool done_first_line = false;
|
|
|
|
uint8_t terms_seen;
|
|
|
|
uint32_t first_timestamp_us;
|
|
|
|
uint32_t first_emit_micros_us;
|
|
|
|
} logic_async_csv;
|
|
|
|
uint16_t read_from_async_csv(uint8_t *buffer, uint16_t space);
|
2023-06-26 06:52:07 -03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void _begin(uint32_t b, uint16_t rxS, uint16_t txS) override;
|
|
|
|
size_t _write(const uint8_t *buffer, size_t size) override;
|
|
|
|
ssize_t _read(uint8_t *buffer, uint16_t count) override;
|
|
|
|
uint32_t _available() override;
|
|
|
|
void _end() override;
|
|
|
|
void _flush() override;
|
|
|
|
bool _discard_input() override;
|
2023-07-17 23:04:09 -03:00
|
|
|
|
2024-04-06 13:07:31 -03:00
|
|
|
#if HAL_UART_STATS_ENABLED
|
|
|
|
// Getters for cumulative tx and rx counts
|
|
|
|
uint32_t get_total_tx_bytes() const override { return _tx_stats_bytes; }
|
|
|
|
uint32_t get_total_rx_bytes() const override { return _rx_stats_bytes; }
|
|
|
|
#endif
|
|
|
|
|
2023-07-17 23:04:09 -03:00
|
|
|
private:
|
2023-07-17 23:05:41 -03:00
|
|
|
void handle_writing_from_writebuffer_to_device();
|
2023-07-17 23:04:09 -03:00
|
|
|
void handle_reading_from_device_to_readbuffer();
|
2024-03-15 11:25:18 -03:00
|
|
|
|
|
|
|
// statistics
|
|
|
|
uint32_t _tx_stats_bytes;
|
|
|
|
uint32_t _rx_stats_bytes;
|
2024-05-25 19:46:35 -03:00
|
|
|
|
2012-12-12 18:04:27 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|