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"
|
2015-11-03 16:59:43 -04:00
|
|
|
#include <AP_HAL/utility/Socket.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>
|
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
|
|
|
|
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
|
|
|
|
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
|
2020-08-05 02:15:43 -03:00
|
|
|
struct sockaddr_in _listen_sockaddr;
|
2015-05-04 21:59:07 -03:00
|
|
|
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-05-10 02:36:18 -03:00
|
|
|
// IPv4 address of target for uartC
|
|
|
|
const char *_tcp_client_addr;
|
|
|
|
|
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;
|
2019-01-24 20:26:38 -04:00
|
|
|
uint32_t last_tick_us;
|
2019-10-28 23:52:19 -03:00
|
|
|
|
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;
|
2012-12-12 18:04:27 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|