@@ -94,7 +94,8 @@ PacketShared::STATUS PacketCommand::addCommand(const byte* type_id,
94
94
size_t type_id_len = strlen ((char *) type_id);
95
95
struct CommandInfo new_command;
96
96
#ifdef PACKETCOMMAND_DEBUG
97
- Serial.print (F (" Adding command #(" ));
97
+ Serial.println (F (" # in PacketCommand::addCommand" ));
98
+ Serial.print (F (" #\t Adding command #(" ));
98
99
Serial.print (_commandCount);
99
100
Serial.print (F (" ): " ));
100
101
Serial.println (name);
@@ -103,16 +104,16 @@ PacketShared::STATUS PacketCommand::addCommand(const byte* type_id,
103
104
Serial.println (" '" );
104
105
Serial.println (type_id_len);
105
106
#endif
106
- if (_commandCount >= _maxCommands){
107
+ if (_commandCount >= _maxCommands - 1 ){
107
108
#ifdef PACKETCOMMAND_DEBUG
108
- Serial.print (F (" Error: exceeded maxCommands=" ));
109
+ Serial.print (F (" ### Error: exceeded maxCommands=" ));
109
110
Serial.println (_maxCommands);
110
111
#endif
111
112
return PacketShared::ERROR_EXCEDED_MAX_COMMANDS;
112
113
}
113
114
if (type_id_len > MAX_TYPE_ID_LEN){
114
115
#ifdef PACKETCOMMAND_DEBUG
115
- Serial.print (F (" Error: 'type_id' cannot exceed MAX_TYPE_ID_LEN=" ));
116
+ Serial.print (F (" ### Error: 'type_id' cannot exceed MAX_TYPE_ID_LEN=" ));
116
117
Serial.println (MAX_TYPE_ID_LEN);
117
118
#endif
118
119
return PacketShared::ERROR_INVALID_TYPE_ID;
@@ -123,43 +124,43 @@ PacketShared::STATUS PacketCommand::addCommand(const byte* type_id,
123
124
// test if the type ID rules are followed
124
125
cur_byte = type_id[i];
125
126
#ifdef PACKETCOMMAND_DEBUG
126
- Serial.print (F (" i =" ));
127
+ Serial.print (F (" # \t i =" ));
127
128
Serial.println (i);
128
- Serial.print (F (" checking type ID byte: " ));
129
+ Serial.print (F (" # \t checking type ID byte: " ));
129
130
Serial.println (cur_byte, HEX);
130
131
#endif
131
132
switch (cur_byte){
132
133
case 0xFF :
133
134
if (i < (type_id_len - 1 )){
134
135
// continue extended type ID
135
136
#ifdef PACKETCOMMAND_DEBUG
136
- Serial.println (F (" \t continue extended type ID" ));
137
+ Serial.println (F (" # \t continue extended type ID" ));
137
138
#endif
138
139
new_command.type_id [i] = 0xFF ;
139
140
}
140
141
else {// cannot end type_id with 0xFF
141
142
#ifdef PACKETCOMMAND_DEBUG
142
- Serial.println (F (" Error: 'type_id' cannot end with 0xFF" ));
143
+ Serial.println (F (" ### Error: 'type_id' cannot end with 0xFF" ));
143
144
#endif
144
145
return PacketShared::ERROR_INVALID_TYPE_ID;
145
146
}
146
147
break ;
147
148
case 0x00 :
148
149
#ifdef PACKETCOMMAND_DEBUG
149
- Serial.println (F (" Error: 'type_id' cannot contain null (0x00) bytes" ));
150
+ Serial.println (F (" ### Error: 'type_id' cannot contain null (0x00) bytes" ));
150
151
#endif
151
152
return PacketShared::ERROR_INVALID_TYPE_ID;
152
153
break ;
153
154
default : // any other byte value
154
155
if (i == (type_id_len - 1 )){// valid type ID completed
155
156
#ifdef PACKETCOMMAND_DEBUG
156
- Serial.println (F (" valid type ID completed" ));
157
+ Serial.println (F (" # \t valid type ID completed" ));
157
158
#endif
158
159
new_command.type_id [i] = cur_byte;
159
160
}
160
161
else {
161
162
#ifdef PACKETCOMMAND_DEBUG
162
- Serial.println (F (" Error: 'type_id' cannot have a prefix != [0xFF]*" ));
163
+ Serial.println (F (" ### Error: 'type_id' cannot have a prefix != [0xFF]*" ));
163
164
#endif
164
165
return PacketShared::ERROR_INVALID_TYPE_ID;
165
166
}
@@ -172,7 +173,7 @@ PacketShared::STATUS PacketCommand::addCommand(const byte* type_id,
172
173
}
173
174
}
174
175
#ifdef PACKETCOMMAND_DEBUG
175
- Serial.print (F (" type_id =" ));
176
+ Serial.print (F (" # \t type_id =" ));
176
177
for (size_t i=0 ; i < MAX_TYPE_ID_LEN; i++){
177
178
if ( new_command.type_id [i] != 0x00 ){
178
179
Serial.print (new_command.type_id [i], HEX);
@@ -277,26 +278,26 @@ PacketShared::STATUS PacketCommand::registerReplyRecvCallback(bool (*function)(P
277
278
*/
278
279
PacketShared::STATUS PacketCommand::processInput (){
279
280
#ifdef PACKETCOMMAND_DEBUG
280
- Serial.println (F (" In PacketCommand::processInput" ));
281
- Serial.print (F (" \t _input_index=" ));Serial.println (_input_index);
282
- Serial.print (F (" \t _input_len=" ));Serial.println (_input_len);
281
+ Serial.println (F (" # In PacketCommand::processInput" ));
282
+ Serial.print (F (" # \t _input_index=" ));Serial.println (_input_index);
283
+ Serial.print (F (" # \t _input_len=" ));Serial.println (_input_len);
283
284
#endif
284
285
PacketShared::STATUS pcs = matchCommand ();
285
286
#ifdef PACKETCOMMAND_DEBUG
286
- Serial.println (F (" (processInput)-after calling matchCommand()" ));
287
- Serial.print (F (" \t _input_index=" ));Serial.println (_input_index);
288
- Serial.print (F (" \t _input_len=" ));Serial.println (_input_len);
287
+ Serial.println (F (" # (processInput)-after calling matchCommand()" ));
288
+ Serial.print (F (" # \t _input_index=" ));Serial.println (_input_index);
289
+ Serial.print (F (" # \t _input_len=" ));Serial.println (_input_len);
289
290
#endif
290
291
if (pcs == PacketShared::SUCCESS){ // a command was matched
291
292
#ifdef PACKETCOMMAND_DEBUG
292
- Serial.print (F (" (processInput)-matched command: " ));
293
+ Serial.print (F (" # (processInput)-matched command: " ));
293
294
CommandInfo cmd = getCurrentCommand ();
294
295
Serial.println (cmd.name );
295
296
#endif
296
297
}
297
298
else if (pcs == PacketShared::ERROR_NO_TYPE_ID_MATCH){ // valid ID but no command was matched
298
299
#ifdef PACKETCOMMAND_DEBUG
299
- Serial.println (F (" (processInput)-no matched command" ));
300
+ Serial.println (F (" # (processInput)-no matched command" ));
300
301
#endif
301
302
}
302
303
else {
@@ -313,20 +314,25 @@ PacketShared::STATUS PacketCommand::processInput(){
313
314
PacketShared::STATUS PacketCommand::lookupCommandByName (const char * name){
314
315
_current_command = _default_command;
315
316
#ifdef PACKETCOMMAND_DEBUG
316
- Serial.print (F (" Searching for command named = " ));
317
+ Serial.println (F (" # In PacketCommand::lookupCommandByName" ));
318
+ Serial.print (F (" #\t Searching for command named = " ));
317
319
Serial.println (name);
318
320
#endif
319
- for (size_t i=0 ; i <= _maxCommands; i++){
321
+ for (size_t i=0 ; i < _maxCommands; i++){
320
322
#ifdef PACKETCOMMAND_DEBUG
321
- Serial.print (F (" \t searching command at index=" ));
323
+ Serial.print (F (" # \t searching command at index=" ));
322
324
Serial.println (i);
323
- Serial.print (F (" \t type_id[" ));Serial.print (_input_index);Serial.print (F (" ]=" ));
325
+ Serial.print (F (" #\t &_commandList[i]=" ));
326
+ Serial.println ((int ) &_commandList[i], HEX);
327
+ Serial.print (F (" #\t type_id[" ));Serial.print (_input_index);Serial.print (F (" ]=" ));
324
328
Serial.println (_commandList[i].type_id [_input_index]);
329
+ Serial.print (F (" #\t name=" ));
330
+ Serial.println (_commandList[i].name );
325
331
#endif
326
332
if (strcmp (_commandList[i].name ,name) == 0 ){
327
333
// a match has been found, so save it and stop
328
334
#ifdef PACKETCOMMAND_DEBUG
329
- Serial.println (F (" match found" ));
335
+ Serial.println (F (" # \t match found" ));
330
336
#endif
331
337
_current_command = _commandList[i];
332
338
return PacketShared::SUCCESS;
@@ -389,18 +395,18 @@ PacketShared::STATUS PacketCommand::matchCommand(){
389
395
_current_command = _default_command;
390
396
// parse out type_id from header
391
397
#ifdef PACKETCOMMAND_DEBUG
392
- Serial.println (F (" In PacketCommand::matchCommand" ));
393
- Serial.print (F (" \t _input_index=" ));Serial.println (_input_index);
394
- Serial.print (F (" \t _input_len=" ));Serial.println (_input_len);
398
+ Serial.println (F (" # In PacketCommand::matchCommand" ));
399
+ Serial.print (F (" # \t _input_index=" ));Serial.println (_input_index);
400
+ Serial.print (F (" # \t _input_len=" ));Serial.println (_input_len);
395
401
#endif
396
402
while (_input_index < _input_len){
397
403
cur_byte = _input_buffer[_input_index];
398
404
#ifdef PACKETCOMMAND_DEBUG
399
- Serial.print (F (" cur_byte =" ));Serial.println (cur_byte,HEX);
405
+ Serial.print (F (" # \t cur_byte =" ));Serial.println (cur_byte,HEX);
400
406
#endif
401
407
if (cur_byte != 0xFF and cur_byte != 0x00 ){ // valid type ID completed
402
408
#ifdef PACKETCOMMAND_DEBUG
403
- Serial.println (F (" valid 'type ID' format detected" ));
409
+ Serial.println (F (" # \t valid 'type ID' format detected" ));
404
410
#endif
405
411
_current_command.type_id [_input_index] = cur_byte;
406
412
break ;
@@ -410,20 +416,20 @@ PacketShared::STATUS PacketCommand::matchCommand(){
410
416
_input_index++;
411
417
if (_input_index >= MAX_TYPE_ID_LEN){
412
418
#ifdef PACKETCOMMAND_DEBUG
413
- Serial.println (F (" Error: invalid 'type ID' detected, exceeded maximum length" ));
419
+ Serial.println (F (" ### Error: invalid 'type ID' detected, exceeded maximum length" ));
414
420
#endif
415
421
return PacketShared::ERROR_INVALID_TYPE_ID;
416
422
}
417
423
else if (_input_index >= _input_len ){ // 0xFF cannot end the type_id
418
424
#ifdef PACKETCOMMAND_DEBUG
419
- Serial.println (F (" Error: invalid packet detected, 'type ID' does not terminate before reaching end of packet" ));
425
+ Serial.println (F (" ### Error: invalid packet detected, 'type ID' does not terminate before reaching end of packet" ));
420
426
#endif
421
427
return PacketShared::ERROR_INVALID_PACKET;
422
428
}
423
429
}
424
430
else { // must be 0x00
425
431
#ifdef PACKETCOMMAND_DEBUG
426
- Serial.println (F (" Error: invalid 'type ID' detected, cannot contain null (0x00) bytes" ));
432
+ Serial.println (F (" ### Error: invalid 'type ID' detected, cannot contain null (0x00) bytes" ));
427
433
#endif
428
434
return PacketShared::ERROR_INVALID_TYPE_ID;
429
435
}
@@ -434,17 +440,17 @@ PacketShared::STATUS PacketCommand::matchCommand(){
434
440
// since pkt_index must be < MAX_TYPE_ID_LEN at this point, it should be within
435
441
// the bounds; and since 'cur_byte' != 0x00 as well, shorter type IDs should
436
442
// not match since the unused bytes are initialized to 0x00.
437
- for (size_t i=0 ; i <= _maxCommands; i++){
443
+ for (size_t i=0 ; i < _maxCommands; i++){
438
444
#ifdef PACKETCOMMAND_DEBUG
439
- Serial.print (F (" Searching command at index=" ));
445
+ Serial.print (F (" # Searching command at index=" ));
440
446
Serial.println (i);
441
- Serial.print (F (" \t type_id[" ));Serial.print (_input_index);Serial.print (F (" ]=" ));
447
+ Serial.print (F (" # \t type_id[" ));Serial.print (_input_index);Serial.print (F (" ]=" ));
442
448
Serial.println (_commandList[i].type_id [_input_index]);
443
449
#endif
444
450
if (_commandList[i].type_id [_input_index] == cur_byte){
445
451
// a match has been found, so save it and stop
446
452
#ifdef PACKETCOMMAND_DEBUG
447
- Serial.println (F (" match found" ));
453
+ Serial.println (F (" # match found" ));
448
454
#endif
449
455
_current_command = _commandList[i];
450
456
return moveInputBufferIndex (1 ); // increment to prepare for data unpacking
@@ -453,14 +459,14 @@ PacketShared::STATUS PacketCommand::matchCommand(){
453
459
// no type ID has been matched
454
460
if (_default_command.function != NULL ){ // set the default handler if it has been registered
455
461
#ifdef PACKETCOMMAND_DEBUG
456
- Serial.println (F (" Setting the default command handler" ));
462
+ Serial.println (F (" # Setting the default command handler" ));
457
463
#endif
458
464
_current_command.function = _default_command.function ;
459
465
return moveInputBufferIndex (1 ); // increment to prepare for data unpacking
460
466
}
461
467
else { // otherwise return and error condition
462
468
#ifdef PACKETCOMMAND_DEBUG
463
- Serial.println (F (" No match found for this packet's type ID" ));
469
+ Serial.println (F (" # No match found for this packet's type ID" ));
464
470
#endif
465
471
return PacketShared::ERROR_NO_TYPE_ID_MATCH;
466
472
}
@@ -486,15 +492,15 @@ PacketCommand::CommandInfo PacketCommand::getCurrentCommand() {
486
492
*/
487
493
PacketShared::STATUS PacketCommand::dispatchCommand () {
488
494
#ifdef PACKETCOMMAND_DEBUG
489
- Serial.println (F (" In PacketCommand::dispatchCommand" ));
495
+ Serial.println (F (" # In PacketCommand::dispatchCommand" ));
490
496
#endif
491
497
if (_current_command.function != NULL ){
492
498
(*_current_command.function )(*this );
493
499
return PacketShared::SUCCESS;
494
500
}
495
501
else {
496
502
#ifdef PACKETCOMMAND_DEBUG
497
- Serial.println (F (" Error: tried to dispatch a NULL handler function pointer" ));
503
+ Serial.println (F (" ### Error: tried to dispatch a NULL handler function pointer" ));
498
504
#endif
499
505
return PacketShared::ERROR_NULL_HANDLER_FUNCTION_POINTER;
500
506
}
@@ -503,6 +509,9 @@ PacketShared::STATUS PacketCommand::dispatchCommand() {
503
509
// use unpack* methods to pull out data from packet
504
510
505
511
PacketShared::STATUS PacketCommand::setupOutputCommandByName (const char * name){
512
+ #ifdef PACKETCOMMAND_DEBUG
513
+ Serial.println (F (" # In PacketCommand::setupOutputCommandByName" ));
514
+ #endif
506
515
PacketShared::STATUS pcs;
507
516
pcs = lookupCommandByName (name); // sets _current_command on SUCCESS
508
517
// reset output buffer state
@@ -534,7 +543,7 @@ PacketShared::STATUS PacketCommand::send(){
534
543
}
535
544
else {
536
545
#ifdef PACKETCOMMAND_DEBUG
537
- Serial.println (F (" Error: tried to send using a NULL send callback function pointer" ));
546
+ Serial.println (F (" ### Error: tried to send using a NULL send callback function pointer" ));
538
547
#endif
539
548
return PacketShared::ERROR_NULL_HANDLER_FUNCTION_POINTER;
540
549
}
@@ -549,7 +558,7 @@ PacketShared::STATUS PacketCommand::send_nonblocking(){
549
558
}
550
559
else {
551
560
#ifdef PACKETCOMMAND_DEBUG
552
- Serial.println (F (" Error: tried to send using a NULL send nonblocking callback function pointer" ));
561
+ Serial.println (F (" ### Error: tried to send using a NULL send nonblocking callback function pointer" ));
553
562
#endif
554
563
return PacketShared::ERROR_NULL_HANDLER_FUNCTION_POINTER;
555
564
}
@@ -565,7 +574,7 @@ PacketShared::STATUS PacketCommand::reply_send(){
565
574
}
566
575
else {
567
576
#ifdef PACKETCOMMAND_DEBUG
568
- Serial.println (F (" Error: tried to send using a NULL send callback function pointer" ));
577
+ Serial.println (F (" ### Error: tried to send using a NULL send callback function pointer" ));
569
578
#endif
570
579
return PacketShared::ERROR_NULL_HANDLER_FUNCTION_POINTER;
571
580
}
@@ -581,7 +590,7 @@ PacketShared::STATUS PacketCommand::reply_recv(){
581
590
}
582
591
else {
583
592
#ifdef PACKETCOMMAND_DEBUG
584
- Serial.println (F (" Error: tried to receive using a NULL recv callback function pointer" ));
593
+ Serial.println (F (" ### Error: tried to receive using a NULL recv callback function pointer" ));
585
594
#endif
586
595
return PacketShared::ERROR_NULL_HANDLER_FUNCTION_POINTER;
587
596
}
@@ -594,26 +603,26 @@ PacketShared::STATUS PacketCommand::reply_recv(){
594
603
595
604
PacketShared::STATUS PacketCommand::assignInputBuffer (byte* buff, size_t len){
596
605
#ifdef PACKETCOMMAND_DEBUG
597
- Serial.println (F (" In PacketCommand::assignInputBuffer" ));
598
- Serial.print (F (" \t len=" ));Serial.println (len);
599
- Serial.print (F (" \t _inputBufferSize=" ));Serial.println (_inputBufferSize);
600
- Serial.print (F (" \t _input_index=" ));Serial.println (_input_index);
601
- Serial.print (F (" \t _input_len=" ));Serial.println (_input_len);
606
+ Serial.println (F (" # In PacketCommand::assignInputBuffer" ));
607
+ Serial.print (F (" # \t len=" ));Serial.println (len);
608
+ Serial.print (F (" # \t _inputBufferSize=" ));Serial.println (_inputBufferSize);
609
+ Serial.print (F (" # \t _input_index=" ));Serial.println (_input_index);
610
+ Serial.print (F (" # \t _input_len=" ));Serial.println (_input_len);
602
611
#endif
603
612
_input_buffer = buff;
604
613
// check the input length before setting
605
614
if (len <= _inputBufferSize){
606
615
_input_len = len;
607
616
#ifdef PACKETCOMMAND_DEBUG
608
- Serial.println (F (" (assignInputBuffer) after setting _input_len" ));
609
- Serial.print (F (" \t _input_index=" ));Serial.println (_input_index);
610
- Serial.print (F (" \t _input_len=" ));Serial.println (_input_len);
617
+ Serial.println (F (" # (assignInputBuffer) after setting _input_len" ));
618
+ Serial.print (F (" # \t _input_index=" ));Serial.println (_input_index);
619
+ Serial.print (F (" # \t _input_len=" ));Serial.println (_input_len);
611
620
#endif
612
621
return PacketShared::SUCCESS;
613
622
}
614
623
else {
615
624
#ifdef PACKETCOMMAND_DEBUG
616
- Serial.println (F (" Error: tried to receive data that would overrun input buffer" ));
625
+ Serial.println (F (" ### Error: tried to receive data that would overrun input buffer" ));
617
626
#endif
618
627
_input_len = _inputBufferSize; // set to safe value
619
628
return PacketShared::ERROR_INPUT_BUFFER_OVERRUN;
@@ -651,15 +660,15 @@ PacketShared::STATUS PacketCommand::moveInputBufferIndex(int n){
651
660
652
661
void PacketCommand::resetInputBuffer (){
653
662
#ifdef PACKETCOMMAND_DEBUG
654
- Serial.println (F (" In PacketCommand::resetInputBuffer" ));
663
+ Serial.println (F (" # In PacketCommand::resetInputBuffer" ));
655
664
#endif
656
665
_input_index = 0 ;
657
666
_input_len = 0 ;
658
667
}
659
668
660
669
PacketShared::STATUS PacketCommand::enqueueInputBuffer (PacketQueue& pq){
661
670
#ifdef PACKETCOMMAND_DEBUG
662
- Serial.println (F (" In PacketCommand::enqueueInputBuffer" ));
671
+ Serial.println (F (" # In PacketCommand::enqueueInputBuffer" ));
663
672
#endif
664
673
// build a packet struct to hold current buffer state
665
674
PacketShared::Packet pkt;
@@ -673,7 +682,7 @@ PacketShared::STATUS PacketCommand::enqueueInputBuffer(PacketQueue& pq){
673
682
674
683
PacketShared::STATUS PacketCommand::dequeueInputBuffer (PacketQueue& pq){
675
684
#ifdef PACKETCOMMAND_DEBUG
676
- Serial.println (F (" In PacketCommand::dequeueInputBuffer" ));
685
+ Serial.println (F (" # In PacketCommand::dequeueInputBuffer" ));
677
686
#endif
678
687
// build a packet struct to hold current buffer state
679
688
PacketShared::Packet pkt;
@@ -727,7 +736,7 @@ PacketShared::STATUS PacketCommand::moveOutputBufferIndex(int n){
727
736
728
737
PacketShared::STATUS PacketCommand::enqueueOutputBuffer (PacketQueue& pq){
729
738
#ifdef PACKETCOMMAND_DEBUG
730
- Serial.println (F (" In PacketCommand::enqueueOutputBuffer" ));
739
+ Serial.println (F (" # In PacketCommand::enqueueOutputBuffer" ));
731
740
#endif
732
741
// build a packet struct to hold current buffer state
733
742
PacketShared::Packet pkt;
@@ -741,7 +750,7 @@ PacketShared::STATUS PacketCommand::enqueueOutputBuffer(PacketQueue& pq){
741
750
742
751
PacketShared::STATUS PacketCommand::dequeueOutputBuffer (PacketQueue& pq){
743
752
#ifdef PACKETCOMMAND_DEBUG
744
- Serial.println (F (" In PacketCommand::dequeueOutputBuffer" ));
753
+ Serial.println (F (" # In PacketCommand::dequeueOutputBuffer" ));
745
754
#endif
746
755
// build a packet struct to hold current buffer state
747
756
PacketShared::Packet pkt;
@@ -766,7 +775,7 @@ PacketShared::STATUS PacketCommand::dequeueOutputBuffer(PacketQueue& pq){
766
775
PacketShared::STATUS PacketCommand::requeueOutputBuffer (PacketQueue& pq){
767
776
// pushes output buffer onto the front of the queue
768
777
#ifdef PACKETCOMMAND_DEBUG
769
- Serial.println (F (" In PacketCommand::requeueOutputBuffer" ));
778
+ Serial.println (F (" # In PacketCommand::requeueOutputBuffer" ));
770
779
#endif
771
780
// build a packet struct to hold current buffer state
772
781
PacketShared::Packet pkt;
0 commit comments