mavtables  0.2.1
MAVLink router and firewall.
SerialPort.hpp
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 #ifndef SERIALPORT_HPP_
19 #define SERIALPORT_HPP_
20 
21 
22 #include <chrono>
23 #include <iterator>
24 #include <ostream>
25 #include <string>
26 #include <vector>
27 
28 
29 /** The base class of all serial port classes.
30  *
31  * This provides an abstraction of serial ports across operating systems.
32  *
33  * \warning This class should be treated as pure virtual and should never be
34  * instantiated.
35  *
36  * \warning Either \ref read(const std::chrono::nanoseconds &) or
37  * read(std::back_insert_iterator<std::vector<uint8_t>>,const std::chrono::nanoseconds &)
38  * must be overridden in child classes to avoid infinite recursion.
39  *
40  * \warning Either \ref write(const std::vector<uint8_t> &data) or
41  * write(std::vector<uint8_t>::const_iterator,std::vector<uint8_t>::const_iterator)
42  * must be overridden in child classes to avoid infinite recursion.
43  */
45 {
46  public:
47  /** Parity options.
48  */
49  enum Parity
50  {
51  NONE, //!< No parity.
52  ODD, //!< Odd parity, must have odd number of set bits.
53  EVEN, //!< Even parity, must have even number of set bits.
54  MARK, //!< Fill parity bit with 1.
55  SPACE //!< Fill parity bit with 0.
56  };
57  /** Feature bitflags.
58  */
59  enum Feature
60  {
61  DEFAULT = 0, //!< No special features.
62  FLOW_CONTROL = 1 << 0 //!< Enable flow control.
63  };
64  virtual ~SerialPort();
65  virtual std::vector<uint8_t> read(
66  const std::chrono::nanoseconds &timeout =
67  std::chrono::nanoseconds::zero());
68  virtual void read(
69  std::back_insert_iterator<std::vector<uint8_t>> it,
70  const std::chrono::nanoseconds &timeout =
71  std::chrono::nanoseconds::zero());
72  virtual void write(const std::vector<uint8_t> &data);
73  virtual void write(
74  std::vector<uint8_t>::const_iterator first,
75  std::vector<uint8_t>::const_iterator last);
76 
77  friend std::ostream &operator<<(
78  std::ostream &os, const SerialPort &serial_port);
79 
80  protected:
81  virtual std::ostream &print_(std::ostream &os) const;
82 };
83 
84 
85 std::ostream &operator<<(std::ostream &os, const SerialPort &serial_port);
86 
87 
88 #endif // SERIALPORT_HPP_
No special features.
Definition: SerialPort.hpp:61
Odd parity, must have odd number of set bits.
Definition: SerialPort.hpp:52
virtual std::ostream & print_(std::ostream &os) const
Definition: SerialPort.cpp:104
No parity.
Definition: SerialPort.hpp:51
Enable flow control.
Definition: SerialPort.hpp:62
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
Fill parity bit with 1.
Definition: SerialPort.hpp:54
virtual void write(const std::vector< uint8_t > &data)
Definition: SerialPort.cpp:78
friend std::ostream & operator<<(std::ostream &os, const SerialPort &serial_port)
Definition: SerialPort.cpp:137
Fill parity bit with 0.
Definition: SerialPort.hpp:55
Even parity, must have even number of set bits.
Definition: SerialPort.hpp:53