UARTDriver.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #pragma once
  2. #include <AP_HAL/AP_HAL.h>
  3. #if CONFIG_HAL_BOARD == HAL_BOARD_SITL
  4. #include <stdint.h>
  5. #include <stdarg.h>
  6. #include "AP_HAL_SITL_Namespace.h"
  7. #include <AP_HAL/utility/Socket.h>
  8. #include <AP_HAL/utility/RingBuffer.h>
  9. class HALSITL::UARTDriver : public AP_HAL::UARTDriver {
  10. public:
  11. friend class HALSITL::SITL_State;
  12. UARTDriver(const uint8_t portNumber, SITL_State *sitlState) {
  13. _portNumber = portNumber;
  14. _sitlState = sitlState;
  15. _fd = -1;
  16. _mc_fd = -1;
  17. _listen_fd = -1;
  18. }
  19. /* Implementations of UARTDriver virtual methods */
  20. void begin(uint32_t b) override {
  21. begin(b, 0, 0);
  22. }
  23. void begin(uint32_t b, uint16_t rxS, uint16_t txS) override;
  24. void end() override;
  25. void flush() override;
  26. bool is_initialized() override {
  27. return true;
  28. }
  29. void set_blocking_writes(bool blocking) override
  30. {
  31. _nonblocking_writes = !blocking;
  32. }
  33. bool tx_pending() override {
  34. return false;
  35. }
  36. /* Implementations of Stream virtual methods */
  37. uint32_t available() override;
  38. uint32_t txspace() override;
  39. int16_t read() override;
  40. /* Implementations of Print virtual methods */
  41. size_t write(uint8_t c) override;
  42. size_t write(const uint8_t *buffer, size_t size) override;
  43. // file descriptor, exposed so SITL_State::loop_hook() can use it
  44. int _fd;
  45. // file descriptor for reading multicast packets
  46. int _mc_fd;
  47. bool _unbuffered_writes;
  48. enum flow_control get_flow_control(void) override { return FLOW_CONTROL_ENABLE; }
  49. void configure_parity(uint8_t v) override;
  50. void set_stop_bits(int n) override;
  51. bool set_unbuffered_writes(bool on) override;
  52. void _timer_tick(void) override;
  53. /*
  54. return timestamp estimate in microseconds for when the start of
  55. a nbytes packet arrived on the uart. This should be treated as a
  56. time constraint, not an exact time. It is guaranteed that the
  57. packet did not start being received after this time, but it
  58. could have been in a system buffer before the returned time.
  59. This takes account of the baudrate of the link. For transports
  60. that have no baudrate (such as USB) the time estimate may be
  61. less accurate.
  62. A return value of zero means the HAL does not support this API
  63. */
  64. uint64_t receive_time_constraint_us(uint16_t nbytes) override;
  65. private:
  66. uint8_t _portNumber;
  67. bool _connected = false; // true if a client has connected
  68. bool _use_send_recv = false;
  69. int _listen_fd; // socket we are listening on
  70. int _serial_port;
  71. static bool _console;
  72. bool _nonblocking_writes;
  73. ByteBuffer _readbuffer{16384};
  74. ByteBuffer _writebuffer{16384};
  75. // default multicast IP and port
  76. const char *mcast_ip_default = "239.255.145.50";
  77. const uint16_t mcast_port_default = 14550;
  78. const char *_uart_path;
  79. uint32_t _uart_baudrate;
  80. // IPv4 address of target for uartC
  81. const char *_tcp_client_addr;
  82. void _tcp_start_connection(uint16_t port, bool wait_for_connection);
  83. void _uart_start_connection(void);
  84. void _check_reconnect();
  85. void _tcp_start_client(const char *address, uint16_t port);
  86. void _udp_start_client(const char *address, uint16_t port);
  87. void _udp_start_multicast(const char *address, uint16_t port);
  88. void _check_connection(void);
  89. static bool _select_check(int );
  90. static void _set_nonblocking(int );
  91. bool set_speed(int speed);
  92. SITL_State *_sitlState;
  93. uint64_t _receive_timestamp;
  94. bool _is_udp;
  95. bool _packetise;
  96. uint16_t _mc_myport;
  97. uint32_t last_tick_us;
  98. };
  99. #endif