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
130
131
132
133
134
135
|
/* -*- c++ -*- */
/* Copyright 2012 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* GNU Radio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* GNU Radio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <gnuradio/digital/packet_header_default.h>
namespace gr {
namespace digital {
packet_header_default::sptr
packet_header_default::make(
long header_len,
const std::string &len_tag_key,
const std::string &num_tag_key,
int bits_per_byte)
{
return packet_header_default::sptr(new packet_header_default(header_len, len_tag_key, num_tag_key, bits_per_byte));
}
const unsigned MASK_LUT[9] = {0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x2F, 0x7F, 0xFF};
packet_header_default::packet_header_default(
long header_len,
const std::string &len_tag_key,
const std::string &num_tag_key,
int bits_per_byte)
: d_header_len(header_len),
d_len_tag_key(pmt::string_to_symbol(len_tag_key)),
d_num_tag_key(num_tag_key.empty() ? pmt::PMT_NIL : pmt::string_to_symbol(num_tag_key)),
d_bits_per_byte(bits_per_byte),
d_header_number(0)
{
if (d_bits_per_byte < 1 || d_bits_per_byte > 8) {
throw std::invalid_argument("bits_per_byte must be in [1, 8]");
}
d_mask = MASK_LUT[d_bits_per_byte];
}
packet_header_default::~packet_header_default()
{
}
bool packet_header_default::header_formatter(
long packet_len,
unsigned char *out,
const std::vector<tag_t> &tags
)
{
packet_len &= 0x0FFF;
memset(out, 0x00, d_header_len);
int parity = 0;
int k = 0; // Position in out
for (int i = 0; i < 12 && k < d_header_len; i += d_bits_per_byte, k++) {
out[k] = (unsigned char) ((packet_len >> i) & d_mask);
parity += out[k];
}
for (int i = 0; i < 16 && k < d_header_len; i += d_bits_per_byte, k++) {
out[k] = (unsigned char) ((d_header_number >> i) & d_mask);
parity += out[k];
}
if (k < d_header_len) {
out[k] = (unsigned char) (parity % 2);
}
d_header_number++;
return true;
}
bool packet_header_default::header_parser(
const unsigned char *in,
std::vector<tag_t> &tags)
{
unsigned header_len = 0;
unsigned header_num = 0;
tag_t tag;
int k = 0; // Position in "in"
for (int i = 0; i < 12 && k < d_header_len; i += d_bits_per_byte, k++) {
header_len |= (((int) in[k]) & d_mask) << i;
}
tag.key = d_len_tag_key;
tag.value = pmt::from_long(header_len);
tags.push_back(tag);
if (k >= d_header_len) {
return true;
}
if (d_num_tag_key == pmt::PMT_NIL) {
k += 16;
} else {
for (int i = 0; i < 16 && k < d_header_len; i += d_bits_per_byte, k++) {
header_num |= (((int) in[k]) & d_mask) << i;
}
tag.key = d_num_tag_key;
tag.value = pmt::from_long(header_num);
tags.push_back(tag);
}
if (k >= d_header_len) {
return true;
}
int parity = in[k];
for (int i = 0; i < 28; i++) {
parity += in[i];
}
return !(parity % 2);
}
} /* namespace digital */
} /* namespace gr */
|