Skip to content

Commit 77dc410

Browse files
author
Craig Versek
committed
2 parents d603ba7 + 4d275b3 commit 77dc410

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

PacketCommand.cpp

+17-9
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,6 @@ PacketShared::STATUS PacketCommand::lookupCommandByName(const char* name){
341341
DEBUG_PORT.println(i);
342342
DEBUG_PORT.print(F("#\t&_commandList[i]="));
343343
DEBUG_PORT.println((int) &_commandList[i], HEX);
344-
DEBUG_PORT.print(F("#\ttype_id["));DEBUG_PORT.print(_input_index);DEBUG_PORT.print(F("]="));
345-
DEBUG_PORT.println(_commandList[i].type_id[_input_index]);
346344
DEBUG_PORT.print(F("#\tname="));
347345
DEBUG_PORT.println(_commandList[i].name);
348346
#endif
@@ -417,28 +415,38 @@ PacketShared::STATUS PacketCommand::set_recvTimestamp(uint32_t timestamp_micros)
417415
PacketShared::STATUS PacketCommand::matchCommand(){
418416
byte cur_byte = 0x00;
419417
_current_command = _default_command;
418+
int type_id_index = 0;
420419
//parse out type_id from header
421420
#ifdef PACKETCOMMAND_DEBUG
422421
DEBUG_PORT.println(F("# In PacketCommand::matchCommand"));
423422
DEBUG_PORT.print(F("#\t_input_index="));DEBUG_PORT.println(_input_index);
424423
DEBUG_PORT.print(F("#\t_input_len="));DEBUG_PORT.println(_input_len);
425424
#endif
425+
if (_input_index >= _input_len){
426+
#ifdef PACKETCOMMAND_DEBUG
427+
DEBUG_PORT.println(F("### Error: invalid 'type ID' detected, no packet data left"));
428+
#endif
429+
return PacketShared::ERROR_INVALID_TYPE_ID;
430+
}
426431
while(_input_index < _input_len){
427432
cur_byte = _input_buffer[_input_index];
428433
#ifdef PACKETCOMMAND_DEBUG
429434
DEBUG_PORT.print(F("#\tcur_byte="));DEBUG_PORT.println(cur_byte,HEX);
435+
DEBUG_PORT.print(F("#\t_type_id_index="));DEBUG_PORT.println(type_id_index);
430436
#endif
431437
if (cur_byte != 0xFF and cur_byte != 0x00){ //valid type ID completed
432438
#ifdef PACKETCOMMAND_DEBUG
433439
DEBUG_PORT.println(F("#\tvalid 'type ID' format detected"));
434440
#endif
435-
_current_command.type_id[_input_index] = cur_byte;
441+
_current_command.type_id[type_id_index] = cur_byte;
442+
//CAUTION do not increment type_id_index or _input_index here!
436443
break;
437444
}
438445
else if (cur_byte == 0xFF){ //extended type ID, need to check the next byte
439-
_current_command.type_id[_input_index] = 0xFF;
446+
_current_command.type_id[type_id_index] = 0xFF;
447+
type_id_index++;
440448
_input_index++;
441-
if (_input_index >= MAX_TYPE_ID_LEN){
449+
if (type_id_index >= MAX_TYPE_ID_LEN){
442450
#ifdef PACKETCOMMAND_DEBUG
443451
DEBUG_PORT.println(F("### Error: invalid 'type ID' detected, exceeded maximum length"));
444452
#endif
@@ -458,7 +466,7 @@ PacketShared::STATUS PacketCommand::matchCommand(){
458466
return PacketShared::ERROR_INVALID_TYPE_ID;
459467
}
460468
}
461-
//For a valid type ID 'cur_byte' will be euqal to its last byte and all previous
469+
//For a valid type ID 'cur_byte' will be equal to its last byte and all previous
462470
//bytes, if they exist, must have been 0xFF (or nothing), so we only need to check the
463471
//corresponding byte for a match in the registered command list. Also,
464472
//since pkt_index must be < MAX_TYPE_ID_LEN at this point, it should be within
@@ -468,10 +476,10 @@ PacketShared::STATUS PacketCommand::matchCommand(){
468476
#ifdef PACKETCOMMAND_DEBUG
469477
DEBUG_PORT.print(F("# Searching command at index="));
470478
DEBUG_PORT.println(i);
471-
DEBUG_PORT.print(F("#\ttype_id["));DEBUG_PORT.print(_input_index);DEBUG_PORT.print(F("]="));
472-
DEBUG_PORT.println(_commandList[i].type_id[_input_index]);
479+
DEBUG_PORT.print(F("#\ttype_id["));DEBUG_PORT.print(type_id_index);DEBUG_PORT.print(F("]="));
480+
DEBUG_PORT.println(_commandList[i].type_id[type_id_index]);
473481
#endif
474-
if(_commandList[i].type_id[_input_index] == cur_byte){
482+
if(_commandList[i].type_id[type_id_index] == cur_byte){
475483
//a match has been found, so save it and stop
476484
#ifdef PACKETCOMMAND_DEBUG
477485
DEBUG_PORT.println(F("#match found"));

PacketQueue.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//#define PACKETQUEUE_DEBUG
1212

1313
#ifndef DEBUG_PORT
14-
#define DEBUG_PORT Serial3
14+
#define DEBUG_PORT Serial
1515
#endif
1616

1717
class PacketQueue

0 commit comments

Comments
 (0)