@@ -44,7 +44,6 @@ PacketCommand::PacketCommand(size_t maxCommands,
44
44
else { // do not allocate anything
45
45
_output_buffer = NULL ;
46
46
}
47
-
48
47
reset ();
49
48
}
50
49
@@ -66,6 +65,7 @@ PacketShared::STATUS PacketCommand::reset(){
66
65
_input_index = 0 ;
67
66
_input_len = 0 ;
68
67
_input_flags = 0x00 ;
68
+ _recv_timestamp_micros = 0 ;
69
69
// reset output buffer
70
70
_output_index = 0 ;
71
71
_output_len = 0 ;
@@ -347,6 +347,7 @@ PacketShared::STATUS PacketCommand::recv() {
347
347
}
348
348
349
349
PacketShared::STATUS PacketCommand::recv (bool & gotPacket) {
350
+ uint32_t timestamp_micros = micros ();
350
351
gotPacket = false ;
351
352
#ifdef PACKETCOMMAND_DEBUG
352
353
Serial.println (F (" In PacketCommand::recv()" ));
@@ -364,13 +365,19 @@ PacketShared::STATUS PacketCommand::recv(bool& gotPacket) {
364
365
return PacketShared::ERROR_NULL_HANDLER_FUNCTION_POINTER;
365
366
}
366
367
if (gotPacket){ // we have a packet
368
+ set_recvTimestamp (timestamp_micros);
367
369
return PacketShared::SUCCESS;
368
370
}
369
371
else { // we have no packet
370
372
return PacketShared::NO_PACKET_RECEIVED;
371
373
}
372
374
}
373
375
376
+ PacketShared::STATUS PacketCommand::set_recvTimestamp (uint32_t timestamp_micros){
377
+ _recv_timestamp_micros = timestamp_micros;
378
+ return PacketShared::SUCCESS;
379
+ }
380
+
374
381
/* *
375
382
*g This checks the characters in the buffer looking for a valid type_id prefix
376
383
* string, which adheres to the following pseudocode rules:
@@ -536,8 +543,20 @@ PacketShared::STATUS PacketCommand::setupOutputCommand(PacketCommand::CommandInf
536
543
537
544
// Use the '_send_callback' to send return packet
538
545
PacketShared::STATUS PacketCommand::send (){
546
+ uint32_t timestamp_micros = micros ();
539
547
if (_send_callback != NULL ){
540
- // call the send callback
548
+ set_sendTimestamp (timestamp_micros); // markdown the time write now
549
+ if (_output_flags & PacketShared::OPFLAG_APPEND_SEND_TIMESTAMP){
550
+ if ((_output_len + sizeof (uint32_t )) < PacketShared::DATA_BUFFER_SIZE){ // prevent buffer overrun
551
+ pack_uint32 (timestamp_micros);
552
+ }
553
+ else {
554
+ #ifdef PACKETCOMMAND_DEBUG
555
+ Serial.println (F (" ### Error: appeding send timestamp would overrun the output buffer" ));
556
+ #endif
557
+ }
558
+ }
559
+ // call the callback!
541
560
(*_send_callback)(*this );
542
561
return PacketShared::SUCCESS;
543
562
}
@@ -551,7 +570,19 @@ PacketShared::STATUS PacketCommand::send(){
551
570
552
571
// Use the '_send_nonblocking_callback' to send return packet
553
572
PacketShared::STATUS PacketCommand::send_nonblocking (){
573
+ uint32_t timestamp_micros = micros ();
554
574
if (_send_nonblocking_callback != NULL ){
575
+ set_sendTimestamp (timestamp_micros); // markdown the time write now
576
+ if (_output_flags & PacketShared::OPFLAG_APPEND_SEND_TIMESTAMP){
577
+ if ((_output_len + sizeof (uint32_t )) < PacketShared::DATA_BUFFER_SIZE){ // prevent buffer overrun
578
+ pack_uint32 (timestamp_micros);
579
+ }
580
+ else {
581
+ #ifdef PACKETCOMMAND_DEBUG
582
+ Serial.println (F (" ### Error: appeding send timestamp would overrun the output buffer" ));
583
+ #endif
584
+ }
585
+ }
555
586
// call the nonblocking send callback
556
587
(*_send_nonblocking_callback)(*this );
557
588
return PacketShared::SUCCESS;
@@ -564,6 +595,10 @@ PacketShared::STATUS PacketCommand::send_nonblocking(){
564
595
}
565
596
}
566
597
598
+ PacketShared::STATUS PacketCommand::set_sendTimestamp (uint32_t timestamp_micros){
599
+ _send_timestamp_micros = timestamp_micros;
600
+ return PacketShared::SUCCESS;
601
+ }
567
602
568
603
// Use the '_reply_send_callback' to send a quick reply
569
604
PacketShared::STATUS PacketCommand::reply_send (){
@@ -596,7 +631,6 @@ PacketShared::STATUS PacketCommand::reply_recv(){
596
631
}
597
632
}
598
633
599
-
600
634
/* *
601
635
* Accessors and mutators for the input buffer
602
636
*/
@@ -673,6 +707,7 @@ PacketShared::STATUS PacketCommand::enqueueInputBuffer(PacketQueue& pq){
673
707
// build a packet struct to hold current buffer state
674
708
PacketShared::Packet pkt;
675
709
pkt.length = min (_input_len, PacketShared::DATA_BUFFER_SIZE);
710
+ pkt.timestamp = _recv_timestamp_micros; // this should have been recorded as close to the RX time as possible
676
711
pkt.flags = _input_flags;
677
712
memcpy (pkt.data , _input_buffer, pkt.length );
678
713
PacketShared::STATUS pqs;
@@ -704,6 +739,8 @@ PacketShared::STATUS PacketCommand::dequeueInputBuffer(PacketQueue& pq){
704
739
}
705
740
}
706
741
742
+
743
+
707
744
void PacketCommand::allocateOutputBuffer (size_t len){
708
745
_outputBufferSize = len;
709
746
_output_buffer = (byte*) calloc (_outputBufferSize, sizeof (byte));
@@ -741,6 +778,7 @@ PacketShared::STATUS PacketCommand::enqueueOutputBuffer(PacketQueue& pq){
741
778
// build a packet struct to hold current buffer state
742
779
PacketShared::Packet pkt;
743
780
pkt.length = min (_output_len, PacketShared::DATA_BUFFER_SIZE);
781
+ pkt.timestamp = 0 ;
744
782
pkt.flags = _output_flags;
745
783
memcpy (pkt.data , _output_buffer, pkt.length );
746
784
PacketShared::STATUS pqs;
@@ -780,6 +818,7 @@ PacketShared::STATUS PacketCommand::requeueOutputBuffer(PacketQueue& pq){
780
818
// build a packet struct to hold current buffer state
781
819
PacketShared::Packet pkt;
782
820
pkt.length = min (_output_len, PacketShared::DATA_BUFFER_SIZE);
821
+ pkt.timestamp = 0 ;
783
822
pkt.flags = _output_flags;
784
823
memcpy (pkt.data , _output_buffer, pkt.length );
785
824
PacketShared::STATUS pqs;
0 commit comments