mavtables  0.2.1
MAVLink router and firewall.
PacketParser.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 PACKETPARSER_HPP_
19 #define PACKETPARSER_HPP_
20 
21 
22 #include <memory>
23 
24 #include "Packet.hpp"
25 #include "PacketVersion1.hpp"
26 #include "PacketVersion2.hpp"
27 
28 
29 /** A MAVLink packet parser.
30  *
31  * Parses wire protocol bytes into a MAVLink \ref Packet.
32  */
34 {
35  public:
36  PacketParser();
37  PacketParser(const PacketParser &other) = delete;
38  PacketParser(PacketParser &&other) = delete;
39  size_t bytes_parsed() const;
40  void clear();
41  std::unique_ptr<Packet> parse_byte(uint8_t byte);
42  PacketParser &operator=(const PacketParser &other) = delete;
43  PacketParser &operator=(PacketParser &&other) = delete;
44 
45  private:
46  // Types
47  /** Packet parser states.
48  */
49  enum State
50  {
51  WAITING_FOR_START_BYTE, //!< Waiting for a magic start byte.
52  WAITING_FOR_HEADER, //!< Waiting for complete header.
53  WAITING_FOR_PACKET //!< Waitinf for complete packet.
54  };
55  // Variables
56  std::vector<uint8_t> buffer_;
57  PacketParser::State state_;
58  Packet::Version version_;
59  size_t bytes_remaining_;
60  // Methods
61  void waiting_for_start_byte_(uint8_t byte);
62  void waiting_for_header_(uint8_t byte);
63  std::unique_ptr<Packet> waiting_for_packet_(uint8_t byte);
64 };
65 
66 
67 #endif // PACKETPARSER_HPP_
size_t bytes_parsed() const
Version
Definition: Packet.hpp:47
PacketParser & operator=(const PacketParser &other)=delete
std::unique_ptr< Packet > parse_byte(uint8_t byte)