Goby Underwater Autonomy Project
Series: 1.1, revision: 163, released on 2013-02-06 14:23:27 -0500
|
00001 // copyright 2009 t. schneider tes@mit.edu 00002 // 2010 c. murphy cmurphy@whoi.edu 00003 // 00004 // this file is part of serial, a library for handling serial 00005 // communications. 00006 // 00007 // This program is free software: you can redistribute it and/or modify 00008 // it under the terms of the GNU General Public License as published by 00009 // the Free Software Foundation, either version 3 of the License, or 00010 // (at your option) any later version. 00011 // 00012 // This software is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 // 00017 // You should have received a copy of the GNU General Public License 00018 // along with this software. If not, see <http://www.gnu.org/licenses/>. 00019 00020 #ifndef NMEASentence20091211H 00021 #define NMEASentence20091211H 00022 00023 #include <exception> 00024 #include <stdexcept> 00025 #include <vector> 00026 #include <sstream> 00027 00028 #include <boost/foreach.hpp> 00029 #include <boost/algorithm/string.hpp> 00030 00031 #include "goby/util/string.h" 00032 00033 namespace goby 00034 { 00035 namespace util 00036 { 00037 // simple exception class 00038 class bad_nmea_sentence : public std::runtime_error 00039 { 00040 public: 00041 bad_nmea_sentence(const std::string& s) 00042 : std::runtime_error(s) 00043 { } 00044 }; 00045 00046 00047 class NMEASentence : public std::vector<std::string> 00048 { 00049 public: 00050 enum strategy { IGNORE, VALIDATE, REQUIRE }; 00051 00052 NMEASentence() {} 00053 NMEASentence(std::string s, strategy cs_strat = VALIDATE); 00054 00055 // Bare message, no checksum or \r\n 00056 std::string message_no_cs() const; 00057 00058 // Includes checksum, but no \r\n 00059 std::string message() const; 00060 00061 // Includes checksum and \r\n 00062 std::string message_cr_nl() const { return message() + "\r\n"; } 00063 00064 // first two talker (CC) 00065 std::string talker_id() const 00066 { return empty() ? "" : front().substr(1, 2); } 00067 00068 // last three (CFG) 00069 std::string sentence_id() const 00070 { return empty() ? "" : front().substr(3); } 00071 00072 template<typename T> 00073 T as(int i) const { return goby::util::as<T>(at(i)); } 00074 00075 template<typename T> 00076 void push_back(T t) 00077 { push_back(goby::util::as<std::string>(t)); } 00078 00079 // necessary when pushing back string "foo,bar" that contain 00080 // commas 00081 void push_back(const std::string& str) 00082 { 00083 std::vector<std::string> vec; 00084 boost::split(vec, str, boost::is_any_of(",")); 00085 00086 BOOST_FOREACH(const std::string& s, vec) 00087 std::vector<std::string>::push_back(s); 00088 } 00089 00090 00091 static unsigned char checksum(const std::string& s); 00092 }; 00093 } 00094 } 00095 00096 // overloaded << 00097 inline std::ostream& operator<< (std::ostream& out, const goby::util::NMEASentence& nmea) 00098 { out << nmea.message(); return out; } 00099 00100 #endif