From Jakub Zawadzki via bug 4273:
[obnox/wireshark/wip.git] / gtk / ansi_a_stat.c
1 /* ansi_a_stat.c
2  *
3  * Copyright 2003, Michael Lum <mlum [AT] telostech.com>
4  * In association with Telos Technology Inc.
5  *
6  * MUCH code modified from service_response_time_table.c.
7  *
8  * $Id$
9  *
10  * Wireshark - Network traffic analyzer
11  * By Gerald Combs <gerald@wireshark.org>
12  * Copyright 1998 Gerald Combs
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27  */
28
29 /*
30  * This TAP provides statistics for the ANSI A-Interface:
31  */
32
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <gtk/gtk.h>
38 #include <string.h>
39
40 #include "epan/packet_info.h"
41 #include "epan/epan.h"
42 #include "epan/value_string.h"
43 #include <epan/stat_cmd_args.h>
44 #include <epan/tap.h>
45 #include <epan/dissectors/packet-bssap.h>
46 #include <epan/dissectors/packet-ansi_a.h>
47
48 #include "../stat_menu.h"
49 #include "../simple_dialog.h"
50 #include "../register.h"
51 #include "../globals.h"
52
53 #include "gtk/gui_stat_menu.h"
54 #include "gtk/dlg_utils.h"
55 #include "gtk/filter_dlg.h"
56 #include "gtk/gui_utils.h"
57
58 enum
59 {
60    IEI_COLUMN,
61    MSG_NAME_COLUMN,
62    COUNT_COLUMN,
63    N_COLUMN /* The number of columns */
64 };
65
66 typedef struct _ansi_a_stat_dlg_t {
67     GtkWidget           *win;
68     GtkWidget           *scrolled_win;
69     GtkWidget           *table;
70 } ansi_a_stat_dlg_t;
71
72 typedef struct _ansi_a_stat_t {
73     int                 bsmap_message_type[0xff];
74     int                 dtap_message_type[0xff];
75 } ansi_a_stat_t;
76
77
78 static ansi_a_stat_dlg_t        dlg_bsmap;
79 static ansi_a_stat_dlg_t        dlg_dtap;
80 static ansi_a_stat_t            ansi_a_stat;
81
82
83 static void
84 ansi_a_stat_reset(
85     void                *tapdata)
86 {
87     ansi_a_stat_t       *stat_p = tapdata;
88
89     memset(stat_p, 0, sizeof(ansi_a_stat_t));
90 }
91
92
93 static int
94 ansi_a_stat_packet(
95     void                *tapdata,
96     packet_info         *pinfo _U_,
97     epan_dissect_t      *edt _U_,
98     const void          *data)
99 {
100     ansi_a_stat_t       *stat_p = tapdata;
101     const ansi_a_tap_rec_t      *data_p = data;
102
103     switch (data_p->pdu_type)
104     {
105     case BSSAP_PDU_TYPE_BSMAP:
106         stat_p->bsmap_message_type[data_p->message_type]++;
107         break;
108
109     case BSSAP_PDU_TYPE_DTAP:
110         stat_p->dtap_message_type[data_p->message_type]++;
111         break;
112
113     default:
114         /*
115          * unknown PDU type !!!
116          */
117         return(0);
118     }
119
120     return(1);
121 }
122
123
124 static void
125 ansi_a_stat_draw(
126     void                *tapdata)
127 {
128     ansi_a_stat_t       *stat_p = tapdata;
129     int                 i;
130     GtkListStore *list_store;
131         GtkTreeIter  iter;
132
133     if (dlg_bsmap.win && tapdata)
134     {
135                 list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW (dlg_bsmap.table))); /* Get store */
136
137                 i = 0;
138                 while (ansi_a_bsmap_strings[i].strptr){
139                         /* Creates a new row at position. iter will be changed to point to this new row. 
140                          * If position is larger than the number of rows on the list, then the new row will be appended to the list.
141                          * The row will be filled with the values given to this function.
142                          * :
143                          * should generally be preferred when inserting rows in a sorted list store.
144                          */
145 #if GTK_CHECK_VERSION(2,6,0)
146                         gtk_list_store_insert_with_values( list_store , &iter, G_MAXINT,
147 #else
148                         gtk_list_store_append  (list_store, &iter);
149                         gtk_list_store_set  (list_store, &iter,
150 #endif
151                                         IEI_COLUMN, ansi_a_bsmap_strings[i].value,
152                                         MSG_NAME_COLUMN, (char *)ansi_a_bsmap_strings[i].strptr,
153                                         COUNT_COLUMN, stat_p->bsmap_message_type[ansi_a_bsmap_strings[i].value],
154                                         -1);
155                         i++;
156                 }
157         }
158
159     if (dlg_dtap.win && tapdata){
160                 i = 0;
161                 list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW (dlg_dtap.table))); /* Get store */
162
163
164                 while (ansi_a_dtap_strings[i].strptr){
165                 /* Creates a new row at position. iter will be changed to point to this new row. 
166                  * If position is larger than the number of rows on the list, then the new row will be appended to the list.
167                  * The row will be filled with the values given to this function.
168                  * :
169                  * should generally be preferred when inserting rows in a sorted list store.
170                  */
171 #if GTK_CHECK_VERSION(2,6,0)
172                         gtk_list_store_insert_with_values( list_store , &iter, G_MAXINT,
173 #else
174                         gtk_list_store_append  (list_store, &iter);
175                         gtk_list_store_set  (list_store, &iter,
176 #endif
177                                 IEI_COLUMN, ansi_a_dtap_strings[i].value,
178                                 MSG_NAME_COLUMN, (char *)ansi_a_dtap_strings[i].strptr,
179                                 COUNT_COLUMN, stat_p->dtap_message_type[ansi_a_dtap_strings[i].value],
180                                 -1);
181
182                         i++;
183                 }
184         }
185 }
186
187 static void
188 ansi_a_stat_gtk_win_destroy_cb(
189     GtkWindow           *win _U_,
190     gpointer            user_data )
191 {
192     memset((void *) user_data, 0, sizeof(ansi_a_stat_dlg_t));
193 }
194
195
196 /* Create list */
197 static
198 GtkWidget* create_list(void)
199 {
200
201     GtkListStore *list_store;
202     GtkWidget *list;
203     GtkTreeViewColumn *column;
204     GtkCellRenderer *renderer;
205     GtkTreeSortable *sortable;
206         GtkTreeView     *list_view;
207         GtkTreeSelection  *selection;
208
209         /* Create the store */
210     list_store = gtk_list_store_new(N_COLUMN,   /* Total number of columns XXX*/
211                                G_TYPE_UINT,             /* IEI                          */
212                                G_TYPE_STRING,   /* Message Name         */
213                                G_TYPE_UINT);    /* Count                        */
214
215     /* Create a view */
216     list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (list_store));
217
218         list_view = GTK_TREE_VIEW(list);
219         sortable = GTK_TREE_SORTABLE(list_store);
220
221 #if GTK_CHECK_VERSION(2,6,0)
222         /* Speed up the list display */
223         gtk_tree_view_set_fixed_height_mode(list_view, TRUE);
224 #endif
225
226     /* Setup the sortable columns */
227     gtk_tree_sortable_set_sort_column_id(sortable, IEI_COLUMN, GTK_SORT_ASCENDING);
228     gtk_tree_view_set_headers_clickable(list_view, FALSE);
229
230     /* The view now holds a reference.  We can get rid of our own reference */
231     g_object_unref (G_OBJECT (list_store));
232
233     /* 
234          * Create the first column packet, associating the "text" attribute of the
235      * cell_renderer to the first column of the model 
236          */
237     renderer = gtk_cell_renderer_text_new ();
238     column = gtk_tree_view_column_new_with_attributes ("IEI", renderer, 
239                 "text", IEI_COLUMN, 
240                 NULL);
241
242         gtk_tree_view_column_set_cell_data_func(column, renderer, present_as_hex_func, 
243                 GINT_TO_POINTER(IEI_COLUMN), NULL);
244  
245         gtk_tree_view_column_set_sort_column_id(column, IEI_COLUMN);
246     gtk_tree_view_column_set_resizable(column, TRUE);
247     gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
248     gtk_tree_view_column_set_min_width(column, 50);
249
250         /* Add the column to the view. */
251     gtk_tree_view_append_column (list_view, column);
252
253     /* Second column.. Message Name. */
254     renderer = gtk_cell_renderer_text_new ();
255     column = gtk_tree_view_column_new_with_attributes ("Message Name", renderer, 
256                 "text", MSG_NAME_COLUMN,
257                 NULL);
258     gtk_tree_view_column_set_sort_column_id(column, MSG_NAME_COLUMN);
259     gtk_tree_view_column_set_resizable(column, TRUE);
260     gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
261     gtk_tree_view_column_set_min_width(column, 280);
262     gtk_tree_view_append_column (list_view, column);
263
264     /* Third column.. Count. */
265     renderer = gtk_cell_renderer_text_new ();
266         column = gtk_tree_view_column_new_with_attributes ("Count", renderer, 
267                 "text", COUNT_COLUMN, 
268                 NULL);
269         
270
271     gtk_tree_view_column_set_sort_column_id(column, COUNT_COLUMN);
272     gtk_tree_view_column_set_resizable(column, TRUE);
273     gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
274     gtk_tree_view_column_set_min_width(column, 50);
275     gtk_tree_view_append_column (list_view, column);
276
277     /* Now enable the sorting of each column */
278     gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(list_view), TRUE);
279     gtk_tree_view_set_headers_clickable(GTK_TREE_VIEW(list_view), TRUE);
280
281         /* Setup the selection handler */
282         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
283         gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
284
285         return list;
286
287 }
288 static void
289 ansi_a_stat_gtk_win_create(
290     ansi_a_stat_dlg_t   *dlg_p,
291     const char          *title)
292 {
293     GtkWidget           *vbox;
294     GtkWidget           *bt_close;
295     GtkWidget           *bbox;
296
297
298         dlg_p->win= dlg_window_new(title);  /* transient_for top_level */
299         gtk_window_set_destroy_with_parent (GTK_WINDOW(dlg_p->win), TRUE);
300
301     gtk_window_set_default_size(GTK_WINDOW(dlg_p->win), 480, 450);
302
303     vbox=gtk_vbox_new(FALSE, 3);
304     gtk_container_add(GTK_CONTAINER(dlg_p->win), vbox);
305     gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
306
307     dlg_p->scrolled_win = scrolled_window_new(NULL, NULL);
308     gtk_box_pack_start(GTK_BOX(vbox), dlg_p->scrolled_win, TRUE, TRUE, 0);
309
310         dlg_p->table = create_list();
311         gtk_widget_show(dlg_p->table);
312
313
314         gtk_container_add(GTK_CONTAINER(dlg_p->scrolled_win), dlg_p->table);
315
316         /* Button row. */
317     bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
318     gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
319
320     bt_close = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
321     window_set_cancel_button(dlg_p->win, bt_close, window_cancel_button_cb);
322
323     g_signal_connect(dlg_p->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
324     g_signal_connect(dlg_p->win, "destroy", G_CALLBACK(ansi_a_stat_gtk_win_destroy_cb), dlg_p);
325
326     gtk_widget_show_all(dlg_p->win);
327     window_present(dlg_p->win);
328 }
329
330
331 static void
332 ansi_a_stat_gtk_bsmap_cb(
333     GtkWidget           *w _U_,
334     gpointer            d _U_)
335 {
336     /*
337      * if the window is already open, bring it to front
338      */
339     if (dlg_bsmap.win)
340     {
341                 gdk_window_raise(dlg_bsmap.win->window);
342                 return;
343     }
344
345     ansi_a_stat_gtk_win_create(&dlg_bsmap, "ANSI A-I/F BSMAP Statistics");
346     ansi_a_stat_draw(&ansi_a_stat);
347 }
348
349
350 static void
351 ansi_a_stat_gtk_bsmap_init(
352     const char          *optarg _U_, void* userdata _U_)
353 {
354     ansi_a_stat_gtk_bsmap_cb(NULL, NULL);
355 }
356
357
358 static void
359 ansi_a_stat_gtk_dtap_cb(
360     GtkWidget           *w _U_,
361     gpointer            d _U_)
362 {
363
364     /*
365      * if the window is already open, bring it to front
366      */
367     if (dlg_dtap.win)
368     {
369                 gdk_window_raise(dlg_dtap.win->window);
370                 return;
371     }
372
373     ansi_a_stat_gtk_win_create(&dlg_dtap, "ANSI A-I/F DTAP Statistics");
374     ansi_a_stat_draw(&ansi_a_stat);
375 }
376
377
378 static void
379 ansi_a_stat_gtk_dtap_init(
380     const char          *optarg _U_,
381     void* userdata _U_)
382 {
383     ansi_a_stat_gtk_dtap_cb(NULL, NULL);
384 }
385
386
387 void
388 register_tap_listener_gtkansi_a_stat(void)
389 {
390     GString             *err_p;
391
392
393     memset((void *) &ansi_a_stat, 0, sizeof(ansi_a_stat_t));
394
395     err_p =
396         register_tap_listener("ansi_a", &ansi_a_stat, NULL, 0,
397             ansi_a_stat_reset,
398             ansi_a_stat_packet,
399             ansi_a_stat_draw);
400
401     if (err_p != NULL)
402     {
403         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", err_p->str);
404         g_string_free(err_p, TRUE);
405
406         exit(1);
407     }
408
409     register_stat_menu_item("_ANSI/A-Interface BSMAP", REGISTER_STAT_GROUP_TELEPHONY, 
410         ansi_a_stat_gtk_bsmap_cb, NULL, NULL ,NULL);
411     register_stat_cmd_arg("ansi_a,bsmap", ansi_a_stat_gtk_bsmap_init,NULL);
412
413     register_stat_menu_item("_ANSI/A-Interface DTAP", REGISTER_STAT_GROUP_TELEPHONY,
414         ansi_a_stat_gtk_dtap_cb, NULL, NULL ,NULL);
415     register_stat_cmd_arg("ansi_a,dtap", ansi_a_stat_gtk_dtap_init, NULL);
416 }