Move the stats.[ch] stuff into epan, so plugins can use it.
[obnox/wireshark/wip.git] / gtk / gsm_a_stat.c
1 /* gsm_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  * 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 the GSM 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.h>
44 #include "stat_menu.h"
45 #include "image/clist_ascend.xpm"
46 #include "image/clist_descend.xpm"
47 #include "simple_dialog.h"
48 #include "dlg_utils.h"
49 #include <epan/tap.h>
50 #include "../register.h"
51 #include <epan/dissectors/packet-bssap.h>
52 #include <epan/dissectors/packet-gsm_a.h>
53 #include "../globals.h"
54 #include "filter_dlg.h"
55 #include "compat_macros.h"
56 #include "ui_util.h"
57
58
59 typedef struct column_arrows {
60     GtkWidget           *table;
61     GtkWidget           *ascend_pm;
62     GtkWidget           *descend_pm;
63 } column_arrows;
64
65 typedef struct _gsm_a_stat_dlg_t {
66     GtkWidget           *win;
67     GtkWidget           *scrolled_win;
68     GtkWidget           *table;
69     char                *entries[3];
70 } gsm_a_stat_dlg_t;
71
72 typedef struct _gsm_a_stat_t {
73     int         bssmap_message_type[0xff];
74     int         dtap_mm_message_type[0xff];
75     int         dtap_rr_message_type[0xff];
76     int         dtap_cc_message_type[0xff];
77     int         dtap_gmm_message_type[0xff];
78     int         dtap_sms_message_type[0xff];
79     int         dtap_sm_message_type[0xff];
80     int         dtap_ss_message_type[0xff];
81 } gsm_a_stat_t;
82
83
84 static gsm_a_stat_dlg_t         dlg_bssmap;
85 static gsm_a_stat_dlg_t         dlg_dtap_mm;
86 static gsm_a_stat_dlg_t         dlg_dtap_rr;
87 static gsm_a_stat_dlg_t         dlg_dtap_cc;
88 static gsm_a_stat_dlg_t         dlg_dtap_gmm;
89 static gsm_a_stat_dlg_t         dlg_dtap_sms;
90 static gsm_a_stat_dlg_t         dlg_dtap_sm;
91 static gsm_a_stat_dlg_t         dlg_dtap_ss;
92 static gsm_a_stat_t             stat;
93
94
95 static void
96 gsm_a_stat_reset(
97     void                *tapdata)
98 {
99     gsm_a_stat_t        *stat_p = tapdata;
100
101     memset(stat_p, 0, sizeof(gsm_a_stat_t));
102 }
103
104
105 static int
106 gsm_a_stat_packet(
107     void                *tapdata,
108     packet_info         *pinfo _U_,
109     epan_dissect_t      *edt _U_,
110     const void          *data)
111 {
112     gsm_a_stat_t        *stat_p = tapdata;
113     const gsm_a_tap_rec_t       *data_p = data;
114
115     switch (data_p->pdu_type)
116     {
117     case BSSAP_PDU_TYPE_BSSMAP:
118         stat_p->bssmap_message_type[data_p->message_type]++;
119         break;
120
121     case BSSAP_PDU_TYPE_DTAP:
122         switch (data_p->protocol_disc)
123         {
124         case PD_CC:
125             stat_p->dtap_cc_message_type[data_p->message_type]++;
126             break;
127         case PD_MM:
128             stat_p->dtap_mm_message_type[data_p->message_type]++;
129             break;
130         case PD_RR:
131             stat_p->dtap_rr_message_type[data_p->message_type]++;
132             break;
133         case PD_GMM:
134             stat_p->dtap_gmm_message_type[data_p->message_type]++;
135             break;
136         case PD_SMS:
137             stat_p->dtap_sms_message_type[data_p->message_type]++;
138             break;
139         case PD_SM:
140             stat_p->dtap_sm_message_type[data_p->message_type]++;
141             break;
142         case PD_SS:
143             stat_p->dtap_ss_message_type[data_p->message_type]++;
144             break;
145         default:
146             /*
147              * unsupported PD
148              */
149             return(0);
150         }
151         break;
152
153     default:
154         /*
155          * unknown PDU type !!!
156          */
157         return(0);
158     }
159
160     return(1);
161 }
162
163
164 static void
165 gsm_a_stat_draw_aux(
166     gsm_a_stat_dlg_t    *dlg_p,
167     int                 *message_count,
168     const value_string  *msg_strings)
169 {
170     int                 i, j;
171     char                *strp;
172
173
174     if (dlg_p->win != NULL)
175     {
176         i = 0;
177
178         while (msg_strings[i].strptr)
179         {
180             j = gtk_clist_find_row_from_data(GTK_CLIST(dlg_p->table), (gpointer) i);
181
182             strp = g_strdup_printf("%d", message_count[msg_strings[i].value]);
183             gtk_clist_set_text(GTK_CLIST(dlg_p->table), j, 2, strp);
184             g_free(strp);
185
186             i++;
187         }
188
189         gtk_clist_sort(GTK_CLIST(dlg_p->table));
190     }
191 }
192
193 static void
194 gsm_a_stat_draw(
195     void                *tapdata)
196 {
197     gsm_a_stat_t        *stat_p = tapdata;
198
199         if (!tapdata) return;
200
201     if (dlg_bssmap.win != NULL)
202     {
203         gsm_a_stat_draw_aux(&dlg_bssmap,
204             stat_p->bssmap_message_type,
205             gsm_a_bssmap_msg_strings);
206     }
207
208     if (dlg_dtap_mm.win != NULL)
209     {
210         gsm_a_stat_draw_aux(&dlg_dtap_mm,
211             stat_p->dtap_mm_message_type,
212             gsm_a_dtap_msg_mm_strings);
213     }
214
215     if (dlg_dtap_rr.win != NULL)
216     {
217         gsm_a_stat_draw_aux(&dlg_dtap_rr,
218             stat_p->dtap_rr_message_type,
219             gsm_a_dtap_msg_rr_strings);
220     }
221
222     if (dlg_dtap_cc.win != NULL)
223     {
224         gsm_a_stat_draw_aux(&dlg_dtap_cc,
225             stat_p->dtap_cc_message_type,
226             gsm_a_dtap_msg_cc_strings);
227     }
228
229     if (dlg_dtap_gmm.win != NULL)
230     {
231         gsm_a_stat_draw_aux(&dlg_dtap_gmm,
232             stat_p->dtap_gmm_message_type,
233             gsm_a_dtap_msg_gmm_strings);
234     }
235
236     if (dlg_dtap_sms.win != NULL)
237     {
238         gsm_a_stat_draw_aux(&dlg_dtap_sms,
239             stat_p->dtap_sms_message_type,
240             gsm_a_dtap_msg_sms_strings);
241     }
242
243     if (dlg_dtap_sm.win != NULL)
244     {
245         gsm_a_stat_draw_aux(&dlg_dtap_sm,
246             stat_p->dtap_sm_message_type,
247             gsm_a_dtap_msg_sm_strings);
248     }
249
250     if (dlg_dtap_ss.win != NULL)
251     {
252         gsm_a_stat_draw_aux(&dlg_dtap_ss,
253             stat_p->dtap_ss_message_type,
254             gsm_a_dtap_msg_ss_strings);
255     }
256 }
257
258
259 static void
260 gsm_a_stat_gtk_click_column_cb(
261     GtkCList            *clist,
262     gint                column,
263     gpointer            data)
264 {
265     column_arrows       *col_arrows = (column_arrows *) data;
266     int                 i;
267
268
269     gtk_clist_freeze(clist);
270
271     for (i=0; i < 3; i++)
272     {
273         gtk_widget_hide(col_arrows[i].ascend_pm);
274         gtk_widget_hide(col_arrows[i].descend_pm);
275     }
276
277     if (column == clist->sort_column)
278     {
279         if (clist->sort_type == GTK_SORT_ASCENDING)
280         {
281             clist->sort_type = GTK_SORT_DESCENDING;
282             gtk_widget_show(col_arrows[column].descend_pm);
283         }
284         else
285         {
286             clist->sort_type = GTK_SORT_ASCENDING;
287             gtk_widget_show(col_arrows[column].ascend_pm);
288         }
289     }
290     else
291     {
292         /*
293          * Columns 0-1 sorted in descending order by default
294          * Columns 2 sorted in ascending order by default
295          */
296         if (column <= 1)
297         {
298             clist->sort_type = GTK_SORT_ASCENDING;
299             gtk_widget_show(col_arrows[column].ascend_pm);
300         }
301         else
302         {
303             clist->sort_type = GTK_SORT_DESCENDING;
304             gtk_widget_show(col_arrows[column].descend_pm);
305         }
306
307         gtk_clist_set_sort_column(clist, column);
308     }
309
310     gtk_clist_thaw(clist);
311     gtk_clist_sort(clist);
312 }
313
314
315 static gint
316 gsm_a_stat_gtk_sort_column(
317     GtkCList            *clist,
318     gconstpointer       ptr1,
319     gconstpointer       ptr2)
320 {
321     const GtkCListRow   *row1 = ptr1;
322     const GtkCListRow   *row2 = ptr2;
323     char                *text1 = NULL;
324     char                *text2 = NULL;
325     int                 i1, i2;
326
327     text1 = GTK_CELL_TEXT(row1->cell[clist->sort_column])->text;
328     text2 = GTK_CELL_TEXT(row2->cell[clist->sort_column])->text;
329
330     switch (clist->sort_column)
331     {
332     case 0:
333         /* FALLTHRU */
334
335     case 2:
336         i1 = strtol(text1, NULL, 0);
337         i2 = strtol(text2, NULL, 0);
338         return(i1 - i2);
339
340     case 1:
341         return(strcmp(text1, text2));
342     }
343
344     g_assert_not_reached();
345
346     return(0);
347 }
348
349
350 static void
351 gsm_a_stat_gtk_win_destroy_cb(
352     GtkWindow           *win _U_,
353     gpointer            user_data _U_)
354 {
355     memset((void *) user_data, 0, sizeof(gsm_a_stat_dlg_t));
356 }
357
358
359 static void
360 gsm_a_stat_gtk_win_create(
361     gsm_a_stat_dlg_t    *dlg_p,
362     const char          *title)
363 {
364 #define INIT_TABLE_NUM_COLUMNS  3
365     const char          *default_titles[] = { "IEI", "Message Name", "Count" };
366     int                 i;
367     column_arrows       *col_arrows;
368     GtkWidget           *column_lb;
369     GtkWidget           *vbox;
370     GtkWidget           *bt_close;
371     GtkWidget           *bbox;
372
373
374     dlg_p->win = window_new(GTK_WINDOW_TOPLEVEL, title);
375     gtk_window_set_default_size(GTK_WINDOW(dlg_p->win), 490, 500);
376
377     vbox = gtk_vbox_new(FALSE, 3);
378         gtk_container_add(GTK_CONTAINER(dlg_p->win), vbox);
379     gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
380
381     dlg_p->scrolled_win = scrolled_window_new(NULL, NULL);
382     gtk_box_pack_start(GTK_BOX(vbox), dlg_p->scrolled_win, TRUE, TRUE, 0);
383
384     dlg_p->table = gtk_clist_new(INIT_TABLE_NUM_COLUMNS);
385
386     col_arrows =
387         (column_arrows *) g_malloc(sizeof(column_arrows) * INIT_TABLE_NUM_COLUMNS);
388
389     for (i = 0; i < INIT_TABLE_NUM_COLUMNS; i++)
390     {
391         col_arrows[i].table = gtk_table_new(2, 2, FALSE);
392
393         gtk_table_set_col_spacings(GTK_TABLE(col_arrows[i].table), 5);
394
395         column_lb = gtk_label_new(default_titles[i]);
396
397         gtk_table_attach(GTK_TABLE(col_arrows[i].table), column_lb,
398             0, 1, 0, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
399
400         gtk_widget_show(column_lb);
401
402         col_arrows[i].ascend_pm = xpm_to_widget(clist_ascend_xpm);
403
404         gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].ascend_pm,
405             1, 2, 1, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
406
407         col_arrows[i].descend_pm = xpm_to_widget(clist_descend_xpm);
408
409         gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].descend_pm,
410             1, 2, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
411
412         if (i == 0)
413         {
414             /* default column sorting */
415             gtk_widget_show(col_arrows[i].ascend_pm);
416         }
417
418         gtk_clist_set_column_widget(GTK_CLIST(dlg_p->table), i, col_arrows[i].table);
419         gtk_widget_show(col_arrows[i].table);
420     }
421     gtk_clist_column_titles_show(GTK_CLIST(dlg_p->table));
422
423     gtk_clist_set_compare_func(GTK_CLIST(dlg_p->table), gsm_a_stat_gtk_sort_column);
424     gtk_clist_set_sort_column(GTK_CLIST(dlg_p->table), 0);
425     gtk_clist_set_sort_type(GTK_CLIST(dlg_p->table), GTK_SORT_ASCENDING);
426
427     gtk_clist_set_column_width(GTK_CLIST(dlg_p->table), 0, 50);
428     gtk_clist_set_column_width(GTK_CLIST(dlg_p->table), 1, 330);
429     gtk_clist_set_column_width(GTK_CLIST(dlg_p->table), 2, 50);
430
431     gtk_clist_set_shadow_type(GTK_CLIST(dlg_p->table), GTK_SHADOW_IN);
432     gtk_clist_column_titles_show(GTK_CLIST(dlg_p->table));
433     gtk_container_add(GTK_CONTAINER(dlg_p->scrolled_win), dlg_p->table);
434
435     SIGNAL_CONNECT(dlg_p->table, "click-column", gsm_a_stat_gtk_click_column_cb, col_arrows);
436
437         /* Button row. */
438     bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
439     gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
440
441     bt_close = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
442     window_set_cancel_button(dlg_p->win, bt_close, window_cancel_button_cb);
443
444     SIGNAL_CONNECT(dlg_p->win, "delete_event", window_delete_event_cb, NULL);
445     SIGNAL_CONNECT(dlg_p->win, "destroy", gsm_a_stat_gtk_win_destroy_cb, dlg_p);
446
447     gtk_widget_show_all(dlg_p->win);
448     window_present(dlg_p->win);
449 }
450
451
452 /*
453  * Never gets called ?
454  */
455 static void
456 gsm_a_stat_gtk_init(
457     const char          *optarg _U_)
458 {
459     /* does not appear to be called */
460 }
461
462
463 static void
464 gsm_a_stat_gtk_bssmap_cb(
465     GtkWidget           *w _U_,
466     gpointer            d _U_)
467 {
468     int                 i;
469     char                str[100];
470
471
472     /*
473      * if the window is already open, bring it to front
474      */
475     if (dlg_bssmap.win)
476     {
477         gdk_window_raise(dlg_bssmap.win->window);
478         return;
479     }
480
481     gsm_a_stat_gtk_win_create(&dlg_bssmap, "GSM A-I/F BSSMAP Statistics");
482
483     i = 0;
484     while (gsm_a_bssmap_msg_strings[i].strptr)
485     {
486         g_snprintf(str, 100, "0x%02x", gsm_a_bssmap_msg_strings[i].value);
487         dlg_bssmap.entries[0] = g_strdup(str);
488
489         dlg_bssmap.entries[1] = g_strdup(gsm_a_bssmap_msg_strings[i].strptr);
490
491         dlg_bssmap.entries[2] = g_strdup("0");
492
493         gtk_clist_insert(GTK_CLIST(dlg_bssmap.table), i, dlg_bssmap.entries);
494         gtk_clist_set_row_data(GTK_CLIST(dlg_bssmap.table), i, (gpointer) i);
495
496         i++;
497     }
498
499     gsm_a_stat_draw(&stat);
500 }
501
502
503 static void
504 gsm_a_stat_gtk_dtap_cb(
505     GtkWidget           *w _U_,
506     gpointer            d _U_,
507     gsm_a_stat_dlg_t    *dlg_dtap_p,
508     const char          *title,
509     const value_string  *dtap_msg_strings)
510 {
511     int                 i;
512     char                str[100];
513
514
515     /*
516      * if the window is already open, bring it to front
517      */
518     if (dlg_dtap_p->win)
519     {
520         gdk_window_raise(dlg_dtap_p->win->window);
521         return;
522     }
523
524     gsm_a_stat_gtk_win_create(dlg_dtap_p, title);
525
526     i = 0;
527     while (dtap_msg_strings[i].strptr)
528     {
529         g_snprintf(str, 100, "0x%02x", dtap_msg_strings[i].value);
530         dlg_dtap_p->entries[0] = g_strdup(str);
531
532         dlg_dtap_p->entries[1] = g_strdup(dtap_msg_strings[i].strptr);
533
534         dlg_dtap_p->entries[2] = g_strdup("0");
535
536         gtk_clist_insert(GTK_CLIST(dlg_dtap_p->table), i, dlg_dtap_p->entries);
537         gtk_clist_set_row_data(GTK_CLIST(dlg_dtap_p->table), i, (gpointer) i);
538
539         i++;
540     }
541
542     gsm_a_stat_draw(&stat);
543 }
544
545 static void
546 gsm_a_stat_gtk_dtap_mm_cb(
547     GtkWidget           *w _U_,
548     gpointer            d _U_)
549 {
550     gsm_a_stat_gtk_dtap_cb(w, d, &dlg_dtap_mm,
551         "GSM A-I/F DTAP Mobility Management Statistics",
552         gsm_a_dtap_msg_mm_strings);
553 }
554
555 static void
556 gsm_a_stat_gtk_dtap_rr_cb(
557     GtkWidget           *w _U_,
558     gpointer            d _U_)
559 {
560     gsm_a_stat_gtk_dtap_cb(w, d, &dlg_dtap_rr,
561         "GSM A-I/F DTAP Radio Resource Management Statistics",
562         gsm_a_dtap_msg_rr_strings);
563 }
564
565 static void
566 gsm_a_stat_gtk_dtap_cc_cb(
567     GtkWidget           *w _U_,
568     gpointer            d _U_)
569 {
570     gsm_a_stat_gtk_dtap_cb(w, d, &dlg_dtap_cc,
571         "GSM A-I/F DTAP Call Control Statistics",
572         gsm_a_dtap_msg_cc_strings);
573 }
574
575 static void
576 gsm_a_stat_gtk_dtap_gmm_cb(
577     GtkWidget           *w _U_,
578     gpointer            d _U_)
579 {
580     gsm_a_stat_gtk_dtap_cb(w, d, &dlg_dtap_gmm,
581         "GSM A-I/F DTAP GPRS Mobility Management Statistics",
582         gsm_a_dtap_msg_gmm_strings);
583 }
584
585 static void
586 gsm_a_stat_gtk_dtap_sms_cb(
587     GtkWidget           *w _U_,
588     gpointer            d _U_)
589 {
590     gsm_a_stat_gtk_dtap_cb(w, d, &dlg_dtap_sms,
591         "GSM A-I/F DTAP Short Message Service Statistics",
592         gsm_a_dtap_msg_sms_strings);
593 }
594
595 static void
596 gsm_a_stat_gtk_dtap_sm_cb(
597     GtkWidget           *w _U_,
598     gpointer            d _U_)
599 {
600     gsm_a_stat_gtk_dtap_cb(w, d, &dlg_dtap_sm,
601         "GSM A-I/F DTAP GPRS Session Management Statistics",
602         gsm_a_dtap_msg_sm_strings);
603 }
604
605 static void
606 gsm_a_stat_gtk_dtap_ss_cb(
607     GtkWidget           *w _U_,
608     gpointer            d _U_)
609 {
610     gsm_a_stat_gtk_dtap_cb(w, d, &dlg_dtap_ss,
611         "GSM A-I/F DTAP Supplementary Services Statistics",
612         gsm_a_dtap_msg_ss_strings);
613 }
614
615
616 void
617 register_tap_listener_gtkgsm_a_stat(void)
618 {
619     GString             *err_p;
620
621
622     register_stat_cmd_arg("gsm_a,", gsm_a_stat_gtk_init);
623
624     memset((void *) &stat, 0, sizeof(gsm_a_stat_t));
625
626     err_p =
627         register_tap_listener("gsm_a", &stat, NULL,
628             gsm_a_stat_reset,
629             gsm_a_stat_packet,
630             gsm_a_stat_draw);
631
632     if (err_p != NULL)
633     {
634         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, err_p->str);
635         g_string_free(err_p, TRUE);
636
637         exit(1);
638     }
639
640     register_stat_menu_item("GSM/A-Interface BSSMAP", REGISTER_STAT_GROUP_TELEPHONY,
641         gsm_a_stat_gtk_bssmap_cb, NULL, NULL, NULL);
642
643     register_stat_menu_item("GSM/A-Interface DTAP/Mobility Management", REGISTER_STAT_GROUP_TELEPHONY,
644         gsm_a_stat_gtk_dtap_mm_cb, NULL, NULL, NULL);
645
646     register_stat_menu_item("GSM/A-Interface DTAP/Radio Resource Management", REGISTER_STAT_GROUP_TELEPHONY,
647         gsm_a_stat_gtk_dtap_rr_cb, NULL, NULL, NULL);
648
649     register_stat_menu_item("GSM/A-Interface DTAP/Call Control", REGISTER_STAT_GROUP_TELEPHONY,
650         gsm_a_stat_gtk_dtap_cc_cb, NULL, NULL, NULL);
651
652     register_stat_menu_item("GSM/A-Interface DTAP/GPRS Mobility Management", REGISTER_STAT_GROUP_TELEPHONY,
653         gsm_a_stat_gtk_dtap_gmm_cb, NULL, NULL, NULL);
654
655     register_stat_menu_item("GSM/A-Interface DTAP/Short Message Service", REGISTER_STAT_GROUP_TELEPHONY,
656         gsm_a_stat_gtk_dtap_sms_cb, NULL, NULL, NULL);
657
658     register_stat_menu_item("GSM/A-Interface DTAP/GPRS Session Management", REGISTER_STAT_GROUP_TELEPHONY,
659         gsm_a_stat_gtk_dtap_sm_cb, NULL, NULL, NULL);
660
661     register_stat_menu_item("GSM/A-Interface DTAP/Supplementary Services", REGISTER_STAT_GROUP_TELEPHONY,
662         gsm_a_stat_gtk_dtap_ss_cb, NULL, NULL, NULL);
663 }