summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-r09.c
blob: fe77bee87ff5c1f370a6f6a6d44e02040ffbbd12 (plain)
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
/*
 * R09.x public transport priority telegrams
 *
 * Anlagen zu VOEV 04.05 "LSA/R09.14 and R09.16"
 * https://www.vdv.de/voev-04-05-1-erg.pdfx
 *
 * Copyright 2020, Tomas Kukosa
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <epan/packet.h>

void proto_register_r09(void);
void proto_reg_handoff_r09(void);

#define PNAME  "R09.x"
#define PSNAME "R09"
#define PFNAME "r09"

static int proto_r09 = -1;
static int hf_r09_modus = -1;
static int hf_r09_ty = -1;
static int hf_r09_tl = -1;
static int hf_r09_zv = -1;
static int hf_r09_zw = -1;
static int hf_r09_mp8 = -1;
static int hf_r09_mp16 = -1;
static int hf_r09_pr = -1;
static int hf_r09_ha = -1;
static int hf_r09_ln = -1;
static int hf_r09_kn = -1;
static int hf_r09_zn = -1;
static int hf_r09_zl = -1;
static int hf_r09_fn = -1;
static int hf_r09_un = -1;

static gint ett_r09 = -1;

static dissector_handle_t r09_handle;

static const value_string r09_zv_vals[] = {
    { 0x00, "Verspätung" },
    { 0x01, "Verfrühung/Vorsprung" },
    {0, NULL}
};

static const value_string r09_ha_vals[] = {
    { 0x00, "Ohne Bedeutung" },
    { 0x01, "Taste 'gerade' betätig" },
    { 0x02, "Taste 'links' betätig" },
    { 0x03, "Taste 'rechts' betätig" },
    {0, NULL}
};

static dgt_set_t Dgt0_9_bcd = {
    {
        /*  0   1   2   3   4   5   6   7   8   9   a   b   c   d   e   f */
           '0','1','2','3','4','5','6','7','8','9','?','?','?','?','?','?'
    }
};

static int
dissect_r09(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_) {
    proto_item *ti= NULL;
    proto_tree *r09_tree = NULL;
    guint8 ib1, ib2;
    guint8 ty, tl;
    guint16 mp;
    const gchar *r09x_str, *ln_str, *kn_str, *zn_str, *fn_str, *un_str;

    ib1 = tvb_get_guint8(tvb, 0);
    ty = ib1 & 0x0F;

    if (ib1 != 0x91) {
        return 0;
    }

    ib2 = tvb_get_guint8(tvb, 1);
    tl = ib2 & 0x0F;

    r09x_str = wmem_strdup_printf(pinfo->pool, "R09.%u%u", ty, tl);
    col_append_fstr(pinfo->cinfo, COL_INFO, " %s", r09x_str);

    ti = proto_tree_add_protocol_format(tree, proto_r09, tvb, 0, -1, "%s", r09x_str);
    r09_tree = proto_item_add_subtree(ti, ett_r09);

    /* Infobyte 1 */
    proto_tree_add_item(r09_tree, hf_r09_modus, tvb, 0, 1, ENC_BIG_ENDIAN);
    proto_tree_add_item(r09_tree, hf_r09_ty, tvb, 0, 1, ENC_BIG_ENDIAN);

    /* Infobyte 2 */
    proto_tree_add_item(r09_tree, hf_r09_zv, tvb, 1, 1, ENC_BIG_ENDIAN);
    proto_tree_add_item(r09_tree, hf_r09_zw, tvb, 1, 1, ENC_BIG_ENDIAN);
    proto_tree_add_item(r09_tree, hf_r09_tl, tvb, 1, 1, ENC_BIG_ENDIAN);

    if (tl == 0) {
        /* Infobyte 3 */
        proto_tree_add_item(r09_tree, hf_r09_mp8, tvb, 2, 1, ENC_BIG_ENDIAN);
        mp = tvb_get_guint8(tvb, 2);
    } else {
        /* Infobyte 3, Zusatzbyte 1 */
        proto_tree_add_item(r09_tree, hf_r09_mp16, tvb, 2, 2, ENC_BIG_ENDIAN);
        mp = tvb_get_guint16(tvb, 2, ENC_BIG_ENDIAN);
    }
    col_append_fstr(pinfo->cinfo, COL_INFO, " MP=%u", mp);

    if (tl >= 2) {
        /* Zusatzbyte 2 */
        proto_tree_add_item(r09_tree, hf_r09_pr, tvb, 4, 1, ENC_BIG_ENDIAN);
        proto_tree_add_item(r09_tree, hf_r09_ha, tvb, 4, 1, ENC_BIG_ENDIAN);
    }

    if (tl >= 3) {
        /* Zusatzbyte 2, 3 */
        ln_str = tvb_get_bcd_string(pinfo->pool, tvb, 4, 2, &Dgt0_9_bcd, TRUE, FALSE, TRUE);
        proto_tree_add_string(r09_tree, hf_r09_ln, tvb, 4, 2, ln_str);
    }

    if (tl >= 4) {
        /* Zusatzbyte 4 */
        kn_str = tvb_get_bcd_string(pinfo->pool, tvb, 6, 1, &Dgt0_9_bcd, FALSE, FALSE, TRUE);
        proto_tree_add_string(r09_tree, hf_r09_kn, tvb, 6, 1, kn_str);
    }

    if (tl >= 6) {
        /* Zusatzbyte 5, 6 */
        zn_str = tvb_get_bcd_string(pinfo->pool, tvb, 7, 2, &Dgt0_9_bcd, FALSE, TRUE, TRUE);
        proto_tree_add_string(r09_tree, hf_r09_zn, tvb, 7, 2, zn_str);
    }

    if (tl == 6) {
        /* Zusatzbyte 6 */
        proto_tree_add_item(r09_tree, hf_r09_zl, tvb, 8, 1, ENC_BIG_ENDIAN);
    }

    if (tl == 8) {
        /* Zusatzbyte 6, 7, 8 */
        fn_str = tvb_get_bcd_string(pinfo->pool, tvb, 8, 2, &Dgt0_9_bcd, TRUE, FALSE, TRUE);
        proto_tree_add_string(r09_tree, hf_r09_fn, tvb, 8, 2, fn_str);
        un_str = tvb_get_bcd_string(pinfo->pool, tvb, 10, 1, &Dgt0_9_bcd, FALSE, FALSE, TRUE);
        proto_tree_add_string(r09_tree, hf_r09_un, tvb, 10, 1, un_str);
    }

    return tvb_captured_length(tvb);
}

void
proto_register_r09(void)
{

    static hf_register_info hf[] = {
        { &hf_r09_modus, { "Modus", "r09.modus",
            FT_UINT8, BASE_DEC, NULL, 0xF0, NULL, HFILL}
        },
        { &hf_r09_ty, { "TY", "r09.ty",
            FT_UINT8, BASE_DEC, NULL, 0x0F, "Typ", HFILL}
        },
        { &hf_r09_zv, { "ZV", "r09.zv",
            FT_UINT8, BASE_DEC, VALS(r09_zv_vals), 0x80, "Vorzeichen einer Fahrplanabweichung", HFILL}
        },
        { &hf_r09_zw, { "ZW", "r09.zw",
            FT_UINT8, BASE_DEC, NULL, 0x70, "Betrag einer Fahrplanabweichung", HFILL}
        },
        { &hf_r09_tl, { "TL", "r09.tl",
            FT_UINT8, BASE_DEC, NULL, 0x0F, "Anzahl der Zusatzbytes", HFILL}
        },
        { &hf_r09_mp8, { "MP", "r09.mp",
            FT_UINT8, BASE_DEC_HEX, NULL, 0x00, "Meldepunktnummer", HFILL}
        },
        { &hf_r09_mp16, { "MP", "r09.mp",
            FT_UINT16, BASE_DEC_HEX, NULL, 0x00, "Meldepunktnummer", HFILL}
        },
        { &hf_r09_pr, { "PR", "r09.pr",
            FT_UINT8, BASE_DEC, NULL, 0xC0, "Priorität", HFILL}
        },
        { &hf_r09_ha, { "HA", "r09.ha",
            FT_UINT8, BASE_DEC, VALS(r09_ha_vals), 0x30, "Anforderung manuell ausgelöst", HFILL}
        },
        { &hf_r09_ln, { "LN", "r09.ln",
            FT_STRING, BASE_NONE, NULL, 0x00, "Liniennummer", HFILL}
        },
        { &hf_r09_kn, { "KN", "r09.kn",
            FT_STRING, BASE_NONE, NULL, 0x00, "Kuzrsnummer", HFILL}
        },
        { &hf_r09_zn, { "ZN", "r09.zn",
            FT_STRING, BASE_NONE, NULL, 0x00, "Zielnummer", HFILL}
        },
        { &hf_r09_zl, { "ZL", "r09.zl",
            FT_UINT8, BASE_DEC, NULL, 0x07, "Zuglänge", HFILL}
        },
        { &hf_r09_fn, { "FN", "r09.fn",
            FT_STRING, BASE_NONE, NULL, 0x00, "Fahrzeugnummer", HFILL}
        },
        { &hf_r09_un, { "UN", "r09.un",
            FT_STRING, BASE_NONE, NULL, 0x00, "Unternehmer", HFILL}
        },
    };

    static gint* ett[] = {
        &ett_r09,
    };

    proto_r09 = proto_register_protocol(PNAME, PSNAME, PFNAME);

    proto_register_subtree_array(ett, array_length(ett));
    proto_register_field_array(proto_r09, hf,array_length(hf));

    r09_handle = register_dissector(PFNAME, dissect_r09, proto_r09);

}

void
proto_reg_handoff_r09(void)
{

    if (find_dissector_table("cam.ptat")) {
        dissector_add_uint("cam.ptat", 1, r09_handle);
    }

}