| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /**************************************************************************** | ||
| 2 | * | ||
| 3 | * Copyright (c) 2014 MAVlink Development Team. All rights reserved. | ||
| 4 | * Author: Trent Lukaczyk, <aerialhedgehog@gmail.com> | ||
| 5 | * Jaycee Lock, <jaycee.lock@gmail.com> | ||
| 6 | * Lorenz Meier, <lm@inf.ethz.ch> | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * | ||
| 12 | * 1. Redistributions of source code must retain the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer. | ||
| 14 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 15 | * notice, this list of conditions and the following disclaimer in | ||
| 16 | * the documentation and/or other materials provided with the | ||
| 17 | * distribution. | ||
| 18 | * 3. Neither the name PX4 nor the names of its contributors may be | ||
| 19 | * used to endorse or promote products derived from this software | ||
| 20 | * without specific prior written permission. | ||
| 21 | * | ||
| 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| 23 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| 24 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
| 25 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
| 26 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
| 27 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
| 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | ||
| 29 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | ||
| 30 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||
| 32 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 33 | * POSSIBILITY OF SUCH DAMAGE. | ||
| 34 | * | ||
| 35 | ****************************************************************************/ | ||
| 36 | |||
| 37 | /** | ||
| 38 | * @file serial_port.hpp | ||
| 39 | * | ||
| 40 | * @brief Serial interface definition | ||
| 41 | * | ||
| 42 | * Functions for opening, closing, reading and writing via serial ports | ||
| 43 | * | ||
| 44 | * @author Trent Lukaczyk, <aerialhedgehog@gmail.com> | ||
| 45 | * @author Jaycee Lock, <jaycee.lock@gmail.com> | ||
| 46 | * @author Lorenz Meier, <lm@inf.ethz.ch> | ||
| 47 | * | ||
| 48 | */ | ||
| 49 | |||
| 50 | #pragma once | ||
| 51 | |||
| 52 | #if __linux__ || __APPLE__ | ||
| 53 | // ------------------------------------------------------------------------------ | ||
| 54 | // Includes | ||
| 55 | // ------------------------------------------------------------------------------ | ||
| 56 | |||
| 57 | #include <cstdlib> | ||
| 58 | #include <unistd.h> // UNIX standard function definitions | ||
| 59 | #include <fcntl.h> // File control definitions | ||
| 60 | #include <termios.h> // POSIX terminal control definitions | ||
| 61 | #include <pthread.h> // This uses POSIX Threads | ||
| 62 | |||
| 63 | #include <mavlink/common/mavlink.h> | ||
| 64 | |||
| 65 | #include "generic_port.hpp" | ||
| 66 | |||
| 67 | // ------------------------------------------------------------------------------ | ||
| 68 | // Defines | ||
| 69 | // ------------------------------------------------------------------------------ | ||
| 70 | |||
| 71 | // The following two non-standard baudrates should have been defined by the system | ||
| 72 | // If not, just fallback to number | ||
| 73 | #ifndef B460800 | ||
| 74 | #define B460800 460800 ///< Baudrate | ||
| 75 | #endif | ||
| 76 | |||
| 77 | #ifndef B921600 | ||
| 78 | #define B921600 921600 ///< Baudrate | ||
| 79 | #endif | ||
| 80 | |||
| 81 | // ------------------------------------------------------------------------------ | ||
| 82 | // Prototypes | ||
| 83 | // ------------------------------------------------------------------------------ | ||
| 84 | |||
| 85 | // class Serial_Port; | ||
| 86 | |||
| 87 | // ---------------------------------------------------------------------------------- | ||
| 88 | // Serial Port Manager Class | ||
| 89 | // ---------------------------------------------------------------------------------- | ||
| 90 | /* | ||
| 91 | * Serial Port Class | ||
| 92 | * | ||
| 93 | * This object handles the opening and closing of the offboard computer's | ||
| 94 | * serial port over which we'll communicate. It also has methods to write | ||
| 95 | * a byte stream buffer. MAVlink is not used in this object yet, it's just | ||
| 96 | * a serialization interface. To help with read and write pthreading, it | ||
| 97 | * gaurds any port operation with a pthread mutex. | ||
| 98 | */ | ||
| 99 | |||
| 100 | /// @brief Serial Port Class | ||
| 101 | class Serial_Port : public Generic_Port | ||
| 102 | { | ||
| 103 | public: | ||
| 104 | /// @brief Default constructor | ||
| 105 | Serial_Port(); | ||
| 106 | /// @brief Constructor | ||
| 107 | Serial_Port(const char* uart_name_, int baudrate_); | ||
| 108 | /// @brief Destructor | ||
| 109 | ~Serial_Port() override; | ||
| 110 | /// @brief Copy constructor | ||
| 111 | Serial_Port(const Serial_Port&) = delete; | ||
| 112 | /// @brief Move constructor | ||
| 113 | Serial_Port(Serial_Port&&) = delete; | ||
| 114 | /// @brief Copy assignment operator | ||
| 115 | Serial_Port& operator=(const Serial_Port&) = delete; | ||
| 116 | /// @brief Move assignment operator | ||
| 117 | Serial_Port& operator=(Serial_Port&&) = delete; | ||
| 118 | |||
| 119 | int read_message(mavlink_message_t& message) override; | ||
| 120 | int write_message(const mavlink_message_t& message) override; | ||
| 121 | |||
| 122 | /// @brief is running | ||
| 123 | ✗ | bool is_running() override | |
| 124 | { | ||
| 125 | ✗ | return is_open; | |
| 126 | } | ||
| 127 | |||
| 128 | /// @brief Start | ||
| 129 | void start() override; | ||
| 130 | |||
| 131 | /// @brief Stop | ||
| 132 | void stop() override; | ||
| 133 | |||
| 134 | private: | ||
| 135 | int fd; ///< fd? | ||
| 136 | mavlink_status_t lastStatus; ///< Last Mavlink status | ||
| 137 | pthread_mutex_t lock; ///< pthread mutex lock | ||
| 138 | |||
| 139 | /// @brief Initialize defaults | ||
| 140 | void initialize_defaults(); | ||
| 141 | |||
| 142 | bool debug; ///< debug flag | ||
| 143 | const char* uart_name; ///< uart name | ||
| 144 | int baudrate; ///< Baudrate | ||
| 145 | bool is_open; ///< Is open | ||
| 146 | |||
| 147 | /// @brief Open port | ||
| 148 | /// @param[in] port Port to open | ||
| 149 | int _open_port(const char* port); | ||
| 150 | |||
| 151 | /// @brief Setup of port | ||
| 152 | /// @param[in] baud Baud rate | ||
| 153 | /// @param[in] data_bits Data bits | ||
| 154 | /// @param[in] stop_bits Stop bits | ||
| 155 | /// @param[in] parity Parity | ||
| 156 | /// @param[in] hardware_control Hardware control | ||
| 157 | [[nodiscard]] bool _setup_port(int baud, int data_bits, int stop_bits, bool parity, bool hardware_control) const; | ||
| 158 | |||
| 159 | /// @brief Read port | ||
| 160 | /// @param[in] cp Port to read | ||
| 161 | int _read_port(uint8_t& cp); | ||
| 162 | |||
| 163 | /// @brief Write port | ||
| 164 | /// @param[in] buf Buffer to write | ||
| 165 | /// @param[in] len Length of buffer to write | ||
| 166 | int _write_port(char* buf, unsigned len); | ||
| 167 | }; | ||
| 168 | |||
| 169 | #endif | ||
| 170 |