Fix for bug 5422:
[obnox/wireshark/wip.git] / epan / dissectors / packet-enttec.c
1 /* packet-enttec.c
2  * Routines for ENTTEC packet disassembly
3  *
4  * $Id$
5  *
6  * Copyright (c) 2003,2004 by Erwin Rol <erwin@erwinrol.com>
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1999 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 /* Include files */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <stdlib.h>
34 #include <ctype.h>
35 #include <time.h>
36 #include <epan/packet.h>
37 #include <epan/addr_resolv.h>
38 #include <epan/prefs.h>
39 #include <epan/strutil.h>
40
41 /*
42  * See
43  *
44  *      http://www.enttec.com/docs/enttec_protocol.pdf
45  */
46
47 /* Define UDP/TCP ports for ENTTEC */
48
49 #define UDP_PORT_ENTTEC 0x0D05
50 #define TCP_PORT_ENTTEC 0x0D05
51
52
53 #define ENTTEC_HEAD_ESPR 0x45535052
54 #define ENTTEC_HEAD_ESPP 0x45535050
55 #define ENTTEC_HEAD_ESAP 0x45534150
56 #define ENTTEC_HEAD_ESDD 0x45534444
57 #define ENTTEC_HEAD_ESNC 0x45534E43
58 #define ENTTEC_HEAD_ESZZ 0x45535A5A
59
60 static const value_string enttec_head_vals[] = {
61         { ENTTEC_HEAD_ESPR,     "Poll Reply" },
62         { ENTTEC_HEAD_ESPP,     "Poll" },
63         { ENTTEC_HEAD_ESAP,     "Ack/nAck" },
64         { ENTTEC_HEAD_ESDD,     "DMX Data" },
65         { ENTTEC_HEAD_ESNC,     "Config" },
66         { ENTTEC_HEAD_ESZZ,     "Reset" },
67         { 0,                    NULL }
68 };
69
70 #define ENTTEC_DATA_TYPE_DMX            0x01
71 #define ENTTEC_DATA_TYPE_CHAN_VAL       0x02
72 #define ENTTEC_DATA_TYPE_RLE            0x04
73
74 static const value_string enttec_data_type_vals[] = {
75         { ENTTEC_DATA_TYPE_DMX,         "Uncompressed DMX" },
76         { ENTTEC_DATA_TYPE_CHAN_VAL,    "Channel+Value" },
77         { ENTTEC_DATA_TYPE_RLE,         "RLE Compressed DMX" },
78         { 0,                            NULL }
79 };
80
81 void proto_reg_handoff_enttec(void);
82
83 /* Define the enttec proto */
84 static int proto_enttec = -1;
85
86 /* general */
87 static int hf_enttec_head = -1;
88
89 /* poll */
90 static int hf_enttec_poll_type = -1;
91
92 /* poll reply */
93 static int hf_enttec_poll_reply_mac = -1;
94 static int hf_enttec_poll_reply_node_type = -1;
95 static int hf_enttec_poll_reply_version = -1;
96 static int hf_enttec_poll_reply_switch = -1;
97 static int hf_enttec_poll_reply_name = -1;
98 static int hf_enttec_poll_reply_option = -1;
99 static int hf_enttec_poll_reply_tos = -1;
100 static int hf_enttec_poll_reply_ttl = -1;
101
102 /* dmx data */
103 static int hf_enttec_dmx_data_universe = -1;
104 static int hf_enttec_dmx_data_start_code = -1;
105 static int hf_enttec_dmx_data_type = -1;
106 static int hf_enttec_dmx_data_size = -1;
107 static int hf_enttec_dmx_data_data = -1;
108 static int hf_enttec_dmx_data_data_filter = -1;
109 static int hf_enttec_dmx_data_dmx_data = -1;
110
111 /* Define the tree for enttec */
112 static int ett_enttec = -1;
113
114 /*
115  * Here are the global variables associated with the preferences
116  * for enttec
117  */
118
119 static guint global_udp_port_enttec = UDP_PORT_ENTTEC;
120 static guint global_tcp_port_enttec = TCP_PORT_ENTTEC;
121
122 static gint global_disp_chan_val_type = 0;
123 static gint global_disp_col_count = 16;
124 static gint global_disp_chan_nr_type = 0;
125
126 static gint
127 dissect_enttec_poll_reply(tvbuff_t *tvb, guint offset, proto_tree *tree)
128 {
129         proto_tree_add_item(tree, hf_enttec_poll_reply_mac, tvb,
130                                         offset, 6, FALSE);
131         offset += 6;
132
133         proto_tree_add_item(tree, hf_enttec_poll_reply_node_type, tvb,
134                                         offset, 2, FALSE);
135         offset += 2;
136
137         proto_tree_add_item(tree, hf_enttec_poll_reply_version, tvb,
138                                         offset, 1, FALSE);
139         offset += 1;
140
141         proto_tree_add_item(tree, hf_enttec_poll_reply_switch, tvb,
142                                         offset, 1, FALSE);
143         offset += 1;
144         
145         proto_tree_add_item(tree, hf_enttec_poll_reply_name, tvb,
146                                         offset, 10, FALSE);
147         offset += 10;
148
149         proto_tree_add_item(tree, hf_enttec_poll_reply_option, tvb,
150                                         offset, 1, FALSE);
151         offset += 1;
152         
153         proto_tree_add_item(tree, hf_enttec_poll_reply_tos, tvb,
154                                         offset, 1, FALSE);
155         offset += 1;
156         
157         proto_tree_add_item(tree, hf_enttec_poll_reply_ttl, tvb,
158                                         offset, 1, FALSE);
159         offset += 1;
160
161         /* data */
162
163         return offset;
164 }
165
166 static gint
167 dissect_enttec_poll(tvbuff_t *tvb, guint offset, proto_tree *tree)
168 {
169         proto_tree_add_item(tree, hf_enttec_poll_type, tvb,
170                                         offset, 1, FALSE);
171         offset += 1;
172
173         return offset;
174 }
175
176 static gint
177 dissect_enttec_ack(tvbuff_t *tvb _U_, guint offset, proto_tree *tree _U_)
178 {
179
180         return offset;
181 }
182
183 static gint
184 dissect_enttec_dmx_data(tvbuff_t *tvb, guint offset, proto_tree *tree)
185 {
186         const char* chan_format[] = {
187                 "%2u ",
188                 "%02x ",
189                 "%3u "
190         };
191         const char* string_format[] = {
192                 "%03x: %s",
193                 "%3u: %s"
194         };
195
196         static guint8 dmx_data[512];
197         static guint16 dmx_data_offset[513]; /* 1 extra for last offset */
198         emem_strbuf_t *dmx_epstr;
199
200         proto_tree *hi,*si;
201         proto_item *item;
202         guint16 length,r,c,row_count;
203         guint8 v,type,count;
204         guint16 ci,ui,i,start_offset,end_offset;
205
206         proto_tree_add_item(tree, hf_enttec_dmx_data_universe, tvb,
207                                         offset, 1, FALSE);
208         offset += 1;
209
210         proto_tree_add_item(tree, hf_enttec_dmx_data_start_code, tvb,
211                                         offset, 1, FALSE);
212         offset += 1;
213
214         type = tvb_get_guint8(tvb, offset);     
215         proto_tree_add_item(tree, hf_enttec_dmx_data_type, tvb,
216                                         offset, 1, FALSE);
217         offset += 1;
218
219         length = tvb_get_ntohs(tvb, offset);
220         proto_tree_add_item(tree, hf_enttec_dmx_data_size, tvb,
221                                         offset, 2, FALSE);
222         offset += 2;
223
224         if (length > 512)
225                 length = 512;
226
227         if (type == ENTTEC_DATA_TYPE_RLE) {
228                 /* uncompres the DMX data */
229                 ui = 0;
230                 ci = 0;
231                 while (ci < length) {
232                         v = tvb_get_guint8(tvb, offset+ci);
233                         if (v == 0xFE) {
234                                 ci++;
235                                 count = tvb_get_guint8(tvb, offset+ci);
236                                 ci++;
237                                 v = tvb_get_guint8(tvb, offset+ci);
238                                 ci++;
239                                 for (i=0;i < count;i++) {
240                                         dmx_data[ui] = v;
241                                         dmx_data_offset[ui] = ci-3;
242                                         ui++;
243                                 }
244                         } else if (v == 0xFD) {
245                                 ci++;
246                                 v = tvb_get_guint8(tvb, offset+ci);                             
247                                 dmx_data[ui] = v;
248                                 dmx_data_offset[ui] = ci;
249                                 ci++;
250                                 ui++;
251                         } else {
252                                 dmx_data[ui] = v;
253                                 dmx_data_offset[ui] = ci;
254                                 ui++;
255                                 ci++;
256                         }
257                 }
258                 dmx_data_offset[ui] = ci;
259         } else {
260                 for (ui=0; ui < length;ui++) {
261                         dmx_data[ui] =  tvb_get_guint8(tvb, offset+ui);
262                         dmx_data_offset[ui] = ui;
263                 }
264                 dmx_data_offset[ui] = ui;
265         } 
266
267
268         if (type == ENTTEC_DATA_TYPE_DMX || type == ENTTEC_DATA_TYPE_RLE) {
269                 hi = proto_tree_add_item(tree,
270                                         hf_enttec_dmx_data_data,
271                                         tvb,
272                                         offset,
273                                         length,
274                                         FALSE);
275
276                 si = proto_item_add_subtree(hi, ett_enttec);
277                         
278                 row_count = (ui/global_disp_col_count) + ((ui%global_disp_col_count) == 0 ? 0 : 1);
279                 dmx_epstr = ep_strbuf_new_label(NULL);
280                 for (r=0; r < row_count;r++) {
281                         for (c=0;(c < global_disp_col_count) && (((r*global_disp_col_count)+c) < ui);c++) {
282                                 if ((c % (global_disp_col_count/2)) == 0) {
283                                         ep_strbuf_append_c(dmx_epstr, ' ');
284                                 }
285                                 v = dmx_data[(r*global_disp_col_count)+c];
286                                 if (global_disp_chan_val_type == 0) {
287                                         v = (v * 100) / 255;
288                                         if (v == 100) {
289                                                 ep_strbuf_append(dmx_epstr, "FL ");
290                                         } else {
291                                                 ep_strbuf_append_printf(dmx_epstr, chan_format[global_disp_chan_val_type], v);
292                                         }
293                                 } else {
294                                         ep_strbuf_append_printf(dmx_epstr, chan_format[global_disp_chan_val_type], v);
295                                 }
296                         }
297
298                         start_offset = dmx_data_offset[(r*global_disp_col_count)];
299                         end_offset = dmx_data_offset[(r*global_disp_col_count)+c];              
300
301                         proto_tree_add_none_format(si,hf_enttec_dmx_data_dmx_data, tvb,
302                                                 offset+start_offset, 
303                                                 end_offset-start_offset,
304                                                 string_format[global_disp_chan_nr_type], (r*global_disp_col_count)+1, dmx_epstr->str);
305                         ep_strbuf_truncate(dmx_epstr, 0);
306                 }
307                 
308                 item = proto_tree_add_item(si, hf_enttec_dmx_data_data_filter, tvb,
309                                 offset, length, FALSE );
310                 PROTO_ITEM_SET_HIDDEN(item);
311                 
312                 offset += length;
313         } else if (type == ENTTEC_DATA_TYPE_CHAN_VAL) {
314                 proto_tree_add_item(tree, hf_enttec_dmx_data_data_filter, tvb,
315                                         offset, length, FALSE);
316                 offset += length;
317         } else {
318                 proto_tree_add_item(tree, hf_enttec_dmx_data_data_filter, tvb,
319                                         offset, length, FALSE);
320                 offset += length;
321         }               
322
323         
324                 
325         return offset;
326 }
327
328 static gint
329 dissect_enttec_config(tvbuff_t *tvb _U_, guint offset, proto_tree *tree _U_)
330 {
331
332         return offset;
333 }
334
335 static gint
336 dissect_enttec_reset(tvbuff_t *tvb _U_, guint offset, proto_tree *tree _U_)
337 {
338
339         return offset;
340 }
341
342 static void
343 dissect_enttec(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) 
344 {
345         gint offset = 0;
346         guint32 head = 0;
347         proto_tree *ti,*enttec_tree=NULL;
348
349         /* Set the protocol column */
350         col_set_str(pinfo->cinfo, COL_PROTOCOL, "ENTTEC");
351
352         head = tvb_get_ntohl(tvb, offset);
353
354         /* Clear out stuff in the info column */
355         if (check_col(pinfo->cinfo,COL_INFO)) {
356                 col_add_fstr(pinfo->cinfo, COL_INFO, "%s",
357                                 val_to_str(head, enttec_head_vals, "Unknown (0x%08x)"));
358         }
359
360         if (tree) {
361                 ti = proto_tree_add_item(tree, proto_enttec, tvb, offset, -1, FALSE);
362                 enttec_tree = proto_item_add_subtree(ti, ett_enttec);
363         }
364
365         if (enttec_tree) {
366                 proto_tree_add_item(enttec_tree, hf_enttec_head, tvb,
367                                         offset, 4, FALSE );
368                 offset += 4;
369
370                 switch (head) {
371                         case ENTTEC_HEAD_ESPR:
372                                 offset = dissect_enttec_poll_reply( tvb, offset, enttec_tree);
373                                 break;
374
375                         case ENTTEC_HEAD_ESPP:
376                                 offset = dissect_enttec_poll( tvb, offset, enttec_tree);
377                                 break;
378
379                         case ENTTEC_HEAD_ESAP:
380                                 offset = dissect_enttec_ack( tvb, offset, enttec_tree);
381                                 break;
382
383                         case ENTTEC_HEAD_ESDD:
384                                 offset = dissect_enttec_dmx_data( tvb, offset, enttec_tree);
385                                 break;
386
387                         case ENTTEC_HEAD_ESNC:
388                                 offset = dissect_enttec_config( tvb, offset, enttec_tree);
389                                 break;
390
391                         case ENTTEC_HEAD_ESZZ:
392                                 offset = dissect_enttec_reset( tvb, offset, enttec_tree);
393                                 break;
394                 }
395
396         }
397 }
398
399 void
400 proto_register_enttec(void) 
401 {
402         static hf_register_info hf[] = {
403                 /* General */
404                 { &hf_enttec_head,
405                         { "Head", "enttec.head",
406                           FT_UINT32, BASE_HEX, VALS(enttec_head_vals), 0x0,
407                           NULL, HFILL } },
408                 { &hf_enttec_poll_reply_mac,
409                         { "MAC", "enttec.poll_reply.mac",
410                           FT_ETHER, BASE_NONE, NULL, 0x0,
411                           NULL, HFILL } },
412                 { &hf_enttec_poll_reply_node_type,
413                         { "Node Type", "enttec.poll_reply.node_type",
414                           FT_UINT16, BASE_HEX, NULL, 0x0,
415                           NULL, HFILL } },
416                 { &hf_enttec_poll_reply_version,
417                         { "Version", "enttec.poll_reply.version",
418                           FT_UINT8, BASE_DEC, NULL, 0x0,
419                           NULL, HFILL } },
420                 { &hf_enttec_poll_reply_switch,
421                         { "Switch settings", "enttec.poll_reply.switch_settings",
422                           FT_UINT8, BASE_HEX, NULL, 0x0,
423                           NULL, HFILL } },
424                 { &hf_enttec_poll_reply_name,
425                         { "Name", "enttec.poll_reply.name",
426                           FT_STRING, BASE_NONE, NULL, 0x0,
427                           NULL, HFILL } },
428                 { &hf_enttec_poll_reply_option,
429                         { "Option Field", "enttec.poll_reply.option_field",
430                           FT_UINT8, BASE_HEX, NULL, 0x0,
431                           NULL, HFILL } },
432                 { &hf_enttec_poll_reply_tos,
433                         { "TOS", "enttec.poll_reply.tos",
434                           FT_UINT8, BASE_HEX, NULL, 0x0,
435                           NULL, HFILL } },
436                 { &hf_enttec_poll_reply_ttl,
437                         { "TTL", "enttec.poll_reply.ttl",
438                           FT_UINT8, BASE_DEC, NULL, 0x0,
439                           NULL, HFILL } },
440                 { &hf_enttec_dmx_data_universe,
441                         { "Universe", "enttec.dmx_data.universe",
442                           FT_UINT8, BASE_DEC, NULL, 0x0,
443                           NULL, HFILL } },
444                 { &hf_enttec_dmx_data_start_code,
445                         { "Start Code", "enttec.dmx_data.start_code",
446                           FT_UINT8, BASE_DEC, NULL, 0x0,
447                           NULL, HFILL } },
448                 { &hf_enttec_dmx_data_type,
449                         { "Data Type", "enttec.dmx_data.type",
450                           FT_UINT8, BASE_HEX, VALS(enttec_data_type_vals), 0x0,
451                           NULL, HFILL } },
452                 { &hf_enttec_dmx_data_size,
453                         { "Data Size", "enttec.dmx_data.size",
454                           FT_UINT16, BASE_DEC, NULL, 0x0,
455                           NULL, HFILL } },
456                 { &hf_enttec_dmx_data_data,
457                         { "DMX Data", "enttec.dmx_data.data",
458                           FT_NONE, BASE_NONE, NULL, 0x0,
459                           NULL, HFILL } },
460                 { &hf_enttec_dmx_data_data_filter,
461                         { "DMX Data", "enttec.dmx_data.data_filter",
462                           FT_BYTES, BASE_NONE, NULL, 0x0,
463                           NULL, HFILL } },
464                 { &hf_enttec_dmx_data_dmx_data,
465                         { "DMX Data", "enttec.dmx_data.dmx_data",
466                           FT_NONE, BASE_NONE, NULL, 0x0,
467                           NULL, HFILL } },
468                 { &hf_enttec_poll_type,
469                         { "Reply Type", "enttec.poll.reply_type",
470                           FT_UINT8, BASE_DEC, NULL, 0x0,
471                           NULL, HFILL } }
472         };
473
474         static gint *ett[] = {
475                 &ett_enttec,
476         };
477
478         module_t *enttec_module;
479
480         static enum_val_t disp_chan_val_types[] = {
481                 { "pro", "Percent", 0 },
482                 { "hex", "Hexadecimal", 1 },
483                 { "dec", "Decimal", 2 },
484                 { NULL, NULL, 0 }
485         };
486
487         static enum_val_t disp_chan_nr_types[] = {
488                 { "hex", "Hexadecimal", 0 },
489                 { "dec", "Decimal", 1 },
490                 { NULL, NULL, 0 }
491         };
492
493         static enum_val_t col_count[] = {
494                 { "6", "6", 6 },
495                 { "10", "10", 10 },
496                 { "12", "12", 12 },
497                 { "16", "16", 16 },
498                 { "24", "24", 24 },
499                 { NULL, NULL, 0 }
500         };
501
502         proto_enttec = proto_register_protocol("ENTTEC", "ENTTEC","enttec");
503         proto_register_field_array(proto_enttec,hf,array_length(hf));
504         proto_register_subtree_array(ett,array_length(ett));
505
506         enttec_module = prefs_register_protocol(proto_enttec,
507                                                 proto_reg_handoff_enttec);
508         prefs_register_uint_preference(enttec_module, "udp_port",
509                                         "ENTTEC UDP Port",
510                                         "The UDP port on which ENTTEC packets will be sent",
511                                         10,&global_udp_port_enttec);
512
513         prefs_register_uint_preference(enttec_module, "tcp_port",
514                                         "ENTTEC TCP Port",
515                                         "The TCP port on which ENTTEC packets will be sent",
516                                         10,&global_tcp_port_enttec);
517
518         prefs_register_enum_preference(enttec_module, "dmx_disp_chan_val_type",
519                                 "DMX Display channel value type",
520                                 "The way DMX values are displayed",
521                                 &global_disp_chan_val_type,
522                                 disp_chan_val_types, FALSE);
523
524         prefs_register_enum_preference(enttec_module, "dmx_disp_chan_nr_type",
525                                 "DMX Display channel nr. type",
526                                 "The way DMX channel numbers are displayed",
527                                 &global_disp_chan_nr_type,
528                                 disp_chan_nr_types, FALSE);
529
530         prefs_register_enum_preference(enttec_module, "dmx_disp_col_count",
531                                 "DMX Display Column Count",
532                                 "The number of columns for the DMX display",
533                                 &global_disp_col_count,
534                                 col_count, FALSE);
535 }
536
537 /* The registration hand-off routing */
538 void
539 proto_reg_handoff_enttec(void) {
540         static gboolean enttec_initialized = FALSE;
541         static dissector_handle_t enttec_handle;
542         static guint udp_port_enttec;
543         static guint tcp_port_enttec;
544
545         if(!enttec_initialized) {
546                 enttec_handle = create_dissector_handle(dissect_enttec,proto_enttec);
547                 enttec_initialized = TRUE;
548         } else {
549                 dissector_delete("udp.port",udp_port_enttec,enttec_handle);
550                 dissector_delete("tcp.port",tcp_port_enttec,enttec_handle);
551         }
552
553         udp_port_enttec = global_udp_port_enttec;
554         tcp_port_enttec = global_tcp_port_enttec;  
555
556         dissector_add("udp.port",global_udp_port_enttec,enttec_handle);
557         dissector_add("tcp.port",global_tcp_port_enttec,enttec_handle);
558 }