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