1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
/*
* NMEAParser.h
*
* Created on: Aug 12, 2014
* Author: Cameron Karlsson
*/
#ifndef NMEAPARSER_H_
#define NMEAPARSER_H_
#include <nmeaparse/Event.h>
#include <string>
#include <functional>
#include <unordered_map>
#include <vector>
#include <cstdint>
#include <exception>
//read class definition for info
#define NMEA_PARSER_MAX_BUFFER_SIZE 2000
namespace nmea {
class NMEAParser;
class NMEASentence {
friend NMEAParser;
private:
bool isvalid;
public:
std::string text; //whole plaintext of the received command
std::string name; //name of the command
std::vector<std::string> parameters; //list of parameters from the command
std::string checksum;
uint8_t parsedChecksum;
uint8_t calculatedChecksum;
enum MessageID { // These ID's are according to NMEA standard.
Unknown = -1,
GGA = 0,
GLL = 1,
GSA = 2,
GSV = 3,
RMC = 4,
VTG = 5, // notice missing 6,7
ZDA = 8
};
public:
NMEASentence();
//NMEASentence(const NMEASentence& ref);
virtual ~NMEASentence();
bool checksumOK() const;
bool valid() const;
};
class NMEAParseError : public std::exception {
public:
std::string message;
NMEASentence nmea;
NMEAParseError(std::string msg);
NMEAParseError(std::string msg, NMEASentence n);
virtual ~NMEAParseError();
std::string what();
};
class NMEAParser {
private:
std::unordered_map<std::string, std::function<void(NMEASentence)>> eventTable;
std::string buffer;
bool fillingbuffer;
uint32_t maxbuffersize; //limit the max size if no newline ever comes... Prevents huge buffer string internally
void parseText (NMEASentence& nmea, std::string s); //fills the given NMEA sentence with the results of parsing the string.
void onInfo (NMEASentence& n, std::string s);
void onWarning (NMEASentence& n, std::string s);
void onError (NMEASentence& n, std::string s);
public:
NMEAParser();
virtual ~NMEAParser();
bool log;
Event<void(const NMEASentence&)> onSentence; // called every time parser receives any NMEA sentence
void setSentenceHandler(std::string cmdKey, std::function<void(const NMEASentence&)> handler); //one handler called for any named sentence where name is the "cmdKey"
// Byte streaming functions
void readByte (uint8_t b);
void readBuffer (uint8_t* b, uint32_t size);
void readLine (std::string line);
// This function expects the data to be a single line with an actual sentence in it, else it throws an error.
void readSentence (std::string cmd); // called when parser receives a sentence from the byte stream. Can also be called by user to inject sentences.
static uint8_t calculateChecksum(std::string); // returns checksum of string -- XOR
};
}
#endif /* NMEAPARSER_H_ */
|