Use MAC address documentation range in filter examples
[metze/wireshark/wip.git] / epan / dissectors / packet-dmx-chan.c
1 /* packet-dmx-chan.c
2  * DMX Channel Data packet disassembly.
3  *
4  * This dissector is written by
5  *
6  *  Erwin Rol <erwin@erwinrol.com>
7  *  Copyright 2011 Erwin Rol
8  *
9  *  Wireshark - Network traffic analyzer
10  *  Gerald Combs <gerald@wireshark.org>
11  *  Copyright 1999 Gerald Combs
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor
26  * Boston, MA  02110-1301, USA.
27  */
28
29 /*
30  * This dissector is based on;
31  * American National Standard E1.11 - 2004
32  * Entertainment Technology USITT DMX512-A
33  * Asynchronous Serial Digital Data Transmission Standard
34  * for Controlling Lighting Equipment and Accessories
35  */
36
37 #include "config.h"
38
39 #include <epan/packet.h>
40 #include <epan/prefs.h>
41
42 void proto_register_dmx_chan(void);
43
44 static int proto_dmx_chan = -1;
45
46 static int hf_dmx_chan_output_dmx_data = -1;
47 static int hf_dmx_chan_output_data_filter = -1;
48
49 static int ett_dmx_chan = -1;
50
51 /*
52  * Here are the global variables associated with the preferences for DMX
53  */
54 static gint global_disp_chan_val_type = 0;
55 static gint global_disp_col_count     = 16;
56 static gint global_disp_chan_nr_type  = 0;
57
58 static void
59 dissect_dmx_chan(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
60 {
61         col_set_str(pinfo->cinfo, COL_PROTOCOL, "DMX Channels");
62         col_clear(pinfo->cinfo, COL_INFO);
63
64         if (tree != NULL) {
65                 static const char *chan_format[]   = {
66                         "%2u%% ",
67                         "0x%02x ",
68                         "%3u "
69                 };
70                 static const char *string_format[] = {
71                         "0x%03x: %s",
72                         "%3u: %s"
73                 };
74                 wmem_strbuf_t *chan_str = wmem_strbuf_new_label(wmem_packet_scope());
75                 proto_item    *item;
76                 guint16        length,r,c,row_count;
77                 guint8         v;
78                 guint          offset   = 0;
79
80                 proto_tree    *ti = proto_tree_add_item(tree, proto_dmx_chan, tvb, offset, -1, ENC_NA);
81                 proto_tree    *dmx_chan_tree = proto_item_add_subtree(ti, ett_dmx_chan);
82
83                 length = tvb_reported_length_remaining(tvb, offset);
84
85                 row_count = (length / global_disp_col_count) + ((length % global_disp_col_count) == 0 ? 0 : 1);
86                 for (r = 0; r < row_count;r++) {
87                         wmem_strbuf_truncate(chan_str, 0);
88                         for (c = 0;(c < global_disp_col_count) && (((r * global_disp_col_count) + c) < length);c++) {
89                                 if ((global_disp_col_count >= 2) && ((c % (global_disp_col_count / 2)) == 0)) {
90                                         wmem_strbuf_append(chan_str, " ");
91                                 }
92
93                                 v = tvb_get_guint8(tvb, (offset + (r * global_disp_col_count) + c));
94                                 if (global_disp_chan_val_type == 0) {
95                                         v = (v * 100) / 255;
96                                         if (v == 100) {
97                                                 wmem_strbuf_append(chan_str, "FL ");
98                                         } else {
99                                                 wmem_strbuf_append_printf(chan_str, chan_format[global_disp_chan_val_type], v);
100                                         }
101                                 } else {
102                                         wmem_strbuf_append_printf(chan_str, chan_format[global_disp_chan_val_type], v);
103                                 }
104                         }
105
106                         proto_tree_add_none_format(dmx_chan_tree, hf_dmx_chan_output_dmx_data, tvb,
107                                                         offset+(r * global_disp_col_count), c,
108                                                         string_format[global_disp_chan_nr_type],
109                                                         (r * global_disp_col_count) + 1, wmem_strbuf_get_str(chan_str));
110                 }
111
112                 /* Add the real type hidden */
113                 item = proto_tree_add_item(dmx_chan_tree, hf_dmx_chan_output_data_filter, tvb,
114                                                 offset, length, ENC_NA );
115                 PROTO_ITEM_SET_HIDDEN(item);
116         }
117 }
118
119 void
120 proto_register_dmx_chan(void)
121 {
122         static hf_register_info hf[] = {
123                 { &hf_dmx_chan_output_data_filter,
124                         { "DMX data filter",
125                                 "dmx_chan.data_filter",
126                                 FT_BYTES, BASE_NONE, NULL, 0x0,
127                                 NULL, HFILL }},
128
129                 { &hf_dmx_chan_output_dmx_data,
130                         { "DMX data",
131                                 "dmx_chan.dmx_data",
132                                 FT_NONE, BASE_NONE, NULL, 0x0,
133                                 NULL, HFILL }},
134         };
135
136         static gint *ett[] = {
137                 &ett_dmx_chan
138         };
139
140         module_t *dmx_chan_module;
141
142         static const enum_val_t disp_chan_val_types[] = {
143                 { "pro", "Percent", 0 },
144                 { "hex", "Hexadecimal", 1 },
145                 { "dec", "Decimal", 2 },
146                 { NULL, NULL, 0 }
147         };
148
149         static const enum_val_t disp_chan_nr_types[] = {
150                 { "hex", "Hexadecimal", 0 },
151                 { "dec", "Decimal", 1 },
152                 { NULL, NULL, 0 }
153         };
154
155         static const enum_val_t col_count[] = {
156                 {  "6",  "6",  6 },
157                 { "10", "10", 10 },
158                 { "12", "12", 12 },
159                 { "16", "16", 16 },
160                 { "24", "24", 24 },
161                 { NULL, NULL, 0 }
162         };
163
164         proto_dmx_chan = proto_register_protocol("DMX Channels","DMX Channels", "dmx-chan");
165         proto_register_field_array(proto_dmx_chan, hf, array_length(hf));
166         proto_register_subtree_array(ett, array_length(ett));
167         register_dissector("dmx-chan", dissect_dmx_chan, proto_dmx_chan);
168
169         dmx_chan_module = prefs_register_protocol(proto_dmx_chan, NULL);
170
171         prefs_register_enum_preference(dmx_chan_module, "dmx_disp_chan_val_type",
172                                         "DMX Display channel value type",
173                                         "The way DMX values are displayed",
174                                         &global_disp_chan_val_type,
175                                         disp_chan_val_types, FALSE);
176
177         prefs_register_enum_preference(dmx_chan_module, "dmx_disp_chan_nr_type",
178                                         "DMX Display channel nr. type",
179                                         "The way DMX channel numbers are displayed",
180                                         &global_disp_chan_nr_type,
181                                         disp_chan_nr_types, FALSE);
182
183         prefs_register_enum_preference(dmx_chan_module, "dmx_disp_col_count",
184                                         "DMX Display Column Count",
185                                         "The number of columns for the DMX display",
186                                         &global_disp_col_count,
187                                         col_count, FALSE);
188 }
189
190 /*
191  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
192  *
193  * Local variables:
194  * c-basic-offset: 8
195  * tab-width: 8
196  * indent-tabs-mode: t
197  * End:
198  *
199  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
200  * :indentSize=8:tabSize=8:noTabs=false:
201  */