-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMsgParser.cpp
More file actions
477 lines (363 loc) · 15.1 KB
/
MsgParser.cpp
File metadata and controls
477 lines (363 loc) · 15.1 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/* MsgParser
* Ilya Brutman
*
*/
#include <avr/pgmspace.h>
#include <inttypes.h>
#include <string.h>
#include <stdlib.h>
#include "MsgParser.h"
//===============================================================================
// Constructor
//===============================================================================
MsgParser::MsgParser()
{
m_BufferWriteIndex = 0;
m_pRead = m_pInputBuffer;
m_pMsgParserCommand = NULL;
m_msgStartByte = '/';
m_msgEndByte = '\r'; //by default, use the carriage return as the end byte
m_useStartByte = false;
m_state = WAITING_FOR_START_BYTE; //This just needs to be some valid value, for now.
m_pFuncTable = NULL;
m_funcTableLength = 0;
m_pCmdNotFoundFunc = NULL;
memset(m_pInputBuffer, NULL, MSGPARSER_INPUT_BUFFER_SIZE);
memset(m_pDescBuf, NULL, MSGPARSER_DESCRIPTION_SIZE);
// Call our useStartByteSet function to set m_state to the correct state.
// Our state will be based on whether we are using the start byte or not.
useStartByteSet(m_useStartByte);
}// end constructor
//================================================================================
// FUNCTION: void setTable(...)
//
// DESCRIPTION: sets a new function table to use
//
// INPUT: *newFunctTable - pointer to the function table to use
// newFunctTableLength - number of entries in that table
//
// RETURNS: Nothing
//================================================================================
void MsgParser::setTable(const FuncEntry_t* newFunctTable, uint8_t newFunctTableLength)
{
if(newFunctTable != NULL)
{
m_pFuncTable = (FuncEntry_t*)newFunctTable;
m_funcTableLength = newFunctTableLength;
}
}// end setTable
//================================================================================
// FUNCTION: void useStartByteSet(...)
//
// DESCRIPTION: Sets the option whether the parser should use a start byte or not.
//
// INPUT: newStatus - true if the parser should use a start byte
// - false if the parser should not.
//
// RETURNS: Nothing
//================================================================================
void MsgParser::useStartByteSet(bool newStatus)
{
m_useStartByte = newStatus;
//if we are currently waiting for the start byte and the new option is to
// not wait for the start byte
if( (m_state == WAITING_FOR_START_BYTE) &&
(m_useStartByte == false)
)
{
//then we have to stop waiting for it. We do this by updating our state.
m_state = READING_MESSAGE;
}
else
{
//otherwise, do nothing more
}
}// end useStartByteSet
//================================================================================
// FUNCTION: void setHandlerForCmdNotFound(...)
//
// DESCRIPTION: Sets a new call handler function for when a command is not found.
//
// INPUT: pNewHandlerFunc - new call back function. This is the function
// to call when we receive a command that we don't process.
//
//
// RETURNS: Nothing
//================================================================================
void MsgParser::setHandlerForCmdNotFound(cmdNotFoundHandler_t pNewHandlerFunc)
{
m_pCmdNotFoundFunc = pNewHandlerFunc;
}//end setHandlerForCmdNotFound
//================================================================================
// FUNCTION: void processByte(...)
//
// DESCRIPTION: Process a new byte. If this new byte completes a message then the function
// will be called that correspondes with the received string, if such function exists.
//
// INPUT: newByte - the byte to process
//
// RETURNS: Nothing
//================================================================================
void MsgParser::processByte(uint8_t newByte)
{
switch (m_state)
{
case WAITING_FOR_START_BYTE:
if( newByte == m_msgStartByte )
{
m_state = READING_MESSAGE;
}
else
{
//ignore all other bytes
}
break;
case READING_MESSAGE:
if( newByte == m_msgEndByte )
{
//we just got the end byte and the message is now fully received. Time to parse it.
if(m_useStartByte == true)
m_state = WAITING_FOR_START_BYTE;
//***process message***
bool msgFound = false;
char *ptr = m_pRead; // make ptr point to start of m_pInputBuffer.
char *rest; // to point to the rest of the string after token extraction.
//We are about to edit our receive buffer, so make a copy of it first.
memcpy(m_pOrigMsg, m_pInputBuffer, m_BufferWriteIndex);
m_pMsgParserCommand = strtok_r(ptr, " ", &rest); //extract the command from the received message [side note: this replaces the first space with a null]
m_pRead = rest; // rest contains the leftover part..assign it to ptr...and start tokenizing again.
//search through the lookup table and try to find the string
uint8_t i = 0; //index variable
while( (msgFound == false) && (i < m_funcTableLength) )
{
memcpy_P( &m_bufStruct, &(m_pFuncTable[i]), sizeof(m_bufStruct) ); //pull a function table entry out of flash and into ram.
if ( strcmp_P( m_pMsgParserCommand, m_bufStruct.pCmdString ) == 0)
{
msgFound = true;
}
else
{
++i;
}
}
//at this point we either found a matching string in the table or reached the end of it
if(i < m_funcTableLength) //check if the message was found in the table
{
//we have a match, call the corresponding function
(*m_bufStruct.pFunc)();
}
else
{
//The received string was not found in our function table.
// Do we have a handler function that we can call when we have a command not found?
if(m_pCmdNotFoundFunc != NULL)
{
// Yes we do. Call that function and pass it the string that we didn't handle.
(*m_pCmdNotFoundFunc)((uint8_t*)m_pOrigMsg, m_BufferWriteIndex);
}
else
{
// We don't have a handler function defined.
// So we don't call any function. We just clear our receive buffer and start
// waiting for the next message.
}
}
clearTheBuffer();
}
else
{
//the new byte is not the end byte
//therefore this is not the end of the message. add the received byte to the buffer
m_pInputBuffer[m_BufferWriteIndex] = newByte;
m_BufferWriteIndex++;
//check if we have reached the end of the buffer without receiving an end byte
if(m_BufferWriteIndex == MSGPARSER_INPUT_BUFFER_SIZE)
{
//yes we have, our buffer is full.
clearTheBuffer();
if(m_useStartByte == true)
{
m_state = WAITING_FOR_START_BYTE;
}
else
{
m_state = READING_MESSAGE;
}
}
else
{
//we still have space in the buffer for at least 1 more byte
//do nothing
}
}
break;
default:
//should never ever get here.
break;
}
}// end processByte
//================================================================================
// FUNCTION: void setEndByte(...)
//
// DESCRIPTION: Sets a new byte to use as the end.
//
// INPUT: newEndByte - the new byte to use
//
// RETURNS: Nothing
//================================================================================
void MsgParser::setEndByte(uint8_t newEndByte)
{
m_msgEndByte = newEndByte;
}// end setEndByte
//================================================================================
// FUNCTION: void getLong(...)
//
// DESCRIPTION: Parsers the received string for a number and returns it as a long
//
// INPUT: Nothing
//
// RETURNS: The number as a long.
// Or 0, if no number is found in the remainder of the string
//================================================================================
long MsgParser::getLong()
{
char *numPtr;
char *ptr = m_pRead; // make ptr point to start of Buffer.
char *rest; // to point to the rest of the string after token extraction.
numPtr = strtok_r(ptr, " ", &rest); //extract the number from the received message [side note: this replaces the first space with a null]
m_pRead = rest; // rest contains the leftover part..assign it to ptr...and start tokenizing again.
return( atol(numPtr) );
}//end getLong
//================================================================================
// FUNCTION: void getInt(...)
//
// DESCRIPTION: Parsers the received string for a number and returns it as a int
//
// INPUT: Nothing
//
// RETURNS: The number as a int.
// Or 0, if no number is found in the remainder of the string
//================================================================================
int MsgParser::getInt()
{
char *numPtr;
char *ptr = m_pRead; // make ptr point to start of Buffer.
char *rest; // to point to the rest of the string after token extraction.
numPtr = strtok_r(ptr, " ", &rest); //extract the number from the received message [side note: this replaces the first space with a null]
m_pRead = rest; // rest contains the leftover part..assign it to ptr...and start tokenizing again.
return( atoi(numPtr) );
}//end getInt
//================================================================================
// FUNCTION: void getFloat(...)
//
// DESCRIPTION: Parsers the received string for a number and returns it as a float
//
// INPUT: Nothing
//
//
// RETURNS: The number as a float.
// Or 0, if no number is found in the remainder of the string
//================================================================================
float MsgParser::getFloat()
{
char *numPtr;
char *ptr = m_pRead; // make ptr point to start of Buffer.
char *rest; // to point to the rest of the string after token extraction.
numPtr = strtok_r(ptr, " ", &rest); //extract the number from the received message [side note: this replaces the first space with a null]
m_pRead = rest; // rest contains the leftover part..assign it to ptr...and start tokenizing again.
return( atof(numPtr) );
}//end getFloat
//================================================================================
// FUNCTION: void clearTheBuffer(...)
//
// DESCRIPTION: empties the receive buffer
//
// INPUT: Nothing
//
//
// RETURNS: Nothing
//================================================================================
void MsgParser::clearTheBuffer()
{
m_BufferWriteIndex = 0; //clear the buffer by setting the buffer write index to the beginning
m_pRead = m_pInputBuffer; //move the read pointer back to the start of the buffer
memset(m_pInputBuffer, NULL, MSGPARSER_INPUT_BUFFER_SIZE); //overwrite the whole buffer with nulls
}//end clearTheBuffer
//================================================================================
// FUNCTION: void numCmds(...)
//
// DESCRIPTION: Returns the number of commands
//
// INPUT: None
//
// RETURNS:
//================================================================================
uint8_t MsgParser::numCmds()
{
return m_funcTableLength;
}//end numCmds
//================================================================================
// FUNCTION: void cmdString(...)
//
// DESCRIPTION: Returns the command string at the requested index
//
// INPUT: cmdIndex - index of the command in the function table.
//
// RETURNS: Command string at the requested index
//================================================================================
char* MsgParser::cmdString(uint8_t cmdIndex)
{
if(cmdIndex >= numCmds())
{
//The requested command index is out of range
return NULL;
}
//pull a function table entry out of flash and into ram.
memcpy_P( &m_bufStruct, &(m_pFuncTable[cmdIndex]), sizeof(m_bufStruct) );
//Pull the command string out of flash and into ram
memcpy_P( m_pDescBuf, m_bufStruct.pCmdString, MSGPARSER_DESCRIPTION_SIZE);
return m_pDescBuf;
}//end cmdString
//================================================================================
// FUNCTION: void cmdDesc(...)
//
// DESCRIPTION: Looks up the command specified by the index and returns its description.
//
// INPUT: cmdIndex - index of the command in the function table.
//
// RETURNS: Description of the command
//================================================================================
char* MsgParser::cmdDesc(uint8_t cmdIndex)
{
if(cmdIndex >= numCmds())
{
//The requested command index is out of range
return NULL;
}
//pull a function table entry out of flash and into ram.
memcpy_P( &m_bufStruct, &(m_pFuncTable[cmdIndex]), sizeof(m_bufStruct) );
//The description string is allowed to be null. Check if it exists.
if(m_bufStruct.pDescString != NULL)
{
//Pull the command string out of flash and into ram
memcpy_P( m_pDescBuf, m_bufStruct.pDescString, MSGPARSER_DESCRIPTION_SIZE);
return m_pDescBuf;
}
else
{
return NULL;
}
}//end cmdDesc
//================================================================================
// FUNCTION: void version(...)
//
// DESCRIPTION: Returns the version number of the MsgParser class
//
// INPUT: None
//
// RETURNS: The version number of the MsgParser class
//================================================================================
uint8_t MsgParser::version()
{
return 0x05; //this is our version number
}//end version