f12a0dfb962846e95612eb1ee8883894e0ce84e8
[metze/wireshark/wip.git] / epan / dissectors / packet-opensafety.c
1 /* packet-opensafety.c
2  *
3  * $Id$
4  *
5  *   openSAFETY is a machine-safety protocol, encapsulated in modern fieldbus
6  *   and industrial ethernet solutions.
7  *
8  *   For more information see http://www.open-safety.org
9  *
10  *   This dissector currently supports the following transport protocols
11  *
12  *   - openSAFETY using POWERLINK
13  *   - openSAFETY using SercosIII
14  *   - openSAFETY using Generic UDP
15  *   - openSAFETY using Modbus/TCP
16  *   - openSAFETY using (openSAFETY over UDP) transport
17  *   - openSAFETY using ProfiNet IO
18  *
19  * By Roland Knall <roland.knall@br-automation.com>
20  * Copyright 2011-2012 Bernecker + Rainer Industrie-Elektronik Ges.m.b.H.
21  *
22  * Wireshark - Network traffic analyzer
23  * By Gerald Combs <gerald@wireshark.org>
24  * Copyright 1998 Gerald Combs
25  *
26  * This program is free software; you can redistribute it and/or
27  * modify it under the terms of the GNU General Public License
28  * as published by the Free Software Foundation; either version 2
29  * of the License, or (at your option) any later version.
30  *
31  * This program is distributed in the hope that it will be useful,
32  * but WITHOUT ANY WARRANTY; without even the implied warranty of
33  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34  * GNU General Public License for more details.
35  *
36  * You should have received a copy of the GNU General Public License
37  * along with this program; if not, write to the Free Software
38  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
39  */
40
41 #include "config.h"
42
43 #include <glib.h>
44
45 #include <epan/packet.h>
46 #include <epan/prefs.h>
47 #include <epan/etypes.h>
48 #include <epan/wmem/wmem.h>
49 #include <epan/expert.h>
50 #include <epan/reassemble.h>
51 #include <epan/strutil.h>
52 #include <epan/dissectors/packet-udp.h>
53 #include <epan/dissectors/packet-frame.h>
54
55 #include <wsutil/crc8.h>
56 #include <wsutil/crc16.h>
57
58 #include <stdio.h>
59 #include <string.h>
60
61
62 /* General definitions */
63
64 /* openSAFETY UDP Port */
65 #ifndef UDP_PORT_OPENSAFETY
66 #define UDP_PORT_OPENSAFETY   9877
67 #endif
68
69 /* SercosIII UDP Port */
70 #ifndef UDP_PORT_SIII
71 #define UDP_PORT_SIII         8755
72 #endif
73
74 #define OPENSAFETY_DEFAULT_DOMAIN       0x1
75
76 /* Under linux, this get's defined in netinet/in.h */
77 #ifndef IPPROTO_UDP
78 #define IPPROTO_UDP 0x11
79 #endif
80
81 #ifndef OPENSAFETY_PINFO_CONST_DATA
82 #define OPENSAFETY_PINFO_CONST_DATA 0xAABBCCDD
83 #endif
84
85 /* openSAFETY CRC types */
86 #define OPENSAFETY_CHECKSUM_CRC8        0x01
87 #define OPENSAFETY_CHECKSUM_CRC16       0x02
88 #define OPENSAFETY_CHECKSUM_CRC32       0x04
89 #define OPENSAFETY_CHECKSUM_CRC16SLIM   0x08
90
91 static const value_string message_crc_type[] = {
92     { OPENSAFETY_CHECKSUM_CRC8,         "CRC8" },
93     { OPENSAFETY_CHECKSUM_CRC16,        "CRC16" },
94     { OPENSAFETY_CHECKSUM_CRC32,        "CRC32" },
95     { OPENSAFETY_CHECKSUM_CRC16SLIM,    "CRC16 Slim" },
96     { 0, NULL }
97 };
98
99 /* openSAFETY Message Types */
100 #define OPENSAFETY_SPDO_MESSAGE_TYPE           0xC0
101 #define OPENSAFETY_SSDO_MESSAGE_TYPE           0xE0
102 #define OPENSAFETY_SLIM_SSDO_MESSAGE_TYPE      0xE8
103 #define OPENSAFETY_SNMT_MESSAGE_TYPE           0xA0
104
105 static const value_string message_id_values[] = {
106     { OPENSAFETY_SPDO_MESSAGE_TYPE,      "openSAFETY SPDO" },
107     { OPENSAFETY_SSDO_MESSAGE_TYPE,      "openSAFETY SSDO" },
108     { OPENSAFETY_SLIM_SSDO_MESSAGE_TYPE, "openSAFETY Slim SSDO" },
109     { OPENSAFETY_SNMT_MESSAGE_TYPE,      "openSAFETY SNMT" },
110     { 0, NULL }
111 };
112
113 /* openSAFETY Message IDs */
114 #define OPENSAFETY_MSG_SPDO_DATA_ONLY                  0xC0
115 #define OPENSAFETY_MSG_SPDO_DATA_WITH_TIME_REQUEST     0xC8
116 #define OPENSAFETY_MSG_SPDO_DATA_WITH_TIME_RESPONSE    0xD0
117 #define OPENSAFETY_MSG_SPDO_RESERVED                   0xD8
118
119 #define OPENSAFETY_MSG_SSDO_SERVICE_REQUEST            0xE0
120 #define OPENSAFETY_MSG_SSDO_SERVICE_RESPONSE           0xE4
121 #define OPENSAFETY_MSG_SSDO_SLIM_SERVICE_REQUEST       0xE8
122 #define OPENSAFETY_MSG_SSDO_SLIM_SERVICE_RESPONSE      0xEC
123
124 #define OPENSAFETY_MSG_SNMT_REQUEST_UDID               0xA0
125 #define OPENSAFETY_MSG_SNMT_RESPONSE_UDID              0xA4
126 #define OPENSAFETY_MSG_SNMT_ASSIGN_SADR                0xA8
127 #define OPENSAFETY_MSG_SNMT_SADR_ASSIGNED              0xAC
128 #define OPENSAFETY_MSG_SNMT_SERVICE_REQUEST            0xB0
129 #define OPENSAFETY_MSG_SNMT_SERVICE_RESPONSE           0xB4
130 #define OPENSAFETY_MSG_SNMT_SN_RESET_GUARDING_SCM      0xBC
131
132 static const value_string message_type_values[] = {
133     { OPENSAFETY_MSG_SPDO_DATA_ONLY,               "SPDO Data only" },
134     { OPENSAFETY_MSG_SPDO_DATA_WITH_TIME_REQUEST,  "SPDO Data with Time Request" },
135     { OPENSAFETY_MSG_SPDO_DATA_WITH_TIME_RESPONSE, "SPDO Data with Time Response" },
136     { OPENSAFETY_MSG_SPDO_RESERVED,                "SPDO Reserved" },
137
138     { OPENSAFETY_MSG_SSDO_SERVICE_REQUEST,         "SSDO Service Request" },
139     { OPENSAFETY_MSG_SSDO_SERVICE_RESPONSE,        "SSDO Service Response" },
140     { OPENSAFETY_MSG_SSDO_SLIM_SERVICE_REQUEST,    "SSDO Slim Service Request" },
141     { OPENSAFETY_MSG_SSDO_SLIM_SERVICE_RESPONSE,   "SSDO Slim Service Response" },
142
143     { OPENSAFETY_MSG_SNMT_REQUEST_UDID,            "SNMT Request UDID" },
144     { OPENSAFETY_MSG_SNMT_RESPONSE_UDID,           "SNMT Response UDID" },
145     { OPENSAFETY_MSG_SNMT_ASSIGN_SADR,             "SNMT Assign SADR" },
146     { OPENSAFETY_MSG_SNMT_SADR_ASSIGNED,           "SNMT SADR Assigned" },
147     { OPENSAFETY_MSG_SNMT_SERVICE_REQUEST,         "SNMT Service Request" },
148     { OPENSAFETY_MSG_SNMT_SERVICE_RESPONSE,        "SNMT Service Response" },
149     { OPENSAFETY_MSG_SNMT_SN_RESET_GUARDING_SCM,   "SNMT SN reset guarding SCM" },
150     {0, NULL }
151 };
152
153 /* SNTM extended Services */
154 #define OPENSAFETY_MSG_SNMT_EXT_SN_SET_TO_PRE_OP              0x00
155 #define OPENSAFETY_MSG_SNMT_EXT_SN_SET_TO_OP                  0x02
156 #define OPENSAFETY_MSG_SNMT_EXT_SCM_SET_TO_STOP               0x04
157 #define OPENSAFETY_MSG_SNMT_EXT_SCM_SET_TO_OP                 0x06
158 #define OPENSAFETY_MSG_SNMT_EXT_SCM_GUARD_SN                  0x08
159 #define OPENSAFETY_MSG_SNMT_EXT_ASSIGN_ADDITIONAL_SADR        0x0A
160 #define OPENSAFETY_MSG_SNMT_EXT_SN_ACKNOWLEDGE                0x0C
161 #define OPENSAFETY_MSG_SNMT_EXT_SN_ASSIGN_UDID_SCM            0x0E
162 #define OPENSAFETY_MSG_SNMT_EXT_SN_STATUS_PRE_OP              0x01
163 #define OPENSAFETY_MSG_SNMT_EXT_SN_STATUS_OP                  0x03
164 #define OPENSAFETY_MSG_SNMT_EXT_SN_ASSIGNED_ADDITIONAL_SADR   0x05
165 #define OPENSAFETY_MSG_SNMT_EXT_SN_FAIL                       0x07
166 #define OPENSAFETY_MSG_SNMT_EXT_SN_BUSY                       0x09
167 #define OPENSAFETY_MSG_SNMT_EXT_SN_ASSIGNED_UDID_SCM          0x0F
168
169 static const value_string message_service_type[] = {
170     { OPENSAFETY_MSG_SNMT_EXT_SN_SET_TO_PRE_OP,             "SN set to pre Operational" },
171     { OPENSAFETY_MSG_SNMT_EXT_SN_SET_TO_OP,                 "SN set to Operational" },
172     { OPENSAFETY_MSG_SNMT_EXT_SCM_SET_TO_STOP,              "SCM set to Stop" },
173     { OPENSAFETY_MSG_SNMT_EXT_SCM_SET_TO_OP,                "SCM set to Operational" },
174     { OPENSAFETY_MSG_SNMT_EXT_SCM_GUARD_SN,                 "SCM guard SN" },
175     { OPENSAFETY_MSG_SNMT_EXT_ASSIGN_ADDITIONAL_SADR,       "Assign additional SADR" },
176     { OPENSAFETY_MSG_SNMT_EXT_SN_ACKNOWLEDGE,               "SN Acknowledge" },
177     { OPENSAFETY_MSG_SNMT_EXT_SN_ASSIGN_UDID_SCM,           "SN assign UDID SCM" },
178     { OPENSAFETY_MSG_SNMT_EXT_SN_STATUS_PRE_OP,             "SN status pre Operational" },
179     { OPENSAFETY_MSG_SNMT_EXT_SN_STATUS_OP,                 "SN status Operational" },
180     { OPENSAFETY_MSG_SNMT_EXT_SN_ASSIGNED_ADDITIONAL_SADR,  "Assigned additional SADR" },
181     { OPENSAFETY_MSG_SNMT_EXT_SN_FAIL,                      "SN Fail" },
182     { OPENSAFETY_MSG_SNMT_EXT_SN_BUSY,                      "SN Busy" },
183     { OPENSAFETY_MSG_SNMT_EXT_SN_ASSIGNED_UDID_SCM,         "SN assigned UDID SCM" },
184     { 0, NULL }
185 };
186
187 /* Values 5-255 are reserved for future use. They will be presented as "Reserved [%d]"
188  * during dissection
189  */
190 static const value_string sn_fail_error_group[] = {
191     { 1, "Application" },
192     { 2, "Parameter" },
193     { 3, "Vendor specific" },
194     { 4, "openSAFETY Stack" },
195     { 5, "Additional parameter needed" },
196     { 0, NULL }
197 };
198
199 /* SNTM extended Services */
200 #define OPENSAFETY_MSG_SSDO_ABORT                           0x04
201 #define OPENSAFETY_MSG_SSDO_UPLOAD_SEGMENT_MIDDLE           0x08
202 #define OPENSAFETY_MSG_SSDO_DOWNLOAD_SEGMENT_MIDDLE         0x09
203 #define OPENSAFETY_MSG_SSDO_UPLOAD_INITIATE_EXPEDITED       0x20
204 #define OPENSAFETY_MSG_SSDO_DOWNLOAD_INITIATE_EXPEDITED     0x21
205 #define OPENSAFETY_MSG_SSDO_UPLOAD_INITIATE_SEGMENTED       0x28
206 #define OPENSAFETY_MSG_SSDO_DOWNLOAD_INITIATE_SEGMENTED     0x29
207 #define OPENSAFETY_MSG_SSDO_UPLOAD_SEGMENT_END              0x48
208 #define OPENSAFETY_MSG_SSDO_DOWNLOAD_SEGMENT_END            0x49
209 #if 0
210 #define OPENSAFETY_MSG_SSDO_BLOCK_UPLOAD_SEGMENT_MIDDLE     0x88
211 #define OPENSAFETY_MSG_SSDO_BLOCK_DOWNLOAD_SEGMENT_MIDDLE   0x89
212 #define OPENSAFETY_MSG_SSDO_BLOCK_UPLOAD_INITIATE           0xA8
213 #define OPENSAFETY_MSG_SSDO_BLOCK_DOWNLOAD_INITIATE         0xA9
214 #define OPENSAFETY_MSG_SSDO_BLOCK_UPLOAD_INITIATE_EXPEDITED 0xC0
215 #define OPENSAFETY_MSG_SSDO_BLOCK_UPLOAD_SEGMENT_END        0x40
216 #define OPENSAFETY_MSG_SSDO_BLOCK_DOWNLOAD_SEGMENT_END      0xC9
217 #endif
218
219 static const value_string ssdo_sacmd_values[] = {
220 #if 0
221     { OPENSAFETY_MSG_SSDO_BLOCK_DOWNLOAD_SEGMENT_END,     "Block Download Segment End" },
222     { OPENSAFETY_MSG_SSDO_BLOCK_UPLOAD_INITIATE,          "Block Upload Expedited Initiate" },
223     { OPENSAFETY_MSG_SSDO_BLOCK_UPLOAD_INITIATE_EXPEDITED,"Block Upload Initiate" },
224     { OPENSAFETY_MSG_SSDO_BLOCK_DOWNLOAD_INITIATE,        "Block Download Initiate" },
225     { OPENSAFETY_MSG_SSDO_BLOCK_DOWNLOAD_SEGMENT_MIDDLE,  "Block Download Middle Segment" },
226     { OPENSAFETY_MSG_SSDO_BLOCK_UPLOAD_SEGMENT_MIDDLE,    "Block Upload Middle Segment" },
227     { OPENSAFETY_MSG_SSDO_BLOCK_UPLOAD_SEGMENT_END,       "Block Upload End Segment" },
228 #endif
229     { OPENSAFETY_MSG_SSDO_DOWNLOAD_SEGMENT_END,           "Download End Segment" },
230     { OPENSAFETY_MSG_SSDO_UPLOAD_SEGMENT_END,             "Upload End Segment" },
231     { OPENSAFETY_MSG_SSDO_DOWNLOAD_INITIATE_EXPEDITED,    "Download Expedited Initiate" },
232     { OPENSAFETY_MSG_SSDO_UPLOAD_INITIATE_SEGMENTED,      "Upload Initiate Segmented" },
233     { OPENSAFETY_MSG_SSDO_DOWNLOAD_INITIATE_SEGMENTED,    "Download Initiate Segmented" },
234     { OPENSAFETY_MSG_SSDO_UPLOAD_INITIATE_EXPEDITED,      "Upload Expedited Initiate" },
235     { OPENSAFETY_MSG_SSDO_DOWNLOAD_SEGMENT_MIDDLE,        "Download Middle Segment" },
236     { OPENSAFETY_MSG_SSDO_UPLOAD_SEGMENT_MIDDLE,          "Upload Middle Segment" },
237     { OPENSAFETY_MSG_SSDO_ABORT,                           "Abort" },
238     { 0, NULL }
239 };
240
241 #define OPENSAFETY_SSDO_SACMD_ACC  0x01
242 #define OPENSAFETY_SSDO_SACMD_RES  0x02
243 #define OPENSAFETY_SSDO_SACMD_ABRT 0x04
244 #define OPENSAFETY_SSDO_SACMD_SEG  0x08
245 #define OPENSAFETY_SSDO_SACMD_TGL  0x10
246 #define OPENSAFETY_SSDO_SACMD_INI  0x20
247 #define OPENSAFETY_SSDO_SACMD_ENSG 0x40
248 #define OPENSAFETY_SSDO_SACMD_BLK  0x80
249
250 static const true_false_string opensafety_sacmd_acc  = { "Write Access", "Read Access" };
251 #if 0
252 static const true_false_string opensafety_sacmd_res  = { "Reserved", "Reserved" };
253 #endif
254 static const true_false_string opensafety_sacmd_abrt = { "Abort Transfer", "Successful Transfer" };
255 static const true_false_string opensafety_sacmd_seg  = { "Segmented Access", "Expedited Access" };
256 static const true_false_string opensafety_on_off     = { "On", "Off" };
257 static const true_false_string opensafety_set_notset = { "Set", "Not set" };
258 static const true_false_string opensafety_sacmd_ini  = { "Initiate", "No Initiate" };
259 static const true_false_string opensafety_sacmd_ensg = { "No more segments", "More segments" };
260 static const true_false_string opensafety_sacmd_blk  = { "Block Transfer", "Normal Transfer" };
261
262 #define OPENSAFETY_SPDO_CONNECTION_VALID  0x04
263
264 #define OPENSAFETY_SOD_DVI 0x1018
265 #define OPENSAFETY_SOD_RXMAP 0x1800
266 #define OPENSAFETY_SOD_TXMAP 0xC000
267
268 static const value_string sod_idx_names[] = {
269     /* SSDO dictionary names, only names that are in common use are presented */
270     { 0x100C0000, "Life Guarding" },
271     { 0x100C0001, "Guard Time" },
272     { 0x100C0002, "LifeTimeFactor" },
273
274     { 0x100D0000, "Number of Retries for Reset Guarding" },
275
276     { 0x10180000 ,  "Device Vendor Information" },
277     { 0x10180001,  "VendorID" },
278     { 0x10180002,  "ProductCode" },
279     { 0x10180003,  "RevisionNumber" },
280     { 0x10180004,  "SerialNumber" },
281     { 0x10180005,  "FirmWareChecksum" },
282     { 0x10180006,  "Parameter Checksum" },
283     { 0x10180007,  "Parameter Timestamp" },
284
285     { 0x10190000,  "Unique Device ID" },
286     { 0x101A0000,  "Parameter Download" },
287     { 0x101B0000,  "SCM Parameters" },
288
289     { 0x12000000, "Common Communication Parameters" },
290     { 0x12000001, "Safety Domain Number" },
291     { 0x12000002, "SADR" },
292     { 0x12000003, "Consecutive Timebase" },
293     { 0x12000004, "UDID of SCM" },
294
295     { 0x14000000, "RxSPDO Communication Parameters" },
296     { 0x14000001, "SADR" },
297     { 0x14000002, "SCT" },
298     { 0x14000003, "Number of consecutive TReq" },
299     { 0x14000004, "Time delay TReq" },
300     { 0x14000005, "Time delay Sync" },
301     { 0x14000006, "Min TSync Propagation Delay" },
302     { 0x14000007, "Max TSync Propagation Delay" },
303     { 0x14000008, "Min SPDO Propagation Delay" },
304     { 0x14000009, "Max SPDO Propagation Delay" },
305     { 0x1400000A, "Best case TRes Delay" },
306     { 0x1400000B, "Time Request Cycle" },
307     { 0x1400000C, "TxSPDO No" },
308
309     { 0x18000000, "RxSPDO Mapping Parameters" },
310
311     { 0x1C000000, "TxSPDO Communication Parameters" },
312     { 0x1C000001, "SADR for broadcast" },
313     { 0x1C000002, "Refresh Prescale" },
314     { 0x1C000003, "Number of TRes" },
315
316     { 0x20000000, "Manufacturer Parameters" },
317     { 0x20010000, "Used Channels" },
318
319     { 0x21000000, "Safe Machine Options" },
320
321     { 0x21010000, "SDG CRC Configuration" },
322     { 0x21010001, "SDG CRC #1" },
323     { 0x21010002, "SDG CRC #2" },
324     { 0x21010003, "SDG CRC #3" },
325     { 0x21010004, "SDG CRC #4" },
326     { 0x21010005, "SDG CRC #5" },
327     { 0x21010006, "SDG CRC #6" },
328     { 0x21010007, "SDG CRC #7" },
329     { 0x21010008, "SDG CRC #8" },
330     { 0x21010009, "SDG CRC #9" },
331     { 0x2101000A, "SDG CRC #10" },
332
333     { 0x21120000, "Manufacturer - Module specific" },
334     { 0x21120002, "PDOmapRx" },
335     { 0x21120003, "PDOmapTx" },
336     { 0x21120004, "CycleTime min [us]" },
337     { 0x21120005, "CycleTime max [us]" },
338     { 0x21120006, "Used Channels (same as 0x2001)" },
339     { 0x21120007, "External Machine Options" },
340     { 0x21120008, "Parameter for SafeMC" },
341
342     { 0xC0000000, "TxSPDO Mapping Parameters" },
343
344     { 0xD0000000, "SCM Module Flags" },
345     { 0xD0000001, "BCM" },
346     { 0xD0000002, "Optional" },
347     { 0xD0000003, "Startup" },
348     { 0xD0000004, "EMOs" },
349     { 0xD0000005, "ext. Startup-Flags allowed" },
350     { 0xD0000006, "Remote-Ctrl allowed" },
351     { 0xD0000007, "Scans at startup" },
352     { 0xD0000008, "Not Present" },
353     { 0xD0000009, "Use Remanent Data" },
354     { 0xD000000A, "SCM-AR specific" },
355
356     { 0xD0100000, "Remanent Data" },
357     { 0xD0100001, "DINT" },
358
359     { 0xD0110000, "Remanent Data" },
360     { 0xD0110001, "DUINT" },
361
362     { 0, NULL }
363 };
364
365 static const value_string abort_codes[] = {
366
367     /* SSDO abort codes */
368     { 0x05030000,  "Reserved" },
369
370     { 0x05040000,  "SSDO protocol timed out" },
371     { 0x05040001,  "Client/server Command ID not valid or unknown" },
372     { 0x05040002,  "Invalid block size" },
373     { 0x05040003,  "Invalid sequence number" },
374     { 0x05040004,  "Reserved" },
375     { 0x05040005,  "Out of memory" },
376
377     { 0x06010000,  "Unsupported access to an object" },
378     { 0x06010001,  "Attempt to read a write-only object" },
379     { 0x06010002,  "Attempt to write a read-only object" },
380
381     { 0x06020000,  "Object does not exist in the object dictionary" },
382
383     { 0x06040041,  "Object cannot be mapped to the SPDO" },
384     { 0x06040042,  "The number and length of the objects to be mapped would exceed SPDO length" },
385     { 0x06040043,  "General parameter incompatibility" },
386     { 0x06040047,  "General internal incompatibility in the device" },
387
388     { 0x06060000,  "Access failed due to a hardware error" },
389
390     { 0x06070010,  "Data type does not match, length of service parameter does not match" },
391     { 0x06070012,  "Data type does not match, length of service parameter too high" },
392     { 0x06070013,  "Data type does not match, length of service parameter too low" },
393
394     { 0x06090011,  "Sub-index does not exist" },
395     { 0x06090030,  "Value range o parameter exceeded (only for write access)" },
396     { 0x06090031,  "Value of parameter written too high" },
397     { 0x06090032,  "Value of parameter written too low" },
398     { 0x06090036,  "Maximum value is less than minimum value" },
399
400     { 0x08000000,  "General error" },
401     { 0x08000020,  "Data cannot be transferred or stored to the application" },
402     { 0x08000021,  "Data cannot be transferred or stored to the application because of local control" },
403     { 0x08000022,  "Data cannot be transferred or stored to the application because of the present device state" },
404     { 0x08000023,  "Data cannot be transferred or stored to the application because of the object data is not available now" },
405
406     { 0, NULL }
407 };
408
409 static const true_false_string opensafety_message_direction = { "Request", "Response" };
410 #define OPENSAFETY_REQUEST  TRUE
411 #define OPENSAFETY_RESPONSE FALSE
412
413 #define OSS_FRAME_POS_ADDR   0
414 #define OSS_FRAME_POS_ID     1
415 #define OSS_FRAME_POS_LEN    2
416 #define OSS_FRAME_POS_CT     3
417 #define OSS_FRAME_POS_DATA   4
418
419 #define OSS_PAYLOAD_MAXSIZE_FOR_CRC8        0x08
420 #define OSS_SLIM_FRAME_WITH_CRC8_MAXSIZE    0x13   /* 19 */
421 #define OSS_SLIM_FRAME2_WITH_CRC8           0x06   /*  6 */
422 #define OSS_SLIM_FRAME2_WITH_CRC16          0x07   /*  7 */
423 #define OSS_MINIMUM_LENGTH                  0x0b   /* 11 */
424
425 #define OSS_FRAME_ADDR(f, offset)        (f[OSS_FRAME_POS_ADDR + offset] + ((guint8)((f[OSS_FRAME_POS_ADDR + offset + 1]) << 6) << 2))
426 #define OSS_FRAME_ID(f, offset)          (f[OSS_FRAME_POS_ID + offset] & 0xFC )
427 #define OSS_FRAME_LENGTH(f, offset)      (f[OSS_FRAME_POS_LEN + offset])
428 #define OSS_FRAME_FIELD(f, position)       (f[position])
429
430 #define OSS_FRAME_ADDR_T(f, offset)        (tvb_get_guint8(f, OSS_FRAME_POS_ADDR + offset) + ((guint8)((tvb_get_guint8( f, OSS_FRAME_POS_ADDR + offset + 1)) << 6) << 2))
431 #define OSS_FRAME_ADDR_T2(f, offset, su1, su2)        (( tvb_get_guint8(f, OSS_FRAME_POS_ADDR + offset) ^ su1) + ((guint8)(((tvb_get_guint8( f, OSS_FRAME_POS_ADDR + offset + 1) ^ su2)) << 6) << 2))
432 #define OSS_FRAME_ID_T(f, offset)          (tvb_get_guint8(f, OSS_FRAME_POS_ID + offset) & 0xFC)
433 #define OSS_FRAME_LENGTH_T(f, offset)      (tvb_get_guint8(f, OSS_FRAME_POS_LEN + offset))
434
435 static int proto_opensafety = -1;
436
437 static gint ett_opensafety = -1;
438 static gint ett_opensafety_checksum = -1;
439 static gint ett_opensafety_snmt = -1;
440 static gint ett_opensafety_ssdo = -1;
441 static gint ett_opensafety_spdo = -1;
442 static gint ett_opensafety_ssdo_sacmd = -1;
443 static gint ett_opensafety_ssdo_payload = -1;
444 static gint ett_opensafety_ssdo_sodentry = -1;
445 static gint ett_opensafety_ssdo_extpar = -1;
446 static gint ett_opensafety_sod_mapping = -1;
447 static gint ett_opensafety_sender = -1;
448 static gint ett_opensafety_receiver = -1;
449
450 static expert_field ei_payload_length_not_positive = EI_INIT;
451 static expert_field ei_payload_unknown_format = EI_INIT;
452 static expert_field ei_crc_slimssdo_instead_of_spdo = EI_INIT;
453 static expert_field ei_crc_frame_1_invalid = EI_INIT;
454 static expert_field ei_crc_frame_1_valid_frame2_invalid = EI_INIT;
455 static expert_field ei_crc_frame_2_invalid = EI_INIT;
456 static expert_field ei_crc_frame_2_unknown_scm_udid = EI_INIT;
457 static expert_field ei_message_unknown_type = EI_INIT;
458 static expert_field ei_message_reassembly_size_differs_from_header = EI_INIT;
459 static expert_field ei_message_spdo_address_invalid = EI_INIT;
460 static expert_field ei_message_id_field_mismatch = EI_INIT;
461 static expert_field ei_scmudid_autodetected = EI_INIT;
462 static expert_field ei_scmudid_invalid_preference = EI_INIT;
463 static expert_field ei_scmudid_unknown = EI_INIT;
464
465 static int hf_oss_msg = -1;
466 static int hf_oss_msg_direction = -1;
467 static int hf_oss_msg_category = -1;
468 static int hf_oss_msg_node = -1;
469 static int hf_oss_msg_network = -1;
470 static int hf_oss_msg_sender = -1;
471 static int hf_oss_msg_receiver = -1;
472 static int hf_oss_length= -1;
473 static int hf_oss_crc = -1;
474
475 static int hf_oss_crc_valid = -1;
476 static int hf_oss_crc2_valid = -1;
477 static int hf_oss_crc_type  = -1;
478
479 static int hf_oss_snmt_slave         = -1;
480 static int hf_oss_snmt_master        = -1;
481 static int hf_oss_snmt_udid          = -1;
482 static int hf_oss_snmt_scm           = -1;
483 static int hf_oss_snmt_tool          = -1;
484 static int hf_oss_snmt_service_id    = -1;
485 static int hf_oss_snmt_error_group   = -1;
486 static int hf_oss_snmt_error_code    = -1;
487
488 static int hf_oss_ssdo_server        = -1;
489 static int hf_oss_ssdo_client        = -1;
490 static int hf_oss_ssdo_sano          = -1;
491 static int hf_oss_ssdo_sacmd         = -1;
492 static int hf_oss_ssdo_sod_index     = -1;
493 static int hf_oss_ssdo_sod_subindex  = -1;
494 static int hf_oss_ssdo_payload       = -1;
495 static int hf_oss_ssdo_payload_size  = -1;
496 static int hf_oss_ssdo_sodentry_size = -1;
497 static int hf_oss_ssdo_sodentry_data = -1;
498 /* static int hf_oss_ssdo_inhibit_time  = -1; */
499 static int hf_oss_ssdo_abort_code    = -1;
500
501 static int hf_oss_sod_par_timestamp = -1;
502 static int hf_oss_sod_par_checksum  = -1;
503 static int hf_oss_ssdo_sodmapping = -1;
504 static int hf_oss_ssdo_sodmapping_bits = -1;
505
506 static int hf_oss_ssdo_sacmd_access_type     = -1;
507 /* static int hf_oss_ssdo_sacmd_reserved        = -1; */
508 static int hf_oss_ssdo_sacmd_abort_transfer  = -1;
509 static int hf_oss_ssdo_sacmd_segmentation    = -1;
510 static int hf_oss_ssdo_sacmd_toggle          = -1;
511 static int hf_oss_ssdo_sacmd_initiate        = -1;
512 static int hf_oss_ssdo_sacmd_end_segment     = -1;
513 static int hf_oss_ssdo_sacmd_block_transfer  = -1;
514
515 static int hf_oss_ssdo_extpar_parset = -1;
516 static int hf_oss_ssdo_extpar_version = -1;
517 static int hf_oss_ssdo_extpar_saddr = -1;
518 static int hf_oss_ssdo_extpar_length = -1;
519 static int hf_oss_ssdo_extpar_crc = -1;
520 static int hf_oss_ssdo_extpar_tstamp = -1;
521 static int hf_oss_ssdo_extpar_data = -1;
522 static int hf_oss_ssdo_extpar = -1;
523
524 static int hf_oss_scm_udid           = -1;
525 static int hf_oss_scm_udid_auto      = -1;
526 static int hf_oss_scm_udid_valid     = -1;
527
528 static int hf_oss_spdo_connection_valid   = -1;
529 static int hf_oss_spdo_payload            = -1;
530 static int hf_oss_spdo_producer           = -1;
531 static int hf_oss_spdo_producer_time      = -1;
532 static int hf_oss_spdo_time_value_sn      = -1;
533 static int hf_oss_spdo_time_request       = -1;
534 static int hf_oss_spdo_time_request_to    = -1;
535 static int hf_oss_spdo_time_request_from  = -1;
536
537 static int hf_oss_fragments = -1;
538 static int hf_oss_fragment = -1;
539 static int hf_oss_fragment_overlap = -1;
540 static int hf_oss_fragment_overlap_conflicts = -1;
541 static int hf_oss_fragment_multiple_tails = -1;
542 static int hf_oss_fragment_too_long_fragment = -1;
543 static int hf_oss_fragment_error = -1;
544 static int hf_oss_fragment_count = -1;
545 static int hf_oss_reassembled_in = -1;
546 static int hf_oss_reassembled_length = -1;
547 static int hf_oss_reassembled_data = -1;
548
549 static gint ett_opensafety_ssdo_fragment = -1;
550 static gint ett_opensafety_ssdo_fragments = -1;
551
552 static const fragment_items oss_frag_items = {
553     /* Fragment subtrees */
554     &ett_opensafety_ssdo_fragment,
555     &ett_opensafety_ssdo_fragments,
556     /* Fragment fields */
557     &hf_oss_fragments,
558     &hf_oss_fragment,
559     &hf_oss_fragment_overlap,
560     &hf_oss_fragment_overlap_conflicts,
561     &hf_oss_fragment_multiple_tails,
562     &hf_oss_fragment_too_long_fragment,
563     &hf_oss_fragment_error,
564     &hf_oss_fragment_count,
565     /* Reassembled in field */
566     &hf_oss_reassembled_in,
567     /* Reassembled length field */
568     &hf_oss_reassembled_length,
569     /* Reassembled data */
570     &hf_oss_reassembled_data,
571     /* Tag */
572     "Message fragments"
573 };
574
575 static const char *global_scm_udid = "00:00:00:00:00:00";
576 static gboolean global_calculate_crc2 = FALSE;
577 static gboolean global_scm_udid_autoset = TRUE;
578 static gboolean global_udp_frame2_first = FALSE;
579 static gboolean global_siii_udp_frame2_first = FALSE;
580 static gboolean global_mbtcp_big_endian = FALSE;
581 static guint global_network_udp_port = UDP_PORT_OPENSAFETY;
582 static guint global_network_udp_port_sercosiii = UDP_PORT_SIII;
583
584 static gboolean global_enable_plk = TRUE;
585 static gboolean global_enable_udp = TRUE;
586 static gboolean global_enable_genudp = TRUE;
587 static gboolean global_enable_siii = TRUE;
588 static gboolean global_enable_pnio = FALSE;
589 static gboolean global_enable_mbtcp = TRUE;
590
591 static gboolean bDissector_Called_Once_Before = FALSE;
592 /* Using local_scm_udid as read variable for global_scm_udid, to
593  * enable automatic detection of scm udid */
594 static char *local_scm_udid = NULL;
595
596 static reassembly_table os_reassembly_table;
597
598 /* Resets the dissector in case the dissection is malformed and the dissector crashes */
599 static void
600 reset_dissector(void)
601 {
602     bDissector_Called_Once_Before = FALSE;
603 }
604
605 static void
606 setup_dissector(void)
607 {
608     if ( local_scm_udid != NULL )
609         local_scm_udid = NULL;
610
611     reassembly_table_init(&os_reassembly_table, &addresses_reassembly_table_functions);
612 }
613
614 void proto_register_opensafety(void);
615 void proto_reg_handoff_opensafety(void);
616
617 /* Conversation functions */
618
619 /* This is defined by the specification. The Address field is 10 bits long, and the node with the number
620  *  1 is always the SCM, therefore ( 2 ^ 10 ) - 1 nodes can be addressed. We use 2 ^ 10 here, because the
621  *  SCM can talk to himself (Assign SADR for instance ) */
622 #define MAX_NUMBER_OF_SAFETY_NODES      ( 2 ^ 10 )
623
624 /* Tracks the information that the packet pinfo has been received by receiver, and adds that information to the tree, using pos, as
625  * byte position in the PDU */
626 #define PACKET_RECEIVER(pinfo, recv, pos, posnet, sdn)                       { \
627         proto_item * psf_item = NULL; \
628         proto_tree *psf_tree = NULL; \
629         psf_item = proto_tree_add_uint(opensafety_tree, hf_oss_msg_receiver, message_tvb, pos, 2, recv); \
630         psf_tree = proto_item_add_subtree(psf_item, ett_opensafety_receiver); \
631         psf_item = proto_tree_add_uint(psf_tree, hf_oss_msg_node, message_tvb, pos, 2, recv);\
632         PROTO_ITEM_SET_GENERATED(psf_item); \
633         if ( sdn > 0 ) \
634         { \
635             psf_item = proto_tree_add_uint_format_value(psf_tree, hf_oss_msg_network, message_tvb, posnet, 2, sdn, "0x%04X", sdn); \
636         } else if ( sdn <= 0 ) { \
637             psf_item = proto_tree_add_uint_format_value(psf_tree, hf_oss_msg_network, message_tvb, posnet, 2, sdn * -1, "0x%04X", sdn * -1); \
638             expert_add_info(pinfo, psf_item, &ei_scmudid_unknown ); \
639         } \
640         PROTO_ITEM_SET_GENERATED(psf_item); \
641         }
642
643 /* Tracks the information that the packet pinfo has been sent by sender, and received by everyone else, and adds that information to
644  * the tree, using pos, as byte position in the PDU */
645 #define PACKET_SENDER(pinfo, sender, pos, posnet, sdn)                { \
646         proto_item * psf_item = NULL; \
647         proto_tree *psf_tree = NULL; \
648         psf_item = proto_tree_add_uint(opensafety_tree, hf_oss_msg_sender, message_tvb, pos, 2, sender); \
649         psf_tree = proto_item_add_subtree(psf_item, ett_opensafety_sender); \
650         psf_item = proto_tree_add_uint(psf_tree, hf_oss_msg_node, message_tvb, pos, 2, sender);\
651         PROTO_ITEM_SET_GENERATED(psf_item); \
652         if ( sdn > 0 ) \
653         { \
654             psf_item = proto_tree_add_uint_format_value(psf_tree, hf_oss_msg_network, message_tvb, posnet, 2, sdn, "0x%04X", sdn); \
655         } else if ( sdn <= 0 ) { \
656             psf_item = proto_tree_add_uint_format_value(psf_tree, hf_oss_msg_network, message_tvb, posnet, 2, sdn * -1, "0x%04X", sdn * -1); \
657             expert_add_info(pinfo, psf_item, &ei_scmudid_unknown ); \
658         } \
659         PROTO_ITEM_SET_GENERATED(psf_item); \
660         }
661
662 /* Tracks the information that the packet pinfo has been sent by sender, and received by receiver, and adds that information to
663  * the tree, using pos for the sender and pos2 for the receiver, as byte position in the PDU */
664 #define PACKET_SENDER_RECEIVER(pinfo, send, pos, recv, pos2, posnet, sdn)         { \
665         PACKET_SENDER(pinfo, send, pos, posnet, sdn); \
666         PACKET_RECEIVER(pinfo, recv, pos2, posnet, sdn); \
667         }
668
669 static guint16
670 findFrame1Position ( tvbuff_t *message_tvb, guint16 byte_offset, guint8 dataLength, gboolean checkIfSlimMistake )
671 {
672     guint16 i_wFrame1Position                   = 0;
673     guint16 i_payloadLength, i_calculatedLength = 0;
674     guint16 i_offset                            = 0, calcCRC = 0, frameCRC = 0;
675     guint8  b_tempByte                          = 0;
676     guint8 *bytes = NULL;
677
678     /*
679      * First, a normal package get's assumed. Calculation of frame 1 position is
680      * pretty easy, because, the length of the whole package is 11 + 2*n + 2*o, which
681      * results in frame 1 start at (6 + n + o), which is length / 2 + 1
682      */
683     i_wFrame1Position = dataLength / 2 + 1;
684     i_payloadLength = tvb_get_guint8(message_tvb, byte_offset + i_wFrame1Position + 2 );
685     /* Calculating the assumed frame length, taking CRC8/CRC16 into account */
686     i_calculatedLength = i_payloadLength * 2 + 11 + 2 * (i_payloadLength > OSS_PAYLOAD_MAXSIZE_FOR_CRC8 ? 1 : 0);
687
688     /* To prevent miscalculations, where by chance the byte at [length / 2] + 3 is a value matching a possible payload length,
689      * but in reality the frame is a slim ssdo, the CRC of frame 1 get's checked additionally. This check
690      * is somewhat time consuming, so it will only run if the normal check led to a mistake detected along the line */
691     if ( checkIfSlimMistake && i_calculatedLength == dataLength )
692     {
693         if (dataLength > OSS_PAYLOAD_MAXSIZE_FOR_CRC8)
694             frameCRC = tvb_get_letohs(message_tvb,  byte_offset + i_wFrame1Position + dataLength + OSS_FRAME_POS_DATA);
695         else
696             frameCRC = tvb_get_guint8(message_tvb,  byte_offset + i_wFrame1Position + dataLength + OSS_FRAME_POS_DATA);
697
698         bytes = (guint8*)tvb_memdup(wmem_packet_scope(), message_tvb, byte_offset + i_wFrame1Position, dataLength + 4);
699         if ( dataLength > OSS_PAYLOAD_MAXSIZE_FOR_CRC8 )
700         {
701             calcCRC = crc16_0x755B(bytes, dataLength + 4, 0);
702             if ( frameCRC != calcCRC )
703                 calcCRC = crc16_0x5935(bytes, dataLength + 4, 0);
704         }
705         else
706             calcCRC = crc8_0x2F(bytes, dataLength + 4, 0);
707
708         /* if the calculated crc does not match the detected, the package is not a normal openSAFETY package */
709         if ( frameCRC != calcCRC )
710             dataLength = 0;
711     }
712
713     /* If the calculated length differs from the given length, a slim package is assumed. */
714     if ( i_calculatedLength != dataLength )
715     {
716         /* possible slim package */
717         i_wFrame1Position = 0;
718         /*
719          * Slim packages have a fixed sublength of either 6 bytes for frame 2 in
720          * case of crc8 and 7 bytes in case of crc16
721          */
722         i_offset = OSS_SLIM_FRAME2_WITH_CRC8 + ( dataLength < (OSS_SLIM_FRAME_WITH_CRC8_MAXSIZE + 1) ? 0 : 1 );
723         /* Last 2 digits belong to addr, therefore have to be cleared */
724         b_tempByte = ( tvb_get_guint8 ( message_tvb, byte_offset + i_offset + 1 ) ) & 0xFC;
725
726         /* If the id byte xor 0xE8 is 0, we have a slim package */
727         if ( ( ( b_tempByte ^ OPENSAFETY_MSG_SSDO_SLIM_SERVICE_REQUEST ) == 0 ) ||
728              ( ( b_tempByte ^ OPENSAFETY_MSG_SSDO_SLIM_SERVICE_RESPONSE ) == 0 ) )
729         {
730             /* Slim package found */
731             i_wFrame1Position = i_offset;
732         }
733     }
734
735     return i_wFrame1Position;
736 }
737
738 static guint8 findSafetyFrame ( tvbuff_t * message_tvb, guint u_Offset, gboolean b_frame2first, guint *u_frameOffset, guint *u_frameLength )
739 {
740     guint    ctr, rem_length;
741     guint16  crc, f2crc, calcCrc;
742     guint8   b_Length, crcOffset;
743     guint8   *bytes;
744     guint    b_ID;
745     gboolean found;
746
747     found = 0;
748     ctr = u_Offset;
749     rem_length = tvb_reported_length_remaining (message_tvb, ctr);
750
751     while ( rem_length >= OSS_MINIMUM_LENGTH)
752     {
753         /* The ID byte must ALWAYS be the second byte, therefore 0 is invalid */
754         if ( ctr != 0 )
755         {
756             *u_frameLength = 0;
757             *u_frameOffset = 0;
758
759             crcOffset = 0;
760             b_ID = tvb_get_guint8(message_tvb, ctr );
761
762             if ( b_ID != 0x0 )
763             {
764                 b_Length = tvb_get_guint8(message_tvb, ctr + 1 );
765
766                 /* 0xFF is often used, but always false, otherwise start detection, if the highest
767                  *  bit is set */
768                 if ( ( b_ID != 0xFF ) && ( b_ID & 0x80 ) )
769                 {
770                     /* The rem_length value might be poluted, due to the else statement of
771                      * above if-decision (frame at end position detection). Therefore we
772                      * calculate it here again, to have a sane value */
773                     rem_length = tvb_reported_length_remaining(message_tvb, ctr);
774
775                     /* Plausability check on length */
776                     if ( (guint)( b_Length * 2 ) < ( rem_length + OSS_MINIMUM_LENGTH ) )
777                     {
778
779                         /* The calculated length must fit, but for the CRC16 check, also the calculated length
780                         * plus the CRC16 end position must fit in the remaining length */
781                         if ( ( b_Length <= (guint) 8 && ( b_Length <= rem_length ) ) ||
782                             ( b_Length > (guint) 8 && ( ( b_Length + (guint) 5 ) <= rem_length ) ) )
783                         {
784                             /* Ensure, that the correct length for CRC calculation
785                              * still exists in byte stream, so that we can calculate the crc */
786                             if ( tvb_bytes_exist(message_tvb, ctr - 1, b_Length + 5) )
787                             {
788                                 /* An openSAFETY command has to have a high-byte range between 0x0A and 0x0E
789                                  *  b_ID & 0x80 took care of everything underneath, we check for 0x09 and 0x0F,
790                                  *  as they remain the only values left, which are not valid */
791                                 if ( ( ( b_ID >> 4 ) != 0x09 ) && ( ( b_ID >> 4 ) != 0x0F ) )
792                                 {
793                                     /* Find CRC position and calculate checksum */
794                                     crc = tvb_get_guint8(message_tvb, ctr + 3 + b_Length );
795
796                                     bytes = (guint8 *)tvb_memdup(wmem_packet_scope(), message_tvb, ctr - 1, b_Length + 5 );
797                                     if ( b_Length > 8 ) {
798                                         crc = tvb_get_letohs ( message_tvb, ctr + 3 + b_Length );
799                                         crcOffset = 1;
800
801                                         calcCrc = crc16_0x755B( bytes, b_Length + 4, 0 );
802                                         if ( ( crc ^ calcCrc ) != 0 )
803                                             calcCrc = crc16_0x5935( bytes, b_Length + 4, 0 );
804                                     } else {
805                                         calcCrc = crc8_0x2F ( bytes, b_Length + 4, 0 );
806                                     }
807
808                                     if ( ( crc ^ calcCrc ) == 0 )
809                                     {
810                                         /* Check if this is a Slim SSDO message */
811                                         if ( ( b_ID >> 3 ) == ( OPENSAFETY_SLIM_SSDO_MESSAGE_TYPE >> 3 ) )
812                                         {
813                                             /* Slim SSDO messages must have a length != 0, as the first byte
814                                              * in the payload contains the SOD access command */
815                                             if ( b_Length > 0 )
816                                             {
817                                                 *u_frameOffset = ( ctr - 1 );
818                                                 *u_frameLength = b_Length + 2 * crcOffset + 11;
819
820                                                 /* It is highly unlikely, that both frame 1 and frame 2 generate
821                                                  * a crc == 0 or equal crc's. Therefore we check, if both crc's are
822                                                  * equal. If so, it is a falsely detected frame. */
823                                                 f2crc = tvb_get_guint8 ( message_tvb, ctr + 3 + 5 + b_Length );
824                                                 if ( b_Length > 8 )
825                                                     f2crc = tvb_get_letohs ( message_tvb, ctr + 3 + 5 + b_Length );
826                                                 if ( crc != f2crc )
827                                                 {
828                                                     found = 1;
829                                                     break;
830                                                 }
831                                             }
832                                         }
833                                         else
834                                         {
835                                             *u_frameLength = 2 * b_Length + 2 * crcOffset + 11;
836                                             *u_frameOffset = ( ctr - 1 );
837
838                                             /* At this point frames had been checked for SoC and SoA types of
839                                              * EPL. This is no longer necessary and leads to false-negatives.
840                                              * SoC and SoA frames get filtered out at the EPL entry point. */
841                                             found = 1;
842                                             break;
843                                         }
844                                     }
845                                 }
846                             }
847                         }
848                     }
849                 }
850                 else
851                 {
852                     /* There exist frames, where the last openSAFETY frame is sitting in the
853                      * very last bytes of the frame, and the complete frame itself contains
854                      * more than one openSAFETY frame. It so happens that in such a case, the
855                      * last openSAFETY frame will miss detection.
856                      *
857                      * If so we look at the transported length, calculate the frame length,
858                      * and take a look if the calculated frame length, might be a fit for the
859                      * remaining length. If such is the case, we increment ctr and increment
860                      * rem_length (to hit the while loop one more time) and the frame will be
861                      * detected correctly. */
862                     if ( rem_length == OSS_MINIMUM_LENGTH )
863                     {
864                         b_ID = tvb_get_guint8(message_tvb, ctr );
865                         b_Length = tvb_get_guint8(message_tvb, ctr + 2 );
866                         if ( ( b_ID >> 3 ) == ( OPENSAFETY_SLIM_SSDO_MESSAGE_TYPE >> 3 ) )
867                             b_Length = ( 11 + ( b_Length > 8 ? 2 : 0 ) + b_Length );
868                         else
869                             b_Length = ( 11 + ( b_Length > 8 ? 2 : 0 ) + 2 * b_Length );
870
871                         if ( rem_length == b_Length )
872                         {
873                             ctr++;
874                             rem_length++;
875                             continue;
876                         }
877                     }
878                 }
879             }
880         }
881
882         ctr++;
883         rem_length = tvb_reported_length_remaining(message_tvb, ctr);
884
885     }
886
887     /* Seem redundant if b_frame2First is false. But in this case, the function is needed for the
888      * simple detection of a possible openSAFETY frame.  */
889     if ( b_frame2first && found )
890         *u_frameOffset = u_Offset;
891
892     return (found ? 1 : 0);
893 }
894
895 static void
896 dissect_opensafety_spdo_message(tvbuff_t *message_tvb, packet_info *pinfo, proto_tree *opensafety_tree,
897         guint16 frameStart1, guint16 frameStart2 , gboolean validSCMUDID, guint8 scm_udid[6])
898 {
899     proto_item *item;
900     proto_tree *spdo_tree;
901     guint16     ct;
902     gint16      taddr;
903     guint       dataLength;
904     guint8      tr, b_ID, conn_Valid;
905
906     dataLength = tvb_get_guint8(message_tvb, OSS_FRAME_POS_LEN + frameStart1);
907     b_ID = tvb_get_guint8(message_tvb, frameStart1 + 1) & 0xF8;
908     conn_Valid = ( (tvb_get_guint8(message_tvb, frameStart1 + 1) & 0x04) == 0x04);
909
910     /* Network address is xor'ed into the start of the second frame, but only legible, if the scm given is valid */
911     taddr = ( ( OSS_FRAME_ADDR_T(message_tvb, frameStart1) ) ^ ( OSS_FRAME_ADDR_T2(message_tvb, frameStart2, scm_udid[0], scm_udid[1]) ) );
912     if ( ! validSCMUDID )
913         taddr = ( -1 * taddr );
914
915     /* An SPDO get's always send by the producer, to everybody else */
916     PACKET_SENDER( pinfo, OSS_FRAME_ADDR_T(message_tvb, frameStart1), OSS_FRAME_POS_ADDR + frameStart1, frameStart2, taddr );
917
918     item = proto_tree_add_uint_format_value(opensafety_tree, hf_oss_msg_category, message_tvb,
919                                             OSS_FRAME_POS_ID + frameStart1, 1, OPENSAFETY_SPDO_MESSAGE_TYPE,
920                                             "%s", val_to_str_const(OPENSAFETY_SPDO_MESSAGE_TYPE, message_id_values, "Unknown") );
921     PROTO_ITEM_SET_GENERATED(item);
922
923     spdo_tree = proto_item_add_subtree(item, ett_opensafety_spdo);
924
925     if ( b_ID == OPENSAFETY_MSG_SPDO_DATA_WITH_TIME_RESPONSE )
926         proto_tree_add_boolean(spdo_tree, hf_oss_msg_direction, message_tvb,
927                                OSS_FRAME_POS_ID + frameStart1, 1, OPENSAFETY_RESPONSE);
928     else if ( b_ID == OPENSAFETY_MSG_SPDO_DATA_WITH_TIME_REQUEST || b_ID == OPENSAFETY_MSG_SPDO_DATA_ONLY )
929         proto_tree_add_boolean(spdo_tree, hf_oss_msg_direction, message_tvb,
930                                OSS_FRAME_POS_ID + frameStart1, 1, OPENSAFETY_REQUEST);
931
932     proto_tree_add_uint_format_value(spdo_tree, hf_oss_msg, message_tvb, OSS_FRAME_POS_ID + frameStart1, 1,
933                                      b_ID, "%s", val_to_str_const(b_ID, message_type_values, "Unknown") );
934
935     proto_tree_add_uint(spdo_tree, hf_oss_spdo_producer, message_tvb,
936                         OSS_FRAME_POS_ADDR + frameStart1, 2, OSS_FRAME_ADDR_T(message_tvb, frameStart1));
937     proto_tree_add_boolean(spdo_tree, hf_oss_spdo_connection_valid, message_tvb,
938                            OSS_FRAME_POS_ID + frameStart1, 1, conn_Valid);
939
940     /* taddr is the 4th octet in the second frame */
941     taddr = OSS_FRAME_ADDR_T2(message_tvb, frameStart2 + 3, scm_udid[3], scm_udid[4]);
942     tr = ( tvb_get_guint8(message_tvb, frameStart2 + 4)  ^ scm_udid[4] ) & 0xFC;
943
944     /* determine the ct value. if complete it can be used for analysis of the package */
945     ct = tvb_get_guint8(message_tvb, frameStart1 + 3);
946     if ( validSCMUDID )
947     {
948         ct = (guint16)((tvb_get_guint8(message_tvb, frameStart2 + 2) ^ scm_udid[2]) << 8) +
949             (tvb_get_guint8(message_tvb, frameStart1 + 3));
950     }
951
952     if ( b_ID == OPENSAFETY_MSG_SPDO_DATA_WITH_TIME_REQUEST )
953     {
954         item = proto_tree_add_uint_format_value(spdo_tree, hf_oss_spdo_time_value_sn, message_tvb, 0, 0, ct,
955                                                 "0x%04X [%d] (%s)", ct, ct,
956                                                 (validSCMUDID ? "Complete" : "Low byte only"));
957         PROTO_ITEM_SET_GENERATED(item);
958
959         proto_tree_add_uint(spdo_tree, hf_oss_spdo_time_request, message_tvb,
960                             OSS_FRAME_POS_ADDR + frameStart2 + 4, 1, tr);
961         proto_tree_add_uint(spdo_tree, hf_oss_spdo_time_request_from, message_tvb,
962                             OSS_FRAME_POS_ADDR + frameStart2 + 3, 2, taddr);
963     }
964     else
965     {
966         item = proto_tree_add_uint_format_value(spdo_tree, hf_oss_spdo_producer_time, message_tvb, 0, 0, ct,
967                 "0x%04X [%d] (%s)", ct, ct, (validSCMUDID ? "Complete" : "Low byte only"));
968         PROTO_ITEM_SET_GENERATED(item);
969
970         if ( b_ID == OPENSAFETY_MSG_SPDO_DATA_WITH_TIME_RESPONSE )
971         {
972             proto_tree_add_uint(spdo_tree, hf_oss_spdo_time_request, message_tvb, OSS_FRAME_POS_ADDR + frameStart2 + 4, 1, tr);
973             proto_tree_add_uint(spdo_tree, hf_oss_spdo_time_request_to, message_tvb, OSS_FRAME_POS_ADDR + frameStart2 + 3, 2, taddr);
974         }
975     }
976
977     if ( dataLength > 0 )
978         proto_tree_add_item(spdo_tree, hf_oss_spdo_payload, message_tvb, OSS_FRAME_POS_ID + 3, dataLength, ENC_NA);
979 }
980
981 static void dissect_ssdo_payload ( packet_info *pinfo, tvbuff_t *new_tvb, proto_tree *ssdo_payload, guint8 sacmd )
982 {
983     guint  dataLength = 0, ctr = 0, n = 0;
984     guint8 ssdoSubIndex = 0;
985     guint16 ssdoIndex = 0, dispSSDOIndex = 0;
986     guint32 sodLength = 0, entry = 0;
987     proto_item *item;
988     proto_tree *sod_tree, *ext_tree;
989
990     dataLength = tvb_length(new_tvb);
991
992     ssdoIndex = tvb_get_letohs(new_tvb, 0);
993
994     sodLength = tvb_get_letohl(new_tvb, 4);
995
996     /* first check for extended parameter */
997     if ( dataLength == 16 || sodLength == ( dataLength - 16 ) || ssdoIndex == 0x0101 )
998     {
999         /* extended parameter header & data */
1000         item = proto_tree_add_string_format(ssdo_payload, hf_oss_ssdo_extpar,
1001                                             new_tvb, 0, dataLength, "", "Extended Parameter Set: %s",
1002                                             (dataLength == 16 ? "Header only" : "Header & Data") );
1003         ext_tree = proto_item_add_subtree(item, ett_opensafety_ssdo_extpar);
1004
1005         proto_tree_add_item(ext_tree, hf_oss_ssdo_extpar_parset, new_tvb, 0, 1, ENC_BIG_ENDIAN );
1006         proto_tree_add_item(ext_tree, hf_oss_ssdo_extpar_version, new_tvb, 1, 1, ENC_BIG_ENDIAN );
1007         proto_tree_add_item(ext_tree, hf_oss_ssdo_extpar_saddr, new_tvb, 2, 2, ENC_LITTLE_ENDIAN );
1008
1009         proto_tree_add_uint_format_value(ext_tree, hf_oss_ssdo_extpar_length,
1010                                          new_tvb, 4, 4, sodLength, "0x%04X (%d octets)",
1011                                          sodLength, sodLength );
1012
1013         proto_tree_add_item(ext_tree, hf_oss_ssdo_extpar_crc, new_tvb, 8, 4, ENC_LITTLE_ENDIAN );
1014         proto_tree_add_item(ext_tree, hf_oss_ssdo_extpar_tstamp, new_tvb, 12, 4, ENC_LITTLE_ENDIAN );
1015
1016         if ( dataLength != 16 )
1017         {
1018             item = proto_tree_add_item(ext_tree, hf_oss_ssdo_extpar_data, new_tvb, 16, dataLength - 16, ENC_NA );
1019
1020             if ( ( dataLength - sodLength ) != 16 )
1021                 expert_add_info ( pinfo, item, &ei_message_reassembly_size_differs_from_header );
1022         }
1023     }
1024     else
1025     {
1026         /* If == upload, it is most likely a par upload */
1027         if ( sacmd == OPENSAFETY_MSG_SSDO_UPLOAD_SEGMENT_END && ( dataLength % 4 == 0 ) )
1028         {
1029
1030             item = proto_tree_add_uint_format_value(ssdo_payload, hf_oss_ssdo_sod_index, new_tvb,
1031                                                     0, 0,  0x1018, "0x%04X (%s)", 0x1018,
1032                                                     val_to_str_const( ((guint32) (0x1018 << 16) ),
1033                                                                       sod_idx_names, "Unknown") );
1034             sod_tree = proto_item_add_subtree(item, ett_opensafety_ssdo_sodentry);
1035             PROTO_ITEM_SET_GENERATED(item);
1036
1037             item = proto_tree_add_uint_format_value(sod_tree, hf_oss_ssdo_sod_subindex, new_tvb, 0, 0,
1038                                                              0x06, "0x%02X (%s)", 0x06,
1039                                                              val_to_str_const(((guint32) (0x1018 << 16) +  0x06),
1040                                                                                             sod_idx_names, "Unknown") );
1041             PROTO_ITEM_SET_GENERATED(item);
1042
1043             entry = tvb_get_letohl ( new_tvb, 0 );
1044             proto_tree_add_uint_format_value ( sod_tree, hf_oss_sod_par_timestamp, new_tvb, 0,
1045                         4, entry, "0x%08X", entry );
1046             for ( n = 4; n < dataLength; n+=4 )
1047             {
1048                 entry = tvb_get_letohl ( new_tvb, n );
1049                 proto_tree_add_uint_format_value ( sod_tree, hf_oss_sod_par_checksum, new_tvb, (n ),
1050                         4, entry, "[#%d] 0x%08X", ( n / 4 ), entry );
1051             }
1052         }
1053         /* If != upload, it is most likely a 101A download */
1054         else
1055         {
1056
1057             /* normal parameter set */
1058             for ( ctr = 0; ctr < dataLength; ctr++ )
1059             {
1060                 ssdoIndex = tvb_get_letohs(new_tvb, ctr);
1061                 ssdoSubIndex = tvb_get_guint8(new_tvb, ctr + 2);
1062                 dispSSDOIndex = ssdoIndex;
1063
1064                 if ( ssdoIndex >= 0x1400 && ssdoIndex <= 0x17FE )
1065                     dispSSDOIndex = 0x1400;
1066                 else if ( ssdoIndex >= 0x1800 && ssdoIndex <= 0x1BFE )
1067                     dispSSDOIndex = 0x1800;
1068                 else if ( ssdoIndex >= 0x1C00 && ssdoIndex <= 0x1FFE )
1069                     dispSSDOIndex = 0x1C00;
1070                 else if ( ssdoIndex >= 0xC000 && ssdoIndex <= 0xC3FE )
1071                     dispSSDOIndex = 0xC000;
1072
1073                 item = proto_tree_add_uint_format_value(ssdo_payload, hf_oss_ssdo_sod_index, new_tvb,
1074                                                         ctr, 2,  ssdoIndex, "0x%04X (%s)", ssdoIndex,
1075                                                         val_to_str_const( ((guint32) (dispSSDOIndex << 16) ),
1076                                                                           sod_idx_names, "Unknown") );
1077                 if ( ssdoIndex != dispSSDOIndex )
1078                     PROTO_ITEM_SET_GENERATED ( item );
1079
1080                 if ( ssdoIndex < 0x1000 || ssdoIndex > 0xE7FF )
1081                     expert_add_info ( pinfo, item, &ei_payload_unknown_format );
1082
1083                 sod_tree = proto_item_add_subtree(item, ett_opensafety_ssdo_sodentry);
1084
1085                 if ( ssdoSubIndex != 0 )
1086                 {
1087                     proto_tree_add_uint_format_value(sod_tree, hf_oss_ssdo_sod_subindex, new_tvb, ctr + 2, 1,
1088                                                  ssdoSubIndex, "0x%02X (%s)", ssdoSubIndex,
1089                                                  val_to_str_const(((guint32) (ssdoIndex << 16) + ssdoSubIndex),
1090                                                                                 sod_idx_names, "Unknown") );
1091                 }
1092                 else
1093                     proto_tree_add_uint_format_value(sod_tree, hf_oss_ssdo_sod_subindex, new_tvb, ctr + 2, 1,
1094                                                  ssdoSubIndex, "0x%02X",ssdoSubIndex );
1095                 ctr += 2;
1096
1097                 /* reading real size */
1098                 sodLength = tvb_get_letohl ( new_tvb, ctr + 1 );
1099                 if ( sodLength > (dataLength - ctr) )
1100                     sodLength = 0;
1101
1102                 if ( ( sodLength + 4 + ctr ) > dataLength )
1103                     break;
1104
1105                 if ( ssdoIndex == OPENSAFETY_SOD_DVI && ssdoSubIndex == 0x06 )
1106                 {
1107                     entry = tvb_get_letohl ( new_tvb, ctr + 5 );
1108                     proto_tree_add_uint_format_value ( sod_tree, hf_oss_sod_par_timestamp, new_tvb, ctr + 5,
1109                                 4, entry, "0x%08X", entry );
1110                     for ( n = 4; n < sodLength; n+=4 )
1111                     {
1112                         entry = tvb_get_letohl ( new_tvb, ctr + 5 + n );
1113                         proto_tree_add_uint_format_value ( sod_tree, hf_oss_sod_par_checksum, new_tvb, (ctr + 5 + n ),
1114                                 4, entry, "[#%d] 0x%08X", ( n / 4 ), entry );
1115                     }
1116                 } else if ( ssdoIndex == OPENSAFETY_SOD_DVI && ssdoSubIndex == 0x07 ) {
1117                     entry = tvb_get_letohl ( new_tvb, ctr + 5 );
1118                     proto_tree_add_uint_format_value ( sod_tree, hf_oss_sod_par_timestamp, new_tvb, ctr + 5,
1119                                 4, entry, "0x%08X", entry );
1120                 } else if ( ( dispSSDOIndex == OPENSAFETY_SOD_RXMAP || dispSSDOIndex == OPENSAFETY_SOD_TXMAP ) && ssdoSubIndex != 0x0 ) {
1121                     proto_tree_add_uint(sod_tree, hf_oss_ssdo_sodentry_size, new_tvb, ctr + 1, 4, sodLength );
1122                     item = proto_tree_add_item(sod_tree, hf_oss_ssdo_sodmapping, new_tvb, ctr + 5, sodLength, ENC_NA );
1123                     ext_tree = proto_item_add_subtree(item, ett_opensafety_sod_mapping);
1124
1125                     proto_tree_add_item(ext_tree, hf_oss_ssdo_sodmapping_bits, new_tvb, ctr + 5, 1, ENC_NA);
1126
1127                     entry = tvb_get_letohl ( new_tvb, ctr + 7 );
1128                     proto_tree_add_item(ext_tree, hf_oss_ssdo_sod_index, new_tvb, ctr + 7, 2, entry);
1129                     proto_tree_add_item(ext_tree, hf_oss_ssdo_sod_subindex, new_tvb, ctr + 6, 1, ENC_NA);
1130
1131                 } else {
1132                     proto_tree_add_uint(sod_tree, hf_oss_ssdo_sodentry_size, new_tvb, ctr + 1, 4, sodLength );
1133                     if ( sodLength > 0 )
1134                         proto_tree_add_item(sod_tree, hf_oss_ssdo_sodentry_data, new_tvb, ctr + 5, sodLength, ENC_NA );
1135                 }
1136                 ctr += sodLength + 4;
1137         }
1138         }
1139     }
1140
1141
1142 }
1143
1144 static void
1145 dissect_opensafety_ssdo_message(tvbuff_t *message_tvb , packet_info *pinfo, proto_tree *opensafety_tree ,
1146         guint16 frameStart1, guint16 frameStart2 , gboolean validSCMUDID, guint8 scm_udid[6])
1147 {
1148     proto_item *item;
1149     proto_tree *ssdo_tree, *ssdo_payload, *ssdo_sacmd_tree;
1150     guint16     taddr = 0, sdn = 0, server = 0, client = 0, n = 0, ct = 0;
1151     guint32     abortcode, ssdoIndex = 0, ssdoSubIndex = 0, payloadSize, fragmentId = 0, entry = 0;
1152     guint8      db0Offset, db0, sacmd, payloadOffset;
1153     guint       dataLength;
1154     gint        calcDataLength;
1155     gboolean    isResponse, decodePayload, isEndSegment, isSegmented, saveFragmented;
1156     tvbuff_t* new_tvb = NULL;
1157     fragment_head *frag_msg = NULL;
1158
1159     dataLength = tvb_get_guint8(message_tvb, OSS_FRAME_POS_LEN + frameStart1);
1160     decodePayload = FALSE;
1161
1162     db0Offset = frameStart1 + OSS_FRAME_POS_DATA;
1163     db0 = tvb_get_guint8(message_tvb, db0Offset);
1164     sacmd = db0;
1165     ssdoIndex = 0;
1166     ssdoSubIndex = 0;
1167
1168     if ( ( sacmd & OPENSAFETY_SSDO_SACMD_TGL ) == OPENSAFETY_SSDO_SACMD_TGL )
1169         sacmd = sacmd & ( ~OPENSAFETY_SSDO_SACMD_TGL );
1170
1171     isResponse = ( ( OSS_FRAME_ID_T(message_tvb, frameStart1) & 0x04 ) == 0x04 );
1172
1173     if ( validSCMUDID )
1174     {
1175         /* taddr is the 4th octet in the second frame */
1176         taddr = OSS_FRAME_ADDR_T2(message_tvb, frameStart2 + 3, scm_udid[3], scm_udid[4]);
1177         sdn =  ( OSS_FRAME_ADDR_T(message_tvb, frameStart1) ^
1178                         ( OSS_FRAME_ADDR_T2(message_tvb, frameStart2, scm_udid[0], scm_udid[1]) ) );
1179
1180         PACKET_SENDER_RECEIVER ( pinfo, taddr, frameStart2 + 3, OSS_FRAME_ADDR_T(message_tvb, frameStart1),
1181                                  frameStart1, frameStart2, sdn );
1182     }
1183     else if ( ! isResponse )
1184     {
1185         PACKET_SENDER(pinfo, OSS_FRAME_ADDR_T(message_tvb, frameStart1), frameStart1, frameStart2,
1186                       -1 * ( ( OSS_FRAME_ADDR_T(message_tvb, frameStart1) ) ^ ( OSS_FRAME_ADDR_T2(message_tvb, frameStart2, scm_udid[0], scm_udid[1]) ) ) );
1187     }
1188     else if ( isResponse )
1189     {
1190         PACKET_RECEIVER(pinfo, OSS_FRAME_ADDR_T(message_tvb, frameStart1), frameStart1, frameStart2,
1191                         -1 * ( ( OSS_FRAME_ADDR_T(message_tvb, frameStart1) ) ^ ( OSS_FRAME_ADDR_T2(message_tvb, frameStart2, scm_udid[0], scm_udid[1]) ) ) );
1192     }
1193
1194     if ( ( OSS_FRAME_ID_T(message_tvb, frameStart1) == OPENSAFETY_MSG_SSDO_SLIM_SERVICE_REQUEST ) ||
1195          ( OSS_FRAME_ID_T(message_tvb, frameStart1) == OPENSAFETY_MSG_SSDO_SLIM_SERVICE_RESPONSE ) )
1196         item = proto_tree_add_uint_format_value(opensafety_tree, hf_oss_msg_category, message_tvb,
1197                                                 OSS_FRAME_POS_ID + frameStart1, 1,
1198                                                 OPENSAFETY_SLIM_SSDO_MESSAGE_TYPE,
1199                                                 "%s", val_to_str_const(OPENSAFETY_SLIM_SSDO_MESSAGE_TYPE, message_id_values, "Unknown") );
1200     else
1201         item = proto_tree_add_uint_format_value(opensafety_tree, hf_oss_msg_category, message_tvb, OSS_FRAME_POS_ID + frameStart1, 1,
1202                                                 OPENSAFETY_SSDO_MESSAGE_TYPE,
1203                                                 "%s", val_to_str_const(OPENSAFETY_SSDO_MESSAGE_TYPE, message_id_values, "Unknown") );
1204     PROTO_ITEM_SET_GENERATED(item);
1205
1206     ssdo_tree = proto_item_add_subtree(item, ett_opensafety_ssdo);
1207
1208     if ( ( OSS_FRAME_ID_T(message_tvb, frameStart1) == OPENSAFETY_MSG_SSDO_SERVICE_RESPONSE ) ||
1209          ( OSS_FRAME_ID_T(message_tvb, frameStart1) == OPENSAFETY_MSG_SSDO_SLIM_SERVICE_RESPONSE ) )
1210         proto_tree_add_boolean(ssdo_tree, hf_oss_msg_direction, message_tvb, OSS_FRAME_POS_ID + frameStart1, 1, OPENSAFETY_RESPONSE);
1211     else
1212         proto_tree_add_boolean(ssdo_tree, hf_oss_msg_direction, message_tvb, OSS_FRAME_POS_ID + frameStart1, 1, OPENSAFETY_REQUEST);
1213
1214     proto_tree_add_uint_format_value(ssdo_tree, hf_oss_msg, message_tvb, OSS_FRAME_POS_ID + frameStart1, 1,
1215                                      OSS_FRAME_ID_T(message_tvb, frameStart1),
1216                                      "%s", val_to_str_const(OSS_FRAME_ID_T(message_tvb, frameStart1), message_type_values, "Unknown") );
1217
1218
1219     if ( isResponse )
1220     {
1221         if ( validSCMUDID )
1222         {
1223             proto_tree_add_uint(ssdo_tree, hf_oss_ssdo_client, message_tvb, frameStart1, 2, OSS_FRAME_ADDR_T(message_tvb, frameStart1));
1224             client = OSS_FRAME_ADDR_T(message_tvb, frameStart1);
1225             proto_tree_add_uint(ssdo_tree, hf_oss_ssdo_server, message_tvb, frameStart2 + 3, 2, taddr);
1226             server = taddr;
1227         }
1228         else
1229         {
1230             proto_tree_add_uint(ssdo_tree, hf_oss_ssdo_client, message_tvb, frameStart1, 2, OSS_FRAME_ADDR_T(message_tvb, frameStart1));
1231             client = OSS_FRAME_ADDR_T(message_tvb, frameStart1);
1232         }
1233     }
1234     else if ( ! isResponse )
1235     {
1236         if ( validSCMUDID )
1237         {
1238             proto_tree_add_uint(ssdo_tree, hf_oss_ssdo_server, message_tvb, frameStart1, 2, OSS_FRAME_ADDR_T(message_tvb, frameStart1));
1239             server = OSS_FRAME_ADDR_T(message_tvb, frameStart1);
1240             proto_tree_add_uint(ssdo_tree, hf_oss_ssdo_client, message_tvb, frameStart2 + 3, 2, taddr);
1241             client = taddr;
1242         }
1243         else
1244         {
1245             proto_tree_add_uint(ssdo_tree, hf_oss_ssdo_server, message_tvb, frameStart1, 2, OSS_FRAME_ADDR_T(message_tvb, frameStart1));
1246             server = OSS_FRAME_ADDR_T(message_tvb, frameStart1);
1247         }
1248     }
1249
1250     item = proto_tree_add_uint(ssdo_tree, hf_oss_ssdo_sacmd, message_tvb, db0Offset, 1, sacmd);
1251     col_append_fstr(pinfo->cinfo, COL_INFO, ", SACMD: %s", val_to_str_const(sacmd, ssdo_sacmd_values, " "));
1252
1253     ssdo_sacmd_tree = proto_item_add_subtree(item, ett_opensafety_ssdo_sacmd);
1254     proto_tree_add_boolean(ssdo_sacmd_tree, hf_oss_ssdo_sacmd_block_transfer, message_tvb, db0Offset, 1, db0);
1255     proto_tree_add_boolean(ssdo_sacmd_tree, hf_oss_ssdo_sacmd_end_segment, message_tvb, db0Offset, 1, db0);
1256     proto_tree_add_boolean(ssdo_sacmd_tree, hf_oss_ssdo_sacmd_initiate, message_tvb, db0Offset, 1, db0);
1257     proto_tree_add_boolean(ssdo_sacmd_tree, hf_oss_ssdo_sacmd_toggle, message_tvb, db0Offset, 1, db0);
1258     proto_tree_add_boolean(ssdo_sacmd_tree, hf_oss_ssdo_sacmd_segmentation, message_tvb, db0Offset, 1, db0);
1259     proto_tree_add_boolean(ssdo_sacmd_tree, hf_oss_ssdo_sacmd_abort_transfer, message_tvb, db0Offset, 1, db0);
1260     proto_tree_add_boolean(ssdo_sacmd_tree, hf_oss_ssdo_sacmd_access_type, message_tvb, db0Offset, 1, db0);
1261
1262     payloadOffset = db0Offset + 1;
1263
1264     ct = tvb_get_guint8(message_tvb, frameStart1 + 3);
1265     if ( validSCMUDID )
1266         ct = (guint16)((tvb_get_guint8(message_tvb, frameStart2 + 2) ^ scm_udid[2]) << 8) + (tvb_get_guint8(message_tvb, frameStart1 + 3));
1267
1268     proto_tree_add_uint(ssdo_tree, hf_oss_ssdo_sano, message_tvb, frameStart1 + 3, 1, ct );
1269
1270     /* When the following clause is met, DB1,2 contain the SOD index, and DB3 the SOD subindex */
1271     if ( ( ( sacmd & OPENSAFETY_SSDO_SACMD_INI ) == OPENSAFETY_SSDO_SACMD_INI ) &&
1272             ( sacmd != OPENSAFETY_MSG_SSDO_ABORT )
1273     )
1274     {
1275         ssdoIndex = tvb_get_letohs(message_tvb, db0Offset + 1);
1276         ssdoSubIndex = tvb_get_guint8(message_tvb, db0Offset + 3);
1277
1278         proto_tree_add_uint_format_value(ssdo_tree, hf_oss_ssdo_sod_index, message_tvb, db0Offset + 1, 2,
1279                 ssdoIndex, "0x%04X (%s)", ssdoIndex,
1280                 val_to_str_const(((guint32) (ssdoIndex << 16)), sod_idx_names, "Unknown") );
1281         col_append_fstr(pinfo->cinfo, COL_INFO, " [%s", val_to_str_const(((guint32) (ssdoIndex << 16)), sod_idx_names, "Unknown"));
1282
1283         /* Some SOD downloads (0x101A for instance) don't have sub-indeces */
1284         if ( ssdoSubIndex != 0x0 )
1285         {
1286             proto_tree_add_uint_format_value(ssdo_tree, hf_oss_ssdo_sod_subindex, message_tvb, db0Offset + 3, 1,
1287                 ssdoSubIndex, "0x%02X (%s)", ssdoSubIndex,
1288                 val_to_str_const(((guint32) (ssdoIndex << 16) + ssdoSubIndex), sod_idx_names, "Unknown") );
1289             col_append_fstr(pinfo->cinfo, COL_INFO, " - %s",
1290                     val_to_str_const(((guint32) (ssdoIndex << 16) + ssdoSubIndex), sod_idx_names, "Unknown"));
1291         }
1292         col_append_str(pinfo->cinfo, COL_INFO, "]" );
1293         payloadOffset += 3;
1294     }
1295
1296     if ( sacmd == OPENSAFETY_MSG_SSDO_ABORT )
1297     {
1298         abortcode = tvb_get_letohl(message_tvb, frameStart1 + OSS_FRAME_POS_DATA + 4);
1299
1300         proto_tree_add_uint_format_value(ssdo_tree, hf_oss_ssdo_abort_code, message_tvb, frameStart1 + OSS_FRAME_POS_DATA + 4, 4, abortcode,
1301                 "0x%04X %04X - %s", (guint16)(abortcode >> 16), (guint16)(abortcode),
1302                 val_to_str_const(abortcode, abort_codes, "Unknown"));
1303         col_append_fstr(pinfo->cinfo, COL_INFO, " - %s", val_to_str_const(abortcode, abort_codes, "Unknown"));
1304
1305
1306     } else {
1307
1308         /* Either the SSDO msg is a response, then data is sent by the server and only in uploads,
1309          * or the message is a request, then data is coming from the client and payload data is
1310          * sent in downloads */
1311         if ( ( isResponse && (sacmd == OPENSAFETY_MSG_SSDO_UPLOAD_INITIATE_SEGMENTED ||
1312                     sacmd == OPENSAFETY_MSG_SSDO_UPLOAD_INITIATE_EXPEDITED ||
1313                     sacmd == OPENSAFETY_MSG_SSDO_UPLOAD_SEGMENT_MIDDLE ||
1314                     sacmd == OPENSAFETY_MSG_SSDO_UPLOAD_SEGMENT_END ) )||
1315                     ( !isResponse && (sacmd == OPENSAFETY_MSG_SSDO_DOWNLOAD_INITIATE_SEGMENTED ||
1316                     sacmd == OPENSAFETY_MSG_SSDO_DOWNLOAD_INITIATE_EXPEDITED ||
1317                     sacmd == OPENSAFETY_MSG_SSDO_DOWNLOAD_SEGMENT_MIDDLE ||
1318                     sacmd == OPENSAFETY_MSG_SSDO_DOWNLOAD_SEGMENT_END ) ) )
1319                 {
1320                    decodePayload = TRUE;
1321                 }
1322
1323         if ( decodePayload )
1324         {
1325             saveFragmented = pinfo->fragmented;
1326             if ( server != 0 && client != 0 )
1327                 fragmentId = (guint32)((((guint32)client) << 16 ) + server );
1328
1329             isSegmented = ( ( db0 & OPENSAFETY_SSDO_SACMD_SEG ) == OPENSAFETY_SSDO_SACMD_SEG );
1330
1331             /* If payload data has to be calculated, either a total size is given, or not */
1332             if ( ( sacmd == OPENSAFETY_MSG_SSDO_DOWNLOAD_INITIATE_SEGMENTED ) ||
1333                     ( sacmd == OPENSAFETY_MSG_SSDO_UPLOAD_INITIATE_SEGMENTED )
1334                 )
1335             {
1336
1337                 payloadOffset += 4;
1338
1339                 /* reading real size */
1340                 payloadSize = tvb_get_letohl(message_tvb, payloadOffset - 4);
1341
1342                 calcDataLength = dataLength - (payloadOffset - db0Offset);
1343
1344                 item = proto_tree_add_uint_format_value(ssdo_tree, hf_oss_ssdo_payload_size, message_tvb, payloadOffset - 4, 4,
1345                         payloadSize, "%d octets total (%d octets in this frame)", payloadSize, calcDataLength);
1346
1347                 if ( fragmentId != 0 && isSegmented )
1348                 {
1349                     pinfo->fragmented = TRUE;
1350                     frag_msg = fragment_add_seq_check(&os_reassembly_table, message_tvb, payloadOffset, pinfo,
1351                                                       fragmentId, NULL, 0, calcDataLength, TRUE );
1352                     fragment_add_seq_offset ( &os_reassembly_table, pinfo, fragmentId, NULL, ct );
1353
1354                     if ( frag_msg != NULL )
1355                     {
1356                         item = proto_tree_add_bytes_format_value(ssdo_tree, hf_oss_ssdo_payload, message_tvb, 0, 0, NULL, "Reassembled" );
1357                         PROTO_ITEM_SET_GENERATED(item);
1358
1359                         ssdo_payload = proto_item_add_subtree(item, ett_opensafety_ssdo_payload);
1360                         process_reassembled_data(message_tvb, 0, pinfo, "Reassembled Message", frag_msg, &oss_frag_items, NULL, ssdo_payload );
1361                     }
1362                 }
1363
1364                 if ( (gint) calcDataLength >= (gint) 0 )
1365                 {
1366                     proto_tree_add_item(ssdo_tree, hf_oss_ssdo_payload, message_tvb, payloadOffset, calcDataLength, ENC_NA );
1367                 } else {
1368                     expert_add_info_format(pinfo, item, &ei_payload_length_not_positive,
1369                                                 "Calculation for payload length yielded non-positive result [%d]", (guint) calcDataLength );
1370                 }
1371             }
1372             else
1373             {
1374                 isEndSegment = FALSE;
1375                 if ( ( sacmd == OPENSAFETY_MSG_SSDO_DOWNLOAD_SEGMENT_END ) || ( sacmd == OPENSAFETY_MSG_SSDO_UPLOAD_SEGMENT_END ) )
1376                     isEndSegment = TRUE;
1377
1378                 payloadSize = dataLength - (payloadOffset - db0Offset);
1379
1380                 if ( fragmentId != 0 && isSegmented )
1381                 {
1382                     pinfo->fragmented = TRUE;
1383
1384                     frag_msg = fragment_add_seq_check(&os_reassembly_table, message_tvb, payloadOffset, pinfo,
1385                                                       fragmentId, NULL,ct,
1386                                                       payloadSize, isEndSegment ? FALSE : TRUE );
1387                 }
1388
1389                 if ( frag_msg )
1390                 {
1391                     item = proto_tree_add_bytes_format_value(ssdo_tree, hf_oss_ssdo_payload, message_tvb,
1392                                                              0, 0, NULL, "Reassembled" );
1393                     PROTO_ITEM_SET_GENERATED(item);
1394                     ssdo_payload = proto_item_add_subtree(item, ett_opensafety_ssdo_payload);
1395
1396                     new_tvb = process_reassembled_data(message_tvb, 0, pinfo, "Reassembled Message", frag_msg,
1397                                                        &oss_frag_items, NULL, ssdo_payload );
1398                     if ( isEndSegment && new_tvb )
1399                     {
1400                         item = proto_tree_add_uint_format_value(ssdo_payload, hf_oss_ssdo_payload_size, message_tvb, 0, 0,
1401                                                                 payloadSize, "%d octets (over all fragments)", frag_msg->len);
1402                         PROTO_ITEM_SET_GENERATED(item);
1403
1404                         col_append_str(pinfo->cinfo, COL_INFO, " (Message Reassembled)" );
1405                         dissect_ssdo_payload ( pinfo, new_tvb, ssdo_payload, sacmd );
1406                     }
1407                 }
1408                 else
1409                 {
1410                     item = proto_tree_add_uint_format_value(ssdo_tree, hf_oss_ssdo_payload_size, message_tvb, 0, 0, payloadSize,
1411                             "%d octets", payloadSize);
1412                     PROTO_ITEM_SET_GENERATED(item);
1413
1414                     if ( ssdoIndex == OPENSAFETY_SOD_DVI && ssdoSubIndex == 0x06 )
1415                     {
1416                         entry = tvb_get_letohl ( message_tvb, payloadOffset );
1417                         proto_tree_add_uint_format_value ( ssdo_tree, hf_oss_sod_par_timestamp, message_tvb, payloadOffset,
1418                                     4, entry, "0x%08X", entry );
1419                         for ( n = 4; n < payloadSize; n+=4 )
1420                         {
1421                             entry = tvb_get_letohl ( message_tvb, payloadOffset + n );
1422                             proto_tree_add_uint_format_value ( ssdo_tree, hf_oss_sod_par_checksum, message_tvb, (payloadOffset + n ),
1423                                     4, entry, "[#%d] 0x%08X", ( n / 4 ), entry );
1424                         }
1425                     } else if ( ssdoIndex == OPENSAFETY_SOD_DVI && ssdoSubIndex == 0x07 ) {
1426                         entry = tvb_get_letohl ( message_tvb, payloadOffset );
1427                         proto_tree_add_uint_format_value ( ssdo_tree, hf_oss_sod_par_timestamp, message_tvb, payloadOffset,
1428                                     4, entry, "0x%08X", entry );
1429                     } else
1430                         proto_tree_add_item(ssdo_tree, hf_oss_ssdo_payload, message_tvb, payloadOffset, payloadSize, ENC_NA );
1431                 }
1432             }
1433
1434             pinfo->fragmented = saveFragmented;
1435         }
1436     }
1437 }
1438
1439 static void
1440 dissect_opensafety_snmt_message(tvbuff_t *message_tvb, packet_info *pinfo , proto_tree *opensafety_tree,
1441         guint16 frameStart1, guint16 frameStart2 )
1442 {
1443     proto_item *item;
1444     proto_tree *snmt_tree;
1445     guint32     entry = 0;
1446     guint16     addr, taddr, sdn;
1447     guint8      db0, byte;
1448     guint       dataLength;
1449     char       *tempString;
1450
1451     dataLength = OSS_FRAME_LENGTH_T(message_tvb, frameStart1);
1452
1453     /* addr is the first field, as well as the recipient of the message */
1454     addr = OSS_FRAME_ADDR_T(message_tvb, frameStart1);
1455     /* taddr is the 4th octet in the second frame */
1456     taddr = OSS_FRAME_ADDR_T(message_tvb, frameStart2 + 3);
1457     /* domain is xor'ed on the first field in the second frame. As this is also addr, it is easy to obtain */
1458     sdn = OSS_FRAME_ADDR_T(message_tvb, frameStart2) ^ addr;
1459
1460     db0 = -1;
1461     if (dataLength > 0)
1462         db0 = tvb_get_guint8(message_tvb, frameStart1 + OSS_FRAME_POS_DATA);
1463
1464     if ( ( (OSS_FRAME_ID_T(message_tvb, frameStart1) ^ OPENSAFETY_MSG_SNMT_SERVICE_RESPONSE) == 0 ) &&
1465          ( (db0 ^ OPENSAFETY_MSG_SNMT_EXT_SCM_SET_TO_STOP) == 0 || (db0 ^ OPENSAFETY_MSG_SNMT_EXT_SCM_SET_TO_OP) == 0 ) )
1466     {
1467         PACKET_RECEIVER( pinfo, addr, OSS_FRAME_POS_ADDR + frameStart1, frameStart2, sdn );
1468     }
1469     else
1470     {
1471         PACKET_SENDER_RECEIVER ( pinfo, taddr, OSS_FRAME_POS_ADDR + frameStart1, addr, frameStart2 + 3,
1472                                  frameStart2, sdn );
1473     }
1474
1475     item = proto_tree_add_uint_format_value(opensafety_tree, hf_oss_msg_category, message_tvb, OSS_FRAME_POS_ID + frameStart1, 1,
1476                                             OPENSAFETY_SNMT_MESSAGE_TYPE,
1477                                             "%s", val_to_str_const(OPENSAFETY_SNMT_MESSAGE_TYPE, message_id_values, "Unknown") );
1478     PROTO_ITEM_SET_GENERATED(item);
1479
1480     snmt_tree = proto_item_add_subtree(item, ett_opensafety_snmt);
1481
1482     if ( ( OSS_FRAME_ID_T(message_tvb, frameStart1) == OPENSAFETY_MSG_SNMT_RESPONSE_UDID ) ||
1483          ( OSS_FRAME_ID_T(message_tvb, frameStart1) == OPENSAFETY_MSG_SNMT_SADR_ASSIGNED ) ||
1484          ( OSS_FRAME_ID_T(message_tvb, frameStart1) == OPENSAFETY_MSG_SNMT_SERVICE_RESPONSE ) )
1485         proto_tree_add_boolean(snmt_tree, hf_oss_msg_direction, message_tvb, OSS_FRAME_POS_ID + frameStart1, 1, OPENSAFETY_RESPONSE);
1486     else
1487         proto_tree_add_boolean(snmt_tree, hf_oss_msg_direction, message_tvb, OSS_FRAME_POS_ID + frameStart1, 1, OPENSAFETY_REQUEST);
1488
1489     proto_tree_add_uint_format_value(snmt_tree, hf_oss_msg, message_tvb, OSS_FRAME_POS_ID + frameStart1, 1,
1490                                      OSS_FRAME_ID_T(message_tvb, frameStart1),
1491                                      "%s", val_to_str_const(OSS_FRAME_ID_T(message_tvb, frameStart1), message_type_values, "Unknown") );
1492
1493     if ( (OSS_FRAME_ID_T(message_tvb, frameStart1) ^ OPENSAFETY_MSG_SNMT_SN_RESET_GUARDING_SCM) == 0 )
1494     {
1495         proto_tree_add_uint(snmt_tree, hf_oss_snmt_master, message_tvb, OSS_FRAME_POS_ADDR + frameStart1, 2, addr);
1496         proto_tree_add_uint(snmt_tree, hf_oss_snmt_slave, message_tvb, frameStart2 + 3, 2, taddr);
1497     }
1498     else if ( (OSS_FRAME_ID_T(message_tvb, frameStart1) ^ OPENSAFETY_MSG_SNMT_SERVICE_RESPONSE) == 0 )
1499     {
1500         proto_tree_add_uint(snmt_tree, hf_oss_snmt_service_id, message_tvb, OSS_FRAME_POS_DATA + frameStart1, 1, db0);
1501         col_append_fstr(pinfo->cinfo, COL_INFO, ", %s", val_to_str_const(db0, message_service_type, "Unknown"));
1502
1503         proto_tree_add_uint(snmt_tree, hf_oss_snmt_master, message_tvb, OSS_FRAME_POS_ADDR + frameStart1, 2, addr);
1504         proto_tree_add_uint(snmt_tree, hf_oss_snmt_slave, message_tvb, frameStart2 + 3, 2, taddr);
1505         if ( (db0 ^ OPENSAFETY_MSG_SNMT_EXT_SN_FAIL) == 0 )
1506         {
1507             byte = tvb_get_guint8(message_tvb, OSS_FRAME_POS_DATA + frameStart1 + 1);
1508             proto_tree_add_uint_format_value(snmt_tree, hf_oss_snmt_error_group, message_tvb, OSS_FRAME_POS_DATA + frameStart1 + 1, 1,
1509                                        byte, "%s", ( byte == 0 ? "Device" : val_to_str(byte, sn_fail_error_group, "Reserved [%d]" ) ) );
1510
1511             byte = tvb_get_guint8(message_tvb, OSS_FRAME_POS_DATA + frameStart1 + 2);
1512             proto_tree_add_uint_format_value(snmt_tree, hf_oss_snmt_error_code, message_tvb, OSS_FRAME_POS_DATA + frameStart1 + 2, 1,
1513                                        byte, "%s [%d]", ( byte == 0 ? "Default" : "Vendor Specific" ), byte );
1514
1515             col_append_fstr(pinfo->cinfo, COL_INFO, " - Group: %s; Code: %s",
1516                 ( byte == 0 ? "Device" : val_to_str(byte, sn_fail_error_group, "Reserved [%d]" ) ),
1517                 ( byte == 0 ? "Default" : "Vendor Specific" )
1518             );
1519         }
1520         else if ( (db0 ^ OPENSAFETY_MSG_SNMT_EXT_SN_ASSIGNED_UDID_SCM) == 0 )
1521         {
1522             item = proto_tree_add_item(snmt_tree, hf_oss_snmt_udid, message_tvb, OSS_FRAME_POS_DATA + frameStart1 + 1, 6, ENC_NA);
1523
1524             if ( global_scm_udid_autoset == TRUE )
1525             {
1526                 tempString = (char *)wmem_alloc0(wmem_packet_scope(), 128 * sizeof(char));
1527                 g_snprintf ( tempString, 18, "%s", tvb_bytes_to_str_punct(message_tvb, OSS_FRAME_POS_DATA + frameStart1 + 1, 6, ':' ) );
1528                 if ( memcmp ( global_scm_udid, tempString, 17 ) != 0 )
1529                 {
1530                     local_scm_udid = (char *)wmem_alloc0(wmem_file_scope(), 18 * sizeof(char));
1531                     g_snprintf(local_scm_udid, 18, "%s", tempString );
1532                     expert_add_info_format(pinfo, item, &ei_scmudid_autodetected, "Auto detected payload as SCM UDID [%s].", tempString);
1533                 }
1534             }
1535
1536         }
1537
1538     }
1539     else if ( (OSS_FRAME_ID_T(message_tvb, frameStart1) ^ OPENSAFETY_MSG_SNMT_SERVICE_REQUEST) == 0 )
1540     {
1541         proto_tree_add_uint(snmt_tree, hf_oss_snmt_service_id, message_tvb, OSS_FRAME_POS_DATA + frameStart1, 1, db0);
1542         col_append_fstr(pinfo->cinfo, COL_INFO, ", %s", val_to_str_const(db0, message_service_type, "Unknown"));
1543
1544         if ( (db0 ^ OPENSAFETY_MSG_SNMT_EXT_SCM_SET_TO_STOP) == 0 || (db0 ^ OPENSAFETY_MSG_SNMT_EXT_SCM_SET_TO_OP) == 0 )
1545         {
1546             proto_tree_add_uint(snmt_tree, hf_oss_snmt_scm, message_tvb, OSS_FRAME_POS_ADDR + frameStart1, 2, addr);
1547             proto_tree_add_uint(snmt_tree, hf_oss_snmt_tool, message_tvb, frameStart2 + 3, 2, taddr);
1548         }
1549         else if ( (db0 ^ OPENSAFETY_MSG_SNMT_EXT_SN_ASSIGN_UDID_SCM) == 0 )
1550         {
1551             proto_tree_add_uint(snmt_tree, hf_oss_snmt_master, message_tvb, OSS_FRAME_POS_ADDR + frameStart1, 2, addr);
1552             proto_tree_add_uint(snmt_tree, hf_oss_snmt_slave, message_tvb, frameStart2 + 3, 2, taddr);
1553             item = proto_tree_add_item(snmt_tree, hf_oss_snmt_udid, message_tvb, OSS_FRAME_POS_DATA + frameStart1 + 1, 6, ENC_NA);
1554
1555             if ( global_scm_udid_autoset == TRUE )
1556             {
1557                 tempString = (char *)wmem_alloc0(wmem_packet_scope(), 18 * sizeof(char));
1558                 g_snprintf ( tempString, 18, "%s", tvb_bytes_to_str_punct(message_tvb, OSS_FRAME_POS_DATA + frameStart1 + 1, 6, ':' ) );
1559                 if ( memcmp ( global_scm_udid, tempString, 17 ) != 0 )
1560                 {
1561                     local_scm_udid = (char *)wmem_alloc0(wmem_file_scope(), 18 * sizeof(char));
1562                     g_snprintf(local_scm_udid, 18, "%s", tempString );
1563                     expert_add_info_format(pinfo, item, &ei_scmudid_autodetected, "Auto detected payload as SCM UDID [%s].", tempString);
1564                 }
1565             }
1566
1567         }
1568         else
1569         {
1570             proto_tree_add_uint(snmt_tree, hf_oss_snmt_master, message_tvb, frameStart2 + 3, 2, taddr);
1571             proto_tree_add_uint(snmt_tree, hf_oss_snmt_slave, message_tvb, OSS_FRAME_POS_ADDR + frameStart1, 2, addr);
1572
1573             if ( (db0 ^ OPENSAFETY_MSG_SNMT_EXT_SN_SET_TO_OP) == 0 )
1574             {
1575                 entry = tvb_get_letohl ( message_tvb, frameStart1 + OSS_FRAME_POS_DATA + 1 );
1576                 proto_tree_add_uint_format_value ( snmt_tree, hf_oss_sod_par_timestamp, message_tvb,
1577                         OSS_FRAME_POS_DATA + frameStart1 + 1, 4, entry, "0x%08X", entry );
1578             }
1579         }
1580     }
1581     else if ( (OSS_FRAME_ID_T(message_tvb, frameStart1) ^ OPENSAFETY_MSG_SNMT_SADR_ASSIGNED) == 0 )
1582     {
1583         proto_tree_add_uint(snmt_tree, hf_oss_snmt_master, message_tvb, OSS_FRAME_POS_ADDR + frameStart1, 2, addr);
1584         proto_tree_add_uint(snmt_tree, hf_oss_snmt_slave, message_tvb, frameStart2 + 3, 2, taddr);
1585
1586         if (dataLength > 0)
1587             proto_tree_add_item(snmt_tree, hf_oss_snmt_udid, message_tvb, OSS_FRAME_POS_DATA + frameStart1, 6, ENC_NA);
1588     }
1589     else if ( (OSS_FRAME_ID_T(message_tvb, frameStart1) ^ OPENSAFETY_MSG_SNMT_ASSIGN_SADR) == 0 )
1590     {
1591         proto_tree_add_uint(snmt_tree, hf_oss_snmt_master, message_tvb, frameStart2 + 3, 2, taddr);
1592         proto_tree_add_uint(snmt_tree, hf_oss_snmt_slave, message_tvb, OSS_FRAME_POS_ADDR + frameStart1, 2, addr);
1593
1594         if (dataLength > 0)
1595             proto_tree_add_item(snmt_tree, hf_oss_snmt_udid, message_tvb, OSS_FRAME_POS_DATA + frameStart1, 6, ENC_NA);
1596
1597     }
1598     else if ( (OSS_FRAME_ID_T(message_tvb, frameStart1) ^ OPENSAFETY_MSG_SNMT_RESPONSE_UDID) == 0 )
1599     {
1600         proto_tree_add_uint(snmt_tree, hf_oss_snmt_master, message_tvb, OSS_FRAME_POS_ADDR + frameStart1, 2, addr);
1601         proto_tree_add_uint(snmt_tree, hf_oss_snmt_slave, message_tvb, frameStart2 + 3, 2, taddr);
1602
1603         if (dataLength > 0)
1604             proto_tree_add_item(snmt_tree, hf_oss_snmt_udid, message_tvb, OSS_FRAME_POS_DATA + frameStart1, 6, ENC_NA);
1605
1606     }
1607     else if ( (OSS_FRAME_ID_T(message_tvb, frameStart1) ^ OPENSAFETY_MSG_SNMT_REQUEST_UDID) == 0 )
1608     {
1609         proto_tree_add_uint(snmt_tree, hf_oss_snmt_master, message_tvb, frameStart2 + 3, 2, taddr);
1610         proto_tree_add_uint(snmt_tree, hf_oss_snmt_slave, message_tvb, OSS_FRAME_POS_ADDR + frameStart1, 2, addr);
1611     }
1612 }
1613
1614 static gboolean
1615 dissect_opensafety_checksum(tvbuff_t *message_tvb, packet_info *pinfo, proto_tree *opensafety_tree,
1616                             guint8 type, guint16 frameStart1, guint16 frameStart2 )
1617 {
1618     guint16     frame1_crc, frame2_crc;
1619     guint16     calc1_crc, calc2_crc;
1620     guint       dataLength, frame2Length;
1621     guint8     *bytes, ctr = 0, crcType = OPENSAFETY_CHECKSUM_CRC8;
1622     proto_item *item;
1623     proto_tree *checksum_tree;
1624     gint        start;
1625     gint        length;
1626     gboolean    isSlim = FALSE;
1627     GByteArray *scmUDID = NULL;
1628
1629     dataLength = OSS_FRAME_LENGTH_T(message_tvb, frameStart1);
1630     start = OSS_FRAME_POS_DATA + dataLength + frameStart1;
1631
1632     if (OSS_FRAME_LENGTH_T(message_tvb, frameStart1) > OSS_PAYLOAD_MAXSIZE_FOR_CRC8)
1633         frame1_crc = tvb_get_letohs(message_tvb, start);
1634     else
1635         frame1_crc = tvb_get_guint8(message_tvb, start);
1636
1637     if ( type == OPENSAFETY_SLIM_SSDO_MESSAGE_TYPE )
1638         isSlim = TRUE;
1639
1640     frame2Length = (isSlim ? 0 : dataLength) + 5;
1641
1642     length = (dataLength > OSS_PAYLOAD_MAXSIZE_FOR_CRC8 ? OPENSAFETY_CHECKSUM_CRC16 : OPENSAFETY_CHECKSUM_CRC8);
1643     item = proto_tree_add_uint_format(opensafety_tree, hf_oss_crc, message_tvb, start, length, frame1_crc,
1644                                       "CRC for subframe #1: 0x%04X", frame1_crc);
1645
1646     checksum_tree = proto_item_add_subtree(item, ett_opensafety_checksum);
1647
1648     bytes = (guint8*)tvb_memdup(wmem_packet_scope(), message_tvb, frameStart1, dataLength + 4);
1649     if ( dataLength > OSS_PAYLOAD_MAXSIZE_FOR_CRC8 )
1650     {
1651         calc1_crc = crc16_0x755B(bytes, dataLength + 4, 0);
1652         if ( frame1_crc == calc1_crc )
1653             crcType = OPENSAFETY_CHECKSUM_CRC16;
1654         if ( frame1_crc != calc1_crc )
1655         {
1656             calc1_crc = crc16_0x5935(bytes, dataLength + 4, 0);
1657             if ( frame1_crc == calc1_crc )
1658             {
1659                 crcType = OPENSAFETY_CHECKSUM_CRC16SLIM;
1660                 if ( ! isSlim )
1661                     expert_add_info(pinfo, item, &ei_crc_slimssdo_instead_of_spdo );
1662             }
1663         }
1664     }
1665     else
1666         calc1_crc = crc8_0x2F(bytes, dataLength + 4, 0);
1667
1668     item = proto_tree_add_boolean(checksum_tree, hf_oss_crc_valid, message_tvb,
1669             frameStart1, dataLength + 4, (frame1_crc == calc1_crc));
1670     PROTO_ITEM_SET_GENERATED(item);
1671     /* using the defines, as the values can change */
1672     proto_tree_add_uint(checksum_tree, hf_oss_crc_type, message_tvb, start, length, crcType );
1673
1674     start = frameStart2 + (isSlim ? 5 : dataLength + OSS_FRAME_POS_DATA + 1 );
1675     if (OSS_FRAME_LENGTH_T(message_tvb, frameStart1) > OSS_PAYLOAD_MAXSIZE_FOR_CRC8)
1676         frame2_crc = tvb_get_letohs(message_tvb, start);
1677     else
1678         frame2_crc = tvb_get_guint8(message_tvb, start);
1679
1680     /* 0xFFFF is an invalid CRC16 value, therefore valid for initialization */
1681     calc2_crc = 0xFFFF;
1682
1683     if ( global_calculate_crc2 )
1684     {
1685         bytes = (guint8*)tvb_memdup(wmem_packet_scope(), message_tvb, frameStart2, frame2Length + length);
1686
1687         /* SLIM SSDO messages, do not contain a payload in frame2 */
1688         if ( isSlim == TRUE )
1689             dataLength = 0;
1690
1691         scmUDID = g_byte_array_new();
1692         if ( hex_str_to_bytes((local_scm_udid != NULL ? local_scm_udid : global_scm_udid), scmUDID, TRUE) && scmUDID->len == 6 )
1693         {
1694             if ( type != OPENSAFETY_SNMT_MESSAGE_TYPE )
1695             {
1696                 for ( ctr = 0; ctr < 6; ctr++ )
1697                     bytes[ctr] = bytes[ctr] ^ (guint8)(scmUDID->data[ctr]);
1698
1699                 /*
1700                  * If the second frame is 6 or 7 (slim) bytes in length, we have to decode the found
1701                  * frame crc again. This must be done using the byte array, as the unxor operation
1702                  * had to take place.
1703                  */
1704                 if ( dataLength == 0 )
1705                     frame2_crc = ( ( isSlim && length == 2 ) ? ( ( bytes[6] << 8 ) + bytes[5] ) : bytes[5] );
1706             }
1707
1708             item = proto_tree_add_uint_format(opensafety_tree, hf_oss_crc, message_tvb, start, length, frame2_crc,
1709                     "CRC for subframe #2: 0x%04X", frame2_crc);
1710
1711             checksum_tree = proto_item_add_subtree(item, ett_opensafety_checksum);
1712
1713             if ( OSS_FRAME_LENGTH_T(message_tvb, frameStart1) > OSS_PAYLOAD_MAXSIZE_FOR_CRC8 )
1714             {
1715                 calc2_crc = crc16_0x755B(bytes, frame2Length, 0);
1716                 if ( frame2_crc != calc2_crc )
1717                     calc2_crc = crc16_0x5935(bytes, frame2Length, 0);
1718             }
1719             else
1720                 calc2_crc = crc8_0x2F(bytes, frame2Length, 0);
1721
1722             item = proto_tree_add_boolean(checksum_tree, hf_oss_crc2_valid, message_tvb,
1723                     frameStart2, frame2Length, (frame2_crc == calc2_crc));
1724             PROTO_ITEM_SET_GENERATED(item);
1725
1726             if ( frame2_crc != calc2_crc )
1727             {
1728                 item = proto_tree_add_uint_format(checksum_tree, hf_oss_crc, message_tvb,
1729                         frameStart2, frame2Length, calc2_crc, "Calculated CRC: 0x%04X", calc2_crc);
1730                 PROTO_ITEM_SET_GENERATED(item);
1731                 expert_add_info(pinfo, item, &ei_crc_frame_2_invalid );
1732             }
1733         }
1734         else
1735             expert_add_info(pinfo, item, &ei_crc_frame_2_unknown_scm_udid );
1736     }
1737
1738     /* For a correct calculation of the second crc we need to know the scm udid.
1739      * If the dissection of the second frame has been triggered, we integrate the
1740      * crc for frame2 into the result */
1741     return (gboolean) (frame1_crc == calc1_crc) && ( global_calculate_crc2 == TRUE ? (frame2_crc == calc2_crc) : TRUE);
1742 }
1743
1744 static gboolean
1745 dissect_opensafety_message(guint16 frameStart1, guint16 frameStart2, guint8 type,
1746                            tvbuff_t *message_tvb, packet_info *pinfo,
1747                            proto_item *opensafety_item, proto_tree *opensafety_tree, guint8 u_nrInPackage)
1748 {
1749     guint8      b_ID, ctr;
1750     guint8      scm_udid[6];
1751     GByteArray *scmUDID = NULL;
1752     gboolean    validSCMUDID;
1753     proto_item *item;
1754     gboolean    messageTypeUnknown, crcValid;
1755
1756     messageTypeUnknown = FALSE;
1757
1758     for ( ctr = 0; ctr < 6; ctr++ )
1759         scm_udid[ctr] = 0;
1760
1761     b_ID = OSS_FRAME_ID_T(message_tvb, frameStart1);
1762     /* Clearing connection valid bit */
1763     if ( type == OPENSAFETY_SPDO_MESSAGE_TYPE )
1764         b_ID = b_ID & 0xF8;
1765
1766     col_append_fstr(pinfo->cinfo, COL_INFO, (u_nrInPackage > 1 ? " | %s" : "%s" ),
1767             val_to_str(b_ID, message_type_values, "Unknown Message (0x%02X) "));
1768
1769     {
1770         if ( type == OPENSAFETY_SNMT_MESSAGE_TYPE )
1771         {
1772             dissect_opensafety_snmt_message ( message_tvb, pinfo, opensafety_tree, frameStart1, frameStart2 );
1773         }
1774         else
1775         {
1776             validSCMUDID = FALSE;
1777             scmUDID = g_byte_array_new();
1778
1779             if ( hex_str_to_bytes((local_scm_udid != NULL ? local_scm_udid : global_scm_udid), scmUDID, TRUE) && scmUDID->len == 6 )
1780             {
1781                 validSCMUDID = TRUE;
1782
1783                 /* Now confirm, that the xor operation was successful. The ID fields of both frames have to be the same */
1784                 b_ID = OSS_FRAME_ID_T(message_tvb, frameStart2) ^ (guint8)(scmUDID->data[OSS_FRAME_POS_ID]);
1785
1786                 if ( ( OSS_FRAME_ID_T(message_tvb, frameStart1) ^ b_ID ) != 0 )
1787                     validSCMUDID = FALSE;
1788                 else
1789                     for ( ctr = 0; ctr < 6; ctr++ )
1790                         scm_udid[ctr] = scmUDID->data[ctr];
1791             }
1792
1793             if ( strlen ( (local_scm_udid != NULL ? local_scm_udid : global_scm_udid) ) > 0  && scmUDID->len == 6 )
1794             {
1795                 if ( local_scm_udid != NULL )
1796                 {
1797                     item = proto_tree_add_string(opensafety_tree, hf_oss_scm_udid_auto, message_tvb, 0, 0, local_scm_udid);
1798                     if ( ! validSCMUDID )
1799                         expert_add_info(pinfo, item, &ei_message_id_field_mismatch );
1800                 }
1801                 else
1802                     item = proto_tree_add_string(opensafety_tree, hf_oss_scm_udid, message_tvb, 0, 0, global_scm_udid);
1803                 PROTO_ITEM_SET_GENERATED(item);
1804             }
1805
1806             item = proto_tree_add_boolean(opensafety_tree, hf_oss_scm_udid_valid, message_tvb, 0, 0, validSCMUDID);
1807             if ( scmUDID->len != 6 )
1808                 expert_add_info(pinfo, item, &ei_scmudid_invalid_preference );
1809             PROTO_ITEM_SET_GENERATED(item);
1810
1811             g_byte_array_free( scmUDID, TRUE);
1812
1813             if ( type == OPENSAFETY_SSDO_MESSAGE_TYPE || type == OPENSAFETY_SLIM_SSDO_MESSAGE_TYPE )
1814             {
1815                 dissect_opensafety_ssdo_message ( message_tvb, pinfo, opensafety_tree, frameStart1, frameStart2, validSCMUDID, scm_udid );
1816             }
1817             else if ( type == OPENSAFETY_SPDO_MESSAGE_TYPE )
1818             {
1819                 dissect_opensafety_spdo_message ( message_tvb, pinfo, opensafety_tree, frameStart1, frameStart2, validSCMUDID, scm_udid );
1820             }
1821             else
1822             {
1823                 messageTypeUnknown = TRUE;
1824             }
1825         }
1826
1827         crcValid = FALSE;
1828         item = proto_tree_add_uint(opensafety_tree, hf_oss_length,
1829                                    message_tvb, OSS_FRAME_POS_LEN + frameStart1, 1, OSS_FRAME_LENGTH_T(message_tvb, frameStart1));
1830         if ( messageTypeUnknown )
1831         {
1832             expert_add_info(pinfo, item, &ei_message_unknown_type );
1833         }
1834         else
1835         {
1836             crcValid = dissect_opensafety_checksum ( message_tvb, pinfo, opensafety_tree, type, frameStart1, frameStart2 );
1837         }
1838
1839         if ( ! crcValid )
1840         {
1841             expert_add_info(pinfo, opensafety_item, &ei_crc_frame_1_invalid );
1842         }
1843
1844         /* with SNMT's we can check if the ID's for the frames match. Rare randomized packages do have
1845          * an issue, where an frame 1 can be valid. The id's for both frames must differ, as well as
1846          * the addresses, but addresses won't be checked yet, as there are issues with SDN xored on it. */
1847         if ( crcValid && type == OPENSAFETY_SNMT_MESSAGE_TYPE )
1848         {
1849             if ( OSS_FRAME_ID_T(message_tvb, frameStart1) != OSS_FRAME_ID_T(message_tvb, frameStart2) )
1850                 expert_add_info(pinfo, opensafety_item, &ei_crc_frame_1_valid_frame2_invalid );
1851         }
1852     }
1853
1854     return TRUE;
1855 }
1856
1857 static gboolean
1858 opensafety_package_dissector(const gchar *protocolName, const gchar *sub_diss_handle,
1859                              gboolean b_frame2First, gboolean do_byte_swap, guint8 force_nr_in_package,
1860                              tvbuff_t *given_tvb , packet_info *pinfo , proto_tree *tree )
1861 {
1862     tvbuff_t           *next_tvb, *message_tvb = NULL;
1863     guint               length, len, frameOffset, frameLength, nodeAddress;
1864     guint8             *bytes;
1865     gboolean            handled, dissectorCalled, call_sub_dissector, markAsMalformed;
1866     guint8              type, found, packageCounter, i, tempByte;
1867     guint16             frameStart1, frameStart2, byte_offset;
1868     gint                reported_len;
1869     dissector_handle_t  protocol_dissector = NULL;
1870     proto_item         *opensafety_item;
1871     proto_tree         *opensafety_tree;
1872
1873     handled            = FALSE;
1874     dissectorCalled    = FALSE;
1875     call_sub_dissector = FALSE;
1876     markAsMalformed    = FALSE;
1877
1878     /* registering frame end routine, to prevent a malformed dissection preventing
1879      * further dissector calls (see bug #6950) */
1880     register_frame_end_routine(pinfo, reset_dissector);
1881
1882     length = tvb_reported_length(given_tvb);
1883     /* Minimum package length is 11 */
1884     if ( length < OSS_MINIMUM_LENGTH )
1885         return FALSE;
1886
1887     if ( strlen( sub_diss_handle ) > 0 )
1888     {
1889         call_sub_dissector = TRUE;
1890         protocol_dissector = find_dissector ( sub_diss_handle );
1891         if ( protocol_dissector == NULL )
1892             protocol_dissector = find_dissector ( "data" );
1893     }
1894
1895     reported_len = tvb_reported_length_remaining(given_tvb, 0);
1896     bytes = (guint8 *) wmem_alloc(pinfo->pool, length);
1897     tvb_memcpy(given_tvb, bytes, 0, length);
1898
1899     if ( do_byte_swap == TRUE && global_mbtcp_big_endian == TRUE )
1900     {
1901         /* Wordswapping for modbus detection */
1902         /* Only a even number of bytes can be swapped */
1903         len = (length / 2);
1904         for ( i = 0; i < len; i++ )
1905         {
1906             tempByte = bytes [ 2 * i ]; bytes [ 2 * i ] = bytes [ 2 * i + 1 ]; bytes [ 2 * i + 1 ] = tempByte;
1907         }
1908
1909         message_tvb = tvb_new_real_data(bytes, length, reported_len);
1910     } else {
1911         message_tvb = given_tvb;
1912     }
1913
1914     frameOffset = 0;
1915     frameLength = 0;
1916     found = 0;
1917     packageCounter = 0;
1918
1919     while ( frameOffset < length )
1920     {
1921         /* Smallest possible frame size is 11 */
1922         if ( tvb_length_remaining(message_tvb, frameOffset ) < OSS_MINIMUM_LENGTH )
1923             break;
1924
1925         /* Finding the start of the first possible safety frame */
1926         if ( findSafetyFrame(message_tvb, frameOffset, b_frame2First, &frameOffset, &frameLength) )
1927         {
1928             /* frameLength is calculated/read directly from the dissected data. If frameLenght and frameOffset together
1929              * are bigger than the reported length, the package is not really an openSAFETY package */
1930             if ( ( frameOffset + frameLength ) > (guint)reported_len )
1931                 break;
1932
1933             found++;
1934
1935             byte_offset = ( b_frame2First ? 0 : frameOffset );
1936             /* We determine a possible position for frame 1 and frame 2 */
1937             if ( b_frame2First )
1938             {
1939                 frameStart1 = findFrame1Position (message_tvb, byte_offset, frameLength, FALSE );
1940                 frameStart2 = 0;
1941             }
1942             else
1943             {
1944                 frameStart1 = 0;
1945                 frameStart2 = ((OSS_FRAME_LENGTH_T(message_tvb, byte_offset + frameStart1) - 1) +
1946                         (OSS_FRAME_LENGTH_T(message_tvb, byte_offset + frameStart1) > OSS_PAYLOAD_MAXSIZE_FOR_CRC8 ? OSS_SLIM_FRAME2_WITH_CRC16 : OSS_SLIM_FRAME2_WITH_CRC8));
1947             }
1948
1949             /* If both frame starts are equal, something went wrong. In which case, we retract the found entry, and
1950              * also increase the search offset, just doing a continue will result in an infinite loop. */
1951             if (frameStart1 == frameStart2)
1952             {
1953                 found--;
1954                 frameOffset += frameLength ;
1955                 continue;
1956             }
1957
1958             /* We determine the possible type, and return false, if there could not be one */
1959             if ( ( OSS_FRAME_ID_T(message_tvb, byte_offset + frameStart1) & OPENSAFETY_SLIM_SSDO_MESSAGE_TYPE ) == OPENSAFETY_SLIM_SSDO_MESSAGE_TYPE )
1960                 type = OPENSAFETY_SLIM_SSDO_MESSAGE_TYPE;
1961             else if ( ( OSS_FRAME_ID_T(message_tvb, byte_offset + frameStart1) & OPENSAFETY_SSDO_MESSAGE_TYPE ) == OPENSAFETY_SSDO_MESSAGE_TYPE )
1962                 type = OPENSAFETY_SSDO_MESSAGE_TYPE;
1963             else if ( ( OSS_FRAME_ID_T(message_tvb, byte_offset + frameStart1) & OPENSAFETY_SPDO_MESSAGE_TYPE ) == OPENSAFETY_SPDO_MESSAGE_TYPE )
1964                 type = OPENSAFETY_SPDO_MESSAGE_TYPE;
1965             else if ( ( OSS_FRAME_ID_T(message_tvb, byte_offset + frameStart1) & OPENSAFETY_SNMT_MESSAGE_TYPE ) == OPENSAFETY_SNMT_MESSAGE_TYPE )
1966                 type = OPENSAFETY_SNMT_MESSAGE_TYPE;
1967             else
1968             {
1969                 /* This is an invalid openSAFETY package, but it could be an undetected slim ssdo message. This specific error
1970                  * will only occur, if findFrame1Position is in play. So we search once more, but this time calculating the CRC.
1971                  * The reason for the second run is, that calculating the CRC is time consuming.  */
1972                 if ( b_frame2First )
1973                 {
1974                     /* Now let's check again, but this time calculate the CRC */
1975                     frameStart1 = findFrame1Position(message_tvb, ( b_frame2First ? 0 : frameOffset ), frameLength, TRUE );
1976                     frameStart2 = 0;
1977
1978                     if ( ( OSS_FRAME_ID_T(message_tvb, byte_offset + frameStart1) & OPENSAFETY_SLIM_SSDO_MESSAGE_TYPE ) == OPENSAFETY_SLIM_SSDO_MESSAGE_TYPE )
1979                         type = OPENSAFETY_SLIM_SSDO_MESSAGE_TYPE;
1980                     else if ( ( OSS_FRAME_ID_T(message_tvb, byte_offset + frameStart1) & OPENSAFETY_SSDO_MESSAGE_TYPE ) == OPENSAFETY_SSDO_MESSAGE_TYPE )
1981                         type = OPENSAFETY_SSDO_MESSAGE_TYPE;
1982                     else if ( ( OSS_FRAME_ID_T(message_tvb, byte_offset + frameStart1) & OPENSAFETY_SPDO_MESSAGE_TYPE ) == OPENSAFETY_SPDO_MESSAGE_TYPE )
1983                         type = OPENSAFETY_SPDO_MESSAGE_TYPE;
1984                     else if ( ( OSS_FRAME_ID_T(message_tvb, byte_offset + frameStart1) & OPENSAFETY_SNMT_MESSAGE_TYPE ) == OPENSAFETY_SNMT_MESSAGE_TYPE )
1985                         type = OPENSAFETY_SNMT_MESSAGE_TYPE;
1986                     else {
1987                         /* Skip this frame.  We cannot continue without
1988                            advancing frameOffset - just doing a continue
1989                            will result in an infinite loop. Advancing with 1 will
1990                            lead to infinite loop, advancing with frameLength might miss
1991                            some packages*/
1992                         frameOffset += 2;
1993                         found--;
1994                         continue;
1995                     }
1996                 } else {
1997                     /* As stated above, you cannot just continue
1998                        without advancing frameOffset. Advancing with 1 will
1999                        lead to infinite loop, advancing with frameLength might miss
2000                        some packages*/
2001                     frameOffset += 2;
2002                     found--;
2003                     continue;
2004                 }
2005             }
2006
2007             /* Some faulty packages do indeed have a valid first frame, but the second is
2008              * invalid. These checks should prevent most faulty detections */
2009             if ( type != OPENSAFETY_SPDO_MESSAGE_TYPE )
2010             {
2011                 /* Is the given type at least known? */
2012                 gint idx = -1;
2013                 try_val_to_str_idx(OSS_FRAME_ID_T(message_tvb, byte_offset + frameStart1), message_type_values, &idx );
2014                 /* Unknown Frame Type */
2015                 if ( idx < 0 )
2016                 {
2017                     frameOffset += 2;
2018                     found--;
2019                     continue;
2020                 }
2021                 /* Frame IDs do not match */
2022                 else if ( type == OPENSAFETY_SNMT_MESSAGE_TYPE &&
2023                         (OSS_FRAME_ID_T(message_tvb, byte_offset + frameStart1) != OSS_FRAME_ID_T(message_tvb, byte_offset + frameStart2)) )
2024                 {
2025                     frameOffset += 2;
2026                     found--;
2027                     continue;
2028                 }
2029             }
2030
2031             /* Checking if the producer for a SPDO message is valid, otherwise the opensafety package
2032              * is malformed. Instead of declining dissection, the package get's marked as malformed */
2033             if ( type == OPENSAFETY_SPDO_MESSAGE_TYPE )
2034             {
2035                 nodeAddress = OSS_FRAME_ADDR_T(message_tvb, byte_offset + frameStart1);
2036                 if ( nodeAddress > 1024 ) {
2037                     markAsMalformed = TRUE;
2038                 }
2039             }
2040
2041             /* If this package is not valid, the next step, which normally occurs in unxorFrame will lead to a
2042              * frameLength bigger than the maximum data size. This is an indicator, that the package in general
2043              * is fault, and therefore we return false.
2044              */
2045             if ( ( (gint)frameLength - (gint)( frameStart2 > frameStart1 ? frameStart2 : frameLength - frameStart1 ) ) < 0 )
2046                 return FALSE;
2047
2048             /* From here on, the package should be correct, therefore adding second frame */
2049             if ( do_byte_swap == TRUE && global_mbtcp_big_endian == TRUE )
2050             {
2051                 next_tvb = tvb_new_child_real_data(message_tvb, &bytes[frameOffset], (frameLength), reported_len);
2052                 /* Adding a visual aid to the dissector tree */
2053                 add_new_data_source(pinfo, next_tvb, "openSAFETY Frame (Swapped)");
2054             }
2055             else
2056             {
2057                 next_tvb = tvb_new_subset(message_tvb, frameOffset, frameLength, reported_len);
2058                 /* Adding a visual aid to the dissector tree */
2059                 add_new_data_source(pinfo, next_tvb, "openSAFETY Frame");
2060             }
2061
2062             /* A new subtype for package dissection will need to set the actual nr. for the whole dissected package */
2063             if ( force_nr_in_package > 0 )
2064             {
2065                 found = force_nr_in_package + 1;
2066                 dissectorCalled = TRUE;
2067                 col_set_str(pinfo->cinfo, COL_PROTOCOL, protocolName);
2068             }
2069
2070             if ( ! dissectorCalled )
2071             {
2072                 if ( call_sub_dissector )
2073                     call_dissector(protocol_dissector, message_tvb, pinfo, tree);
2074                 dissectorCalled = TRUE;
2075
2076                 col_set_str(pinfo->cinfo, COL_PROTOCOL, protocolName);
2077                 col_clear(pinfo->cinfo,COL_INFO);
2078             }
2079
2080             /* if the tree is NULL, we are called for the overview, otherwise for the
2081              more detailed view of the package */
2082             if ( tree )
2083             {
2084                 /* create the opensafety protocol tree */
2085                 opensafety_item = proto_tree_add_item(tree, proto_opensafety, message_tvb, frameOffset, frameLength, ENC_NA);
2086                 opensafety_tree = proto_item_add_subtree(opensafety_item, ett_opensafety);
2087             } else {
2088                 opensafety_item = NULL;
2089                 opensafety_tree = NULL;
2090             }
2091
2092             if ( dissect_opensafety_message(frameStart1, frameStart2, type, next_tvb, pinfo, opensafety_item, opensafety_tree, found) == TRUE )
2093                 packageCounter++;
2094             else
2095                 markAsMalformed = TRUE;
2096
2097             if ( tree && markAsMalformed )
2098             {
2099                 if ( OSS_FRAME_ADDR_T(message_tvb, byte_offset + frameStart1) > 1024 )
2100                     expert_add_info(pinfo, opensafety_item, &ei_message_spdo_address_invalid );
2101             }
2102             handled = TRUE;
2103         }
2104         else
2105             break;
2106
2107         frameOffset += frameLength;
2108     }
2109
2110     if ( handled == TRUE && packageCounter == 0 )
2111         handled = FALSE;
2112
2113     if ( ! handled )
2114     {
2115         if ( call_sub_dissector )
2116             call_dissector(protocol_dissector, message_tvb, pinfo, tree);
2117         handled = TRUE;
2118     }
2119
2120     return ( handled ? TRUE : FALSE );
2121 }
2122
2123 static gboolean
2124 dissect_opensafety_epl(tvbuff_t *message_tvb , packet_info *pinfo , proto_tree *tree , void *data _U_ )
2125 {
2126     gboolean        result     = FALSE;
2127     guint8          firstByte;
2128
2129     if ( ! global_enable_plk )
2130         return result;
2131
2132     /* We will call the epl dissector by using call_dissector(). The epl dissector will then call
2133      * the heuristic openSAFETY dissector again. By setting this information, we prevent a dissector
2134      * loop */
2135     if ( bDissector_Called_Once_Before == FALSE )
2136     {
2137         bDissector_Called_Once_Before = TRUE;
2138
2139         firstByte = ( tvb_get_guint8(message_tvb, 0) << 1 );
2140
2141         /* No frames can be sent in SoA and SoC messages, therefore those get filtered right away */
2142         if ( ( firstByte != 0x02 ) && ( firstByte != 0x0A ) )
2143         {
2144             result = opensafety_package_dissector("openSAFETY/Powerlink", "epl",
2145                                                   FALSE, FALSE, 0, message_tvb, pinfo, tree);
2146         }
2147
2148         bDissector_Called_Once_Before = FALSE;
2149     }
2150
2151     return result;
2152 }
2153
2154
2155 static gboolean
2156 dissect_opensafety_siii(tvbuff_t *message_tvb , packet_info *pinfo , proto_tree *tree , void *data _U_ )
2157 {
2158     gboolean        result     = FALSE;
2159     guint8          firstByte;
2160
2161     if ( ! global_enable_siii )
2162         return result;
2163
2164     if ( pinfo->ipproto == IPPROTO_UDP )
2165     {
2166         return  opensafety_package_dissector("openSAFETY/SercosIII UDP", "", FALSE, FALSE, 0, message_tvb, pinfo, tree);
2167     }
2168
2169     /* We can assume to have a SercosIII package, as the SercosIII dissector won't detect
2170      * SercosIII-UDP packages, this is most likely SercosIII-over-ethernet */
2171
2172     /* We will call the SercosIII dissector by using call_dissector(). The SercosIII dissector will
2173      * then call the heuristic openSAFETY dissector again. By setting this information, we prevent
2174      * a dissector loop. */
2175     if ( bDissector_Called_Once_Before == FALSE )
2176     {
2177         bDissector_Called_Once_Before = TRUE;
2178         /* No frames can be sent in AT messages, therefore those get filtered right away */
2179         firstByte = ( tvb_get_guint8(message_tvb, 0) << 1 );
2180         if ( ( firstByte & 0x40 ) == 0x40 )
2181         {
2182             result = opensafety_package_dissector("openSAFETY/SercosIII", "sercosiii",
2183                                                   FALSE, FALSE, 0, message_tvb, pinfo, tree);
2184         }
2185         bDissector_Called_Once_Before = FALSE;
2186     }
2187
2188     return result;
2189 }
2190
2191 static gboolean
2192 dissect_opensafety_pn_io(tvbuff_t *message_tvb , packet_info *pinfo , proto_tree *tree , void *data _U_ )
2193 {
2194     gboolean        result     = FALSE;
2195
2196     if ( ! global_enable_pnio )
2197         return result;
2198
2199     /* We will call the pn_io dissector by using call_dissector(). The epl dissector will then call
2200      * the heuristic openSAFETY dissector again. By setting this information, we prevent a dissector
2201      * loop */
2202     if ( bDissector_Called_Once_Before == FALSE )
2203     {
2204         bDissector_Called_Once_Before = TRUE;
2205         result = opensafety_package_dissector("openSAFETY/Profinet IO", "pn_io",
2206                                               FALSE, FALSE, 0, message_tvb, pinfo, tree);
2207         bDissector_Called_Once_Before = FALSE;
2208     }
2209
2210     return result;
2211 }
2212
2213 static gboolean
2214 dissect_opensafety_mbtcp(tvbuff_t *message_tvb , packet_info *pinfo , proto_tree *tree, void *data _U_ )
2215 {
2216     if ( ! global_enable_mbtcp )
2217         return FALSE;
2218
2219     /* When Modbus/TCP get's dissected, openSAFETY would be sorted as a child protocol. Although,
2220      * this behaviour is technically correct, it differs from other implemented IEM protocol handlers.
2221      * Therefore, the openSAFETY frame get's put one up, if the parent is not NULL */
2222     return opensafety_package_dissector("openSAFETY/Modbus TCP", "", FALSE, TRUE, 0,
2223                                         message_tvb, pinfo, ( tree->parent != NULL ? tree->parent : tree ));
2224 }
2225
2226 static gboolean
2227 dissect_opensafety_udpdata(tvbuff_t *message_tvb , packet_info *pinfo , proto_tree *tree , void *data _U_ )
2228 {
2229     gboolean       result   = FALSE;
2230     static guint32 frameNum = 0;
2231     static guint32 frameIdx = 0;
2232
2233     if ( ! global_enable_udp )
2234         return result;
2235
2236     /* An openSAFETY frame has at least OSS_MINIMUM_LENGTH bytes */
2237     if ( tvb_length ( message_tvb ) < OSS_MINIMUM_LENGTH )
2238         return result;
2239
2240     /* More than one openSAFETY package could be transported in the same frame,
2241         * in such a case, we need to establish the number of packages inside the frame */
2242     if ( pinfo->fd->num != frameNum )
2243     {
2244         frameIdx = 0;
2245         frameNum = pinfo->fd->num;
2246     }
2247
2248     result = opensafety_package_dissector((pinfo->destport == UDP_PORT_SIII ? "openSAFETY/SercosIII" : "openSAFETY/UDP" ),
2249                                           "", pinfo->destport == UDP_PORT_SIII ? global_siii_udp_frame2_first : global_udp_frame2_first,
2250                                           FALSE, frameIdx, message_tvb, pinfo, tree);
2251
2252     if ( result )
2253         frameIdx++;
2254
2255     return result;
2256 }
2257
2258 static void
2259 apply_prefs ( void )
2260 {
2261     static gboolean opensafety_init = FALSE;
2262     static guint    opensafety_udp_port_number;
2263     static guint    opensafety_udp_siii_port_number;
2264
2265     /* It only should delete dissectors, if run for any time except the first */
2266     if ( opensafety_init )
2267     {
2268         /* Delete dissectors in preparation of a changed config setting */
2269         dissector_delete_uint ("udp.port", opensafety_udp_port_number, find_dissector("opensafety_udpdata"));
2270         dissector_delete_uint ("udp.port", opensafety_udp_siii_port_number, find_dissector("opensafety_siii"));
2271     }
2272
2273     opensafety_init = TRUE;
2274
2275     /* Storing the port numbers locally, to being able to delete the old associations */
2276     opensafety_udp_port_number = global_network_udp_port;
2277     opensafety_udp_siii_port_number = global_network_udp_port_sercosiii;
2278
2279     /* Default UDP only based dissector */
2280     dissector_add_uint("udp.port", opensafety_udp_port_number, find_dissector("opensafety_udpdata"));
2281
2282     /* Sercos III dissector does not handle UDP transport, has to be handled
2283      *  separately, everything else should be caught by the heuristic dissector
2284      */
2285     dissector_add_uint("udp.port", opensafety_udp_siii_port_number, find_dissector("opensafety_siii"));
2286
2287 }
2288
2289 void
2290 proto_register_opensafety(void)
2291 {
2292     /* Setup list of header fields */
2293     static hf_register_info hf[] = {
2294         { &hf_oss_scm_udid,
2295             { "SCM UDID Configured",    "opensafety.scm_udid",
2296             FT_STRING,   BASE_NONE, NULL,   0x0, NULL, HFILL } },
2297         { &hf_oss_scm_udid_auto,
2298             { "SCM UDID Auto Detect",    "opensafety.scm_udid.auto",
2299             FT_STRING,   BASE_NONE, NULL,   0x0, NULL, HFILL } },
2300         { &hf_oss_scm_udid_valid,
2301             { "SCM UDID Valid",    "opensafety.scm_udid.valid",
2302             FT_BOOLEAN,   BASE_NONE, NULL,   0x0, NULL, HFILL } },
2303
2304         { &hf_oss_msg,
2305             { "Message",    "opensafety.msg.id",
2306             FT_UINT8,   BASE_HEX, VALS(message_type_values),   0x0, NULL, HFILL } },
2307         { &hf_oss_msg_category,
2308             { "Type",  "opensafety.msg.type",
2309             FT_UINT16,   BASE_HEX, VALS(message_id_values),   0x0, NULL, HFILL } },
2310         { &hf_oss_msg_direction,
2311             { "Direction",  "opensafety.msg.direction",
2312             FT_BOOLEAN,   BASE_NONE, TFS(&opensafety_message_direction),   0x0, NULL, HFILL } },
2313         { &hf_oss_msg_node,
2314             { "Safety Node",  "opensafety.msg.node",
2315             FT_UINT16,   BASE_HEX, NULL,   0x0, NULL, HFILL } },
2316         { &hf_oss_msg_network,
2317             { "Safety Domain",  "opensafety.msg.network",
2318             FT_UINT16,   BASE_HEX, NULL,   0x0, NULL, HFILL } },
2319         { &hf_oss_msg_sender,
2320             { "Sender",  "opensafety.msg.sender",
2321             FT_UINT16,   BASE_HEX, NULL,   0x0, NULL, HFILL } },
2322         { &hf_oss_msg_receiver,
2323             { "Receiver",  "opensafety.msg.receiver",
2324             FT_UINT16,   BASE_HEX, NULL,   0x0, NULL, HFILL } },
2325         { &hf_oss_length,
2326             { "Length",    "opensafety.length",
2327             FT_UINT8,   BASE_DEC, NULL,     0x0, NULL, HFILL } },
2328         { &hf_oss_crc,
2329             { "CRC",       "opensafety.crc.data",
2330             FT_UINT16,  BASE_HEX, NULL,    0x0, NULL, HFILL } },
2331
2332         { &hf_oss_crc_valid,
2333             { "Is Valid", "opensafety.crc.valid",
2334             FT_BOOLEAN, BASE_NONE, NULL,    0x0, NULL, HFILL } },
2335         { &hf_oss_crc_type,
2336             { "CRC Type",  "opensafety.crc.type",
2337             FT_UINT8,   BASE_DEC, VALS(message_crc_type),    0x0, NULL, HFILL } },
2338         { &hf_oss_crc2_valid,
2339             { "Is Valid", "opensafety.crc2.valid",
2340             FT_BOOLEAN, BASE_NONE, NULL,    0x0, NULL, HFILL } },
2341
2342         /* SNMT Specific fields */
2343         { &hf_oss_snmt_slave,
2344             { "SNMT Slave",    "opensafety.snmt.slave",
2345             FT_UINT16,  BASE_HEX, NULL,    0x0, NULL, HFILL } },
2346         { &hf_oss_snmt_master,
2347             { "SNMT Master",   "opensafety.snmt.master",
2348             FT_UINT16,  BASE_HEX, NULL,    0x0, NULL, HFILL } },
2349         { &hf_oss_snmt_scm,
2350             { "SCM",    "opensafety.snmt.scm",
2351             FT_UINT16,  BASE_HEX, NULL,    0x0, NULL, HFILL } },
2352         { &hf_oss_snmt_tool,
2353             { "Tool ID",   "opensafety.snmt.tool_id",
2354             FT_UINT16,  BASE_HEX, NULL,    0x0, NULL, HFILL } },
2355         { &hf_oss_snmt_udid,
2356             { "UDID for SN",   "opensafety.snmt.udid",
2357             FT_ETHER,  BASE_NONE, NULL,    0x0, NULL, HFILL } },
2358         { &hf_oss_snmt_service_id,
2359             { "Extended Service ID",   "opensafety.snmt.service_id",
2360             FT_UINT8,  BASE_HEX, VALS(message_service_type),    0x0, NULL, HFILL } },
2361         { &hf_oss_snmt_error_group,
2362             { "Error Group",   "opensafety.snmt.error_group",
2363             FT_UINT8,  BASE_DEC, NULL,    0x0, NULL, HFILL } },
2364         { &hf_oss_snmt_error_code,
2365             { "Error Code",   "opensafety.snmt.error_code",
2366             FT_UINT8,  BASE_DEC, NULL,   0x0, NULL, HFILL } },
2367
2368         /* SSDO Specific fields */
2369         { &hf_oss_ssdo_server,
2370             { "SSDO Server", "opensafety.ssdo.master",
2371             FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL } },
2372         { &hf_oss_ssdo_client,
2373             { "SSDO Client", "opensafety.ssdo.client",
2374             FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL } },
2375         { &hf_oss_ssdo_sano,
2376             { "SOD Access Request Number", "opensafety.ssdo.sano",
2377             FT_UINT16,  BASE_DEC, NULL,    0x0, NULL, HFILL } },
2378         { &hf_oss_ssdo_sacmd,
2379             { "SOD Access Command", "opensafety.ssdo.sacmd",
2380             FT_UINT8,  BASE_HEX, VALS(ssdo_sacmd_values),    0x0, NULL, HFILL } },
2381         { &hf_oss_ssdo_sod_index,
2382             { "SOD Index", "opensafety.ssdo.sodentry.index",
2383             FT_UINT16,  BASE_HEX, NULL,    0x0, NULL, HFILL } },
2384         { &hf_oss_ssdo_sod_subindex,
2385             { "SOD Sub Index", "opensafety.ssdo.sodentry.subindex",
2386             FT_UINT8,  BASE_HEX, NULL,    0x0, NULL, HFILL } },
2387         { &hf_oss_ssdo_payload,
2388             { "SOD Payload", "opensafety.ssdo.payload",
2389             FT_BYTES,  BASE_NONE, NULL,    0x0, NULL, HFILL } },
2390         { &hf_oss_ssdo_payload_size,
2391             { "SOD Payload Size", "opensafety.ssdo.payloadsize",
2392             FT_UINT32,  BASE_DEC, NULL,    0x0, NULL, HFILL } },
2393         { &hf_oss_ssdo_sodentry_size,
2394             { "SOD Entry Size", "opensafety.ssdo.sodentry.size",
2395             FT_UINT32,  BASE_DEC, NULL,    0x0, NULL, HFILL } },
2396         { &hf_oss_ssdo_sodentry_data,
2397             { "SOD Data", "opensafety.ssdo.sodentry.data",
2398             FT_BYTES,  BASE_NONE, NULL,    0x0, NULL, HFILL } },
2399         { &hf_oss_sod_par_timestamp,
2400             { "Parameter Timestamp", "opensafety.sod.parameter.timestamp",
2401             FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL } },
2402         { &hf_oss_sod_par_checksum,
2403             { "Parameter Checksum", "opensafety.sod.parameter.checksum",
2404             FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL } },
2405
2406         { &hf_oss_ssdo_sodmapping,
2407             { "Mapping entry", "opensafety.sod.mapping",
2408             FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL } },
2409         { &hf_oss_ssdo_sodmapping_bits,
2410             { "Mapping size", "opensafety.sod.mapping.bits",
2411             FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL } },
2412
2413         { &hf_oss_ssdo_extpar_parset,
2414             { "Set Nr", "opensafety.ssdo.extpar.setnr",
2415             FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL } },
2416         { &hf_oss_ssdo_extpar_version,
2417             { "Version", "opensafety.ssdo.extpar.version",
2418             FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL } },
2419         { &hf_oss_ssdo_extpar_saddr,
2420             { "SADDR", "opensafety.ssdo.extpar.saddr",
2421             FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL } },
2422         { &hf_oss_ssdo_extpar_length,
2423             { "Set Length", "opensafety.ssdo.extpar.length",
2424             FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL } },
2425         { &hf_oss_ssdo_extpar_crc,
2426             { "Set CRC", "opensafety.ssdo.extpar.crc",
2427             FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL } },
2428         { &hf_oss_ssdo_extpar_tstamp,
2429             { "Timestamp", "opensafety.ssdo.extpar.timestamp",
2430             FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL } },
2431         { &hf_oss_ssdo_extpar_data,
2432             { "Ext. Parameter Data", "opensafety.ssdo.extpar.data",
2433             FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL } },
2434         { &hf_oss_ssdo_extpar,
2435             { "Ext. Parameter", "opensafety.ssdo.extpar",
2436             FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL } },
2437
2438         {&hf_oss_fragments,
2439             {"Message fragments", "opensafety.ssdo.fragments",
2440             FT_NONE, BASE_NONE, NULL, 0x00, NULL, HFILL } },
2441         {&hf_oss_fragment,
2442             {"Message fragment", "opensafety.ssdo.fragment",
2443             FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL } },
2444         {&hf_oss_fragment_overlap,
2445             {"Message fragment overlap", "opensafety.ssdo.fragment.overlap",
2446             FT_BOOLEAN, 0, NULL, 0x00, NULL, HFILL } },
2447         {&hf_oss_fragment_overlap_conflicts,
2448             {"Message fragment overlapping with conflicting data",
2449             "opensafety.ssdo.fragment.overlap.conflicts",
2450             FT_BOOLEAN, 0, NULL, 0x00, NULL, HFILL } },
2451         {&hf_oss_fragment_multiple_tails,
2452             {"Message has multiple tail fragments", "opensafety.ssdo.fragment.multiple_tails",
2453             FT_BOOLEAN, 0, NULL, 0x00, NULL, HFILL } },
2454         {&hf_oss_fragment_too_long_fragment,
2455             {"Message fragment too long", "opensafety.ssdo.fragment.too_long_fragment",
2456             FT_BOOLEAN, 0, NULL, 0x00, NULL, HFILL } },
2457         {&hf_oss_fragment_error,
2458             {"Message defragmentation error", "opensafety.ssdo.fragment.error",
2459             FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL } },
2460         {&hf_oss_fragment_count,
2461             {"Message fragment count", "opensafety.ssdo.fragment.count",
2462             FT_UINT32, BASE_DEC, NULL, 0x00, NULL, HFILL } },
2463         {&hf_oss_reassembled_in,
2464             {"Reassembled in", "opensafety.ssdo.reassembled.in",
2465             FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL } },
2466         {&hf_oss_reassembled_length,
2467             {"Reassembled length", "opensafety.ssdo.reassembled.length",
2468             FT_UINT32, BASE_DEC, NULL, 0x00, NULL, HFILL } },
2469         {&hf_oss_reassembled_data,
2470             {"Reassembled Data", "opensafety.ssdo.reassembled.data",
2471             FT_BYTES, BASE_NONE, NULL, 0x00, NULL, HFILL } },
2472
2473 #if 0
2474         { &hf_oss_ssdo_inhibit_time,
2475           { "Inhibit Time", "opensafety.ssdo.inhibittime",
2476             FT_UINT32,  BASE_HEX, NULL,    0x0, NULL, HFILL } },
2477 #endif
2478         { &hf_oss_ssdo_abort_code,
2479             { "Abort Code", "opensafety.ssdo.abortcode",
2480             FT_UINT32,  BASE_HEX, NULL,    0x0, NULL, HFILL } },
2481
2482         /* SSDO SACmd specific fields */
2483         { &hf_oss_ssdo_sacmd_access_type,
2484           { "Access Type", "opensafety.ssdo.sacmd.access",
2485             FT_BOOLEAN,  8, TFS(&opensafety_sacmd_acc), OPENSAFETY_SSDO_SACMD_ACC, NULL, HFILL } },
2486 #if 0
2487         { &hf_oss_ssdo_sacmd_reserved,
2488           { "Reserved", "opensafety.ssdo.sacmd.reserved",
2489             FT_BOOLEAN,  8, TFS(&opensafety_sacmd_res), OPENSAFETY_SSDO_SACMD_RES, NULL, HFILL } },
2490 #endif
2491         { &hf_oss_ssdo_sacmd_abort_transfer,
2492           { "Abort Transfer", "opensafety.ssdo.sacmd.abort_transfer",
2493             FT_BOOLEAN,  8, TFS(&opensafety_sacmd_abrt), OPENSAFETY_SSDO_SACMD_ABRT, NULL, HFILL } },
2494         { &hf_oss_ssdo_sacmd_segmentation,
2495           { "Segmentation", "opensafety.ssdo.sacmd.segmentation",
2496             FT_BOOLEAN,  8, TFS(&opensafety_sacmd_seg), OPENSAFETY_SSDO_SACMD_SEG, NULL, HFILL } },
2497         { &hf_oss_ssdo_sacmd_toggle,
2498           { "Toggle Bit", "opensafety.ssdo.sacmd.toggle",
2499             FT_BOOLEAN,  8, TFS(&opensafety_on_off), OPENSAFETY_SSDO_SACMD_TGL, NULL, HFILL } },
2500         { &hf_oss_ssdo_sacmd_initiate,
2501           { "Initiate Transfer", "opensafety.ssdo.sacmd.initiate",
2502             FT_BOOLEAN,  8, TFS(&opensafety_sacmd_ini), OPENSAFETY_SSDO_SACMD_INI, NULL, HFILL } },
2503         { &hf_oss_ssdo_sacmd_end_segment,
2504           { "End Segment", "opensafety.ssdo.sacmd.end_segment",
2505             FT_BOOLEAN,  8, TFS(&opensafety_sacmd_ensg), OPENSAFETY_SSDO_SACMD_ENSG, NULL, HFILL } },
2506         { &hf_oss_ssdo_sacmd_block_transfer,
2507           { "Block Transfer", "opensafety.ssdo.sacmd.block_transfer",
2508             FT_BOOLEAN,  8, TFS(&opensafety_sacmd_blk), OPENSAFETY_SSDO_SACMD_BLK, NULL, HFILL } },
2509
2510         /* SPDO Specific fields */
2511         { &hf_oss_spdo_connection_valid,
2512           { "Connection Valid Bit", "opensafety.spdo.connection_valid",
2513             FT_BOOLEAN,  BASE_NONE, TFS(&opensafety_set_notset),  0x0, NULL, HFILL } },
2514         { &hf_oss_spdo_payload,
2515           { "SPDO Payload", "opensafety.spdo.payload",
2516             FT_BYTES,  BASE_NONE, NULL,    0x0, NULL, HFILL } },
2517         { &hf_oss_spdo_producer,
2518           { "Producer", "opensafety.spdo.producer",
2519             FT_UINT16,  BASE_HEX, NULL,    0x0, NULL, HFILL } },
2520         { &hf_oss_spdo_producer_time,
2521           { "Internal Time Producer", "opensafety.spdo.time.producer",
2522             FT_UINT16,  BASE_HEX, NULL,    0x0, NULL, HFILL } },
2523         { &hf_oss_spdo_time_value_sn,
2524           { "Internal Time SN", "opensafety.spdo.time.sn",
2525             FT_UINT16,  BASE_HEX, NULL,    0x0, NULL, HFILL } },
2526         { &hf_oss_spdo_time_request,
2527           { "Time Request Counter", "opensafety.spdo.time.request_counter",
2528             FT_UINT8,  BASE_HEX, NULL,    0x0, NULL, HFILL } },
2529         { &hf_oss_spdo_time_request_to,
2530           { "Time Request from", "opensafety.spdo.time.request_from",
2531             FT_UINT16,  BASE_HEX, NULL,    0x0, NULL, HFILL } },
2532         { &hf_oss_spdo_time_request_from,
2533           { "Time Request by", "opensafety.spdo.time.request_to",
2534             FT_UINT16,  BASE_HEX, NULL,    0x0, NULL, HFILL } },
2535
2536     };
2537
2538     /* Setup protocol subtree array */
2539     static gint *ett[] = {
2540         &ett_opensafety,
2541         &ett_opensafety_sender,
2542         &ett_opensafety_receiver,
2543         &ett_opensafety_checksum,
2544         &ett_opensafety_snmt,
2545         &ett_opensafety_ssdo,
2546         &ett_opensafety_ssdo_sacmd,
2547         &ett_opensafety_ssdo_fragment,
2548         &ett_opensafety_ssdo_fragments,
2549         &ett_opensafety_ssdo_payload,
2550         &ett_opensafety_ssdo_sodentry,
2551         &ett_opensafety_sod_mapping,
2552         &ett_opensafety_ssdo_extpar,
2553         &ett_opensafety_spdo,
2554     };
2555
2556     static ei_register_info ei[] = {
2557         { &ei_crc_frame_1_invalid,
2558             { "opensafety.crc.error.frame1_invalid", PI_PROTOCOL, PI_ERROR,
2559               "Frame 1 CRC invalid, Possible error in package", EXPFILL } },
2560         { &ei_crc_frame_1_valid_frame2_invalid,
2561             { "opensafety.crc.error.frame1_valid_frame2_invalid", PI_PROTOCOL, PI_ERROR,
2562               "Frame 1 is valid, frame 2 id is invalid", EXPFILL } },
2563         { &ei_crc_slimssdo_instead_of_spdo,
2564             { "opensafety.crc.warning.wrong_crc_for_spdo", PI_PROTOCOL, PI_WARN,
2565               "Frame 1 SPDO CRC is Slim SSDO CRC16 0x5935", EXPFILL } },
2566         { &ei_crc_frame_2_invalid,
2567             { "opensafety.crc.error.frame2_invalid", PI_PROTOCOL, PI_ERROR,
2568               "Frame 2 CRC invalid, Possible error in package or crc calculation", EXPFILL } },
2569         { &ei_crc_frame_2_unknown_scm_udid,
2570             { "opensafety.crc.error.frame2_unknown_scmudid", PI_PROTOCOL, PI_WARN,
2571               "Frame 2 CRC invalid, SCM UDID was not auto-detected", EXPFILL } },
2572
2573         { &ei_message_reassembly_size_differs_from_header,
2574             { "opensafety.msg.warning.reassembly_size_fail", PI_PROTOCOL, PI_WARN,
2575               "Reassembled message size differs from size in header", EXPFILL } },
2576         { &ei_message_unknown_type,
2577             { "opensafety.msg.error.unknown_type", PI_MALFORMED, PI_ERROR,
2578               "Unknown openSAFETY message type", EXPFILL } },
2579         { &ei_message_spdo_address_invalid,
2580             { "opensafety.msg.error.spdo_address_invalid", PI_MALFORMED, PI_ERROR,
2581               "SPDO address is invalid", EXPFILL } },
2582         { &ei_message_id_field_mismatch,
2583             { "opensafety.msg.error.id.mismatch", PI_PROTOCOL, PI_ERROR,
2584               "ID for frame 2 is not the same as for frame 1", EXPFILL } },
2585
2586         { &ei_scmudid_autodetected,
2587             { "opensafety.scm_udid.note.autodetected", PI_PROTOCOL, PI_NOTE,
2588               "Auto detected payload as SCM UDID", EXPFILL } },
2589         { &ei_scmudid_invalid_preference,
2590             { "opensafety.scm_udid.note.invalid_preference", PI_PROTOCOL, PI_WARN,
2591               "openSAFETY protocol settings are invalid! SCM UDID first octet will be assumed to be 00", EXPFILL } },
2592         { &ei_scmudid_unknown,
2593             { "opensafety.scm_udid.warning.assuming_first_octet", PI_PROTOCOL, PI_WARN,
2594               "SCM UDID unknown, assuming 00 as first UDID octet", EXPFILL } },
2595
2596         { &ei_payload_unknown_format,
2597             { "opensafety.msg.warning.unknown_format", PI_PROTOCOL, PI_WARN,
2598               "Unknown payload format detected", EXPFILL } },
2599         { &ei_payload_length_not_positive,
2600             { "opensafety.msg.warning.reassembly_length_not_positive", PI_PROTOCOL, PI_NOTE,
2601               "Calculation for payload length yielded non-positive result", EXPFILL } },
2602     };
2603
2604     module_t *opensafety_module;
2605     expert_module_t *expert_opensafety;
2606
2607     /* Register the protocol name and description */
2608     proto_opensafety = proto_register_protocol("openSAFETY", "openSAFETY",  "opensafety");
2609     opensafety_module = prefs_register_protocol(proto_opensafety, apply_prefs);
2610
2611     /* Required function calls to register the header fields and subtrees used */
2612     proto_register_field_array(proto_opensafety, hf, array_length(hf));
2613     proto_register_subtree_array(ett, array_length(ett));
2614
2615     expert_opensafety = expert_register_protocol ( proto_opensafety );
2616     expert_register_field_array ( expert_opensafety, ei, array_length (ei ) );
2617
2618     /* register user preferences */
2619     prefs_register_string_preference(opensafety_module, "scm_udid",
2620                  "SCM UDID (xx:xx:xx:xx:xx:xx)",
2621                  "To be able to fully dissect SSDO and SPDO packages, a valid UDID for the SCM has to be provided",
2622                  &global_scm_udid);
2623     prefs_register_bool_preference(opensafety_module, "scm_udid_autoset",
2624                  "Set SCM UDID if detected in stream",
2625                  "Automatically assign a detected SCM UDID (by reading SNMT->SNTM_assign_UDID_SCM) and set it for the file",
2626                  &global_scm_udid_autoset);
2627     prefs_register_bool_preference(opensafety_module, "calculate_crc2",
2628                  "Enable CRC calculation in frame 2",
2629                  "Enable the calculation for the second CRC",
2630                  &global_calculate_crc2);
2631
2632     prefs_register_uint_preference(opensafety_module, "network_udp_port",
2633                 "Port used for Generic UDP",
2634                 "Port used by any UDP demo implementation to transport data", 10,
2635                 &global_network_udp_port);
2636     prefs_register_uint_preference(opensafety_module, "network_udp_port_sercosiii",
2637                 "Port used for SercosIII/UDP",
2638                 "UDP port used by SercosIII to transport data", 10,
2639                 &global_network_udp_port_sercosiii);
2640     prefs_register_bool_preference(opensafety_module, "network_udp_frame_first_sercosiii",
2641                 "openSAFETY frame 2 before frame 1 (SercosIII/UDP only)",
2642                 "In an SercosIII/UDP transport stream, openSAFETY frame 2 will be expected before frame 1",
2643                 &global_siii_udp_frame2_first );
2644     prefs_register_bool_preference(opensafety_module, "network_udp_frame_first",
2645                 "openSAFETY frame 2 before frame 1 (UDP only)",
2646                 "In the transport stream, openSAFETY frame 2 will be expected before frame 1",
2647                 &global_udp_frame2_first );
2648     prefs_register_bool_preference(opensafety_module, "mbtcp_big_endian",
2649                 "Big Endian Word Coding (Modbus/TCP only)",
2650                 "Modbus/TCP words can be transcoded either big- or little endian. Default will be little endian",
2651                 &global_mbtcp_big_endian);
2652
2653     prefs_register_bool_preference(opensafety_module, "enable_plk",
2654                 "Enable heuristic dissection for Ethernet POWERLINK", "Enable heuristic dissection for Ethernet POWERLINK",
2655                 &global_enable_plk);
2656     prefs_register_bool_preference(opensafety_module, "enable_udp",
2657                 "Enable heuristic dissection for openSAFETY over UDP encoded traffic", "Enable heuristic dissection for openSAFETY over UDP encoded traffic",
2658                 &global_enable_udp);
2659     prefs_register_bool_preference(opensafety_module, "enable_genudp",
2660                 "Enable heuristic dissection for generic UDP encoded traffic", "Enable heuristic dissection for generic UDP encoded traffic",
2661                 &global_enable_genudp);
2662     prefs_register_bool_preference(opensafety_module, "enable_siii",
2663                 "Enable heuristic dissection for SercosIII", "Enable heuristic dissection for SercosIII",
2664                 &global_enable_siii);
2665     prefs_register_bool_preference(opensafety_module, "enable_pnio",
2666                 "Enable heuristic dissection for Profinet IO", "Enable heuristic dissection for Profinet IO",
2667                 &global_enable_pnio);
2668     prefs_register_bool_preference(opensafety_module, "enable_mbtcp",
2669                 "Enable heuristic dissection for Modbus/TCP", "Enable heuristic dissection for Modbus/TCP",
2670                 &global_enable_mbtcp);
2671
2672     /* Registering default and ModBus/TCP dissector */
2673     new_register_dissector("opensafety_udpdata", dissect_opensafety_udpdata, proto_opensafety );
2674     new_register_dissector("opensafety_mbtcp", dissect_opensafety_mbtcp, proto_opensafety );
2675     new_register_dissector("opensafety_siii", dissect_opensafety_siii, proto_opensafety );
2676     new_register_dissector("opensafety_pnio", dissect_opensafety_pn_io, proto_opensafety);
2677 }
2678
2679 void
2680 proto_reg_handoff_opensafety(void)
2681 {
2682     static int opensafety_inited = FALSE;
2683
2684     if ( !opensafety_inited )
2685     {
2686         /* EPL & SercosIII dissector registration */
2687         heur_dissector_add("epl", dissect_opensafety_epl, proto_opensafety);
2688         heur_dissector_add("sercosiii", dissect_opensafety_siii, proto_opensafety);
2689
2690         /* If an openSAFETY UDP transport filter is present, add to its
2691          * heuristic filter list. Otherwise ignore the transport */
2692         if ( find_dissector("opensafety_udp") != NULL )
2693                 heur_dissector_add("opensafety_udp", dissect_opensafety_udpdata, proto_opensafety);
2694
2695         /* Modbus TCP dissector registration */
2696         dissector_add_string("modbus.data", "data", find_dissector("opensafety_mbtcp"));
2697
2698         /* For Profinet we have to register as a heuristic dissector, as Profinet
2699          *  is implemented as a plugin, and therefore the heuristic dissector is not
2700          *  added by the time this method is being called
2701          */
2702         if ( find_dissector("pn_io") != NULL )
2703         {
2704             heur_dissector_add("pn_io", dissect_opensafety_pn_io, proto_opensafety);
2705         }
2706         else
2707         {
2708             /* The native dissector cannot be loaded. so we add our protocol directly to
2709              * the ethernet subdissector list. No PNIO specific data will be dissected
2710              * and a warning will be displayed, recognizing the missing dissector plugin.
2711              */
2712             dissector_add_uint("ethertype", ETHERTYPE_PROFINET, find_dissector("opensafety_pnio"));
2713         }
2714
2715         register_init_routine ( setup_dissector );
2716
2717         /* registering frame end routine, to prevent a malformed dissection preventing
2718          * further dissector calls (see bug #6950) */
2719         /* register_frame_end_routine(reset_dissector); */
2720     }
2721
2722 }
2723
2724 /*
2725  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
2726  *
2727  * Local variables:
2728  * c-basic-offset: 8
2729  * tab-width: 8
2730  * indent-tabs-mode: t
2731  * End:
2732  *
2733  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
2734  * :indentSize=8:tabSize=8:noTabs=false:
2735  */