-
Notifications
You must be signed in to change notification settings - Fork 18
/
eti_assembler.c
179 lines (143 loc) · 4.27 KB
/
eti_assembler.c
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
/*
* eti_assembler.c
*
* Created on: 14 лип. 2017 р.
* Author: tipok
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <inttypes.h>
#include <unistd.h>
#include <ctype.h>
#include "edi_parser.h"
#include "logging.h"
#include "buffer_unpack.h"
#include "crc.h"
int AssembleETIFrame(struct etiBuilder *h, int fmt, callback_t outCallback, void *priv_data)
{
uint8_t eti[6144];
int idx=0;
int i;
if (!h->is_eti) {
msg_Log("ETI: Cannot assemble ETI before protocol");
return 0;
}
if (!h->m_fc_valid) {
msg_Log("ETI: Cannot assemble ETI without FC");
return 0;
}
if (!h->fic_length) {
msg_Log("ETI: Cannot assemble ETI without FIC data");
return 0;
}
// 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 ( (h->m_fc.mid == 3 && h->fic_length != 32 * 4) ||
(h->m_fc.mid != 3 && h->fic_length != 24 * 4) ) {
msg_Log("ETI: Invalid FIC length %u for MID %u",h->fic_length, h->m_fc.mid);
return 0;
}
eti[0] = 0xff;
uint8_t fct = h->m_fc.dflc % 250;
// FSYNC
if (fct % 2 == 1) {
eti[1] = 0xf8;
eti[2] = 0xc5;
eti[3] = 0x49;
} else {
eti[1] = 0x07;
eti[2] = 0x3a;
eti[3] = 0xb6;
}
// LIDATA
// FC
eti[4] = fct;
const uint8_t NST = h->m_fc.nst;
eti[5] = ((uint8_t)h->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 + h->fic_length/4;
for (i=0;i<NST;i++) {
//fprintf(stderr,"assemble: subch.stream_index=%d, subch.scid=%d subch.sad=%d len:%d FL=%d\n",subch.stream_index, subch.scid, subch.sad, subch.mst.size(), FL);
FL += h->m_stc[i].mst_size/4;
}
const uint16_t fp_mid_fl = (h->m_fc.fp << 13) | (h->m_fc.mid << 11) | FL;
eti[6] = (uint16_t) (fp_mid_fl >> 8);
eti[7] = (fp_mid_fl & 0xFF);
// msg_Dump(eti, 8);
// STC
for (i=0;i<NST;i++) {
eti[8 + i*4] = (h->m_stc[i].scid << 2) | (uint16_t)((h->m_stc[i].sad & 0x300) >> 8);
eti[8 + i*4 + 1] = h->m_stc[i].sad & 0xff;
eti[8 + i*4 + 2] = (h->m_stc[i].tpl << 2) | (uint16_t)(((h->m_stc[i].mst_size/8) & 0x300) >> 8);
eti[8 + i*4 + 3] = (uint16_t)(h->m_stc[i].mst_size/8) & 0xff;
// msg_Log("STC:%u",i);
// msg_Dump(&eti[8 + i*4], 4);
}
idx+=8 + NST*4;
// EOH
// MNSC
eti[idx] = (((uint16_t)h->m_mnsc) >> 8);
eti[idx+1] = (h->m_mnsc & 0xFF);
// CRC
// Calculate CRC from eti[4] to current position
uint16_t eti_crc = 0xFFFF;
eti_crc = crc16(eti_crc, &eti[4], idx - 4 + 2);
eti_crc ^= 0xffff;
// msg_Log("CRC of:%u bytes: 0x%04x",idx - 4 + 2, eti_crc);
// msg_Dump(&eti[4], idx - 4 + 2);
eti[idx+2] = (uint16_t)eti_crc >> 8;
eti[idx+3] = eti_crc & 0xFF;
idx+=4;
const size_t mst_start = idx;
// MST
// FIC data
memcpy(eti+idx, h->fic, h->fic_length);
idx+=h->fic_length;
// Data stream
for (i=0;i<NST;i++) {
if(idx+h->m_stc[i].mst_size >= 6144-8) {
msg_Log("ETI: Invalid ETI length %u",idx+h->m_stc[i].mst_size);
return 0;
}
memcpy(eti+idx, h->m_stc[i].mst, h->m_stc[i].mst_size);
idx+=h->m_stc[i].mst_size;
}
// EOF
// CRC
uint16_t mst_crc = 0xFFFF;
mst_crc = crc16(mst_crc, &eti[mst_start], idx - mst_start);
mst_crc ^= 0xffff;
eti[idx] = (uint16_t)(mst_crc >> 8);
eti[idx+1] = (mst_crc & 0xFF);
// RFU
eti[idx+2] = (uint16_t)(h->m_rfu >> 8);
eti[idx+3] = (h->m_rfu & 0xFF);
// TIST
eti[idx+4] = (uint32_t)(h->m_fc.tsta >> 24);
eti[idx+5] = ((uint32_t)(h->m_fc.tsta >> 16) & 0xFF);
eti[idx+6] = ((uint32_t)(h->m_fc.tsta >> 8) & 0xFF);
eti[idx+7] = (h->m_fc.tsta & 0xFF);
idx+=8;
if(idx>6144) {
msg_Log("ETI: Invalid ETI length %u",idx);
return 0;
}
//raise packet if needed
if(fmt == ETI_FMT_RAW && idx<6144) {
memset(eti+idx, 0x55, 6144-idx);
idx=6144;
}
if(outCallback)
outCallback(priv_data, eti, idx);
return 1;
}