Skip to content

Commit

Permalink
second refactor of bitvector
Browse files Browse the repository at this point in the history
  • Loading branch information
marenz2569 committed Jun 6, 2024
1 parent 8366ad8 commit 2325e3e
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 66 deletions.
2 changes: 1 addition & 1 deletion include/l2/lower_mac.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class LowerMac {
-> void;
static auto deinterleave(const uint8_t* data, uint8_t* res, std::size_t K, std::size_t a) noexcept -> void;
[[nodiscard]] static auto depuncture23(const uint8_t* data, uint32_t len) noexcept -> std::vector<int16_t>;
static auto reed_muller_3014_decode(const uint8_t* data, bool res[14]) noexcept -> void;
static auto reed_muller_3014_decode(const uint8_t* data, std::array<bool, 14>& res) noexcept -> void;
[[nodiscard]] static auto check_crc_16_ccitt(const std::vector<bool>& data, std::size_t len) noexcept -> bool;

[[nodiscard]] auto viter_bi_decode_1614(const std::vector<int16_t>& data) const noexcept -> std::vector<bool>;
Expand Down
34 changes: 15 additions & 19 deletions include/utils/bit_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#pragma once

#include <cassert>
#include <cstddef>
#include <cstdint>
#include <ostream>
#include <vector>
Expand All @@ -19,7 +20,7 @@
class BitVector {
private:
/// The bits we hold
std::vector<bool> data_{};
std::vector<bool> data_;
/// The length of the currently viewed data to support taking bits from the back.
std::size_t len_ = 0;
/// The current read offset to support taking bits from the front.
Expand All @@ -29,24 +30,20 @@ class BitVector {
BitVector() = default;
explicit BitVector(const std::vector<bool>& vec)
: data_(vec)
, len_(vec.size())
, read_offset_(0){};
explicit BitVector(const BitVector& other)
: data_()
, len_(0)
, read_offset_(0) {
append(other);
};
BitVector(const std::vector<bool>::const_iterator iterator, const std::size_t len)
: data_(iterator, iterator + len)
, len_(len)
, read_offset_(0){};
BitVector(const bool* const iterator, const std::size_t len)
: data_(iterator, iterator + len)
, len_(len)
, read_offset_(0){};
, len_(vec.size()){};
explicit BitVector(std::vector<bool>&& vec)
: data_(std::move(vec))
, len_(vec.size()){};

BitVector(const BitVector&) = default;
auto operator=(const BitVector&) -> BitVector& = default;
BitVector(BitVector&&) = default;
auto operator=(BitVector&&) -> BitVector& = default;

~BitVector() noexcept = default;

[[nodiscard]] inline auto bits_left() const noexcept -> auto{ return len_; };

/// Append another bitvector to the current one. This will cause data to be copied.
auto append(const BitVector& other) -> void;

Expand Down Expand Up @@ -91,7 +88,6 @@ class BitVector {
/// take all the remaining bits
[[nodiscard]] auto take_all() -> uint64_t;

[[nodiscard]] inline auto bits_left() const noexcept -> std::size_t { return len_; };
[[nodiscard]] auto is_mac_padding() const noexcept -> bool;

friend auto operator<<(std::ostream& stream, const BitVector& vec) -> std::ostream&;
Expand All @@ -104,7 +100,7 @@ class BitVector {
// This condition is implicitly there on the first iteation of the loop, but not detected by some compilers
// leading to a false warning: shift count >= width of type [-Wshift-count-overflow] with N == 1
if (N > 1) {
for (std::size_t i = 1; i < N; i++) {
for (decltype(iterator)::difference_type i = 1; i < N; i++) {
ret <<= 1;
ret |= iterator[i];
}
Expand Down
67 changes: 43 additions & 24 deletions src/l2/lower_mac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ auto LowerMac::processChannels(const std::vector<uint8_t>& frame, BurstType burs
// ✅ done
uint8_t bb_desc[30];
descramble(frame.data() + 252, bb_desc, 30, bsc.scrambling_code);
bool bb_rm[14];
std::array<bool, 14> bb_rm;
reed_muller_3014_decode(bb_desc, bb_rm);
auto _aach = AccessAssignmentChannel(burst_type, bsc.time, BitVector(bb_rm, 14));
auto _aach =
AccessAssignmentChannel(burst_type, bsc.time, BitVector(std::vector(bb_rm.cbegin(), bb_rm.cend())));

// bkn2 block
// ✅ done
Expand All @@ -71,8 +72,10 @@ auto LowerMac::processChannels(const std::vector<uint8_t>& frame, BurstType burs
auto bkn2_bits = viter_bi_decode_1614(depuncture23(bkn2_dei, 216));
if (check_crc_16_ccitt(bkn2_bits, 140)) {
// SCH/HD or BNCH mapped
auto bkn2_bv = BitVector(bkn2_bits.cbegin(), 124);
functions.push_back(std::bind(&UpperMac::process_SCH_HD, upper_mac_, burst_type, bkn2_bv));
functions.emplace_back([this, burst_type, bkn2_bits] {
auto bkn2_bv = BitVector(std::vector(bkn2_bits.cbegin(), bkn2_bits.cbegin() + 124));
upper_mac_->process_SCH_HD(burst_type, bkn2_bv);
});
}
} else if (burst_type == BurstType::NormalDownlinkBurst) {
// bb contains AACH
Expand All @@ -82,9 +85,9 @@ auto LowerMac::processChannels(const std::vector<uint8_t>& frame, BurstType burs
vectorAppend(frame, bb, 266, 16);
uint8_t bb_desc[30];
descramble(bb.data(), bb_desc, 30, bsc.scrambling_code);
bool bb_rm[14];
std::array<bool, 14> bb_rm;
reed_muller_3014_decode(bb_desc, bb_rm);
auto aach = AccessAssignmentChannel(burst_type, bsc.time, BitVector(bb_rm, 14));
auto aach = AccessAssignmentChannel(burst_type, bsc.time, BitVector(std::vector(bb_rm.cbegin(), bb_rm.cend())));

// TCH or SCH/F
vectorAppend(frame, bkn1, 14, 216);
Expand All @@ -105,8 +108,10 @@ auto LowerMac::processChannels(const std::vector<uint8_t>& frame, BurstType burs
// control channel
// ✅done
if (check_crc_16_ccitt(bkn1_bits, 284)) {
functions.emplace_back(
std::bind(&UpperMac::process_SCH_F, upper_mac_, burst_type, BitVector(bkn1_bits.cbegin(), 268)));
functions.emplace_back([this, burst_type, bkn1_bits] {
auto bkn1_bv = BitVector(std::vector(bkn1_bits.cbegin(), bkn1_bits.cbegin() + 268));
upper_mac_->process_SCH_F(burst_type, bkn1_bv);
});
}
}
} else if (burst_type == BurstType::NormalDownlinkBurstSplit) {
Expand All @@ -117,9 +122,9 @@ auto LowerMac::processChannels(const std::vector<uint8_t>& frame, BurstType burs
vectorAppend(frame, bb, 266, 16);
uint8_t bb_desc[30];
descramble(bb.data(), bb_desc, 30, bsc.scrambling_code);
bool bb_rm[14];
std::array<bool, 14> bb_rm;
reed_muller_3014_decode(bb_desc, bb_rm);
auto aach = AccessAssignmentChannel(burst_type, bsc.time, BitVector(bb_rm, 14));
auto aach = AccessAssignmentChannel(burst_type, bsc.time, BitVector(std::vector(bb_rm.cbegin(), bb_rm.cend())));

uint8_t bkn1_desc[216];
uint8_t bkn2_desc[216];
Expand All @@ -130,7 +135,6 @@ auto LowerMac::processChannels(const std::vector<uint8_t>& frame, BurstType burs
uint8_t bkn1_dei[216];
deinterleave(bkn1_desc, bkn1_dei, 216, 101);
auto bkn1_bits = viter_bi_decode_1614(depuncture23(bkn1_dei, 216));
auto bkn1_bv = BitVector(bkn1_bits.cbegin(), 124);

uint8_t bkn2_dei[216];
deinterleave(bkn2_desc, bkn2_dei, 216, 101);
Expand All @@ -140,12 +144,18 @@ auto LowerMac::processChannels(const std::vector<uint8_t>& frame, BurstType burs
if (aach.downlink_usage == DownlinkUsage::Traffic) {
// STCH + TCH
// STCH + STCH
functions.emplace_back(std::bind(&UpperMac::process_STCH, upper_mac_, burst_type, bkn1_bv));
functions.emplace_back([this, burst_type, bkn1_bits] {
auto bkn1_bv = BitVector(std::vector(bkn1_bits.cbegin(), bkn1_bits.cbegin() + 124));
upper_mac_->process_STCH(burst_type, bkn1_bv);
});
} else {
// SCH/HD + SCH/HD
// SCH/HD + BNCH
// ✅done
functions.emplace_back(std::bind(&UpperMac::process_SCH_HD, upper_mac_, burst_type, bkn1_bv));
functions.emplace_back([this, burst_type, bkn1_bits] {
auto bkn1_bv = BitVector(std::vector(bkn1_bits.cbegin(), bkn1_bits.cbegin() + 124));
upper_mac_->process_SCH_HD(burst_type, bkn1_bv);
});
}
}

Expand All @@ -154,7 +164,7 @@ auto LowerMac::processChannels(const std::vector<uint8_t>& frame, BurstType burs
functions.emplace_back([this, burst_type, aach, bkn2_bits, bkn2_crc]() {
if (upper_mac_->second_slot_stolen()) {
if (bkn2_crc) {
auto bkn2_bv = BitVector(bkn2_bits.cbegin(), 124);
auto bkn2_bv = BitVector(std::vector(bkn2_bits.cbegin(), bkn2_bits.cbegin() + 124));
upper_mac_->process_STCH(burst_type, bkn2_bv);
}
} else {
Expand All @@ -166,8 +176,10 @@ auto LowerMac::processChannels(const std::vector<uint8_t>& frame, BurstType burs
} else {
// control channel
if (bkn2_crc) {
functions.emplace_back(
std::bind(&UpperMac::process_SCH_HD, upper_mac_, burst_type, BitVector(bkn2_bits.cbegin(), 124)));
functions.emplace_back([this, burst_type, bkn2_bits] {
auto bkn2_bv = BitVector(std::vector(bkn2_bits.cbegin(), bkn2_bits.cbegin() + 124));
upper_mac_->process_SCH_HD(burst_type, bkn2_bv);
});
}
}
} else if (burst_type == BurstType::ControlUplinkBurst) {
Expand All @@ -181,8 +193,10 @@ auto LowerMac::processChannels(const std::vector<uint8_t>& frame, BurstType burs
deinterleave(cb_desc, cb_dei, 168, 13);
auto cb_bits = viter_bi_decode_1614(depuncture23(cb_dei, 168));
if (check_crc_16_ccitt(cb_bits, 108)) {
functions.emplace_back(
std::bind(&UpperMac::process_SCH_HU, upper_mac_, burst_type, BitVector(cb_bits.cbegin(), 92)));
functions.emplace_back([this, burst_type, cb_bits] {
auto bkn1_bv = BitVector(std::vector(cb_bits.cbegin(), cb_bits.cbegin() + 92));
upper_mac_->process_SCH_HU(burst_type, bkn1_bv);
});
}
} else if (burst_type == BurstType::NormalUplinkBurst) {
vectorAppend(frame, bkn1, 4, 216);
Expand All @@ -197,8 +211,10 @@ auto LowerMac::processChannels(const std::vector<uint8_t>& frame, BurstType burs
auto bkn1_bits = viter_bi_decode_1614(depuncture23(bkn1_dei, 432));
if (check_crc_16_ccitt(bkn1_bits, 284)) {
// fmt::print("NUB Burst crc good\n");
functions.emplace_back(
std::bind(&UpperMac::process_SCH_F, upper_mac_, burst_type, BitVector(bkn1_bits.cbegin(), 268)));
functions.emplace_back([this, burst_type, bkn1_bits] {
auto bkn1_bv = BitVector(std::vector(bkn1_bits.cbegin(), bkn1_bits.cbegin() + 268));
upper_mac_->process_SCH_HU(burst_type, bkn1_bv);
});
} else {
// Either we have a faulty burst or a TCH here.
}
Expand All @@ -217,8 +233,10 @@ auto LowerMac::processChannels(const std::vector<uint8_t>& frame, BurstType burs
if (check_crc_16_ccitt(bkn1_bits, 140)) {
bkn1 = std::vector(bkn1.begin(), bkn1.begin() + 124);
// fmt::print("NUB_S 1 Burst crc good\n");
functions.emplace_back(
std::bind(&UpperMac::process_STCH, upper_mac_, burst_type, BitVector(bkn1_bits.cbegin(), 124)));
functions.emplace_back([this, burst_type, bkn1_bits] {
auto bkn1_bv = BitVector(std::vector(bkn1_bits.cbegin(), bkn1_bits.cbegin() + 124));
upper_mac_->process_STCH(burst_type, bkn1_bv);
});
}

uint8_t bkn2_dei[216];
Expand All @@ -229,7 +247,7 @@ auto LowerMac::processChannels(const std::vector<uint8_t>& frame, BurstType burs
functions.emplace_back([this, burst_type, bkn2_bits]() {
if (upper_mac_->second_slot_stolen()) {
// fmt::print("NUB_S 2 Burst crc good\n");
auto bkn2_bv = BitVector(bkn2_bits.cbegin(), 124);
auto bkn2_bv = BitVector(std::vector(bkn2_bits.cbegin(), bkn2_bits.cbegin() + 124));
upper_mac_->process_STCH(burst_type, bkn2_bv);
}
});
Expand Down Expand Up @@ -268,7 +286,8 @@ auto LowerMac::process(const std::vector<uint8_t>& frame, BurstType burst_type)
deinterleave(sb_desc, sb_dei, 120, 11);
auto sb_bits = viter_bi_decode_1614(depuncture23(sb_dei, 120));
if (check_crc_16_ccitt(sb_bits, 76)) {
current_sync = BroadcastSynchronizationChannel(burst_type, BitVector(sb_bits.cbegin(), 60));
current_sync = BroadcastSynchronizationChannel(
burst_type, BitVector(std::vector(sb_bits.cbegin(), sb_bits.cbegin() + 60)));
} else {
decode_error |= true;
}
Expand Down
Loading

0 comments on commit 2325e3e

Please sign in to comment.