Update Free Software Foundation address.
[metze/wireshark/wip.git] / epan / dissectors / packet-slimp3.c
1 /* packet-slimp3.c
2  * Routines for SliMP3 protocol dissection
3  *
4  * Ashok Narayanan <ashokn@cisco.com>
5  *
6  * Adds support for the data packet protocol for the SliMP3
7  * See www.slimdevices.com for details.
8  *
9  * $Id$
10  *
11  * Wireshark - Network traffic analyzer
12  * By Gerald Combs <gerald@wireshark.org>
13  * Copyright 1998 Gerald Combs
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28  */
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <glib.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <epan/packet.h>
38
39 #include "isprint.h"
40
41 static int proto_slimp3 = -1;
42 static int hf_slimp3_opcode = -1;
43 static int hf_slimp3_ir = -1;
44 static int hf_slimp3_display = -1;
45 static int hf_slimp3_control = -1;
46 static int hf_slimp3_hello = -1;
47 static int hf_slimp3_i2c = -1;
48 static int hf_slimp3_data_request = -1;
49 static int hf_slimp3_data = -1;
50 static int hf_slimp3_discover_request = -1;
51 static int hf_slimp3_discover_response = -1;
52 static int hf_slimp3_data_ack = -1;
53
54 static gint ett_slimp3 = -1;
55
56 #define UDP_PORT_SLIMP3_V1    1069
57 #define UDP_PORT_SLIMP3_V2    3483
58
59 #define SLIMP3_IR       'i'
60 #define SLIMP3_CONTROL  's'
61 #define SLIMP3_HELLO    'h'
62 #define SLIMP3_DATA     'm'
63 #define SLIMP3_DATA_REQ 'r'
64 #define SLIMP3_DISPLAY  'l'
65 #define SLIMP3_I2C      '2'
66 #define SLIMP3_DISC_REQ 'd'
67 #define SLIMP3_DISC_RSP 'D'
68 #define SLIMP3_DATA_ACK 'a'
69
70 static const value_string slimp3_opcode_vals[] = {
71   { SLIMP3_IR,       "Infrared Remote Code" },
72   { SLIMP3_CONTROL,  "Stream Control" },
73   { SLIMP3_DATA,     "MPEG Data" },
74   { SLIMP3_DATA_REQ, "Data Request" },
75   { SLIMP3_HELLO,    "Hello" },
76   { SLIMP3_DISPLAY,  "Display" },
77   { SLIMP3_I2C,      "I2C" },
78   { SLIMP3_DISC_REQ, "Discovery Request" },
79   { SLIMP3_DISC_RSP, "Discovery Response" },
80   { SLIMP3_DATA_ACK, "Ack" },
81   { 0,               NULL }
82 };
83
84 /* IR remote control types */
85 static const value_string slimp3_ir_types[] = {
86     { 0x02, "SLIMP3" },
87     { 0xff, "JVC DVD Player" },
88
89     { 0, NULL }
90 };
91
92 /* IR codes for the custom SLIMP3 remote control */
93 static const value_string slimp3_ir_codes_slimp3[] = {
94     { 0x76899867, "0" },
95     { 0x7689f00f, "1" },
96     { 0x768908f7, "2" },
97     { 0x76898877, "3" },
98     { 0x768948b7, "4" },
99     { 0x7689c837, "5" },
100     { 0x768928d7, "6" },
101     { 0x7689a857, "7" },
102     { 0x76896897, "8" },
103     { 0x7689e817, "9" },
104     { 0x7689b04f, "arrow_down" },
105     { 0x7689906f, "arrow_left" },
106     { 0x7689d02f, "arrow_right" },
107     { 0x7689e01f, "arrow_up" },
108     { 0x768900ff, "voldown" },
109     { 0x7689807f, "volup" },
110     { 0x768940bf, "power" },
111     { 0x7689c03f, "rew" },
112     { 0x768920df, "pause" },
113     { 0x7689a05f, "fwd" },
114     { 0x7689609f, "add" },
115     { 0x768910ef, "play" },
116     { 0x768958a7, "search" },
117     { 0x7689d827, "shuffle" },
118     { 0x768938c7, "repeat" },
119     { 0x7689b847, "sleep" },
120     { 0x76897887, "now_playing" },
121     { 0x7689f807, "size" },
122     { 0x768904fb, "brightness" },
123
124     { 0,      NULL }
125 };
126
127 /* IR codes for the JVC remote control */
128 static const value_string slimp3_ir_codes_jvc[] = {
129     { 0xf786, "One" },
130     { 0xf746, "Two" },
131     { 0xf7c6, "Three" },
132     { 0xf726, "Four" },
133     { 0xf7a6, "Five" },
134     { 0xf766, "Six" },
135     { 0xf7e6, "Seven" },
136     { 0xf716, "Eight" },
137     { 0xf796, "Nine" },
138     { 0xf776, "Ten" },
139
140     { 0xf7f6, "Picture-In-Picture" },
141     /* { 0xf7XX, "Enter" }, */
142     { 0xf70e, "Back" },
143     { 0xf732, "Play" },
144     { 0xf76e, "Forward" },
145     { 0xf743, "Record" },
146     { 0xf7c2, "Stop" },
147     { 0xf7b2, "Pause" },
148     /* { 0xf7XX, "TV/Video" }, */
149     { 0xf703, "Display" },
150     { 0xf7b3, "Sleep" },
151     { 0xf7b6, "Guide" },
152     { 0xf70b, "Up" },
153     { 0xf74b, "Left" },
154     { 0xf7cb, "Right" },
155     { 0xf78b, "Down" },
156     { 0xf783, "Menu" },
157     { 0xf72b, "OK" },
158     { 0xf778, "Volume Up" },
159     { 0xf7f8, "Volume Down" },
160     { 0xf70d, "Channel Up" },
161     { 0xf78d, "Channel Down" },
162     /* { 0xf7XX, "Mute" },  */
163     { 0xf7ab, "Recall" },
164     { 0xf702, "Power" },
165
166     { 0,      NULL }
167 };
168
169
170 static const value_string slimp3_display_commands[] = {
171     {  0x1, "Clear Display"},
172     {  0x2, "Cursor to 1st Line Home"},
173
174     {  0x4, "Mode: Decrement Address, Shift Cursor"},
175     {  0x5, "Mode: Decrement Address, Shift Display"},
176     {  0x6, "Mode: Increment Address, Shift Cursor"},
177     {  0x7, "Mode: Increment Address, Shift Display"},
178
179     {  0x8, "Display Off"},
180     {  0xd, "Display On, With Blinking"},
181     {  0xe, "Display On, With Cursor"},
182     {  0xf, "Display On, With Cursor And Blinking"},
183
184     { 0x10, "Move Cursor Left"},
185     { 0x14, "Move Cursor Right"},
186     { 0x18, "Shift Display Left"},
187     { 0x1b, "Shift Display Right"},
188
189     { 0x30, "Set (8-bit)"},
190     { 0x20, "Set (4-bit)"},
191
192     { 0xa0, "Cursor to Top Right"},
193     { 0xc0, "Cursor to 2nd Line Home"},
194
195     {    0, NULL},
196 };
197
198 static const value_string slimp3_display_fset8[] = {
199     { 0x0, "Brightness 100%"},
200     { 0x1, "Brightness 75%"},
201     { 0x2, "Brightness 50%"},
202     { 0x3, "Brightness 25%"},
203
204     {   0, NULL },
205 };
206
207 static const value_string slimp3_stream_control[] = {
208     { 1, "Reset buffer, Start New Stream"},
209     { 2, "Pause Playback"},
210     { 4, "Resume Playback"},
211     {   0, NULL },
212 };
213
214
215 static const value_string slimp3_mpg_control[] = {
216     { 0, "Go"},           /* Run the decoder */
217     { 1, "Stop"},         /* Halt decoder but don't reset rptr */
218     { 3, "Reset"},        /* Halt decoder and reset rptr */
219
220     { 0, NULL }
221 };
222
223 #define MAX_LCD_STR_LEN 128
224 static int
225 dissect_slimp3(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
226 {
227     const char          *opcode_str;
228     proto_tree          *slimp3_tree = NULL;
229     proto_item          *ti = NULL, *hidden_item;
230     gint                i1;
231     gint                offset = 0;
232     guint16             opcode;
233     guchar              lcd_char;
234     char                lcd_str[MAX_LCD_STR_LEN + 1];
235     int                 to_server = FALSE;
236     int                 old_protocol = FALSE;
237     address             tmp_addr;
238     gboolean            in_str;
239     int                 lcd_strlen;
240
241     /*
242      * If it doesn't begin with a known opcode, reject it, so that
243      * traffic that happens to be do or from one of our ports
244      * doesn't get misidentified as SliMP3 traffic.
245      */
246     if (!tvb_bytes_exist(tvb, offset, 1))
247         return 0;       /* not even an opcode */
248     opcode = tvb_get_guint8(tvb, offset);
249     opcode_str = match_strval(opcode, slimp3_opcode_vals);
250     if (opcode_str == NULL)
251         return 0;
252
253     col_set_str(pinfo->cinfo, COL_PROTOCOL, "SliMP3");
254
255     if (check_col(pinfo->cinfo, COL_INFO)) {
256
257         col_add_str(pinfo->cinfo, COL_INFO, opcode_str);
258
259     }
260
261     if (tree) {
262
263         ti = proto_tree_add_item(tree, proto_slimp3, tvb, offset, -1, ENC_NA);
264         slimp3_tree = proto_item_add_subtree(ti, ett_slimp3);
265
266         proto_tree_add_uint(slimp3_tree, hf_slimp3_opcode, tvb,
267                             offset, 1, opcode);
268     }
269
270     /* The new protocol (v1.3 and later) uses an IANA-assigned port number.
271     * It usually uses the same number for both sizes of the conversation, so
272     * the port numbers can't always be used to determine client and server.
273     * The new protocol places the clients MAC address in the packet, so that
274     * is used to identify packets originating at the client.
275     */
276     if ((pinfo->destport == UDP_PORT_SLIMP3_V2) && (pinfo->srcport == UDP_PORT_SLIMP3_V2)) {
277         SET_ADDRESS(&tmp_addr, AT_ETHER, 6, tvb_get_ptr(tvb, offset+12, 6));
278         to_server = ADDRESSES_EQUAL(&tmp_addr, &pinfo->dl_src);
279     }
280     else if (pinfo->destport == UDP_PORT_SLIMP3_V2) {
281         to_server = TRUE;
282     }
283     else if (pinfo->srcport == UDP_PORT_SLIMP3_V2) {
284         to_server = FALSE;
285     }
286     if (pinfo->destport == UDP_PORT_SLIMP3_V1) {
287         to_server = TRUE;
288         old_protocol = TRUE;
289     }
290     else if (pinfo->srcport == UDP_PORT_SLIMP3_V1) {
291         to_server = FALSE;
292         old_protocol = TRUE;
293     }
294
295     switch (opcode) {
296
297     case SLIMP3_IR:
298         /* IR code
299         *
300         * [0]        'i' as in "IR"
301         * [1]        0x00
302         * [2..5]     player's time since startup in ticks @625 KHz
303         * [6]        IR code id, ff=JVC, 02=SLIMP3
304         * [7]        number of meaningful bits - 16 for JVC, 32 for SLIMP3
305         * [8..11]    the 32-bit IR code
306         * [12..17]   reserved
307         */
308         if (tree) {
309             hidden_item = proto_tree_add_item(slimp3_tree, hf_slimp3_ir, tvb, offset+8, 4, ENC_BIG_ENDIAN);
310             PROTO_ITEM_SET_HIDDEN(hidden_item);
311
312             i1 = tvb_get_ntohl(tvb, offset+2);
313             proto_tree_add_text(slimp3_tree, tvb, offset+2, 4, "Uptime: %u sec (%u ticks)",
314                                 i1/625000, i1);
315             i1 = tvb_get_guint8(tvb, offset+6);
316             proto_tree_add_text(slimp3_tree, tvb, offset+6, 1, "Code identifier: 0x%0x: %s",
317                                 i1, val_to_str(i1, slimp3_ir_types, "Unknown"));
318             proto_tree_add_text(slimp3_tree, tvb, offset+7, 1, "Code bits: %d",
319                                 tvb_get_guint8(tvb, offset+7));
320
321             i1 = tvb_get_ntohl(tvb, offset+8);
322             /* Check the code to figure out which remote is being used. */
323             if (tvb_get_guint8(tvb, offset+6) == 0x02 &&
324                 tvb_get_guint8(tvb, offset+7) == 32) {
325                 /* This is the custom SLIMP3 remote. */
326                 proto_tree_add_text(slimp3_tree, tvb, offset+8, 4,
327                                     "Infrared Code: %s: 0x%0x",
328                                     val_to_str(i1, slimp3_ir_codes_slimp3, "Unknown"), i1);
329             }
330             else if (tvb_get_guint8(tvb, offset+6) == 0xff &&
331                      tvb_get_guint8(tvb, offset+7) == 16) {
332                 /* This is a JVC DVD player remote */
333                 proto_tree_add_text(slimp3_tree, tvb, offset+8, 4,
334                                     "Infrared Code: %s: 0x%0x",
335                                     val_to_str(i1, slimp3_ir_codes_jvc, "Unknown"), i1);
336             } else {
337                 /* Unknown code; just write it */
338                 proto_tree_add_text(slimp3_tree, tvb, offset+8, 4, "Infrared Code: 0x%0x", i1);
339             }
340         }
341         if (check_col(pinfo->cinfo, COL_INFO)) {
342             i1 = tvb_get_ntohl(tvb, offset+8);
343             if (tvb_get_guint8(tvb, offset+6) == 0x02 &&
344                 tvb_get_guint8(tvb, offset+7) == 32) {
345                 col_append_fstr(pinfo->cinfo, COL_INFO, ", SLIMP3: %s",
346                                 val_to_str(i1, slimp3_ir_codes_slimp3, "Unknown (0x%0x)"));
347             }
348             else if (tvb_get_guint8(tvb, offset+6) == 0xff &&
349                      tvb_get_guint8(tvb, offset+7) == 16) {
350                 col_append_fstr(pinfo->cinfo, COL_INFO, ", JVC: %s",
351                                 val_to_str(i1, slimp3_ir_codes_jvc, "Unknown (0x%0x)"));
352             } else {
353                 /* Unknown code; just write it */
354                 col_append_fstr(pinfo->cinfo, COL_INFO, ", 0x%0x", i1);
355             }
356         }
357         break;
358
359     case SLIMP3_DISPLAY:
360         if (tree) {
361
362             hidden_item = proto_tree_add_item(slimp3_tree, hf_slimp3_display,
363                                        tvb, offset, 1, ENC_BIG_ENDIAN);
364             PROTO_ITEM_SET_HIDDEN(hidden_item);
365
366             /* Loop through the commands */
367             i1 = 18;
368             in_str = FALSE;
369             lcd_strlen = 0;
370             while (i1 < tvb_reported_length_remaining(tvb, offset)) {
371                 switch(tvb_get_guint8(tvb, offset + i1)) {
372                 case 0:
373                     in_str = FALSE;
374                     lcd_strlen = 0;
375                     proto_tree_add_text(slimp3_tree, tvb, offset + i1, 2,
376                                         "Delay (%d ms)", tvb_get_guint8(tvb, offset + i1 + 1));
377                     i1 += 2;
378                     break;
379                 case 3:
380                     lcd_char = tvb_get_guint8(tvb, offset + i1 + 1);
381                     if (!isprint(lcd_char)) lcd_char = '.';
382                     if (ti && in_str) {
383                         lcd_strlen += 2;
384                         proto_item_append_text(ti, "%c", lcd_char);
385                         proto_item_set_len(ti, lcd_strlen);
386                     } else {
387                         ti = proto_tree_add_text(slimp3_tree, tvb, offset + i1, 2,
388                                                  "String: %c", lcd_char);
389                         in_str = TRUE;
390                         lcd_strlen = 2;
391                     }
392                     i1 += 2;
393                     break;
394
395                 case 2:
396                     in_str = FALSE;
397                     lcd_strlen = 0;
398                     ti = proto_tree_add_text(slimp3_tree, tvb, offset + i1, 2,
399                                              "Command: %s",
400                                              val_to_str(tvb_get_guint8(tvb, offset + i1 + 1),
401                                                         slimp3_display_commands,
402                                                         "Unknown (0x%0x)"));
403                     if ((tvb_get_guint8(tvb, offset + i1 + 1) & 0xf0) == 0x30) {
404                         proto_item_append_text(ti, ": %s",
405                                                val_to_str(tvb_get_guint8(tvb, offset + i1 + 2),
406                                                           slimp3_display_fset8,
407                                                           "Unknown (0x%0x)"));
408                         i1 += 2;
409                     }
410                     i1 += 2;
411                     break;
412
413                 default:
414                     proto_tree_add_text(slimp3_tree, tvb, offset + i1, 2,
415                                         "Unknown 0x%0x, 0x%0x",
416                                         tvb_get_guint8(tvb, offset + i1),
417                                         tvb_get_guint8(tvb, offset + i1 + 1));
418                     i1 += 2;
419                     break;
420                 }
421             }
422         }
423
424         if (check_col(pinfo->cinfo, COL_INFO)) {
425             i1 = 18;
426             lcd_strlen = 0;
427             while (tvb_offset_exists(tvb, offset + i1) &&
428                     lcd_strlen < MAX_LCD_STR_LEN) {
429                 switch (tvb_get_guint8(tvb, offset + i1)) {
430
431                 case 0:
432                     lcd_str[lcd_strlen++] = '.';
433                     break;
434
435                 case 2:
436                     lcd_str[lcd_strlen++] = '|';
437                     if (tvb_offset_exists(tvb, offset + i1 + 1) &&
438                           (tvb_get_guint8(tvb, offset + i1 + 1) & 0xf0) == 0x30)
439                         i1 += 2;
440                     break;
441
442                 case 3:
443                     if (tvb_offset_exists(tvb, offset + i1 + 1)) {
444                         if (lcd_strlen < 1 ||
445                               lcd_str[lcd_strlen-1] != ' ' ||
446                               tvb_get_guint8(tvb, offset + i1 + 1) != ' ') {
447                             lcd_char = tvb_get_guint8(tvb, offset + i1 + 1);
448                             lcd_str[lcd_strlen++] = isprint(lcd_char) ? lcd_char : '.';
449                         }
450                     }
451                 }
452                 i1 += 2;
453             }
454             lcd_str[lcd_strlen] = '\0';
455             if (lcd_strlen > 0)
456                 col_append_fstr(pinfo->cinfo, COL_INFO, ", %s", lcd_str);
457         }
458
459         break;
460
461     case SLIMP3_CONTROL:
462         if (tree) {
463             hidden_item = proto_tree_add_item(slimp3_tree, hf_slimp3_control,
464                                        tvb, offset+1, 1, ENC_BIG_ENDIAN);
465             PROTO_ITEM_SET_HIDDEN(hidden_item);
466             proto_tree_add_text(slimp3_tree, tvb, offset+1, 1, "Command: %s",
467                                 val_to_str(tvb_get_guint8(tvb, offset+1),
468                                            slimp3_stream_control, "Unknown (0x%0x)"));
469         }
470
471         if (check_col(pinfo->cinfo, COL_INFO)) {
472             col_append_fstr(pinfo->cinfo, COL_INFO, ", %s",
473                             val_to_str(tvb_get_guint8(tvb, offset+1),
474                                        slimp3_stream_control, "Unknown (0x%0x)"));
475         }
476         break;
477
478     case SLIMP3_HELLO:
479         if (tree) {
480             hidden_item = proto_tree_add_item(slimp3_tree, hf_slimp3_hello,
481                                        tvb, offset+1, 1, ENC_BIG_ENDIAN);
482             PROTO_ITEM_SET_HIDDEN(hidden_item);
483             if (to_server) {
484                 guint8 fw_ver = 0;
485                 /* Hello response; client->server */
486                 proto_tree_add_text(slimp3_tree, tvb, offset, 1, "Hello Response (Client --> Server)");
487                 proto_tree_add_text(slimp3_tree, tvb, offset+1, 1, "Device ID: %d",
488                                     tvb_get_guint8(tvb, offset+1));
489                 fw_ver = tvb_get_guint8(tvb, offset+2);
490                 proto_tree_add_text(slimp3_tree, tvb, offset+2, 1, "Firmware Revision: %d.%d (0x%0x)",
491                                     fw_ver>>4, fw_ver & 0xf, fw_ver);
492             } else {
493                 /* Hello request; server->client */
494                 proto_tree_add_text(slimp3_tree, tvb, offset, 1, "Hello Request (Server --> Client)");
495             }
496         }
497         break;
498
499     case SLIMP3_I2C:
500         if (tree) {
501             hidden_item = proto_tree_add_item(slimp3_tree, hf_slimp3_i2c,
502                                        tvb, offset, 1, ENC_BIG_ENDIAN);
503             PROTO_ITEM_SET_HIDDEN(hidden_item);
504             if (to_server) {
505                 /* Hello response; client->server */
506                 proto_tree_add_text(slimp3_tree, tvb, offset, -1,
507                                     "I2C Response (Client --> Server)");
508             } else {
509                 /* Hello request; server->client */
510                 proto_tree_add_text(slimp3_tree, tvb, offset, -1,
511                                     "I2C Request (Server --> Client)");
512             }
513         }
514
515         if (check_col(pinfo->cinfo, COL_INFO)) {
516             if (to_server) {
517                 col_append_str(pinfo->cinfo, COL_INFO, ", Response");
518             } else {
519                 col_append_str(pinfo->cinfo, COL_INFO, ", Request");
520             }
521         }
522         break;
523
524     case SLIMP3_DATA_REQ:
525         if (tree) {
526             hidden_item = proto_tree_add_item(slimp3_tree, hf_slimp3_data_request,
527                                        tvb, offset, 1, ENC_BIG_ENDIAN);
528             PROTO_ITEM_SET_HIDDEN(hidden_item);
529             proto_tree_add_text(slimp3_tree, tvb, offset+2, 2,
530                                 "Requested offset: %d bytes.",
531                                 tvb_get_ntohs(tvb, offset+2)*2);
532         }
533
534         if (check_col(pinfo->cinfo, COL_INFO)) {
535             col_append_fstr(pinfo->cinfo, COL_INFO, ", Offset: %d bytes",
536                             tvb_get_ntohs(tvb, offset+2)*2);
537         }
538         break;
539
540     case SLIMP3_DATA:
541         /* MPEG data (v1.3 and later)
542         *
543         *  [0]       'm'
544         *  [1..5]    reserved
545         *  [6..7]    Write pointer (in words)
546         *  [8..9]    reserved
547         *  [10..11]  Sequence number
548         *  [12..17]  reserved
549         *  [18..]    MPEG data
550         */
551         if (tree) {
552             hidden_item = proto_tree_add_item(slimp3_tree, hf_slimp3_data,
553                                        tvb, offset, 1, ENC_BIG_ENDIAN);
554             PROTO_ITEM_SET_HIDDEN(hidden_item);
555             if (old_protocol) {
556                 proto_tree_add_text(slimp3_tree, tvb, offset, -1,
557                                     "Length: %d bytes",
558                                     tvb_reported_length_remaining(tvb, offset+18));
559                 proto_tree_add_text(slimp3_tree, tvb, offset+2, 2,
560                                     "Buffer offset: %d bytes.",
561                                     tvb_get_ntohs(tvb, offset+2) * 2);
562             }
563             else {
564                 proto_tree_add_text(slimp3_tree, tvb, offset+1, 1, "Command: %s",
565                                     val_to_str(tvb_get_guint8(tvb, offset+1),
566                                                slimp3_mpg_control, "Unknown (0x%0x)"));
567                 proto_tree_add_text(slimp3_tree, tvb, offset, -1,
568                                     "Length: %d bytes",
569                                     tvb_reported_length_remaining(tvb, offset+18));
570                 proto_tree_add_text(slimp3_tree, tvb, offset+6, 2,
571                                     "Write Pointer: %d",
572                                     tvb_get_ntohs(tvb, offset+6) * 2);
573                 proto_tree_add_text(slimp3_tree, tvb, offset+10, 2,
574                                     "Sequence: %d",
575                                     tvb_get_ntohs(tvb, offset+10));
576             }
577         }
578
579         if (check_col(pinfo->cinfo, COL_INFO)) {
580             if (old_protocol) {
581                 col_append_fstr(pinfo->cinfo, COL_INFO,
582                                 ", Length: %d bytes, Offset: %d bytes.",
583                                 tvb_reported_length_remaining(tvb, offset+18),
584                                 tvb_get_ntohs(tvb, offset+2) * 2);
585             }
586             else {
587                 col_append_fstr(pinfo->cinfo, COL_INFO,
588                                 ", %s, %d bytes at %d, Sequence: %d",
589                                 val_to_str(tvb_get_guint8(tvb, offset+1),
590                                            slimp3_mpg_control, "Unknown (0x%0x)"),
591                                 tvb_reported_length_remaining(tvb, offset+18),
592                                 tvb_get_ntohs(tvb, offset+6) * 2,
593                                 tvb_get_ntohs(tvb, offset+10));
594             }
595         }
596         break;
597
598     case SLIMP3_DISC_REQ:
599         if (tree) {
600             guint8 fw_ver;
601             hidden_item = proto_tree_add_item(slimp3_tree, hf_slimp3_discover_request,
602                                        tvb, offset, 1, ENC_BIG_ENDIAN);
603             PROTO_ITEM_SET_HIDDEN(hidden_item);
604             proto_tree_add_text(slimp3_tree, tvb, offset+1, 1,
605                                 "Device ID: %d.", tvb_get_guint8(tvb, offset+1));
606             fw_ver = tvb_get_guint8(tvb, offset+2);
607             proto_tree_add_text(slimp3_tree, tvb, offset+2, 1, "Firmware Revision: %d.%d (0x%0x)",
608                                 fw_ver>>4, fw_ver & 0xf, fw_ver);
609         }
610
611         if (check_col(pinfo->cinfo, COL_INFO)) {
612             guint8 fw_ver = tvb_get_guint8(tvb, offset+2);
613             col_append_fstr(pinfo->cinfo, COL_INFO, ", Device ID: %d. Firmware: %d.%d",
614                             tvb_get_guint8(tvb, offset+1), fw_ver>>4, fw_ver & 0xf);
615         }
616         break;
617
618     case SLIMP3_DISC_RSP:
619         if (tree) {
620             hidden_item = proto_tree_add_item(slimp3_tree, hf_slimp3_discover_response,
621                                        tvb, offset, 1, ENC_BIG_ENDIAN);
622             PROTO_ITEM_SET_HIDDEN(hidden_item);
623             proto_tree_add_text(slimp3_tree, tvb, offset+2, 4,
624                                 "Server Address: %s.",
625                                 tvb_ip_to_str(tvb, offset+2));
626             proto_tree_add_text(slimp3_tree, tvb, offset+6, 2,
627                                 "Server Port: %d", tvb_get_ntohs(tvb, offset + 6));
628         }
629
630         if (check_col(pinfo->cinfo, COL_INFO)) {
631             col_append_fstr(pinfo->cinfo, COL_INFO, ", Server Address: %s. Server Port: %d",
632                             tvb_ip_to_str(tvb, offset+2),
633                             tvb_get_ntohs(tvb, offset + 6));
634         }
635         break;
636
637     case SLIMP3_DATA_ACK:
638         /* Acknowledge MPEG data
639         *
640         *  [0]       'a'
641         *  [1..5]
642         *  [6..7]    Write pointer (in words)
643         *  [8..9]    Read pointer (in words)
644         *  [10..11]  Sequence number
645         *  [12..17]  client MAC address (v1.3 and later)
646         */
647         if (tree) {
648             hidden_item = proto_tree_add_item(slimp3_tree, hf_slimp3_data_ack,
649                                        tvb, offset, 1, ENC_BIG_ENDIAN);
650             PROTO_ITEM_SET_HIDDEN(hidden_item);
651             proto_tree_add_text(slimp3_tree, tvb, offset+6, 2,
652                                 "Write Pointer: %d",
653                                 tvb_get_ntohs(tvb, offset+6) * 2);
654             proto_tree_add_text(slimp3_tree, tvb, offset+8, 2,
655                                 "Read Pointer: %d",
656                                 tvb_get_ntohs(tvb, offset+8) * 2);
657             proto_tree_add_text(slimp3_tree, tvb, offset+10, 2,
658                                 "Sequence: %d",
659                                 tvb_get_ntohs(tvb, offset+10));
660         }
661         if (check_col(pinfo->cinfo, COL_INFO)) {
662             col_append_fstr(pinfo->cinfo, COL_INFO, ", Sequence: %d",
663                             tvb_get_ntohs(tvb, offset+10));
664         }
665         break;
666
667     default:
668         if (tree) {
669             proto_tree_add_text(slimp3_tree, tvb, offset, -1,
670                                 "Data (%d bytes)", tvb_reported_length_remaining(tvb, offset));
671         }
672         break;
673
674     }
675     return tvb_length(tvb);
676 }
677
678 void
679 proto_register_slimp3(void)
680 {
681   static hf_register_info hf[] = {
682     { &hf_slimp3_opcode,
683       { "Opcode",             "slimp3.opcode",
684         FT_UINT8, BASE_DEC, VALS(slimp3_opcode_vals), 0x0,
685         "SLIMP3 message type", HFILL }},
686
687     { &hf_slimp3_ir,
688       { "Infrared",           "slimp3.ir",
689         FT_UINT32, BASE_HEX, NULL, 0x0,
690         "SLIMP3 Infrared command", HFILL }},
691
692     { &hf_slimp3_control,
693       { "Control Packet",   "slimp3.control",
694         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
695         "SLIMP3 control", HFILL }},
696
697     { &hf_slimp3_display,
698       { "Display",                    "slimp3.display",
699         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
700         "SLIMP3 display", HFILL }},
701
702     { &hf_slimp3_hello,
703       { "Hello",                      "slimp3.hello",
704         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
705         "SLIMP3 hello", HFILL }},
706
707     { &hf_slimp3_i2c,
708       { "I2C",                "slimp3.i2c",
709         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
710         "SLIMP3 I2C", HFILL }},
711
712     { &hf_slimp3_data,
713       { "Data",              "slimp3.data",
714         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
715         "SLIMP3 Data", HFILL }},
716
717     { &hf_slimp3_data_request,
718       { "Data Request",      "slimp3.data_req",
719         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
720         "SLIMP3 Data Request", HFILL }},
721
722     { &hf_slimp3_discover_request,
723       { "Discovery Request", "slimp3.discovery_req",
724         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
725         "SLIMP3 Discovery Request", HFILL }},
726
727     { &hf_slimp3_discover_response,
728       { "Discovery Response", "slimp3.discovery_response",
729         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
730         "SLIMP3 Discovery Response", HFILL }},
731
732     { &hf_slimp3_data_ack,
733       { "Data Ack",      "slimp3.data_ack",
734         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
735         "SLIMP3 Data Ack", HFILL }},
736
737   };
738   static gint *ett[] = {
739     &ett_slimp3,
740   };
741
742   proto_slimp3 = proto_register_protocol("SliMP3 Communication Protocol",
743                                        "SliMP3", "slimp3");
744   proto_register_field_array(proto_slimp3, hf, array_length(hf));
745   proto_register_subtree_array(ett, array_length(ett));
746 }
747
748 void
749 proto_reg_handoff_slimp3(void)
750 {
751   dissector_handle_t slimp3_handle;
752
753   slimp3_handle = new_create_dissector_handle(dissect_slimp3, proto_slimp3);
754   dissector_add_uint("udp.port", UDP_PORT_SLIMP3_V1, slimp3_handle);
755   dissector_add_uint("udp.port", UDP_PORT_SLIMP3_V2, slimp3_handle);
756 }