29#define MAC_CRC_SIZE 2u
30#define MAX_PWR_QUEUE_SIZE 4
31#define SM(value, shift, mask) (((value) << (shift)) & (mask))
32#define MS(value, mask, shift) (((value) & (mask)) >> (shift))
62 return ((i % n) + n) % n;
79 inline T
clamp(
const T d,
const T min,
const T max) {
80 const T t = d < min ? min : d;
81 return t > max ? max : t;
96 inline uint32_t
rotl(uint32_t bits, uint32_t count = 1u,
const uint32_t size = 8u) {
97 const uint32_t len_mask = (1u << size) - 1u;
102 return ((bits << count) & len_mask) | (bits >> (size - count));
115 template <
typename T>
116 inline std::string
to_bin(
const T v,
const uint32_t bitwidth) {
118 const uint64_t maxpow = bitwidth ? (1ull << (bitwidth - 1)) : 0;
121 std::string result =
"";
123 for (mask = 0x1; mask <= maxpow; mask <<= 1) {
124 result += (v & mask) ?
"1" :
"0";
127 uint64_t mask = bitwidth ? (1ull << bitwidth) : 0;
129 std::string result =
"";
132 result += (v & mask) ?
"1" :
"0";
153 template <
typename T>
154 inline void print_vector_bin(std::ostream& out,
const std::vector<T>& v,
const std::string& prefix,
const int element_len_bits) {
155 out << prefix <<
": ";
158 out <<
to_bin(x, element_len_bits) <<
", ";
160 out << std::endl << std::flush;
172 size_t count = 0, i = 0;
175 if (word[i++] ==
'1')
179 return (count & 0x1) == !even;
194 word = (word & 0x1111111111111111UL) * 0x1111111111111111UL;
196 return ((word >> 60ull) & 1ull) == !even;
209 inline uint32_t
select_bits(
const uint32_t data,
const uint8_t *indices,
const uint8_t n) {
212 for(uint8_t i = 0u; i < n; ++i)
213 r |= (data & (1u << indices[i])) ? (1u << i) : 0u;
226 inline uint8_t
bit(
const uint8_t v,
const uint8_t i) {
227 return ((v >> i) & 0x01);
236 inline uint8_t
pack_byte(
const uint8_t a,
const uint8_t b,
const uint8_t c,
const uint8_t d,
237 const uint8_t e,
const uint8_t f,
const uint8_t g,
const uint8_t h) {
238 return a | (b << 1) | (c << 2) | (d << 3) | (e << 4) | (f << 5) | (g << 6) | (h << 7);
247 inline uint8_t
pack_nibble(
const uint8_t a,
const uint8_t b,
const uint8_t c,
const uint8_t d) {
248 return a | (b << 1) | (c << 2) | (d << 3);
258 const uint8_t p1 =
bit(v, 1) ^
bit(v, 2) ^
bit(v, 3);
259 const uint8_t p2 =
bit(v, 0) ^
bit(v, 1) ^
bit(v, 2);
260 const uint8_t p3 =
bit(v, 0) ^
bit(v, 1) ^
bit(v, 3);
261 const uint8_t p4 =
bit(v, 0) ^
bit(v, 2) ^
bit(v, 3);
263 return pack_byte(p1,
bit(v, 0),
bit(v, 1),
bit(v, 2), p2,
bit(v, 3), p3, p4);
275 for(uint32_t i = 0; i < length; i++) {
276 array[i] = ((array[i] & 0x0f) << 4) | ((array[i] & 0xf0) >> 4);
318 static const uint8_t H[16] = { 0x0, 0x0, 0x4, 0x0, 0x6, 0x0, 0x0, 0x2,
319 0x7, 0x0, 0x0, 0x3, 0x0, 0x5, 0x1, 0x0 };
323 const uint8_t p1 =
bit(v, 0),
332 const uint8_t syndrome =
pack_nibble((uint8_t)(p1 != p1c), (uint8_t)(p2 != p2c), (uint8_t)(p3 != p3c), (uint8_t)(p4 != p4c));
335 v ^= 1u << H[syndrome];
341 template <
typename T>
342 inline void print_vector(std::ostream& out,
const T* v,
const std::string& prefix,
const int size,
const int element_len_bits) {
343 out << prefix <<
": ";
345 for (
int i = 0; i < size; i++)
346 out <<
to_bin(v[i], element_len_bits) <<
", ";
348 out << std::endl << std::flush;
351 template <
typename T>
352 inline void print_vector_hex(std::ostream& out,
const T* v,
const uint32_t size,
bool endline,
bool print_ascii) {
353 std::stringstream ss;
355 for (uint32_t i = 0u; i < size; i++) {
356 out <<
" " << std::hex << std::setw(2) << std::setfill(
'0') << (int)v[i];
357 if (v[i] >=
' ' && v[i] <=
'~')
362 out <<
" (" << ss.str() <<
")";
370 template <
typename T>
372 uint32_t
cr = v.size();
374 for(uint32_t i = 0; i <
cr; i++)
378 out <<
"LSB" << std::endl;
380 for(int32_t i =
sf-1; i >= 0; i--) {
381 for(int32_t j = 0; j < (int32_t)
cr; j++) {
387 out <<
"MSB" << std::endl;
389 for(uint32_t i = 0; i <
cr; i++)
406 inline uint32_t
dissect_packet(
const void **header, uint32_t header_size,
const uint8_t *buffer, uint32_t offset) {
407 (*header) = buffer + offset;
408 return offset + header_size;
411 inline uint32_t
build_packet(uint8_t *buffer, uint32_t offset,
const void* header, uint32_t header_size) {
412 memcpy(buffer + offset, header, header_size);
413 offset += header_size;
cr
Definition: loraphy.h:23
sf
Definition: loratap.h:33
uint32_t build_packet(uint8_t *buffer, uint32_t offset, const void *header, uint32_t header_size)
Definition: utilities.h:411
static uint8_t hamming_decode_soft_byte(uint8_t v)
Hamming(8,4) decoding by constructing a Syndrome matrix LUT for XORing on parity errors.
Definition: utilities.h:288
bool check_parity_string(const std::string &word, const bool even=true)
Check whether the parity of the given binary string is even.
Definition: utilities.h:171
uint32_t dissect_packet(const void **header, uint32_t header_size, const uint8_t *buffer, uint32_t offset)
Definition: utilities.h:406
T clamp(const T d, const T min, const T max)
Clamp given value in the given range.
Definition: utilities.h:79
void print_vector_hex(std::ostream &out, const T *v, const uint32_t size, bool endline, bool print_ascii)
Definition: utilities.h:352
bool check_parity(uint64_t word, const bool even=true)
Check whether the parity of the given uint64_t is even. See https://graphics.stanford....
Definition: utilities.h:191
static std::vector< std::string > term_colors
Definition: utilities.h:36
uint8_t pack_byte(const uint8_t a, const uint8_t b, const uint8_t c, const uint8_t d, const uint8_t e, const uint8_t f, const uint8_t g, const uint8_t h)
Pack the given 8 bits in a byte with: hgfe dcba
Definition: utilities.h:236
bool header_checksum(const uint8_t *header)
Definition: utilities.h:396
void print_vector(std::ostream &out, const T *v, const std::string &prefix, const int size, const int element_len_bits)
Definition: utilities.h:342
void print_interleave_matrix(std::ostream &out, const std::vector< T > &v, const uint32_t sf)
Definition: utilities.h:371
int32_t wrap_index(int32_t i, int32_t n)
Wrap indices Python-like, i.e. array[wrap_index(-1, array_length)] gets the last element.
Definition: utilities.h:61
std::string to_bin(const T v, const uint32_t bitwidth)
Return the v represented in a binary string.
Definition: utilities.h:116
uint8_t hamming_encode_soft(const uint8_t v)
Encode the given word with standard Hamming(7,4) and return a byte with the set parity bits.
Definition: utilities.h:257
uint32_t rotl(uint32_t bits, uint32_t count=1u, const uint32_t size=8u)
Rotate the given bits to the left and return the result.
Definition: utilities.h:96
void swap_nibbles(uint8_t *array, uint32_t length)
Swap nibbles of a byte array.
Definition: utilities.h:274
void print_vector_bin(std::ostream &out, const std::vector< T > &v, const std::string &prefix, const int element_len_bits)
Append the data in a given vector to an output stream with a comma delimiter.
Definition: utilities.h:154
uint8_t pack_nibble(const uint8_t a, const uint8_t b, const uint8_t c, const uint8_t d)
Pack the given 4 bits in a nibble with: dcba
Definition: utilities.h:247
uint8_t bit(const uint8_t v, const uint8_t i)
Select a single bit from the given byte.
Definition: utilities.h:226
uint32_t select_bits(const uint32_t data, const uint8_t *indices, const uint8_t n)
Select the bits in data given by the indices in *indices.
Definition: utilities.h:209
Definition: channelizer.h:28