Line data Source code
1 : /* 2 : This program is free software: you can redistribute it and/or modify 3 : it under the terms of the GNU General Public License as published by 4 : the Free Software Foundation, either version 3 of the License, or 5 : (at your option) any later version. 6 : 7 : This program is distributed in the hope that it will be useful, 8 : but WITHOUT ANY WARRANTY; without even the implied warranty of 9 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 : GNU General Public License for more details. 11 : 12 : You should have received a copy of the GNU General Public License 13 : along with this program. If not, see <http://www.gnu.org/licenses/>. 14 : */ 15 : /* 16 : base class for serially-attached simulated devices 17 : */ 18 : 19 : #pragma once 20 : 21 : #include "SIM_config.h" 22 : 23 : #include <unistd.h> 24 : #include <AP_HAL/utility/RingBuffer.h> 25 : 26 : #if AP_SIM_SERIALDEVICE_NETWORK_ENABLED 27 : #include <AP_HAL/utility/Socket_native.h> 28 : #endif // AP_SIM_SERIALDEVICE_NETWORK_ENABLED 29 : 30 : namespace SITL { 31 : 32 : class SerialDevice { 33 : public: 34 : 35 : SerialDevice(uint16_t tx_bufsize=512, uint16_t rx_bufsize=512); 36 : 37 : 38 : // methods for autopilot to use to talk to device: 39 : ssize_t read_from_device(char *buffer, size_t size) const; 40 : ssize_t write_to_device(const char *buffer, size_t size) const; 41 14933 : void set_autopilot_baud(uint32_t baud) { autopilot_baud = baud; } 42 : 43 : // methods for simulated device to use: 44 : ssize_t read_from_autopilot(char *buffer, size_t size) const; 45 : virtual ssize_t write_to_autopilot(const char *buffer, size_t size) const; 46 13460733 : virtual uint32_t device_baud() const { return 0; } // 0 meaning unset 47 : 48 : #if AP_SIM_SERIALDEVICE_NETWORK_ENABLED 49 : // attach this device to a TCP server socket rather than to a 50 : // simulated serial port. This simulates a device which the 51 : // autopilot reaches over the network (e.g. via a NET_Pn port) 52 : // rather than over one of its serial ports. Returns true on success 53 : bool listen_on_tcp_port(uint16_t port) WARN_IF_UNUSED; 54 : 55 : // true if this device is attached to the autopilot via a network 56 : // socket rather than via a simulated serial port 57 2761 : bool is_network_attached() const { return listener != nullptr; } 58 : 59 : // move bytes between the network socket and this device. Does 60 : // nothing if the device is not attached to a socket 61 : void network_update(); 62 : #else 63 : bool is_network_attached() const { return false; } 64 : #endif // AP_SIM_SERIALDEVICE_NETWORK_ENABLED 65 : 66 : protected: 67 : 68 : class SIM *_sitl; 69 : 70 : ByteBuffer *to_autopilot; 71 : ByteBuffer *from_autopilot; 72 : 73 : bool init_sitl_pointer() WARN_IF_UNUSED; 74 : 75 : private: 76 : 77 : bool is_match_baud(void) const; 78 : 79 : // baudrate the autopilot has this device open at; zero if the 80 : // device is not attached to a simulated serial port 81 : uint32_t autopilot_baud; 82 : 83 : #if AP_SIM_SERIALDEVICE_NETWORK_ENABLED 84 : SocketAPM_native *listener = nullptr; // socket the autopilot connects to, nullptr if serially attached 85 : SocketAPM_native *sock = nullptr; // socket to the connected autopilot, nullptr if not connected 86 : #endif // AP_SIM_SERIALDEVICE_NETWORK_ENABLED 87 : 88 : ssize_t corrupt_transfer(char *buffer, const ssize_t ret, const size_t size) const; 89 : }; 90 : 91 : }