summaryrefslogtreecommitdiff
path: root/gr-digital/lib/header_format_crc.cc
blob: d53f4eb7ed90fb1d8e166844b4806027e096633f (plain)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* -*- c++ -*- */
/* Copyright 2016 Free Software Foundation, Inc.
 *
 * This file is part of GNU Radio
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 */

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

#include <gnuradio/digital/header_buffer.h>
#include <gnuradio/digital/header_format_crc.h>
#include <volk/volk_alloc.hh>
#include <cstring>

namespace gr {
namespace digital {

header_format_crc::sptr header_format_crc::make(const std::string& len_key_name,
                                                const std::string& num_key_name)
{
    return header_format_crc::sptr(new header_format_crc(len_key_name, num_key_name));
}

header_format_crc::header_format_crc(const std::string& len_key_name,
                                     const std::string& num_key_name)
    : header_format_base(), d_header_number(0)
{
    d_len_key_name = pmt::intern(len_key_name);
    d_num_key_name = pmt::intern(num_key_name);
}

header_format_crc::~header_format_crc() {}

bool header_format_crc::format(int nbytes_in,
                               const unsigned char* input,
                               pmt::pmt_t& output,
                               pmt::pmt_t& info)
{
    // Creating the output pmt copies data; free our own here when done.
    volk::vector<uint8_t> bytes_out(header_nbytes());

    // Should this throw instead of mask if the payload is too big
    // for 12-bit representation?
    nbytes_in &= 0x0FFF;

    d_crc_impl.reset();
    d_crc_impl.process_bytes((void const*)&nbytes_in, 2);
    d_crc_impl.process_bytes((void const*)&d_header_number, 2);
    uint8_t crc = d_crc_impl();

    // Form 2 12-bit items into 1 2-byte item
    uint32_t concat = 0;
    concat = (d_header_number << 12) | (nbytes_in);

    header_buffer header(bytes_out.data());
    header.add_field32(concat, 24, true);
    header.add_field8(crc);

    d_header_number++;
    d_header_number &= 0x0FFF;

    // Package output data into a PMT vector
    output = pmt::init_u8vector(header_nbytes(), bytes_out.data());

    return true;
}

bool header_format_crc::parse(int nbits_in,
                              const unsigned char* input,
                              std::vector<pmt::pmt_t>& info,
                              int& nbits_processed)
{
    while (nbits_processed <= nbits_in) {
        d_hdr_reg.insert_bit(input[nbits_processed++]);
        if (d_hdr_reg.length() == header_nbits()) {
            if (header_ok()) {
                int payload_len = header_payload();
                enter_have_header(payload_len);
                info.push_back(d_info);
                d_hdr_reg.clear();
                return true;
            } else {
                d_hdr_reg.clear();
                return false;
            }
            break;
        }
    }

    return true;
}

size_t header_format_crc::header_nbits() const { return 32; }


bool header_format_crc::header_ok()
{
    uint32_t pkt = d_hdr_reg.extract_field32(0, 24, true);
    uint16_t pktlen = static_cast<uint16_t>((pkt >> 8) & 0x0fff);
    uint16_t pktnum = static_cast<uint16_t>((pkt >> 20) & 0x0fff);
    uint8_t crc_rcvd = d_hdr_reg.extract_field8(24);

    // Check CRC8
    d_crc_impl.reset();
    d_crc_impl.process_bytes((void const*)&pktlen, 2);
    d_crc_impl.process_bytes((void const*)&pktnum, 2);
    uint8_t crc_clcd = d_crc_impl();

    return (crc_rcvd == crc_clcd);
}

int header_format_crc::header_payload()
{
    uint32_t pkt = d_hdr_reg.extract_field32(0, 24, true);
    uint16_t pktlen = static_cast<uint16_t>((pkt >> 8) & 0x0fff);
    uint16_t pktnum = static_cast<uint16_t>((pkt >> 20) & 0x0fff);

    d_info = pmt::make_dict();
    d_info = pmt::dict_add(d_info, d_len_key_name, pmt::from_long(8 * pktlen));
    d_info = pmt::dict_add(d_info, d_num_key_name, pmt::from_long(pktnum));
    return static_cast<int>(pktlen);
}

} /* namespace digital */
} /* namespace gr */