aboutsummaryrefslogtreecommitdiffstats
path: root/gtk/rlc_lte_stat_dlg.c
blob: bb45d8cb54bc4e1c4551ba88f6c265eb469f6277 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
/* rlc_lte_stat_dlg.c
 * Copyright 2010 Martin Mathieson
 *
 * $Id$
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */


/* TODO:
   - per-channel graph tap
   - apply top-level filter (e.g. to tap only one sector)
   - common channel stats
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif

#include <string.h>

#include <gtk/gtk.h>

#include "gtk/gtkglobals.h"

#include <epan/packet.h>
#include <epan/packet_info.h>
#include <epan/tap.h>
#include <epan/dissectors/packet-rlc-lte.h>

#include "../register.h"
#include "../simple_dialog.h"
#include "../stat_menu.h"

#include "gtk/dlg_utils.h"
#include "gtk/gui_stat_menu.h"
#include "gtk/gui_utils.h"
#include "gtk/help_dlg.h"
#include "gtk/main.h"

/**********************************************/
/* Table column identifiers and title strings */

enum {
    UEID_COLUMN,
    UL_FRAMES_COLUMN,
    UL_BYTES_COLUMN,
    DL_FRAMES_COLUMN,
    DL_BYTES_COLUMN,
    TABLE_COLUMN,
    NUM_UE_COLUMNS
};

enum {
    CHANNEL_NAME,
    CHANNEL_MODE,
    CHANNEL_UL_FRAMES,
    CHANNEL_UL_BYTES,
    CHANNEL_UL_ACKS,
    CHANNEL_UL_NACKS,
    CHANNEL_DL_FRAMES,
    CHANNEL_DL_BYTES,
    CHANNEL_DL_ACKS,
    CHANNEL_DL_NACKS,
    CHANNEL_TABLE_COLUMN,
    NUM_CHANNEL_COLUMNS
};

static const gchar *ue_titles[] = { "UEId",
                                    "UL Frames", "UL Bytes",
                                    "DL Frames", "DL Bytes"};

static const gchar *channel_titles[] = { "", "Mode",
                                         "UL Frames", "UL Bytes", "UL ACKs", "UL NACKs",
                                         "DL Frames", "DL Bytes", "DL ACKs", "DL NACKs"};

/* Stats kept for one channel */
typedef struct rlc_channel_stats {
    guint8   inUse;
    guint8   rlcMode;
    guint16  channelType;
    guint16  channelId;

    guint32 UL_frames;
    guint32 UL_bytes;
    guint32 DL_frames;
    guint32 DL_bytes;

    guint32 UL_acks;
    guint32 UL_nacks;

    guint32 DL_acks;
    guint32 DL_nacks;

    GtkTreeIter iter;
    gboolean iter_valid;
} rlc_channel_stats;

/* Stats for one UE */
typedef struct rlc_lte_row_data {
    /* Key for matching this row */
    guint16 ueid;

    gboolean is_predefined_data;

    guint32 UL_frames;
    guint32 UL_total_bytes;

    guint32 DL_frames;
    guint32 DL_total_bytes;

    rlc_channel_stats CCCH_stats;
    rlc_channel_stats srb_stats[2];
    rlc_channel_stats drb_stats[32];
} rlc_lte_row_data;


/* One row/UE in the UE table */
typedef struct rlc_lte_ep {
    struct rlc_lte_ep* next;
    struct rlc_lte_row_data stats;
    GtkTreeIter iter;                                         
    gboolean iter_valid;
} rlc_lte_ep_t;


/* Top-level dialog and labels */
static GtkWidget  *rlc_lte_stat_dlg_w = NULL;
static GtkWidget  *rlc_lte_stat_ues_lb = NULL;
static GtkWidget  *rlc_lte_stat_channels_lb = NULL;
static GtkWidget  *rlc_lte_stat_filter_buttons_lb = NULL;

GtkWidget         *ul_filter_bt;
GtkWidget         *dl_filter_bt;
GtkWidget         *uldl_filter_bt;

gboolean          s_show_mac = FALSE;



/* Used to keep track of whole RLC LTE statistics window */
typedef struct rlc_lte_stat_t {
    GtkTreeView   *ue_table;
    rlc_lte_ep_t  *ep_list;
    guint32       total_frames;

    GtkTreeView   *channel_table;
} rlc_lte_stat_t;


static void enable_filter_buttons(guint8 enabled)
{
    gtk_widget_set_sensitive(ul_filter_bt, enabled);
    gtk_widget_set_sensitive(dl_filter_bt, enabled);
    gtk_widget_set_sensitive(uldl_filter_bt, enabled);
}



/* Reset the statistics window */
static void
rlc_lte_stat_reset(void *phs)
{
    rlc_lte_stat_t* rlc_lte_stat = (rlc_lte_stat_t *)phs;
    rlc_lte_ep_t* list = rlc_lte_stat->ep_list;
    gchar title[256];
    GtkListStore *store;

    /* Set the title */
    if (rlc_lte_stat_dlg_w != NULL) {
        g_snprintf(title, sizeof(title), "Wireshark: LTE RLC Traffic Statistics: %s",
                   cf_get_display_name(&cfile));
        gtk_window_set_title(GTK_WINDOW(rlc_lte_stat_dlg_w), title);
    }

    g_snprintf(title, sizeof(title), "0 UEs");
    gtk_frame_set_label(GTK_FRAME(rlc_lte_stat_ues_lb), title);

    rlc_lte_stat->total_frames = 0;

    /* Remove all entries from the UE list */
    store = GTK_LIST_STORE(gtk_tree_view_get_model(rlc_lte_stat->ue_table));
    gtk_list_store_clear(store);

    if (!list) {
        return;
    }

    rlc_lte_stat->ep_list = NULL;
}


/* Allocate a rlc_lte_ep_t struct to store info for new UE */
static rlc_lte_ep_t* alloc_rlc_lte_ep(struct rlc_lte_tap_info *si, packet_info *pinfo _U_)
{
    rlc_lte_ep_t* ep;
    int n;

    if (!si) {
        return NULL;
    }

    if (!(ep = g_malloc(sizeof(rlc_lte_ep_t)))) {
        return NULL;
    }

    /* Copy SI data into ep->stats */
    ep->stats.ueid = si->ueid;

    /* Counts for new UE are all 0 */
    ep->stats.UL_frames = 0;
    ep->stats.DL_frames = 0;
    ep->stats.UL_total_bytes = 0;
    ep->stats.DL_total_bytes = 0;

    memset(&ep->stats.CCCH_stats, 0, sizeof(rlc_channel_stats));
    for (n=0; n < 2; n++) {
        memset(&ep->stats.srb_stats[n], 0, sizeof(rlc_channel_stats));
    }
    for (n=0; n < 32; n++) {
        memset(&ep->stats.drb_stats[n], 0, sizeof(rlc_channel_stats));
    }

    ep->next = NULL;
    ep->iter_valid = FALSE;

    return ep;
}


/* Return string for RLC mode for display */
static const char *print_rlc_channel_mode(guint8 mode)
{
    static char unknown[16];

    switch (mode) {
        case RLC_TM_MODE:  return "TM";
        case RLC_UM_MODE:  return "UM";
        case RLC_AM_MODE:  return "AM";
        case RLC_PREDEF:   return "Predef";

        default:
            g_snprintf(unknown, 32, "Unknown (%u)", mode);
            return unknown;
    }
}


/* Process stat struct for a RLC LTE frame */
static int
rlc_lte_stat_packet(void *phs, packet_info *pinfo, epan_dissect_t *edt _U_,
                    const void *phi)
{
    /* Get reference to stat window instance */
    rlc_lte_stat_t *hs = (rlc_lte_stat_t *)phs;
    rlc_lte_ep_t *tmp = NULL, *te = NULL;
    rlc_channel_stats *channel_stats = NULL;

    /* Cast tap info struct */
    struct rlc_lte_tap_info *si = (struct rlc_lte_tap_info *)phi;

    /* Need this */
    if (!hs) {
        return 0;
    }

    /* Are we ignoring RLC frames that were found in MAC frames, or only those
       that were logged separately? */
    if (!s_show_mac && si->loggedInMACFrame) {
        return 0;
    }

    /* Inc top-level frame count */
    hs->total_frames++;

    /* For per-UE data, must create a new row if none already existing */
    if (!hs->ep_list) {
        /* Allocate new list */
        hs->ep_list = alloc_rlc_lte_ep(si, pinfo);
        /* Make it the first/only entry */
        te = hs->ep_list;
    } else {
        /* Look among existing rows for this UEId */
        for (tmp = hs->ep_list;(tmp != NULL); tmp = tmp->next) {
            if (tmp->stats.ueid == si->ueid) {
                te = tmp;
                break;
            }
        }

        /* Not found among existing, so create a new one anyway */
        if (te == NULL) {
            if ((te = alloc_rlc_lte_ep(si, pinfo))) {
                /* Add new item to end of list */
                rlc_lte_ep_t *p = hs->ep_list;
                while (p->next) {
                    p = p->next;
                }
                p->next = te;
                te->next = NULL;
            }
        }
    }

    /* Really should have a row pointer by now */
    if (!te) {
        return 0;
    }

    /* Update entry with details from si */
    te->stats.ueid = si->ueid;

    /* Top-level traffic stats */
    if (si->direction == DIRECTION_UPLINK) {
        te->stats.UL_frames++;
        te->stats.UL_total_bytes += si->pduLength;
    }
    else {
        te->stats.DL_frames++;
        te->stats.DL_total_bytes += si->pduLength;
    }

    /* Find channel struct */
    switch (si->channelType) {
        case CHANNEL_TYPE_CCCH:
            channel_stats = &te->stats.CCCH_stats;
            break;

        case CHANNEL_TYPE_SRB:
            channel_stats = &te->stats.srb_stats[si->channelId-1];
            break;

        case CHANNEL_TYPE_DRB:
            channel_stats = &te->stats.drb_stats[si->channelId-1];
            break;

        case CHANNEL_TYPE_BCCH:
        case CHANNEL_TYPE_PCCH:
            /* TODO: count these common channels separately? */
            break;
    }

    if (channel_stats != NULL) {
        channel_stats->inUse = TRUE;
        channel_stats->iter_valid = FALSE;
        channel_stats->rlcMode = si->rlcMode;
        channel_stats->channelType = si->channelType;
        channel_stats->channelId = si->channelId;
    }

    if (si->direction == DIRECTION_UPLINK) {
        channel_stats->UL_frames++;
        channel_stats->UL_bytes += si->pduLength;
        channel_stats->UL_nacks += si->noOfNACKs;
        if (si->isControlPDU) {
            channel_stats->UL_acks++;
        }
    }
    else {
        channel_stats->DL_frames++;
        channel_stats->DL_bytes += si->pduLength;
        channel_stats->DL_nacks += si->noOfNACKs;
        if (si->isControlPDU) {
            channel_stats->DL_acks++;
        }
    }

    return 1;
}


/* The channels for any UE would need to be re-added to the list */
static void invalidate_channel_iters(rlc_lte_stat_t *hs)
{
    gint n;
    rlc_lte_ep_t *ep = hs->ep_list;

    while (ep) {
        ep->stats.CCCH_stats.iter_valid = FALSE;
        for (n=0; n < 2; n++) {
            ep->stats.srb_stats[n].iter_valid = FALSE;
        }
        for (n=0; n < 32; n++) {
            ep->stats.drb_stats[n].iter_valid = FALSE;
        }

        ep = ep->next;
    }
}


/* Draw the channels table according to the current UE selection */
static void
rlc_lte_channels(rlc_lte_ep_t *rlc_stat_ep, rlc_lte_stat_t *hs)
{
    GtkListStore *channels_store = GTK_LIST_STORE(gtk_tree_view_get_model(hs->channel_table));
    rlc_channel_stats *channel_stats;
    char buff[32];
    int n;

    /* Clear any existing rows */
    gtk_list_store_clear(channels_store);
    invalidate_channel_iters(hs);

    if (rlc_stat_ep == NULL) {
        return;
    }

    /* Add one row for each channel */

    /* CCCH */
    channel_stats = &rlc_stat_ep->stats.CCCH_stats;
    if (channel_stats->inUse) {

        if (!channel_stats->iter_valid) {
            /* Add to list control if not drawn this UE before */
            gtk_list_store_append(channels_store, &channel_stats->iter);
            channel_stats->iter_valid = TRUE;
        }

        /* Set each column for this row */
        gtk_list_store_set(channels_store, &channel_stats->iter,
                           CHANNEL_NAME, "CCCH",
                           CHANNEL_MODE, print_rlc_channel_mode(channel_stats->rlcMode),
                           CHANNEL_UL_FRAMES, channel_stats->UL_frames,
                           CHANNEL_UL_BYTES, channel_stats->UL_bytes,
                           CHANNEL_DL_FRAMES, channel_stats->DL_frames,
                           CHANNEL_DL_BYTES, channel_stats->DL_bytes,
                           CHANNEL_TABLE_COLUMN, channel_stats,
                           -1);
    }


    /* SRB */
    for (n=0; n < 2; n++) {
        channel_stats = &rlc_stat_ep->stats.srb_stats[n];
        if (channel_stats->inUse) {

            if (!channel_stats->iter_valid) {
                /* Add to list control if not drawn this UE before */
                gtk_list_store_append(channels_store, &channel_stats->iter);
                channel_stats->iter_valid = TRUE;
            }

            g_snprintf(buff, 32, "SRB-%u", n+1);

            /* Set each column for this row */
            gtk_list_store_set(channels_store, &channel_stats->iter,
                               CHANNEL_NAME, buff,
                               CHANNEL_MODE, print_rlc_channel_mode(channel_stats->rlcMode),
                               CHANNEL_UL_FRAMES, channel_stats->UL_frames,
                               CHANNEL_UL_BYTES, channel_stats->UL_bytes,
                               CHANNEL_UL_ACKS, channel_stats->UL_acks,
                               CHANNEL_UL_NACKS, channel_stats->UL_nacks,
                               CHANNEL_DL_FRAMES, channel_stats->DL_frames,
                               CHANNEL_DL_BYTES, channel_stats->DL_bytes,
                               CHANNEL_DL_ACKS, channel_stats->DL_acks,
                               CHANNEL_DL_NACKS, channel_stats->DL_nacks,
                               CHANNEL_TABLE_COLUMN, channel_stats,
                               -1);
        }
    }


    /* DRB */
    for (n=0; n < 32; n++) {
        channel_stats = &rlc_stat_ep->stats.drb_stats[n];
        if (channel_stats->inUse) {

            if (!channel_stats->iter_valid) {
                /* Add to list control if not drawn this UE before */
                gtk_list_store_append(channels_store, &channel_stats->iter);
                channel_stats->iter_valid = TRUE;
            }

            g_snprintf(buff, 32, "DRB-%u", n+1);

            /* Set each column for this row */
            gtk_list_store_set(channels_store, &channel_stats->iter,
                               CHANNEL_NAME, buff,
                               CHANNEL_MODE, print_rlc_channel_mode(channel_stats->rlcMode),
                               CHANNEL_UL_FRAMES, channel_stats->UL_frames,
                               CHANNEL_UL_BYTES, channel_stats->UL_bytes,
                               CHANNEL_UL_ACKS, channel_stats->UL_acks,
                               CHANNEL_UL_NACKS, channel_stats->UL_nacks,
                               CHANNEL_DL_FRAMES, channel_stats->DL_frames,
                               CHANNEL_DL_BYTES, channel_stats->DL_bytes,
                               CHANNEL_DL_ACKS, channel_stats->DL_acks,
                               CHANNEL_DL_NACKS, channel_stats->DL_nacks,
                               CHANNEL_TABLE_COLUMN, channel_stats,
                               -1);
        }
    }
}



/* (Re)draw the whole dialog window */
static void
rlc_lte_stat_draw(void *phs)
{
    guint16 number_of_ues = 0;
    gchar title[256];

    /* Look up the statistics window */
    rlc_lte_stat_t *hs = (rlc_lte_stat_t *)phs;
    rlc_lte_ep_t* list = hs->ep_list, *tmp = 0;

    GtkListStore *ues_store;
    GtkTreeSelection *sel;
    GtkTreeModel *model;
    GtkTreeIter iter;


    /* Per-UE table entries */
    ues_store = GTK_LIST_STORE(gtk_tree_view_get_model(hs->ue_table));

    /* Set title that shows how many UEs currently in table */
    for (tmp = list; (tmp!=NULL); tmp=tmp->next, number_of_ues++);
    g_snprintf(title, sizeof(title), "%u UEs", number_of_ues);
    gtk_frame_set_label(GTK_FRAME(rlc_lte_stat_ues_lb), title);

    /* Update title to include number of UEs and frames */
    g_snprintf(title, sizeof(title), "Wireshark: LTE RLC Traffic Statistics: %s (%u UEs, %u frames)",
               cf_get_display_name(&cfile),
               number_of_ues,
               hs->total_frames);
    gtk_window_set_title(GTK_WINDOW(rlc_lte_stat_dlg_w), title);


    /* For each row/UE in the model */
    for (tmp = list; tmp; tmp=tmp->next) {
        if (tmp->iter_valid != TRUE) {
            /* Add to list control if not drawn this UE before */
            gtk_list_store_append(ues_store, &tmp->iter);
            tmp->iter_valid = TRUE;
        }

        /* Set each column for this row */
        gtk_list_store_set(ues_store, &tmp->iter,
                           UEID_COLUMN, tmp->stats.ueid,
                           UL_FRAMES_COLUMN, tmp->stats.UL_frames,
                           UL_BYTES_COLUMN, tmp->stats.UL_total_bytes,
                           DL_FRAMES_COLUMN, tmp->stats.DL_frames,
                           DL_BYTES_COLUMN, tmp->stats.DL_total_bytes,
                           TABLE_COLUMN, tmp,
                           -1);
    }

    /* If there is a UE selected, update its counters in details window */
    sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(hs->ue_table));
    if (gtk_tree_selection_get_selected(sel, &model, &iter)) {
        rlc_lte_ep_t *ep;

        gtk_tree_model_get(model, &iter, TABLE_COLUMN, &ep, -1);
        rlc_lte_channels(ep, hs);
    }
}


/* What to do when a UE list item is selected/unselected */
static void rlc_lte_select_ue_cb(GtkTreeSelection *sel, gpointer data)
{
    rlc_lte_ep_t   *ep;
    GtkTreeModel   *model;
    GtkTreeIter    iter;

    if (gtk_tree_selection_get_selected(sel, &model, &iter)) {
        /* Show details of selected UE */
        gtk_tree_model_get(model, &iter, TABLE_COLUMN, &ep, -1);
        rlc_lte_channels(ep, (rlc_lte_stat_t*)data);
    }
    else {
        rlc_lte_channels(NULL, (rlc_lte_stat_t*)data);
    }

    /* Channel will be deselected */
    enable_filter_buttons(FALSE);
}


/* What to do when a channel list item is selected/unselected */
static void rlc_lte_select_channel_cb(GtkTreeSelection *sel, gpointer data _U_)
{
    rlc_lte_ep_t   *ep;
    GtkTreeModel   *model;
    GtkTreeIter    iter;

    if (gtk_tree_selection_get_selected(sel, &model, &iter)) {
        /* Enable buttons */
        enable_filter_buttons(TRUE);

        /* Show details of selected UE */
        gtk_tree_model_get(model, &iter, TABLE_COLUMN, &ep, -1);
    }
    else {
        /* Disable buttons */
        enable_filter_buttons(FALSE);
    }
}


/* Destroy the stats window */
static void win_destroy_cb(GtkWindow *win _U_, gpointer data)
{
    rlc_lte_stat_t *hs = (rlc_lte_stat_t *)data;

    protect_thread_critical_region();
    remove_tap_listener(hs);
    unprotect_thread_critical_region();

    if (rlc_lte_stat_dlg_w != NULL) {
        window_destroy(rlc_lte_stat_dlg_w);
        rlc_lte_stat_dlg_w = NULL;
    }
    rlc_lte_stat_reset(hs);
    g_free(hs);
}



static void
toggle_show_mac(GtkWidget *widget, gpointer data _U_)
{
    /* Read state */
    s_show_mac = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON (widget));

    /* Retap */
    cf_retap_packets(&cfile);
}


/* Check that a UE / channel is currently selected.  If so, fill in out
   parameters with details of channel.
   Return TRUE if a channel is selected */
static int get_channel_selection(rlc_lte_stat_t *hs,
                                 guint16 *ueid, guint8 *rlcMode,
                                 guint16 *channelType, guint16 *channelId)
{
    GtkTreeModel *model;
    GtkTreeIter iter;

    /* Check UE selection */
    GtkTreeSelection *sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(hs->ue_table));
    if (gtk_tree_selection_get_selected(sel, &model, &iter)) {
        rlc_lte_ep_t *ep;

        gtk_tree_model_get(model, &iter, TABLE_COLUMN, &ep, -1);
        *ueid = ep->stats.ueid;

        /* Check channel selection */
        sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(hs->channel_table));
        if (gtk_tree_selection_get_selected(sel, &model, &iter)) {
            /* Find details of selected channel */
            rlc_channel_stats *channel_stats;
            gtk_tree_model_get(model, &iter, CHANNEL_TABLE_COLUMN, &channel_stats, -1);
            *rlcMode = channel_stats->rlcMode;
            *channelType = channel_stats->channelType;
            *channelId = channel_stats->channelId;
        }
        else {
            return FALSE;
        }
    }
    else {
        return FALSE;
    }

    return TRUE;
}


/* Build and set a display filter to match the given channel settings */
typedef enum ChannelDirection_t {UL_Only, DL_Only, UL_and_DL} ChannelDirection_t;
static void set_channel_filter_expression(guint16  ueid,
                                          guint8   rlcMode,
                                          guint16  channelType,
                                          guint16  channelId,
                                          ChannelDirection_t channelDirection)
{
    static char buffer[256];
    int offset = 0;

    /* Should we exclude MAC frames? */
    if (!s_show_mac) {
        offset += g_snprintf(buffer+offset, 256-offset, "not mac-lte and ");
    }

    /* UEId */
    offset += g_snprintf(buffer+offset, 256-offset, "(rlc-lte.ueid == %u) and ", ueid);
    offset += g_snprintf(buffer+offset, 256-offset, "(rlc-lte.channel-type == %u)", channelType);

    /* Channel-id for srb/drb */
    if ((channelType == CHANNEL_TYPE_SRB) || (channelType == CHANNEL_TYPE_DRB)) {
        offset += g_snprintf(buffer+offset, 256-offset, " and (rlc-lte.channel-id == %u)", channelId);
    }

    /* Direction (also depends upon RLC mode) */
    switch (channelDirection) {
        case UL_Only:
            if (rlcMode == RLC_AM_MODE) {
                offset += g_snprintf(buffer+offset, 256-offset,
                                     " and (rlc-lte.direction == 0 and rlc-lte.am.frame_type == 1) or "
                                          "(rlc-lte.direction == 1 and rlc-lte.am.frame_type == 0)");
            }
            else {
                offset += g_snprintf(buffer+offset, 256-offset, " and (rlc-lte.direction == 0)");
            }
            break;
        case DL_Only:
            if (rlcMode == RLC_AM_MODE) {
                offset += g_snprintf(buffer+offset, 256-offset,
                                     " and (rlc-lte.direction == 1 and rlc-lte.am.frame_type == 1) or "
                                          "(rlc-lte.direction == 0 and rlc-lte.am.frame_type == 0)");
            }
            else {
                offset += g_snprintf(buffer+offset, 256-offset, " and (rlc-lte.direction == 1)");
            }
            break;

        default:
            break;
    }

    /* Set its value to our new string */
    gtk_entry_set_text(GTK_ENTRY(main_display_filter_widget), buffer);

    /* Run the filter */
    main_filter_packets(&cfile, buffer, TRUE);
}

/* Respond to UL filter button being clicked by building and using filter */
static void ul_filter_clicked(GtkWindow *win _U_, rlc_lte_stat_t* hs)
{
    guint16  ueid;
    guint8   rlcMode;
    guint16  channelType;
    guint16  channelId;

    if (!get_channel_selection(hs, &ueid, &rlcMode, &channelType, &channelId)) {
        return;
    }

    set_channel_filter_expression(ueid, rlcMode, channelType, channelId, UL_Only);
}

/* Respond to DL filter button being clicked by building and using filter */
static void dl_filter_clicked(GtkWindow *win _U_, rlc_lte_stat_t* hs)
{
    guint16  ueid;
    guint8   rlcMode;
    guint16  channelType;
    guint16  channelId;

    if (!get_channel_selection(hs, &ueid, &rlcMode, &channelType, &channelId)) {
        return;
    }

    set_channel_filter_expression(ueid, rlcMode, channelType, channelId, DL_Only);
}

/* Respond to UL/DL filter button being clicked by building and using filter */
static void uldl_filter_clicked(GtkWindow *win _U_, rlc_lte_stat_t* hs)
{
    guint16  ueid;
    guint8   rlcMode;
    guint16  channelType;
    guint16  channelId;

    if (!get_channel_selection(hs, &ueid, &rlcMode, &channelType, &channelId)) {
        return;
    }

    set_channel_filter_expression(ueid, rlcMode, channelType, channelId, UL_and_DL);
}


/* Create a new RLC LTE stats dialog */
static void rlc_lte_stat_dlg_create(void)
{
    rlc_lte_stat_t    *hs;
    GString           *error_string;
    GtkWidget         *ues_scrolled_window;
    GtkWidget         *channels_scrolled_window;
    GtkWidget         *bbox;
    GtkWidget         *top_level_vbox;

    GtkWidget         *show_mac_cb;
    GtkWidget         *ues_vb;
    GtkWidget         *channels_vb;
    GtkWidget         *filter_buttons_hb;

    GtkWidget         *close_bt;
    GtkWidget         *help_bt;

    GtkListStore      *store;

    GtkTreeView       *tree_view;
    GtkCellRenderer   *renderer;
    GtkTreeViewColumn *column;
    GtkTreeSelection  *sel;
    gchar title[256];
    gint i;

    /* Create dialog */
    hs = g_malloc(sizeof(rlc_lte_stat_t));
    hs->ep_list = NULL;

    /* Set title */
    g_snprintf(title, sizeof(title), "Wireshark: LTE RLC Statistics: %s",
               cf_get_display_name(&cfile));
    rlc_lte_stat_dlg_w = window_new_with_geom(GTK_WINDOW_TOPLEVEL, title, "LTE RLC Statistics");

    /* Window size */
    gtk_window_set_default_size(GTK_WINDOW(rlc_lte_stat_dlg_w), 750, 300);

    /* Will stack widgets vertically inside dlg */
    top_level_vbox = gtk_vbox_new(FALSE, 3);       /* FALSE = not homogeneous */
    gtk_container_add(GTK_CONTAINER(rlc_lte_stat_dlg_w), top_level_vbox);

    gtk_container_set_border_width(GTK_CONTAINER(top_level_vbox), 6);
    gtk_widget_show(top_level_vbox);

    /**********************************************/
    /* Exclude-MAC checkbox                       */
    show_mac_cb = gtk_check_button_new_with_mnemonic("Show RLC PDUs found inside logged MAC frames");
    gtk_container_add(GTK_CONTAINER(top_level_vbox), show_mac_cb);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(show_mac_cb), FALSE);
    /* TODO: add tooltip */
    g_signal_connect(show_mac_cb, "toggled", G_CALLBACK(toggle_show_mac), hs);


    /**********************************************/
    /* UE List                                    */
    /**********************************************/

    rlc_lte_stat_ues_lb = gtk_frame_new("UE Data (0 UEs)");
    ues_vb = gtk_vbox_new(FALSE, 0);
    gtk_container_add(GTK_CONTAINER(rlc_lte_stat_ues_lb), ues_vb);
    gtk_container_set_border_width(GTK_CONTAINER(ues_vb), 5);

    ues_scrolled_window = scrolled_window_new(NULL, NULL);
    gtk_box_pack_start(GTK_BOX(ues_vb), ues_scrolled_window, TRUE, TRUE, 0);
    gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(ues_scrolled_window),
                                        GTK_SHADOW_IN);

    /* Create the table of UE data */
    store = gtk_list_store_new(NUM_UE_COLUMNS, G_TYPE_INT,
                               G_TYPE_INT, G_TYPE_INT, /* UL */
                               G_TYPE_INT, G_TYPE_INT, /* DL */
                               G_TYPE_POINTER);
    hs->ue_table = GTK_TREE_VIEW(tree_view_new(GTK_TREE_MODEL(store)));
    gtk_container_add(GTK_CONTAINER (ues_scrolled_window), GTK_WIDGET(hs->ue_table));
    g_object_unref(G_OBJECT(store));

    tree_view = hs->ue_table;
    gtk_tree_view_set_headers_visible(tree_view, TRUE);
    gtk_tree_view_set_headers_clickable(tree_view, TRUE);

    /* Create the titles for each column of the per-UE table */
    for (i = 0; i < TABLE_COLUMN; i++) {
        renderer = gtk_cell_renderer_text_new();
        column = gtk_tree_view_column_new_with_attributes(ue_titles[i], renderer,
                                                          "text", i, NULL);
        gtk_tree_view_column_set_sort_column_id(column, i);

        if (i == 0) {
            /* Expand first column (RNTI, which is Key) */
            gtk_tree_view_column_set_expand(column, TRUE);
        } else {
            /* For other columns, set all of the free space to be on the left */
            g_object_set(G_OBJECT(renderer), "xalign", 1.0, NULL);
        }
        gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
        gtk_tree_view_column_set_resizable(column, TRUE);
        gtk_tree_view_append_column(tree_view, column);
    }

    /* Set callback function for selecting a row in the UE table */
    sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(hs->ue_table));
    gtk_tree_selection_set_mode(sel, GTK_SELECTION_SINGLE);
    g_signal_connect(sel, "changed", G_CALLBACK(rlc_lte_select_ue_cb), hs);

    gtk_box_pack_start(GTK_BOX(top_level_vbox), rlc_lte_stat_ues_lb, TRUE, TRUE, 0);


    /**********************************************/
    /* Channels of selected UE                    */
    /**********************************************/
    rlc_lte_stat_channels_lb = gtk_frame_new("Channels of selected UE");

    channels_vb = gtk_vbox_new(FALSE, 6);
    gtk_container_add(GTK_CONTAINER(rlc_lte_stat_channels_lb), channels_vb);
    gtk_container_set_border_width(GTK_CONTAINER(channels_vb), 5);

    channels_scrolled_window = scrolled_window_new(NULL, NULL);
    gtk_box_pack_start(GTK_BOX(channels_vb), channels_scrolled_window, TRUE, TRUE, 0);
    gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(channels_scrolled_window),
                                        GTK_SHADOW_IN);

    /* Create the table of UE data */
    store = gtk_list_store_new(NUM_CHANNEL_COLUMNS,
                               G_TYPE_STRING, G_TYPE_STRING, /* name & type */
                               G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, /* UL */
                               G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, /* DL */
                               G_TYPE_POINTER);
    hs->channel_table = GTK_TREE_VIEW(tree_view_new(GTK_TREE_MODEL(store)));
    gtk_container_add(GTK_CONTAINER (channels_scrolled_window), GTK_WIDGET(hs->channel_table));
    g_object_unref(G_OBJECT(store));

    tree_view = hs->channel_table;
    gtk_tree_view_set_headers_visible(tree_view, TRUE);
    gtk_tree_view_set_headers_clickable(tree_view, TRUE);

    /* Create the titles for each column of the per-UE table */
    for (i = 0; i < CHANNEL_TABLE_COLUMN; i++) {
        renderer = gtk_cell_renderer_text_new();
        column = gtk_tree_view_column_new_with_attributes(channel_titles[i], renderer,
                                                          "text", i, NULL);
        gtk_tree_view_column_set_sort_column_id(column, i);

        if (i == 0) {
            /* Expand first column (Type) */
            gtk_tree_view_column_set_expand(column, TRUE);
        } else {
            /* For other columns, set all of the free space to be on the left */
            g_object_set(G_OBJECT(renderer), "xalign", 1.0, NULL);
        }
        gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
        gtk_tree_view_column_set_resizable(column, TRUE);
        gtk_tree_view_append_column(tree_view, column);
    }

    /* Set callback function for selecting a row in the channel table */
    sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(hs->channel_table));
    gtk_tree_selection_set_mode(sel, GTK_SELECTION_SINGLE);
    g_signal_connect(sel, "changed", G_CALLBACK(rlc_lte_select_channel_cb), hs);

    gtk_box_pack_start(GTK_BOX(top_level_vbox), rlc_lte_stat_channels_lb, TRUE, TRUE, 0);


    /**********************************************/
    /* Channel filter buttons                     */
    /**********************************************/

    rlc_lte_stat_filter_buttons_lb = gtk_frame_new("Filter on selected channel");

    filter_buttons_hb = gtk_hbox_new(FALSE, 6);
    gtk_container_add(GTK_CONTAINER(rlc_lte_stat_filter_buttons_lb), filter_buttons_hb);
    gtk_container_set_border_width(GTK_CONTAINER(filter_buttons_hb), 2);


    /* UL button */
    ul_filter_bt = gtk_button_new_with_label("Set UL display filter for this channel");
    gtk_box_pack_start(GTK_BOX(filter_buttons_hb), ul_filter_bt, TRUE, TRUE, 0);
    g_signal_connect(ul_filter_bt, "clicked", G_CALLBACK(ul_filter_clicked), hs);
    gtk_widget_show(ul_filter_bt);

    dl_filter_bt = gtk_button_new_with_label("Set DL display filter for this channel");
    gtk_box_pack_start(GTK_BOX(filter_buttons_hb), dl_filter_bt, TRUE, TRUE, 0);
    g_signal_connect(dl_filter_bt, "clicked", G_CALLBACK(dl_filter_clicked), hs);
    gtk_widget_show(dl_filter_bt);

    uldl_filter_bt = gtk_button_new_with_label("Set UL / DL display filter for this channel");
    gtk_box_pack_start(GTK_BOX(filter_buttons_hb), uldl_filter_bt, TRUE, TRUE, 0);
    g_signal_connect(uldl_filter_bt, "clicked", G_CALLBACK(uldl_filter_clicked), hs);
    gtk_widget_show(uldl_filter_bt);

    gtk_box_pack_start(GTK_BOX(top_level_vbox), rlc_lte_stat_filter_buttons_lb, TRUE, TRUE, 0);

    enable_filter_buttons(FALSE);

    /**********************************************/
    /* Register the tap listener                  */
    /**********************************************/

    error_string = register_tap_listener("rlc-lte", hs, NULL, 0,
                                         rlc_lte_stat_reset,
                                         rlc_lte_stat_packet,
                                         rlc_lte_stat_draw);
    if (error_string) {
        simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
        g_string_free(error_string, TRUE);
        g_free(hs);
        return;
    }


    /************************************/
    /* Bottom utton row.                */
    /************************************/

    bbox = dlg_button_row_new (GTK_STOCK_CLOSE, GTK_STOCK_HELP, NULL);
    gtk_box_pack_end (GTK_BOX(top_level_vbox), bbox, FALSE, FALSE, 0);

    /* Add the close button */
    close_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
    window_set_cancel_button(rlc_lte_stat_dlg_w, close_bt, window_cancel_button_cb);

    help_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_HELP);
    g_signal_connect(help_bt, "clicked", G_CALLBACK(topic_cb), (gpointer)HELP_STATS_LTE_RLC_TRAFFIC_DIALOG);

    /* Set callbacks */
    g_signal_connect(rlc_lte_stat_dlg_w, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
    g_signal_connect(rlc_lte_stat_dlg_w, "destroy", G_CALLBACK(win_destroy_cb), hs);

    /* Show the window */
    gtk_widget_show_all(rlc_lte_stat_dlg_w);
    window_present(rlc_lte_stat_dlg_w);

    /* Retap */
    cf_retap_packets(&cfile);
    gdk_window_raise(rlc_lte_stat_dlg_w->window);
}


/* Show window, creating if necessary */
static void rlc_lte_stat_launch(GtkWidget *w _U_, gpointer data _U_)
{
    if (rlc_lte_stat_dlg_w) {
        reactivate_window(rlc_lte_stat_dlg_w);
    } else {
        rlc_lte_stat_dlg_create();
    }
}

/* Register this tap listener (need void on own so line register function found) */
void
register_tap_listener_rlc_lte_stat(void)
{
    register_stat_menu_item("_LTE RLC...", REGISTER_STAT_GROUP_TELEPHONY,
                            rlc_lte_stat_launch, NULL, NULL, NULL);
}