-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstdc_parser.cpp
551 lines (535 loc) · 32.5 KB
/
stdc_parser.cpp
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
#include <iostream>
#include <inmarsatc_parser.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <map>
#include <json.hpp> //https://github.com/nlohmann/json
using json = nlohmann::json;
void printHelp() {
std::cout << "Help: " << std::endl;
std::cout << "stdc_parse - open-source cli program to parse inmarsat-C frames using inmarsatc library based on Scytale-C source code" << std::endl;
std::cout << "Keys: " << std::endl;
std::cout << "--help - this help" << std::endl;
std::cout << "--verbose - print all data for all parsed packets" << std::endl;
std::cout << "--in-udp <port> - input decoded frames via udp(default port: 15004)" << std::endl;
std::cout << "--out-udp <ip> <port> - output parsed packets via udp JSON(default: 127.0.0.1 15005)" << std::endl;
std::cout << "--print-all-packets - parse data for any packets type(otherwise just message packets)" << std::endl;
std::cout << "(one source should be selected)" << std::endl;
}
int parseArg(int argc, int* position, char* argv[], std::map<std::string, std::string>* params, bool recursive) {
std::string arg1 = std::string(argv[*position]);
//i would be using switch() here... but it's not available for strings, so...
if(arg1 == "--help") {
return 1;
} else if(arg1 == "--verbose") {
params->insert(std::pair<std::string, std::string>("frameparserVerbose", "true"));
return 0;
} else if(arg1 == "--in-udp") {
std::string arg2;
arg2 = "15004";
int nextpos = *position + 1;
if(nextpos < argc and !recursive) {
int parseRes = parseArg(argc, &nextpos, argv, params, true);
if(parseRes == 2) {
arg2 = std::string(argv[nextpos]);
}
*position = nextpos;
}
params->insert(std::pair<std::string, std::string>("frameparserSource", "udp"));
params->insert(std::pair<std::string, std::string>("frameparserSourceUdpPort", arg2));
return 0;
} else if(arg1 == "--out-udp") {
std::string arg2;
std::string arg3;
arg2 = "127.0.0.1";
arg3 = "15005";
int nextpos = *position + 1;
if(nextpos < argc and !recursive) {
int parseRes = parseArg(argc, &nextpos, argv, params, true);
if(parseRes == 2) {
arg2 = std::string(argv[nextpos]);
}
*position = nextpos;
nextpos++;
if(nextpos < argc) {
parseRes = parseArg(argc, &nextpos, argv, params, true);
if(parseRes == 2) {
arg3 = std::string(argv[nextpos]);
}
*position = nextpos;
}
}
params->insert(std::pair<std::string, std::string>("frameparserOutUdp", "true"));
params->insert(std::pair<std::string, std::string>("frameparserOutUdpIp", arg2));
params->insert(std::pair<std::string, std::string>("frameparserOutUdpPort", arg3));
return 0;
} else if(arg1 == "--print-all-packets") {
params->insert(std::pair<std::string, std::string>("frameparserPrintAllPackets", "true"));
return 0;
} else {
return 2;
}
}
//serialization
template< typename T >
std::array< char, sizeof(T) > to_bytes( const T& object ) {
std::array< char, sizeof(T) > bytes ;
const char* begin = reinterpret_cast< const char* >( std::addressof(object) ) ;
const char* end = begin + sizeof(T) ;
std::copy( begin, end, std::begin(bytes) ) ;
return bytes ;
}
template< typename T >
T& from_bytes( const std::array< char, sizeof(T) >& bytes, T& object )
{
// http://en.cppreference.com/w/cpp/types/is_trivially_copyable
static_assert( std::is_trivially_copyable<T>::value, "not a TriviallyCopyable type" ) ;
char* begin_object = reinterpret_cast< char* >( std::addressof(object) ) ;
std::copy( std::begin(bytes), std::end(bytes), begin_object ) ;
return object ;
}
void sendParserDataViaUdp(std::string data, int sockfd, sockaddr_in serveraddr) {
sendto(sockfd, data.c_str(), data.size(), 0, (const struct sockaddr *) &serveraddr, sizeof(serveraddr));
}
inmarsatc::decoder::Decoder::decoder_result receiveDemodDataViaUdp(int sockfd, sockaddr_in serveraddr) {
inmarsatc::decoder::Decoder::decoder_result ret;
socklen_t len_useless = sizeof(serveraddr);
char buf[sizeof(inmarsatc::decoder::Decoder::decoder_result)];
recvfrom(sockfd, &buf, (sizeof(buf)), MSG_WAITALL, ( struct sockaddr *) &serveraddr, &len_useless);
std::array<char, sizeof(inmarsatc::decoder::Decoder::decoder_result)> buf_std;
std::copy(std::begin(buf), std::end(buf), std::begin(buf_std));
from_bytes(buf_std, ret);
return ret;
}
bool ifPacketIsMessage(inmarsatc::frameParser::FrameParser::frameParser_result pack_dec_res) {
switch(pack_dec_res.decoding_result.packetDescriptor) {
case 0xA3:
case 0xA8:
return pack_dec_res.decoding_result.packetVars.find("shortMessage") != pack_dec_res.decoding_result.packetVars.end();
case 0xAA:
case 0xB1:
case 0xB2:
return true;
default:
return false;
}
}
void printFrameParserPacket(inmarsatc::frameParser::FrameParser::frameParser_result pack_dec_res, bool isFrameParserPrintAllPackets, bool isFrameParserVerbose) {
if(isFrameParserPrintAllPackets || ifPacketIsMessage(pack_dec_res)) {
std::cout << "packet: " << std::endl;
std::cout << " type: " << pack_dec_res.decoding_result.packetVars["packetDescriptorText"] << std::dec << " (" << std::hex << (uint16_t)pack_dec_res.decoding_result.packetDescriptor << ")" << std::endl;
if(isFrameParserVerbose) {
std::cout << " frameNumber: " << pack_dec_res.decoding_result.frameNumber << std::endl;
std::time_t timestamp = std::chrono::high_resolution_clock::to_time_t(pack_dec_res.decoding_result.timestamp);
std::cout << " timestamp: " << std::ctime(×tamp);
std::cout << " decodingStage: " << (pack_dec_res.decoding_result.decodingStage == PACKETDECODER_DECODING_STAGE_NONE ? "none" : (pack_dec_res.decoding_result.decodingStage == PACKETDECODER_DECODING_STAGE_COMPLETE ? "complete" : "partial")) << std::endl;
std::cout << " packetLength: " << pack_dec_res.decoding_result.packetLength << std::endl;
bool payload = pack_dec_res.decoding_result.payload.presentation != -1;
std::cout << " payload: " << (payload ? "yes" : "no") << std::endl;
if(payload) {
std::cout << " presentation: " << (pack_dec_res.decoding_result.payload.presentation == PACKETDECODER_PRESENTATION_BINARY ? "binary" : (pack_dec_res.decoding_result.payload.presentation == PACKETDECODER_PRESENTATION_IA5 ? "IA5" : (pack_dec_res.decoding_result.payload.presentation == PACKETDECODER_PRESENTATION_ITA2 ? "ITA2" : "unknown"))) << std::endl;
std::cout << " data: {" << std::endl << " ";
if(pack_dec_res.decoding_result.payload.presentation == PACKETDECODER_PRESENTATION_IA5) {
for(int i = 0; i < (int)pack_dec_res.decoding_result.payload.data8Bit.size(); i++) {
char chr = pack_dec_res.decoding_result.payload.data8Bit[i] & 0b01111111;
if((chr < 0x20 && chr != '\n' && chr != '\r')) {
std::cout << "(" << std::hex << (uint16_t)chr << std::dec << ")";
} else if(chr != '\n' && chr != '\r') {
std::cout << chr;
} else {
std::cout << std::endl << " ";
}
}
} else if(pack_dec_res.decoding_result.payload.presentation == PACKETDECODER_PRESENTATION_ITA2) {
} else {
for(int i = 0; i < (int)pack_dec_res.decoding_result.payload.data8Bit.size(); i++) {
uint8_t data = pack_dec_res.decoding_result.payload.data8Bit[i];
std::cout << std::hex << (uint16_t)data << std::dec << " ";
}
}
std::cout << std::endl << " }" << std::endl;
}
std::cout << " packetVars:" << std::endl;
for(std::map<std::string, std::string>::const_iterator it = pack_dec_res.decoding_result.packetVars.begin(); it != pack_dec_res.decoding_result.packetVars.end(); ++it) {
std::string key = it->first;
std::string data = it->second;
if(key == "packetDescriptorText") {
continue;
}
std::cout << " " << key << ": " << std::endl << " ";
for(int k = 0; k < (int)data.length(); k++) {
if(data[k] != '\n') {
std::cout << data[k];
} else {
std::cout << std::endl << " ";
}
}
std::cout << std::endl;
}
} else {
switch(pack_dec_res.decoding_result.packetDescriptor) {
case 0x27:
{
std::cout << " msgId: " << pack_dec_res.decoding_result.packetVars["mesId"] << " sat: " << pack_dec_res.decoding_result.packetVars["satName"] << " les: " << pack_dec_res.decoding_result.packetVars["lesName"] << " LCN: " << pack_dec_res.decoding_result.packetVars["logicalChannelNo"] << std::endl;
}
break;
case 0x2A:
{
std::cout << " msgId: " << pack_dec_res.decoding_result.packetVars["mesId"] << " sat: " << pack_dec_res.decoding_result.packetVars["satName"] << " les: " << pack_dec_res.decoding_result.packetVars["lesName"] << " LCN: " << pack_dec_res.decoding_result.packetVars["logicalChannelNo"] << std::endl;
}
break;
case 0x08:
{
std::cout << " sat: " << pack_dec_res.decoding_result.packetVars["satName"] << " les: " << pack_dec_res.decoding_result.packetVars["lesName"] << " LCN: " << pack_dec_res.decoding_result.packetVars["logicalChannelNo"] << " ULF: " << pack_dec_res.decoding_result.packetVars["uplinkChannelMhz"] << std::endl;
}
break;
case 0x6C:
{
std::cout << " ULF: " << pack_dec_res.decoding_result.packetVars["uplinkChannelMhz"] << std::endl;
std::cout << " Services: ";
std::string services = pack_dec_res.decoding_result.packetVars["services"];
for(int k = 0; k < (int)services.length(); k++) {
if(services[k] != '\n') {
std::cout << services[k];
} else {
std::cout << " ";
}
}
std::cout << std::endl;
std::cout << " Tdm slots: ";
std::string tdmSlots = pack_dec_res.decoding_result.packetVars["tdmSlots"];
for(int k = 0; k < (int)tdmSlots.length(); k++) {
if(tdmSlots[k] != '\n') {
std::cout << tdmSlots[k];
} else {
std::cout << " ";
}
}
std::cout << std::endl;
}
break;
case 0x7D:
{
std::cout << " netVer: " << pack_dec_res.decoding_result.packetVars["networkVersion"] << " sat: " << pack_dec_res.decoding_result.packetVars["satName"] << " les: " << pack_dec_res.decoding_result.packetVars["lesName"] << " sigCh: " << pack_dec_res.decoding_result.packetVars["signallingChannel"] << " count: " << pack_dec_res.decoding_result.packetVars["count"] << " chType: " << pack_dec_res.decoding_result.packetVars["channelTypeName"] << " sat: " << pack_dec_res.decoding_result.packetVars["satName"] << " local: " << pack_dec_res.decoding_result.packetVars["local"] << " randInt: " << pack_dec_res.decoding_result.packetVars["randomInterval"] << std::endl;
std::cout << " Status: ";
std::string status = pack_dec_res.decoding_result.packetVars["status"];
for(int k = 0; k < (int)status.length(); k++) {
if(status[k] != '\n') {
std::cout << status[k];
} else {
std::cout << " ";
}
}
std::cout << std::endl;
std::cout << " Services: ";
std::string services = pack_dec_res.decoding_result.packetVars["services"];
for(int k = 0; k < (int)services.length(); k++) {
if(services[k] != '\n') {
std::cout << services[k];
} else {
std::cout << " ";
}
}
std::cout << std::endl;
}
break;
case 0x81:
{
std::cout << " msgId: " << pack_dec_res.decoding_result.packetVars["mesId"] << " sat: " << pack_dec_res.decoding_result.packetVars["satName"] << " les: " << pack_dec_res.decoding_result.packetVars["lesName"] << " LCN: " << pack_dec_res.decoding_result.packetVars["logicalChannelNo"] << " dlFr: " << pack_dec_res.decoding_result.packetVars["downlinkChannelMhz"] << " pres: " << pack_dec_res.decoding_result.packetVars["presentation"] << std::endl;
}
break;
case 0x83:
{
std::cout << " msgId: " << pack_dec_res.decoding_result.packetVars["mesId"] << " sat: " << pack_dec_res.decoding_result.packetVars["satName"] << " les: " << pack_dec_res.decoding_result.packetVars["lesName"] << " status: " << pack_dec_res.decoding_result.packetVars["status_bits"] << " LCN: " << pack_dec_res.decoding_result.packetVars["logicalChannelNo"] << " frLen: " << pack_dec_res.decoding_result.packetVars["frameLength"] << " dur: " << pack_dec_res.decoding_result.packetVars["duration"] << " dlFr: " << pack_dec_res.decoding_result.packetVars["downlinkChannelMhz"] << " ulFr: " << pack_dec_res.decoding_result.packetVars["uplinkChannelMhz"] << " frOffs: " << pack_dec_res.decoding_result.packetVars["frameOffset"] << " PD1: " << pack_dec_res.decoding_result.packetVars["packetDescriptor1"] << std::endl;
}
break;
case 0x91:
{
//not implemented
}
break;
case 0x92:
{
std::cout << " loginAckLen: " << pack_dec_res.decoding_result.packetVars["loginAckLength"] << " dlFr: " << pack_dec_res.decoding_result.packetVars["downlinkChannelMhz"] << " les: " << pack_dec_res.decoding_result.packetVars["les"] << " stStartHex: " << pack_dec_res.decoding_result.packetVars["stationStartHex"] << std::endl;
if(pack_dec_res.decoding_result.packetVars.find("stationCount") != pack_dec_res.decoding_result.packetVars.end()) {
std::cout << " stationCnt: " << pack_dec_res.decoding_result.packetVars["stationCount"] << " Stations: ";
std::string stations = pack_dec_res.decoding_result.packetVars["stations"];
for(int k = 0; k < (int)stations.length(); k++) {
if(stations[k] != '\n') {
std::cout << stations[k];
} else {
std::cout << " ";
}
}
std::cout << std::endl;
}
}
break;
case 0x9A:
{
//not implemented
}
break;
case 0xA0:
{
//not implemented
}
break;
case 0xA3:
{
std::cout << " msgId: " << pack_dec_res.decoding_result.packetVars["mesId"] << " sat: " << pack_dec_res.decoding_result.packetVars["satName"] << " les: " << pack_dec_res.decoding_result.packetVars["lesName"]<< std::endl;
if(pack_dec_res.decoding_result.packetVars.find("shortMessage") != pack_dec_res.decoding_result.packetVars.end()) {
std::cout << " Short message: " << std::endl << " ";
std::string shortMessage = pack_dec_res.decoding_result.packetVars["shortMessage"];
for(int k = 0; k < (int)shortMessage.length(); k++) {
char chr = shortMessage[k] & 0x7F;
if((chr < 0x20 && chr != '\n' && chr != '\r')) {
std::cout << "(" << std::hex << (uint16_t)chr << std::dec << ")";
} else if(chr != '\n' && chr != '\r') {
std::cout << chr;
} else {
std::cout << std::endl << " ";
}
}
std::cout << std::endl;
}
}
break;
case 0xA8:
{
std::cout << " msgId: " << pack_dec_res.decoding_result.packetVars["mesId"] << " sat: " << pack_dec_res.decoding_result.packetVars["satName"] << " les: " << pack_dec_res.decoding_result.packetVars["lesName"]<< std::endl;
if(pack_dec_res.decoding_result.packetVars.find("shortMessage") != pack_dec_res.decoding_result.packetVars.end()) {
std::cout << " Short message: " << std::endl << " ";
std::string shortMessage = pack_dec_res.decoding_result.packetVars["shortMessage"];
for(int k = 0; k < (int)shortMessage.length(); k++) {
char chr = shortMessage[k] & 0x7F;
if((chr < 0x20 && chr != '\n' && chr != '\r')) {
std::cout << "(" << std::hex << (uint16_t)chr << std::dec << ")";
} else if(chr != '\n' && chr != '\r') {
std::cout << chr;
} else {
std::cout << std::endl << " ";
}
}
std::cout << std::endl;
}
}
break;
case 0xAA:
{
std::cout << " sat: " << pack_dec_res.decoding_result.packetVars["satName"] << " les: " << pack_dec_res.decoding_result.packetVars["lesName"] << " LCN: " << pack_dec_res.decoding_result.packetVars["logicalChannelNo"] << " packetNo: " << pack_dec_res.decoding_result.packetVars["packetNo"] << std::endl;
bool isBinary = pack_dec_res.decoding_result.payload.presentation == PACKETDECODER_PRESENTATION_BINARY;
std::cout << " Message(" << (isBinary ? "hex" : "text") << "): " << std::endl << " ";
if(pack_dec_res.decoding_result.payload.presentation == PACKETDECODER_PRESENTATION_IA5) {
for(int i = 0; i < (int)pack_dec_res.decoding_result.payload.data8Bit.size(); i++) {
char chr = pack_dec_res.decoding_result.payload.data8Bit[i] & 0x7F;
if((chr < 0x20 && chr != '\n' && chr != '\r')) {
std::cout << "(" << std::hex << (uint16_t)chr << std::dec << ")";
} else if(chr != '\n' && chr != '\r') {
std::cout << chr;
} else {
std::cout << std::endl << " ";
}
}
} else if(pack_dec_res.decoding_result.payload.presentation == PACKETDECODER_PRESENTATION_ITA2) {
} else {
for(int i = 0; i < (int)pack_dec_res.decoding_result.payload.data8Bit.size(); i++) {
uint8_t data = pack_dec_res.decoding_result.payload.data8Bit[i];
std::cout << std::hex << (uint16_t)data << std::dec << " ";
}
}
std::cout << std::endl;
}
break;
case 0xAB:
{
std::cout << " lesListLen: " << pack_dec_res.decoding_result.packetVars["lesListLength"] << " stStartHex: " << pack_dec_res.decoding_result.packetVars["stationStartHex"] << " stCnt: " << pack_dec_res.decoding_result.packetVars["stationCount"] << std::endl;
std::cout << " Stations: ";
std::string stations = pack_dec_res.decoding_result.packetVars["stations"];
for(int k = 0; k < (int)stations.length(); k++) {
if(stations[k] != '\n') {
std::cout << stations[k];
} else {
std::cout << " ";
}
}
std::cout << std::endl;
}
break;
case 0xAC:
{
//not implemented
}
break;
case 0xAD:
{
//not implemented
}
break;
case 0xB1:
{
std::cout << " msgType: " << pack_dec_res.decoding_result.packetVars["messageType"] << " svcCd&AddrName: " << pack_dec_res.decoding_result.packetVars["serviceCodeAndAddressName"] << " contin: " << pack_dec_res.decoding_result.packetVars["continuation"] << " prio: " << pack_dec_res.decoding_result.packetVars["priorityText"] << " rep: " << pack_dec_res.decoding_result.packetVars["repetition"] << " msgId: " << pack_dec_res.decoding_result.packetVars["messageId"] << " packetNo: " << pack_dec_res.decoding_result.packetVars["packetNo"] << " isNewPayl: " << pack_dec_res.decoding_result.packetVars["isNewPayload"] << " addrHex: " << pack_dec_res.decoding_result.packetVars["addressHex"] << std::endl;
bool isBinary = pack_dec_res.decoding_result.payload.presentation == PACKETDECODER_PRESENTATION_BINARY;
std::cout << " Payload(" << (isBinary ? "hex" : "text") << "): " << std::endl << " ";
if(!isBinary) {
for(int k = 0; k < (int)pack_dec_res.decoding_result.payload.data8Bit.size(); k++) {
char chr = pack_dec_res.decoding_result.payload.data8Bit[k] & 0x7F;
if((chr < 0x20 && chr != '\n' && chr != '\r')) {
std::cout << "(" << std::hex << (uint16_t)chr << std::dec << ")";
} else if(chr != '\n' && chr != '\r') {
std::cout << chr;
} else {
std::cout << std::endl << " ";
}
}
} else {
for(int i = 0; i < (int)pack_dec_res.decoding_result.payload.data8Bit.size(); i++) {
uint8_t data = pack_dec_res.decoding_result.payload.data8Bit[i];
std::cout << std::hex << (uint16_t)data << std::dec << " ";
}
}
std::cout << std::endl;
}
break;
case 0xB2:
{
std::cout << " msgType: " << pack_dec_res.decoding_result.packetVars["messageType"] << " svcCd&AddrName: " << pack_dec_res.decoding_result.packetVars["serviceCodeAndAddressName"] << " contin: " << pack_dec_res.decoding_result.packetVars["continuation"] << " prio: " << pack_dec_res.decoding_result.packetVars["priorityText"] << " rep: " << pack_dec_res.decoding_result.packetVars["repetition"] << " msgId: " << pack_dec_res.decoding_result.packetVars["messageId"] << " packetNo: " << pack_dec_res.decoding_result.packetVars["packetNo"] << " isNewPayl: " << pack_dec_res.decoding_result.packetVars["isNewPayload"] << " addrHex: " << pack_dec_res.decoding_result.packetVars["addressHex"] << std::endl;
bool isBinary = pack_dec_res.decoding_result.payload.presentation == PACKETDECODER_PRESENTATION_BINARY;
std::cout << " Payload(" << (isBinary ? "hex" : "text") << "): " << std::endl << " ";
if(!isBinary) {
for(int k = 0; k < (int)pack_dec_res.decoding_result.payload.data8Bit.size(); k++) {
char chr = pack_dec_res.decoding_result.payload.data8Bit[k] & 0x7F;
if((chr < 0x20 && chr != '\n' && chr != '\r')) {
std::cout << "(" << std::hex << (uint16_t)chr << std::dec << ")";
} else if(chr != '\n' && chr != '\r') {
std::cout << chr;
} else {
std::cout << std::endl << " ";
}
}
} else {
for(int i = 0; i < (int)pack_dec_res.decoding_result.payload.data8Bit.size(); i++) {
uint8_t data = pack_dec_res.decoding_result.payload.data8Bit[i];
std::cout << std::hex << (uint16_t)data << std::dec << " ";
}
}
std::cout << std::endl;
}
break;
case 0xBD:
{
//nothing
}
break;
case 0xBE:
{
//nothing
}
break;
default:
break;
}
}
std::cout << std::endl;
}
}
int main(int argc, char* argv[]) {
std::map<std::string, std::string> params;
if(argc < 2) {
printHelp();
return 1;
}
for(int i = 1; i < argc; i++) {
int res = parseArg(argc, &i, argv, ¶ms, false);
if(res == 1 or res == 2) {
std::cout << "Wrong args!" << std::endl;
printHelp();
return 1;
}
}
bool isFrameparserVerbose = params.find("frameparserVerbose") != params.end() && params["frameparserVerbose"] == "true";
bool isFrameparserPrintAllPackets = params.find("frameparserPrintAllPackets") != params.end() && params["frameparserPrintAllPackets"] == "true";
bool isFrameparserOutUdp = params.find("frameparserOutUdp") != params.end() && params["frameparserOutUdp"] == "true";
sockaddr_in clientaddr;
int sockfd;
if(isFrameparserOutUdp) {
std::string udpIp = params["frameparserOutUdpIp"];
std::string udpPort = params["frameparserOutUdpPort"];
memset(&clientaddr, 0, sizeof(clientaddr));
// Filling server information
clientaddr.sin_family = AF_INET;
clientaddr.sin_port = htons(std::stoi(udpPort));
clientaddr.sin_addr.s_addr=inet_addr(udpIp.c_str());
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
std::cout << "Socket creation failed!" << std::endl;
return 1;
}
}
if(params.find("frameparserSource") == params.end()) {
std::cout << "Wrong/No source selected!" << std::endl;
printHelp();
return 1;
}
std::string frameparserSource = params["frameparserSource"];
inmarsatc::frameParser::FrameParser parser;
if(frameparserSource == "udp") {
if(params.find("frameparserSourceUdpPort") == params.end()) {
std::cout << "Udp port not specified!" << std::endl;
return 1;
}
std::string udpPort = params["frameparserSourceUdpPort"];
int clisockfd;
if ((clisockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
std::cout << "Socket creation failed!" << std::endl;
return 1;
}
// Filling server information
sockaddr_in serveraddr, cliaddr;
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(std::stoi(udpPort));
serveraddr.sin_addr.s_addr=INADDR_ANY;
if (bind(clisockfd, (const struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0) {
std::cout << "Binding to port failed!" << std::endl;
return 1;
}
int received;
while(true) {
inmarsatc::decoder::Decoder::decoder_result frame = receiveDemodDataViaUdp(clisockfd, serveraddr);
std::vector<inmarsatc::frameParser::FrameParser::frameParser_result> pack_dec_res_vec = parser.parseFrame(frame);
for(int k = 0; k < (int)pack_dec_res_vec.size(); k++) {
inmarsatc::frameParser::FrameParser::frameParser_result pack_dec_res = pack_dec_res_vec[k];
if(!pack_dec_res.decoding_result.isDecodedPacket || !pack_dec_res.decoding_result.isCrc) {
continue;
}
printFrameParserPacket(pack_dec_res, isFrameparserPrintAllPackets, isFrameparserVerbose);
if(isFrameparserOutUdp) {
if((isFrameparserPrintAllPackets || (ifPacketIsMessage(pack_dec_res))) && pack_dec_res.decoding_result.isDecodedPacket) {
json j;
j["frameNumber"] = pack_dec_res.decoding_result.frameNumber;
j["timestamp"] = pack_dec_res.decoding_result.timestamp.time_since_epoch().count();
j["packetDescriptor"] = pack_dec_res.decoding_result.packetDescriptor;
j["packetLength"] = pack_dec_res.decoding_result.packetLength;
j["decodingStage"] = pack_dec_res.decoding_result.decodingStage == PACKETDECODER_DECODING_STAGE_COMPLETE ? "Complete" : (pack_dec_res.decoding_result.decodingStage == PACKETDECODER_DECODING_STAGE_PARTIAL ? "Partial" : "None");
j["payload"]["presentation"] = pack_dec_res.decoding_result.payload.presentation;
std::ostringstream os;
for(int i = 0; i < (int)pack_dec_res.decoding_result.payload.data8Bit.size(); i++) {
char chr = pack_dec_res.decoding_result.payload.data8Bit[i] & 0b01111111;
if((chr < 0x20 && chr != '\n' && chr != '\r')) {
os << "(" << std::hex << (uint16_t)chr << std::dec << ")";
} else if(chr != '\n' && chr != '\r') {
os << chr;
} else {
os << std::endl;
}
}
std::string payload_data = os.str();
j["payload"]["data"] = payload_data;
j["packetVars"] = pack_dec_res.decoding_result.packetVars;
std::string data = j.dump(-1, ' ', false, json::error_handler_t::ignore);
sendParserDataViaUdp(data, sockfd, clientaddr);
}
}
}
}
}
}