Goby3  3.1.5a
2024.05.23
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 
40 namespace goby
41 {
42 namespace util
43 {
44 // simple exception class
45 class bad_nmea_sentence : public std::runtime_error
46 {
47  public:
48  bad_nmea_sentence(const std::string& s) : std::runtime_error(s) {}
49 };
50 
51 class NMEASentence : public std::vector<std::string>
52 {
53  public:
54  enum strategy
55  {
59  };
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 <<
109 inline std::ostream& operator<<(std::ostream& out, const goby::util::NMEASentence& nmea)
110 {
111  out << nmea.message();
112  return out;
113 }
114 
115 #endif
goby::util::NMEASentence::VALIDATE
@ VALIDATE
Definition: nmea_sentence.h:57
goby::util::NMEASentence::sentence_id
std::string sentence_id() const
Definition: nmea_sentence.h:77
goby
The global namespace for the Goby project.
Definition: acomms_constants.h:33
as.h
goby::util::NMEASentence::checksum
static unsigned char checksum(const std::string &s)
goby::util::NMEASentence::talker_id
std::string talker_id() const
Definition: nmea_sentence.h:74
goby::util::NMEASentence::REQUIRE
@ REQUIRE
Definition: nmea_sentence.h:58
goby::util::NMEASentence::message_cr_nl
std::string message_cr_nl() const
Definition: nmea_sentence.h:71
goby::util::NMEASentence::push_back
void push_back(T t)
Definition: nmea_sentence.h:81
goby::time::str
std::string str(TimeType value=SystemClock::now< TimeType >())
Returns the provided time (or current time if omitted) as a human-readable string.
Definition: convert.h:191
goby::util::NMEASentence::IGNORE
@ IGNORE
Definition: nmea_sentence.h:56
goby::util::bad_nmea_sentence
Definition: nmea_sentence.h:45
goby::util::NMEASentence::NMEASentence
NMEASentence()=default
goby::util::bad_nmea_sentence::bad_nmea_sentence
bad_nmea_sentence(const std::string &s)
Definition: nmea_sentence.h:48
goby::util::NMEASentence::push_back
void push_back(const std::string &str)
Definition: nmea_sentence.h:85
goby::util::NMEASentence::as
T as(int i) const
Definition: nmea_sentence.h:79
goby::util::NMEASentence::message_no_cs
std::string message_no_cs() const
goby::util::NMEASentence::strategy
strategy
Definition: nmea_sentence.h:54
httplib::detail::split
void split(const char *b, const char *e, char d, std::function< void(const char *, const char *)> fn)
Definition: httplib.h:2494
goby::util::NMEASentence::enforce_talker_length
static bool enforce_talker_length
Definition: nmea_sentence.h:103
goby::util::NMEASentence::message
std::string message() const
goby::util::NMEASentence
Definition: nmea_sentence.h:51
operator<<
std::ostream & operator<<(std::ostream &out, const goby::util::NMEASentence &nmea)
Definition: nmea_sentence.h:109