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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
/*
Copyright (C) 2016
Matthias P. Braendli, matthias.braendli@mpb.li
http://opendigitalradio.org
This program 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 2 of the License, or
(at your option) any later version.
This program 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 this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "ETIWriter.hpp"
#include "crc.h"
#include "Log.h"
#include <stdio.h>
#include <cassert>
#include <stdexcept>
#include <sstream>
namespace EdiDecoder {
using namespace std;
void ETIWriter::update_protocol(
const std::string& proto,
uint16_t major,
uint16_t minor)
{
m_proto_valid = (proto == "DETI" and major == 0 and minor == 0);
if (not m_proto_valid) {
throw std::invalid_argument("Wrong EDI protocol");
}
}
void ETIWriter::reinit()
{
m_proto_valid = false;
m_fc_valid = false;
m_fic.clear();
m_etiFrame.clear();
m_subchannels.clear();
}
void ETIWriter::update_err(uint8_t err)
{
if (not m_proto_valid) {
throw std::logic_error("Cannot update ERR before protocol");
}
m_err = err;
}
void ETIWriter::update_fc_data(const eti_fc_data& fc_data)
{
if (not m_proto_valid) {
throw std::logic_error("Cannot update FC before protocol");
}
m_fc_valid = false;
m_fc = fc_data;
if (not m_fc.ficf) {
throw std::invalid_argument("FIC must be present");
}
if (m_fc.mid > 4) {
throw std::invalid_argument("Invalid MID");
}
if (m_fc.fp > 7) {
throw std::invalid_argument("Invalid FP");
}
m_fc_valid = true;
}
void ETIWriter::update_fic(const std::vector<uint8_t>& fic)
{
if (not m_proto_valid) {
throw std::logic_error("Cannot update FIC before protocol");
}
m_fic = fic;
}
void ETIWriter::update_edi_time(
uint32_t utco,
uint32_t seconds)
{
if (not m_proto_valid) {
throw std::logic_error("Cannot update time before protocol");
}
m_utco = utco;
m_seconds = seconds;
// TODO check validity
m_time_valid = true;
}
void ETIWriter::update_mnsc(uint16_t mnsc)
{
if (not m_proto_valid) {
throw std::logic_error("Cannot update MNSC before protocol");
}
m_mnsc = mnsc;
}
void ETIWriter::update_rfu(uint16_t rfu)
{
if (not m_proto_valid) {
throw std::logic_error("Cannot update RFU before protocol");
}
m_rfu = rfu;
}
void ETIWriter::add_subchannel(const eti_stc_data& stc)
{
if (not m_proto_valid) {
throw std::logic_error("Cannot add subchannel before protocol");
}
m_subchannels.push_back(stc);
if (m_subchannels.size() > 64) {
throw std::invalid_argument("Too many subchannels");
}
}
void ETIWriter::assemble()
{
if (not m_proto_valid) {
throw std::logic_error("Cannot assemble ETI before protocol");
}
if (not m_fc_valid) {
throw std::logic_error("Cannot assemble ETI without FC");
}
if (m_fic.empty()) {
throw std::logic_error("Cannot assemble ETI without FIC data");
}
// Accept zero subchannels, because of an edge-case that can happen
// during reconfiguration. See ETS 300 799 Clause 5.3.3
// TODO check time validity
// ETS 300 799 Clause 5.3.2, but we don't support not having
// a FIC
if ( (m_fc.mid == 3 and m_fic.size() != 32 * 4) or
(m_fc.mid != 3 and m_fic.size() != 24 * 4) ) {
stringstream ss;
ss << "Invalid FIC length " << m_fic.size() <<
" for MID " << m_fc.mid;
throw std::invalid_argument(ss.str());
}
std::vector<uint8_t> eti;
eti.reserve(6144);
eti.push_back(m_err);
// FSYNC
if (m_fc.fct() % 2 == 1) {
eti.push_back(0xf8);
eti.push_back(0xc5);
eti.push_back(0x49);
}
else {
eti.push_back(0x07);
eti.push_back(0x3a);
eti.push_back(0xb6);
}
// LIDATA
// FC
eti.push_back(m_fc.fct());
const uint8_t NST = m_subchannels.size();
eti.push_back((m_fc.ficf << 7) | NST);
// We need to pack:
// FP 3 bits
// MID 2 bits
// FL 11 bits
// FL: EN 300 799 5.3.6
uint16_t FL = NST + 1 + m_fic.size();
for (const auto& subch : m_subchannels) {
FL += subch.mst.size();
}
const uint16_t fp_mid_fl = (m_fc.fp << 13) | (m_fc.mid << 11) | FL;
eti.push_back(fp_mid_fl >> 8);
eti.push_back(fp_mid_fl & 0xFF);
// STC
for (const auto& subch : m_subchannels) {
eti.push_back( (subch.scid << 2) | (subch.sad & 0x300) );
eti.push_back( subch.sad & 0xff );
eti.push_back( (subch.tpl << 2) | ((subch.stl() & 0x300) >> 8) );
eti.push_back( subch.stl() & 0xff );
}
// EOH
// MNSC
eti.push_back(m_mnsc >> 8);
eti.push_back(m_mnsc & 0xFF);
// CRC
// Calculate CRC from eti[4] to current position
uint16_t eti_crc = 0xFFFF;
eti_crc = crc16(eti_crc, &eti[4], eti.size() - 4);
eti_crc ^= 0xffff;
eti.push_back(eti_crc >> 8);
eti.push_back(eti_crc & 0xFF);
const size_t mst_start = eti.size();
// MST
// FIC data
copy(m_fic.begin(), m_fic.end(), back_inserter(eti));
// Data stream
for (const auto& subch : m_subchannels) {
copy(subch.mst.begin(), subch.mst.end(), back_inserter(eti));
}
// EOF
// CRC
uint16_t mst_crc = 0xFFFF;
mst_crc = crc16(mst_crc, &eti[mst_start], eti.size() - mst_start);
mst_crc ^= 0xffff;
eti.push_back(mst_crc >> 8);
eti.push_back(mst_crc & 0xFF);
// RFU
eti.push_back(m_rfu >> 8);
eti.push_back(m_rfu);
// TIST
eti.push_back(m_fc.tsta >> 24);
eti.push_back((m_fc.tsta >> 16) & 0xFF);
eti.push_back((m_fc.tsta >> 8) & 0xFF);
eti.push_back(m_fc.tsta & 0xFF);
if (eti.size() > 6144) {
throw std::logic_error("ETI frame cannot be longer than 6144");
}
eti.resize(6144, 0x55);
m_etiFrame = eti;
}
std::vector<uint8_t> ETIWriter::getEtiFrame()
{
if (m_etiFrame.empty()) {
return {};
}
vector<uint8_t> eti(move(m_etiFrame));
reinit();
return eti;
}
}
|