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