Line data Source code
1 : #pragma once 2 : 3 : #include <AP_HAL/utility/OwnPtr.h> 4 : #include <AP_HAL/utility/RingBuffer.h> 5 : 6 : #include "AP_HAL_Linux.h" 7 : #include "SerialDevice.h" 8 : #include "Semaphores.h" 9 : 10 : namespace Linux { 11 : 12 : class UARTDriver : public AP_HAL::UARTDriver { 13 : public: 14 : UARTDriver(bool default_console); 15 : 16 : static UARTDriver *from(AP_HAL::UARTDriver *uart) { 17 : return static_cast<UARTDriver*>(uart); 18 : } 19 : 20 : bool is_initialized() override; 21 : bool tx_pending() override; 22 : 23 : uint32_t txspace() override; 24 : 25 : void set_device_path(const char *path); 26 : 27 : bool _write_pending_bytes(void); 28 : virtual void _timer_tick(void) override; 29 : 30 0 : virtual enum flow_control get_flow_control(void) override 31 : { 32 0 : return _device->get_flow_control(); 33 : } 34 : 35 : void configure_parity(uint8_t v) override; 36 : 37 0 : virtual void set_flow_control(enum flow_control flow_control_setting) override 38 : { 39 0 : _device->set_flow_control(flow_control_setting); 40 0 : } 41 : 42 : /* 43 : return timestamp estimate in microseconds for when the start of 44 : a nbytes packet arrived on the uart. This should be treated as a 45 : time constraint, not an exact time. It is guaranteed that the 46 : packet did not start being received after this time, but it 47 : could have been in a system buffer before the returned time. 48 : 49 : This takes account of the baudrate of the link. For transports 50 : that have no baudrate (such as USB) the time estimate may be 51 : less accurate. 52 : 53 : A return value of zero means the HAL does not support this API 54 : */ 55 : uint64_t receive_time_constraint_us(uint16_t nbytes) override; 56 : 57 : uint32_t bw_in_bytes_per_second() const override; 58 : 59 0 : virtual uint32_t get_baud_rate() const override { return _baudrate; } 60 : 61 : private: 62 : AP_HAL::OwnPtr<SerialDevice> _device; 63 : bool _console; 64 : volatile bool _in_timer; 65 : uint16_t _base_port; 66 : uint32_t _baudrate; 67 : char *_ip; 68 : char *_flag; 69 : bool _connected; // true if a client has connected 70 : bool _packetise; // true if writes should try to be on mavlink boundaries 71 : 72 : void _allocate_buffers(uint16_t rxS, uint16_t txS); 73 : void _deallocate_buffers(); 74 : 75 : AP_HAL::OwnPtr<SerialDevice> _parseDevicePath(const char *arg); 76 : 77 : // timestamp for receiving data on the UART, avoiding a lock 78 : uint64_t _receive_timestamp[2]; 79 : uint8_t _receive_timestamp_idx; 80 : 81 : protected: 82 : const char *device_path; 83 : volatile bool _initialised; 84 : 85 : // we use in-task ring buffers to reduce the system call cost 86 : // of ::read() and ::write() in the main loop 87 : ByteBuffer _readbuf{0}; 88 : ByteBuffer _writebuf{0}; 89 : 90 : virtual int _write_fd(const uint8_t *buf, uint16_t n); 91 : virtual int _read_fd(uint8_t *buf, uint16_t n); 92 : 93 : Linux::Semaphore _write_mutex; 94 : 95 : bool _discard_input() override; 96 : void _begin(uint32_t b, uint16_t rxS, uint16_t txS) override; 97 : void _end() override; 98 : void _flush() override; 99 : uint32_t _available() override; 100 : size_t _write(const uint8_t *buffer, size_t size) override; 101 : ssize_t _read(uint8_t *buffer, uint16_t count) override WARN_IF_UNUSED; 102 : }; 103 : 104 : }