AsciiMassage
MassageParser.h
1 /* * Copyright (C) 2017 Thomas O. Fredericks, Sofian Audry
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy of
4  * this software and associated documentation files (the "Software"), to deal in
5  * the Software without restriction, including without limitation the rights to
6  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7  * the Software, and to permit persons to whom the Software is furnished to do so,
8  * subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in all
11  * copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 #ifndef MassageParser_h
22 #define MassageParser_h
23 
24 #if defined(ARDUINO) && ARDUINO >= 100
25 #include "Arduino.h"
26 #else
27 #include "WProgram.h"
28 #endif
29 
30 #ifndef MASSAGE_PARSER_BUFFERSIZE
31 
35 #define MASSAGE_PARSER_BUFFERSIZE 256
36 #endif
37 
38 
41 {
42 public:
43  typedef void (*callbackFunction)(void);
44 
47  flush();
48  }
49 
50  // Virtual destructor.
51  virtual ~MassageParser() {}
52 
58  [[deprecated("Please us parseStream() instead")]]
59  virtual bool parse(int data, callbackFunction callback = 0)
60  {
61  // Flush if needed.
62  if ( _needToFlush) {
63  flush();
64  }
65 
66  // Read stream.
67  if ( _decode(data) ) {
68  _needToFlush = true;
69 
70  // Call optional callback.
71  if (callback)
72  callback();
73 
74  return true;
75  }
76 
77  return false;
78  }
79 
85  virtual bool parseStream(Stream* stream, callbackFunction callback = 0) {
86  while ( stream->available() ) {
87  if ( parse( stream->read() , callback ) ) {
88  return true;
89  }
90  }
91  return false;
92  }
93 
95  virtual void flush() {
96  _needToFlush = false;
97  _messageSize = 0;
98  }
99 
101  virtual bool fullMatch(const char* address)
102  {
103  // Verity if address matches beginning of buffer.
104  bool matches = (strcmp((const char*) _buffer, address) == 0);
105  return matches;
106  }
107 
112  virtual bool dispatch(const char* address, callbackFunction callback) {
113  // Verify if address matches beginning of buffer.
114  if ( fullMatch(address) ) {
115  callback();
116  return true;
117  }
118  return false;
119  }
120 
122  virtual int8_t nextByte(bool* error=0) = 0;
123 
125  virtual int16_t nextInt(bool* error=0) = 0;
126 
128  virtual int32_t nextLong(bool* error=0) = 0;
129 
131  virtual float nextFloat(bool* error=0) = 0;
132 
134  virtual int nextString(char* receivedString, int bufferLength) = 0;
135 
136 protected:
137  // Decodes a single value read from the serial stream (returns true if message is complete).
138  virtual bool _decode(int serialByte) = 0;
139 
140 /*
141  void parseStream(Stream* stream, callbackFunction callback)
142  {
143  while ( stream->available() ) {
144  // PARSE INPUT AND EXECUTRE massageReceived IF A COMPLETE MASSAGE IS RECEIVED
145  parse( stream->read() , callback );
146  }
147  }
148 */
149 
150  // Writes single byte to buffer (returns false if buffer is full and cannot be written to).
151  bool _store(uint8_t value)
152  {
153  if (_messageSize >= MASSAGE_PARSER_BUFFERSIZE)
154  return false;
155 
156  _buffer[_messageSize++] = value;
157  return true;
158  }
159 
160  // True if message has ended (internal use).
161  bool _needToFlush;
162 
163  // Current size of message in buffer.
164  size_t _messageSize;
165 
166  // Buffer that holds the data for current message to be sent.
167  uint8_t _buffer[MASSAGE_PARSER_BUFFERSIZE];
168 };
169 
170 
171 
172 
173 
174 
175 #endif
virtual int8_t nextByte(bool *error=0)=0
Reads next byte.
virtual int16_t nextInt(bool *error=0)=0
Reads next int.
virtual int nextString(char *receivedString, int bufferLength)=0
Reads next string.
virtual bool parse(int data, callbackFunction callback=0)
Definition: MassageParser.h:59
Main MassageParser abstract class.
Definition: MassageParser.h:40
virtual void flush()
Flushes current message in buffer (if any).
Definition: MassageParser.h:95
virtual int32_t nextLong(bool *error=0)=0
Reads next long.
MassageParser()
Constructor.
Definition: MassageParser.h:46
virtual bool dispatch(const char *address, callbackFunction callback)
Definition: MassageParser.h:112
virtual bool fullMatch(const char *address)
Return true if current message matches "address".
Definition: MassageParser.h:101
virtual float nextFloat(bool *error=0)=0
Reads next float.
virtual bool parseStream(Stream *stream, callbackFunction callback=0)
Definition: MassageParser.h:85