Goby3 3.2.3
2025.05.13
Loading...
Searching...
No Matches
nmea_sentence.h
Go to the documentation of this file.
1// Copyright 2009-2021:
2// GobySoft, LLC (2013-)
3// Massachusetts Institute of Technology (2007-2014)
4// Community contributors (see AUTHORS file)
5// File authors:
6// Toby Schneider <toby@gobysoft.org>
7//
8//
9// This file is part of the Goby Underwater Autonomy Project Libraries
10// ("The Goby Libraries").
11//
12// The Goby Libraries are free software: you can redistribute them and/or modify
13// them under the terms of the GNU Lesser General Public License as published by
14// the Free Software Foundation, either version 2.1 of the License, or
15// (at your option) any later version.
16//
17// The Goby Libraries are distributed in the hope that they will be useful,
18// but WITHOUT ANY WARRANTY; without even the implied warranty of
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20// GNU Lesser General Public License for more details.
21//
22// You should have received a copy of the GNU Lesser General Public License
23// along with Goby. If not, see <http://www.gnu.org/licenses/>.
24
25#ifndef GOBY_UTIL_LINEBASEDCOMMS_NMEA_SENTENCE_H
26#define GOBY_UTIL_LINEBASEDCOMMS_NMEA_SENTENCE_H
27
28#include <algorithm> // for max
29#include <memory> // for allocator_trait...
30#include <sstream> // for ostream
31#include <stdexcept> // for runtime_error
32#include <string> // for string, operator+
33#include <vector> // for vector
34
35#include <boost/algorithm/string/classification.hpp> // for is_any_ofF, is_...
36#include <boost/algorithm/string/split.hpp> // for split
37
38#include "goby/util/as.h" // for as
39
40namespace goby
41{
42namespace util
43{
44// simple exception class
45class bad_nmea_sentence : public std::runtime_error
46{
47 public:
48 bad_nmea_sentence(const std::string& s) : std::runtime_error(s) {}
49};
50
51class NMEASentence : public std::vector<std::string>
52{
53 public:
60
61 NMEASentence() = default;
62 NMEASentence(std::string s, strategy cs_strat = VALIDATE);
63
64 // Bare message, no checksum or \r\n
65 std::string message_no_cs() const;
66
67 // Includes checksum, but no \r\n
68 std::string message() const;
69
70 // Includes checksum and \r\n
71 std::string message_cr_nl() const { return message() + "\r\n"; }
72
73 // first two talker (CC)
74 std::string talker_id() const { return empty() ? "" : front().substr(1, 2); }
75
76 // last three (CFG)
77 std::string sentence_id() const { return empty() ? "" : front().substr(3); }
78
79 template <typename T> T as(int i) const { return goby::util::as<T>(at(i)); }
80
81 template <typename T> void push_back(T t) { push_back(goby::util::as<std::string>(t)); }
82
83 // necessary when pushing back string "foo,bar" that contain
84 // commas
85 void push_back(const std::string& str)
86 {
87 if (str.find(',') == std::string::npos)
88 {
89 std::vector<std::string>::push_back(str);
90 return;
91 }
92 else
93 {
94 std::vector<std::string> vec;
95 boost::split(vec, str, boost::is_any_of(","));
96
97 for (const std::string& s : vec) std::vector<std::string>::push_back(s);
98 }
99 }
100
101 static unsigned char checksum(const std::string& s);
102
104};
105} // namespace util
106} // namespace goby
107
108// overloaded <<
109inline std::ostream& operator<<(std::ostream& out, const goby::util::NMEASentence& nmea)
110{
111 out << nmea.message();
112 return out;
113}
114
115#endif
NMEASentence(std::string s, strategy cs_strat=VALIDATE)
std::string message() const
static unsigned char checksum(const std::string &s)
std::string sentence_id() const
std::string message_cr_nl() const
static bool enforce_talker_length
std::string talker_id() const
std::string message_no_cs() const
void push_back(const std::string &str)
bad_nmea_sentence(const std::string &s)
The global namespace for the Goby project.
STL namespace.