fix usage of "if(tree) {" to display the right things, even if no coloring rule is set
[obnox/wireshark/wip.git] / epan / dissectors / packet-rtp-events.c
1 /* packet-rtp-events.c
2  *
3  * Routines for RFC 2833 RTP Events dissection
4  * Copyright 2003, Kevin A. Noll <knoll[AT]poss.com>
5  *
6  * $Id$
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
10  * Copyright 1998 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 /*
28  * This dissector tries to dissect RTP Events.
29  */
30
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include <glib.h>
37 #include <epan/packet.h>
38 #include <epan/prefs.h>
39
40 #include <stdio.h>
41 #include <string.h>
42 #include "packet-rtp-events.h"
43
44 /*  rtp_event_payload_type_value is the value used globally
45         to set the appropriate payload type
46     saved_pt_value is a temporary place to save the value
47         so we can properly reinitialize when the settings
48         get changed
49 */
50 static guint rtp_event_payload_type_value = 101;
51 static guint saved_payload_type_value;
52
53
54 /* RTP Event Fields */
55
56 static int proto_rtp_events          = -1;
57
58 static int hf_rtp_events_event = -1; /* one byte */
59 static int hf_rtp_events_end = -1; /* one bit */
60 static int hf_rtp_events_reserved = -1; /* one bit */
61 static int hf_rtp_events_volume = -1; /* six bits */
62 static int hf_rtp_events_duration = -1; /* sixteen bits */
63
64
65 /* RTP Events fields defining a subtree */
66
67 static gint ett_rtp_events           = -1;
68
69 void
70 proto_reg_handoff_rtp_events(void);
71
72 static void
73 dissect_rtp_events( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree )
74 {
75         proto_item *ti            = NULL;
76         proto_tree *rtp_events_tree     = NULL;
77         unsigned int offset       = 0;
78
79         guint8      rtp_evt;
80         guint8      octet;
81
82         if ( check_col( pinfo->cinfo, COL_PROTOCOL ) )
83           {
84             col_set_str( pinfo->cinfo, COL_PROTOCOL, "RTP EVENT" );
85           }
86         if (check_col(pinfo->cinfo, COL_INFO))
87                 col_clear(pinfo->cinfo, COL_INFO);
88
89
90         /* Get event fields */
91
92         rtp_evt = tvb_get_guint8(tvb, offset );
93
94         if ( check_col( pinfo->cinfo, COL_INFO) )
95           {
96                 col_add_fstr( pinfo->cinfo, COL_INFO,
97                     "Payload type=RTP Event, %s",
98                     val_to_str( rtp_evt, rtp_event_type_values, "Unknown (%u)" ));
99
100
101           }
102
103         if ( tree )
104           {
105             ti = proto_tree_add_item( tree, proto_rtp_events, tvb, offset, -1, FALSE );
106             rtp_events_tree = proto_item_add_subtree( ti, ett_rtp_events );
107
108             proto_tree_add_uint ( rtp_events_tree, hf_rtp_events_event, tvb, offset, 1, rtp_evt);
109
110             octet = tvb_get_guint8(tvb, offset +1 );
111             proto_tree_add_boolean (rtp_events_tree, hf_rtp_events_end, tvb, offset+1, 1, octet);
112             proto_tree_add_boolean (rtp_events_tree, hf_rtp_events_reserved, tvb, offset+1, 1, octet);
113             proto_tree_add_uint ( rtp_events_tree, hf_rtp_events_volume, tvb, offset+1, 1, octet);
114
115             proto_tree_add_item ( rtp_events_tree, hf_rtp_events_duration, tvb, offset+2, 2, FALSE);
116
117             /* Make end-of-event packets obvious in the info column */
118             if ((octet & 0x80) && check_col(pinfo->cinfo, COL_INFO))
119             {
120                     col_append_str(pinfo->cinfo, COL_INFO, " (end)");
121             }
122
123           }
124 }
125
126
127 void
128 proto_register_rtp_events(void)
129 {
130
131         module_t *rtp_events_module;
132
133         static hf_register_info hf[] =
134         {
135                 {
136                         &hf_rtp_events_event,
137                         {
138                                 "Event ID",
139                                 "rtpevent.event_id",
140                                 FT_UINT8,
141                                 BASE_DEC,
142                                 VALS(rtp_event_type_values),
143                                 0x0,
144                                 "", HFILL
145                         }
146                 },
147                 {
148                         &hf_rtp_events_end,
149                         {
150                                 "End of Event",
151                                 "rtpevent.end_of_event",
152                                 FT_BOOLEAN,
153                                 8,
154                                 NULL,
155                                 0x80,
156                                 "", HFILL
157                         }
158                 },
159                 {
160                         &hf_rtp_events_reserved,
161                         {
162                                 "Reserved",
163                                 "rtpevent.reserved",
164                                 FT_BOOLEAN,
165                                 8,
166                                 NULL,
167                                 0x40,
168                                 "", HFILL
169                         }
170                 },
171                 {
172                         &hf_rtp_events_volume,
173                         {
174                                 "Volume",
175                                 "rtpevent.volume",
176                                 FT_UINT8,
177                                 BASE_DEC,
178                                 NULL,
179                                 0x3F,
180                                 "", HFILL
181                         }
182                 },
183
184                 {
185                         &hf_rtp_events_duration,
186                         {
187                                 "Event Duration",
188                                 "rtpevent.duration",
189                                 FT_UINT16,
190                                 BASE_DEC,
191                                 NULL,
192                                 0x0,
193                                 "", HFILL
194                         }
195                 },
196
197         };
198
199         static gint *ett[] =
200         {
201                 &ett_rtp_events,
202         };
203
204
205         proto_rtp_events = proto_register_protocol("RFC 2833 RTP Event", "RTP Event", "rtpevent");
206         proto_register_field_array(proto_rtp_events, hf, array_length(hf));
207         proto_register_subtree_array(ett, array_length(ett));
208
209
210     /* Register preferences */
211     rtp_events_module = prefs_register_protocol (proto_rtp_events, proto_reg_handoff_rtp_events);
212     prefs_register_uint_preference (rtp_events_module,
213                                     "event_payload_type_value", "Payload Type for RFC2833 RTP Events",
214                                     "This is the value of the Payload Type field"
215                                     "that specifies RTP Events", 10,
216                                     &rtp_event_payload_type_value);
217 }
218
219
220
221 void
222 proto_reg_handoff_rtp_events(void)
223 {
224         static dissector_handle_t rtp_events_handle;
225         static int rtp_events_prefs_initialized = FALSE;
226
227         if (!rtp_events_prefs_initialized) {
228                 rtp_events_handle = create_dissector_handle(dissect_rtp_events, proto_rtp_events);
229                 rtp_events_prefs_initialized = TRUE;
230         }
231         else {
232                 dissector_delete("rtp.pt", saved_payload_type_value, rtp_events_handle);
233         }
234
235         saved_payload_type_value = rtp_event_payload_type_value;
236         /* rtp_event_payload_type_value is set from preferences */
237
238         dissector_add("rtp.pt", saved_payload_type_value, rtp_events_handle);
239
240 }