More eradication of old-style function definitions.
[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
51 #include "gtk/gui_stat_menu.h"
52 #include "gtk/dlg_utils.h"
53 #include "gtk/filter_dlg.h"
54 #include "gtk/gui_utils.h"
55
56 enum
57 {
58    IEI_COLUMN,
59    MSG_NAME_COLUMN,
60    COUNT_COLUMN,
61    N_COLUMN /* The number of columns */
62 };
63
64 typedef struct _ansi_a_stat_dlg_t {
65     GtkWidget           *win;
66     GtkWidget           *scrolled_win;
67     GtkWidget           *table;
68 } ansi_a_stat_dlg_t;
69
70 typedef struct _ansi_a_stat_t {
71     int                 bsmap_message_type[0xff];
72     int                 dtap_message_type[0xff];
73 } ansi_a_stat_t;
74
75
76 static ansi_a_stat_dlg_t        dlg_bsmap;
77 static ansi_a_stat_dlg_t        dlg_dtap;
78 static ansi_a_stat_t            ansi_a_stat;
79
80
81 static void
82 ansi_a_stat_reset(
83     void                *tapdata)
84 {
85     ansi_a_stat_t       *stat_p = tapdata;
86
87     memset(stat_p, 0, sizeof(ansi_a_stat_t));
88 }
89
90
91 static gboolean
92 ansi_a_stat_packet(
93     void                *tapdata,
94     packet_info         *pinfo _U_,
95     epan_dissect_t      *edt _U_,
96     const void          *data)
97 {
98     ansi_a_stat_t       *stat_p = tapdata;
99     const ansi_a_tap_rec_t      *data_p = data;
100
101     switch (data_p->pdu_type)
102     {
103     case BSSAP_PDU_TYPE_BSMAP:
104         stat_p->bsmap_message_type[data_p->message_type]++;
105         break;
106
107     case BSSAP_PDU_TYPE_DTAP:
108         stat_p->dtap_message_type[data_p->message_type]++;
109         break;
110
111     default:
112         /*
113          * unknown PDU type !!!
114          */
115         return(FALSE);
116     }
117
118     return(TRUE);
119 }
120
121
122 static void
123 ansi_a_stat_draw(
124     void           *tapdata)
125 {
126     ansi_a_stat_t  *stat_p = tapdata;
127     int             i;
128     GtkListStore   *list_store;
129     GtkTreeIter     iter;
130
131     if (dlg_bsmap.win && tapdata)
132     {
133         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW (dlg_bsmap.table))); /* Get store */
134
135         i = 0;
136         while (ansi_a_bsmap_strings[i].strptr){
137             /* Creates a new row at position. iter will be changed to point to this new row.
138              * If position is larger than the number of rows on the list, then the new row will be appended to the list.
139              * The row will be filled with the values given to this function.
140              * :
141              * should generally be preferred when inserting rows in a sorted list store.
142              */
143 #if GTK_CHECK_VERSION(2,6,0)
144             gtk_list_store_insert_with_values( list_store , &iter, G_MAXINT,
145 #else
146             gtk_list_store_append  (list_store, &iter);
147             gtk_list_store_set  (list_store, &iter,
148 #endif
149                                  IEI_COLUMN, ansi_a_bsmap_strings[i].value,
150                                  MSG_NAME_COLUMN, (char *)ansi_a_bsmap_strings[i].strptr,
151                                  COUNT_COLUMN, stat_p->bsmap_message_type[ansi_a_bsmap_strings[i].value],
152                                  -1);
153             i++;
154         }
155     }
156
157     if (dlg_dtap.win && tapdata){
158         i = 0;
159         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW (dlg_dtap.table))); /* Get store */
160
161
162         while (ansi_a_dtap_strings[i].strptr){
163             /* Creates a new row at position. iter will be changed to point to this new row.
164              * If position is larger than the number of rows on the list, then the new row will be appended to the list.
165              * The row will be filled with the values given to this function.
166              * :
167              * should generally be preferred when inserting rows in a sorted list store.
168              */
169 #if GTK_CHECK_VERSION(2,6,0)
170             gtk_list_store_insert_with_values( list_store , &iter, G_MAXINT,
171 #else
172             gtk_list_store_append  (list_store, &iter);
173             gtk_list_store_set  (list_store, &iter,
174 #endif
175                                  IEI_COLUMN, ansi_a_dtap_strings[i].value,
176                                  MSG_NAME_COLUMN, (char *)ansi_a_dtap_strings[i].strptr,
177                                  COUNT_COLUMN, stat_p->dtap_message_type[ansi_a_dtap_strings[i].value],
178                                  -1);
179
180             i++;
181         }
182     }
183 }
184
185 static void
186 ansi_a_stat_gtk_win_destroy_cb(
187     GtkWindow   *win _U_,
188     gpointer     user_data )
189 {
190     memset((void *) user_data, 0, sizeof(ansi_a_stat_dlg_t));
191 }
192
193
194 /* Create list */
195 static
196 GtkWidget* create_list(void)
197 {
198
199     GtkListStore *list_store;
200     GtkWidget *list;
201     GtkTreeViewColumn *column;
202     GtkCellRenderer *renderer;
203     GtkTreeSortable *sortable;
204     GtkTreeView     *list_view;
205     GtkTreeSelection  *selection;
206
207     /* Create the store */
208     list_store = gtk_list_store_new(N_COLUMN,           /* Total number of columns XXX  */
209                                     G_TYPE_UINT,        /* IEI                          */
210                                     G_TYPE_STRING,      /* Message Name                 */
211                                     G_TYPE_UINT);       /* Count                        */
212
213     /* Create a view */
214     list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (list_store));
215
216     list_view = GTK_TREE_VIEW(list);
217     sortable = GTK_TREE_SORTABLE(list_store);
218
219 #if GTK_CHECK_VERSION(2,6,0)
220     /* Speed up the list display */
221     gtk_tree_view_set_fixed_height_mode(list_view, TRUE);
222 #endif
223
224     /* Setup the sortable columns */
225     gtk_tree_sortable_set_sort_column_id(sortable, IEI_COLUMN, GTK_SORT_ASCENDING);
226     gtk_tree_view_set_headers_clickable(list_view, FALSE);
227
228     /* The view now holds a reference.  We can get rid of our own reference */
229     g_object_unref (G_OBJECT (list_store));
230
231     /*
232      * Create the first column packet, associating the "text" attribute of the
233      * cell_renderer to the first column of the model
234      */
235     renderer = gtk_cell_renderer_text_new ();
236     column = gtk_tree_view_column_new_with_attributes ("IEI",  renderer,
237                                                        "text", IEI_COLUMN,
238                                                        NULL);
239
240     gtk_tree_view_column_set_cell_data_func(column, renderer, present_as_hex_func,
241                                             GINT_TO_POINTER(IEI_COLUMN), NULL);
242
243     gtk_tree_view_column_set_sort_column_id(column, IEI_COLUMN);
244     gtk_tree_view_column_set_resizable(column, TRUE);
245     gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
246     gtk_tree_view_column_set_min_width(column, 50);
247
248     /* Add the column to the view. */
249     gtk_tree_view_append_column (list_view, column);
250
251     /* Second column.. Message Name. */
252     renderer = gtk_cell_renderer_text_new ();
253     column = gtk_tree_view_column_new_with_attributes ("Message Name", renderer,
254                                                        "text", MSG_NAME_COLUMN,
255                                                        NULL);
256     gtk_tree_view_column_set_sort_column_id(column, MSG_NAME_COLUMN);
257     gtk_tree_view_column_set_resizable(column, TRUE);
258     gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
259     gtk_tree_view_column_set_min_width(column, 280);
260     gtk_tree_view_append_column (list_view, column);
261
262     /* Third column.. Count. */
263     renderer = gtk_cell_renderer_text_new ();
264     column = gtk_tree_view_column_new_with_attributes ("Count", renderer,
265                                                        "text", COUNT_COLUMN,
266                                                        NULL);
267
268
269     gtk_tree_view_column_set_sort_column_id(column, COUNT_COLUMN);
270     gtk_tree_view_column_set_resizable(column, TRUE);
271     gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
272     gtk_tree_view_column_set_min_width(column, 50);
273     gtk_tree_view_append_column (list_view, column);
274
275     /* Now enable the sorting of each column */
276     gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(list_view), TRUE);
277     gtk_tree_view_set_headers_clickable(GTK_TREE_VIEW(list_view), TRUE);
278
279     /* Setup the selection handler */
280     selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
281     gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
282
283     return list;
284
285 }
286 static void
287 ansi_a_stat_gtk_win_create(
288     ansi_a_stat_dlg_t   *dlg_p,
289     const char          *title)
290 {
291     GtkWidget           *vbox;
292     GtkWidget           *bt_close;
293     GtkWidget           *bbox;
294
295
296     dlg_p->win= dlg_window_new(title);  /* transient_for top_level */
297     gtk_window_set_destroy_with_parent (GTK_WINDOW(dlg_p->win), TRUE);
298
299     gtk_window_set_default_size(GTK_WINDOW(dlg_p->win), 480, 450);
300
301     vbox=gtk_vbox_new(FALSE, 3);
302     gtk_container_add(GTK_CONTAINER(dlg_p->win), vbox);
303     gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
304
305     dlg_p->scrolled_win = scrolled_window_new(NULL, NULL);
306     gtk_box_pack_start(GTK_BOX(vbox), dlg_p->scrolled_win, TRUE, TRUE, 0);
307
308     dlg_p->table = create_list();
309     gtk_widget_show(dlg_p->table);
310
311
312     gtk_container_add(GTK_CONTAINER(dlg_p->scrolled_win), dlg_p->table);
313
314     /* Button row. */
315     bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
316     gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
317
318     bt_close = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
319     window_set_cancel_button(dlg_p->win, bt_close, window_cancel_button_cb);
320
321     g_signal_connect(dlg_p->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
322     g_signal_connect(dlg_p->win, "destroy", G_CALLBACK(ansi_a_stat_gtk_win_destroy_cb), dlg_p);
323
324     gtk_widget_show_all(dlg_p->win);
325     window_present(dlg_p->win);
326 }
327
328 #ifdef MAIN_MENU_USE_UIMANAGER
329 void
330 ansi_a_stat_gtk_bsmap_cb(GtkAction *action _U_, gpointer user_data _U_)
331 #else
332 static void
333 ansi_a_stat_gtk_bsmap_cb(
334     GtkWidget   *w _U_,
335     gpointer     d _U_)
336 #endif
337 {
338     /*
339      * if the window is already open, bring it to front
340      */
341     if (dlg_bsmap.win)
342     {
343         gdk_window_raise(dlg_bsmap.win->window);
344         return;
345     }
346
347     ansi_a_stat_gtk_win_create(&dlg_bsmap, "ANSI A-I/F BSMAP Statistics");
348     ansi_a_stat_draw(&ansi_a_stat);
349 }
350
351
352 static void
353 ansi_a_stat_gtk_bsmap_init(
354     const char  *optarg _U_, void* userdata _U_)
355 {
356     ansi_a_stat_gtk_bsmap_cb(NULL, NULL);
357 }
358
359 #ifdef MAIN_MENU_USE_UIMANAGER
360 void
361 ansi_a_stat_gtk_dtap_cb(GtkAction *action _U_, gpointer user_data _U_)
362 #else
363 static void
364 ansi_a_stat_gtk_dtap_cb(
365     GtkWidget   *w _U_,
366     gpointer     d _U_)
367 #endif
368 {
369
370     /*
371      * if the window is already open, bring it to front
372      */
373     if (dlg_dtap.win)
374     {
375         gdk_window_raise(dlg_dtap.win->window);
376         return;
377     }
378
379     ansi_a_stat_gtk_win_create(&dlg_dtap, "ANSI A-I/F DTAP Statistics");
380     ansi_a_stat_draw(&ansi_a_stat);
381 }
382
383
384 static void
385 ansi_a_stat_gtk_dtap_init(
386     const char     *optarg _U_,
387     void* userdata  _U_)
388 {
389     ansi_a_stat_gtk_dtap_cb(NULL, NULL);
390 }
391
392
393 void
394 register_tap_listener_gtkansi_a_stat(void)
395 {
396     GString         *err_p;
397
398
399     memset((void *) &ansi_a_stat, 0, sizeof(ansi_a_stat_t));
400
401     err_p =
402         register_tap_listener("ansi_a", &ansi_a_stat, NULL, 0,
403                               ansi_a_stat_reset,
404                               ansi_a_stat_packet,
405                               ansi_a_stat_draw);
406
407     if (err_p != NULL)
408     {
409         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", err_p->str);
410         g_string_free(err_p, TRUE);
411
412         exit(1);
413     }
414
415 #ifdef MAIN_MENU_USE_UIMANAGER
416 #else
417     register_stat_menu_item("_ANSI/A-Interface BSMAP", REGISTER_STAT_GROUP_TELEPHONY,
418                             ansi_a_stat_gtk_bsmap_cb, NULL, NULL ,NULL);
419     register_stat_menu_item("_ANSI/A-Interface DTAP", REGISTER_STAT_GROUP_TELEPHONY,
420                             ansi_a_stat_gtk_dtap_cb, NULL, NULL ,NULL);
421 #endif
422     register_stat_cmd_arg("ansi_a,bsmap", ansi_a_stat_gtk_bsmap_init,NULL);
423
424     register_stat_cmd_arg("ansi_a,dtap", ansi_a_stat_gtk_dtap_init, NULL);
425 }