-
Notifications
You must be signed in to change notification settings - Fork 1
/
tmcdecode.php
387 lines (363 loc) · 10.3 KB
/
tmcdecode.php
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
<?php
include_once('rdspdo.php');
function decode_time($time, $now = 0)
{
if(!$now)
$now = time();
$today = 86400 * (int)($now / 86400);
$tomorrow = $today + 86400;
if($time < 96)
return date("l j. F H:i T", $today + 900 * $time);
else if($time < 201)
return date("l j. F H:i T", $tomorrow + 3600 * ($time - 96));
else if($time < 232)
return ($time - 200) . ". " . date("F", strtotime(($time - 200 > date("j", $now) ? "today UTC" : "first day of next month UTC")));
else
return ($time % 2 ? "end of" : "middle of") . " " . date("F", strtotime((int)(($time - 230) / 2) . "/1"));
}
function decode_duration($dur, $type, $nature)
{
if(strpos($type, 'D') !== false)
{
$text = ($nature == 'F' ? "within the next " : "for at least ");
switch($dur)
{
case 1:
$text .= "15 minutes";
break;
case 2:
$text .= "30 minutes";
break;
case 3:
$text .= "1 hour";
break;
case 4:
$text .= "2 hours";
break;
case 5:
$text .= "3 hours";
break;
case 6:
$text .= "4 hours";
break;
case 7:
$text = ($nature == 'F' ? "later today" : "for the rest of the day");
break;
default:
$text = "no time given";
break;
}
}
else if(strpos($type, 'L') !== false)
{
switch($dur)
{
case 1:
$text = ($nature == 'F' ? "within the next few hours" : "for the next few hours");
break;
case 2:
$text = ($nature == 'F' ? "later today" : "for the rest of the day");
break;
case 3:
$text = ($nature == 'F' ? "tomorrow" : "until tomorrow evening");
break;
case 4:
$text = ($nature == 'F' ? "the day after tomorrow" : "for the rest of the week");
break;
case 5:
$text = ($nature == 'F' ? "this weekend" : "until the end of next week");
break;
case 6:
$text = ($nature == 'F' ? "later this week" : "until the end of the month");
break;
case 7:
$text = ($nature == 'F' ? "next week" : "for a long period");
break;
default:
$text = "no time given";
break;
}
}
else
$text = "";
return $text;
}
function decode_message($ecd, $lcd, $dir, $ext, $dur, $div, $bits)
{
static $ccnames = array('increase urgency', 'decrease urgency', 'switch directionality', 'switch duration', 'switch verbosity', 'set diversion', 'increase extent by 8', 'increase extent by 16');
$event = find_event($ecd);
$last = 0;
$message = array(); // Holds the whole message.
$message['ccodes'] = array(); // Holds the control codes.
$message['iblocks'] = array(); // Holds the information blocks.
$message['iblocks'][] = array(); // The first information block.
$message['iblocks'][0]['events'] = array($event);
$message['supps'] = array(); // Holds supplementary information.
$message['diversions'] = array(); // Holds detailed diversions.
$message['lcd'] = $lcd;
$message['direction'] = $dir;
$message['extent'] = $ext;
$message['directions'] = $event['direction'];
$message['urgency'] = $event['urgency'];
$message['nature'] = $event['nature'];
echo "Event: $ecd - " . $event['text'];
echo "\nLocation: $lcd";
echo "\nDirection: " . ($dir ? "negative" : "positive");
echo "\nExtent: $ext";
echo "\nBits: $bits";
if($dur)
{
echo "\nDuration: $dur";
$message['duration'] = $dur; // Duration only once per message.
$message['durtype'] = $event['duration'];
}
if($div !== null)
{
echo "\nDiversion: " . ($div ? "yes" : "no");
$message['diversion'] = $div;
}
while(strpos($bits, '1') !== false)
{
$label = bindec(substr($bits, 0, 4));
$bits = substr($bits, 4);
switch($label)
{
case 0:
$duration = bindec(substr($bits, 0, 3));
$bits = substr($bits, 3);
echo "\nDuration: $duration";
$message['duration'] = $duration; // Duration (only once per message).
$message['durtype'] = $message['iblocks'][count($message['iblocks']) - 1]['events'][count($message['iblocks'][count($message['iblocks']) - 1]['events']) - 1]['duration'];
break;
case 1:
$control = bindec(substr($bits, 0, 3));
$bits = substr($bits, 3);
echo "\nControl code: $control - " . $ccnames[$control];
$message['ccodes'][] = $control; // Add control code.
break;
case 2:
$lencd = bindec(substr($bits, 0, 5));
$bits = substr($bits, 5);
if($lencd == 0)
$length = "> 100km";
else if($lencd <= 10)
$length = $lencd . "km";
else if($lencd <= 15)
$length = sprintf("%dkm", 2 * $lencd - 10);
else
$length = sprintf("%dkm", 5 * $lencd - 55);
echo "\nAffected route length: $lencd - $length";
$message['iblocks'][count($message['iblocks']) - 1]['length'] = $length; // Affected route length per information block.
break;
case 3:
$spcd = bindec(substr($bits, 0, 5));
$bits = substr($bits, 5);
$speed = (5 * $spcd) . "km/h";
$message['iblocks'][count($message['iblocks']) - 1]['speed'] = $speed; // Speed limit advice per information block.
echo "\nSpeed limit: $spcd - $speed";
break;
case 4:
$qcd = bindec(substr($bits, 0, 5));
$bits = substr($bits, 5);
$message['iblocks'][count($message['iblocks']) - 1]['events'][count($message['iblocks'][count($message['iblocks']) - 1]['events']) - 1]['quant'] = $qcd;
echo "\nQuantifier (5 bits): $qcd";
break;
case 5:
$qcd = bindec(substr($bits, 0, 8));
$bits = substr($bits, 8);
$message['iblocks'][count($message['iblocks']) - 1]['events'][count($message['iblocks'][count($message['iblocks']) - 1]['events']) - 1]['quant'] = $qcd;
echo "\nQuantifier (8 bits): $qcd";
break;
case 6:
$scd = bindec(substr($bits, 0, 8));
$bits = substr($bits, 8);
$supp = find_supplement($scd);
echo "\nSupplementary information: $scd - " . $supp['text'];
$message['supps'][] = $supp; // Supplementary information (may be several per message).
break;
case 7:
$start = bindec(substr($bits, 0, 8));
$bits = substr($bits, 8);
echo "\nStart time: $start";
$message['start'] = $start; // Start time (only once per message).
break;
case 8:
$stop = bindec(substr($bits, 0, 8));
$bits = substr($bits, 8);
echo "\nStop time: $stop";
$message['stop'] = $stop; // Start time (only once per message).
break;
case 9:
$ecd = bindec(substr($bits, 0, 11));
$bits = substr($bits, 11);
$event = find_event($ecd);
$message['iblocks'][count($message['iblocks']) - 1]['events'][] = $event;
$message['directions'] = min($message['directions'], $event['direction']);
$message['urgency'] = max($message['urgency'], $event['urgency']);
if(strlen($message['nature']) < strlen($event['nature']))
$message['nature'] = $event['nature'];
echo "\nAdditional event: $ecd - " . $event['text'];
break;
case 10:
$diversion = bindec(substr($bits, 0, 16));
$bits = substr($bits, 16);
echo "\nDiversion: $diversion";
if($last != $label)
{
$message['diversions'][] = array();
$message['diversions'][count($message['diversions']) - 1]['route'] = array();
if($last == 11)
$message['diversions'][count($message['diversions']) - 1]['destinations'] = $dests;
}
$message['diversions'][count($message['diversions']) - 1]['route'][] = $diversion;
break;
case 11:
$destination = bindec(substr($bits, 0, 16));
$bits = substr($bits, 16);
echo "\nDestination: $destination";
if($last != $label)
$dests = array();
$dests[] = $destination;
if((bindec(substr($bits, 0, 4)) != 10) && (bindec(substr($bits, 0, 4)) != 11))
{
$message['iblocks'][count($message['iblocks']) - 1]['destinations'] = $dests;
unset($dests);
}
break;
case 12:
$reserved = bindec(substr($bits, 0, 16));
$bits = substr($bits, 16);
echo "\nReserved: $reserved";
break;
case 13:
$cross = bindec(substr($bits, 0, 16));
$bits = substr($bits, 16);
echo "\nCross link: $cross";
$message['cross'] = $cross; // Cross link (only once per message).
break;
case 14:
echo "\nSeparator";
$message['iblocks'][] = array(); // Create new information block.
break;
default:
break;
}
$last = $label;
}
foreach($message['ccodes'] as $control)
{
switch($control)
{
case 0:
$message['urgency'] = ($message['urgency'] + 1) % 3;
break;
case 1:
$message['urgency'] = ($message['urgency'] + 2) % 3;
break;
case 2:
$message['directions'] = 3 - $message['directions'];
break;
case 3:
$message['durtype'] = preg_replace_callback('/(D|L)/', function($matches) {return chr(144 - ord($matches[0]));}, $message['durtype']);
break;
case 4:
$message['durtype'] = (strlen($message['durtype']) == 3 ? substr($message['durtype'], 1, 1) : "({$message['durtype']})");
break;
case 5:
$message['diversion'] = 1;
break;
case 6:
$message['extent'] += 8;
break;
case 7:
$message['extent'] += 16;
break;
default:
break;
}
}
return $message;
}
function decode_tmc($blocks)
{
static $message = array();
static $last = array(0,0,0,0);
if(array_reduce(array_map(function($a, $b) {return $a == $b;}, $blocks, $last), function($a, $b) {return $a and $b;}, true))
return;
$x = $blocks[1] & 0x1f;
$y = $blocks[2];
$z = $blocks[3];
$result = false;
$multi = ($x >> 3) & 0x3;
if($multi == 1)
{
//echo "Single group message.\n";
$message['lcd'] = $z;
$message['ecd'] = $y & 0x7ff;
$message['ext'] = ($y >> 11) & 0x7;
$message['dir'] = ($y >> 14) & 0x1;
$message['div'] = ($y >> 15) & 0x1;
$message['dur'] = $x & 0x7;
$result = $message;
$message = array();
}
else if($multi == 0)
{
$continuity = $x & 0x7;
$first = ($y >> 15) & 0x1;
//echo "Multi group message; CI = $continuity;";
if($first)
{
//echo " first.\n";
$message['lcd'] = $z;
$message['ecd'] = $y & 0x7ff;
$message['ext'] = ($y >> 11) & 0x7;
$message['dir'] = ($y >> 14) & 0x1;
}
else
{
$second = ($y >> 14) & 0x1;
$gsi = ($y >> 12) & 0x3;
$bit = str_pad(decbin((($y & 0xfff) << 16) + $z), 28, '0', STR_PAD_LEFT);
if($second)
{
//echo " second;";
$message['bits'] = $bit;
}
else
{
$message['bits'] .= $bit;
}
//echo " gsi = $gsi.\n";
if(!$gsi)
{
if(array_key_exists('ecd', $message))
$result = $message;
$message = array();
}
}
}
$last = $blocks;
return $result;
}
function system_tmc($blocks)
{
echo "\nTMC system information:\n";
echo sprintf("Application ID %04x.\n", $blocks[3]);
if($blocks[3] != 0xcd46)
return;
switch($blocks[2] >> 14)
{
case 0:
$tabcd = ($blocks[2] >> 6) & 0x3f;
echo "Location table number $tabcd.\n";
break;
case 1:
$sid = ($blocks[2] >> 6) & 0x3f;
echo "Service identifier $sid.\n";
break;
default:
break;
}
}
?>