Skip to content

Commit

Permalink
optimize imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Consti10 committed Aug 13, 2023
1 parent e57bec7 commit 646eb16
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
3 changes: 3 additions & 0 deletions executables/wfb_keygen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

#include <getopt.h>

#include <iostream>
#include <optional>

#include "../src/Encryption.h"

/**
Expand Down
13 changes: 7 additions & 6 deletions src/FEC.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ void fecEncode(unsigned int fragmentSize,
//std::cout<<"fec_encode step took:"<<std::chrono::duration_cast<std::chrono::microseconds>(delta).count()<<"us\n";
}

enum FragmentStatus { UNAVAILABLE = 0, AVAILABLE = 1 };
static constexpr auto FRAGMENT_STATUS_UNAVAILABLE=false;
static constexpr auto FRAGMENT_STATUS_AVAILABLE=true;

/**
* @param fragmentSize size of each fragment
Expand All @@ -66,14 +67,14 @@ template<std::size_t S>
std::vector<unsigned int> fecDecode(unsigned int fragmentSize,
std::vector<std::array<uint8_t, S>> &blockBuffer,
const unsigned int nPrimaryFragments,
const std::vector<FragmentStatus> &fragmentStatusList) {
const std::vector<bool> &fragmentStatusList) {
assert(fragmentSize <= S);
assert(fragmentStatusList.size() <= blockBuffer.size());
assert(fragmentStatusList.size() == blockBuffer.size());
std::vector<unsigned int> indicesMissingPrimaryFragments;
std::vector<uint8_t *> primaryFragmentP(nPrimaryFragments);
for (unsigned int idx = 0; idx < nPrimaryFragments; idx++) {
if (fragmentStatusList[idx] == UNAVAILABLE) {
if (fragmentStatusList[idx] == FRAGMENT_STATUS_UNAVAILABLE) {
indicesMissingPrimaryFragments.push_back(idx);
}
primaryFragmentP[idx] = blockBuffer[idx].data();
Expand All @@ -83,7 +84,7 @@ std::vector<unsigned int> fecDecode(unsigned int fragmentSize,
std::vector<unsigned int> secondaryFragmentIndices;
for (int i = 0; i < fragmentStatusList.size() - nPrimaryFragments; i++) {
const auto idx = nPrimaryFragments + i;
if (fragmentStatusList[idx] == AVAILABLE) {
if (fragmentStatusList[idx] == FRAGMENT_STATUS_AVAILABLE) {
secondaryFragmentP.push_back(blockBuffer[idx].data());
secondaryFragmentIndices.push_back(i);
}
Expand Down Expand Up @@ -124,10 +125,10 @@ static void testFecCPlusPlusWrapperY(const int nPrimaryFragments, const int nSec
std::cout << "(Emulated) receivedFragmentIndices" << StringHelper::vectorAsString(receivedFragmentIndices) << "\n";

auto rxBlockBuffer = std::vector<std::array<uint8_t, FRAGMENT_SIZE>>(nPrimaryFragments + nSecondaryFragments);
std::vector<FragmentStatus> fragmentMap(nPrimaryFragments + nSecondaryFragments, FragmentStatus::UNAVAILABLE);
std::vector<bool> fragmentMap(nPrimaryFragments + nSecondaryFragments, FRAGMENT_STATUS_UNAVAILABLE);
for (const auto idx: receivedFragmentIndices) {
rxBlockBuffer[idx] = txBlockBuffer[idx];
fragmentMap[idx] = FragmentStatus::AVAILABLE;
fragmentMap[idx] = FRAGMENT_STATUS_AVAILABLE;
}

fecDecode(FRAGMENT_SIZE, rxBlockBuffer, nPrimaryFragments, fragmentMap);
Expand Down
16 changes: 9 additions & 7 deletions src/FECEnabled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "FECEnabled.h"
#include "wifibroadcast-spdlog.h"

#include "FEC.hpp"

void FECEncoder::encode_block(
std::vector<std::shared_ptr<std::vector<uint8_t>>> data_packets,
int n_secondary_fragments) {
Expand Down Expand Up @@ -87,7 +89,7 @@ void FECEncoder::encode_block(

bool RxBlock::hasFragment(const int fragment_idx) {
assert(fragment_idx<fragment_map.size());
return fragment_map[fragment_idx] == AVAILABLE;
return fragment_map[fragment_idx] == FRAGMENT_STATUS_AVAILABLE;
}

bool RxBlock::allPrimaryFragmentsHaveBeenForwarded() const {
Expand Down Expand Up @@ -115,11 +117,11 @@ void RxBlock::addFragment(const uint8_t* data, const std::size_t dataLen) {
FECPayloadHdr& header=*hdr_p;
assert(!hasFragment(header.fragment_idx));
assert(header.block_idx == blockIdx);
assert(fragment_map[header.fragment_idx] == UNAVAILABLE);
assert(fragment_map[header.fragment_idx] == FRAGMENT_STATUS_UNAVAILABLE);
assert(header.fragment_idx < blockBuffer.size());
fragment_copy_payload(header.fragment_idx,data,dataLen);
// mark it as available
fragment_map[header.fragment_idx] = FragmentStatus::AVAILABLE;
fragment_map[header.fragment_idx] = FRAGMENT_STATUS_AVAILABLE;

// each fragment inside a block should report the same n of primary fragments
if(m_n_primary_fragments_in_block ==-1){
Expand Down Expand Up @@ -162,7 +164,7 @@ std::vector<uint16_t> RxBlock::pullAvailablePrimaryFragments(
// note: when pulling the available fragments, we do not need to know how many primary fragments this block actually contains
std::vector<uint16_t> ret;
for (int i = nAlreadyForwardedPrimaryFragments; i < m_n_available_primary_fragments; i++) {
if (fragment_map[i] == FragmentStatus::UNAVAILABLE) {
if (fragment_map[i] == FRAGMENT_STATUS_UNAVAILABLE) {
if (discardMissingPackets) {
continue;
} else {
Expand All @@ -177,15 +179,15 @@ std::vector<uint16_t> RxBlock::pullAvailablePrimaryFragments(
}

const uint8_t* RxBlock::get_primary_fragment_data_p(const int fragment_index) {
assert(fragment_map[fragment_index] == AVAILABLE);
assert(fragment_map[fragment_index] == FRAGMENT_STATUS_AVAILABLE);
assert(m_n_primary_fragments_in_block !=-1);
assert(fragment_index< m_n_primary_fragments_in_block);
//return blockBuffer[fragment_index].data()+sizeof(FECPayloadHdr);
return blockBuffer[fragment_index].data()+sizeof(uint16_t);
}

const int RxBlock::get_primary_fragment_data_size(const int fragment_index) {
assert(fragment_map[fragment_index] == AVAILABLE);
assert(fragment_map[fragment_index] == FRAGMENT_STATUS_AVAILABLE);
assert(m_n_primary_fragments_in_block !=-1);
assert(fragment_index< m_n_primary_fragments_in_block);
uint16_t* len_p=(uint16_t*)blockBuffer[fragment_index].data();
Expand All @@ -205,7 +207,7 @@ int RxBlock::reconstructAllMissingData() {
m_n_primary_fragments_in_block, fragment_map);
// now mark them as available
for (const auto idx: recoveredFragmentIndices) {
fragment_map[idx] = AVAILABLE;
fragment_map[idx] = FRAGMENT_STATUS_AVAILABLE;
}
m_n_available_primary_fragments += recoveredFragmentIndices.size();
// n of reconstructed packets
Expand Down
4 changes: 2 additions & 2 deletions src/FECEnabled.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class RxBlock {
// allocate much more memory every time for a new RX block than needed.
explicit RxBlock(const unsigned int maxNFragmentsPerBlock, const uint64_t blockIdx1)
:blockIdx(blockIdx1),
fragment_map(maxNFragmentsPerBlock,FragmentStatus::UNAVAILABLE), //after creation of the RxBlock every f. is marked as unavailable
fragment_map(maxNFragmentsPerBlock,FRAGMENT_STATUS_UNAVAILABLE), //after creation of the RxBlock every f. is marked as unavailable
blockBuffer(maxNFragmentsPerBlock) {
assert(fragment_map.size() == blockBuffer.size());
}
Expand Down Expand Up @@ -185,7 +185,7 @@ class RxBlock {
// n of primary fragments that are already pulled out
int nAlreadyForwardedPrimaryFragments = 0;
// for each fragment (via fragment_idx) store if it has been received yet
std::vector<FragmentStatus> fragment_map;
std::vector<bool> fragment_map;
// holds all the data for all received fragments (if fragment_map says UNAVALIABLE at this position, content is undefined)
std::vector<std::array<uint8_t, MAX_PAYLOAD_BEFORE_FEC>> blockBuffer;
// time point when the first fragment for this block was received (via addFragment() )
Expand Down

0 comments on commit 646eb16

Please sign in to comment.