forked from piratfm/eti-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edi2eti.c
389 lines (317 loc) · 8.31 KB
/
edi2eti.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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
* edi2eti.c
*
* Created on: 13.07.2017
* Author: tipok
*/
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include "edi_parser.h"
#include "network.h"
#include "logging.h"
#ifdef HAVE_ZMQ
#include <zmq.h>
#endif
#define VERSION_ID "$Rev$"
#define DEFAULT_TTL 16
#define DEFAULT_PORT 5004
#define DEFAULT_HOST "239.100.254.254"
uint32_t errors_count = 0;
uint32_t cntr = 0;
int Interrupted = 0;
int sock = -1;
char *ip = DEFAULT_HOST;
/***********************************************
* Usage
***********************************************/
static void usage(char *name) {
fprintf(
stderr,
"%s udp unicast/multicast EDI-to-ETI coverter.\n"
"This is UDP unicast/multicast EDI-stream receiver that converts received stream to ETI-format %s.\n"
"Usage: %s [options] [address]:[port]\n"
"-v, --verbose : Show more info (default: no)\n"
"-q, --quiet : Minimum info (default: no)\n"
"-I, --interval <value> : Application udp reception time in seconds. Exit after that. (default:run forever)\n"
#ifdef HAVE_ZMQ
"-o, --output <file|zmq-url> : Output file path or zeromq url (default: stdout)\n"
"-L, --no-align : Disable eti-packets alignment for ZeroMQ packing\n"
#else
"-o, --output <file> : Output file path (default: stdout)\n"
#endif
"-a, --activity : Dislay activity cursor (default: no)\n"
"-h, --help : Show this help\n"
"\n", name, VERSION_ID, name);
}
static void build_info(char *name) {
fprintf(stderr, "ZeroMQ:%s, FEC:%s\n",
#ifdef HAVE_ZMQ
"enabled",
#else
"disabled",
#endif
#ifdef HAVE_FEC
"enabled"
#else
"disabled"
#endif
);
}
static void signal_handler(int signum) {
// if (signum != SIGPIPE) {
if (signum != SIGPIPE) {
Interrupted = 1;
//fprintf(stdout, "TOTAL ERRORS: %d\n", errors_count);
msg_Log("TOTAL PACKETS: %d", cntr);
// if (sock > 0)
// close(sock);
// exit(1);
}
}
void write_file(void *privData, void *etiData, int etiLen)
{
if(verbosity > 2)
msg_Log("write:%u bytes to file", etiLen);
FILE *fh = (FILE *) privData;
fwrite(etiData, 1, etiLen, fh);
}
#ifdef HAVE_ZMQ
#define NUM_FRAMES_PER_ZMQ_MESSAGE 4
typedef struct {
uint32_t version;
int16_t buflen[NUM_FRAMES_PER_ZMQ_MESSAGE];
/* The head stops here. Use the macro below to calculate
* the head size.
*/
uint8_t buf[NUM_FRAMES_PER_ZMQ_MESSAGE*6144];
} PACKED zmq_dab_message_t;
#define ZMQ_DAB_MESSAGE_HEAD_LENGTH (4 + NUM_FRAMES_PER_ZMQ_MESSAGE*2)
int is_initial = 1;
zmq_dab_message_t zmq_msg;
int zmq_msg_ix;
int zmq_align = 1;
void write_zmq(void *privData, void *etiData, int etiLen)
{
int i;
int offset = 0;
uint8_t *etiPkt = (uint8_t *) etiData;
if(verbosity > 1)
msg_Log("write:%u bytes to zmq part: %d", etiLen, zmq_msg_ix);
// Increment the offset by the accumulated frame offsets
for (i = 0; i < zmq_msg_ix; i++) {
offset += zmq_msg.buflen[i];
}
if (offset + etiLen > NUM_FRAMES_PER_ZMQ_MESSAGE*6144) {
msg_Log("FAULT: invalid ETI frame size!");
return;
}
if(etiLen < 16 || (zmq_align && zmq_msg_ix != ((etiPkt[6] >> 5) & 0x03))) {
zmq_msg_ix=0;
zmq_msg.buflen[0] = -1;
zmq_msg.buflen[1] = -1;
zmq_msg.buflen[2] = -1;
zmq_msg.buflen[3] = -1;
msg_Log("ZMQ: skip non-aligned frames: %02x != %02x", zmq_msg_ix, ((etiPkt[6] >> 5) & 0x03));
return;
}
// Append the new frame to our message
memcpy(zmq_msg.buf + offset, etiPkt, etiLen);
zmq_msg.buflen[zmq_msg_ix] = etiLen;
zmq_msg_ix++;
// As soon as we have NUM_FRAMES_PER_ZMQ_MESSAGE frames, we transmit
if (zmq_msg_ix == NUM_FRAMES_PER_ZMQ_MESSAGE) {
// Size of the header:
int full_length = ZMQ_DAB_MESSAGE_HEAD_LENGTH;
for (i = 0; i < NUM_FRAMES_PER_ZMQ_MESSAGE; i++) {
full_length += zmq_msg.buflen[i];
}
zmq_msg_ix = 0;
const int flags = 0;
if(verbosity > 1)
msg_Log("zmq send:%u bytes", full_length);
zmq_send(privData, (uint8_t*)&zmq_msg, full_length, flags);
for (i = 0; i < NUM_FRAMES_PER_ZMQ_MESSAGE; i++) {
zmq_msg.buflen[i] = -1;
}
}
}
#endif
int main(int argc, char **argv) {
int ret, vertex=0, continous=1, timeout=0;
size_t recv_size = 0;
FILE *out_fh = stdout;
char *outpath = NULL;
int port = DEFAULT_PORT;
int activity=0;
edi_handler_t *edi_p=NULL;
#ifdef HAVE_ZMQ
void *zmq_context = NULL;
void *zmq_publisher = NULL;
#endif
/******************************************************
* Getopt
******************************************************/
const char short_options[] = "vLaqhI:o:";
const struct option long_options[] = {
{ "interval", optional_argument, NULL, 'I' },
{ "verbose", optional_argument, NULL, 'v' },
{ "quiet", optional_argument, NULL, 'q' },
{ "output", optional_argument, NULL, 'o' },
{ "activity", optional_argument, NULL, 'a' },
{ "no-align", no_argument, NULL, 'L' },
{ "help", no_argument, NULL, 'h' },
{ 0, 0, 0, 0 }
};
int c, option_index = 0;
if (argc == 1) {
usage(argv[0]);
build_info(argv[0]);
exit(-1);
}
while (1) {
c = getopt_long(argc, argv, short_options, long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 'q':
verbosity--;
break;
case 'v':
verbosity++;
break;
case 'a':
activity++;
break;
#ifdef HAVE_ZMQ
case 'L':
zmq_align=0;
break;
#endif
case 'I':
timeout = atoi(optarg);
break;
case 'o':
outpath = optarg;
break;
case 'h':
usage(argv[0]);
build_info(argv[0]);
exit(0);
break;
}
}
if(verbosity > 0)
build_info(argv[0]);
if (argc <= optind) {
if (verbosity > 0)
msg_Log("need to specify ip:port a:%d, o:%d", argc, optind);
exit(-1);
}
if(outpath) {
if(strncmp("zmq+", outpath, 4) == 0 && strlen(outpath) > 10) {
#ifdef HAVE_ZMQ
zmq_context = zmq_ctx_new ();
zmq_publisher = zmq_socket (zmq_context, ZMQ_PUB);
zmq_bind (zmq_publisher, outpath+4);
edi_p = initEDIHandle(ETI_FMT_ZMQ, write_zmq, zmq_publisher);
zmq_msg.buflen[0] = -1;
zmq_msg.buflen[1] = -1;
zmq_msg.buflen[2] = -1;
zmq_msg.buflen[3] = -1;
zmq_msg.version = 1;
zmq_msg_ix=0;
#else
msg_Log("ZEROMQ is disabled! Can't stream to specified destination: %s", outpath);
exit(-1);
#endif
} else
{
out_fh = fopen(outpath, "wb");
edi_p = initEDIHandle(ETI_FMT_RAW, write_file, out_fh);
}
} else {
edi_p = initEDIHandle(ETI_FMT_RAW, write_file, out_fh);
}
char *pch = strtok(argv[optind], ":");
if (pch != NULL) {
if (strchr(pch, '.'))
ip = pch;
else
port = atoi(pch);
}
pch = strtok(NULL, " \n");
if (pch != NULL)
port = atoi(pch);
if (verbosity > 0)
msg_Log("receiving from: %s:%d...", ip, port);
uint8_t *buff;
char ui[4] = { '-', '\\', '|', '/' };
if (timeout) {
// Alarm for automatic stopping
// if (signal (SIGALRM, signal_handler) == SIG_IGN)
// signal (SIGALRM, SIG_IGN);
signal(SIGALRM, signal_handler);
alarm(timeout);
}
signal(SIGINT, signal_handler);
buff = malloc(8192);
sock = input_init_udp(ip, port);
if (!sock) {
if (verbosity > 0)
msg_Log("init socket failed");
exit(-1);
}
/* read flood */
while (!Interrupted) {
if (activity > 0 && !(cntr % 32)) {
fprintf(stderr, "\x08%c", ui[vertex]);
vertex++;
if(vertex > 3) vertex=0;
}
do {
recv_size = 0;
//wait 500ms to receive buffer
ret = udp_read_timeout(sock, buff, &recv_size, 500);
if(ret == -ETIMEDOUT) {
if (continous) {
continous=0;
if(verbosity > 0)
msg_Log("Stream Disappears!");
}
} else {
if (!continous) {
continous=1;
if(verbosity > 0)
msg_Log("Stream Appears!");
}
}
} while ((recv_size <= 0 || ret == -EAGAIN) && !Interrupted);
if (Interrupted)
break;
if(HandleEDIPacket(edi_p, buff, recv_size) < 0)
errors_count++;
cntr++;
}
if (verbosity > 0)
msg_Log("Caught signal %d. ", Interrupted);
if(outpath)
fclose(out_fh);
closeEDIHandle(edi_p);
close(sock);
sock = 0;
free(buff);
#ifdef HAVE_ZMQ
if(zmq_publisher)
zmq_close (zmq_publisher);
if(zmq_context)
zmq_term (zmq_context);
#endif
return 1;
}