mavtables  0.2.1
MAVLink router and firewall.
SerialPort.cpp
Go to the documentation of this file.
1 // MAVLink router and firewall.
2 // Copyright (C) 2018 Michael R. Shannon <mrshannon.aerospace@gmail.com>
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 
18 #include <algorithm>
19 #include <chrono>
20 #include <cstdint>
21 #include <iterator>
22 #include <ostream>
23 #include <utility>
24 #include <vector>
25 
26 #include "SerialPort.hpp"
27 
28 
29 // Placed here to avoid weak-vtables error.
30 // LCOV_EXCL_START
32 {
33 }
34 // LCOV_EXCL_STOP
35 
36 
37 /** Read data from the serial port.
38  *
39  * \note The \p timeout is not guaranteed to be up to nanosecond precision, the
40  * actual precision is up to the operating system's implementation but is
41  * guaranteed to have at least millisecond precision.
42  *
43  * \param timeout How long to wait for data to arrive on the serial port if
44  * there is not already data to read. The default is to not wait.
45  * \returns The data read from the serial port.
46  */
47 std::vector<uint8_t> SerialPort::read(const std::chrono::nanoseconds &timeout)
48 {
49  std::vector<uint8_t> vec;
50  read(std::back_inserter(vec), timeout);
51  return vec;
52 }
53 
54 
55 /** Read data from the serial port.
56  *
57  * \note The \p timeout is not guaranteed to be up to nanosecond precision, the
58  * actual precision is up to the operating system's implementation but is
59  * guaranteed to have at least millisecond precision.
60  *
61  * \param it A back insert iterator to read bytes into.
62  * \param timeout How long to wait for data to arrive on the serial port if
63  * there is not already data to read. The default is to not wait.
64  */
66  std::back_insert_iterator<std::vector<uint8_t>> it,
67  const std::chrono::nanoseconds &timeout)
68 {
69  auto vec = read(timeout);
70  std::copy(vec.begin(), vec.end(), it);
71 }
72 
73 
74 /** Write data to the serial port (blocking write).
75  *
76  * \param data The bytes to send.
77  */
78 void SerialPort::write(const std::vector<uint8_t> &data)
79 {
80  write(data.begin(), data.end());
81 }
82 
83 
84 /** Write data to the serial port (blocking write).
85  *
86  * \param first Iterator to first byte in range to send.
87  * \param last Iterator to one past the last byte to send.
88  */
90  std::vector<uint8_t>::const_iterator first,
91  std::vector<uint8_t>::const_iterator last)
92 {
93  std::vector<uint8_t> vec;
94  std::copy(first, last, std::back_inserter(vec));
95  write(vec);
96 }
97 
98 
99 /** Print the serial port to the given output stream.
100  *
101  * \param os The output stream to print to.
102  * \returns The output stream.
103  */
104 std::ostream &SerialPort::print_(std::ostream &os) const
105 {
106  os << "unknown serial port";
107  return os;
108 }
109 
110 
111 /** Print the given serial port to the given output stream.
112  *
113  * \note This is a polymorphic print, it will work on any child of \ref
114  * SerialPort even if the pointer/reference is to the base class \ref
115  * SerialPort.
116  *
117  * An example:
118  * ```
119  * serial {
120  * device /dev/ttyUSB0;
121  * baudrate 115200;
122  * flow_control yes;
123  * }
124  * ```
125  *
126  * The base \ref SerialPort class will print:
127  * ```
128  * unknown serial port
129  * ```
130 
131  * \relates SerialPort
132  * \param os The output stream to print to.
133  * \param serial_port The serial port (or any child of \ref SerialPort) to
134  * print.
135  * \returns The output stream.
136  */
137 std::ostream &operator<<(std::ostream &os, const SerialPort &serial_port)
138 {
139  return serial_port.print_(os);
140 }
virtual std::ostream & print_(std::ostream &os) const
Definition: SerialPort.cpp:104
virtual ~SerialPort()
Definition: SerialPort.cpp:31
std::ostream & operator<<(std::ostream &os, const Action &action)
Definition: Action.cpp:188
virtual std::vector< uint8_t > read(const std::chrono::nanoseconds &timeout=std::chrono::nanoseconds::zero())
Definition: SerialPort.cpp:47
virtual void write(const std::vector< uint8_t > &data)
Definition: SerialPort.cpp:78