Commit d024340d authored by rna's avatar rna

Encode/decode macros. Fixed packet headers.

parent 39648ec6
Pipeline #2564 failed with stages
in 1 minute and 14 seconds
......@@ -31,7 +31,7 @@ uint8_t PrrtPacket_priority(PrrtPacket *packet_ptr) {
}
prrtPacketLength_t PrrtPacket_size(PrrtPacket *packet_ptr) {
return (prrtPacketLength_t) (packet_ptr->payloadLength + PRRT_PACKET_GENERAL_HEADER_SIZE);
return (prrtPacketLength_t) (packet_ptr->payloadLength + PRRT_PACKET_ENCODED_GENERAL_HEADER_LENGTH);
}
int PrrtPacket_print(PrrtPacket *packet_ptr) {
......@@ -133,7 +133,7 @@ create_header(uint8_t priority, prrtSequenceNumber_t seqno, prrtPacketLength_t s
bool PrrtPacket_encode(void *buf_ptr, uint16_t buf_size, PrrtPacket *packet_ptr) {
void *payload = packet_ptr->payload;
check(packet_ptr->payloadLength + PRRT_PACKET_GENERAL_HEADER_SIZE <= buf_size, "Buffer too small.");
check(packet_ptr->payloadLength + PRRT_PACKET_ENCODED_GENERAL_HEADER_LENGTH <= buf_size, "Buffer too small.");
buf_ptr = encode_general_header(buf_ptr, packet_ptr);
......@@ -156,24 +156,22 @@ bool PrrtPacket_encode(void *buf_ptr, uint16_t buf_size, PrrtPacket *packet_ptr)
return false;
}
void *encode_redundancy_header(void *buf_ptr, const void *payload) {
const PrrtPacketRedundancyPayload *redundancyPayload = payload;
prrtSequenceNumber_t *baseSeqNo = (prrtSequenceNumber_t *) buf_ptr;
*baseSeqNo = htons(redundancyPayload->baseSequenceNumber);
buf_ptr += sizeof(prrtSequenceNumber_t);
#define identity(x) (x)
prrtTimestamp_t *timestamp = (prrtTimestamp_t *) buf_ptr;
*timestamp = htonl(redundancyPayload->timestamp);
buf_ptr += sizeof(prrtTimestamp_t);
#define PrrtPacketField_encode(payload, buf_ptr, field_t, field, conversion) \
do { \
field_t *ptr = (field_t *) (buf_ptr); \
*ptr = conversion((payload)->field); \
(buf_ptr) += sizeof(field_t); \
} while (false)
uint8_t *n = (uint8_t *) buf_ptr;
*n = redundancyPayload->n;
buf_ptr += sizeof(uint8_t);
void *encode_redundancy_header(void *buf_ptr, const void *payload) {
const PrrtPacketRedundancyPayload *redundancyPayload = payload;
uint8_t *k = (uint8_t *) buf_ptr;
*k = redundancyPayload->k;
buf_ptr += sizeof(uint8_t);
PrrtPacketField_encode(redundancyPayload, buf_ptr, prrtSequenceNumber_t, baseSequenceNumber, htons);
PrrtPacketField_encode(redundancyPayload, buf_ptr, prrtTimestamp_t, timestamp, htonl);
PrrtPacketField_encode(redundancyPayload, buf_ptr, uint8_t, n, identity);
PrrtPacketField_encode(redundancyPayload, buf_ptr, uint8_t, k, identity);
return buf_ptr;
}
......@@ -181,45 +179,16 @@ void *encode_redundancy_header(void *buf_ptr, const void *payload) {
void *encode_feedback_header(void *buf_ptr, const void *payload) {
const PrrtPacketFeedbackPayload *feedbackPayload = payload;
prrtTimedelta_t *groupRoundTripTime = (prrtTimedelta_t *) buf_ptr;
*groupRoundTripTime = htonl(feedbackPayload->groupRTT_us);
buf_ptr += sizeof(prrtTimedelta_t);
prrtTimestamp_t *forwardTripTimestamp = (prrtTimestamp_t *) buf_ptr;
*forwardTripTimestamp = htonl(feedbackPayload->forwardTripTimestamp_us);
buf_ptr += sizeof(prrtTimestamp_t);
prrtSequenceNumber_t *erasureCount = (prrtSequenceNumber_t *) buf_ptr;
*erasureCount = htons(feedbackPayload->erasureCount);
buf_ptr += sizeof(prrtSequenceNumber_t);
prrtSequenceNumber_t *packetCount = (prrtSequenceNumber_t *) buf_ptr;
*packetCount = htons(feedbackPayload->packetCount);
buf_ptr += sizeof(prrtSequenceNumber_t);
prrtSequenceNumber_t *gap = (prrtSequenceNumber_t *) buf_ptr;
*gap = htons(feedbackPayload->gapLength);
buf_ptr += sizeof(prrtSequenceNumber_t);
prrtSequenceNumber_t *ngap = (prrtSequenceNumber_t *) buf_ptr;
*ngap = htons(feedbackPayload->gapCount);
buf_ptr += sizeof(prrtSequenceNumber_t);
prrtSequenceNumber_t *burstLength = (prrtSequenceNumber_t *) buf_ptr;
*burstLength = htons(feedbackPayload->burstLength);
buf_ptr += sizeof(prrtSequenceNumber_t);
prrtSequenceNumber_t *burstCount = (prrtSequenceNumber_t *) buf_ptr;
*burstCount = htons(feedbackPayload->burstCount);
buf_ptr += sizeof(prrtSequenceNumber_t);
prrtPacketType_t *ackPacketType = (prrtPacketType_t *) buf_ptr;
*ackPacketType = feedbackPayload->ackPacketType;
buf_ptr += sizeof(prrtPacketType_t);
prrtSequenceNumber_t *ackSequenceNumber = (prrtSequenceNumber_t *) buf_ptr;
*ackSequenceNumber = htons(feedbackPayload->ackSequenceNumber);
buf_ptr += sizeof(prrtSequenceNumber_t);
PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtTimedelta_t, groupRTT_us, htonl);
PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtTimestamp_t, forwardTripTimestamp_us, htonl);
PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtSequenceNumber_t, erasureCount, htons);
PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtSequenceNumber_t, packetCount, htons);
PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtSequenceNumber_t, gapLength, htons);
PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtSequenceNumber_t, gapCount, htons);
PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtSequenceNumber_t, burstLength, htons);
PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtSequenceNumber_t, burstCount, htons);
PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtPacketType_t, ackPacketType, identity);
PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtSequenceNumber_t, ackSequenceNumber, htons);
return buf_ptr;
}
......@@ -227,43 +196,25 @@ void *encode_feedback_header(void *buf_ptr, const void *payload) {
void *encode_data_header(void *buf_ptr, const void *payload) {
const PrrtPacketDataPayload *data_payload = payload;
prrtPacketLength_t *packetLength = (prrtPacketLength_t *) buf_ptr;
*packetLength = htonl(data_payload->dataLength);
buf_ptr += sizeof(prrtPacketLength_t);
prrtTimestamp_t *timestamp = (prrtTimestamp_t *) buf_ptr;
*timestamp = htonl(data_payload->timestamp);
buf_ptr += sizeof(prrtTimestamp_t);
prrtTimedelta_t *group_round_trip_time = (prrtTimedelta_t *) buf_ptr;
*group_round_trip_time = htonl(data_payload->groupRTprop_us);
buf_ptr += sizeof(prrtTimedelta_t);
prrtTimedelta_t *packet_timeout = (prrtTimedelta_t *) buf_ptr;
*packet_timeout = htonl(data_payload->packetTimeout_us);
buf_ptr += sizeof(prrtTimedelta_t);
PrrtPacketField_encode(data_payload, buf_ptr, prrtPacketLength_t, dataLength, htonl);
PrrtPacketField_encode(data_payload, buf_ptr, prrtTimestamp_t, timestamp, htonl);
PrrtPacketField_encode(data_payload, buf_ptr, prrtTimedelta_t, groupRTprop_us, htonl);
PrrtPacketField_encode(data_payload, buf_ptr, prrtTimedelta_t, packetTimeout_us, htonl);
return buf_ptr;
}
void *encode_general_header(void *buf_ptr, const PrrtPacket *packet) {
uint8_t *type_priority = (uint8_t *) buf_ptr;
*type_priority = packet->type_priority;
buf_ptr += sizeof(uint8_t);
uint8_t *index = (uint8_t *) buf_ptr;
*index = packet->index;
buf_ptr += sizeof(uint8_t);
prrtSequenceNumber_t *seqno = (prrtSequenceNumber_t *) buf_ptr;
*seqno = htons(packet->sequenceNumber);
buf_ptr += sizeof(prrtSequenceNumber_t);
PrrtPacketField_encode(packet, buf_ptr, uint8_t, type_priority, identity);
PrrtPacketField_encode(packet, buf_ptr, uint8_t, index, identity);
PrrtPacketField_encode(packet, buf_ptr, prrtSequenceNumber_t, sequenceNumber, htons);
return buf_ptr;
}
bool PrrtPacket_decode(void *srcBuffer, uint16_t srcBufferSize, PrrtPacket *targetPacket) {
prrtPacketLength_t payload_len = (prrtPacketLength_t) (srcBufferSize - PRRT_PACKET_GENERAL_HEADER_SIZE);
prrtPacketLength_t payload_len = (prrtPacketLength_t) (srcBufferSize - PRRT_PACKET_ENCODED_GENERAL_HEADER_LENGTH);
targetPacket->type_priority = *(uint8_t *) srcBuffer;
srcBuffer += 1;
uint8_t *index_ptr = (uint8_t *) srcBuffer;
......@@ -297,24 +248,20 @@ bool PrrtPacket_decode(void *srcBuffer, uint16_t srcBufferSize, PrrtPacket *targ
return false;
}
#define PrrtPacketField_decode(payload, buf_ptr, field_t, field, conversion) \
do { \
field_t *PrrtPacketField_decode_ptr = (field_t *) (buf_ptr); \
(payload)->field = conversion(*PrrtPacketField_decode_ptr); \
(buf_ptr) += sizeof(field_t); \
} while (false)
void *decode_redundancy_header(void *dstBuffer, const void *srcBuffer) {
PrrtPacketRedundancyPayload *redundancyPayload = (PrrtPacketRedundancyPayload *) srcBuffer;
prrtSequenceNumber_t *baseSeqNo = (prrtSequenceNumber_t *) dstBuffer;
redundancyPayload->baseSequenceNumber = ntohs(*baseSeqNo);
dstBuffer += sizeof(prrtSequenceNumber_t);
prrtTimestamp_t *timestamp = (prrtTimestamp_t*) dstBuffer;
redundancyPayload->timestamp = ntohl(*timestamp);
dstBuffer += sizeof(prrtTimestamp_t);
uint8_t *n = (uint8_t *) dstBuffer;
redundancyPayload->n = *n;
dstBuffer += sizeof(uint8_t);
uint8_t *k = (uint8_t *) dstBuffer;
redundancyPayload->k = *k;
dstBuffer += sizeof(uint8_t);
PrrtPacketField_decode(redundancyPayload, dstBuffer, prrtSequenceNumber_t, baseSequenceNumber, ntohs);
PrrtPacketField_decode(redundancyPayload, dstBuffer, prrtTimestamp_t, timestamp, ntohl);
PrrtPacketField_decode(redundancyPayload, dstBuffer, uint8_t, n, identity);
PrrtPacketField_decode(redundancyPayload, dstBuffer, uint8_t, k, identity);
return dstBuffer;
}
......@@ -322,45 +269,16 @@ void *decode_redundancy_header(void *dstBuffer, const void *srcBuffer) {
void *decode_feedback_header(void *dstBuffer, const void *srcBuffer) {
PrrtPacketFeedbackPayload *feedback_payload = (PrrtPacketFeedbackPayload *) srcBuffer;
prrtTimedelta_t *groupRoundTripTime = (prrtTimedelta_t *) dstBuffer;
feedback_payload->groupRTT_us = ntohl(*groupRoundTripTime);
dstBuffer += sizeof(prrtTimedelta_t);
prrtTimestamp_t *forwardTripTime = (prrtTimestamp_t *) dstBuffer;
feedback_payload->forwardTripTimestamp_us = ntohl(*forwardTripTime);
dstBuffer += sizeof(prrtTimestamp_t);
prrtSequenceNumber_t *erasureCount = (prrtSequenceNumber_t *) dstBuffer;
feedback_payload->erasureCount = ntohs(*erasureCount);
dstBuffer += sizeof(prrtSequenceNumber_t);
prrtSequenceNumber_t *packetCount = (prrtSequenceNumber_t *) dstBuffer;
feedback_payload->packetCount = ntohs(*packetCount);
dstBuffer += sizeof(prrtSequenceNumber_t);
prrtSequenceNumber_t *gap = (prrtSequenceNumber_t *) dstBuffer;
feedback_payload->gapLength = ntohs(*gap);
dstBuffer += sizeof(prrtSequenceNumber_t);
prrtSequenceNumber_t *ngap = (prrtSequenceNumber_t *) dstBuffer;
feedback_payload->gapCount = ntohs(*ngap);
dstBuffer += sizeof(prrtSequenceNumber_t);
prrtSequenceNumber_t *burst = (prrtSequenceNumber_t *) dstBuffer;
feedback_payload->burstLength = ntohs(*burst);
dstBuffer += sizeof(prrtSequenceNumber_t);
prrtSequenceNumber_t *nburst = (prrtSequenceNumber_t *) dstBuffer;
feedback_payload->burstCount = ntohs(*nburst);
dstBuffer += sizeof(prrtSequenceNumber_t);
prrtPacketType_t *ackPacketType = (prrtPacketType_t *) dstBuffer;
feedback_payload->ackPacketType = *ackPacketType;
dstBuffer += sizeof(prrtPacketType_t);
prrtSequenceNumber_t *ackSequenceNumber = (prrtSequenceNumber_t *) dstBuffer;
feedback_payload->ackSequenceNumber = ntohs(*ackSequenceNumber);
dstBuffer += sizeof(prrtSequenceNumber_t);
PrrtPacketField_decode(feedback_payload, dstBuffer, prrtTimedelta_t, groupRTT_us, ntohl);
PrrtPacketField_decode(feedback_payload, dstBuffer, prrtTimestamp_t, forwardTripTimestamp_us, ntohl);
PrrtPacketField_decode(feedback_payload, dstBuffer, prrtSequenceNumber_t, erasureCount, ntohs);
PrrtPacketField_decode(feedback_payload, dstBuffer, prrtSequenceNumber_t, packetCount, ntohs);
PrrtPacketField_decode(feedback_payload, dstBuffer, prrtSequenceNumber_t, gapLength, ntohs);
PrrtPacketField_decode(feedback_payload, dstBuffer, prrtSequenceNumber_t, gapCount, ntohs);
PrrtPacketField_decode(feedback_payload, dstBuffer, prrtSequenceNumber_t, burstLength, ntohs);
PrrtPacketField_decode(feedback_payload, dstBuffer, prrtSequenceNumber_t, burstCount, ntohs);
PrrtPacketField_decode(feedback_payload, dstBuffer, prrtPacketType_t, ackPacketType, identity);
PrrtPacketField_decode(feedback_payload, dstBuffer, prrtSequenceNumber_t, ackSequenceNumber, ntohs);
return dstBuffer;
}
......@@ -368,21 +286,10 @@ void *decode_feedback_header(void *dstBuffer, const void *srcBuffer) {
void *decode_data_header(void *dstBuffer, const void *srcBuffer) {
PrrtPacketDataPayload *data_payload = (PrrtPacketDataPayload *) srcBuffer;
prrtPacketLength_t *dataLength = (prrtPacketLength_t *) dstBuffer;
data_payload->dataLength = ntohl(*dataLength);
dstBuffer += sizeof(prrtPacketLength_t);
prrtTimestamp_t *timestamp = (prrtTimestamp_t *) dstBuffer;
data_payload->timestamp = ntohl(*timestamp);
dstBuffer += sizeof(prrtTimestamp_t);
prrtTimedelta_t *group_round_trip_time = (prrtTimedelta_t *) dstBuffer;
data_payload->groupRTprop_us = ntohl(*group_round_trip_time);
dstBuffer += sizeof(prrtTimedelta_t);
prrtTimedelta_t *packet_timeout = (prrtTimedelta_t *) dstBuffer;
data_payload->packetTimeout_us = ntohl(*packet_timeout);
dstBuffer += sizeof(prrtTimedelta_t);
PrrtPacketField_decode(data_payload, dstBuffer, prrtPacketLength_t, dataLength, ntohl);
PrrtPacketField_decode(data_payload, dstBuffer, prrtTimestamp_t, timestamp, ntohl);
PrrtPacketField_decode(data_payload, dstBuffer, prrtTimedelta_t, groupRTprop_us, ntohl);
PrrtPacketField_decode(data_payload, dstBuffer, prrtTimedelta_t, packetTimeout_us, ntohl);
return dstBuffer;
}
......
......@@ -15,7 +15,7 @@ typedef enum {
PACKET_TYPE_FEEDBACK = 3,
PACKET_TYPE_PRESENT_REDUNDANCY = 4,
PACKET_TYPE_CHANNEL_FEEDBACK = 5
} prrtPacketType_t;
} prrtPacketTypeEnum_t;
typedef uint16_t prrtSequenceNumber_t;
typedef uint8_t prrtIndex_t;
......@@ -26,7 +26,7 @@ typedef uint32_t prrtPacketLength_t; // bytes
typedef float prrtPacketLossRate_t;
typedef uint32_t prrtDeliveryRate_t; // bits per second
typedef uint32_t prrtByteCount_t;
typedef uint8_t prrtPacketType_t;
typedef struct prrtIncompleteBlock {
prrtSequenceNumber_t sequenceNumberBase;
......@@ -60,7 +60,12 @@ typedef struct prrtPacketDataPayload {
prrtTimedelta_t groupRTprop_us;
prrtTimestamp_t packetTimeout_us;
} PrrtPacketDataPayload;
#define PRRT_PACKET_DATA_HEADER_SIZE sizeof(PrrtPacketDataPayload)
#define PRRT_PACKET_DATA_HEADER_SIZE (\
sizeof(prrtPacketLength_t) + \
sizeof(prrtTimestamp_t) + \
sizeof(prrtTimedelta_t) + \
sizeof(prrtTimestamp_t) )
typedef struct prrtPacketRedundancyPayload {
prrtSequenceNumber_t baseSequenceNumber;
......@@ -68,7 +73,12 @@ typedef struct prrtPacketRedundancyPayload {
uint8_t n;
uint8_t k;
} PrrtPacketRedundancyPayload;
#define PRRT_PACKET_REDUNDANCY_HEADER_SIZE sizeof(PrrtPacketRedundancyPayload)
#define PRRT_PACKET_REDUNDANCY_HEADER_SIZE (\
sizeof(prrtSequenceNumber_t) + \
sizeof(prrtTimestamp_t) + \
sizeof(uint8_t) + \
sizeof(uint8_t) )
typedef struct prrtPacketFeedbackPayload {
prrtTimedelta_t groupRTT_us;
......@@ -83,7 +93,18 @@ typedef struct prrtPacketFeedbackPayload {
prrtPacketType_t ackPacketType;
PrrtIncompleteBlock *incompleteBlocks;
} PrrtPacketFeedbackPayload;
#define PRRT_PACKET_FEEDBACK_HEADER_SIZE (sizeof(PrrtPacketFeedbackPayload) - sizeof(PrrtIncompleteBlock*))
#define PRRT_PACKET_FEEDBACK_HEADER_SIZE (\
sizeof(prrtTimedelta_t) + \
sizeof(prrtTimestamp_t) + \
sizeof(prrtSequenceNumber_t) + \
sizeof(prrtSequenceNumber_t) + \
sizeof(prrtSequenceNumber_t) + \
sizeof(prrtSequenceNumber_t) + \
sizeof(prrtSequenceNumber_t) + \
sizeof(prrtSequenceNumber_t) + \
sizeof(prrtSequenceNumber_t) + \
sizeof(prrtPacketType_t) )
prrtPacketType_t PrrtPacket_type(PrrtPacket *packet_ptr);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment