summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-hartip.c
blob: faef1f81db3bef0dc4813ead51d07d15fbbefac9 (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
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
/* packet-hartip.c
 * Routines for HART-IP packet dissection
 * Copyright 2012, Bill Schiller <bill.schiller@emerson.com>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * Copied from packet-mbtcp.c
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <epan/packet.h>
#include <epan/stats_tree.h>
#include <epan/expert.h>
#include <epan/prefs.h>
#include "packet-tcp.h"

void proto_register_hartip(void);
void proto_reg_handoff_hartip(void);

static dissector_handle_t hartip_tcp_handle;
static dissector_handle_t hartip_udp_handle;

static gboolean hartip_desegment  = TRUE;

static int proto_hartip = -1;
static int hf_hartip_hdr_version = -1;
static int hf_hartip_hdr_message_id = -1;
static int hf_hartip_hdr_message_type = -1;
static int hf_hartip_hdr_status = -1;
static int hf_hartip_hdr_transaction_id = -1;
static int hf_hartip_hdr_msg_length = -1;

static int hf_hartip_data = -1;
static int hf_hartip_master_type = -1;
static int hf_hartip_inactivity_close_timer = -1;
static int hf_hartip_error_code = -1;

static int hf_hartip_pt_preambles = -1;
static int hf_hartip_pt_delimiter = -1;
static int hf_hartip_pt_delimiter_frame_type = -1;
static int hf_hartip_pt_delimiter_physical_layer_type = -1;
static int hf_hartip_pt_delimiter_number_of_expansion_bytes = -1;
static int hf_hartip_pt_delimiter_address_type = -1;
static int hf_hartip_pt_short_addr = -1;
static int hf_hartip_pt_long_addr = -1;
static int hf_hartip_pt_expansion_bytes = -1;
static int hf_hartip_pt_command = -1;
static int hf_hartip_pt_length = -1;
static int hf_hartip_pt_response_code = -1;
static int hf_hartip_pt_device_status = -1;
static int hf_hartip_pt_payload = -1;
static int hf_hartip_pt_checksum = -1;

static gint ett_hartip = -1;
static gint ett_hartip_hdr = -1;
static gint ett_hartip_body = -1;
static gint ett_hartip_pt_delimiter = -1;

static expert_field ei_hartip_data_none = EI_INIT;
static expert_field ei_hartip_data_unexpected = EI_INIT;

/* Command 0 response */
static int hf_hartip_pt_rsp_expansion_code = -1;
static int hf_hartip_pt_rsp_expanded_device_type = -1;
static int hf_hartip_pt_rsp_req_min_preambles = -1;
static int hf_hartip_pt_rsp_hart_protocol_major_rev = -1;
static int hf_hartip_pt_rsp_device_rev = -1;
static int hf_hartip_pt_rsp_software_rev = -1;
static int hf_hartip_pt_rsp_hardware_rev_physical_signal = -1;
static int hf_hartip_pt_rsp_flage = -1;
static int hf_hartip_pt_rsp_device_id = -1;
static int hf_hartip_pt_rsp_rsp_min_preambles = -1;
static int hf_hartip_pt_rsp_max_device_variables = -1;
static int hf_hartip_pt_rsp_configuration_change_counter = -1;
static int hf_hartip_pt_rsp_extended_device_status = -1;
static int hf_hartip_pt_rsp_manufacturer_Identification_code = -1;
static int hf_hartip_pt_rsp_private_label = -1;
static int hf_hartip_pt_rsp_device_profile = -1;

/* Command 2 response */
static int hf_hartip_pt_rsp_pv_percent_range = -1;

/* Command 3 response */
static int hf_hartip_pt_rsp_pv_loop_current = -1;
static int hf_hartip_pt_rsp_pv_units = -1;
static int hf_hartip_pt_rsp_pv = -1;
static int hf_hartip_pt_rsp_sv_units = -1;
static int hf_hartip_pt_rsp_sv = -1;
static int hf_hartip_pt_rsp_tv_units = -1;
static int hf_hartip_pt_rsp_tv = -1;
static int hf_hartip_pt_rsp_qv_units = -1;
static int hf_hartip_pt_rsp_qv = -1;

/* Command 7 response*/
static int hf_hartip_pt_rsp_loop_current_mode = -1;

/* Command 8 response*/
static int hf_hartip_pt_rsp_primary_var_classify = -1;
static int hf_hartip_pt_rsp_secondary_var_classify = -1;
static int hf_hartip_pt_rsp_tertiary_var_classify = -1;
static int hf_hartip_pt_rsp_quaternary_var_classify = -1;

/* Command 9 response */
static int hf_hartip_pt_rsp_slot0_device_var = -1;
static int hf_hartip_pt_rsp_slot0_device_var_classify = -1;
static int hf_hartip_pt_rsp_slot0_units = -1;
static int hf_hartip_pt_rsp_slot0_device_var_value = -1;
static int hf_hartip_pt_rsp_slot0_device_var_status = -1;

static int hf_hartip_pt_rsp_slot1_device_var = -1;
static int hf_hartip_pt_rsp_slot1_device_var_classify = -1;
static int hf_hartip_pt_rsp_slot1_units = -1;
static int hf_hartip_pt_rsp_slot1_device_var_value = -1;
static int hf_hartip_pt_rsp_slot1_device_var_status = -1;

static int hf_hartip_pt_rsp_slot2_device_var = -1;
static int hf_hartip_pt_rsp_slot2_device_var_classify = -1;
static int hf_hartip_pt_rsp_slot2_units = -1;
static int hf_hartip_pt_rsp_slot2_device_var_value = -1;
static int hf_hartip_pt_rsp_slot2_device_var_status = -1;

static int hf_hartip_pt_rsp_slot3_device_var = -1;
static int hf_hartip_pt_rsp_slot3_device_var_classify = -1;
static int hf_hartip_pt_rsp_slot3_units = -1;
static int hf_hartip_pt_rsp_slot3_device_var_value = -1;
static int hf_hartip_pt_rsp_slot3_device_var_status = -1;

static int hf_hartip_pt_rsp_slot4_device_var = -1;
static int hf_hartip_pt_rsp_slot4_device_var_classify = -1;
static int hf_hartip_pt_rsp_slot4_units = -1;
static int hf_hartip_pt_rsp_slot4_device_var_value = -1;
static int hf_hartip_pt_rsp_slot4_device_var_status = -1;

static int hf_hartip_pt_rsp_slot5_device_var = -1;
static int hf_hartip_pt_rsp_slot5_device_var_classify = -1;
static int hf_hartip_pt_rsp_slot5_units = -1;
static int hf_hartip_pt_rsp_slot5_device_var_value = -1;
static int hf_hartip_pt_rsp_slot5_device_var_status = -1;

static int hf_hartip_pt_rsp_slot6_device_var = -1;
static int hf_hartip_pt_rsp_slot6_device_var_classify = -1;
static int hf_hartip_pt_rsp_slot6_units = -1;
static int hf_hartip_pt_rsp_slot6_device_var_value = -1;
static int hf_hartip_pt_rsp_slot6_device_var_status = -1;

static int hf_hartip_pt_rsp_slot7_device_var = -1;
static int hf_hartip_pt_rsp_slot7_device_var_classify = -1;
static int hf_hartip_pt_rsp_slot7_units = -1;
static int hf_hartip_pt_rsp_slot7_device_var_value = -1;
static int hf_hartip_pt_rsp_slot7_device_var_status = -1;

static int hf_hartip_pt_rsp_slot0_timestamp = -1;

/* Command 13 response */
static int hf_hartip_pt_rsp_packed_descriptor = -1;
static int hf_hartip_pt_rsp_day = -1;
static int hf_hartip_pt_rsp_month = -1;
static int hf_hartip_pt_rsp_year = -1;

/* Command 14 response */
static int hf_hartip_pt_rsp_transducer_serial_number = -1;
static int hf_hartip_pt_rsp_transducer_limit_min_span_units = -1;
static int hf_hartip_pt_rsp_upper_transducer_limit = -1;
static int hf_hartip_pt_rsp_lower_transducer_limit = -1;
static int hf_hartip_pt_rsp_minimum_span = -1;

/* Command 15 response */
static int hf_hartip_pt_rsp_pv_alarm_selection_code = -1;
static int hf_hartip_pt_rsp_pv_transfer_function_code = -1;
static int hf_hartip_pt_rsp_pv_upper_and_lower_range_values_units = -1;
static int hf_hartip_pt_rsp_pv_upper_range_value = -1;
static int hf_hartip_pt_rsp_pv_lower_range_value = -1;
static int hf_hartip_pt_rsp_pv_damping_value = -1;
static int hf_hartip_pt_rsp_write_protect_code = -1;
static int hf_hartip_pt_rsp_reserved = -1;
static int hf_hartip_pt_rsp_pv_analog_channel_flags = -1;

/* Command 16 and 19 response */
static int hf_hartip_pt_rsp_final_assembly_number = -1;

/* response Tag */
static int hf_hartip_pt_rsp_tag = -1;

/* response Message */
static int hf_hartip_pt_rsp_message = -1;

/* Command 48 response */
static int hf_hartip_pt_rsp_device_sp_status = -1;
static int hf_hartip_pt_rsp_device_op_mode = -1;
static int hf_hartip_pt_rsp_standardized_status_0 = -1;
static int hf_hartip_pt_rsp_standardized_status_1 = -1;
static int hf_hartip_pt_rsp_analog_channel_saturated = -1;
static int hf_hartip_pt_rsp_standardized_status_2 = -1;
static int hf_hartip_pt_rsp_standardized_status_3 = -1;
static int hf_hartip_pt_rsp_analog_channel_fixed = -1;

/* Command 77 response */
static int hf_hartip_pt_rsp_io_card = -1;
static int hf_hartip_pt_rsp_channel = -1;
static int hf_hartip_pt_req_tx_preamble_count = -1;
static int hf_hartip_pt_rsp_embedded_cmd_delimiter = -1;
static int hf_hartip_pt_rsp_poll_address = -1;
static int hf_hartip_pt_rsp_unique_id = -1;
static int hf_hartip_pt_rsp_embedded_cmd = -1;

/* Command 31 and 178 response */
static int hf_hartip_pt_rsp_number_of_commands = -1;
static int hf_hartip_pt_rsp_command_number = -1;
static int hf_hartip_pt_rsp_command_byte_count = -1;

/* Command 31, 77 and 178 response */
static int hf_hartip_pt_rsp_data = -1;

/*Command 203 response*/
static int hf_hartip_pt_rsp_index_of_first_discrete_var = -1;
static int hf_hartip_pt_rsp_number_of_discrete_vars = -1;
static int hf_hartip_pt_rsp_timestamp_for_most_recent_discrete_change = -1;

static int hf_hartip_pt_rsp_slot0_discrete_var_state = -1;
static int hf_hartip_pt_rsp_slot0_discrete_var_status = -1;

static int hf_hartip_pt_rsp_slot1_discrete_var_state = -1;
static int hf_hartip_pt_rsp_slot1_discrete_var_status = -1;

static int hf_hartip_pt_rsp_slot2_discrete_var_state = -1;
static int hf_hartip_pt_rsp_slot2_discrete_var_status = -1;

static int hf_hartip_pt_rsp_slot3_discrete_var_state = -1;
static int hf_hartip_pt_rsp_slot3_discrete_var_status = -1;

static int hf_hartip_pt_rsp_slot4_discrete_var_state = -1;
static int hf_hartip_pt_rsp_slot4_discrete_var_status = -1;

static int hf_hartip_pt_rsp_slot5_discrete_var_state = -1;
static int hf_hartip_pt_rsp_slot5_discrete_var_status = -1;

#define HARTIP_HEADER_LENGTH     8
#define HARTIP_PORT           5094

/* HARTIP header */
typedef struct _hartip_hdr {
  guint8   version;
  guint8   message_type;
  guint8   message_id;
  guint8   status;
  guint16  transaction_id;
  guint16  length;
} hartip_hdr;

/* Message IDs */
#define SESSION_INITIATE_ID       0
#define SESSION_CLOSE_ID          1
#define KEEP_ALIVE_ID             2
#define PASS_THROUGH_ID           3

/* Message types */
#define REQUEST_MSG_TYPE       0
#define RESPONSE_MSG_TYPE      1
#define PUBLISH_MSG_TYPE       2
#define ERROR_MSG_TYPE         3
#define NAK_MSG_TYPE           15

static const value_string hartip_message_id_values[] = {
  { SESSION_INITIATE_ID,     "Session Initiate" },
  { SESSION_CLOSE_ID,        "Session Close" },
  { KEEP_ALIVE_ID,           "Keep Alive" },
  { PASS_THROUGH_ID,         "Pass Through" },
  { 0, NULL }
};

static const value_string hartip_message_type_values[] = {
  { REQUEST_MSG_TYPE,        "Request" },
  { RESPONSE_MSG_TYPE,       "Response" },
  { PUBLISH_MSG_TYPE,        "Publish" },
  { ERROR_MSG_TYPE,          "Error" },
  { NAK_MSG_TYPE,            "Error" },
  { 0, NULL }
};

/* Host types */
#define SECONDARY_MASTER_TYPE    0
#define PRIMARY_MASTER_TYPE      1

static const value_string hartip_master_type_values[] = {
  { SECONDARY_MASTER_TYPE,      "Secondary Host" },
  { PRIMARY_MASTER_TYPE,        "Primary Host" },
  { 0, NULL }
};

/* Error Codes */
#define SESSION_CLOSED_ERROR                0
#define PRIMARY_SESSION_UNAVAILABLE_ERROR   1
#define SERVICE_UNAVAILABLE_ERROR           2

static const value_string hartip_error_code_values[] = {
  { SESSION_CLOSED_ERROR,              "Session closed" },
  { PRIMARY_SESSION_UNAVAILABLE_ERROR, "Primary session unavailable" },
  { SERVICE_UNAVAILABLE_ERROR,         "Service unavailable" },
  { 0, NULL }
};

static const value_string hartip_pt_delimiter_frame_type_values[] = {
  { 1, "BACK (Burst Frame)" },
  { 2, "STX (Master to Field Device)" },
  { 6, "ACK (Field Device to Master)" },
  { 0, NULL }
};

static const value_string hartip_pt_delimiter_physical_layer_type_values[] = {
  { 0, "Asynchronous" },
  { 1, "Synchronous (i.e, PSK)" },
  { 0, NULL }
};

static const value_string hartip_pt_delimiter_address_type_values[] = {
  { 0, "Polling (1 Byte)" },
  { 1, "Unique (5 Bytes)" },
  { 0, NULL }
};

/* Handle for statistics tap. */
static int hartip_tap = -1;

/* Structure used for passing data for statistics processing. */
typedef struct _hartip_tap_info {
  gint8  message_type;
  gint8  message_id;
} hartip_tap_info;

/* Names of items in statistics tree. */
static const gchar* st_str_packets   = "Total HART_IP Packets";
static const gchar* st_str_requests  = "Request Packets";
static const gchar* st_str_responses = "Response Packets";
static const gchar* st_str_publish   = "Publish Packets";
static const gchar* st_str_errors    = "Error Packets";

/* Handles of items in statistics tree. */
static int st_node_packets = -1;
static int st_node_requests = -1;
static int st_node_responses = -1;
static int st_node_publish = -1;
static int st_node_errors = -1;

/* Local flags for pass through dissection. */
#define HARTIP_PT_IS_RSP    0x01

static void
hartip_stats_tree_init(stats_tree* st)
{
  st_node_packets   = stats_tree_create_node(st, st_str_packets, 0, STAT_DT_INT, TRUE);
  st_node_requests  = stats_tree_create_pivot(st, st_str_requests, st_node_packets);
  st_node_responses = stats_tree_create_node(st, st_str_responses, st_node_packets, STAT_DT_INT, TRUE);
  st_node_publish   = stats_tree_create_node(st, st_str_publish, st_node_packets, STAT_DT_INT, TRUE);
  st_node_errors    = stats_tree_create_node(st, st_str_errors, st_node_packets, STAT_DT_INT, TRUE);
}

static tap_packet_status
hartip_stats_tree_packet(stats_tree* st, packet_info* pinfo _U_,
                         epan_dissect_t* edt _U_, const void* p, tap_flags_t flags _U_)
{
  const hartip_tap_info *tapinfo = (const hartip_tap_info *)p;
  const gchar           *message_type_node_str, *message_id_node_str;
  int                    message_type_node;

  switch (tapinfo->message_type) {
  case REQUEST_MSG_TYPE:
    message_type_node_str = st_str_requests;
    message_type_node     = st_node_requests;
    break;
  case RESPONSE_MSG_TYPE:
    message_type_node_str = st_str_responses;
    message_type_node     = st_node_responses;
    break;
  case PUBLISH_MSG_TYPE:
    message_type_node_str = st_str_publish;
    message_type_node     = st_node_publish;
    break;
  case ERROR_MSG_TYPE:
  case NAK_MSG_TYPE:
    message_type_node_str = st_str_errors;
    message_type_node     = st_node_errors;
    break;
  default:
    return TAP_PACKET_DONT_REDRAW;  /* Don't want to track invalid messages for now. */
  }

  message_id_node_str = val_to_str(tapinfo->message_id,
                                   hartip_message_id_values,
                                   "Unknown message %d");

  tick_stat_node(st, st_str_packets, 0, FALSE);
  tick_stat_node(st, message_type_node_str, st_node_packets, FALSE);
  tick_stat_node(st, message_id_node_str, message_type_node, FALSE);

  return TAP_PACKET_REDRAW;
}

static gint
dissect_empty_body(proto_tree *tree, packet_info* pinfo, tvbuff_t *tvb,
                   gint offset, gint bodylen)
{
  proto_item  *ti;

  ti = proto_tree_add_item(tree, hf_hartip_data, tvb, offset, bodylen, ENC_NA);
  if (bodylen == 0) {
    expert_add_info(pinfo, ti, &ei_hartip_data_none);
  } else {
    expert_add_info(pinfo, ti, &ei_hartip_data_unexpected);
  }
  return bodylen;
}

static gint
dissect_session_init(proto_tree *body_tree, tvbuff_t *tvb, gint offset,
                     gint bodylen)
{
  if (bodylen == 5) {
    proto_tree_add_item(body_tree, hf_hartip_master_type, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset += 1;

    proto_tree_add_item(body_tree, hf_hartip_inactivity_close_timer, tvb, offset, 4, ENC_BIG_ENDIAN);
  } else {
    proto_tree_add_item(body_tree, hf_hartip_data, tvb, offset, bodylen, ENC_NA);
  }

  return bodylen;
}

static gint
dissect_error(proto_tree *body_tree, tvbuff_t *tvb, gint offset, gint bodylen)
{
  if (bodylen == 1) {
    proto_tree_add_item(body_tree, hf_hartip_error_code, tvb, offset, 1, ENC_BIG_ENDIAN);
  } else {
    proto_tree_add_item(body_tree, hf_hartip_data, tvb, offset, bodylen, ENC_NA);
  }

  return bodylen;
}

static gint
dissect_session_close(proto_tree *body_tree, packet_info* pinfo, tvbuff_t *tvb,
                      gint offset, gint bodylen)
{
  return dissect_empty_body(body_tree, pinfo, tvb, offset, bodylen);
}

static gint
dissect_keep_alive(proto_tree *body_tree, packet_info* pinfo, tvbuff_t *tvb,
                   gint offset, gint bodylen)
{
  return dissect_empty_body(body_tree, pinfo, tvb, offset, bodylen);
}

static gint
dissect_byte(proto_tree *tree, int hf, tvbuff_t *tvb, gint offset)
{
  proto_tree_add_item(tree, hf, tvb, offset, 1, ENC_BIG_ENDIAN);
  return 1;
}

static gint
dissect_short(proto_tree *tree, int hf, tvbuff_t *tvb, gint offset)
{
  proto_tree_add_item(tree, hf, tvb, offset, 2, ENC_BIG_ENDIAN);
  return 2;
}

static gint
dissect_float(proto_tree *tree, int hf, tvbuff_t *tvb, gint offset)
{
  proto_tree_add_item(tree, hf, tvb, offset, 4, ENC_BIG_ENDIAN);
  return 4;
}

static gint
dissect_packAscii(proto_tree *tree, int hf, tvbuff_t *tvb, gint offset, int len)
{
  gushort     usIdx;
  gushort     usGroupCnt;
  gushort     usMaxGroups;      /* Number of 4 byte groups to pack. */
  gushort     usMask;
  gint        iIndex;
  gint        i   = 0;
  gushort     buf[4];
  guint8     *tmp;
  char       *str = NULL;

  tmp = (guint8 *)wmem_alloc0(wmem_packet_scope(), len);
  tvb_memcpy(tvb, tmp, offset, len);

  /* Maximum possible unpacked length = (len / 3) * 4 */
  str = (char *)wmem_alloc(wmem_packet_scope(), ((len / 3) * 4)+1);

  iIndex = 0;
  usMaxGroups = (gushort)(len / 3);
  for (usGroupCnt = 0; usGroupCnt < usMaxGroups; usGroupCnt++) {
    /*
     * First unpack 3 bytes into a group of 4 bytes, clearing bits 6 & 7.
     */
    buf[0] = (gushort)(tmp[iIndex] >> 2);
    buf[1] = (gushort)(((tmp[iIndex] << 4) & 0x30) | (tmp[iIndex + 1] >> 4));
    buf[2] = (gushort)(((tmp[iIndex + 1] << 2) & 0x3C) | (tmp[iIndex + 2] >> 6));
    buf[3] = (gushort)(tmp[iIndex + 2] & 0x3F);
    iIndex += 3;

    /*
     * Now transfer to unpacked area, setting bit 6 to complement of bit 5.
     */
    for (usIdx = 0; usIdx < 4; usIdx++) {
      usMask = (gushort)(((buf[usIdx] & 0x20) << 1) ^ 0x40);
      DISSECTOR_ASSERT(i < 256);
      str[i++] = (gchar)(buf[usIdx] | usMask);
    }
  }
  str[i] = '\0';

  proto_tree_add_string(tree, hf, tvb, offset, len, str);

  return len;
}

static gint
dissect_timestamp(proto_tree *tree, int hf, const char *name, int len,
                  tvbuff_t *tvb, gint offset)
{
  proto_item *ti;
  guint32     t;
  guint32     hrs  = 0;
  guint32     mins = 0;
  guint32     secs = 0;
  guint32     ms   = 0;

  ti = proto_tree_add_item(tree, hf, tvb, offset, len, ENC_NA);
  t  = tvb_get_ntohl(tvb, offset);

  if (t > 0 ) {
    t /= 32;
    ms = t % 1000;
    t /= 1000;
    secs = t % 60;
    t /= 60;
    mins = t % 60;
    hrs = (guint)(t / 60);
  }

  proto_item_set_text(ti, "%s: %02d:%02d:%02d.%03d", name, hrs, mins, secs, ms);
  return len;
}

static gint
dissect_cmd0(proto_tree *body_tree, tvbuff_t *tvb, gint offset, gint bodylen)
{
  if (bodylen >= 12) {
    offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_expansion_code,                   tvb, offset);
    offset += dissect_short(body_tree, hf_hartip_pt_rsp_expanded_device_type,             tvb, offset);
    offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_req_min_preambles,                tvb, offset);
    offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_hart_protocol_major_rev,          tvb, offset);
    offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_device_rev,                       tvb, offset);
    offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_software_rev,                     tvb, offset);
    offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_hardware_rev_physical_signal,     tvb, offset);
    offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_flage,                            tvb, offset);
    proto_tree_add_item(body_tree,     hf_hartip_pt_rsp_device_id,                        tvb, offset, 3, ENC_NA);
    offset += 3;

    if (bodylen >= 16) {
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_rsp_min_preambles,                tvb, offset);
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_max_device_variables,             tvb, offset);
      offset += dissect_short(body_tree, hf_hartip_pt_rsp_configuration_change_counter,     tvb, offset);
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_extended_device_status,           tvb, offset);
    }

    if (bodylen >= 18) {
      offset += dissect_short(body_tree, hf_hartip_pt_rsp_manufacturer_Identification_code, tvb, offset);
    }

    if (bodylen >= 22) {
      offset += dissect_short(body_tree, hf_hartip_pt_rsp_private_label,                    tvb, offset);
      /*offset += */dissect_byte(body_tree,  hf_hartip_pt_rsp_device_profile,                   tvb, offset);
    }

    return bodylen;
  }

  return 0;
}

static gint
dissect_cmd1(proto_tree *body_tree, tvbuff_t *tvb, gint offset, gint bodylen)
{
  if (bodylen >= 5) {
    offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_pv_units, tvb, offset);
    /*offset += */dissect_float(body_tree, hf_hartip_pt_rsp_pv,       tvb, offset);
    return bodylen;
  }

  return 0;
}

static gint
dissect_cmd2(proto_tree *body_tree, tvbuff_t *tvb, gint offset, gint bodylen)
{
  if (bodylen >= 8) {
    offset += dissect_float(body_tree, hf_hartip_pt_rsp_pv_loop_current,  tvb, offset);
    /*offset += */dissect_float(body_tree, hf_hartip_pt_rsp_pv_percent_range, tvb, offset);
    return bodylen;
  }

  return 0;
}

static gint
dissect_cmd3(proto_tree *body_tree, tvbuff_t *tvb, gint offset, gint bodylen)
{
  if (bodylen >= 24) {
    offset += dissect_float(body_tree, hf_hartip_pt_rsp_pv_loop_current, tvb, offset);
    offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_pv_units,        tvb, offset);
    offset += dissect_float(body_tree, hf_hartip_pt_rsp_pv,              tvb, offset);
    offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_sv_units,        tvb, offset);
    offset += dissect_float(body_tree, hf_hartip_pt_rsp_sv,              tvb, offset);
    offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_tv_units,        tvb, offset);
    offset += dissect_float(body_tree, hf_hartip_pt_rsp_tv,              tvb, offset);
    offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_qv_units,        tvb, offset);
    /*offset += */dissect_float(body_tree, hf_hartip_pt_rsp_qv,              tvb, offset);

    return bodylen;
  }

  return 0;
}

static gint
dissect_cmd7(proto_tree *body_tree, tvbuff_t *tvb, gint offset, gint bodylen)
{
  if (bodylen >= 2) {
    offset += dissect_byte(body_tree, hf_hartip_pt_rsp_poll_address,     tvb, offset);
    dissect_byte(body_tree, hf_hartip_pt_rsp_loop_current_mode,          tvb, offset);

    return bodylen;
  }
   return 0;
}

static gint
dissect_cmd8(proto_tree *body_tree, tvbuff_t *tvb, gint offset, gint bodylen)
{
  if (bodylen >= 4) {
    offset += dissect_byte(body_tree, hf_hartip_pt_rsp_primary_var_classify,     tvb, offset);
    offset += dissect_byte(body_tree, hf_hartip_pt_rsp_secondary_var_classify,   tvb, offset);
    offset += dissect_byte(body_tree, hf_hartip_pt_rsp_tertiary_var_classify,    tvb, offset);
    dissect_byte(body_tree, hf_hartip_pt_rsp_quaternary_var_classify,            tvb, offset);

    return bodylen;
  }
   return 0;
}

static gint
dissect_cmd9(proto_tree *body_tree, tvbuff_t *tvb, gint offset, gint bodylen)
{
  if (bodylen >= 13) {
    offset += dissect_byte(body_tree,    hf_hartip_pt_rsp_extended_device_status,    tvb, offset);
    offset += dissect_byte(body_tree,    hf_hartip_pt_rsp_slot0_device_var,          tvb, offset);
    offset += dissect_byte(body_tree,    hf_hartip_pt_rsp_slot0_device_var_classify, tvb, offset);
    offset += dissect_byte(body_tree,    hf_hartip_pt_rsp_slot0_units,               tvb, offset);
    offset += dissect_float(body_tree,   hf_hartip_pt_rsp_slot0_device_var_value,    tvb, offset);
    offset += dissect_byte(body_tree,    hf_hartip_pt_rsp_slot0_device_var_status,   tvb, offset);

    if (bodylen >= 21) {
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_slot1_device_var,          tvb, offset);
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_slot1_device_var_classify, tvb, offset);
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_slot1_units,               tvb, offset);
      offset += dissect_float(body_tree, hf_hartip_pt_rsp_slot1_device_var_value,    tvb, offset);
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_slot1_device_var_status,   tvb, offset);
    }

    if (bodylen >= 29) {
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_slot2_device_var,          tvb, offset);
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_slot2_device_var_classify, tvb, offset);
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_slot2_units,               tvb, offset);
      offset += dissect_float(body_tree, hf_hartip_pt_rsp_slot2_device_var_value,    tvb, offset);
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_slot2_device_var_status,   tvb, offset);
    }

    if (bodylen >= 37) {
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_slot3_device_var,          tvb, offset);
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_slot3_device_var_classify, tvb, offset);
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_slot3_units,               tvb, offset);
      offset += dissect_float(body_tree, hf_hartip_pt_rsp_slot3_device_var_value,    tvb, offset);
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_slot3_device_var_status,   tvb, offset);
    }

    if (bodylen >= 45) {
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_slot4_device_var,          tvb, offset);
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_slot4_device_var_classify, tvb, offset);
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_slot4_units,               tvb, offset);
      offset += dissect_float(body_tree, hf_hartip_pt_rsp_slot4_device_var_value,    tvb, offset);
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_slot4_device_var_status,   tvb, offset);
    }

    if (bodylen >= 53) {
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_slot5_device_var,          tvb, offset);
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_slot5_device_var_classify, tvb, offset);
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_slot5_units,               tvb, offset);
      offset += dissect_float(body_tree, hf_hartip_pt_rsp_slot5_device_var_value,    tvb, offset);
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_slot5_device_var_status,   tvb, offset);
    }

    if (bodylen >= 61) {
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_slot6_device_var,          tvb, offset);
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_slot6_device_var_classify, tvb, offset);
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_slot6_units,               tvb, offset);
      offset += dissect_float(body_tree, hf_hartip_pt_rsp_slot6_device_var_value,    tvb, offset);
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_slot6_device_var_status,   tvb, offset);
    }

    if (bodylen >= 69) {
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_slot7_device_var,          tvb, offset);
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_slot7_device_var_classify, tvb, offset);
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_slot7_units,               tvb, offset);
      offset += dissect_float(body_tree, hf_hartip_pt_rsp_slot7_device_var_value,    tvb, offset);
      offset += dissect_byte(body_tree,  hf_hartip_pt_rsp_slot7_device_var_status,   tvb, offset);
    }

    dissect_timestamp(body_tree, hf_hartip_pt_rsp_slot0_timestamp, "Slot0 Data TimeStamp", 4, tvb, offset);

    return bodylen;
  }

  return 0;
}

static gint
dissect_cmd13(proto_tree *body_tree, tvbuff_t *tvb, gint offset, gint bodylen)
{
  if (bodylen >= 21) {
    offset += dissect_packAscii(body_tree, hf_hartip_pt_rsp_tag, tvb, offset, 6);
    offset += dissect_packAscii(body_tree, hf_hartip_pt_rsp_packed_descriptor, tvb, offset, 12);
    offset += dissect_byte(body_tree,      hf_hartip_pt_rsp_day,                                 tvb, offset);
    offset += dissect_byte(body_tree,      hf_hartip_pt_rsp_month,                               tvb, offset);
    /*offset += */dissect_byte(body_tree,      hf_hartip_pt_rsp_year,                                tvb, offset);

    return bodylen;
  }

  return 0;
}

static gint
dissect_cmd14(proto_tree *body_tree, tvbuff_t *tvb, gint offset, gint bodylen)
{
  if (bodylen >= 16) {
    proto_tree_add_item(body_tree, hf_hartip_pt_rsp_transducer_serial_number, tvb, offset, 3, ENC_NA);
    offset += 3;
    offset += dissect_byte(body_tree, hf_hartip_pt_rsp_transducer_limit_min_span_units, tvb, offset);
    offset += dissect_float(body_tree, hf_hartip_pt_rsp_upper_transducer_limit,         tvb, offset);
    offset += dissect_float(body_tree, hf_hartip_pt_rsp_lower_transducer_limit,         tvb, offset);
    dissect_float(body_tree, hf_hartip_pt_rsp_minimum_span,                             tvb, offset);

    return bodylen;
  }

  return 0;
}

static gint
dissect_cmd15(proto_tree *body_tree, tvbuff_t *tvb, gint offset, gint bodylen)
{
  if (bodylen >= 18) {
    offset += dissect_byte(body_tree, hf_hartip_pt_rsp_pv_alarm_selection_code,               tvb, offset);
    offset += dissect_byte(body_tree, hf_hartip_pt_rsp_pv_transfer_function_code,             tvb, offset);
    offset += dissect_byte(body_tree, hf_hartip_pt_rsp_pv_upper_and_lower_range_values_units, tvb, offset);
    offset += dissect_float(body_tree, hf_hartip_pt_rsp_pv_upper_range_value,                 tvb, offset);
    offset += dissect_float(body_tree, hf_hartip_pt_rsp_pv_lower_range_value,                 tvb, offset);
    offset += dissect_float(body_tree, hf_hartip_pt_rsp_pv_damping_value,                     tvb, offset);
    offset += dissect_byte(body_tree, hf_hartip_pt_rsp_write_protect_code,                    tvb, offset);
    offset += dissect_byte(body_tree, hf_hartip_pt_rsp_reserved,                              tvb, offset);
    dissect_byte(body_tree, hf_hartip_pt_rsp_pv_analog_channel_flags,                         tvb, offset);

    return bodylen;
  }

  return 0;
}

static gint
dissect_cmd16(proto_tree *body_tree, tvbuff_t *tvb, gint offset, gint bodylen)
{
  if (bodylen >= 3) {
    proto_tree_add_item(body_tree, hf_hartip_pt_rsp_final_assembly_number, tvb, offset, 3, ENC_NA);

    return bodylen;
  }

  return 0;
}

static gint
dissect_cmd33(proto_tree *body_tree, tvbuff_t *tvb, gint offset, gint bodylen)
{
  if (bodylen >= 6) {
    offset += dissect_byte(body_tree,    hf_hartip_pt_rsp_slot0_device_var,          tvb, offset);
    offset += dissect_byte(body_tree,    hf_hartip_pt_rsp_slot0_units,               tvb, offset);
    offset += dissect_float(body_tree,   hf_hartip_pt_rsp_slot0_device_var_value,    tvb, offset);

    if (bodylen >= 12) {
      offset += dissect_byte(body_tree,    hf_hartip_pt_rsp_slot1_device_var,        tvb, offset);
      offset += dissect_byte(body_tree,    hf_hartip_pt_rsp_slot1_units,             tvb, offset);
      offset += dissect_float(body_tree,   hf_hartip_pt_rsp_slot1_device_var_value,  tvb, offset);
    }

    if (bodylen >= 18) {
      offset += dissect_byte(body_tree,    hf_hartip_pt_rsp_slot2_device_var,        tvb, offset);
      offset += dissect_byte(body_tree,    hf_hartip_pt_rsp_slot2_units,             tvb, offset);
      offset += dissect_float(body_tree,   hf_hartip_pt_rsp_slot2_device_var_value,  tvb, offset);
    }

    if (bodylen >= 24) {
      offset += dissect_byte(body_tree,    hf_hartip_pt_rsp_slot3_device_var,        tvb, offset);
      offset += dissect_byte(body_tree,    hf_hartip_pt_rsp_slot3_units,             tvb, offset);
      dissect_float(body_tree,   hf_hartip_pt_rsp_slot3_device_var_value,            tvb, offset);
    }

    return bodylen;
  }

  return 0;
}

static gint
dissect_cmd38(proto_tree *body_tree, tvbuff_t *tvb, gint offset, gint bodylen)
{
  if (bodylen >= 2) {
    dissect_short(body_tree, hf_hartip_pt_rsp_configuration_change_counter, tvb, offset);

   return bodylen;
  }

  return 0;
}

static gint
dissect_cmd48(proto_tree *body_tree, tvbuff_t *tvb, gint offset, gint bodylen)
{
  if (bodylen >= 6) {
    proto_tree_add_item(body_tree,      hf_hartip_pt_rsp_device_sp_status,         tvb, offset, 6, ENC_NA);
    offset += 6;

    if (bodylen >= 9) {
      offset += dissect_byte(body_tree,   hf_hartip_pt_rsp_extended_device_status, tvb, offset);
      offset += dissect_byte(body_tree,   hf_hartip_pt_rsp_device_op_mode,         tvb, offset);
      offset += dissect_byte(body_tree,   hf_hartip_pt_rsp_standardized_status_0,  tvb, offset);
    }

    if (bodylen >= 13) {
      offset += dissect_byte(body_tree, hf_hartip_pt_rsp_standardized_status_1,    tvb, offset);
      offset += dissect_byte(body_tree, hf_hartip_pt_rsp_analog_channel_saturated, tvb, offset);
      offset += dissect_byte(body_tree, hf_hartip_pt_rsp_standardized_status_2,    tvb, offset);
      offset += dissect_byte(body_tree, hf_hartip_pt_rsp_standardized_status_3,    tvb, offset);
    }

    if (bodylen >= 14) {
        offset += dissect_byte(body_tree, hf_hartip_pt_rsp_analog_channel_fixed,   tvb, offset);
    }

    if (bodylen >= 24) {
      proto_tree_add_item(body_tree,    hf_hartip_pt_rsp_device_sp_status,         tvb, offset, 11, ENC_NA);
      /*offset += 11;*/
    }
    return bodylen;
  }

  return 0;
}

static gint
dissect_cmd77(proto_tree *body_tree, tvbuff_t *tvb, gint offset, gint bodylen, gint flags)
{
  guint8  byte_count;
  gint    length = bodylen;

  if (length >= 6) {
     guint8 delimiter;

     offset += dissect_byte(body_tree, hf_hartip_pt_rsp_io_card,                tvb, offset);
     offset += dissect_byte(body_tree, hf_hartip_pt_rsp_channel,                tvb, offset);
     if (!(flags & HARTIP_PT_IS_RSP)) {
       offset += dissect_byte(body_tree, hf_hartip_pt_req_tx_preamble_count, tvb, offset);
     }

     delimiter = tvb_get_guint8(tvb, offset);
     offset += dissect_byte(body_tree, hf_hartip_pt_rsp_embedded_cmd_delimiter, tvb, offset);

     if ((delimiter & 0x80) == 0) {
       guint8 short_addr;
       short_addr = tvb_get_guint8(tvb, offset);
       short_addr &= 0x3F;
       proto_tree_add_uint(body_tree, hf_hartip_pt_rsp_poll_address, tvb, offset, 1, short_addr);
       offset += 1;
     } else {
       /* TODO  Chop of the two first bits here, too? */
       proto_tree_add_item(body_tree, hf_hartip_pt_rsp_unique_id, tvb, offset, 5, ENC_NA);
       offset += 5;
     }

     offset += dissect_byte(body_tree, hf_hartip_pt_rsp_embedded_cmd,           tvb, offset);

     byte_count = tvb_get_guint8(tvb, offset);
     proto_tree_add_item(body_tree, hf_hartip_pt_rsp_command_byte_count, tvb, offset, 1, ENC_BIG_ENDIAN);
     offset += 1;
     length -= 6;

     if ((byte_count >= 2) && (length >= 2) && (byte_count <= length)) {
       offset += dissect_byte(body_tree, hf_hartip_pt_response_code, tvb, offset);
       offset += dissect_byte(body_tree, hf_hartip_pt_device_status, tvb, offset);
       length -= 2;
       byte_count -= 2;

       if ((byte_count > 0) && (length > 0) && (byte_count <= length)) {
         proto_tree_add_item(body_tree, hf_hartip_pt_rsp_data, tvb, offset,
                             byte_count, ENC_NA);
       }
     }

    return bodylen;
  }

  return 0;
}

static gint
dissect_cmd178(proto_tree *body_tree, tvbuff_t *tvb, gint offset, gint bodylen)
{
   guint16 cmd;
   guint8  number_of_cmds;
   guint8  cmd_byte_count;
   gint    length = bodylen;
   gint8   i;
   gint    result;

   if (length >= 5) {
     number_of_cmds = tvb_get_guint8(tvb, offset);
     proto_tree_add_uint(body_tree, hf_hartip_pt_rsp_number_of_commands, tvb, offset, 1, number_of_cmds);
     offset += 1;
     length -= 1;

     for (i = 0; i < number_of_cmds; i++)
     {
       if (length >= 4)
       {
         cmd = tvb_get_ntohs(tvb, offset);
         proto_tree_add_item(body_tree, hf_hartip_pt_rsp_command_number, tvb, offset, 2, ENC_BIG_ENDIAN);
         offset += 2;
         length -= 2;

         cmd_byte_count = tvb_get_guint8(tvb, offset);
         proto_tree_add_item(body_tree, hf_hartip_pt_rsp_command_byte_count, tvb, offset, 1, ENC_BIG_ENDIAN);
         offset += 1;
         length -= 1;

         offset += dissect_byte(body_tree, hf_hartip_pt_response_code, tvb, offset);
         length -= 1;
         cmd_byte_count -= 1;

         if ((cmd_byte_count > 0) && (length > 0) && (cmd_byte_count <= length))
         {
           switch(cmd)
           {
             case 3:
               result = dissect_cmd3(body_tree, tvb, offset, cmd_byte_count);
               break;
             case 9:
               result = dissect_cmd9(body_tree, tvb, offset, cmd_byte_count);
               break;
             case 48:
               result = dissect_cmd48(body_tree, tvb, offset, cmd_byte_count);
               break;
             default:
               result = 0;
               break;
           }

           if (result == 0 ) {
             proto_tree_add_item(body_tree, hf_hartip_pt_rsp_data, tvb, offset,
                                 cmd_byte_count, ENC_NA);
           }

           offset += cmd_byte_count;
           length -= cmd_byte_count;
         }
       } else {
         return bodylen;
       }
     }

     return bodylen;
   }

   return 0;
}

static gint
dissect_cmd203(proto_tree *body_tree, tvbuff_t *tvb, gint offset, gint bodylen)
{
  if (bodylen >= 8) {
    offset += dissect_short(body_tree, hf_hartip_pt_rsp_index_of_first_discrete_var, tvb, offset);
    offset += dissect_byte(body_tree, hf_hartip_pt_rsp_number_of_discrete_vars,      tvb, offset);
    offset += dissect_byte(body_tree, hf_hartip_pt_rsp_extended_device_status,       tvb, offset);
    dissect_timestamp(body_tree, hf_hartip_pt_rsp_timestamp_for_most_recent_discrete_change,
                      "TimeStamp for Most Recent Discrete Change", 4,                tvb, offset);
    offset += 4;

    if (bodylen >= 11)
    {
       offset += dissect_short(body_tree, hf_hartip_pt_rsp_slot0_discrete_var_state, tvb, offset);
       offset += dissect_byte(body_tree, hf_hartip_pt_rsp_slot0_discrete_var_status, tvb, offset);
    }

    if (bodylen >= 14)
    {
       offset += dissect_short(body_tree, hf_hartip_pt_rsp_slot1_discrete_var_state, tvb, offset);
       offset += dissect_byte(body_tree, hf_hartip_pt_rsp_slot1_discrete_var_status, tvb, offset);
    }

    if (bodylen >= 17)
    {
       offset += dissect_short(body_tree, hf_hartip_pt_rsp_slot2_discrete_var_state, tvb, offset);
       offset += dissect_byte(body_tree, hf_hartip_pt_rsp_slot2_discrete_var_status, tvb, offset);
    }

    if (bodylen >= 20)
    {
       offset += dissect_short(body_tree, hf_hartip_pt_rsp_slot3_discrete_var_state, tvb, offset);
       offset += dissect_byte(body_tree, hf_hartip_pt_rsp_slot3_discrete_var_status, tvb, offset);
    }

    if (bodylen >= 23)
    {
       offset += dissect_short(body_tree, hf_hartip_pt_rsp_slot4_discrete_var_state, tvb, offset);
       offset += dissect_byte(body_tree, hf_hartip_pt_rsp_slot4_discrete_var_status, tvb, offset);
    }

    if (bodylen >= 26)
    {
       offset += dissect_short(body_tree, hf_hartip_pt_rsp_slot5_discrete_var_state, tvb, offset);
       dissect_byte(body_tree, hf_hartip_pt_rsp_slot5_discrete_var_status,           tvb, offset);
    }

    return bodylen;
  }

  return 0;
}

static gint
dissect_cmd31(proto_tree *body_tree, tvbuff_t *tvb, gint offset, gint bodylen)
{
  gint    length = bodylen;
  gint    result = 0;
  guint16 cmd;

  if (length >= 2) {
    cmd = tvb_get_ntohs(tvb, offset);
    proto_tree_add_item(body_tree, hf_hartip_pt_rsp_command_number, tvb, offset, 2, ENC_BIG_ENDIAN);
    offset += 2;
    length -= 2;

    if (length > 0) {
      switch(cmd)
      {
        case 64386:
          result = dissect_cmd203(body_tree, tvb, offset, length);
          break;
      }

      if (result == 0) {
        proto_tree_add_item(body_tree, hf_hartip_pt_rsp_data, tvb, offset,
                            length, ENC_NA);
      }
    }

    return bodylen;
  }

  return 0;
}

static gint
dissect_parse_hart_cmds(proto_tree *body_tree, tvbuff_t *tvb, guint8 cmd,
                        gint offset, gint bodylen, gint flags)
{
  switch(cmd)
  {
  case 0:
  case 11:
  case 21:
    return dissect_cmd0(body_tree, tvb, offset, bodylen);
  case 1:
    return dissect_cmd1(body_tree, tvb, offset, bodylen);
  case 2:
    return dissect_cmd2(body_tree, tvb, offset, bodylen);
  case 3:
    return dissect_cmd3(body_tree, tvb, offset, bodylen);
  case 6:
  case 7:
    return dissect_cmd7(body_tree, tvb, offset, bodylen);
  case 8:
    return dissect_cmd8(body_tree, tvb, offset, bodylen);
  case 9:
    return dissect_cmd9(body_tree, tvb, offset, bodylen);
  case 12:
  case 17:
    if (bodylen >= 24)
      return dissect_packAscii(body_tree, hf_hartip_pt_rsp_message, tvb, offset, 24);
    break;
  case 13:
  case 18:
    return dissect_cmd13(body_tree, tvb, offset, bodylen);
  case 14:
    return dissect_cmd14(body_tree, tvb, offset, bodylen);
  case 15:
    return dissect_cmd15(body_tree, tvb, offset, bodylen);
  case 16:
  case 19:
    return dissect_cmd16(body_tree, tvb, offset, bodylen);
  case 20:
  case 22:
    if (bodylen >= 32) {
      proto_tree_add_item(body_tree, hf_hartip_pt_rsp_tag, tvb, offset, 32,
                          ENC_ASCII|ENC_NA);
      return 32;
    }
    break;
  case 31:
    return dissect_cmd31(body_tree, tvb, offset, bodylen);
  case 33:
    return dissect_cmd33(body_tree, tvb, offset, bodylen);
  case 38:
    return dissect_cmd38(body_tree, tvb, offset, bodylen);
  case 48:
    return dissect_cmd48(body_tree, tvb, offset, bodylen);
  case 77:
    return dissect_cmd77(body_tree, tvb, offset, bodylen, flags);
  case 178:
    return dissect_cmd178(body_tree, tvb, offset, bodylen);
  case 203:
    return dissect_cmd203(body_tree, tvb, offset, bodylen);
  }

  return 0;
}

static gint
dissect_pass_through(proto_tree *body_tree, tvbuff_t *tvb, gint offset,
                     gint bodylen)
{
  guint8      delimiter;
  guint8      cmd           = 0;
  gint        length        = bodylen;
  gint        is_short      = 0;
  gint        is_rsp        = 0;
  gint        num_preambles = 0;
  gint        result;
  guint8      short_addr;
  guint8      bytes         = 0;

  /* find number of preambles */
  while (length > num_preambles) {
    delimiter = tvb_get_guint8(tvb, offset + num_preambles);
    if (delimiter != 0xFF)
      break;

    num_preambles += 1;
  }

  if (num_preambles > 0) {
    proto_tree_add_item(body_tree, hf_hartip_pt_preambles, tvb, offset,
                        num_preambles, ENC_NA);
    offset += num_preambles;
    length -= num_preambles;
  }

  if (length > 0) {
    delimiter = tvb_get_guint8(tvb, offset);

    static int * const hartip_pt_delimiter_flag[] = {
        &hf_hartip_pt_delimiter_frame_type,
        &hf_hartip_pt_delimiter_physical_layer_type,
        &hf_hartip_pt_delimiter_number_of_expansion_bytes,
        &hf_hartip_pt_delimiter_address_type,
        NULL
    };

    proto_tree_add_bitmask(body_tree, tvb, offset, hf_hartip_pt_delimiter, ett_hartip_pt_delimiter,
                           hartip_pt_delimiter_flag, ENC_NA);
    offset += 1;
    length -= 1;

    if ((delimiter & 0x7) == 6) {
      is_rsp = 1;
    } else if ((delimiter & 0x7) == 1) {
      is_rsp = 1;
    }

    if ((delimiter & 0x80) == 0) {
      is_short = 1;
    }

    bytes = (delimiter & 0x60) >> 5;
  }

  if (is_short == 1) {
    if (length > 0) {
      short_addr = tvb_get_guint8(tvb, offset);
      short_addr = short_addr & 0x3F;
      proto_tree_add_uint(body_tree, hf_hartip_pt_short_addr, tvb, offset, 1,
                               short_addr);
      offset += 1;
      length -= 1;
    }
  } else {
    if (length > 4) {
      proto_tree_add_item(body_tree, hf_hartip_pt_long_addr, tvb, offset, 5, ENC_NA);
      offset += 5;
      length -= 5;
    } else if (length > 0) {
      proto_tree_add_item(body_tree, hf_hartip_data, tvb, offset, length, ENC_NA);
      length = 0;
    }
  }

  if (bytes > 0) {
    proto_tree_add_item(body_tree, hf_hartip_pt_expansion_bytes, tvb, offset, bytes, ENC_NA);
    offset += bytes;
    length -= bytes;
  }

  if (length > 0) {
    cmd = tvb_get_guint8(tvb, offset);
    proto_tree_add_item(body_tree, hf_hartip_pt_command, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset += 1;
    length -= 1;
  }
  if (length > 0) {
    proto_tree_add_item(body_tree, hf_hartip_pt_length, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset += 1;
    length -= 1;
  }

  if (is_rsp == 1) {
    if (length > 0) {
      proto_tree_add_item(body_tree, hf_hartip_pt_response_code, tvb, offset, 1, ENC_BIG_ENDIAN);
      offset += 1;
      length -= 1;
    }
    if (length > 0) {
      proto_tree_add_item(body_tree, hf_hartip_pt_device_status, tvb, offset, 1, ENC_BIG_ENDIAN);
      offset += 1;
      length -= 1;
    }
  }

  if (length > 1) {
    gint flags = 0;
    if ( is_rsp ) {
      flags |= HARTIP_PT_IS_RSP;
    }
    result = dissect_parse_hart_cmds(body_tree, tvb, cmd, offset, (length - 1), flags);
    if (result == 0 ) {
      proto_tree_add_item(body_tree, hf_hartip_pt_payload, tvb, offset,
                          (length - 1), ENC_NA);
    }
    offset += (length - 1);
    length = 1;
  }
  if (length > 0) {
    proto_tree_add_checksum(body_tree, tvb, offset, hf_hartip_pt_checksum, -1, NULL, NULL, 0, ENC_BIG_ENDIAN, PROTO_CHECKSUM_NO_FLAGS);
  }

  return bodylen;
}

static void
hartip_set_conversation(packet_info *pinfo)
{
  conversation_t *conversation = NULL;

  if (!pinfo->fd->visited && (pinfo->ptype == PT_UDP)) {
    /*
     * This function is called for a session initiate send over UDP.
     * The session initiate is sent to the server on port HARTIP_PORT.
     * The server then responds from a different port.  All subsequent
     * communication for the session between the client and server
     * uses the new server port and the original client port.
     *
     * A new conversation is created here and this dissector is set to
     * be used for it.  This allows the packets to be dissected properly
     * for this protocol.
     */
    conversation = find_conversation(pinfo->num,
                                     &pinfo->src, &pinfo->dst, conversation_pt_to_conversation_type(pinfo->ptype),
                                     pinfo->srcport, 0, NO_PORT_B);
    if( (conversation == NULL) ||
        (conversation_get_dissector(conversation, pinfo->num) != hartip_udp_handle) ) {
      conversation = conversation_new(pinfo->num,
                                      &pinfo->src, &pinfo->dst, conversation_pt_to_conversation_type(pinfo->ptype),
                                      pinfo->srcport, 0, NO_PORT2);
      conversation_set_dissector(conversation, hartip_udp_handle);
    }
  }
}

static int
dissect_hartip_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                      gint offset)
{
  proto_tree      *hartip_tree, *hdr_tree, *body_tree;
  proto_item      *hart_item;
  gint             bodylen;
  guint8           message_type, message_id;
  guint16          transaction_id, length;
  const char      *msg_id_str, *msg_type_str;
  hartip_tap_info *tapinfo;

  col_set_str(pinfo->cinfo, COL_PROTOCOL, "HART_IP");
  col_clear(pinfo->cinfo, COL_INFO);

  length  = tvb_get_ntohs(tvb, offset+6);

  hart_item = proto_tree_add_item(tree, proto_hartip, tvb, 0, length, ENC_NA);
  hartip_tree = proto_item_add_subtree(hart_item, ett_hartip);

  hdr_tree = proto_tree_add_subtree(hartip_tree, tvb, offset, HARTIP_HEADER_LENGTH,
                      ett_hartip_hdr, NULL, "HART_IP Header");

  proto_tree_add_item(hdr_tree, hf_hartip_hdr_version, tvb, offset, 1, ENC_BIG_ENDIAN);
  offset += 1;

  message_type = tvb_get_guint8(tvb, offset);
  msg_type_str = val_to_str(message_type, hartip_message_type_values, "Unknown message type %d");
  proto_tree_add_item(hdr_tree, hf_hartip_hdr_message_type, tvb, offset, 1, ENC_BIG_ENDIAN);
  offset += 1;

  message_id = tvb_get_guint8(tvb, offset);
  msg_id_str = val_to_str(message_id, hartip_message_id_values, "Unknown message %d");
  proto_tree_add_item(hdr_tree, hf_hartip_hdr_message_id, tvb, offset, 1, ENC_BIG_ENDIAN);
  offset += 1;

  /* Setup statistics for tap. */
  tapinfo = wmem_new(wmem_packet_scope(), hartip_tap_info);
  tapinfo->message_type = message_type;
  tapinfo->message_id   = message_id;
  tap_queue_packet(hartip_tap, pinfo, tapinfo);

  if (message_id == SESSION_INITIATE_ID) {
    hartip_set_conversation(pinfo);
  }

  proto_tree_add_item(hdr_tree, hf_hartip_hdr_status, tvb, offset, 1, ENC_BIG_ENDIAN);
  offset += 1;

  transaction_id = tvb_get_ntohs(tvb, offset);
  proto_tree_add_item(hdr_tree, hf_hartip_hdr_transaction_id, tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_item_append_text(hart_item, ", %s %s, Sequence Number %d", msg_id_str,
                         msg_type_str, transaction_id);

  col_append_sep_fstr(pinfo->cinfo, COL_INFO, " | ",
                      "%s %s, Sequence Number %d",
                      msg_id_str, msg_type_str, transaction_id);
  col_set_fence(pinfo->cinfo, COL_INFO);

  proto_tree_add_item(hdr_tree, hf_hartip_hdr_msg_length, tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  if (length < HARTIP_HEADER_LENGTH)
    return tvb_reported_length(tvb);
  bodylen = length - HARTIP_HEADER_LENGTH;

  /* add body elements. */
  body_tree = proto_tree_add_subtree_format(hartip_tree, tvb, offset, bodylen,
                           ett_hartip_body, NULL,
                           "HART_IP Body, %s, %s", msg_id_str, msg_type_str);

  if ((message_type == ERROR_MSG_TYPE) ||
      (message_type == NAK_MSG_TYPE)) {
    offset += dissect_error(body_tree, tvb, offset, bodylen);
  } else {
    /*  Dissect the various HARTIP messages. */
    switch(message_id) {
    case SESSION_INITIATE_ID:
      offset += dissect_session_init(body_tree, tvb, offset, bodylen);
      break;
    case SESSION_CLOSE_ID:
      offset += dissect_session_close(body_tree, pinfo, tvb, offset, bodylen);
      break;
    case KEEP_ALIVE_ID:
      offset += dissect_keep_alive(body_tree, pinfo, tvb, offset, bodylen);
      break;
    case PASS_THROUGH_ID:
      offset += dissect_pass_through(body_tree, tvb, offset, bodylen);
      break;
    default:
      proto_tree_add_item(body_tree, hf_hartip_data, tvb, offset, bodylen, ENC_NA);
      offset += bodylen;
      break;
    }
  }

  return offset;
}

static guint
get_dissect_hartip_len(packet_info *pinfo _U_, tvbuff_t *tvb,
                       int offset, void *data _U_)
{
  return tvb_get_ntohs(tvb, offset+6);
}

static int
dissect_hartip_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
  return dissect_hartip_common(tvb, pinfo, tree, 0);
}

static int
dissect_hartip_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                   void *data)
{
  /* Simple hueristic check*/
  const gchar * msg_str = try_val_to_str(tvb_get_guint8(tvb, 1), hartip_message_type_values);
  if (!msg_str) {
    return 0;
  }

  msg_str = try_val_to_str(tvb_get_guint8(tvb, 2), hartip_message_id_values);
  if (!msg_str) {
    return 0;
  }

  tcp_dissect_pdus(tvb, pinfo, tree, hartip_desegment, HARTIP_HEADER_LENGTH,
                   get_dissect_hartip_len, dissect_hartip_pdu, data);
  return tvb_reported_length(tvb);
}

static int
dissect_hartip_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                   void *data _U_)
{
  gint offset = 0;

  /* Simple hueristic check*/
  const gchar * msg_str = try_val_to_str(tvb_get_guint8(tvb, 1), hartip_message_type_values);
  if (!msg_str) {
    return 0;
  }

  msg_str = try_val_to_str(tvb_get_guint8(tvb, 2), hartip_message_id_values);
  if (!msg_str) {
    return 0;
  }

  while (tvb_reported_length_remaining(tvb, offset) >= HARTIP_HEADER_LENGTH)
    offset += dissect_hartip_common(tvb, pinfo, tree, offset);

  return offset;
}

void
proto_register_hartip(void)
{
  static hf_register_info hf[] = {
    /* HARTIP header elements. */
    { &hf_hartip_hdr_version,
      { "Version",           "hart_ip.version",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        "HART_IP version number", HFILL }
    },
    { &hf_hartip_hdr_message_type,
      { "Message Type",           "hart_ip.message_type",
        FT_UINT8, BASE_DEC, VALS(hartip_message_type_values), 0,
        "HART_IP message type", HFILL }
    },
    { &hf_hartip_hdr_message_id,
      { "Message ID",           "hart_ip.message_id",
        FT_UINT8, BASE_DEC, VALS(hartip_message_id_values), 0,
        "HART_IP message id", HFILL }
    },
    { &hf_hartip_hdr_status,
      { "Status",           "hart_ip.status",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        "HART_IP status field", HFILL }
    },
    { &hf_hartip_hdr_transaction_id,
      { "Sequence Number",           "hart_ip.transaction_id",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        "HART_IP Sequence Number", HFILL }
    },
    { &hf_hartip_hdr_msg_length,
      { "Message Length",           "hart_ip.msg_length",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        "HART_IP Message Length", HFILL }
    },

    /* HARTIP Body elements   */
    { &hf_hartip_data,
      { "Message Data",           "hart_ip.data",
        FT_BYTES, BASE_NONE, NULL, 0x0,
        "HART_IP Message Data", HFILL }
    },
    { &hf_hartip_master_type,
      { "Host Type",           "hart_ip.session_init.master_type",
        FT_UINT8, BASE_DEC, VALS(hartip_master_type_values), 0x0,
        "Session Host Type", HFILL }
    },
    { &hf_hartip_inactivity_close_timer,
      { "Inactivity Close Timer",           "hart_ip.session_init.inactivity_close_timer",
        FT_UINT32, BASE_DEC, NULL, 0x0,
        "Session Inactivity Close Timer", HFILL }
    },
    { &hf_hartip_error_code,
      { "Error",           "hart_ip.error.error_code",
        FT_UINT8, BASE_DEC, VALS(hartip_error_code_values), 0x0,
        "Error Code", HFILL }
    },

    /* HARTIP Pass-through commads. */
    { &hf_hartip_pt_preambles,
      { "Preambles",           "hart_ip.pt.preambles",
        FT_BYTES, BASE_NONE, NULL, 0x0,
        "Pass Through Preambles", HFILL }
    },
    { &hf_hartip_pt_delimiter,
      { "Delimiter",           "hart_ip.pt.delimiter",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        "Pass Through Delimiter", HFILL }
    },
    { &hf_hartip_pt_delimiter_frame_type,
      { "Frame Type",           "hart_ip.pt.delimiter.frame_type",
        FT_UINT8, BASE_DEC, VALS(hartip_pt_delimiter_frame_type_values), 0x07,
        NULL, HFILL }
    },
    { &hf_hartip_pt_delimiter_physical_layer_type,
      { "Physical Layer Type",           "hart_ip.pt.delimiter.physical_layer_type",
        FT_UINT8, BASE_DEC, VALS(hartip_pt_delimiter_physical_layer_type_values), 0x18,
        NULL, HFILL }
    },
    { &hf_hartip_pt_delimiter_number_of_expansion_bytes,
      { "Number of Expansion Bytes",           "hart_ip.pt.delimiter.number_of_expansion_bytes",
        FT_UINT8, BASE_DEC, NULL, 0x60,
        NULL, HFILL }
    },
    { &hf_hartip_pt_delimiter_address_type,
      { "Address Type",           "hart_ip.pt.delimiter.address_type",
        FT_UINT8, BASE_DEC, VALS(hartip_pt_delimiter_address_type_values), 0x80,
        NULL, HFILL }
    },
    { &hf_hartip_pt_short_addr,
      { "Short Address",           "hart_ip.pt.short_addr",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        "Pass Through Short Address", HFILL }
    },
    { &hf_hartip_pt_long_addr,
      { "Long Address",           "hart_ip.pt.long_address",
        FT_BYTES, BASE_NONE, NULL, 0x0,
        "Pass Through Long Address", HFILL }
    },
        { &hf_hartip_pt_expansion_bytes,
      { "Expansion Bytes",           "hart_ip.pt.expansion_bytes",
        FT_BYTES, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_command,
      { "Command",           "hart_ip.pt.command",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        "Pass Through Command", HFILL }
    },
    { &hf_hartip_pt_length,
      { "Length",           "hart_ip.pt.length",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        "Pass Through Length", HFILL }
    },
    { &hf_hartip_pt_response_code,
      { "Response Code",           "hart_ip.pt.response_code",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        "Pass Through Response Code", HFILL }
    },
    { &hf_hartip_pt_device_status,
      { "Device Status",           "hart_ip.pt.device_status",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        "Pass Through Device Status", HFILL }
    },
    { &hf_hartip_pt_payload,
      { "Payload",           "hart_ip.pt.payload",
        FT_BYTES, BASE_NONE, NULL, 0x0,
        "Pass Through Payload", HFILL }
    },
    { &hf_hartip_pt_checksum,
      { "Checksum",           "hart_ip.pt.checksum",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        "Pass Through Checksum", HFILL }
    },

    /* add fields for universal commands. */
    /* command 0 */
    { &hf_hartip_pt_rsp_expansion_code,
      { "Expansion Code",           "hart_ip.pt.rsp.expansion_code",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_expanded_device_type,
      { "Expanded Device Type",           "hart_ip.pt.rsp.expanded_device_type",
        FT_UINT16, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_req_min_preambles,
      { "Minimum Number of Request Preambles",           "hart_ip.pt.rsp.req_min_preambles",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_hart_protocol_major_rev,
      { "HART Universal Revision",           "hart_ip.pt.rsp.hart_univ_rev",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_device_rev,
      { "Device Revision",           "hart_ip.pt.rsp.device_rev",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_software_rev,
      { "Device Software Revision",           "hart_ip.pt.rsp.software_rev",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_hardware_rev_physical_signal,
      { "Hardware Rev and Physical Signaling",           "hart_ip.pt.rsp.hardrev_and_physical_signal",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_flage,
      { "Flags",           "hart_ip.pt.rsp.flags",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_device_id,
      { "Device ID",           "hart_ip.pt.rsp.device_id",
        FT_BYTES, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_rsp_min_preambles,
      { "Minimum Number of Response Preambles",           "hart_ip.pt.rsp.rsp_min_preambles",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_max_device_variables,
      { "Maximum Number of Device Variables",           "hart_ip.pt.rsp.device_variables",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_configuration_change_counter,
      { "Configuration Change Counter",           "hart_ip.pt.rsp.configure_change",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_extended_device_status,
      { "Extended Device Status",           "hart_ip.pt.rsp.ext_device_status",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_manufacturer_Identification_code,
      { "Manufacturer ID",           "hart_ip.pt.rsp.manufacturer_Id",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_private_label,
      { "Private Label",           "hart_ip.pt.rsp.private_label",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_device_profile,
      { "Device Profile",           "hart_ip.pt.rsp.device_profile",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },

    /* command 2 */
    { &hf_hartip_pt_rsp_pv_percent_range,
      { "PV Percent Range",           "hart_ip.pt.rsp.pv_percent_range",
        FT_FLOAT, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },

    /* command 3 */
    { &hf_hartip_pt_rsp_pv_loop_current,
      { "PV Loop Current",           "hart_ip.pt.rsp.pv_loop_current",
        FT_FLOAT, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_pv_units,
      { "PV Units",           "hart_ip.pt.rsp.pv_units",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_pv,
      { "PV",           "hart_ip.pt.rsp.pv",
        FT_FLOAT, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_sv_units,
      { "SV Units",           "hart_ip.pt.rsp.sv_units",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_sv,
      { "SV",           "hart_ip.pt.rsp.sv",
        FT_FLOAT, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_tv_units,
      { "TV Units",           "hart_ip.pt.rsp.tv_units",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_tv,
      { "TV",           "hart_ip.pt.rsp.tv",
        FT_FLOAT, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_qv_units,
      { "QV Units",           "hart_ip.pt.rsp.qv_units",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_qv,
      { "QV",           "hart_ip.pt.rsp.qv",
        FT_FLOAT, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },

    /* command 7 */
    { &hf_hartip_pt_rsp_loop_current_mode,
      { "Loop Current Mode",                     "hart_ip.pt.rsp.loop_current_mode",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },

    /* command 8 */
    { &hf_hartip_pt_rsp_primary_var_classify,
      { "Primary Variable Classification",       "hart_ip.pt.rsp.primary_variable_classification",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_secondary_var_classify,
      { "Secondary Variable Classification",     "hart_ip.pt.rsp.secondary_variable_classification",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_tertiary_var_classify,
      { "Tertiary Variable Classification",      "hart_ip.pt.rsp.tertiary_variable_classification",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_quaternary_var_classify,
      { "Quaternary Variable Classification",    "hart_ip.pt.rsp.quaternary_variable_classification",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },

    /* command 9 */
    { &hf_hartip_pt_rsp_slot0_device_var,
      { "Slot0 Device Variable",           "hart_ip.pt.rsp.slot0_device_var",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot0_device_var_classify,
      { "Slot0 Device Variable Classification",  "hart_ip.pt.rsp.slot0_device_var_classification",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot0_units,
      { "Slot0 Units",           "hart_ip.pt.rsp.slot0_units",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot0_device_var_value,
      { "Slot0 Device Variable Value",           "hart_ip.pt.rsp.slot0_device_var_value",
        FT_FLOAT, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot0_device_var_status,
      { "Slot0 Device Variable Status",           "hart_ip.pt.rsp.slot0_device_var_status",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot1_device_var,
      { "Slot1 Device Variable",           "hart_ip.pt.rsp.slot1_device_var",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot1_device_var_classify,
      { "Slot1 Device Variable Classification",           "hart_ip.pt.rsp.slot1_device_var_classify",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot1_units,
      { "Slot1 Units",           "hart_ip.pt.rsp.slot1_units",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot1_device_var_value,
      { "Slot1 Device Variable Value",           "hart_ip.pt.rsp.slot1_device_var_value",
        FT_FLOAT, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot1_device_var_status,
      { "Slot1 Device Variable Status",           "hart_ip.pt.rsp.slot1_device_var_status",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot2_device_var,
      { "Slot2 Device Variable",           "hart_ip.pt.rsp.slot2_device_var",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot2_device_var_classify,
      { "Slot2 Device Variable Classification",           "hart_ip.pt.rsp.slot2_device_var_classify",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot2_units,
      { "Slot2 Units",           "hart_ip.pt.rsp.slot2_units",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot2_device_var_value,
      { "Slot2 Device Variable Value",           "hart_ip.pt.rsp.slot2_device_var_value",
        FT_FLOAT, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot2_device_var_status,
      { "Slot2 Device Variable Status",           "hart_ip.pt.rsp.slot2_device_var_status",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot3_device_var,
      { "Slot3 Device Variable",           "hart_ip.pt.rsp.slot3_device_var",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot3_device_var_classify,
      { "Slot3 Device Variable Classification",           "hart_ip.pt.rsp.slot3_device_var_classify",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot3_units,
      { "Slot3 Units",           "hart_ip.pt.rsp.slot3_units",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot3_device_var_value,
      { "Slot3 Device Variable Value",           "hart_ip.pt.rsp.slot3_device_var_value",
        FT_FLOAT, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot3_device_var_status,
      { "Slot3 Device Variable Status",           "hart_ip.pt.rsp.slot3_device_var_status",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot4_device_var,
      { "Slot4 Device Variable",           "hart_ip.pt.rsp.slot4_device_var",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot4_device_var_classify,
      { "Slot4 Device Variable Classification",           "hart_ip.pt.rsp.slot4_device_var_classify",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot4_units,
      { "Slot4 Units",           "hart_ip.pt.rsp.slot4_units",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot4_device_var_value,
      { "Slot4 Device Variable Value",           "hart_ip.pt.rsp.slot4_device_var_value",
        FT_FLOAT, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot4_device_var_status,
      { "Slot4 Device Variable Status",           "hart_ip.pt.rsp.slot4_device_var_status",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot5_device_var,
      { "Slot5 Device Variable",           "hart_ip.pt.rsp.slot5_device_var",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot5_device_var_classify,
      { "Slot5 Device Variable Classification",           "hart_ip.pt.rsp.slot5_device_var_classify",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot5_units,
      { "Slot5 Units",           "hart_ip.pt.rsp.slot5_units",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot5_device_var_value,
      { "Slot5 Device Variable Value",           "hart_ip.pt.rsp.slot5_device_var_value",
        FT_FLOAT, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot5_device_var_status,
      { "Slot5 Device Variable Status",           "hart_ip.pt.rsp.slot5_device_var_status",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot6_device_var,
      { "Slot6 Device Variable",           "hart_ip.pt.rsp.slot6_device_var",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot6_device_var_classify,
      { "Slot6 Device Variable Classification",           "hart_ip.pt.rsp.slot6_device_var_classify",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot6_units,
      { "Slot6 Units",           "hart_ip.pt.rsp.slot6_units",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot6_device_var_value,
      { "Slot6 Device Variable Value",           "hart_ip.pt.rsp.slot6_device_var_value",
        FT_FLOAT, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot6_device_var_status,
      { "Slot6 Device Variable Status",           "hart_ip.pt.rsp.slot6_device_var_status",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot7_device_var,
      { "Slot7 Device Variable",           "hart_ip.pt.rsp.slot7_device_var",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot7_device_var_classify,
      { "Slot7 Device Variable Classification",           "hart_ip.pt.rsp.slot7_device_var_classify",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot7_units,
      { "Slot7 Units",           "hart_ip.pt.rsp.slot7_units",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot7_device_var_value,
      { "Slot7 Device Variable Value",           "hart_ip.pt.rsp.slot7_device_var_value",
        FT_FLOAT, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot7_device_var_status,
      { "Slot7 Device Variable Status",           "hart_ip.pt.rsp.slot7_device_var_status",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot0_timestamp,
      { "Slot0 Data TimeStamp",           "hart_ip.pt.rsp.slot0_data_timestamp",
        FT_BYTES, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },

    /* command 13 */
    { &hf_hartip_pt_rsp_packed_descriptor,
      { "Descriptor",           "hart_ip.pt.rsp.descriptor",
        FT_STRINGZPAD, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_day,
      { "Day",           "hart_ip.pt.rsp.day",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_month,
      { "Month",           "hart_ip.pt.rsp.month",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_year,
      { "Year",           "hart_ip.pt.rsp.year",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },

    /* command 14 */
    { &hf_hartip_pt_rsp_transducer_serial_number,
      { "Transducer Serail Number",              "hart_ip.pt.rsp.transducer_serail_number",
        FT_BYTES, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_transducer_limit_min_span_units,
      { "Transducer Limit Min Span Units",  "hart_ip.pt.rsp.transducer_limit_min_span_units",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_upper_transducer_limit,
      { "Upper Transducer Limit",                "hart_ip.pt.rsp.upper_transducer_limit",
        FT_FLOAT, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_lower_transducer_limit,
      { "Lower Transducer Limit",                "hart_ip.pt.rsp.lower_transducer_limit",
        FT_FLOAT, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_minimum_span,
      { "Minimum Span",                          "hart_ip.pt.rsp.minimum_span",
        FT_FLOAT, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },

    /* command 15 */
    { &hf_hartip_pt_rsp_pv_alarm_selection_code,
      { "PV Alarm Selection Code",                     "hart_ip.pt.rsp.pv_alarm_selection_code",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_pv_transfer_function_code,
      { "PV Transfer Function Code",                   "hart_ip.pt.rsp.pv_transfer_function_code",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_pv_upper_and_lower_range_values_units,
      { "PV Upper and Lower Range Values Units",  "hart_ip.pt.rsp.pv_upper_and_lower_range_values_units",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_pv_upper_range_value,
      { "PV Upper Range Value",                        "hart_ip.pt.rsp.pv_upper_range_value",
        FT_FLOAT, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_pv_lower_range_value,
      { "PV Lower Range Value",                        "hart_ip.pt.rsp.pv_lower_range_value",
        FT_FLOAT, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_pv_damping_value,
      { "PV Damping Value",                            "hart_ip.pt.rsp.pv_damping_value",
        FT_FLOAT, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_write_protect_code,
      { "Write Protect Code",                          "hart_ip.pt.rsp.write_protect_code",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_reserved,
      { "Reserved",                                    "hart_ip.pt.rsp.reserved",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_pv_analog_channel_flags,
      { "PV Analog Channel Flags",                     "hart_ip.pt.rsp.pv_analog_channel_flags",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },

    /* command 16 and 19 */
    { &hf_hartip_pt_rsp_final_assembly_number,
      { "Final Assembly Number",                       "hart_ip.pt.rsp.final_assembly_number",
        FT_BYTES, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },

    /* Tag */
    { &hf_hartip_pt_rsp_tag,
      { "Tag",           "hart_ip.pt.rsp.tag",
        FT_STRINGZPAD, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },

    /* Message */
    { &hf_hartip_pt_rsp_message,
      { "Message",           "hart_ip.pt.rsp.message",
        FT_STRINGZPAD, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },

    /* command 48 */
    { &hf_hartip_pt_rsp_device_sp_status,
      { "Device-Specific Status",           "hart_ip.pt.rsp.device_sp_status",
        FT_BYTES, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_device_op_mode,
      { "Device Operating Mode",           "hart_ip.pt.rsp.device_op_mode",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_standardized_status_0,
      { "Standardized Status 0",           "hart_ip.pt.rsp.standardized_status_0",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_standardized_status_1,
      { "Standardized Status 1",           "hart_ip.pt.rsp.standardized_status_1",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_analog_channel_saturated,
      { "Analog Channel Saturated",           "hart_ip.pt.rsp.analog_channel_saturated",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_standardized_status_2,
      { "Standardized Status 2",           "hart_ip.pt.rsp.standardized_status_2",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_standardized_status_3,
      { "Standardized Status 3",           "hart_ip.pt.rsp.standardized_status_3",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_analog_channel_fixed,
      { "Analog Channel Fixed",           "hart_ip.pt.rsp.analog_channel_fixed",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },

    /* command 77 */
    { &hf_hartip_pt_rsp_io_card,
      { "IO Card",                     "hart_ip.pt.rsp.io_card",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_channel,
      { "Channel",                     "hart_ip.pt.rsp.channel",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_req_tx_preamble_count,
      { "Transmit Preamble Count",     "hart_ip.pt.rsp.tx_preamble_count",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_embedded_cmd_delimiter,
      { "Embedded Command Delimiter",  "hart_ip.pt.rsp.embedded_command_delimiter",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_poll_address,
      { "Poll Address",                "hart_ip.pt.rsp.poll_address",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        "Embedded Command Short Address", HFILL }
    },
    { &hf_hartip_pt_rsp_unique_id,
      { "Unique ID",                   "hart_ip.pt.rsp.unique_id",
        FT_BYTES, BASE_NONE, NULL, 0x0,
        "Embedded Command Long Address", HFILL }
    },
    { &hf_hartip_pt_rsp_embedded_cmd,
      { "Embedded Command",            "hart_ip.pt.rsp.embedded_command",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },

    /* command 178 */
    { &hf_hartip_pt_rsp_number_of_commands,
      { "Number of Commands",          "hart_ip.pt.rsp.number_of_commands",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    /* command 31 and 178 */
    { &hf_hartip_pt_rsp_command_number,
      { "Command number",              "hart_ip.pt.rsp.command_number",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_data,
      { "Data",                        "hart_ip.pt.rsp.data",
        FT_BYTES, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
    /* command 77 and 178 */
    { &hf_hartip_pt_rsp_command_byte_count,
      { "Command byte count",          "hart_ip.pt.rsp.command_byte_count",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },

    /* command 203 */
    { &hf_hartip_pt_rsp_index_of_first_discrete_var,
      { "Index of First Discrete Variable",           "hart_ip.pt.rsp.index_of_first_discrete_var",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_number_of_discrete_vars,
      { "Number of Discrete Variables",               "hart_ip.pt.rsp.number_of_discrete_vars",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_timestamp_for_most_recent_discrete_change,
      { "TimeStamp for Most Recent Discrete Change",  "hart_ip.pt.rsp.timestamp_for_most_recent_discrete_change",
        FT_BYTES, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot0_discrete_var_state,
      { "Slot0 Discrete Variable State",              "hart_ip.pt.rsp.slot0_discrete_var_state",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot0_discrete_var_status,
      { "Slot0 Discrete Variable Status",             "hart_ip.pt.rsp.slot0_discrete_var_status",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot1_discrete_var_state,
      { "Slot1 Discrete Variable State",              "hart_ip.pt.rsp.slot1_discrete_var_state",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot1_discrete_var_status,
      { "Slot1 Discrete Variable Status",             "hart_ip.pt.rsp.slot1_discrete_var_status",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot2_discrete_var_state,
      { "Slot2 Discrete Variable State",              "hart_ip.pt.rsp.slot2_discrete_var_state",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot2_discrete_var_status,
      { "Slot2 Discrete Variable Status",             "hart_ip.pt.rsp.slot2_discrete_var_status",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot3_discrete_var_state,
      { "Slot3 Discrete Variable State",              "hart_ip.pt.rsp.slot3_discrete_var_state",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot3_discrete_var_status,
      { "Slot3 Discrete Variable Status",             "hart_ip.pt.rsp.slot3_discrete_var_status",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot4_discrete_var_state,
      { "Slot4 Discrete Variable State",              "hart_ip.pt.rsp.slot4_discrete_var_state",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot4_discrete_var_status,
      { "Slot4 Discrete Variable Status",             "hart_ip.pt.rsp.slot4_discrete_var_status",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot5_discrete_var_state,
      { "Slot5 Discrete Variable State",              "hart_ip.pt.rsp.slot5_discrete_var_state",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_hartip_pt_rsp_slot5_discrete_var_status,
      { "Slot5 Discrete Variable Status",             "hart_ip.pt.rsp.slot5_discrete_var_status",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL }
    }
  };

  static gint *ett[] = {
    &ett_hartip,
    &ett_hartip_hdr,
    &ett_hartip_body,
    &ett_hartip_pt_delimiter
  };

  static ei_register_info ei[] = {
     { &ei_hartip_data_none, { "hart_ip.data.none", PI_PROTOCOL, PI_NOTE, "No data", EXPFILL }},
     { &ei_hartip_data_unexpected, { "hart_ip.data.unexpected", PI_PROTOCOL, PI_WARN, "Unexpected message body", EXPFILL }},
  };

  module_t *hartip_module;
  expert_module_t* expert_hartip;

  proto_hartip = proto_register_protocol("HART_IP Protocol", "HART_IP", "hart_ip");
  proto_register_field_array(proto_hartip, hf, array_length(hf));
  proto_register_subtree_array(ett, array_length(ett));
  expert_hartip = expert_register_protocol(proto_hartip);
  expert_register_field_array(expert_hartip, ei, array_length(ei));

  hartip_module = prefs_register_protocol(proto_hartip, NULL);
  prefs_register_bool_preference(hartip_module, "desegment",
                                  "Desegment all HART-IP messages spanning multiple TCP segments",
                                  "Whether the HART-IP dissector should desegment all messages spanning multiple TCP segments",
                                  &hartip_desegment);

  hartip_tap = register_tap("hart_ip");

  hartip_udp_handle = register_dissector_with_description("hart_ip", "HART-IP over UDP", dissect_hartip_udp, proto_hartip);
  hartip_tcp_handle = register_dissector_with_description("hart_ip.tcp", "HART-IP over TCP", dissect_hartip_tcp, proto_hartip);
}

void
proto_reg_handoff_hartip(void)
{
  dissector_add_uint_with_preference("udp.port", HARTIP_PORT, hartip_udp_handle);
  dissector_add_uint_with_preference("tcp.port", HARTIP_PORT, hartip_tcp_handle);

  stats_tree_register("hart_ip", "hart_ip", "HART-IP", 0,
                      hartip_stats_tree_packet, hartip_stats_tree_init, NULL);
}

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