forked from EliasOenal/multimon-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fms.c
379 lines (326 loc) · 13.2 KB
/
fms.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
/*
* fms.c -- fms decoder and packet dump
*
* Copyright (C) 2007
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* ---------------------------------------------------------------------- */
#include "multimon.h"
#include <string.h>
/* ---------------------------------------------------------------------- */
static void fms_disp_service_id(uint8_t service_id)
{
verbprintf(0, "%1x=", service_id);
switch (service_id)
{
case 0x0: verbprintf(0, "UNKNOWN \t"); break;
case 0x1: verbprintf(0, "POLIZEI \t"); break;
case 0x2: verbprintf(0, "BGS \t"); break;
case 0x3: verbprintf(0, "BKA \t"); break;
case 0x4: verbprintf(0, "KatS \t"); break;
case 0x5: verbprintf(0, "ZOLL \t"); break;
case 0x6: verbprintf(0, "Feuerwehr \t"); break;
case 0x7: verbprintf(0, "THW \t"); break;
case 0x8: verbprintf(0, "ASB \t"); break;
case 0x9: verbprintf(0, "Rotkreuz \t"); break;
case 0xa: verbprintf(0, "Johanniter \t"); break;
case 0xb: verbprintf(0, "Malteser \t"); break;
case 0xc: verbprintf(0, "DLRG \t"); break;
case 0xd: verbprintf(0, "Rettungsdienst\t"); break;
case 0xe: verbprintf(0, "ZivilSchutz \t"); break;
case 0xf: verbprintf(0, "FernWirk \t"); break;
}
}
static void fms_disp_state_id(uint8_t state_id, uint8_t loc_id)
{
verbprintf(0, "%1x=", state_id);
switch (state_id)
{
case 0x0: verbprintf(0, "Sachsen \t"); break;
case 0x1: verbprintf(0, "Bund \t"); break;
case 0x2: verbprintf(0, "Baden-Wurtemberg\t"); break;
case 0x3: verbprintf(0, "Bayern 1 \t"); break;
case 0x4: verbprintf(0, "Berlin \t"); break;
case 0x5: verbprintf(0, "Bremen \t"); break;
case 0x6: verbprintf(0, "Hamburg \t"); break;
case 0x7: verbprintf(0, "Hessen \t"); break;
case 0x8: verbprintf(0, "Niedersachsen \t"); break;
case 0x9: verbprintf(0, "Nordrhein-Wesfal\t"); break;
case 0xa: verbprintf(0, "Rheinland-Pfalz \t"); break;
case 0xb: verbprintf(0, "Schleswig-Holste\t"); break;
case 0xc: verbprintf(0, "Saarland \t"); break;
case 0xd: verbprintf(0, "Bayern 2 \t"); break;
case 0xe: if (loc_id < 50) verbprintf(0, "Meckl-Vorpommern\t"); else verbprintf(0, "Sachsen-Anhalt \t"); break;
case 0xf: if (loc_id < 50) verbprintf(0, "Brandenburg \t"); else verbprintf(0, "Thuringen \t"); break;
}
}
static void fms_disp_loc_id(uint8_t loc_id)
{
//fix due to wrong location id
//now we are according to TR-BOS
uint8_t tmp = 0;
tmp = loc_id;
loc_id <<= 4;
tmp >>= 4;
loc_id = loc_id^tmp;
verbprintf(0, "Ort 0x%2x=%03d\t", loc_id, loc_id);
}
static void fms_disp_vehicle_id(uint16_t vehicle_id)
{
uint8_t nib0 = (vehicle_id) & 0xF;
uint8_t nib1 = (vehicle_id >> 4) & 0xF;
uint8_t nib2 = (vehicle_id >> 8) & 0xF;
uint8_t nib3 = (vehicle_id >> 12) & 0xF;
verbprintf(0, "FZG %1x%1x%1x%1x\t", nib0, nib1, nib2, nib3);
}
static void fms_disp_state(uint8_t state, uint8_t direction)
{
verbprintf(0, "Status %1x=", state);
// TODO: Other services?
if (direction == 0){
// FZG -> LST
switch (state)
{
case 0x0: verbprintf(0, "Notfall \t"); break;
case 0x1: verbprintf(0, "Einbuchen \t"); break;
case 0x2: verbprintf(0, "Bereit Wache \t"); break;
case 0x3: verbprintf(0, "Einsatz Ab \t"); break; // Einsatz übernommen, "Ab"
case 0x4: verbprintf(0, "Am EinsatzZiel\t"); break; // "AN"
case 0x5: verbprintf(0, "Sprechwunsch \t"); break;
case 0x6: verbprintf(0, "Nicht Bereit \t"); break;
case 0x7: verbprintf(0, "Patient aufgen\t"); break; // Patient aufgenommen
case 0x8: verbprintf(0, "Am TranspZiel \t"); break;
case 0x9: verbprintf(0, "Arzt Aufgenomm\t"); break; // Arzt aufgenommen / Handquittung / Anmeldung im Fremdkreis / Dringender Sprechwunsch
case 0xa: verbprintf(0, "Vorbertg Folge\t"); break; // Vorbereitung Folgetelegram
case 0xb: verbprintf(0, "Beendig Folge\t"); break; // Beendigung Folgetelegram
case 0xc: verbprintf(0, "Sonder 1 \t"); break;
case 0xd: verbprintf(0, "Sonder 2 \t"); break;
case 0xe: verbprintf(0, "AutomatQuittun\t"); break; // Automatische Quittung
case 0xf: verbprintf(0, "Sprechtaste \t"); break;
}
}
else
{
// LST -> FZG
switch (state)
{
case 0x0: verbprintf(0, "StatusAbfrage \t"); break;
case 0x1: verbprintf(0, "SammelRuf \t"); break;
case 0x2: verbprintf(0, "Einrucken/Abbr\t"); break; // Einrücken / Einsatz abgebrochen
case 0x3: verbprintf(0, "Ubernahme \t"); break; // Melden für Einsatzübernahme
case 0x4: verbprintf(0, "Kommen Draht \t"); break; // "Kommen Sie über Draht"
case 0x5: verbprintf(0, "Fahre Wache \t"); break; // "Fahren Sie Wache an"
case 0x6: verbprintf(0, "Sprechaufford \t"); break; // Sprechaufforderung
case 0x7: verbprintf(0, "Lagemeldung \t"); break; // "Geben Sie Lagemeldung"
case 0x8: verbprintf(0, "FernWirk 1 \t"); break;
case 0x9: verbprintf(0, "FernWirk 2 \t"); break;
case 0xa: verbprintf(0, "Vorbertg TXT \t"); break;
case 0xb: verbprintf(0, "Beendig TXT \t"); break;
case 0xc: verbprintf(0, "KurzTXT C \t"); break;
case 0xd: verbprintf(0, "KurzTXT D \t"); break;
case 0xe: verbprintf(0, "KurzTXT E \t"); break;
case 0xf: verbprintf(0, "AutomatQuittun\t"); break; // Automatische Quittung
}
}
}
static void fms_disp_direction(uint8_t direction)
{
if (direction)
{
verbprintf(0, "1=LST->FZG\t");
}
else
{
verbprintf(0, "0=FZG->LST\t");
}
}
static void fms_disp_shortinfo(uint8_t short_info)
{
verbprintf(0, "%1x=", short_info);
switch (short_info)
{
case 0x0: verbprintf(0, "I (ohneNA,ohneSIGNAL)\t"); break;
case 0x1: verbprintf(0, "II (ohneNA,mit SIGNAL)\t"); break;
case 0x2: verbprintf(0, "III(mit NA,ohneSIGNAL)\t"); break;
case 0x3: verbprintf(0, "IV (mit NA,mit SIGNAL)\t"); break;
}
}
static void fms_print_crc(char crc[7]) {
int i;
verbprintf(2, "FMS CRC:");
for (i = 0; i < 7; ++i) {
if (crc[i]) {
verbprintf(2, "1");
} else {
verbprintf(2, "0");
}
}
verbprintf(2, "\n");
}
void fms_print_message_hex(uint64_t message)
{
verbprintf(2, "FMS MESSAGE HIGH: %08x\n", message >> 32);
verbprintf(2, "FMS MESSAGE LOW: %08x\n", message);
}
static char fms_is_crc_correct(uint64_t message)
{
int i;
char crc[7];
char doinvert;
// Initialize the crc to 0s
for (i=0; i<7; ++i)
{
crc[i] = 0;
}
for (i=0; i<48; ++i)
{
// update the running CRC
// The implementation is not the most performant one but it suffices for my basic understanding of CRCs.
// Code generated by http://ghsi.de/CRC/index.php
// ==========================================================================
// CRC Generation Unit - Linear Feedback Shift Register implementation
// (c) Kay Gorontzi, GHSi.de, distributed under the terms of LGPL
// ==========================================================================
verbprintf(4, "FMS CRC BIT: %1x\n", ((message >> (16+i)) & 1));
doinvert = ((message >> (16+i)) & 1) ^ crc[6]; // XOR required?
crc[6] = crc[5] ^ doinvert;
crc[5] = crc[4];
crc[4] = crc[3];
crc[3] = crc[2];
crc[2] = crc[1] ^ doinvert;
crc[1] = crc[0];
crc[0] = doinvert;
//fms_print_crc(crc);
}
for (i=0; i<7; ++i)
{
if (crc[i])
{
fms_print_crc(crc);
return 0;
}
}
return 1;
}
/*
* As specified in http://www.lfs-bw.de/Fachthemen/Digitalfunk-Funk/Documents/Pruefstelle/TRBOS-FMS.pdf
*/
static void fms_disp_packet(uint64_t message)
{
uint8_t service_id; // BOS-Kennung
uint8_t state_id; // Landeskennung
uint8_t loc_id; // Ortskennung
uint16_t vehicle_id; // Fahrzeugkennung
uint8_t state; // Status
// uint8_t model; // Baustufenkennung
uint8_t direction; // Richtungskennung
uint8_t short_info; // taktische Kurzinformation
uint8_t crc; // Redundanz
fms_print_message_hex(message);
verbprintf(0, "FMS: %08x%04x (", message >> 32, ((uint32_t)message >> 16));
service_id = (message >> 16) & 0xF;
fms_disp_service_id(service_id);
state_id = (message >> 20) & 0xF;
loc_id = (message >> 24) & 0xFF;
fms_disp_state_id(state_id, loc_id);
fms_disp_loc_id(loc_id);
vehicle_id = (message >> 32) & 0xFFFF;
fms_disp_vehicle_id(vehicle_id);
state = (message >> 48) & 0xF;
//model = (message >> 52) & 0x1;
direction = (message >> 53) & 0x1;
fms_disp_state(state, direction);
fms_disp_direction(direction);
short_info = (message >> 54) & 0x3;
fms_disp_shortinfo(short_info);
crc = (message >> 55) & 0x3F;
verbprintf(0, ") ");
if (fms_is_crc_correct(message))
{
verbprintf(0, "CRC correct");
if (message & 1)
{
verbprintf(0, " AFTER SWAPPING ONE BIT");
}
}
else
{
verbprintf(0, "CRC INCORRECT (%x)", crc);
}
verbprintf(0, "\n");
}
/* ---------------------------------------------------------------------- */
void fms_init(struct demod_state *s)
{
memset(&s->l2.uart, 0, sizeof(s->l2.uart));
s->l2.fmsfsk.rxstate = 0;
s->l2.fmsfsk.rxbitstream = 0;
s->l2.fmsfsk.rxbitcount = 0;
}
/* ---------------------------------------------------------------------- */
void fms_rxbit(struct demod_state *s, int bit)
{
uint64_t msg;
int i;
// General note on performance and logic: For the generation of the
// variable that tracks if a SYNC-frame has been received, we use
// a << since it is significantly faster (5s vs. 16s on a 20m test wave file)
// For the message itself, we use a >> since that makes the message easier to decode
// Append the bit to our "state machine tracker" so we can properly detect sync bits
s->l2.fmsfsk.rxstate = ((s->l2.fmsfsk.rxstate << 1) & 0x000FFFFE) | bit;
// Check if the sync pattern is in the buffer
if ((s->l2.fmsfsk.rxstate & 0x0007FFFF) == 0x7FF1A)
{
verbprintf(1, "FMS ->SYNC<-\n");
s->l2.fmsfsk.rxbitstream = 0; // reset RX buffer
s->l2.fmsfsk.rxbitcount = 1; // > 1 means we have a valid SYNC
}
// If we have a valid SYNC, record the message by appending it (from the left) to the RX buffer
else if (s->l2.fmsfsk.rxbitcount >= 1) {
verbprintf(4, "FMS BIT: %1x\n", bit);
s->l2.fmsfsk.rxbitstream = (s->l2.fmsfsk.rxbitstream >> 1) | ((uint64_t) bit << 63);
s->l2.fmsfsk.rxbitcount++;
// Wait until message has been completely received. If so, decode and display it and reset
if (s->l2.fmsfsk.rxbitcount == 49)
{
// If the CRC check fails, try to fix a one bit error
if (!fms_is_crc_correct(s->l2.fmsfsk.rxbitstream))
{
i = 0;
msg = s->l2.fmsfsk.rxbitstream;
while (i <= 47)
{
if (fms_is_crc_correct(msg ^ (1 << (i+16))))
{
verbprintf(2, "FMS was able to correct a one bit error by swapping bit %d Original packet:\n", i);
fms_disp_packet(s->l2.fmsfsk.rxbitstream);
s->l2.fmsfsk.rxbitstream = (msg ^ (1 << (i+16))) | 1; // lowest bit set means that the CRC has been corrected by us
break;
}
i++;
}
if (i == 48)
{
verbprintf(2, "FMS: unable to correct CRC error\n");
}
}
fms_disp_packet(s->l2.fmsfsk.rxbitstream);
s->l2.fmsfsk.rxbitcount = 0; // Reset counter, meaning "no valid SYNC yet"
s->l2.fmsfsk.rxstate = 0; // Reset message input buffer
}
}
}
/* ---------------------------------------------------------------------- */