Skip to content

Commit

Permalink
adding downlink frame counter capability to SystemManager (for tracki…
Browse files Browse the repository at this point in the history
…ng) and DownlinkBufferElement (for implementation of the packet header)
  • Loading branch information
thanasipantazides committed Nov 25, 2024
1 parent f91a7f6 commit bf998e1
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
27 changes: 27 additions & 0 deletions include/Buffers.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class DownlinkBufferElement {
const size_t get_max_packet_size() const {return max_packet_size;};
const uint16_t get_packets_per_frame() const {return packets_per_frame;};
const uint16_t get_this_packet_index() const {return this_packet_index;};
const uint16_t get_frame_counter() const {return frame_counter;};
const RING_BUFFER_TYPE_OPTIONS get_type() const {return type;};

/**
Expand All @@ -105,12 +106,20 @@ class DownlinkBufferElement {
* @warning Indexed from 1, not 0.
* @param new_this_packet_index the index of this packet in the frame.
*/

void set_this_packet_index(uint16_t new_this_packet_index);

/**
* @brief Set the data type transmitted in this frame.
* @param new_type type of data.
*/
void set_type(RING_BUFFER_TYPE_OPTIONS new_type);

/**
* @brief Set the frame counter for this packet.
* @param new_frame_counter frame number this packet comes from.
*/
void set_frame_counter(uint16_t new_frame_counter);

/**
* @brief Create a `std::string` representation of this object.
Expand Down Expand Up @@ -168,6 +177,15 @@ class DownlinkBufferElement {
* This will be used on the ground to log data to the appropriate file.
*/
RING_BUFFER_TYPE_OPTIONS type;

/**
* @brief A counter for each complete downlink frame to be sent.
* If this `DownlinkBufferElement` is a packet which is part of a larger
* frame which requires reassembly on the ground, this `::frame_counter`
* field can be used to identify dropped packets and still correctly
* assemble partial source frames.
*/
uint16_t frame_counter;
};


Expand Down Expand Up @@ -561,6 +579,14 @@ class SystemManager {
* @return PacketFramer*
*/
PacketFramer* get_packet_framer(RING_BUFFER_TYPE_OPTIONS type);
/**
* @brief Getter for total downlinked frame count for the provided data `type`.
* @param type data type of the frame you want the count for.
* @return uint8_t
*/
uint16_t get_frame_count(RING_BUFFER_TYPE_OPTIONS type);

void increment_frame_count(RING_BUFFER_TYPE_OPTIONS type);

/**
* @brief The underlying `System` configuration information.
Expand Down Expand Up @@ -608,6 +634,7 @@ class SystemManager {
private:
std::unordered_map<RING_BUFFER_TYPE_OPTIONS, FramePacketizer*> lookup_frame_packetizer;
std::unordered_map<RING_BUFFER_TYPE_OPTIONS, PacketFramer*> lookup_packet_framer;
std::unordered_map<RING_BUFFER_TYPE_OPTIONS, uint16_t> lookup_frame_count;

};

Expand Down
42 changes: 41 additions & 1 deletion src/Buffers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ DownlinkBufferElement::DownlinkBufferElement(System* new_system, size_t new_max_
// set these these to default for now:
DownlinkBufferElement::set_packets_per_frame(1);
DownlinkBufferElement::set_this_packet_index(1);
DownlinkBufferElement::set_frame_counter(0);
std::vector<uint8_t> temp_payload = {};
DownlinkBufferElement::set_payload(temp_payload);
type = RING_BUFFER_TYPE_OPTIONS::NONE;
Expand Down Expand Up @@ -54,6 +55,7 @@ DownlinkBufferElement::DownlinkBufferElement(System *from_system, System *to_sys
payload.reserve(max_packet_size - 8);
payload.resize(0);
type = new_type;
set_frame_counter(0);
}

DownlinkBufferElement &DownlinkBufferElement::operator=(const DownlinkBufferElement &other) {
Expand All @@ -63,6 +65,7 @@ DownlinkBufferElement &DownlinkBufferElement::operator=(const DownlinkBufferElem
packets_per_frame = other.packets_per_frame;
this_packet_index = other.this_packet_index;
type = other.type;
frame_counter = other.frame_counter;

return *this;
}
Expand All @@ -74,13 +77,15 @@ DownlinkBufferElement::DownlinkBufferElement(const DownlinkBufferElement &other)
DownlinkBufferElement::set_packets_per_frame(other.get_packets_per_frame());
DownlinkBufferElement::set_this_packet_index(other.get_this_packet_index());
DownlinkBufferElement::set_payload(other.get_payload());
DownlinkBufferElement::set_frame_counter(other.get_frame_counter());
}

DownlinkBufferElement::DownlinkBufferElement() {
system = nullptr;
max_packet_size = 0;
DownlinkBufferElement::set_packets_per_frame(1);
DownlinkBufferElement::set_this_packet_index(1);
DownlinkBufferElement::set_frame_counter(0);
std::vector<uint8_t> temp_payload = {};
DownlinkBufferElement::set_payload(temp_payload);
DownlinkBufferElement::set_type(RING_BUFFER_TYPE_OPTIONS::NONE);
Expand Down Expand Up @@ -117,6 +122,10 @@ void DownlinkBufferElement::set_type(RING_BUFFER_TYPE_OPTIONS new_type) {
type = new_type;
}

void DownlinkBufferElement::set_frame_counter(uint16_t new_frame_counter) {
frame_counter = new_frame_counter;
}

const std::string DownlinkBufferElement::to_string() {
std::string result;
result.append("DownlinkBufferElement::");
Expand Down Expand Up @@ -144,11 +153,13 @@ std::vector<uint8_t> DownlinkBufferElement::get_header() {
header.push_back(system->hex);
std::vector<uint8_t> bytes_packets_per_frame = utilities::splat_to_nbytes(2, get_packets_per_frame());
std::vector<uint8_t> bytes_packet_index = utilities::splat_to_nbytes(2, get_this_packet_index());
std::vector<uint8_t> bytes_frame_counter = utilities::splat_to_nbytes(2, get_frame_counter());
std::vector<uint8_t> reserved_pad = {0x00, 0x00};
header.insert(header.end(), bytes_packets_per_frame.begin(), bytes_packets_per_frame.end());
header.insert(header.end(), bytes_packet_index.begin(), bytes_packet_index.end());
header.push_back(static_cast<uint8_t>(type));
header.insert(header.end(), reserved_pad.begin(), reserved_pad.end());
// header.insert(header.end(), reserved_pad.begin(), reserved_pad.end());
header.insert(header.end(), bytes_frame_counter.begin(), bytes_frame_counter.end());

return header;
}
Expand Down Expand Up @@ -661,6 +672,9 @@ void SystemManager::clear_errors() {

void SystemManager::add_frame_packetizer(RING_BUFFER_TYPE_OPTIONS new_type, FramePacketizer* new_frame_packetizer) {
lookup_frame_packetizer[new_type] = new_frame_packetizer;
if (!lookup_frame_count.contains(new_type)) {
lookup_frame_count.insert(std::make_pair(new_type, 0x00));
}
// auto it = lookup_frame_packetizer.find(new_type);
// if (it != lookup_frame_packetizer.end()) {
// it->second = new_frame_packetizer;
Expand All @@ -676,6 +690,11 @@ void SystemManager::add_packet_framer(RING_BUFFER_TYPE_OPTIONS new_type, PacketF
} else {
lookup_packet_framer.insert(std::make_pair(new_type, new_packet_framer));
}

// add this type to the frame_counter lookup as well:
if (!lookup_frame_count.contains(new_type)) {
lookup_frame_count.insert(std::make_pair(new_type, 0x00));
}
}

void SystemManager::add_timing(Timing *new_timing) {
Expand Down Expand Up @@ -705,3 +724,24 @@ FramePacketizer* SystemManager::get_frame_packetizer(RING_BUFFER_TYPE_OPTIONS ty
return nullptr;
}
}

uint16_t SystemManager::get_frame_count(RING_BUFFER_TYPE_OPTIONS type) {
auto it = lookup_frame_count.find(type);
if (it != lookup_frame_count.end()) {
return it->second;
} else {
// utilities::error_print("could not find ring buffer type in FramePacketizer map!\n");
utilities::error_log("SystemManager::get_frame_count()\tcould not find buffer type: " + RING_BUFFER_TYPE_OPTIONS_NAMES.at(type));
errors |= errors::system::buffer_lookup;
return 0x00;
}
}

void SystemManager::increment_frame_count(RING_BUFFER_TYPE_OPTIONS type) {
if (lookup_frame_count.find(type) != lookup_frame_count.end()) {
lookup_frame_count[type]++;
} else {
utilities::error_log("SystemManager::increment_frame_count()\tcould not find buffer type: " + RING_BUFFER_TYPE_OPTIONS_NAMES.at(type));
errors |= errors::system::buffer_lookup;
}
}
2 changes: 2 additions & 0 deletions src/TransportLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -865,9 +865,11 @@ size_t TransportLayerMachine::sync_remote_buffer_transaction(SystemManager &sys_
while (!fp->frame_emptied()) {
// fp.pop_buffer_element()
DownlinkBufferElement this_dbe(fp->pop_buffer_element());
this_dbe.set_frame_counter(sys_man.get_frame_count(buffer_type));
downlink_buffer->enqueue(this_dbe);
// utilities::debug_print("\t\tqueued packet\n");
}
sys_man.increment_frame_count(buffer_type);
utilities::debug_log("TransportLayerMachine::sync_remote_buffer_transaction()\tqueued downlink packets.");

// clear the old frame to use the PacketFramer again.
Expand Down

0 comments on commit bf998e1

Please sign in to comment.