mavtables  0.2.1
MAVLink router and firewall.
Packet.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 <cstdint>
19 #include <memory>
20 #include <optional>
21 #include <ostream>
22 #include <string>
23 #include <vector>
24 
25 #include "MAVAddress.hpp"
26 #include "Packet.hpp"
27 
28 
29 /** Construct a packet.
30  *
31  * \param data Raw packet data.
32  */
33 Packet::Packet(std::vector<uint8_t> data)
34  : data_(std::move(data))
35 {
36 }
37 
38 
39 // GCC generates a seemingly uncallable destructor for pure virtual classes.
40 // Therefore, it must be excluded from test coverage.
41 // LCOV_EXCL_START
43 {
44 }
45 // LCOV_EXCL_STOP
46 
47 
48 /** Return the packet data.
49  *
50  * \returns The packet data as a vector of bytes.
51  */
52 const std::vector<uint8_t> &Packet::data() const
53 {
54  return data_;
55 }
56 
57 
58 /** Set the source connection of the packet.
59  *
60  * \param connection The source connection.
61  * \sa connection()
62  */
63 void Packet::connection(std::weak_ptr<Connection> connection)
64 {
65  connection_ = connection;
66 }
67 
68 
69 /** Get the source connection of the packet.
70  *
71  * \returns The source connection if set and it still exists, otherwise
72  * nullptr.
73  * \sa connection(std::weak_ptr<Connection>)
74  */
75 const std::shared_ptr<Connection> Packet::connection() const
76 {
77  return connection_.lock();
78 }
79 
80 
81 /** Equality comparison.
82  *
83  * Compares the raw packet data.
84  *
85  * \relates Packet
86  * \param lhs The left hand side packet.
87  * \param rhs The right hand side packet.
88  * \retval true if \p lhs and \p rhs have the same packet data.
89  * \retval false if \p lhs and \p rhs do not have the same packet data.
90  */
91 bool operator==(const Packet &lhs, const Packet &rhs)
92 {
93  return lhs.data() == rhs.data();
94 }
95 
96 
97 /** Inequality comparison.
98  *
99  * Compares the raw packet data.
100  *
101  * \relates Packet
102  * \param lhs The left hand side packet.
103  * \param rhs The right hand side packet.
104  * \retval true if \p lhs and \p rhs do not have the same packet data.
105  * \retval false if \p lhs and \p rhs have the same packet data.
106  */
107 bool operator!=(const Packet &lhs, const Packet &rhs)
108 {
109  return lhs.data() != rhs.data();
110 }
111 
112 
113 /** Print the packet to the given output stream.
114  *
115  * The format is "<Message Name> (#<Message ID>) from <Source Address> to
116  * <Destination Address> (v<Packet Version>)". However, "to <Destination
117  * Address>" is optional and will not be printed if the destination is the
118  * broadcast address 0.0.
119  *
120  * Some examples are:
121  * - `HEARTBEAT (#1) from 16.8 (v1.0)`
122  * - `PING (#4) from 128.4 to 16.8 (v2.0)`
123  * - `DATA_TRANSMISSION_HANDSHAKE (#130) from 16.8 (v2.0)`
124  * - `ENCAPSULATED_DATA (#131) from 128.4 (v2.0)`
125  *
126  * \relates Packet
127  * \param os The output stream to print to.
128  * \param packet The MAVLink packet to print.
129  * \returns The output stream.
130  */
131 std::ostream &operator<<(std::ostream &os, const Packet &packet)
132 {
133  // ID, name, and source.
134  os << packet.name() << " (#" << packet.id() << ")";
135  os << " from " << packet.source();
136 
137  // Destination.
138  if (auto dest = packet.dest())
139  {
140  os << " to " << dest.value();
141  }
142 
143  // Version.
144  os << " (v" << ((packet.version() & 0xFF00) >> 8) << "." <<
145  (packet.version() & 0x00FF) << ")";
146  return os;
147 }
bool operator!=(const Packet &lhs, const Packet &rhs)
Definition: Packet.cpp:107
virtual unsigned long id() const =0
Packet(const Packet &other)=default
const std::shared_ptr< Connection > connection() const
Definition: Packet.cpp:75
STL namespace.
virtual std::optional< MAVAddress > dest() const =0
virtual std::string name() const =0
std::ostream & operator<<(std::ostream &os, const Packet &packet)
Definition: Packet.cpp:131
bool operator==(const Packet &lhs, const Packet &rhs)
Definition: Packet.cpp:91
virtual MAVAddress source() const =0
virtual ~Packet()
Definition: Packet.cpp:42
virtual Version version() const =0
const std::vector< uint8_t > & data() const
Definition: Packet.cpp:52