aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-cp2179.c
blob: c7377ac1cc45291c8112f1d43ce9ef49aa535491 (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
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
/* packet-cp2179.c
 * Routines for Communication Protocol 2179 (CP2179) Dissection
 * By Qiaoyin Yang (qiaoyin[DOT]yang[AT]gmail.com
 * Copyright 2014-2015,Schweitzer Engineering Laboratories
 *
 *
 ************************************************************************************************
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 ************************************************************************************************
CP2179 protocol is a serial based protocol. The 2179 protocol is implemented with minor variations between vendors.
The RTAC implemented the 2179 client supporting a limited function codes and command codes. The RTAC doesn't support
multiple function codes in a single request and the dissector also doesn't support decoding these or corresponding responses.
 * Dissector Notes:
 A brief explanation of how a request and response messages are formulated in 2179 protocol.
 The CP2179 request messages will follow the pattern below:
AA AA BB CC DD DD XX XX .... XX EE EE

A = 16-bit address field. The Most significant 5 bit is the Client address, the 11 bits for RTU address.
B = 8-bit Function code
C = 8-bit Command code
D = 16-bit Number of characters in the data field.
X = data field
E = 16-bit CRC

AA AA BB CC DD EE EE XX XX ... XX FF FF

A = 16-bit address field. The Most significant 5 bit is the Client address, the 11 bits for RTU address.
B = 8-bit Function code
C = 8-bit Status
D = 8-bit Port Status
E = 16-bit Number of characters
X = data field
F = 16-bit CRC
 ************************************************************************************************/

#include "config.h"
#include <epan/packet.h>
#include <epan/conversation.h>
#include <epan/prefs.h>
#include <epan/proto_data.h>

void proto_reg_handoff_cp2179(void);
void proto_register_cp2179(void);

/* CP2179 function codes */
#define BASIC_SCAN                          0x00
#define SCAN_INCLUSIVE                      0x01
#define SCAN_FOR_SPECIAL_CALC               0x03
#define RETRIEVE_TIME_TAGGED_INFOR          0x04   /* not supported */
#define SCAN_BY_TABLE                       0x0A
#define SUPERVISORY_CONTROL                 0x10
#define RTU_CONFIG                          0x20
#define RETURN_RTU_CONFIG                   0x25
#define REPORT_EXCEPTION_DATA               0x0D

/* Function Code 0x00 (Basic Scan) Command codes */
#define SIMPLE_STATUS_DATA                 0x01
#define ALWAYS_RESERVED                    0x02
#define TWO_BIT_STATUS                     0x04
#define ANALOG_16_BIT                      0x08
#define ACCUMULATOR_16_BIT                 0x40

/* Function Code 0x03 (Special Calc) Command Codes */
#define SPECIAL_CALC_RANGE                 0x00
#define SPECIAL_CALC_ALL                   0x80

/* Function Code 0x10 (Supervisory Control) Command Codes */
#define SBO_SELECT_OPEN                    0x10
#define SBO_SELECT_CLOSE                   0x11
#define SBO_OPERATE                        0x20

/* Function Code 0x20 (RTU Control) Command Codes */
#define INIT_RTU_CONFIGURATION             0x00
#define RESET_ACCUMULATOR                  0x11

/* packet type */
#define BASIC_SCAN_QUERY_PACKET            1
#define BASIC_SCAN_RESPONSE_PACKET         2
#define SPECIAL_CALC_REQUEST_ALL           3
#define SPECIAL_CALC_RESPONSE_ALL          4
#define SPECIAL_CALC_REQUEST_RANGE         5
#define SPECIAL_CALC_RESPONSE_RANGE        6
#define SCAN_INCLUSIVE_16_ANALOG_REQUEST   7
#define SCAN_INCLUSIVE_16_ANALOG_RESPONSE  8
#define SBO_SELECT_REQUEST                 9
#define SBO_SELECT_RESPONSE                10
#define SBO_OPERATE_REQUEST                11
#define SBO_OPERATE_RESPONSE               12
#define INIT_RTU_REQUEST                   13
#define INIT_RTU_RESPONSE                  14
#define RESET_ACC_REQUEST                  15
#define RESET_ACC_RESPONSE                 16
#define SPECIAL_CALC_RESPONSE              17

/* packet length */
#define CP2179_MIN_LENGTH                  7
#define RESPONSE_HEADER_SIZE               7  /*includes addr, addr, function, status, port status, number of characters */
#define BASIC_SCAN_REQ_LEN                 8
#define SPECIAL_CALC_REQ_ALL_LEN           8
#define SBO_OPERATE_REQ_LEN                8
#define SPECIAL_CALC_REQ_RANGE_LEN         9
#define SBO_SELECT_REQ_LEN                 9
#define SBO_OPERATE_REPLY_LEN              9
#define SBO_SELECT_REPLY_LEN               10

static gboolean cp2179_telnet_clean = TRUE;


/* Packet type Lookup */
static const value_string cp2179_packettype_vals[] = {
{BASIC_SCAN_QUERY_PACKET,               "Basic Scan Request"},
{BASIC_SCAN_RESPONSE_PACKET,            "Basic Scan Response"},
{SPECIAL_CALC_REQUEST_ALL,              "Special Calc Request All"},
{SPECIAL_CALC_RESPONSE_ALL,             "Special Calc Response All"},
{SPECIAL_CALC_REQUEST_RANGE,            "Special Calc Request a Range"},
{SPECIAL_CALC_RESPONSE_RANGE,           "Special Calc Response a Range"},
{SPECIAL_CALC_RESPONSE,                 "Special Calc Response"},
{SCAN_INCLUSIVE_16_ANALOG_REQUEST,      "Scan Inclusive Request"},
{SCAN_INCLUSIVE_16_ANALOG_RESPONSE,     "Scan Inclusive Response"},
{SBO_SELECT_REQUEST,                    "SBO Select Request"},
{SBO_SELECT_RESPONSE,                   "SBO Select Response"},
{SBO_OPERATE_REQUEST,                   "SBO Operate Request"},
{SBO_OPERATE_RESPONSE,                  "SBO Operate Response"},
{INIT_RTU_REQUEST,                      "INIT RTU Request"},
{INIT_RTU_RESPONSE,                     "INIT RTU Response"},
{RESET_ACC_REQUEST,                     "RESET Accumulator Request"},
{RESET_ACC_RESPONSE,                    "RESET Accumulator Response"},
{-99,                                   "Unknown Function Code"},
{ 0,                                    NULL }

};

static value_string_ext cp2179_packettype_vals_ext = VALUE_STRING_EXT_INIT(cp2179_packettype_vals);

/* List contains request data  */
typedef struct {
    wmem_list_t *bs_request_frame_data;
} cp2179_conversation;


/* Function code Lookup */
static const value_string FunctionCodenames[] = {
{ BASIC_SCAN,                     "Basic Scan" },
{ SCAN_INCLUSIVE,                 "Scan Inclusive"},
{ SCAN_FOR_SPECIAL_CALC,          "Scan Floating Points" },
{ SCAN_BY_TABLE,                  "Scan by Table" },
{ SUPERVISORY_CONTROL,            "Supervisory Control" },
{ RTU_CONFIG,                     "RTU Internal Control" },
{ RETURN_RTU_CONFIG,              "Return RTU Config"},
{ REPORT_EXCEPTION_DATA,          "Report Exception data"},
{ 0,                              NULL }
};

/* Command code Lookup (FC00, FC03, FC10) */
static const value_string cp2179_CommandCodeNames [] = {
{ SIMPLE_STATUS_DATA,             "Simple Status" },
{ ALWAYS_RESERVED,                "Reserved" },
{ TWO_BIT_STATUS,                 "2 Bit Data Status" },
{ ANALOG_16_BIT,                  "16 Bit Analog" },
{ ACCUMULATOR_16_BIT,             "16 Bit Pulsed Accumulator" },
{ SBO_SELECT_OPEN,                "SBO Open" },
{ SBO_SELECT_CLOSE,               "SBO Close" },
{ SBO_OPERATE,                    "SBO Operate" },
{ SPECIAL_CALC_ALL,               "Request All Special Calc Data"},
{ SPECIAL_CALC_RANGE,             "Request a Range of Special Calc"},
{ 0,                              NULL }
};

static value_string_ext cp2179_CommandCodeNames_ext = VALUE_STRING_EXT_INIT(cp2179_CommandCodeNames);

/* Function Code 0x20 Command Code Lookup */
static const value_string cp2179_FC20_CommandCodeNames [] = {
{ INIT_RTU_CONFIGURATION,         "Initialize RTU Config" },
{ RESET_ACCUMULATOR,              "Accumulator Reset" },
{ 0,                              NULL }
};

/* Holds Request information required to later decode a response  */
typedef struct {
   guint32  fnum;  /* frame number */
   guint16  address_word;
   guint8   function_code;
   guint8   commmand_code;
   guint16  numberofcharacters;
   guint8   *requested_points;
} bs_request_frame;


static int proto_cp2179 = -1;

/* Initialize the subtree pointers */
static gint ett_cp2179 = -1;
static gint ett_cp2179_header = -1;
static gint ett_cp2179_addr = -1;
static gint ett_cp2179_fc = -1;
static gint ett_cp2179_data = -1;
static gint ett_cp2179_subdata = -1;

/* Initialize the protocol and registered fields */
static int hf_cp2179_request_frame = -1;
static int hf_cp2179_rtu_address = -1;
static int hf_cp2179_master_address = -1;
static int hf_cp2179_function_code = -1;
static int hf_cp2179_nop_flag = -1;
static int hf_cp2179_rst_flag = -1;
static int hf_cp2179_reserved = -1;
static int hf_cp2179_command_code = -1;
static int hf_cp2179_command_code_fc20 = -1;
static int hf_cp2179_sbo_request_point = -1;
static int hf_cp2179_resetacc_request_point = -1;
static int hf_cp2179_speccalc_request_point = -1;
static int hf_cp2179_scaninc_startreq_point = -1;
static int hf_cp2179_scaninc_stopreq_point = -1;
static int hf_cp2179_number_characters = -1;
static int hf_cp2179_analog_16bit = -1;
static int hf_cp2179_accumulator = -1;
static int hf_cp2179_crc = -1;
/* static int hf_cp2179_data_field = -1; */
static int hf_cp2179_status_byte = -1;
static int hf_cp2179_port_status_byte = -1;
static int hf_cp2179_simplestatusbit  = -1;
static int hf_cp2179_simplestatusbit0 = -1;
static int hf_cp2179_simplestatusbit1 = -1;
static int hf_cp2179_simplestatusbit2 = -1;
static int hf_cp2179_simplestatusbit3 = -1;
static int hf_cp2179_simplestatusbit4 = -1;
static int hf_cp2179_simplestatusbit5 = -1;
static int hf_cp2179_simplestatusbit6 = -1;
static int hf_cp2179_simplestatusbit7 = -1;
static int hf_cp2179_simplestatusbit8 = -1;
static int hf_cp2179_simplestatusbit9 = -1;
static int hf_cp2179_simplestatusbit10 = -1;
static int hf_cp2179_simplestatusbit11 = -1;
static int hf_cp2179_simplestatusbit12 = -1;
static int hf_cp2179_simplestatusbit13 = -1;
static int hf_cp2179_simplestatusbit14 = -1;
static int hf_cp2179_simplestatusbit15 = -1;
static int hf_cp2179_specialcalc       = -1;
static int hf_cp2179_2bitstatus        = -1;
static int hf_cp2179_2bitstatuschg0    = -1;
static int hf_cp2179_2bitstatuschg1    = -1;
static int hf_cp2179_2bitstatuschg2    = -1;
static int hf_cp2179_2bitstatuschg3    = -1;
static int hf_cp2179_2bitstatuschg4    = -1;
static int hf_cp2179_2bitstatuschg5    = -1;
static int hf_cp2179_2bitstatuschg6    = -1;
static int hf_cp2179_2bitstatuschg7    = -1;
static int hf_cp2179_2bitstatusstatus0 = -1;
static int hf_cp2179_2bitstatusstatus1 = -1;
static int hf_cp2179_2bitstatusstatus2 = -1;
static int hf_cp2179_2bitstatusstatus3 = -1;
static int hf_cp2179_2bitstatusstatus4 = -1;
static int hf_cp2179_2bitstatusstatus5 = -1;
static int hf_cp2179_2bitstatusstatus6 = -1;
static int hf_cp2179_2bitstatusstatus7 = -1;

static dissector_handle_t cp2179_handle;

static const int *cp2179_simplestatus_bits[] = {
  &hf_cp2179_simplestatusbit0,
  &hf_cp2179_simplestatusbit1,
  &hf_cp2179_simplestatusbit2,
  &hf_cp2179_simplestatusbit3,
  &hf_cp2179_simplestatusbit4,
  &hf_cp2179_simplestatusbit5,
  &hf_cp2179_simplestatusbit6,
  &hf_cp2179_simplestatusbit7,
  &hf_cp2179_simplestatusbit8,
  &hf_cp2179_simplestatusbit9,
  &hf_cp2179_simplestatusbit10,
  &hf_cp2179_simplestatusbit11,
  &hf_cp2179_simplestatusbit12,
  &hf_cp2179_simplestatusbit13,
  &hf_cp2179_simplestatusbit14,
  &hf_cp2179_simplestatusbit15,
  NULL
};

static const int *cp2179_2bitstatus_bits[] = {
  &hf_cp2179_2bitstatuschg0,
  &hf_cp2179_2bitstatuschg1,
  &hf_cp2179_2bitstatuschg2,
  &hf_cp2179_2bitstatuschg3,
  &hf_cp2179_2bitstatuschg4,
  &hf_cp2179_2bitstatuschg5,
  &hf_cp2179_2bitstatuschg6,
  &hf_cp2179_2bitstatuschg7,
  &hf_cp2179_2bitstatusstatus0,
  &hf_cp2179_2bitstatusstatus1,
  &hf_cp2179_2bitstatusstatus2,
  &hf_cp2179_2bitstatusstatus3,
  &hf_cp2179_2bitstatusstatus4,
  &hf_cp2179_2bitstatusstatus5,
  &hf_cp2179_2bitstatusstatus6,
  &hf_cp2179_2bitstatusstatus7,
  NULL
};


/**********************************************************************************************************/
/* Clean all instances of 0xFFFF from Telnet payload to compensate for IAC control code (replace w/ 0xFF) */
/* Function Duplicated from packet-telnet.c (unescape_and_tvbuffify_telnet_option)                        */
/**********************************************************************************************************/
static tvbuff_t *
clean_telnet_iac(packet_info *pinfo, tvbuff_t *tvb, int offset, int len)
{
  tvbuff_t     *telnet_tvb;
  guint8       *buf;
  const guint8 *spos;
  guint8       *dpos;
  int           skip_byte, len_remaining;

  spos=tvb_get_ptr(tvb, offset, len);
  buf = (guint8 *)wmem_alloc(pinfo->pool, len);
  dpos = buf;
  skip_byte = 0;
  len_remaining = len;
  while(len_remaining > 0){

    /* Only analyze two sequential bytes of source tvb if we have at least two bytes left */
    if (len_remaining > 1) {
        /* If two sequential 0xFF's exist, increment skip_byte counter, decrement  */
        /* len_remaining by 2 and copy a single 0xFF to dest tvb. */
        if((spos[0]==0xff) && (spos[1]==0xff)){
            skip_byte++;
            len_remaining -= 2;
            *(dpos++) = 0xff;
            spos += 2;
            continue;
        }
    }
    /* If we only have a single byte left, or there were no sequential 0xFF's, copy byte from src tvb to dest tvb */
    *(dpos++) = *(spos++);
    len_remaining--;
  }
  telnet_tvb = tvb_new_child_real_data(tvb, buf, len-skip_byte, len-skip_byte);
  add_new_data_source(pinfo, telnet_tvb, "Processed Telnet Data");

  return telnet_tvb;
}

/******************************************************************************************************/
/* Code to Dissect Request frames */
/******************************************************************************************************/
static int
dissect_request_frame(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo, int offset, guint16 packet_type )
{
/* Set up structures needed to add the protocol subtree and manage it */
    proto_tree *cp2179_proto_tree = NULL;
    proto_tree *cp2179_addr_tree = NULL;
    proto_tree *cp2179_fc_tree = NULL;

    proto_item *cp2179_proto_item = NULL;

    guint8 req_command_code = 0;
    guint8 function_code = 0;

    guint16 address_word = -1;
    guint16 requestnumberofcharacters = 0;

    cp2179_proto_item = proto_tree_add_item(tree, proto_cp2179, tvb, 0, -1, ENC_NA);
    cp2179_proto_tree = proto_item_add_subtree(cp2179_proto_item, ett_cp2179_header);

    /* RTU & Master Address are encoded into a 16-bit word */
    address_word = tvb_get_letohs(tvb, offset);
    cp2179_addr_tree = proto_tree_add_subtree_format(cp2179_proto_tree, tvb, offset, 2, ett_cp2179_addr, NULL,
               "RTU Address: %d, Master Address: %d", (address_word & 0x7FF), ((address_word & 0xF800) >> 11) );

    proto_tree_add_item(cp2179_addr_tree, hf_cp2179_rtu_address, tvb, offset, 2, ENC_LITTLE_ENDIAN);
    proto_tree_add_item(cp2179_addr_tree, hf_cp2179_master_address, tvb, offset, 2, ENC_LITTLE_ENDIAN);
    offset += 2;

    /* Report the function code */
    function_code = tvb_get_guint8(tvb, offset) & 0x3f;
    cp2179_fc_tree = proto_tree_add_subtree_format(cp2179_proto_tree, tvb, offset, 1, ett_cp2179_fc, NULL,
               "Function Code: %s (0x%02x)", val_to_str_const(function_code, FunctionCodenames, "Unknown Function Code"), function_code);

    proto_tree_add_item(cp2179_fc_tree, hf_cp2179_function_code, tvb, offset, 1, ENC_LITTLE_ENDIAN);
    proto_tree_add_item(cp2179_fc_tree, hf_cp2179_reserved, tvb, offset, 1, ENC_LITTLE_ENDIAN);
    offset += 1;

    /* Because the function code basic scan for simple status and function code Internal Control for INIT RTU have the same
       command code. If the packet type is a INIT RTU request, interpret the command code as INIT RTU, or else report it as
       basic scan simple status command code.*/
    switch(packet_type)
    {
    case INIT_RTU_REQUEST:
    case RESET_ACC_REQUEST:
        proto_tree_add_item(cp2179_proto_tree,  hf_cp2179_command_code_fc20 , tvb, offset, 1, ENC_LITTLE_ENDIAN);
        break;

    case BASIC_SCAN_QUERY_PACKET:
    case SCAN_INCLUSIVE_16_ANALOG_REQUEST:
        req_command_code = tvb_get_guint8(tvb, offset);
        /* Update Info column with useful information of Command Code Type */
        col_append_fstr(pinfo->cinfo, COL_INFO, " [ %s ]", val_to_str_ext_const(req_command_code, &cp2179_CommandCodeNames_ext, "Unknown Command Code"));
        proto_tree_add_item(cp2179_proto_tree, hf_cp2179_command_code, tvb, offset, 1, ENC_LITTLE_ENDIAN);
        break;

    default:
        proto_tree_add_item(cp2179_proto_tree, hf_cp2179_command_code, tvb, offset, 1, ENC_LITTLE_ENDIAN);
        break;
    }
    offset += 1;

    requestnumberofcharacters = tvb_get_letohs(tvb, 4);
    proto_tree_add_item(cp2179_proto_tree, hf_cp2179_number_characters, tvb, offset, 2, ENC_LITTLE_ENDIAN);
    offset += 2;

    /*If request number is greater than 0, data field in the request is not empty, we need to report the data field*/
    if ( requestnumberofcharacters > 0 ){
        /*Depends on the packet type, the data field should be dissect differently*/
        switch (packet_type)
        {
            case SBO_SELECT_REQUEST:
                proto_tree_add_item(cp2179_proto_tree, hf_cp2179_sbo_request_point, tvb, offset, 1, ENC_LITTLE_ENDIAN);
                offset += 1;
                break;

            /*Reset Accumulator request data field will always only has 1 byte. The Sequence ID of the Accumulator that it wants to reset*/
            case RESET_ACC_REQUEST:
                proto_tree_add_item(cp2179_proto_tree, hf_cp2179_resetacc_request_point, tvb, offset, 1, ENC_LITTLE_ENDIAN);
                offset += 1;
                break;

            /* A special calculation that requests for a range of points will have a list of sequence ID of the Special Calculation points */
            case SPECIAL_CALC_REQUEST_RANGE:
                do{
                    proto_tree_add_item(cp2179_proto_tree, hf_cp2179_speccalc_request_point, tvb, offset, 1, ENC_LITTLE_ENDIAN);
                    offset += 1;
                }while(tvb_reported_length_remaining(tvb, offset) > 2);
                break;

            /*Scan Inclusive will have a starting sequence ID and a ending sequence ID in the data field.*/
            case SCAN_INCLUSIVE_16_ANALOG_REQUEST:
                do{
                    proto_tree_add_item(cp2179_proto_tree, hf_cp2179_scaninc_startreq_point, tvb, offset, 1, ENC_LITTLE_ENDIAN);
                    proto_tree_add_item(cp2179_proto_tree, hf_cp2179_scaninc_stopreq_point, tvb, offset+1, 1, ENC_LITTLE_ENDIAN);
                    offset += 2;
                }while(tvb_reported_length_remaining(tvb, offset) > 2);
                break;
        }
    }
        /*report the last two bytes as CRC in the request*/
    proto_tree_add_item(cp2179_proto_tree, hf_cp2179_crc, tvb, offset, 2, ENC_LITTLE_ENDIAN);

    return tvb_reported_length(tvb);
}


/******************************************************************************************************/
/* Code to dissect Response frames  */
/******************************************************************************************************/
static int
dissect_bs_response_frame(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo, int offset, guint16 packet_type)
{
    /* Set up structures needed to add the protocol subtree and manage it */
    proto_item *bs_response_item = NULL;
    proto_item *cp2179_proto_item = NULL;
    proto_item *cp2179_subdata_item = NULL;

    proto_tree *cp2179_proto_tree = NULL;
    proto_tree *cp2179_addr_tree = NULL;
    proto_tree *cp2179_fc_tree = NULL;
    proto_tree *cp2179_data_tree = NULL;

    cp2179_conversation  *conv;
    guint32 req_frame_num;
    guint16 req_address_word;
    guint8  req_command_code;
    gboolean request_found = FALSE;
    bs_request_frame *request_data;

    gint analogtestvalue = 0;
    gint analog16_num = 0;
    gint point_num = 0;

    guint function_code;
    guint simplestatusseq = 0x30;

    guint16 address_word = 0;
    guint16 numberofcharacters = -1;

    gfloat specialcalvalue = 0;

    cp2179_proto_item = proto_tree_add_item(tree, proto_cp2179, tvb, 0, -1, ENC_NA);
    cp2179_proto_tree = proto_item_add_subtree(cp2179_proto_item, ett_cp2179_header);

    /* RTU & Master Address are encoded into a 16-bit word */
    address_word = tvb_get_letohs(tvb, offset);

    cp2179_addr_tree = proto_tree_add_subtree_format(cp2179_proto_tree, tvb, offset, 2, ett_cp2179_addr, NULL,
                   "RTU Address: %d, Master Address: %d", (address_word & 0x7FF), ((address_word & 0xF800) >> 11) );

    proto_tree_add_item(cp2179_addr_tree, hf_cp2179_rtu_address, tvb, 0, 2, ENC_LITTLE_ENDIAN);
    proto_tree_add_item(cp2179_addr_tree, hf_cp2179_master_address, tvb, 0, 2, ENC_LITTLE_ENDIAN);
    offset += 2;

    /*The response always echos the function code in request, except when the RTU can't perform the required function.
    It may set the NOP or RST bit. Bit 0 to bit 5 is the field for function codes. Bit 6 is NOP bit. Bit 7 is RST bit. */
    function_code = tvb_get_guint8(tvb, offset) & 0x3f;

    cp2179_fc_tree = proto_tree_add_subtree_format(cp2179_proto_tree, tvb, offset, 1, ett_cp2179_fc, NULL,
               "Function Code: %s (0x%02x)", val_to_str_const(function_code, FunctionCodenames, "Unknown Function Code"), function_code);

    proto_tree_add_item(cp2179_fc_tree, hf_cp2179_function_code, tvb, offset, 1, ENC_LITTLE_ENDIAN);
    proto_tree_add_item(cp2179_fc_tree, hf_cp2179_nop_flag, tvb, offset, 1, ENC_LITTLE_ENDIAN);
    proto_tree_add_item(cp2179_fc_tree, hf_cp2179_rst_flag, tvb, offset, 1, ENC_LITTLE_ENDIAN);

    offset += 1;

    /* Status Byte & Port Status */
    proto_tree_add_item(cp2179_proto_tree, hf_cp2179_status_byte, tvb, offset, 1, ENC_LITTLE_ENDIAN);
    offset += 1;
    proto_tree_add_item(cp2179_proto_tree, hf_cp2179_port_status_byte, tvb, offset, 1, ENC_LITTLE_ENDIAN);
    offset += 1;

    /* Number of characters */
    numberofcharacters = tvb_get_letohs(tvb, 5);
    proto_tree_add_item(cp2179_proto_tree, hf_cp2179_number_characters, tvb, offset, 2, ENC_LITTLE_ENDIAN);
    offset += 2;

    /* get the converstation data */
    conv = (cp2179_conversation *)p_get_proto_data(wmem_file_scope(), pinfo, proto_cp2179, 0);

    if (conv) {
        wmem_list_frame_t *frame = wmem_list_head(conv->bs_request_frame_data);
        /* Cycle through all logged instances of request frames, looking for request frame number that occurred immediately
           prior to current frame number that has a matching address word */
        while (frame && !request_found) {
            request_data = (bs_request_frame *)wmem_list_frame_data(frame);
            req_frame_num = request_data->fnum;
            req_command_code = request_data->commmand_code;
            req_address_word = request_data->address_word;
                if ((pinfo->num > req_frame_num) && (req_address_word == address_word)) {
                    bs_response_item = proto_tree_add_uint(cp2179_proto_tree, hf_cp2179_request_frame, tvb, 0, 0, req_frame_num);
                    PROTO_ITEM_SET_GENERATED(bs_response_item);
                    request_found = TRUE;
                }
                frame = wmem_list_frame_next(frame);
        }

        if (request_found)
        {
            switch (packet_type)
            {
                case SBO_SELECT_RESPONSE:
                case SBO_OPERATE_RESPONSE:
                case RESET_ACC_RESPONSE:
                case INIT_RTU_RESPONSE:

                    if ( numberofcharacters > 0 ){
                        /*Based on the packet type, change the displayed messages*/
                        if ( packet_type == SBO_SELECT_RESPONSE ){
                            proto_tree_add_item(cp2179_proto_tree, hf_cp2179_sbo_request_point, tvb, offset, 1, ENC_LITTLE_ENDIAN);
                            offset += 1;
                        }
                        if ( packet_type == RESET_ACC_RESPONSE ){
                            proto_tree_add_item(cp2179_proto_tree, hf_cp2179_resetacc_request_point, tvb, offset, 1, ENC_LITTLE_ENDIAN);
                            offset += 1;
                        }
                    }
                    break;

                case SPECIAL_CALC_RESPONSE:
                    /* Based on the command code from the corresponding request, dissect the data field differently.
                       If required a range of Special calculation, display the requested sequence ID and corresponding
                       values. The requested sequence number is obtained from the previous request frame.  */
                    cp2179_data_tree = proto_tree_add_subtree(cp2179_proto_tree, tvb, offset, numberofcharacters, ett_cp2179_data, NULL, "CP2179 Data Field");

                    if (req_command_code == SPECIAL_CALC_ALL ){
                        do{
                            specialcalvalue = tvb_get_letohieee_float(tvb, offset );
                            proto_tree_add_float_format(cp2179_data_tree, hf_cp2179_specialcalc, tvb, offset, 4, specialcalvalue,
                                "Special Calculation %u : %f", point_num, specialcalvalue);
                            point_num += 1;
                            offset += 4;
                        }while(tvb_reported_length_remaining(tvb, offset) > 2);
                    }
                    /*If it request all the special calculation data, dissect all of them and associated a sequence ID with it.*/
                    else if (req_command_code == SPECIAL_CALC_RANGE ){
                        do{
                            specialcalvalue = tvb_get_letohieee_float(tvb, offset );
                            proto_tree_add_float_format(cp2179_data_tree, hf_cp2179_specialcalc, tvb, offset, 4, specialcalvalue,
                                "Special Calculation %u : %f",  request_data->requested_points[point_num], specialcalvalue);
                            point_num += 1;
                            offset += 4;
                        }while(tvb_reported_length_remaining(tvb, offset) > 2);
                    }
                    break;

                case SCAN_INCLUSIVE_16_ANALOG_RESPONSE:

                    cp2179_data_tree = proto_tree_add_subtree(cp2179_proto_tree, tvb, offset, numberofcharacters, ett_cp2179_data, NULL, "CP2179 Data Field");

                    /* Update Info column with useful information of Command Code Type */
                    col_append_fstr(pinfo->cinfo, COL_INFO, " [ %s ]", val_to_str_ext_const(req_command_code, &cp2179_CommandCodeNames_ext, "Unknown Command Code"));

                    /*Report the values of the requested SCAN inclusive data. To figure out which sequence ID the values in the response associated with,
                    we read the bs_request_frame information and show the corresponding sequence ID of the data in response frame.*/
                    do{
                        analogtestvalue = (gint16)tvb_get_letohs(tvb, offset);
                        proto_tree_add_uint_format(cp2179_data_tree, hf_cp2179_analog_16bit, tvb, offset, 2, request_data->requested_points[point_num],
                                                   "Analog (16 bit) %u : %i",  request_data->requested_points[point_num], analogtestvalue);
                        point_num += 1;
                        offset += 2;
                    }while(tvb_reported_length_remaining(tvb, offset) > 2);

                    break;

                case BASIC_SCAN_RESPONSE_PACKET:
                {
                    cp2179_data_tree = proto_tree_add_subtree(cp2179_proto_tree, tvb, offset, numberofcharacters, ett_cp2179_data, NULL, "CP2179 Data Field");

                    /* Update Info column with useful information of Command Code Type */
                    col_append_fstr(pinfo->cinfo, COL_INFO, " [ %s ]", val_to_str_ext_const(req_command_code, &cp2179_CommandCodeNames_ext, "Unknown Command Code"));

                    switch (req_command_code)
                    {
                        /* Based the command code from the request frame, we dissect the response data differently.
                           For example, if the request packet has command byte as ANALOG_16_BIT, the
                           the data field in the response should be dissected as 16-bit signed integer(s). */
                        case ACCUMULATOR_16_BIT:
                            do{
                                analogtestvalue = tvb_get_letohs(tvb, offset);
                                proto_tree_add_uint_format(cp2179_data_tree, hf_cp2179_accumulator, tvb, offset, 2, analog16_num,
                                                           "Accumulator %u : %u", analog16_num, analogtestvalue);
                                analog16_num += 1;
                                offset += 2;
                            }while(tvb_reported_length_remaining(tvb, offset) > 2);

                            break;

                        case ANALOG_16_BIT:
                            do{
                                analogtestvalue =(gint16)tvb_get_letohs(tvb, offset);
                                proto_tree_add_uint_format(cp2179_data_tree, hf_cp2179_analog_16bit, tvb, offset, 2, analog16_num,
                                                           "Analog (16 bit) %u : %i", analog16_num, analogtestvalue);
                                analog16_num += 1;
                                offset += 2;
                            }while(tvb_reported_length_remaining(tvb, offset) > 2);

                            break;

                        case SIMPLE_STATUS_DATA:
                            do{
                                cp2179_subdata_item = proto_tree_add_bitmask(cp2179_data_tree, tvb, offset, hf_cp2179_simplestatusbit,
                                                       ett_cp2179_subdata, cp2179_simplestatus_bits, ENC_LITTLE_ENDIAN);
                                proto_item_set_text(cp2179_subdata_item, "Simple Status Point 0x%x", simplestatusseq);

                                simplestatusseq += 1;
                                offset += 2;
                            }while(tvb_reported_length_remaining(tvb, offset) > 2);

                            break;

                        case TWO_BIT_STATUS:
                            do{
                                cp2179_subdata_item = proto_tree_add_bitmask(cp2179_data_tree, tvb, offset, hf_cp2179_2bitstatus,
                                                       ett_cp2179_subdata, cp2179_2bitstatus_bits, ENC_LITTLE_ENDIAN);
                                proto_item_set_text(cp2179_subdata_item, "2 Bit Status Point 0x%x", simplestatusseq);

                                simplestatusseq += 1;
                                offset += 2;
                            }while(tvb_reported_length_remaining(tvb, offset) > 2);

                            break;
                    } /* end of command code switch */

                } /* end of basic scan response switch */

                break;

            } /* end of packet type switch */

            proto_tree_add_item(cp2179_proto_tree, hf_cp2179_crc, tvb, offset, 2, ENC_LITTLE_ENDIAN);

        } /* request found */

    } /* conversation data found */

    if (!request_found) {
        proto_item_append_text(bs_response_item, ", No Request found");
        return 0;
    }

    return tvb_reported_length(tvb);
}

/******************************************************************************************************/
/* Load Request information into bs request struct */
/******************************************************************************************************/
static bs_request_frame* copy_bs_request_frame(tvbuff_t *tvb  )
{
 /* Set up structures needed to add the protocol request and use it for dissecting response packet */
    guint offset = 0;
    guint8 idx=0 ;
    bs_request_frame *frame;
    guint16 num_objects=0;

    /* get a new frame and initialize it */
    frame = wmem_new(wmem_file_scope(), bs_request_frame);

    /* update the data within the structure frame */
    frame->address_word = tvb_get_letohs(tvb, offset); offset +=2;
    frame->function_code = tvb_get_guint8(tvb, offset); offset +=1;
    frame->commmand_code = tvb_get_guint8(tvb, offset); offset +=1;
    frame->numberofcharacters = tvb_get_letohs(tvb, offset);offset +=2;

    /*Keep track of the request data field in a request.
      Such as SCAN INCLUSIVE request contains a Start Sequence Number and an Ending Sequence Number. */
    if (frame->function_code == SCAN_INCLUSIVE) {
        guint8 startpt, endpt;
        startpt = tvb_get_guint8(tvb, offset);
        endpt = tvb_get_guint8(tvb, offset+1);
        num_objects = (endpt - startpt) + 1;
        frame->requested_points = (guint8 *)wmem_alloc(wmem_file_scope(), num_objects * sizeof(guint8));

        /* We have a range of 'request' points */
        for (idx = 0; idx < num_objects; idx++) {
            frame->requested_points[idx] = startpt;
            startpt++;
        }
        /* offset += 2; */
    }
    /* Get Details for all Requested Points */
    else {
        num_objects = frame->numberofcharacters;
        frame->requested_points = (guint8 *)wmem_alloc(wmem_file_scope(), num_objects * sizeof(guint8));
        for (idx = 0; idx < num_objects; idx++) {
            frame->requested_points[idx] = tvb_get_guint8(tvb, offset);
            offset += 1;
        }

    }

    return frame;
}



/******************************************************************************************************/
/* Classify the different packet type  */
/******************************************************************************************************/
static int
classify_packet_type(tvbuff_t *tvb)
{
    int packet_type = -1;
    guint8 function_code;
    guint8 command_code;
    guint16 requestnumberofcharacters = 0;
    guint16 responsenumberofcharacters = 0;
    guint16 packet_length = 0;


    packet_length = tvb_reported_length(tvb);
    /*Get function code*/
    function_code = tvb_get_guint8(tvb, 2);
    /*Get command codes */
    command_code = tvb_get_guint8(tvb, 3);
    /* Get number of characters */
    requestnumberofcharacters = tvb_get_letohs(tvb, 4);
    responsenumberofcharacters = tvb_get_letohs(tvb, 5);

    /*The response always echos the function code in request, except when the RTU can't perform the required function.
    It may set the NOP or RST bit. Bit 0 to bit 5 is the field for function codes. Bit 6 is NOP bit. Bit 7 is RST bit. */

    /*Remove NOP and RST bit*/
    function_code = function_code & 0x3f ;

    /*2179 protocol frames doesn't have data tells you whether it is a request or a response. We will decide what packet type is based on
    multiple factors, function code, command code, the length of the packet*/
    switch (function_code ){
        case BASIC_SCAN:
            /*Basic scan request message, the number of characters is always 0 and length is fixed*/
            if ( (requestnumberofcharacters == 0) && (packet_length == BASIC_SCAN_REQ_LEN) ) {
                packet_type = BASIC_SCAN_QUERY_PACKET ; /* supported */
            }
            else if ( (responsenumberofcharacters > 0) && (packet_length > BASIC_SCAN_REQ_LEN) ) {
                packet_type = BASIC_SCAN_RESPONSE_PACKET; /* supported */
            }

            break;

        case SUPERVISORY_CONTROL:
            /*SBO select request messages always has number of characters equals to 1 and SBO length is fixed*/
            if ( (requestnumberofcharacters == 1) && (packet_length == SBO_SELECT_REQ_LEN) ) {
                packet_type = SBO_SELECT_REQUEST; /* supported */
            }
            /*SBO select response always has number of characters equals to 1 and SBO length is fixed. */
            else if ( (responsenumberofcharacters == 1) && (packet_length == SBO_SELECT_REPLY_LEN) ) {
                packet_type = SBO_SELECT_RESPONSE; /* supported */
            }
            /*SBO operate request always has number of characters as 0 */
            else if (requestnumberofcharacters == 0) {
                if ( (packet_length == SBO_OPERATE_REQ_LEN) && (command_code == SBO_OPERATE) ) {
                    packet_type = SBO_OPERATE_REQUEST; /* supported */
                }
            }
            /*SBO operate response always has number of characters as 0 */
            else if (responsenumberofcharacters == 0) {
                if (packet_length == SBO_OPERATE_REPLY_LEN) {
                    packet_type = SBO_OPERATE_RESPONSE; /* supported */
                }
            }

            break;

        case SCAN_FOR_SPECIAL_CALC:
            /*Scan for special cal has to command code associated with it, requests all special calculation data or a range of it */
            if ( (requestnumberofcharacters == 0) && (command_code == SPECIAL_CALC_ALL ) ) {
                packet_type = SPECIAL_CALC_REQUEST_ALL; /* supported */
            }
            else if ( (requestnumberofcharacters > 0) && (command_code == SPECIAL_CALC_RANGE ) ) {
                packet_type = SPECIAL_CALC_REQUEST_RANGE; /* supported */
            }
            /*If a packet has SCAN_FOR_SPECIAL_CAL as function code and it is not a request, then it is a response */
            else if ( (responsenumberofcharacters > 0) && (packet_length == (responsenumberofcharacters + 9) ) ) {
                packet_type = SPECIAL_CALC_RESPONSE; /* supported */
            }

            break;
        /*Scan Inclusive request always has request number of characters equals to 2 and a fixed command code */
        case SCAN_INCLUSIVE:
            /*If a packet has SCAN Inclusive function code and it is not a request, then it is a SCAN Inclusive response*/
            if ( (responsenumberofcharacters > 0) ) {
                packet_type = SCAN_INCLUSIVE_16_ANALOG_RESPONSE; /* supported */
            }

            if( (command_code == ANALOG_16_BIT) && (requestnumberofcharacters == 2) ) {
                packet_type = SCAN_INCLUSIVE_16_ANALOG_REQUEST; /* supported */
            }

            break;

        case RTU_CONFIG:
            if (responsenumberofcharacters == 0) {
                packet_type = INIT_RTU_RESPONSE;
            }
            if ( (requestnumberofcharacters == 0) && (command_code == INIT_RTU_CONFIGURATION) ) {
                packet_type = INIT_RTU_REQUEST;
            }

            if (responsenumberofcharacters == 1) {
                packet_type = RESET_ACC_RESPONSE;
            }
            if ( (requestnumberofcharacters == 1) && (command_code == RESET_ACCUMULATOR) ) {
                packet_type = RESET_ACC_REQUEST;
            }
            break;
        default :
            packet_type = -99;
            break;
    }

    return packet_type;
}



/******************************************************************************************************/
/* Code to dissect CP2179 protocol packets */
/******************************************************************************************************/
static int
dissect_cp2179_pdu(tvbuff_t *cp2179_tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
    int offset = 0;
    gint16 packet_type;
    col_set_str(pinfo->cinfo, COL_PROTOCOL, "CP2179");
    col_clear(pinfo->cinfo,COL_INFO);

    packet_type = classify_packet_type(cp2179_tvb);
    /* set information for Information column for CP2179 */
    col_add_fstr(pinfo->cinfo, COL_INFO, "%s", val_to_str_ext_const(packet_type, &cp2179_packettype_vals_ext, "Unknown Packet Type"));

    if (!pinfo->fd->flags.visited){
        conversation_t       *conversation = NULL;
        cp2179_conversation      *bs_conv_data = NULL;

        /* Find a conversation, create a new if no one exists */
        conversation = find_or_create_conversation(pinfo);
        bs_conv_data = (cp2179_conversation *)conversation_get_proto_data(conversation, proto_cp2179);

        if (bs_conv_data == NULL){
           bs_conv_data = wmem_new(wmem_file_scope(), cp2179_conversation);
           bs_conv_data->bs_request_frame_data = wmem_list_new(wmem_file_scope());
           conversation_add_proto_data(conversation, proto_cp2179, (void *)bs_conv_data);
        }

        p_add_proto_data(wmem_file_scope(), pinfo, proto_cp2179, 0, bs_conv_data);

        if ((packet_type == BASIC_SCAN_QUERY_PACKET) || (packet_type == SBO_SELECT_REQUEST)
           ||(packet_type == SPECIAL_CALC_REQUEST_ALL)||(packet_type == SBO_OPERATE_REQUEST)
           ||(packet_type == SPECIAL_CALC_REQUEST_RANGE)||(packet_type == INIT_RTU_REQUEST)
           ||(packet_type == RESET_ACC_REQUEST)||(packet_type == SCAN_INCLUSIVE_16_ANALOG_REQUEST)) {

            /*fill the bs request frame. It holds the request information.*/
            bs_request_frame    *frame_ptr = NULL;
            frame_ptr = copy_bs_request_frame(cp2179_tvb);

            /*also hold the current frame number*/
            frame_ptr->fnum = pinfo->num;
            wmem_list_prepend(bs_conv_data->bs_request_frame_data, frame_ptr);
        }
    } /* !visited */

    if (tvb_reported_length_remaining(cp2179_tvb, offset) > 0){
        switch (packet_type){
            case BASIC_SCAN_QUERY_PACKET:
            case SBO_SELECT_REQUEST:
            case SBO_OPERATE_REQUEST:
            case SPECIAL_CALC_REQUEST_ALL:
            case SPECIAL_CALC_REQUEST_RANGE:
            case SCAN_INCLUSIVE_16_ANALOG_REQUEST:
            case RESET_ACC_REQUEST:
            case INIT_RTU_REQUEST:
                dissect_request_frame(cp2179_tvb, tree, pinfo, offset, packet_type);
                break;

            case BASIC_SCAN_RESPONSE_PACKET:
            case SBO_SELECT_RESPONSE:
            case SBO_OPERATE_RESPONSE:
            case SPECIAL_CALC_RESPONSE:
            case SCAN_INCLUSIVE_16_ANALOG_RESPONSE:
            case INIT_RTU_RESPONSE:
            case RESET_ACC_RESPONSE:
                dissect_bs_response_frame(cp2179_tvb, tree, pinfo, offset, packet_type);
                break;
            default:
                break;
        } /* packet type */
    } /* length remaining */

    return tvb_reported_length(cp2179_tvb);
}


/******************************************************************************************************/
/* Dissect (and possibly Re-assemble) CP2179 protocol payload data */
/******************************************************************************************************/
static int
dissect_cp2179(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
{
    tvbuff_t *cp2179_tvb;
    gint length = tvb_reported_length(tvb);

   /* Check for the packet length, a 2179 Message is at least 7 byte long*/
    if(length < CP2179_MIN_LENGTH){
        return 0;
    }

    if((pinfo->srcport) && cp2179_telnet_clean){
        cp2179_tvb = clean_telnet_iac(pinfo, tvb, 0, length);
    }
    else{
        /* cp2179_tvb = tvb_new_subset_length_caplen( tvb, 0, length, length); */
        cp2179_tvb = tvb_new_subset_length( tvb, 0, length);
    }

    dissect_cp2179_pdu(cp2179_tvb, pinfo, tree, data);

    return length;
}

void
proto_register_cp2179(void)
{
    static hf_register_info hf[] =
    {
        { &hf_cp2179_request_frame,
            { "Request Frame", "cp2179.request_frame",
            FT_FRAMENUM, BASE_NONE,
            NULL, 0x0,
            NULL, HFILL }
        },

        { &hf_cp2179_rtu_address,
            { "RTU Address", "cp2179.RTUAddress",
            FT_UINT16, BASE_DEC,
            NULL, 0x7FF,
            NULL, HFILL }
        },

       { &hf_cp2179_master_address,
            { "Master Address", "cp2179.MasterAddress",
            FT_UINT16, BASE_DEC,
            NULL, 0xF800,
            NULL, HFILL }
        },
        { &hf_cp2179_function_code,
            { "Function Code", "cp2179.functioncode",
            FT_UINT8, BASE_HEX,
            VALS(FunctionCodenames), 0x3F,
            NULL, HFILL }
        },

        { &hf_cp2179_nop_flag,
            { "NOP Flag", "cp2179.nop_flag",
            FT_UINT8, BASE_DEC,
            NULL, 0x40,
            NULL, HFILL }
        },

        { &hf_cp2179_rst_flag,
            { "RST Flag", "cp2179.rst_flag",
            FT_UINT8, BASE_DEC,
            NULL, 0x80,
            NULL, HFILL }
        },

        { &hf_cp2179_reserved,
            { "Reserved Bits", "cp2179.Reserved",
            FT_UINT8, BASE_DEC,
            NULL, 0xC0,
            NULL, HFILL }
        },

        { &hf_cp2179_command_code,
            { "Command Code", "cp2179.commandcode",
            FT_UINT8, BASE_HEX,
            VALS(cp2179_CommandCodeNames), 0x0,
            NULL, HFILL }
        },

        { &hf_cp2179_command_code_fc20,
            { "Command Code (FC 0x20)", "cp2179.commandcodeinitrtu",
            FT_UINT8, BASE_HEX,
            VALS(cp2179_FC20_CommandCodeNames), 0x0,
            NULL, HFILL }
        },
       { &hf_cp2179_status_byte,
            { "RTU Status", "cp2179.rtustatus",
            FT_UINT8, BASE_DEC,
            0x0, 0x0,
            NULL, HFILL }
        },
       { &hf_cp2179_port_status_byte,
            { "Port Status", "cp2179.portstatus",
            FT_UINT8, BASE_DEC,
            0x0, 0x0,
            NULL, HFILL }
        },
       { &hf_cp2179_sbo_request_point,
            { "SBO Request Point", "cp2179.sbo_requestpoint",
            FT_UINT8, BASE_DEC,
            0x0, 0x0,
            NULL, HFILL }
        },
       { &hf_cp2179_resetacc_request_point,
            { "Reset Accumulator Request Point", "cp2179.resetacc_requestpoint",
            FT_UINT8, BASE_DEC,
            0x0, 0x0,
            NULL, HFILL }
        },
       { &hf_cp2179_speccalc_request_point,
            { "Special Calc Request Point", "cp2179.speccalc_requestpoint",
            FT_UINT8, BASE_DEC,
            0x0, 0x0,
            NULL, HFILL }
        },
       { &hf_cp2179_scaninc_startreq_point,
            { "Start Request Point", "cp2179.scaninc_startreq_point",
            FT_UINT8, BASE_DEC,
            0x0, 0x0,
            NULL, HFILL }
        },
       { &hf_cp2179_scaninc_stopreq_point,
            { "Stop Request Point", "cp2179.scaninc_stopreq_point",
            FT_UINT8, BASE_DEC,
            0x0, 0x0,
            NULL, HFILL }
        },
      { &hf_cp2179_number_characters,
            { "Number of Characters", "cp2179.numberofcharacters",
            FT_UINT16, BASE_DEC,
            0x0, 0x0,
            NULL, HFILL }
        },
      { &hf_cp2179_crc,
            { "CRC", "cp2179.crc",
            FT_UINT16, BASE_HEX,
            0x0, 0x0,
            NULL, HFILL }
        },
#if 0
      { &hf_cp2179_data_field,
            { "Data Field", "cp2179.datafield",
            FT_UINT8, BASE_DEC,
            0x0, 0x0,
            NULL, HFILL }
        },
#endif
      { &hf_cp2179_accumulator,
            { "Accumulator", "cp2179.accumulator",
            FT_UINT16, BASE_DEC,
            0x0, 0x0,
            NULL, HFILL }
        },

      { &hf_cp2179_specialcalc,
            { "Special Calc", "cp2179.specialcalc",
            FT_FLOAT, BASE_NONE,
            0x0, 0x0,
            NULL, HFILL }
        },

      { &hf_cp2179_analog_16bit,
         { "Analog 16-bit", "cp2179.analogdata",
         FT_UINT16, BASE_DEC,
         0x0, 0x0,
         NULL, HFILL }
        },
      { &hf_cp2179_simplestatusbit,
         { "Simple Status Bit", "cp2179.simplestatusbit",
         FT_UINT16, BASE_HEX,
         NULL, 0x0,
         NULL, HFILL }
      },
      { &hf_cp2179_simplestatusbit0,
         { "Simple Status bit 0", "cp2179.simplestatusbit0",
         FT_BOOLEAN, 16,
         NULL, 0x0001,
         NULL, HFILL }
      },
      { &hf_cp2179_simplestatusbit1,
         { "Simple Status bit 1", "cp2179.simplestatusbit1",
         FT_BOOLEAN, 16,
         NULL, 0x0002,
         NULL, HFILL }
      },
      { &hf_cp2179_simplestatusbit2,
         { "Simple Status bit 2", "cp2179.simplestatusbit2",
         FT_BOOLEAN, 16,
         NULL, 0x0004,
         NULL, HFILL }
      },
      { &hf_cp2179_simplestatusbit3,
         { "Simple Status bit 3", "cp2179.simplestatusbit3",
         FT_BOOLEAN, 16,
         NULL, 0x0008,
         NULL, HFILL }
      },
      { &hf_cp2179_simplestatusbit4,
         { "Simple Status bit 4", "cp2179.simplestatusbit4",
         FT_BOOLEAN, 16,
         NULL, 0x0010,
         NULL, HFILL }
      },

      { &hf_cp2179_simplestatusbit5,
         { "Simple Status bit 5", "cp2179.simplestatusbit5",
         FT_BOOLEAN, 16,
         NULL, 0x0020,
         NULL, HFILL }
      },

      { &hf_cp2179_simplestatusbit6,
         { "Simple Status bit 6", "cp2179.simplestatusbit6",
         FT_BOOLEAN, 16,
         NULL, 0x0040,
         NULL, HFILL }
      },
      { &hf_cp2179_simplestatusbit7,
         { "Simple Status bit 7", "cp2179.simplestatusbit7",
         FT_BOOLEAN, 16,
         NULL, 0x0080,
         NULL, HFILL }
      },
      { &hf_cp2179_simplestatusbit8,
         { "Simple Status bit 8", "cp2179.simplestatusbit8",
         FT_BOOLEAN, 16,
         NULL, 0x0100,
         NULL, HFILL }
      },
      { &hf_cp2179_simplestatusbit9,
         { "Simple Status bit 9", "cp2179.simplestatusbit9",
         FT_BOOLEAN, 16,
         NULL, 0x0200,
         NULL, HFILL }
      },
      { &hf_cp2179_simplestatusbit10,
         { "Simple Status bit 10", "cp2179.simplestatusbit10",
         FT_BOOLEAN, 16,
         NULL, 0x0400,
         NULL, HFILL }
      },
      { &hf_cp2179_simplestatusbit11,
         { "Simple Status bit 11", "cp2179.simplestatusbit11",
         FT_BOOLEAN, 16,
         NULL, 0x0800,
         NULL, HFILL }
      },
      { &hf_cp2179_simplestatusbit12,
         { "Simple Status bit 12", "cp2179.simplestatusbit12",
         FT_BOOLEAN, 16,
         NULL, 0x1000,
         NULL, HFILL }
      },
      { &hf_cp2179_simplestatusbit13,
         { "Simple Status bit 13", "cp2179.simplestatusbit13",
         FT_BOOLEAN, 16,
         NULL, 0x2000,
         NULL, HFILL }
      },

      { &hf_cp2179_simplestatusbit14,
         { "Simple Status bit 14", "cp2179.simplestatusbit14",
         FT_BOOLEAN, 16,
         NULL, 0x4000,
         NULL, HFILL }
      },

      { &hf_cp2179_simplestatusbit15,
         { "Simple Status bit 15", "cp2179.simplestatusbit15",
         FT_BOOLEAN, 16,
         NULL, 0x8000,
         NULL, HFILL }
      },
      { &hf_cp2179_2bitstatus,
         { "2 Bit Status", "cp2179.twobitstatus",
         FT_UINT16, BASE_HEX,
         NULL, 0x0,
         NULL, HFILL }
      },
      { &hf_cp2179_2bitstatuschg0,
         { "2 Bit Status Change 0", "cp2179.twobitstatuschg0",
         FT_BOOLEAN, 16,
         NULL, 0x0001,
         NULL, HFILL }
      },
      { &hf_cp2179_2bitstatuschg1,
         { "2 Bit Status Change 1", "cp2179.twobitstatuschg1",
         FT_BOOLEAN, 16,
         NULL, 0x0002,
         NULL, HFILL }
      },
      { &hf_cp2179_2bitstatuschg2,
         { "2 Bit Status Change 2", "cp2179.twobitstatuschg2",
         FT_BOOLEAN, 16,
         NULL, 0x0004,
         NULL, HFILL }
      },
      { &hf_cp2179_2bitstatuschg3,
         { "2 Bit Status Change 3", "cp2179.twobitstatuschg3",
         FT_BOOLEAN, 16,
         NULL, 0x0008,
         NULL, HFILL }
      },
      { &hf_cp2179_2bitstatuschg4,
         { "2 Bit Status Change 4", "cp2179.twobitstatuschg4",
         FT_BOOLEAN, 16,
         NULL, 0x0010,
         NULL, HFILL }
      },
      { &hf_cp2179_2bitstatuschg5,
         { "2 Bit Status Change 5", "cp2179.twobitstatuschg5",
         FT_BOOLEAN, 16,
         NULL, 0x0020,
         NULL, HFILL }
      },

      { &hf_cp2179_2bitstatuschg6,
         { "2 Bit Status Change 6", "cp2179.twobitstatuschg6",
         FT_BOOLEAN, 16,
         NULL, 0x0040,
         NULL, HFILL }
      },

      { &hf_cp2179_2bitstatuschg7,
         {  "2 Bit Status Change 7", "cp2179.twobitstatuschg7",
         FT_BOOLEAN, 16,
         NULL, 0x0080,
         NULL, HFILL }
      },

      { &hf_cp2179_2bitstatusstatus0,
         { "2 Bit Status bit 0", "cp2179.twobitstatusbit0",
         FT_BOOLEAN, 16,
         NULL, 0x0100,
         NULL, HFILL }
      },
      { &hf_cp2179_2bitstatusstatus1,
         { "2 Bit Status bit 1", "cp2179.twobitstatusbit1",
         FT_BOOLEAN, 16,
         NULL, 0x0200,
         NULL, HFILL }
      },
      { &hf_cp2179_2bitstatusstatus2,
         { "2 Bit Status bit 2", "cp2179.twobitstatusbit2",
         FT_BOOLEAN, 16,
         NULL, 0x0400,
         NULL, HFILL }
      },
      { &hf_cp2179_2bitstatusstatus3,
         { "2 Bit Status bit 3", "cp2179.twobitstatusbit3",
         FT_BOOLEAN, 16,
         NULL, 0x0800,
         NULL, HFILL }
      },
      { &hf_cp2179_2bitstatusstatus4,
         { "2 Bit Status bit 4", "cp2179.twobitstatusbit4",
         FT_BOOLEAN, 16,
         NULL, 0x1000,
         NULL, HFILL }
      },
      { &hf_cp2179_2bitstatusstatus5,
         { "2 Bit Status bit 5", "cp2179.twobitstatusbit5",
         FT_BOOLEAN, 16,
         NULL, 0x2000,
         NULL, HFILL }
      },
      { &hf_cp2179_2bitstatusstatus6,
         { "2 Bit Status bit 6", "cp2179.twobitstatusbit6",
         FT_BOOLEAN, 16,
         NULL, 0x4000,
         NULL, HFILL }
      },
      { &hf_cp2179_2bitstatusstatus7,
         { "2 Bit Status bit 7", "cp2179.twobitstatusbit7",
         FT_BOOLEAN, 16,
         NULL, 0x8000,
         NULL, HFILL }
}
    };

    /* Setup protocol subtree array */
    static gint *ett[] = {
      &ett_cp2179,
      &ett_cp2179_header,
      &ett_cp2179_addr,
      &ett_cp2179_fc,
      &ett_cp2179_data,
      &ett_cp2179_subdata

    };

    module_t *cp2179_module;

    proto_cp2179 = proto_register_protocol ("CP2179 Protocol", "CP2179", "cp2179");
    cp2179_handle = register_dissector("cp2179", dissect_cp2179, proto_cp2179);
    proto_register_field_array(proto_cp2179, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));

    /* Register required preferences for CP2179 Encapsulated-over-TCP decoding */
    cp2179_module = prefs_register_protocol(proto_cp2179, NULL);

    /* Telnet protocol IAC (0xFF) processing; defaults to TRUE to allow Telnet Encapsulated Data */
    prefs_register_bool_preference(cp2179_module, "telnetclean",
                                  "Remove extra 0xFF (IAC) bytes from Telnet-encapsulated data",
                                  "Whether the SEL Protocol dissector should automatically pre-process Telnet data to remove IAC bytes",
                                  &cp2179_telnet_clean);


}


void
proto_reg_handoff_cp2179(void)
{
    dissector_add_for_decode_as_with_preference("tcp.port", cp2179_handle);
    dissector_add_for_decode_as("rtacser.data", cp2179_handle);
}

/*
 * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */