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