Revert "Fixup: tvb_* -> tvb_captured"
[metze/wireshark/wip.git] / epan / dissectors / packet-tdmoe.c
1 /* packet-tdmoe.c
2  * Routines for TDM over Ethernet packet disassembly
3  * Copyright 2010, Haakon Nessjoen <haakon.nessjoen@gmail.com>
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
22  */
23
24 #include "config.h"
25
26 #include <glib.h>
27
28 #include <epan/packet.h>
29 #include <epan/etypes.h>
30 #include <epan/prefs.h>
31
32 /* Bitmasks for the flags in the fourth byte of the packet */
33 #define TDMOE_YELLOW_ALARM_BITMASK 0x01
34 #define TDMOE_SIGBITS_BITMASK      0x02
35
36 void proto_reg_handoff_tdmoe(void);
37 void proto_register_tdmoe(void);
38
39 /* protocols and header fields */
40 static int proto_tdmoe = -1;
41
42 static int hf_tdmoe_subaddress = -1;
43 static int hf_tdmoe_samples = -1;
44 static int hf_tdmoe_flags = -1;
45 static int hf_tdmoe_yellow_alarm = -1;
46 static int hf_tdmoe_sig_bits_present = -1;
47 static int hf_tdmoe_packet_counter = -1;
48 static int hf_tdmoe_channels = -1;
49 static int hf_tdmoe_sig_bits = -1;
50
51 static gint ett_tdmoe = -1;
52 static gint ett_tdmoe_flags = -1;
53
54 static dissector_handle_t lapd_handle;
55 static dissector_handle_t data_handle;
56
57 static gint pref_tdmoe_d_channel = 24;
58
59 static int
60 dissect_tdmoe(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
61 {
62         proto_item *ti;
63         proto_tree *tdmoe_tree;
64         tvbuff_t   *next_client;
65         guint16     channels;
66         guint16     subaddress;
67         gint32 offset = 0;
68         static const gint *flags[] = { &hf_tdmoe_yellow_alarm, &hf_tdmoe_sig_bits_present, NULL };
69         int         chan;
70
71         /* Check that there's enough data */
72         if (tvb_length(tvb) < 8)
73                 return 0;
74
75         subaddress = tvb_get_ntohs(tvb, 0);
76         channels   = tvb_get_ntohs(tvb, 6);
77
78         col_set_str(pinfo->cinfo, COL_PROTOCOL, "TDMoE");
79         col_add_fstr(pinfo->cinfo, COL_INFO, "Subaddress: %d Channels: %d %s",
80                 subaddress,
81                 channels,
82                 (tvb_get_guint8(tvb, 3) & TDMOE_YELLOW_ALARM_BITMASK ? "[YELLOW ALARM]" : "")
83         );
84
85
86         /* create display subtree for the protocol */
87         ti = proto_tree_add_item(tree, proto_tdmoe, tvb, 0, -1, ENC_NA);
88         tdmoe_tree = proto_item_add_subtree(ti, ett_tdmoe);
89
90         /* Subaddress */
91         proto_tree_add_item(tdmoe_tree, hf_tdmoe_subaddress, tvb, offset, 2, ENC_BIG_ENDIAN);
92         offset+=2;
93
94         /* Samples (1 byte) */
95         proto_tree_add_item(tdmoe_tree, hf_tdmoe_samples, tvb, offset, 1, ENC_NA);
96         offset++;
97
98         /* Yellow alarm & "Sig bits present" bits (1 byte) */
99         proto_tree_add_bitmask(tdmoe_tree, tvb, offset, hf_tdmoe_flags, ett_tdmoe_flags, flags, ENC_BIG_ENDIAN);
100         offset++;
101
102         /* Packet counter (2 bytes) */
103         proto_tree_add_item(tdmoe_tree, hf_tdmoe_packet_counter, tvb, offset, 2, ENC_BIG_ENDIAN);
104         offset+=2;
105
106         /* Number of channels in packet (2 bytes) */
107         proto_tree_add_item(tdmoe_tree, hf_tdmoe_channels, tvb, offset, 2, ENC_BIG_ENDIAN);
108         offset+=2;
109
110         if (tvb_get_guint8(tvb, 3) & TDMOE_SIGBITS_BITMASK) {
111                 /* 4 sigbits per channel. Might be different on other sub-protocols? */
112                 guint16 length = (channels >> 1) + ((channels & 0x01) ? 1 : 0);
113
114                 proto_tree_add_item(tdmoe_tree, hf_tdmoe_sig_bits, tvb, offset, length, ENC_NA);
115                 offset += length;
116         }
117
118         /* The rest is SAMPLES * CHANNELS bytes of channel data */
119         for (chan = 1; chan <= channels; chan++) {
120                 next_client = tvb_new_subset_length(tvb, offset + ((chan - 1) * 8), 8);
121                 if (chan == pref_tdmoe_d_channel) {
122                         call_dissector(lapd_handle, next_client, pinfo, tree);
123                 } else {
124                         call_dissector(data_handle, next_client, pinfo, tree);
125                 }
126         }
127         return 1;
128 }
129
130 void
131 proto_register_tdmoe(void)
132 {
133         static hf_register_info hf[] = {
134
135                 { &hf_tdmoe_subaddress,
136                 { "Subaddress", "tdmoe.subaddress", FT_UINT8, BASE_DEC, NULL, 0x0,
137                         NULL, HFILL }},
138                 { &hf_tdmoe_samples,
139                 { "Samples",    "tdmoe.samples", FT_UINT8, BASE_DEC, NULL, 0x0,
140                         "Samples per channel", HFILL }},
141                 { &hf_tdmoe_flags,
142                 { "Flags",      "tdmoe.flags", FT_UINT8, BASE_HEX, NULL, 0x0,
143                         NULL, HFILL }},
144                 { &hf_tdmoe_yellow_alarm,
145                 { "Yellow Alarm", "tdmoe.yellowalarm", FT_BOOLEAN, 8, NULL, TDMOE_YELLOW_ALARM_BITMASK,
146                         NULL, HFILL }},
147                 { &hf_tdmoe_sig_bits_present,
148                 { "Sig bits present", "tdmoe.sig_bits_present", FT_BOOLEAN, 8, NULL, TDMOE_SIGBITS_BITMASK,
149                         NULL, HFILL }},
150                 { &hf_tdmoe_packet_counter,
151                 { "Counter", "tdmoe.counter", FT_UINT16, BASE_DEC, NULL, 0x0,
152                         "Packet number", HFILL }},
153                 { &hf_tdmoe_channels,
154                 { "Channels", "tdmoe.channels", FT_UINT16, BASE_DEC, NULL, 0x0,
155                         NULL, HFILL }},
156                 { &hf_tdmoe_sig_bits,
157                 { "Sig bits", "tdmoe.sig_bits", FT_BYTES, BASE_NONE, NULL, 0x0,
158                         NULL, HFILL }},
159         };
160         static gint *ett[] = {
161                 &ett_tdmoe,
162                 &ett_tdmoe_flags
163         };
164         module_t *tdmoe_module;
165
166         proto_tdmoe = proto_register_protocol("Digium TDMoE Protocol", "TDMoE", "tdmoe");
167         proto_register_field_array(proto_tdmoe, hf, array_length(hf));
168         proto_register_subtree_array(ett, array_length(ett));
169         tdmoe_module = prefs_register_protocol(proto_tdmoe, proto_reg_handoff_tdmoe);
170         prefs_register_uint_preference(tdmoe_module, "d_channel",
171                 "TDMoE D-Channel",
172                 "The TDMoE channel that contains the D-Channel.",
173                 10, &pref_tdmoe_d_channel);
174 }
175
176 void
177 proto_reg_handoff_tdmoe(void)
178 {
179         dissector_handle_t tdmoe_handle;
180
181         tdmoe_handle = new_create_dissector_handle(dissect_tdmoe, proto_tdmoe);
182         dissector_add_uint("ethertype", ETHERTYPE_TDMOE, tdmoe_handle);
183
184         lapd_handle = find_dissector("lapd-bitstream");
185         data_handle = find_dissector("data");
186 }