forked from szpajder/dumpvdl2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecpdlc.c
153 lines (148 loc) · 4.49 KB
/
decpdlc.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
/*
* decpdlc - a simple FANS-1/A CPDLC message decoder
*
* Copyright (c) 2018 Tomasz Lemiech <szpajder@gmail.com>
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "dumpvdl2.h"
#include "cpdlc.h"
FILE *outf;
uint32_t msg_filter = MSGFLT_ALL;
size_t slurp_hexstring(char* string, uint8_t **buf);
uint8_t dump_asn1 = 0;
void usage() {
fprintf(stderr,
"decpdlc version %s\n"
"(c) 2018 Tomasz Lemiech <szpajder@gmail.com>\n"
"A little utility for decoding FANS-1/A CPDLC messages embedded in ACARS text\n\n"
"Usage:\n\n"
"To decode a single message from command line:\n\n"
"\t./decpdlc <direction> <acars_message_text>\n\n"
"where <direction> is one of:\n"
"\tu - means \"uplink\" (ground-to-air message)\n"
"\td - means \"downlink\" (air-to-ground message)\n\n"
"Enclose ACARS message text in quotes if it contains spaces or other shell\n"
"special shell characters, like '#'.\n\n"
"Example: ./decpdlc u '- #MD/AA ATLTWXA.CR1.N7881A203A44E8E5C1A932E80E'\n\n"
"To decode multiple messages from a text file:\n\n"
"1. Prepare a file with multiple messages, one per line. Precede each line\n"
" with 'u' or 'd' (to indicate message direction) and a space. Direction\n"
" indicator must appear as a first character on the line (no preceding\n"
" spaces please). Example:\n\n"
"u /AKLCDYA.AT1.9M-MTB215B659D84995674293583561CB9906744E9AF40F9EB\n"
"u /AKLCDYA.AT1.B-27372142ABDD84A7066418F583561CB9906744E9AF405DA1\n"
"d /MSTEC7X.AT1.VT-ANE21409DCC3DD03BB52350490502B2E5129D5A15692BA009A08892E7CC831E210A4C06EEBC28B1662BC02360165C80E1F7\n"
"u - #MD/AA ATLTWXA.CR1.N856DN203A3AA8E5C1A9323EDD\n\n"
"2. Run decpdlc and pipe the the file contents on standard input:\n\n"
"\t./decpdlc < cpdlc_messages.txt\n\n"
"Supported FANS-1/A message types: CR1, CC1, DR1, AT1\n",
DUMPVDL2_VERSION);
}
void parse(char *txt, uint32_t *msg_type) {
void *ptr = NULL;
cpdlc_msgid_t msgid = CPDLC_MSG_UNKNOWN;
char *s = strstr(txt, ".AT1");
if(s != NULL) {
msgid = CPDLC_MSG_AT1;
goto msgid_set;
}
s = strstr(txt, ".CR1");
if(s != NULL) {
msgid = CPDLC_MSG_CR1;
goto msgid_set;
}
s = strstr(txt, ".CC1");
if(s != NULL) {
msgid = CPDLC_MSG_CC1;
goto msgid_set;
}
s = strstr(txt, ".DR1");
if(s != NULL) {
msgid = CPDLC_MSG_DR1;
goto msgid_set;
}
msgid_set:
if(msgid == CPDLC_MSG_UNKNOWN) {
fprintf(stderr, "not a FANS-1/A CPDLC message\n");
return;
}
s += 4;
if(strlen(s) < 7) {
fprintf(stderr, "regnr not found\n");
goto end;
}
s += 7;
uint8_t *buf = NULL;
size_t buflen = slurp_hexstring(s, &buf);
ptr = cpdlc_parse_msg(msgid, buf, (size_t)buflen, msg_type);
end:
outf = stdout;
fprintf(outf, "%s\n", txt);
if(ptr != NULL) {
cpdlc_output_msg(ptr);
fprintf(outf, "\n");
}
free(buf);
}
int main(int argc, char **argv) {
uint32_t msg_type = 0;
if(argc > 1 && !strcmp(argv[1], "-h")) {
usage();
exit(0);
} else if(argc < 2) {
fprintf(stderr,
"No command line options found - reading messages from standard input.\n"
"Use '-h' option for help.\n"
);
char buf[1024];
for(;;) {
memset(buf, 0, sizeof(buf));
msg_type = 0;
if(fgets(buf, sizeof(buf), stdin) == NULL)
break;
char *end = strchr(buf, '\n');
if(end)
*end = '\0';
if(strlen(buf) < 3 || (buf[0] != 'u' && buf[0] != 'd') || buf[1] != ' ') {
fprintf(stderr, "Garbled input: expecting 'u|d acars_message_text'\n");
continue;
}
if(buf[0] == 'u')
msg_type |= MSGFLT_SRC_GND;
else if(buf[0] == 'd')
msg_type |= MSGFLT_SRC_AIR;
parse(buf, &msg_type);
}
} else if(argc == 3) {
if(argv[1][0] == 'u')
msg_type |= MSGFLT_SRC_GND;
else if(argv[1][0] == 'd')
msg_type |= MSGFLT_SRC_AIR;
else {
fprintf(stderr, "Invalid command line options\n\n");
usage();
exit(1);
}
parse(argv[2], &msg_type);
} else {
fprintf(stderr, "Invalid command line options\n\n");
usage();
exit(1);
}
}