More warining fixes: char -> const char
[obnox/wireshark/wip.git] / gtk / mtp3_stat.c
1 /* mtp3_stat.c
2  *
3  * Copyright 2004, Michael Lum <mlum [AT] telostech.com>
4  * In association with Telos Technology Inc.
5  *
6  * Modified from gsm_map_stat.c
7  *
8  * $Id$
9  *
10  * Ethereal - Network traffic analyzer
11  * By Gerald Combs <gerald@ethereal.com>
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 MTP3:
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 "tap_menu.h"
44 #include "image/clist_ascend.xpm"
45 #include "image/clist_descend.xpm"
46 #include "simple_dialog.h"
47 #include "dlg_utils.h"
48 #include <epan/tap.h>
49 #include "../register.h"
50 #include "../globals.h"
51 #include "filter_dlg.h"
52 #include "compat_macros.h"
53 #include "ui_util.h"
54
55 #include <epan/dissectors/packet-mtp3.h>
56 #include "mtp3_stat.h"
57
58 typedef struct column_arrows {
59     GtkWidget           *table;
60     GtkWidget           *ascend_pm;
61     GtkWidget           *descend_pm;
62 } column_arrows;
63
64 #define MTP3_INIT_TABLE_NUM_COLUMNS             6
65
66 typedef struct _my_columns_t {
67     guint32             value;
68     gchar               *strptr;
69     GtkJustification    just;
70 } my_columns_t;
71
72 static my_columns_t columns[MTP3_INIT_TABLE_NUM_COLUMNS] = {
73     { 80,       "OPC",                  GTK_JUSTIFY_LEFT },
74     { 80,       "DPC",                  GTK_JUSTIFY_LEFT },
75     { 110,      "SI",                   GTK_JUSTIFY_LEFT },
76     { 80,       "Num MSUs",             GTK_JUSTIFY_RIGHT },
77     { 100,      "Num Bytes",            GTK_JUSTIFY_RIGHT },
78     { 80,       "Avg Bytes",            GTK_JUSTIFY_RIGHT }
79 };
80
81 typedef struct _mtp3_stat_dlg_t {
82     GtkWidget           *win;
83     GtkWidget           *scrolled_win;
84     GtkWidget           *table;
85     char                *entries[MTP3_INIT_TABLE_NUM_COLUMNS];
86 } mtp3_stat_dlg_t;
87
88 static mtp3_stat_dlg_t  dlg;
89
90 mtp3_stat_t             mtp3_stat[MTP3_MAX_NUM_OPC_DPC];
91 guint8                  mtp3_num_used;
92
93
94 static void
95 mtp3_stat_reset(
96     void                *tapdata)
97 {
98     mtp3_stat_t         (*stat_p)[MTP3_MAX_NUM_OPC_DPC] = tapdata;
99
100     mtp3_num_used = 0;
101     memset(stat_p, 0, MTP3_MAX_NUM_OPC_DPC * sizeof(mtp3_stat_t));
102
103     if (dlg.win != NULL)
104     {
105         gtk_clist_clear(GTK_CLIST(dlg.table));
106     }
107 }
108
109
110 static int
111 mtp3_stat_packet(
112     void                *tapdata,
113     packet_info         *pinfo _U_,
114     epan_dissect_t      *edt _U_,
115     const void          *data)
116 {
117     mtp3_stat_t         (*stat_p)[MTP3_MAX_NUM_OPC_DPC] = tapdata;
118     const mtp3_tap_rec_t        *data_p = data;
119     int                         i;
120
121     if (data_p->si_code >= MTP3_NUM_SI_CODE)
122     {
123         /*
124          * we thought this si_code was not used ?
125          * is MTP3_NUM_SI_CODE out of date ?
126          */
127         return(0);
128     }
129
130     /*
131      * look for opc/dpc pair
132      */
133     i = 0;
134     while (i < mtp3_num_used)
135     {
136         if (memcmp(&data_p->addr_opc, &(*stat_p)[i].addr_opc, sizeof(mtp3_addr_pc_t)) == 0)
137         {
138             if (memcmp(&data_p->addr_dpc, &(*stat_p)[i].addr_dpc, sizeof(mtp3_addr_pc_t)) == 0)
139             {
140                 break;
141             }
142         }
143
144         i++;
145     }
146
147     if (i == mtp3_num_used)
148     {
149         if (mtp3_num_used == MTP3_MAX_NUM_OPC_DPC)
150         {
151             /*
152              * too many
153              */
154             return(0);
155         }
156
157         mtp3_num_used++;
158     }
159
160     (*stat_p)[i].addr_opc = data_p->addr_opc;
161     (*stat_p)[i].addr_dpc = data_p->addr_dpc;
162     (*stat_p)[i].si_code[data_p->si_code].num_msus++;
163     (*stat_p)[i].si_code[data_p->si_code].size += data_p->size;
164
165     return(1);
166 }
167
168
169 static void
170 mtp3_stat_draw(
171     void                *tapdata)
172 {
173     mtp3_stat_t         (*stat_p)[MTP3_MAX_NUM_OPC_DPC] = tapdata;
174     int                 i, j, row_offset;
175     char                str[256];
176
177     if (!dlg.win || !tapdata)
178     {
179         return;
180     }
181
182     i = 0;
183
184     while (i < mtp3_num_used)
185     {
186         row_offset = i * MTP3_NUM_SI_CODE;
187
188         mtp3_addr_to_str_buf((guint8 *) &(*stat_p)[i].addr_opc, str);
189         dlg.entries[0] = g_strdup(str);
190
191         mtp3_addr_to_str_buf((guint8 *) &(*stat_p)[i].addr_dpc, str);
192         dlg.entries[1] = g_strdup(str);
193
194         for (j=0; j < MTP3_NUM_SI_CODE; j++)
195         {
196             dlg.entries[2] = g_strdup(mtp3_service_indicator_code_short_vals[j].strptr);
197
198             dlg.entries[3] = g_strdup_printf("%u", (*stat_p)[i].si_code[j].num_msus);
199
200             dlg.entries[4] = g_strdup_printf("%.0f", (*stat_p)[i].si_code[j].size);
201
202             dlg.entries[5] =
203                 g_strdup_printf("%.2f",
204                     (*stat_p)[i].si_code[j].size/(*stat_p)[i].si_code[j].num_msus);
205
206             gtk_clist_insert(GTK_CLIST(dlg.table), row_offset + j, dlg.entries);
207         }
208
209         i++;
210     }
211
212     gtk_clist_sort(GTK_CLIST(dlg.table));
213 }
214
215
216 static void
217 mtp3_stat_gtk_click_column_cb(
218     GtkCList            *clist,
219     gint                column,
220     gpointer            data)
221 {
222     column_arrows       *col_arrows = (column_arrows *) data;
223     int                 i;
224
225
226     gtk_clist_freeze(clist);
227
228     for (i=0; i < MTP3_INIT_TABLE_NUM_COLUMNS; i++)
229     {
230         gtk_widget_hide(col_arrows[i].ascend_pm);
231         gtk_widget_hide(col_arrows[i].descend_pm);
232     }
233
234     if (column == clist->sort_column)
235     {
236         if (clist->sort_type == GTK_SORT_ASCENDING)
237         {
238             clist->sort_type = GTK_SORT_DESCENDING;
239             gtk_widget_show(col_arrows[column].descend_pm);
240         }
241         else
242         {
243             clist->sort_type = GTK_SORT_ASCENDING;
244             gtk_widget_show(col_arrows[column].ascend_pm);
245         }
246     }
247     else
248     {
249         /*
250          * Columns 0-1 sorted in descending order by default
251          */
252         if (column <= 1)
253         {
254             clist->sort_type = GTK_SORT_ASCENDING;
255             gtk_widget_show(col_arrows[column].ascend_pm);
256         }
257         else
258         {
259             clist->sort_type = GTK_SORT_DESCENDING;
260             gtk_widget_show(col_arrows[column].descend_pm);
261         }
262
263         gtk_clist_set_sort_column(clist, column);
264     }
265
266     gtk_clist_thaw(clist);
267     gtk_clist_sort(clist);
268 }
269
270
271 static gint
272 mtp3_stat_gtk_sort_column(
273     GtkCList            *clist,
274     gconstpointer       ptr1,
275     gconstpointer       ptr2)
276 {
277     const GtkCListRow   *row1 = ptr1;
278     const GtkCListRow   *row2 = ptr2;
279     char                *text1 = NULL;
280     char                *text2 = NULL;
281     int                 i1, i2;
282
283     text1 = GTK_CELL_TEXT(row1->cell[clist->sort_column])->text;
284     text2 = GTK_CELL_TEXT(row2->cell[clist->sort_column])->text;
285
286     switch (clist->sort_column)
287     {
288     case 0:
289     case 1:
290     case 2:
291         /* text columns */
292         return(strcmp(text1, text2));
293
294     default:
295         /* number columns */
296         i1 = strtol(text1, NULL, 0);
297         i2 = strtol(text2, NULL, 0);
298         return(i1 - i2);
299     }
300
301     g_assert_not_reached();
302
303     return(0);
304 }
305
306
307 static void
308 mtp3_stat_gtk_win_destroy_cb(
309     GtkWindow           *win _U_,
310     gpointer            user_data _U_)
311 {
312     memset((void *) user_data, 0, sizeof(mtp3_stat_dlg_t));
313 }
314
315
316 static void
317 mtp3_stat_gtk_win_create(
318     mtp3_stat_dlg_t     *dlg_p,
319     char                *title)
320 {
321     int                 i;
322     column_arrows       *col_arrows;
323     GtkWidget           *column_lb;
324     GtkWidget           *vbox;
325     GtkWidget           *bt_close;
326     GtkWidget           *bbox;
327
328
329     dlg_p->win = window_new(GTK_WINDOW_TOPLEVEL, title);
330     gtk_window_set_default_size(GTK_WINDOW(dlg_p->win), 640, 390);
331
332     vbox = gtk_vbox_new(FALSE, 3);
333         gtk_container_add(GTK_CONTAINER(dlg_p->win), vbox);
334     gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
335
336     dlg_p->scrolled_win = scrolled_window_new(NULL, NULL);
337     gtk_box_pack_start(GTK_BOX(vbox), dlg_p->scrolled_win, TRUE, TRUE, 0);
338
339     dlg_p->table = gtk_clist_new(MTP3_INIT_TABLE_NUM_COLUMNS);
340
341     col_arrows =
342         (column_arrows *) g_malloc(sizeof(column_arrows) * MTP3_INIT_TABLE_NUM_COLUMNS);
343
344     for (i = 0; i < MTP3_INIT_TABLE_NUM_COLUMNS; i++)
345     {
346         col_arrows[i].table = gtk_table_new(2, 2, FALSE);
347
348         gtk_table_set_col_spacings(GTK_TABLE(col_arrows[i].table), 5);
349
350         column_lb = gtk_label_new(columns[i].strptr);
351
352         gtk_table_attach(GTK_TABLE(col_arrows[i].table), column_lb,
353             0, 1, 0, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
354
355         gtk_widget_show(column_lb);
356
357         col_arrows[i].ascend_pm = xpm_to_widget(clist_ascend_xpm);
358
359         gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].ascend_pm,
360             1, 2, 1, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
361
362         col_arrows[i].descend_pm = xpm_to_widget(clist_descend_xpm);
363
364         gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].descend_pm,
365             1, 2, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
366
367         if (i == 0)
368         {
369             /* default column sorting */
370             gtk_widget_show(col_arrows[i].ascend_pm);
371         }
372
373         gtk_clist_set_column_justification(GTK_CLIST(dlg_p->table), i, columns[i].just);
374
375         gtk_clist_set_column_widget(GTK_CLIST(dlg_p->table), i, col_arrows[i].table);
376         gtk_widget_show(col_arrows[i].table);
377     }
378     gtk_clist_column_titles_show(GTK_CLIST(dlg_p->table));
379
380     gtk_clist_set_compare_func(GTK_CLIST(dlg_p->table), mtp3_stat_gtk_sort_column);
381     gtk_clist_set_sort_column(GTK_CLIST(dlg_p->table), 0);
382     gtk_clist_set_sort_type(GTK_CLIST(dlg_p->table), GTK_SORT_ASCENDING);
383
384     for (i = 0; i < MTP3_INIT_TABLE_NUM_COLUMNS; i++)
385     {
386         gtk_clist_set_column_width(GTK_CLIST(dlg_p->table), i, columns[i].value);
387     }
388
389     gtk_clist_set_shadow_type(GTK_CLIST(dlg_p->table), GTK_SHADOW_IN);
390     gtk_clist_column_titles_show(GTK_CLIST(dlg_p->table));
391     gtk_container_add(GTK_CONTAINER(dlg_p->scrolled_win), dlg_p->table);
392
393     SIGNAL_CONNECT(dlg_p->table, "click-column", mtp3_stat_gtk_click_column_cb, col_arrows);
394
395     /* Button row. */
396     bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
397         gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
398
399     bt_close = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
400     window_set_cancel_button(dlg_p->win, bt_close, window_cancel_button_cb);
401
402     SIGNAL_CONNECT(dlg_p->win, "delete_event", window_delete_event_cb, NULL);
403     SIGNAL_CONNECT(dlg_p->win, "destroy", mtp3_stat_gtk_win_destroy_cb, dlg_p);
404
405     gtk_widget_show_all(dlg_p->win);
406     window_present(dlg_p->win);
407 }
408
409
410 /*
411  * Never gets called ?
412  */
413 static void
414 mtp3_stat_gtk_init(
415     char                *optarg)
416 {
417     /* does not appear to be called */
418
419     optarg = optarg;
420 }
421
422
423 static void
424 mtp3_stat_gtk_cb(
425     GtkWidget           *w _U_,
426     gpointer            d _U_)
427 {
428
429     /*
430      * if the window is already open, bring it to front
431      */
432     if (dlg.win)
433     {
434         gdk_window_raise(dlg.win->window);
435         return;
436     }
437
438     mtp3_stat_gtk_win_create(&dlg, "MTP3 Statistics");
439
440     mtp3_stat_draw(NULL);
441 }
442
443
444 void
445 register_tap_listener_gtkmtp3_stat(void)
446 {
447     GString             *err_p;
448
449
450     register_tap_listener_cmd_arg("mtp3,", mtp3_stat_gtk_init);
451
452     memset((void *) &mtp3_stat, 0, sizeof(mtp3_stat_t));
453
454     err_p =
455         register_tap_listener("mtp3", &mtp3_stat, NULL,
456             mtp3_stat_reset,
457             mtp3_stat_packet,
458             mtp3_stat_draw);
459
460     if (err_p != NULL)
461     {
462         simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK, err_p->str);
463         g_string_free(err_p, TRUE);
464
465         exit(1);
466     }
467
468     register_tap_menu_item("MTP3/MSUs",  REGISTER_TAP_GROUP_TELEPHONY,
469         mtp3_stat_gtk_cb, NULL, NULL, NULL);
470 }