File: crc_append_impl.cc

package info (click to toggle)
gr-satellites 5.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,836 kB
  • sloc: python: 29,546; cpp: 5,448; ansic: 1,247; sh: 118; makefile: 24
file content (108 lines) | stat: -rw-r--r-- 3,716 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* -*- c++ -*- */
/*
 * Copyright 2022 Daniel Estevez <daniel@destevez.net>.
 *
 * This file is part of gr-satellites
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdexcept>
#include <vector>

#include "crc_append_impl.h"
#include <gnuradio/io_signature.h>

namespace gr {
namespace satellites {

crc_append::sptr crc_append::make(unsigned num_bits,
                                  uint64_t poly,
                                  uint64_t initial_value,
                                  uint64_t final_xor,
                                  bool input_reflected,
                                  bool result_reflected,
                                  bool swap_endianness,
                                  unsigned skip_header_bytes)
{
    return gnuradio::make_block_sptr<crc_append_impl>(num_bits,
                                                      poly,
                                                      initial_value,
                                                      final_xor,
                                                      input_reflected,
                                                      result_reflected,
                                                      swap_endianness,
                                                      skip_header_bytes);
}

crc_append_impl::crc_append_impl(unsigned num_bits,
                                 uint64_t poly,
                                 uint64_t initial_value,
                                 uint64_t final_xor,
                                 bool input_reflected,
                                 bool result_reflected,
                                 bool swap_endianness,
                                 unsigned skip_header_bytes)
    : gr::block(
          "crc_append", gr::io_signature::make(0, 0, 0), gr::io_signature::make(0, 0, 0)),
      d_num_bits(num_bits),
      d_swap_endianness(swap_endianness),
      d_crc(crc(
          num_bits, poly, initial_value, final_xor, input_reflected, result_reflected)),
      d_header_bytes(skip_header_bytes)
{
    if (num_bits % 8 != 0) {
        throw std::runtime_error("CRC number of bits must be divisible by 8");
    }
    message_port_register_out(pmt::mp("out"));
    message_port_register_in(pmt::mp("in"));
    set_msg_handler(pmt::mp("in"), [this](pmt::pmt_t msg) { this->msg_handler(msg); });
}

crc_append_impl::~crc_append_impl() {}

void crc_append_impl::forecast(int noutput_items, gr_vector_int& ninput_items_required) {}

int crc_append_impl::general_work(int noutput_items,
                                  gr_vector_int& ninput_items,
                                  gr_vector_const_void_star& input_items,
                                  gr_vector_void_star& output_items)
{
    return 0;
}

void crc_append_impl::msg_handler(pmt::pmt_t pmt_msg)
{
    std::vector<uint8_t> msg = pmt::u8vector_elements(pmt::cdr(pmt_msg));

    const auto size = msg.size();
    if (size <= d_header_bytes) {
        this->d_logger->warn("PDU too short; dropping");
        return;
    }

    uint64_t crc = d_crc.compute(&msg[d_header_bytes], size - d_header_bytes);

    unsigned num_bytes = d_num_bits / 8;
    if (d_swap_endianness) {
        for (unsigned i = 0; i < num_bytes; ++i) {
            msg.push_back(crc & 0xff);
            crc >>= 8;
        }
    } else {
        for (unsigned i = 0; i < num_bytes; ++i) {
            msg.push_back((crc >> (d_num_bits - 8 * (i + 1))) & 0xff);
        }
    }

    message_port_pub(pmt::mp("out"),
                     pmt::cons(pmt::car(pmt_msg), pmt::init_u8vector(msg.size(), msg)));
}

} /* namespace satellites */
} /* namespace gr */