Skip to content

Commit beea0ec

Browse files
author
Craig Versek
committed
handling of timestamp info for packets; better names for input and output flag values
1 parent 209ae3d commit beea0ec

File tree

4 files changed

+82
-24
lines changed

4 files changed

+82
-24
lines changed

PacketCommand.cpp

+42-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ PacketCommand::PacketCommand(size_t maxCommands,
4444
else{ //do not allocate anything
4545
_output_buffer = NULL;
4646
}
47-
4847
reset();
4948
}
5049

@@ -66,6 +65,7 @@ PacketShared::STATUS PacketCommand::reset(){
6665
_input_index = 0;
6766
_input_len = 0;
6867
_input_flags = 0x00;
68+
_recv_timestamp_micros = 0;
6969
//reset output buffer
7070
_output_index = 0;
7171
_output_len = 0;
@@ -347,6 +347,7 @@ PacketShared::STATUS PacketCommand::recv() {
347347
}
348348

349349
PacketShared::STATUS PacketCommand::recv(bool& gotPacket) {
350+
uint32_t timestamp_micros = micros();
350351
gotPacket = false;
351352
#ifdef PACKETCOMMAND_DEBUG
352353
Serial.println(F("In PacketCommand::recv()"));
@@ -364,13 +365,19 @@ PacketShared::STATUS PacketCommand::recv(bool& gotPacket) {
364365
return PacketShared::ERROR_NULL_HANDLER_FUNCTION_POINTER;
365366
}
366367
if (gotPacket){ //we have a packet
368+
set_recvTimestamp(timestamp_micros);
367369
return PacketShared::SUCCESS;
368370
}
369371
else{ //we have no packet
370372
return PacketShared::NO_PACKET_RECEIVED;
371373
}
372374
}
373375

376+
PacketShared::STATUS PacketCommand::set_recvTimestamp(uint32_t timestamp_micros){
377+
_recv_timestamp_micros = timestamp_micros;
378+
return PacketShared::SUCCESS;
379+
}
380+
374381
/**
375382
*g This checks the characters in the buffer looking for a valid type_id prefix
376383
* string, which adheres to the following pseudocode rules:
@@ -536,8 +543,20 @@ PacketShared::STATUS PacketCommand::setupOutputCommand(PacketCommand::CommandInf
536543

537544
// Use the '_send_callback' to send return packet
538545
PacketShared::STATUS PacketCommand::send(){
546+
uint32_t timestamp_micros = micros();
539547
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!
541560
(*_send_callback)(*this);
542561
return PacketShared::SUCCESS;
543562
}
@@ -551,7 +570,19 @@ PacketShared::STATUS PacketCommand::send(){
551570

552571
// Use the '_send_nonblocking_callback' to send return packet
553572
PacketShared::STATUS PacketCommand::send_nonblocking(){
573+
uint32_t timestamp_micros = micros();
554574
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+
}
555586
//call the nonblocking send callback
556587
(*_send_nonblocking_callback)(*this);
557588
return PacketShared::SUCCESS;
@@ -564,6 +595,10 @@ PacketShared::STATUS PacketCommand::send_nonblocking(){
564595
}
565596
}
566597

598+
PacketShared::STATUS PacketCommand::set_sendTimestamp(uint32_t timestamp_micros){
599+
_send_timestamp_micros = timestamp_micros;
600+
return PacketShared::SUCCESS;
601+
}
567602

568603
// Use the '_reply_send_callback' to send a quick reply
569604
PacketShared::STATUS PacketCommand::reply_send(){
@@ -596,7 +631,6 @@ PacketShared::STATUS PacketCommand::reply_recv(){
596631
}
597632
}
598633

599-
600634
/**
601635
* Accessors and mutators for the input buffer
602636
*/
@@ -673,6 +707,7 @@ PacketShared::STATUS PacketCommand::enqueueInputBuffer(PacketQueue& pq){
673707
//build a packet struct to hold current buffer state
674708
PacketShared::Packet pkt;
675709
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
676711
pkt.flags = _input_flags;
677712
memcpy(pkt.data, _input_buffer, pkt.length);
678713
PacketShared::STATUS pqs;
@@ -704,6 +739,8 @@ PacketShared::STATUS PacketCommand::dequeueInputBuffer(PacketQueue& pq){
704739
}
705740
}
706741

742+
743+
707744
void PacketCommand::allocateOutputBuffer(size_t len){
708745
_outputBufferSize = len;
709746
_output_buffer = (byte*) calloc(_outputBufferSize, sizeof(byte));
@@ -741,6 +778,7 @@ PacketShared::STATUS PacketCommand::enqueueOutputBuffer(PacketQueue& pq){
741778
//build a packet struct to hold current buffer state
742779
PacketShared::Packet pkt;
743780
pkt.length = min(_output_len, PacketShared::DATA_BUFFER_SIZE);
781+
pkt.timestamp = 0;
744782
pkt.flags = _output_flags;
745783
memcpy(pkt.data, _output_buffer, pkt.length);
746784
PacketShared::STATUS pqs;
@@ -780,6 +818,7 @@ PacketShared::STATUS PacketCommand::requeueOutputBuffer(PacketQueue& pq){
780818
//build a packet struct to hold current buffer state
781819
PacketShared::Packet pkt;
782820
pkt.length = min(_output_len, PacketShared::DATA_BUFFER_SIZE);
821+
pkt.timestamp = 0;
783822
pkt.flags = _output_flags;
784823
memcpy(pkt.data, _output_buffer, pkt.length);
785824
PacketShared::STATUS pqs;

PacketCommand.h

+15-8
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#include "PacketShared.h"
3535

3636
// Uncomment the next line to run the library in debug mode (verbose messages)
37-
#define PACKETCOMMAND_DEBUG
37+
//#define PACKETCOMMAND_DEBUG
3838

3939
typedef float float32_t;
4040
typedef double float64_t;
@@ -82,10 +82,13 @@ class PacketCommand{
8282
CommandInfo getCurrentCommand();
8383
PacketShared::STATUS recv(); // Use the '_recv_callback' to put data into _input_buffer
8484
PacketShared::STATUS recv(bool& gotPacket); // Use the '_recv_callback' to put data into _input_buffer
85+
PacketShared::STATUS set_recvTimestamp(uint32_t timestamp_micros);
86+
uint32_t get_recvTimestamp(){return _recv_timestamp_micros;};
8587
PacketShared::STATUS matchCommand(); // Read the packet header from the input buffer and locate a matching registered handler function
8688
PacketShared::STATUS dispatchCommand(); // Call the current Command
8789
PacketShared::STATUS send(); // Use the '_send_callback' to send _output_buffer
8890
PacketShared::STATUS send_nonblocking(); // Use the '_send_nonblocking_callback' to send _schedule the output_buffer contents to be sent, returning immediately
91+
PacketShared::STATUS set_sendTimestamp(uint32_t timestamp_micros);
8992
PacketShared::STATUS reply_send();
9093
PacketShared::STATUS reply_recv();
9194

@@ -102,8 +105,10 @@ class PacketCommand{
102105
int getOutputBufferIndex();
103106
size_t getOutputLen(){return _output_len;};
104107
PacketShared::STATUS setOutputBufferIndex(int new_index);
105-
PacketShared::STATUS markOutputAsQuery(){_output_flags|=PacketShared::PFLAG_IS_QUERY;return PacketShared::SUCCESS;}
106-
bool outputIsQuery(){return (bool)_output_flags&PacketShared::PFLAG_IS_QUERY;}
108+
byte getOutputFlags(){return _output_flags;};
109+
void flagOutputAsQuery(){_output_flags|=PacketShared::OPFLAG_IS_QUERY;};
110+
void flagOutputAppendSendTimestamp(){_output_flags|=PacketShared::OPFLAG_APPEND_SEND_TIMESTAMP;};
111+
bool outputIsQuery(){return (bool)_output_flags&PacketShared::OPFLAG_IS_QUERY;};
107112
PacketShared::STATUS enqueueOutputBuffer(PacketQueue& pq);
108113
PacketShared::STATUS dequeueOutputBuffer(PacketQueue& pq);
109114
PacketShared::STATUS requeueOutputBuffer(PacketQueue& pq);
@@ -162,17 +167,19 @@ class PacketCommand{
162167
size_t _commandCount;
163168
size_t _maxCommands;
164169
//track state of input buffer
165-
size_t _inputBufferSize;
166-
byte* _input_buffer; //this will be a fixed buffer location
167-
size_t _input_index;
168-
size_t _input_len;
169-
byte _input_flags;
170+
size_t _inputBufferSize;
171+
byte* _input_buffer; //this will be a fixed buffer location
172+
size_t _input_index;
173+
size_t _input_len;
174+
byte _input_flags;
175+
uint32_t _recv_timestamp_micros;
170176
//track state of output buffer
171177
size_t _outputBufferSize;
172178
byte* _output_buffer; //this will be a fixed buffer location
173179
size_t _output_index;
174180
size_t _output_len;
175181
byte _output_flags;
182+
uint32_t _send_timestamp_micros;
176183
//cached callbacks
177184
void (*_send_callback)(PacketCommand& this_pCmd);
178185
void (*_send_nonblocking_callback)(PacketCommand& this_pCmd);

PacketQueue.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "PacketShared.h"
99

1010
//uncomment for debugging
11-
#define PACKETQUEUE_DEBUG
11+
//#define PACKETQUEUE_DEBUG
1212

1313
class PacketQueue
1414
{

PacketShared.h

+24-12
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,32 @@ namespace PacketShared{
2727
static const size_t DATA_BUFFER_SIZE = 32;
2828
// Packet structure
2929
struct Packet {
30-
byte data[DATA_BUFFER_SIZE];
31-
size_t length;
32-
byte flags;
30+
byte data[DATA_BUFFER_SIZE];
31+
size_t length;
32+
uint32_t timestamp;
33+
byte flags;
34+
};
35+
36+
enum InputPacketFlags {
37+
IPFLAG_IS_QUERY = 0x01,
38+
//IPFLAG_1 = 0x02,
39+
//IPFLAG_2 = 0x04,
40+
//IPFLAG_3 = 0x08,
41+
//IPFLAG_4 = 0x10,
42+
//IPFLAG_5 = 0x20,
43+
//IPFLAG_6 = 0x40,
44+
//IPFLAG_7 = 0x80
3345
};
3446

35-
enum PacketFlags {
36-
PFLAG_IS_QUERY = 0x01,
37-
//PFLAG_1 = 0x02,
38-
//PFLAG_2 = 0x04,
39-
//PFLAG_3 = 0x08,
40-
//PFLAG_4 = 0x10,
41-
//PFLAG_5 = 0x20,
42-
//PFLAG_6 = 0x40,
43-
//PFLAG_7 = 0x80
47+
enum OutputPacketFlags {
48+
OPFLAG_IS_QUERY = 0x01,
49+
OPFLAG_APPEND_SEND_TIMESTAMP = 0x02,
50+
//OPFLAG_2 = 0x04,
51+
//OPFLAG_3 = 0x08,
52+
//OPFLAG_4 = 0x10,
53+
//OPFLAG_5 = 0x20,
54+
//OPFLAG_6 = 0x40,
55+
//OPFLAG_7 = 0x80
4456
};
4557
};
4658

0 commit comments

Comments
 (0)