Don't do fcn calls in arg of g_?to??(); Macro may very well eval args multiple times.
[obnox/wireshark/wip.git] / epan / dissectors / packet-sbus.c
1 /* packet-sbus.c
2  * Routines for Ether-S-Bus dissection
3  * Copyright 2010, Christian Durrer <christian.durrer@sensemail.ch>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <glib.h>
31 #include <epan/packet.h>
32 #include <epan/conversation.h>
33 #include <epan/emem.h>
34 #include <epan/expert.h>
35
36 /* Attribute values*/
37 #define SBUS_REQUEST                   0x00
38 #define SBUS_RESPONSE                  0x01
39 #define SBUS_ACKNAK                    0x02
40
41 /*SBus command codes*/
42 #define SBUS_RD_COUNTER                0x00
43 #define SBUS_RD_DISPLAY_REGISTER       0x01
44 #define SBUS_RD_FLAG                   0x02
45 #define SBUS_RD_INPUT                  0x03
46 #define SBUS_RD_RTC                    0x04
47 #define SBUS_RD_OUTPUT                 0x05
48 #define SBUS_RD_REGISTER               0x06
49 #define SBUS_RD_TIMER                  0x07
50 #define SBUS_WR_COUNTER                0x0A
51 #define SBUS_WR_FLAG                   0x0B
52 #define SBUS_WR_RTC                    0x0C
53 #define SBUS_WR_OUTPUT                 0x0D
54 #define SBUS_WR_REGISTER               0x0E
55 #define SBUS_WR_TIMER                  0x0F
56 #define SBUS_RDWR_MULTI_MEDIAS         0x13
57 #define SBUS_RD_PCD_STATUS_CPU0        0x14
58 #define SBUS_RD_PCD_STATUS_CPU1        0x15
59 #define SBUS_RD_PCD_STATUS_CPU2        0x16
60 #define SBUS_RD_PCD_STATUS_CPU3        0x17
61 #define SBUS_RD_PCD_STATUS_CPU4        0x18
62 #define SBUS_RD_PCD_STATUS_CPU5        0x19
63 #define SBUS_RD_PCD_STATUS_CPU6        0x1A
64 #define SBUS_RD_PCD_STATUS_OWN         0x1B
65 #define SBUS_RD_SBUS_STN_NBR           0x1D
66 #define SBUS_RD_USER_MEMORY            0x1E
67 #define SBUS_RD_PROGRAM_LINE           0x1F
68 #define SBUS_RD_PROGRAM_VERSION        0x20
69 #define SBUS_RD_TEXT                   0x21
70 #define SBUS_RD_ACTIVE_TRANSITION      0x22
71 #define SBUS_WR_USER_MEMORY            0x23
72 #define SBUS_WR_PROGRAM_LINE           0x24
73 #define SBUS_WR_TEXT                   0x25
74 #define SBUS_RUN_PROCEDURE_CPU0        0x28
75 #define SBUS_RUN_PROCEDURE_CPU1        0x29
76 #define SBUS_RUN_PROCEDURE_CPU2        0x2A
77 #define SBUS_RUN_PROCEDURE_CPU3        0x2B
78 #define SBUS_RUN_PROCEDURE_CPU4        0x2C
79 #define SBUS_RUN_PROCEDURE_CPU5        0x2D
80 #define SBUS_RUN_PROCEDURE_CPU6        0x2E
81 #define SBUS_RUN_PROCEDURE_OWN         0x2F
82 #define SBUS_RUN_PROCEDURE_ALL         0x30
83 #define SBUS_RESTART_COLD_CPU1         0x32
84 #define SBUS_RESTART_COLD_CPU2         0x33
85 #define SBUS_RESTART_COLD_CPU3         0x34
86 #define SBUS_RESTART_COLD_CPU4         0x35
87 #define SBUS_RESTART_COLD_CPU5         0x36
88 #define SBUS_RESTART_COLD_CPU6         0x37
89 #define SBUS_RESTART_COLD_OWN          0x38
90 #define SBUS_RESTART_COLD_ALL          0x39
91 #define SBUS_STOP_PROCEDURE_CPU0       0x3C
92 #define SBUS_STOP_PROCEDURE_CPU1       0x3D
93 #define SBUS_STOP_PROCEDURE_CPU2       0x3E
94 #define SBUS_STOP_PROCEDURE_CPU3       0x3F
95 #define SBUS_STOP_PROCEDURE_CPU4       0x40
96 #define SBUS_STOP_PROCEDURE_CPU5       0x41
97 #define SBUS_STOP_PROCEDURE_CPU6       0x42
98 #define SBUS_STOP_PROCEDURE_OWN        0x43
99 #define SBUS_STOP_PROCEDURE_ALL        0x44
100 #define SBUS_RD_STATUSFLAG_ACCU        0x46
101 #define SBUS_RD_BYTE                   0x47
102 #define SBUS_RD_HALT_FAILURE_REG       0x48
103 #define SBUS_RD_INDEX_REGISTER         0x49
104 #define SBUS_RD_INSTRUCTION_POINTER    0x4A
105 #define SBUS_FIND_HISTORY              0x4B
106 #define SBUS_WR_STATUSFLAG_ACCU        0x50
107 #define SBUS_WR_BYTE                   0x51
108 #define SBUS_WR_INDEX_REGISTER         0x52
109 #define SBUS_WR_INSTRUCTION_POINTER    0x53
110 #define SBUS_CLEAR_ALL                 0x5A
111 #define SBUS_CLEAR_FLAGS               0x5B
112 #define SBUS_CLEAR_OUTPUTS             0x5C
113 #define SBUS_CLEAR_REGISTERS           0x5D
114 #define SBUS_CLEAR_TIMERS              0x5E
115 #define SBUS_RESTART_WARM_CPU1         0x64
116 #define SBUS_RESTART_WARM_CPU2         0x65
117 #define SBUS_RESTART_WARM_CPU3         0x66
118 #define SBUS_RESTART_WARM_CPU4         0x67
119 #define SBUS_RESTART_WARM_CPU5         0x68
120 #define SBUS_RESTART_WARM_CPU6         0x69
121 #define SBUS_RESTART_WARM_OWN          0x6A
122 #define SBUS_RESTART_WARM_ALL          0x6B
123 #define SBUS_CHANGE_BLOCK              0x6E
124 #define SBUS_CLEAR_HISTORY_FAILURE     0x6F
125 #define SBUS_DELETE_PROGRAM_LINE       0x70
126 #define SBUS_GO_CONDITIONAL            0x71
127 #define SBUS_INSERT_PROGRAM_LINE       0x72
128 #define SBUS_LOCAL_CYCLE               0x73
129 #define SBUS_ALL_CYCLES                0x74
130 #define SBUS_MAKE_TEXT                 0x75
131 #define SBUS_EXECUTE_SINGLE_INSTR      0x76
132 #define SBUS_SINGLE_STEP               0x77
133 #define SBUS_XOB_17_INTERRUPT          0x82
134 #define SBUS_XOB_18_INTERRUPT          0x83
135 #define SBUS_XOB_19_INTERRUPT          0x84
136 #define SBUS_RD_HANGUP_TIMEOUT         0x91
137 #define SBUS_RD_DATA_BLOCK             0x96
138 #define SBUS_WR_DATA_BLOCK             0x97
139 #define SBUS_MAKE_DATA_BLOCK           0x98
140 #define SBUS_CLEAR_DATA_BLOCK          0x99
141 #define SBUS_CLEAR_TEXT                0x9A
142 #define SBUS_RD_BLOCK_ADDRESSES        0x9B
143 #define SBUS_RD_BLOCK_SIZES            0x9C
144 #define SBUS_RD_CURRENT_BLOCK          0x9D
145 #define SBUS_RD_CALL_STACK             0x9E
146 #define SBUS_RD_DBX                    0x9F
147 #define SBUS_RD_USER_EEPROM_REGISTER   0xA1
148 #define SBUS_WR_USER_EEPROM_REGISTER   0xA3
149 #define SBUS_ERASE_FLASH               0xA5
150 #define SBUS_RESTART_COLD_FLAG         0xA6
151 #define SBUS_WR_SYSTEM_BUFFER          0xA7
152 #define SBUS_RD_SYSTEM_BUFFER          0xA8
153 #define SBUS_RD_WR_PCD_BLOCK           0xA9
154 #define SBUS_GET_DIAGNOSTIC            0xAA
155 #define SBUS_RD_SYSTEM_INFORMATION     0xAB
156 #define SBUS_CHANGE_BLOCKS_ON_RUN      0xAC
157 #define SBUS_FLASHCARD_TELEGRAM        0xAD
158 #define SBUS_DOWNLOAD_FIRMWARE         0xAE
159 #define SBUS_WEB_SERVER_SERIAL_COMM    0xAF
160
161 /* Bitfield in the arithmetic flags and accu*/
162 #define F_ACCU      (1<<0)           /* Accumulator of PCD              */
163 #define F_ERROR     (1<<1)           /* Error flag of PCD               */
164 #define F_NEGATIVE  (1<<2)           /* Negative arithmetic status flag */
165 #define F_ZERO      (1<<3)           /* Zero arithmetic status flag     */
166
167 /* Bitfield in the system information*/
168 /*#define F_EMPTY      (1<<0)          always 0                         */
169 #define F_MEMSIZE      (1<<1)        /* Memory size information         */
170 #define F_TRACE        (1<<2)        /* Trace buffer feature            */
171 #define F_INFO_B1      (1<<3)        /* EEPROM information of slot B1   */
172 #define F_INFO_B2      (1<<4)        /* EEPROM information of slot B2   */
173 #define F_PGU_BAUD (1<<5)            /* PGU baudrate can be switched    */
174
175
176 /* Read/write block command codes*/
177 #define SBUS_WR_START_OF_STREAM        0x00
178 #define SBUS_WR_BLOCK_DATA_STREAM      0x01
179 #define SBUS_WR_BLOCK_END_OF_STREAM    0x02
180 #define SBUS_WR_ABORT_BLOCK_STREAM     0x07
181 #define SBUS_WR_BLOCK_DATA_BYTES       0x08
182 #define SBUS_RD_BLOCK_START_OF_STREAM  0X10
183 #define SBUS_RD_BLOCK_DATA_STREAM      0x11
184 #define SBUS_RD_ABORT_BLOCK_STREAM     0x17
185 #define SBUS_RD_BLOCK_DATA_BYTES       0x18
186 #define SBUS_DELETE_BLOCK              0x20
187 #define SBUS_GET_BLOCK_SIZE            0x21
188 #define SBUS_GET_PROGRAM_BLOCK_LIST    0x22
189
190 /* Read/write block types*/
191 #define SBUS_RD_WR_CONFIGURATION_FILE  0x20
192 #define SBUS_RD_WR_PROGRAM_BLOCK_FILE  0x21
193 #define SBUS_RD_WR_UNKNOWN_BLOCK_TYPE  0x83
194
195 /* Read/write block error codes*/
196 #define SBUS_RD_WR_NAK                 0x80
197 #define SBUS_RD_WR_NAK_INVALID_SIZE    0x8A
198
199 /* Initialize the protocol and registered fields */
200 static int proto_sbus = -1;
201 static int hf_sbus_length = -1;
202 static int hf_sbus_version = -1;
203 static int hf_sbus_protocol = -1;
204 static int hf_sbus_sequence = -1;
205 static int hf_sbus_attribut = -1;
206 static int hf_sbus_dest = -1;
207 static int hf_sbus_address = -1;
208 static int hf_sbus_command = -1;
209 static int hf_sbus_command_extension = -1;
210 static int hf_sbus_rcount = -1;
211 static int hf_sbus_wcount = -1;
212 static int hf_sbus_wcount_calculated = -1;
213 static int hf_sbus_fio_count = -1;
214 static int hf_sbus_addr_rtc = -1;
215 static int hf_sbus_addr_iof = -1;
216 static int hf_sbus_addr_eeprom = -1;
217 static int hf_sbus_addr_prog = -1;
218 static int hf_sbus_addr_68k = -1;
219 static int hf_sbus_block_type = -1;
220 static int hf_sbus_block_nr = -1;
221 static int hf_sbus_nbr_elements = -1;
222 static int hf_sbus_display_register = -1;
223 static int hf_sbus_data_rtc = -1;
224 static int hf_sbus_data_byte = -1;
225 static int hf_sbus_data_byte_hex = -1;
226 static int hf_sbus_data_iof = -1;
227 static int hf_sbus_cpu_type = -1;
228 static int hf_sbus_fw_version = -1;
229 static int hf_sbus_sysinfo_nr = -1;
230 static int hf_sbus_sysinfo0_1 = -1;
231 static int hf_sbus_sysinfo0_2 = -1;
232 static int hf_sbus_sysinfo0_3 = -1;
233 static int hf_sbus_sysinfo0_4 = -1;
234 static int hf_sbus_sysinfo0_5 = -1;
235 static int hf_sbus_sysinfo_length = -1;
236 static int hf_sbus_f_module_type = -1;
237 static int hf_sbus_harware_version = -1;
238 static int hf_sbus_hardware_modification = -1;
239 static int hf_sbus_various = -1;
240 static int hf_sbus_acknackcode = -1;
241 static int hf_sbus_cpu_status = -1;
242 static int hf_sbus_week_day = -1;
243 static int hf_sbus_date = -1;
244 static int hf_sbus_time = -1;
245 static int hf_sbus_crc = -1;
246 static int hf_sbus_crc_bad = -1;
247 static int hf_sbus_retry = -1;
248 static int hf_sbus_flags_accu = -1;
249 static int hf_sbus_flags_error = -1;
250 static int hf_sbus_flags_negative = -1;
251 static int hf_sbus_flags_zero = -1;
252 /* Web server telegram */
253 static int hf_sbus_web_size = -1;
254 static int hf_sbus_web_aid = -1;
255 static int hf_sbus_web_seq = -1;
256 /* Read/Write block telegram*/
257 static int hf_sbus_rdwr_block_length = -1;
258 static int hf_sbus_rdwr_block_length_ext = -1;
259 static int hf_sbus_rdwr_telegram_type = -1;
260 static int hf_sbus_rdwr_telegram_sequence = -1;
261 static int hf_sbus_rdwr_block_size = -1;
262 static int hf_sbus_rdwr_block_addr = -1;
263 static int hf_sbus_rdwr_file_name = -1;
264 static int hf_sbus_rdwr_list_type = -1;
265 static int hf_sbus_rdwr_acknakcode = -1;
266 /* Request-Response tracking */
267 static int hf_sbus_response_in = -1;
268 static int hf_sbus_response_to = -1;
269 static int hf_sbus_response_time = -1;
270 static int hf_sbus_timeout = -1;
271 static int hf_sbus_request_in = -1;
272
273 /* Initialize the subtree pointers */
274 static gint ett_sbus = -1;
275 static gint ett_sbus_ether = -1;
276 static gint ett_sbus_data = -1;
277
278 /* True/False strings*/
279 static const true_false_string tfs_sbus_flags= {
280        "Is high",
281        "Is low"
282 };
283
284 static const true_false_string tfs_sbus_present= {
285        "Is present",
286        "Is not present"
287 };
288
289 /* value to string definitions*/
290 /* telegram types*/
291 static const value_string sbus_att_vals[] = {
292        {0, "Request"},
293        {1, "Response"},
294        {2, "ACK/NAK"},
295        {0, NULL}
296 };
297 /* Block types*/
298 static const value_string sbus_block_types[] = {
299        {0x00, "COB"},                        /* Cyclic organization block */
300        {0x01, "XOB"},                        /* Exception organization block */
301        {0x02, "PB"},                         /* Program block */
302        {0x03, "FB"},                         /* Function block */
303        {0x04, "ST"},                         /* Step of Graftec structure*/
304        {0x05, "TR"},                         /* Transition of Graftec structure*/
305        {0x04, "TEXT"},                       /* Text*/
306        {0x05, "DB"},                         /* Data Block*/
307        {0x08, "SB"},                         /* Sequential Block (Graftec)*/
308        {0x09, "DBX"},                        /* Special Data Block*/
309        {0x10, "BACnet"},                     /* BACnet configuration block */
310        {0x11, "CANopen"},                    /* CANopen configuration */
311        {0x12, "LONIP"},                      /* LONIP configuration */
312        {0x20, "Configuration file"},         /* LONIP configuration */
313        {0x21, "Program block file"},         /* LONIP configuration */
314        {0xFE, "All configuration blocks"},   /* all configuration blocks (delete blocks only) */
315        {0xFF, "All blocks"},                 /* all blocks (incl. program blocks) (delete blocks only) */
316        {0, NULL}
317 };
318 /* ACK NAK values*/
319 static const value_string sbus_CPU_status[] = {
320        {0x43, "C"},
321        {0x44, "D"},
322        {0x48, "Halt"},
323        {0x52, "Run"},
324        {0x53, "Stop"},
325        {0x58, "X, Exceptional Intermediate Status (MODEMS+)"},
326        {0, NULL}
327 };
328 /* CPU status*/
329 static const value_string sbus_ack_nak_vals[] = {
330        {0, "ACK (Acknowledged)"},
331        {1, "NAK, no reason specified"},
332        {2, "NAK, because of password"},
333        {3, "NAK, PGU port is in reduced protocol"},
334        {4, "NAK, PGU port is already used"},
335        {0, NULL}
336 };
337 /* S-Bus commands*/
338 static const value_string sbus_command_vals[] = {
339        {0x00, "Read counter(s)"},
340        {0x01, "Read display register"},
341        {0x02, "Read flag(s)"},
342        {0x03, "Read input(s)"},
343        {0x04, "Read real time clock"},
344        {0x05, "Read output(s)"},
345        {0x06, "Read register(s)"},
346        {0x07, "Read timer(s)"},
347        {0x0A, "Write counter(s)"},
348        {0x0B, "Write flag(s)"},
349        {0x0C, "Write real time clock"},
350        {0x0D, "Write output(s)"},
351        {0x0E, "Write register(s)"},
352        {0x0F, "Write timer(s)"},
353        {0x14, "Read PCD status, CPU 0"},
354        {0x15, "Read PCD status, CPU 1"},
355        {0x16, "Read PCD status, CPU 2"},
356        {0x17, "Read PCD status, CPU 3"},
357        {0x18, "Read PCD status, CPU 4"},
358        {0x19, "Read PCD status, CPU 5"},
359        {0x1A, "Read PCD status, CPU 6"},
360        {0x1B, "Read PCD status (own)"},
361        {0x1D, "Read S-Bus station number"},
362        {0x1E, "Read user memory*"},
363        {0x1F, "Read program line*"},
364        {0x20, "Read firmware version"},
365        {0x21, "Read text*"},
366        {0x22, "Read active transition*"},
367        {0x23, "Write user memory*"},
368        {0x24, "Write program line*"},
369        {0x25, "Write text*"},
370        {0x28, "Run procedure*, CPU 0"},
371        {0x29, "Run procedure*, CPU 1"},
372        {0x2A, "Run procedure*, CPU 2"},
373        {0x2B, "Run procedure*, CPU 3"},
374        {0x2C, "Run procedure*, CPU 4"},
375        {0x2D, "Run procedure*, CPU 5"},
376        {0x2E, "Run procedure*, CPU 6"},
377        {0x2F, "Run procedure* (own CPU)"},
378        {0x30, "Run procedure* (All CPUs)"},
379        {0x32, "Restart cold CPU 1*"},
380        {0x33, "Restart cold CPU 2*"},
381        {0x34, "Restart cold CPU 3*"},
382        {0x35, "Restart cold CPU 4*"},
383        {0x36, "Restart cold CPU 5*"},
384        {0x37, "Restart cold CPU 6*"},
385        {0x38, "Restart cold own CPU*"},
386        {0x39, "Restart cold all CPUs*"},
387        {0x3C, "Stop procedure*, CPU 0"},
388        {0x3D, "Stop procedure*, CPU 1"},
389        {0x3E, "Stop procedure*, CPU 2"},
390        {0x3F, "Stop procedure*, CPU 3"},
391        {0x40, "Stop procedure*, CPU 4"},
392        {0x41, "Stop procedure*, CPU 5"},
393        {0x42, "Stop procedure*, CPU 6"},
394        {0x43, "Stop procedure*, (own CPU)"},
395        {0x44, "Stop procedure*, (All CPUs)"},
396        {0x46, "Read arithmetic status and ACCU*"},
397        {0x47, "Read byte"},
398        {0x48, "Read halt failure register*"},
399        {0x49, "Read index register*"},
400        {0x4A, "Read instruction pointer*"},
401        {0x4B, "Find history*"},
402        {0x50, "Write arithmetic staus and ACCU*"},
403        {0x51, "Write byte*"},
404        {0x52, "Write index register"},
405        {0x53, "Write instruction pointer*"},
406        {0x5A, "Clear all (F, O, R, T)*"},
407        {0x5B, "Clear flags*"},
408        {0x5C, "Clear outputs*"},
409        {0x5D, "Clear registers*"},
410        {0x5E, "Clear timers*"},
411        {0x64, "Restart warm CPU 1*"},
412        {0x65, "Restart warm CPU 2*"},
413        {0x66, "Restart warm CPU 3*"},
414        {0x67, "Restart warm CPU 4*"},
415        {0x68, "Restart warm CPU 5*"},
416        {0x69, "Restart warm CPU 6*"},
417        {0x6A, "Restart warm (own CPU)*"},
418        {0x6B, "Restart warm (All CPUs)*"},
419        {0x6E, "Change block*"},
420        {0x6F, "Clear history failure*"},
421        {0x70, "Delete program line*"},
422        {0x71, "Go conditional*"},
423        {0x72, "Insert program line*"},
424        {0x73, "Local cycles*"},
425        {0x74, "All cycles*"},
426        {0x75, "Make text*"},
427        {0x76, "Execute single instruction*"},
428        {0x77, "Single step*"},
429        {0x82, "XOB 17 interrupt"},
430        {0x83, "XOB 18 interrupt"},
431        {0x84, "XOB 19 interrupt"},
432        {0x91, "Read hangup timeout"},
433        {0x96, "Read data block"},
434        {0x97, "Write data block"},
435        {0x98, "Make data block*"},
436        {0x99, "Clear data block*"},
437        {0x9A, "Clear text*"},
438        {0x9B, "Read block address"},
439        {0x9C, "Read block sizes"},
440        {0x9D, "Read current block*"},
441        {0x9E, "Read call stack*"},
442        {0x9F, "Read DBX"},
443        {0xA1, "Read user EEPROM register"},
444        {0xA3, "Write user EEPROM register"},
445        {0xA5, "Erase flash*"},
446        {0xA6, "Restart cold flag*"},
447        {0xA7, "Write system buffer"},
448        {0xA8, "Read system buffer"},
449        {0xA9, "Read/write block data*"},
450        {0xAA, "Get diagnostic*"},
451        {0xAB, "Read system information*"},
452        {0xAC, "Changes blocks on run*"},
453        {0xAD, "Flashcard telegram*"},
454        {0xAE, "Download FW*"},
455        {0xAF, "Web server serial communication*"},
456        {0, NULL}
457 };
458
459 static const value_string webserver_aid_vals[] = {
460        {0x01, "Partial request"},
461        {0x02, "Request end"},
462        {0x07, "Get Data"},
463        {0x10, "Transfer OK"},
464        {0x11, "Partial answer"},
465        {0x12, "Last part of answer"},
466        {0x13, "Server not ready"},
467        {0, NULL}
468 };
469 static const value_string rdwrblock_vals[] = {
470        {0x00, "WR block start of stream"},
471        {0x01, "WR block data stream"},
472        {0x02, "WR block end of stream"},
473        {0x07, "Abort block WR stream"},
474        {0x08, "WR block data"},
475        {0x10, "RD block start of stream"},
476        {0x11, "RD block data stream"},
477        {0x17, "Abort block RD stream"},
478        {0x18, "RD block data"},
479        {0x20, "Delete block"},
480        {0x21, "Get block size"},
481        {0x22, "Get program block list"},
482        {0, NULL}
483 };
484
485 static const value_string rdwrblock_sts[] = {
486        {0x00, "ACK (Acknowledged)"},
487        {0x01, "Data"},
488        {0x02, "Busy"},
489        {0x03, "End of stream"},
490        {0x04, "Data EOF reached"},
491        {0x80, "NAK"},
492        {0x81, "NAK, unknown Tlg_Type"},
493        {0x82, "NAK, not supported  Tlg_Type"},
494        {0x83, "NAK, unknown Block Type"},
495        {0x84, "NAK, out of sequence"},
496        {0x85, "NAK, not supported Block number"},
497        {0x86, "NAK, Block Size invalid (to big)"},
498        {0x87, "NAK, Block Address invalid"},
499        {0x88, "NAK, CRC invalid"},
500        {0x89, "NAK, invalid status"},
501        {0x8A, "NAK, invalid command size (w-count)"},
502        {0xFF, "Abort (stream)"},
503        {0, NULL}
504 };
505
506 static const value_string rdwrblock_list_type_vals[] = {
507        {0x40, "Start request of program block"},
508        {0x41, "Get next program block"},
509        {0xFF, "Abort get list"},
510        {0, NULL}
511 };
512
513 static const guint crc_table[] = {
514        0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef,
515        0x1231,0x0210,0x3273,0x2252,0x52b5,0x4294,0x72f7,0x62d6,0x9339,0x8318,0xb37b,0xa35a,0xd3bd,0xc39c,0xf3ff,0xe3de,
516        0x2462,0x3443,0x0420,0x1401,0x64e6,0x74c7,0x44a4,0x5485,0xa56a,0xb54b,0x8528,0x9509,0xe5ee,0xf5cf,0xc5ac,0xd58d,
517        0x3653,0x2672,0x1611,0x0630,0x76d7,0x66f6,0x5695,0x46b4,0xb75b,0xa77a,0x9719,0x8738,0xf7df,0xe7fe,0xd79d,0xc7bc,
518        0x48c4,0x58e5,0x6886,0x78a7,0x0840,0x1861,0x2802,0x3823,0xc9cc,0xd9ed,0xe98e,0xf9af,0x8948,0x9969,0xa90a,0xb92b,
519        0x5af5,0x4ad4,0x7ab7,0x6a96,0x1a71,0x0a50,0x3a33,0x2a12,0xdbfd,0xcbdc,0xfbbf,0xeb9e,0x9b79,0x8b58,0xbb3b,0xab1a,
520        0x6ca6,0x7c87,0x4ce4,0x5cc5,0x2c22,0x3c03,0x0c60,0x1c41,0xedae,0xfd8f,0xcdec,0xddcd,0xad2a,0xbd0b,0x8d68,0x9d49,
521        0x7e97,0x6eb6,0x5ed5,0x4ef4,0x3e13,0x2e32,0x1e51,0x0e70,0xff9f,0xefbe,0xdfdd,0xcffc,0xbf1b,0xaf3a,0x9f59,0x8f78,
522        0x9188,0x81a9,0xb1ca,0xa1eb,0xd10c,0xc12d,0xf14e,0xe16f,0x1080,0x00a1,0x30c2,0x20e3,0x5004,0x4025,0x7046,0x6067,
523        0x83b9,0x9398,0xa3fb,0xb3da,0xc33d,0xd31c,0xe37f,0xf35e,0x02b1,0x1290,0x22f3,0x32d2,0x4235,0x5214,0x6277,0x7256,
524        0xb5ea,0xa5cb,0x95a8,0x8589,0xf56e,0xe54f,0xd52c,0xc50d,0x34e2,0x24c3,0x14a0,0x0481,0x7466,0x6447,0x5424,0x4405,
525        0xa7db,0xb7fa,0x8799,0x97b8,0xe75f,0xf77e,0xc71d,0xd73c,0x26d3,0x36f2,0x0691,0x16b0,0x6657,0x7676,0x4615,0x5634,
526        0xd94c,0xc96d,0xf90e,0xe92f,0x99c8,0x89e9,0xb98a,0xa9ab,0x5844,0x4865,0x7806,0x6827,0x18c0,0x08e1,0x3882,0x28a3,
527        0xcb7d,0xdb5c,0xeb3f,0xfb1e,0x8bf9,0x9bd8,0xabbb,0xbb9a,0x4a75,0x5a54,0x6a37,0x7a16,0x0af1,0x1ad0,0x2ab3,0x3a92,
528        0xfd2e,0xed0f,0xdd6c,0xcd4d,0xbdaa,0xad8b,0x9de8,0x8dc9,0x7c26,0x6c07,0x5c64,0x4c45,0x3ca2,0x2c83,0x1ce0,0x0cc1,
529        0xef1f,0xff3e,0xcf5d,0xdf7c,0xaf9b,0xbfba,0x8fd9,0x9ff8,0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,0x3eb2,0x0ed1,0x1ef0
530 };
531
532 /* Conversion values passing structure*/
533 typedef struct {
534        guint32 conversation;  /*Conversation ID*/
535        guint16 sequence;      /*Sequence number of request telegram*/
536 } sbus_request_key;
537
538 typedef struct {
539        guint8 cmd_code;       /*command code from request*/
540        guint8 count;          /*rcount value*/
541        guint8 sysinfo;        /*system information number*/
542        guint8 block_tlg;      /*telegram type of RD/WR block telegrams*/
543        guint8 retry_count;    /*number of retries*/
544        guint32 req_frame;     /*frame number of last request*/
545        guint32 resp_frame;    /*frame number of response*/
546        nstime_t req_time;     /*time of the last request*/
547 } sbus_request_val;
548
549 /* The hash structure (for conversations)*/
550 static GHashTable *sbus_request_hash = NULL;
551
552 static guint crc_calc (guint crc, guint val)
553 {
554        int index;
555        guint ncrc;
556
557        index = (((crc >> 8) ^ val) & 0xff);
558        ncrc = crc_table[index] ^ ((crc << 8) & 0xffff);
559
560        return ncrc;
561 }
562
563 /* Hash functions*/
564 static gint sbus_equal(gconstpointer v, gconstpointer w)
565 {
566        sbus_request_key *v1 = (sbus_request_key *)v;
567        sbus_request_key *v2 = (sbus_request_key *)w;
568
569        if (v1->conversation == v2->conversation &&
570            v1->sequence == v2->sequence) {
571               return 1;
572        }
573        return 0;
574 }
575
576 static guint sbus_hash(gconstpointer v)
577 {
578        sbus_request_key *key = (sbus_request_key *)v;
579        guint val;
580        val = key->conversation + key->sequence;
581        return val;
582 }
583
584 /*Protocol initialisation*/
585 static void sbus_init_protocol(void){
586        if (sbus_request_hash){
587               g_hash_table_destroy(sbus_request_hash);
588        }
589        sbus_request_hash = g_hash_table_new(sbus_hash, sbus_equal);
590 }
591
592 /* check whether the packet looks like SBUS or not */
593 static gboolean
594 is_sbus_pdu(tvbuff_t *tvb)
595 {
596        guint32 length;
597
598        /* we need at least 8 bytes to determine whether this is sbus or
599           not*/
600        if(tvb_length(tvb)<8){
601               return FALSE;
602        }
603
604        /* the length must be >= 8 bytes to accomodate the header,
605           it also must be <65536 to fit inside a udp packet
606        */
607        length=tvb_get_ntohl(tvb, 0);
608        if ( (length<8) || (length>65535) ) {
609               return FALSE;
610        }
611        if (tvb_reported_length(tvb) != length) {
612               return FALSE;
613        }
614        /* First four byte indicate the length which must be at least 12 bytes*/
615        if (tvb_get_ntohl(tvb, 0) < 12) {
616               return (FALSE);
617        }
618        /* Fifth byte indicates protocol version which can be 0 or 1*/
619        if (tvb_get_guint8(tvb, 4) > 0x01) {
620               return (FALSE);
621        }
622        /* Sixth byte indicates protocol type and must be 0*/
623        if ( tvb_get_guint8(tvb, 5) > 0x01 ) {
624               return (FALSE);
625        }
626        /* Seventh and eigth byte indicates the packet sequence number and can
627           be 0 to 65565 (--> check does not make sense)*/
628        /* Ninth byte the "attributes character" and must be either 0, 1 or 2
629           (request, response or ACK/NAK)*/
630        if (tvb_get_guint8(tvb, 8) > 0x02 ) {
631               return (FALSE);
632        }
633        return TRUE;
634 }
635
636 /*Dissect the telegram*/
637 static int
638 dissect_sbus(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
639 {
640
641 /* Set up structures needed to add the protocol subtree and manage it */
642        proto_item *ti, *et, *dt, *hi, *cs;
643        proto_tree *sbus_tree, *ethsbus_tree, *sbusdata_tree;
644
645        gint i;        /*for CRC calculation*/
646        gint j;        /*for CRC calculation*/
647        gint offset;
648        gint sbus_eth_len;
649        guint sbus_crc_calc;
650        guint8 sbus_attribut;
651        guint8 sbus_media_cnt;
652        guint8 sbus_fio_cnt;
653        guint8 sbus_cmd_code;
654        guint8 sbus_web_size;
655        guint8 sbus_web_aid;
656        guint8 sbus_web_seq;
657        guint8 sbus_rdwr_type;
658        guint8 sbus_rdwr_sequence;
659        guint8 sbus_rdwr_block_tlg;
660        guint8 sbus_rdwr_block_type;
661        guint8 sbus_rdwr_ack_nak;
662        guint8 sbus_quint8_helper0;
663        guint32 sbus_binarymasked;
664        guint32 sbus_binaries;
665        guint16 sbus_ack_code;
666        guint32 sbus_show_bin;
667        guint32 sbus_rdwr_length;
668        guint32 sbus_helper;
669        guint32 sbus_helper1;
670        guint32 sbus_helper2;
671        char *tmp_string;
672        nstime_t ns; /*we use this for the response time*/
673
674 /* Set up conversations*/
675        conversation_t *conversation = NULL;
676        sbus_request_key request_key, *new_request_key;
677        sbus_request_val *request_val = NULL;
678
679        /* does this look like an sbus pdu? */
680        if(!is_sbus_pdu(tvb)){
681            return 0;
682        }
683
684        conversation = find_or_create_conversation(pinfo);
685
686        request_key.conversation = conversation->index;
687        request_key.sequence = tvb_get_ntohs(tvb,6);
688
689        request_val = (sbus_request_val *) g_hash_table_lookup(sbus_request_hash,
690                             &request_key);
691        /*Get type of telegram for finding retries
692         *As we are storing the info in a hash table we need to update the info
693         *also in case this is no retry*/
694        sbus_attribut = tvb_get_guint8(tvb,8);
695        if (request_val && sbus_attribut == SBUS_REQUEST) {
696               if (request_val->req_frame < pinfo->fd->num){ /*a retry; req_frame smaller this frame*/
697                      request_val->retry_count +=1;
698               }
699               else { /*we have a conversation but this is not a retry so we store the packet info*/
700                      request_val->retry_count = 0;
701                      request_val->req_frame = pinfo->fd->num; /*store actual frame nr.*/
702                      request_val->req_time = pinfo->fd->abs_ts;
703               }
704        }
705        if (request_val && (sbus_attribut == SBUS_RESPONSE ||
706                                       sbus_attribut == SBUS_ACKNAK)) { /*a response*/
707             request_val->resp_frame = pinfo->fd->num; /*so store this frame nr.*/
708        }
709        /* Only allocate a new hash element when it's a request*/
710        sbus_attribut = tvb_get_guint8(tvb,8);
711
712        if ( !request_val && sbus_attribut == 0 ) {/* request telegram */
713               new_request_key = se_alloc(sizeof(sbus_request_key));
714               *new_request_key = request_key;
715
716               request_val = se_alloc(sizeof(sbus_request_val));
717               request_val->cmd_code=tvb_get_guint8(tvb,10);
718               request_val->retry_count=0;
719               request_val->req_frame = pinfo->fd->num; /*store actual frame nr.*/
720               request_val->req_time = pinfo->fd->abs_ts;
721               request_val->resp_frame = 0; /*response frame is not known yet*/
722
723               if (((request_val->cmd_code) == SBUS_RD_USER_EEPROM_REGISTER) ||
724                   ((request_val->cmd_code) == SBUS_WR_USER_EEPROM_REGISTER)) {
725                      request_val->count=((tvb_get_guint8(tvb,12))+1);
726               } else {
727                      request_val->count=((tvb_get_guint8(tvb,11))+1);
728               }
729
730               /*Enter system info or telegram type (for rd/wr block telegrams)*/
731               if ((request_val->cmd_code) == SBUS_RD_SYSTEM_INFORMATION) {
732                      request_val->sysinfo=(tvb_get_guint8(tvb,12));
733                      request_val->block_tlg=0x0;
734               } else if ((request_val->cmd_code) == SBUS_RD_WR_PCD_BLOCK) {
735                      request_val->sysinfo=0x0;
736                      request_val->block_tlg=(tvb_get_guint8(tvb,12));
737               } else {
738                      request_val->sysinfo=0x0;
739                      request_val->block_tlg=0x0;
740               }
741
742               g_hash_table_insert(sbus_request_hash, new_request_key, request_val);
743        }
744 /* End of attaching data to hash table*/
745
746 /* Make entries in Protocol column and Info column on summary display */
747        col_set_str(pinfo->cinfo, COL_PROTOCOL, "S-Bus");
748
749        col_clear(pinfo->cinfo, COL_INFO);
750        offset = 0;
751
752        if (check_col(pinfo->cinfo, COL_INFO)) {
753               switch (sbus_attribut){
754                      case SBUS_REQUEST:
755                             sbus_cmd_code = tvb_get_guint8(tvb,10);
756                             switch (sbus_cmd_code){
757                                    case SBUS_WEB_SERVER_SERIAL_COMM:
758                                           /* Special treatment of web server request
759                                            * as is is very helpful to see more information in the packetlist */
760                                           sbus_web_aid = tvb_get_guint8(tvb,12);
761                                           sbus_web_seq = tvb_get_guint8(tvb,13);
762                                           col_add_fstr(pinfo->cinfo, COL_INFO,
763                                                        "Web Server Request: %s (Seq No: %d)",
764                                                        val_to_str(sbus_web_aid,
765                                                                   webserver_aid_vals, "Unknown Request!"),
766                                                        sbus_web_seq);
767                                           break;
768                                    case SBUS_RD_WR_PCD_BLOCK:
769                                           sbus_rdwr_type = tvb_get_guint8(tvb, 12);
770                                           col_add_fstr( pinfo->cinfo, COL_INFO,
771                                                         "Request:  %s", val_to_str( sbus_rdwr_type, rdwrblock_vals,
772                                                                                     "This RD/WR block telegram is not implemented"));
773                                           /* Add name of file to be written in case of start of file stream */
774                                           if (sbus_rdwr_type == SBUS_WR_START_OF_STREAM) {
775                                                  sbus_rdwr_block_type = tvb_get_guint8(tvb, 14);
776                                                  if ((sbus_rdwr_block_type == SBUS_RD_WR_CONFIGURATION_FILE) ||
777                                                      (sbus_rdwr_block_type == SBUS_RD_WR_PROGRAM_BLOCK_FILE)) {
778                                                         sbus_quint8_helper0=0;
779                                                         for (i=19; i<43; i++) { /*max length is 24 chars*/
780                                                                /*find zero-termination of string*/
781                                                                if ((tvb_get_guint8(tvb, i)) == 0x00) {
782                                                                       break;
783                                                                }
784                                                                sbus_quint8_helper0 += 1;
785                                                         }
786                                                         tmp_string = tvb_get_ephemeral_string(tvb , 19,
787                                                                                               sbus_quint8_helper0);
788                                                         col_append_fstr(pinfo->cinfo, COL_INFO,
789                                                                         ": (File: %s)", tmp_string);
790                                                  }
791                                           } else if (sbus_rdwr_type == SBUS_RD_BLOCK_START_OF_STREAM) {
792                                                  sbus_rdwr_block_type = tvb_get_guint8(tvb, 14);
793                                                  if ((sbus_rdwr_block_type == SBUS_RD_WR_CONFIGURATION_FILE) ||
794                                                      (sbus_rdwr_block_type == SBUS_RD_WR_PROGRAM_BLOCK_FILE)) {
795                                                         sbus_quint8_helper0=0;
796                                                         for (i=15; i<39; i++) { /*max length is 24 chars*/
797                                                                /*find zero-termination of string*/
798                                                                if ((tvb_get_guint8(tvb, i)) == 0x00) {
799                                                                       break;
800                                                                }
801                                                                sbus_quint8_helper0 += 1;
802                                                         }
803                                                         tmp_string = tvb_get_ephemeral_string(tvb , 15,
804                                                                                               sbus_quint8_helper0);
805                                                         col_append_fstr(pinfo->cinfo, COL_INFO,
806                                                                         ": (File: %s)", tmp_string);
807                                                  }
808                                           }
809
810                                           break;
811
812
813                                    default:
814                                           /* All other requests */
815                                           col_add_fstr(pinfo->cinfo, COL_INFO,
816                                                        "Request: %s", val_to_str(sbus_cmd_code,
817                                                                                  sbus_command_vals, "Unknown Command!"));
818                                           break;
819                             }
820                             /*mark retries*/
821                             if (request_val->retry_count>0) {
822                                    col_append_str(pinfo->cinfo, COL_INFO,
823                                    " (Retry)");
824                             } /*no retry number as it is not always correctly calculated*/
825                             break;
826
827                      case SBUS_RESPONSE:
828                             /* Special treatment of web server request
829                              * as is is very helpful to see more information in the packetlist */
830                             if (request_val && ((request_val->cmd_code) == SBUS_WEB_SERVER_SERIAL_COMM)) {
831                                    sbus_web_size = tvb_get_guint8(tvb,9);
832                                    sbus_web_aid = tvb_get_guint8(tvb,10);
833                                    col_add_fstr(pinfo->cinfo, COL_INFO,
834                                           "Response: %s",
835                                           val_to_str(sbus_web_aid,
836                                                  webserver_aid_vals, "Unknown Request!"));
837                                    if (sbus_web_size > 1) {
838                                           sbus_web_seq = tvb_get_guint8(tvb,11);
839                                           col_append_fstr(pinfo->cinfo, COL_INFO,
840                                               " (Seq No: %d)",
841                                               sbus_web_seq);
842                                    }
843                             } else if (request_val && ((request_val->cmd_code) == SBUS_RD_WR_PCD_BLOCK)) {
844                                    /* Treat the ACK/NAK telgrams in a special way*/
845                                    switch (request_val->block_tlg) {
846                                           case SBUS_WR_START_OF_STREAM:
847                                           case SBUS_WR_BLOCK_DATA_STREAM:
848                                           case SBUS_WR_BLOCK_END_OF_STREAM:
849                                           case SBUS_WR_ABORT_BLOCK_STREAM:
850                                           case SBUS_WR_BLOCK_DATA_BYTES:
851                                           case SBUS_DELETE_BLOCK:
852                                           case SBUS_RD_ABORT_BLOCK_STREAM:
853                                                  sbus_rdwr_ack_nak = tvb_get_guint8(tvb, 10);
854                                                  col_add_fstr( pinfo->cinfo, COL_INFO,
855                                                                "Response: %s", val_to_str(sbus_rdwr_ack_nak,
856                                                                                           rdwrblock_sts, "Unknown response!"));
857                                                  break;
858                                           default:
859                                                  sbus_rdwr_type = tvb_get_guint8(tvb, 9);
860                                                  col_add_fstr( pinfo->cinfo, COL_INFO,
861                                                                "Response: (%d byte)", sbus_rdwr_type);
862                                                  break;
863                                    }
864
865                             } else {
866                                    col_set_str(pinfo->cinfo, COL_INFO, "Response");
867                             }
868                             break;
869
870                      case SBUS_ACKNAK:
871                             sbus_ack_code = tvb_get_ntohs(tvb,9);
872                             col_add_fstr(pinfo->cinfo, COL_INFO,
873                                          "%s", val_to_str(sbus_ack_code,
874                                                           sbus_ack_nak_vals,
875                                                           "Unknown NAK response code!"));
876                             break;
877
878                      default:
879                             col_set_str(pinfo->cinfo, COL_INFO, "Unknown attribute");
880                             break;
881               }
882
883        }
884 /* create display subtree for the protocol */
885        if (tree) {
886
887               ti = proto_tree_add_item(tree, proto_sbus, tvb, offset, -1, ENC_NA);
888               sbus_tree = proto_item_add_subtree(ti, ett_sbus);
889
890 /*Add subtree for Ether-S-Bus header*/
891               et = proto_tree_add_text(sbus_tree, tvb, offset, 8, "Ether-S-Bus header");
892               ethsbus_tree = proto_item_add_subtree(et, ett_sbus_ether);
893
894 /* add an item to the subtree*/
895               sbus_eth_len = tvb_get_ntohl(tvb,offset);
896               proto_tree_add_item(ethsbus_tree,
897                                   hf_sbus_length, tvb, offset, 4, ENC_BIG_ENDIAN);
898               offset += 4;
899
900               proto_tree_add_item(ethsbus_tree,
901                                   hf_sbus_version, tvb, offset, 1, ENC_BIG_ENDIAN);
902               offset += 1;
903
904               proto_tree_add_item(ethsbus_tree,
905                                   hf_sbus_protocol, tvb, offset, 1, ENC_BIG_ENDIAN);
906               offset += 1;
907
908               proto_tree_add_item(ethsbus_tree,
909                                   hf_sbus_sequence, tvb, offset, 2, ENC_BIG_ENDIAN);
910               offset += 2;
911
912 /* Continue adding stuff to the main tree*/
913               sbus_attribut = tvb_get_guint8(tvb,offset);
914               proto_tree_add_item(sbus_tree,
915                                   hf_sbus_attribut, tvb, offset, 1, ENC_BIG_ENDIAN);
916               offset += 1;
917
918               if (sbus_attribut == SBUS_REQUEST) {
919                      proto_tree_add_item(sbus_tree,
920                                          hf_sbus_dest, tvb, offset, 1, ENC_BIG_ENDIAN);
921                      offset += 1;
922                      sbus_cmd_code = tvb_get_guint8(tvb,offset);
923                      proto_tree_add_item(sbus_tree,
924                                          hf_sbus_command, tvb, offset, 1, ENC_BIG_ENDIAN);
925                      offset += 1;
926                      if (request_val && request_val->retry_count > 0) {/*this is a retry telegram*/
927                             hi = proto_tree_add_boolean(sbus_tree,
928                                                         hf_sbus_retry, tvb, 0, 0, TRUE);
929                             PROTO_ITEM_SET_GENERATED(hi);
930                             expert_add_info_format(pinfo, hi, PI_SEQUENCE, PI_NOTE,
931                                                    "Repeated telegram (due to timeout?)");
932                             nstime_delta(&ns, &pinfo->fd->abs_ts, &request_val->req_time);
933                             proto_tree_add_time(sbus_tree, hf_sbus_timeout,
934                                                 tvb, 0, 0, &ns);
935                             proto_tree_add_uint(sbus_tree, hf_sbus_request_in, tvb, 0, 0,
936                                                 request_val->req_frame);
937                      }
938                      if (request_val && request_val->resp_frame > pinfo->fd->num){
939                             proto_tree_add_uint(sbus_tree, hf_sbus_response_in, tvb, 0, 0,
940                                                 request_val->resp_frame);
941                      }
942                      switch (sbus_cmd_code) {
943                             /*Read Counter, Register or Timer*/
944                             case SBUS_RD_COUNTER:
945                             case SBUS_RD_REGISTER:
946                             case SBUS_RD_TIMER:
947                                    sbus_media_cnt = (tvb_get_guint8(tvb,offset))+1;
948                                    proto_tree_add_uint(sbus_tree,
949                                                        hf_sbus_rcount, tvb, offset, 1, sbus_media_cnt);
950                                    offset += 1;
951                                    proto_tree_add_item(sbus_tree,
952                                                        hf_sbus_addr_rtc, tvb, offset, 2, ENC_BIG_ENDIAN);
953                                    offset += 2;
954                                    break;
955
956                                    /*Read Flag, Input or Output*/
957                             case SBUS_RD_FLAG:
958                             case SBUS_RD_INPUT:
959                             case SBUS_RD_OUTPUT:
960                                    sbus_media_cnt = (tvb_get_guint8(tvb,offset))+1;
961                                    proto_tree_add_uint(sbus_tree,
962                                                        hf_sbus_rcount, tvb, offset, 1, sbus_media_cnt);
963                                    offset += 1;
964                                    proto_tree_add_item(sbus_tree,
965                                                        hf_sbus_addr_iof, tvb, offset, 2, ENC_BIG_ENDIAN);
966                                    offset += 2;
967                                    break;
968
969                                    /*Write Register Timer Counter*/
970                             case SBUS_WR_COUNTER:
971                             case SBUS_WR_REGISTER:
972                             case SBUS_WR_TIMER:
973                                    sbus_media_cnt = (tvb_get_guint8(tvb,offset));
974                                    sbus_media_cnt = ((sbus_media_cnt - 1)/4);
975                                    proto_tree_add_uint(sbus_tree,
976                                                        hf_sbus_wcount_calculated, tvb, offset,
977                                                        1, sbus_media_cnt);
978                                    proto_tree_add_item(sbus_tree,
979                                                        hf_sbus_wcount, tvb, offset, 1, ENC_BIG_ENDIAN);
980                                    offset += 1;
981                                    proto_tree_add_item(sbus_tree,
982                                                        hf_sbus_addr_rtc, tvb, offset, 2, ENC_BIG_ENDIAN);
983                                    offset += 2;
984                                    /*Add subtree for Data*/
985                                    dt = proto_tree_add_text(sbus_tree, tvb, offset,
986                                                             ((sbus_media_cnt) * 4),"Data");
987
988                                    sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
989                                    for (i=((sbus_media_cnt)); i>0; i--) {
990                                           proto_tree_add_item(sbusdata_tree,
991                                                               hf_sbus_data_rtc, tvb, offset,
992                                                               4, ENC_BIG_ENDIAN);
993                                           offset += 4;
994                                    }
995                                    break;
996
997                                    /* Write flags and outputs*/
998                             case SBUS_WR_FLAG:
999                             case SBUS_WR_OUTPUT:
1000                                    sbus_media_cnt = (tvb_get_guint8(tvb,offset));
1001                                    sbus_media_cnt = (sbus_media_cnt - 2);
1002                                    proto_tree_add_uint(sbus_tree,
1003                                                        hf_sbus_wcount_calculated, tvb, offset,
1004                                                        1, sbus_media_cnt);
1005                                    proto_tree_add_item(sbus_tree,
1006                                                        hf_sbus_wcount, tvb, offset, 1, ENC_BIG_ENDIAN);
1007                                    offset += 1;
1008                                    proto_tree_add_item(sbus_tree,
1009                                                        hf_sbus_addr_iof, tvb, offset, 2, ENC_BIG_ENDIAN);
1010                                    offset += 2;
1011                                    sbus_fio_cnt = (tvb_get_guint8(tvb,offset));
1012                                    sbus_fio_cnt = ((sbus_fio_cnt + 1));
1013                                    proto_tree_add_uint(sbus_tree,
1014                                                        hf_sbus_fio_count, tvb, offset, 1, sbus_fio_cnt);
1015                                    offset += 1;
1016                                    /*Add subtree for Data*/
1017                                    dt = proto_tree_add_text(sbus_tree, tvb, offset,
1018                                                             sbus_media_cnt,"Data");
1019
1020                                    sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
1021                                    for (i=sbus_media_cnt; i>0; i--) {
1022                                           sbus_helper = 1;
1023                                           sbus_show_bin = 0;
1024                                           sbus_binarymasked = 0x01;
1025                                           sbus_binaries = tvb_get_guint8(tvb, offset);
1026                                           for (j=0; j<8; j++) {
1027                                                  if ((sbus_binarymasked & sbus_binaries) != 0) {
1028                                                         sbus_show_bin = (sbus_show_bin + sbus_helper);
1029                                                  }
1030                                                  sbus_binarymasked = sbus_binarymasked<<1;
1031                                                  sbus_helper = 10 * sbus_helper;
1032                                           }
1033
1034                                           proto_tree_add_uint_format(sbusdata_tree,
1035                                                                      hf_sbus_data_iof, tvb, offset, 1, sbus_show_bin,
1036                                                                      "Binary data: %08u", sbus_show_bin);
1037                                           offset += 1;
1038                                    }
1039                                    break;
1040
1041                                    /* Request: Write Real time clock*/
1042                             case SBUS_WR_RTC:
1043                                    sbus_helper = tvb_get_guint8(tvb, (offset +5));  /*hours*/
1044                                    sbus_helper1 = tvb_get_guint8(tvb, (offset +6)); /*minutes*/
1045                                    sbus_helper2 = tvb_get_guint8(tvb, (offset +7)); /*seconds*/
1046                                    proto_tree_add_text(sbus_tree, tvb, (offset +5), 3,
1047                                                        "Time (HH:MM:SS): %02x:%02x:%02x", sbus_helper, sbus_helper1, sbus_helper2);
1048                                    sbus_helper = tvb_get_guint8(tvb, (offset +2));  /*year*/
1049                                    sbus_helper1 = tvb_get_guint8(tvb, (offset +3)); /*month*/
1050                                    sbus_helper2 = tvb_get_guint8(tvb, (offset +4)); /*day*/
1051                                    proto_tree_add_text(sbus_tree, tvb, (offset +2), 3,
1052                                                        "Date (YY/MM/DD): %02x/%02x/%02x", sbus_helper, sbus_helper1, sbus_helper2);
1053                                    sbus_helper = tvb_get_guint8(tvb, (offset));  /*year-week*/
1054                                    sbus_helper1 = tvb_get_guint8(tvb, (offset +1)); /*week-day*/
1055                                    proto_tree_add_text(sbus_tree, tvb, offset, 2,
1056                                                        "Calendar week: %x, Week day: %x", sbus_helper, sbus_helper1);
1057                                    /*Add subtree for Data*/
1058                                    dt = proto_tree_add_text(sbus_tree, tvb, offset,
1059                                                             8, "Clock data");
1060                                    sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
1061
1062                                    proto_tree_add_item(sbusdata_tree,
1063                                                        hf_sbus_week_day, tvb, offset, 2, ENC_BIG_ENDIAN);
1064                                    offset += 2;
1065                                    proto_tree_add_item(sbusdata_tree,
1066                                                        hf_sbus_date, tvb, offset, 3, ENC_BIG_ENDIAN);
1067                                    offset += 3;
1068                                    proto_tree_add_item(sbusdata_tree,
1069                                                        hf_sbus_time, tvb, offset, 3, ENC_BIG_ENDIAN);
1070                                    offset += 3;
1071                                    break;
1072
1073                                    /* Read user memory or program line*/
1074                             case SBUS_RD_USER_MEMORY:
1075                             case SBUS_RD_PROGRAM_LINE:
1076                                    sbus_media_cnt = (tvb_get_guint8(tvb,offset))+1;
1077                                    proto_tree_add_uint(sbus_tree,
1078                                                        hf_sbus_rcount, tvb, offset, 1, sbus_media_cnt);
1079                                    offset += 1;
1080                                    proto_tree_add_item(sbus_tree,
1081                                                        hf_sbus_addr_prog, tvb, offset, 3, ENC_BIG_ENDIAN);
1082                                    offset += 3;
1083                                    break;
1084
1085                                    /*Write user memory*/
1086                             case SBUS_WR_USER_MEMORY:
1087                                    sbus_media_cnt = (tvb_get_guint8(tvb,offset));
1088                                    sbus_media_cnt = ((sbus_media_cnt - 2)/4);
1089                                    proto_tree_add_uint(sbus_tree,
1090                                                        hf_sbus_wcount_calculated, tvb, offset,
1091                                                        1, sbus_media_cnt);
1092                                    proto_tree_add_item(sbus_tree,
1093                                                        hf_sbus_wcount, tvb, offset, 1, ENC_BIG_ENDIAN);
1094                                    offset += 1;
1095                                    proto_tree_add_item(sbus_tree,
1096                                                        hf_sbus_addr_68k, tvb, offset, 3, ENC_BIG_ENDIAN);
1097                                    offset += 3;
1098                                    /*Add subtree for Data*/
1099                                    dt = proto_tree_add_text(sbus_tree, tvb, offset,
1100                                                             ((sbus_media_cnt) * 4),"Program lines");
1101
1102                                    sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
1103                                    for (i=((sbus_media_cnt)); i>0; i--) {
1104                                           proto_tree_add_item(sbusdata_tree,
1105                                                               hf_sbus_data_rtc, tvb, offset,
1106                                                               4, ENC_BIG_ENDIAN);
1107                                           offset += 4;
1108
1109                                    }
1110                                    break;
1111
1112                                    /* Read byte*/
1113                             case SBUS_RD_BYTE:
1114                                    sbus_media_cnt = (tvb_get_guint8(tvb,offset))+1;
1115                                    proto_tree_add_uint(sbus_tree,
1116                                                        hf_sbus_rcount, tvb, offset, 1, sbus_media_cnt);
1117                                    offset += 1;
1118                                    proto_tree_add_item(sbus_tree,
1119                                                        hf_sbus_addr_68k, tvb, offset, 3, ENC_BIG_ENDIAN);
1120                                    offset += 3;
1121                                    break;
1122
1123                                    /* Write byte */
1124                             case SBUS_WR_BYTE:
1125                                    sbus_media_cnt = (tvb_get_guint8(tvb,offset));
1126                                    sbus_media_cnt = (sbus_media_cnt - 2);
1127                                    proto_tree_add_uint(sbus_tree,
1128                                                        hf_sbus_wcount_calculated, tvb, offset,
1129                                                        1, sbus_media_cnt);
1130                                    proto_tree_add_item(sbus_tree,
1131                                                        hf_sbus_wcount, tvb, offset, 1, ENC_BIG_ENDIAN);
1132                                    offset += 1;
1133                                    proto_tree_add_item(sbus_tree,
1134                                                        hf_sbus_addr_68k, tvb, offset, 3, ENC_BIG_ENDIAN);
1135                                    offset += 3;
1136                                    /*Add subtree for Data*/
1137                                    dt = proto_tree_add_text(sbus_tree, tvb, offset,
1138                                                             ((sbus_media_cnt) * 4),"Data (bytes)");
1139
1140                                    sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
1141                                    for (i=sbus_media_cnt; i>0; i--) {
1142                                           proto_tree_add_item(sbusdata_tree,
1143                                                               hf_sbus_data_byte, tvb, offset,
1144                                                               1, ENC_BIG_ENDIAN);
1145                                           offset += 1;
1146                                    }
1147                                    break;
1148
1149                                    /*Read EEPROM register*/
1150                             case SBUS_RD_USER_EEPROM_REGISTER:
1151                                    proto_tree_add_item(sbus_tree,
1152                                                        hf_sbus_command_extension, tvb, offset, 1, ENC_BIG_ENDIAN);
1153                                    offset += 1;
1154                                    sbus_media_cnt = (tvb_get_guint8(tvb,offset))+1;
1155                                    proto_tree_add_uint(sbus_tree,
1156                                                        hf_sbus_rcount, tvb, offset, 1, sbus_media_cnt);
1157                                    offset += 1;
1158                                    proto_tree_add_item(sbus_tree,
1159                                                        hf_sbus_addr_eeprom, tvb, offset, 2, ENC_BIG_ENDIAN);
1160                                    offset += 2;
1161                                    break;
1162
1163                                    /*Request for reading system info*/
1164                                    /*Syinfo 05 is not implemented as no serial baud is possible*/
1165                             case SBUS_RD_SYSTEM_INFORMATION:
1166                                    proto_tree_add_item(sbus_tree,
1167                                                        hf_sbus_sysinfo_nr, tvb, offset, 1, ENC_BIG_ENDIAN);
1168                                    offset += 1;
1169                                    proto_tree_add_item(sbus_tree,
1170                                                        hf_sbus_sysinfo_nr, tvb, offset, 1, ENC_BIG_ENDIAN);
1171                                    offset += 1;
1172                                    break;
1173
1174                                    /* WebServer Request */
1175                             case SBUS_WEB_SERVER_SERIAL_COMM:
1176                                    sbus_web_size = tvb_get_guint8(tvb,offset);
1177                                    proto_tree_add_uint(sbus_tree,
1178                                                        hf_sbus_web_size, tvb, offset,
1179                                                        1, sbus_web_size);
1180                                    offset += 1;
1181
1182                                    sbus_web_aid = tvb_get_guint8(tvb,offset);
1183                                    proto_tree_add_uint(sbus_tree,
1184                                                        hf_sbus_web_aid, tvb, offset,
1185                                                        1, sbus_web_aid);
1186                                    offset += 1;
1187
1188                                    sbus_web_seq = tvb_get_guint8(tvb,offset);
1189                                    proto_tree_add_uint(sbus_tree,
1190                                                        hf_sbus_web_seq, tvb, offset,
1191                                                        1, sbus_web_seq);
1192                                    offset += 1;
1193
1194                                    if (sbus_web_size > 1) {
1195                                           dt = proto_tree_add_text(sbus_tree, tvb, offset,
1196                                                                    (sbus_web_size - 1),"Data (bytes)");
1197
1198                                           sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
1199                                           for (i=sbus_web_size -1 ; i>0; i--) {
1200                                                  proto_tree_add_item(sbusdata_tree,
1201                                                                      hf_sbus_data_byte, tvb, offset,
1202                                                                      1, ENC_BIG_ENDIAN);
1203                                                  offset += 1;
1204                                           }
1205                                    }
1206                                    break;
1207                                    /* Read/write block request */
1208                             case SBUS_RD_WR_PCD_BLOCK:
1209                                    if (tvb_get_guint8(tvb,offset) == 0xff){
1210                                           sbus_rdwr_length = ((tvb_get_ntohl(tvb,0))-15);
1211                                           proto_tree_add_uint(sbus_tree,
1212                                                               hf_sbus_rdwr_block_length_ext, tvb, 0, 4, sbus_rdwr_length);
1213                                           offset += 1;
1214                                    } else {
1215                                           sbus_rdwr_length = tvb_get_guint8(tvb,offset);
1216                                           proto_tree_add_uint(sbus_tree,
1217                                                               hf_sbus_rdwr_block_length, tvb, offset,
1218                                                               1, sbus_rdwr_length);
1219                                           offset += 1;
1220                                    }
1221                                    sbus_rdwr_type = tvb_get_guint8(tvb,offset);
1222                                    proto_tree_add_uint(sbus_tree,
1223                                                        hf_sbus_rdwr_telegram_type, tvb, offset,
1224                                                        1, sbus_rdwr_type);
1225                                    offset += 1;
1226                                    switch(sbus_rdwr_type) {
1227                                           case SBUS_WR_START_OF_STREAM:
1228                                                  sbus_rdwr_block_type = tvb_get_guint8(tvb, 14);
1229                                                  proto_tree_add_item(sbus_tree,
1230                                                                      hf_sbus_rdwr_telegram_sequence, tvb, offset,
1231                                                                      1, ENC_BIG_ENDIAN);
1232                                                  offset += 1;
1233                                                  proto_tree_add_item(sbus_tree,
1234                                                                      hf_sbus_block_type, tvb, offset,
1235                                                                      1, ENC_BIG_ENDIAN);
1236                                                  offset += 1;
1237
1238                                                  /* Check for file or block download */
1239                                                  if ((sbus_rdwr_block_type == SBUS_RD_WR_CONFIGURATION_FILE) ||
1240                                                      (sbus_rdwr_block_type == SBUS_RD_WR_PROGRAM_BLOCK_FILE)) {
1241                                                         proto_tree_add_item(sbus_tree,
1242                                                                             hf_sbus_rdwr_block_size, tvb, offset,
1243                                                                             4, ENC_BIG_ENDIAN);
1244                                                         offset += 4;
1245                                                         sbus_quint8_helper0=0;
1246                                                         /*find zero-termination of string*/
1247                                                         for (i=19; i<43; i++) { /*max length string is 24 char*/
1248                                                                if ((tvb_get_guint8(tvb, i)) == 0x00) {
1249                                                                       break;
1250                                                                }
1251                                                                sbus_quint8_helper0 += 1;
1252                                                         }
1253                                                         tmp_string = tvb_get_ephemeral_string(tvb , 19, sbus_quint8_helper0);
1254                                                         proto_tree_add_string(sbus_tree,
1255                                                                               hf_sbus_rdwr_file_name, tvb, offset,
1256                                                                               sbus_quint8_helper0, tmp_string);
1257                                                         offset += sbus_quint8_helper0;
1258                                                         /*do not display a field for block data (skip)*/
1259                                                         offset += (sbus_rdwr_length-6-sbus_quint8_helper0);
1260                                                  } else { /* block write telegram, no file write*/
1261                                                         proto_tree_add_item(sbus_tree,
1262                                                                             hf_sbus_block_nr, tvb, offset,
1263                                                                             2, ENC_BIG_ENDIAN);
1264                                                         offset += 2;
1265                                                         proto_tree_add_item(sbus_tree,
1266                                                                             hf_sbus_rdwr_block_size, tvb, offset,
1267                                                                             4, ENC_BIG_ENDIAN);
1268                                                         offset += 4;
1269                                                         /*do not display a field for block data (skip)*/
1270                                                         offset += (sbus_rdwr_length-8);
1271                                                  }
1272                                                  break;
1273                                           case SBUS_WR_BLOCK_DATA_STREAM:
1274                                                  sbus_rdwr_sequence = tvb_get_guint8(tvb,offset);
1275                                                  proto_tree_add_uint(sbus_tree,
1276                                                                      hf_sbus_rdwr_telegram_sequence, tvb, offset,
1277                                                                      1, sbus_rdwr_sequence);
1278                                                  offset += 1;
1279                                                  /*do not display a field for block data (skip)*/
1280                                                  offset += (sbus_rdwr_length-1);
1281                                                  break;
1282                                           case SBUS_WR_BLOCK_END_OF_STREAM:
1283                                                  sbus_rdwr_sequence = tvb_get_guint8(tvb,offset);
1284                                                  proto_tree_add_uint(sbus_tree,
1285                                                                      hf_sbus_rdwr_telegram_sequence, tvb, offset,
1286                                                                      1, sbus_rdwr_sequence);
1287                                                  offset += 1;
1288                                                  /*do not display a field for block data (skip it)*/
1289                                                  offset += (sbus_rdwr_length-5);
1290                                                  /*do not display a field for block CRC (skip it)*/
1291                                                  offset += 4;
1292                                                  break;
1293                                           case SBUS_WR_ABORT_BLOCK_STREAM:
1294                                           case SBUS_RD_ABORT_BLOCK_STREAM:
1295                                                  break;
1296                                           case SBUS_WR_BLOCK_DATA_BYTES:
1297                                                  sbus_rdwr_block_type = tvb_get_guint8(tvb, 14);
1298                                                  proto_tree_add_item(sbus_tree,
1299                                                                      hf_sbus_block_type, tvb, offset,
1300                                                                      1, ENC_BIG_ENDIAN);
1301                                                  offset += 1;
1302
1303                                                  /* Check for file or block download */
1304                                                  if ((sbus_rdwr_block_type == SBUS_RD_WR_CONFIGURATION_FILE) ||
1305                                                      (sbus_rdwr_block_type == SBUS_RD_WR_PROGRAM_BLOCK_FILE)) {
1306                                                         proto_tree_add_item(sbus_tree,
1307                                                                             hf_sbus_rdwr_block_addr, tvb, offset,
1308                                                                             4, ENC_BIG_ENDIAN);
1309                                                         offset += 4;
1310                                                         sbus_quint8_helper0=0;
1311                                                         /*find zero-termination of string*/
1312                                                         for (i=19; i<43; i++) { /*max length string is 24 char*/
1313                                                                if ((tvb_get_guint8(tvb, i)) == 0x00) {
1314                                                                       break;
1315                                                                }
1316                                                                sbus_quint8_helper0 += 1;
1317                                                         }
1318                                                         tmp_string = tvb_get_ephemeral_string(tvb, 19, sbus_quint8_helper0);
1319                                                         proto_tree_add_string(sbus_tree,
1320                                                                               hf_sbus_rdwr_file_name, tvb, offset,
1321                                                                               sbus_quint8_helper0, tmp_string);
1322                                                         offset += sbus_quint8_helper0;
1323                                                         /*do not display a field for block data (skip)*/
1324                                                         offset += (sbus_rdwr_length-6-sbus_quint8_helper0);
1325                                                  } else { /* block write telegram, no file write*/
1326                                                         proto_tree_add_item(sbus_tree,
1327                                                                             hf_sbus_block_nr, tvb, offset,
1328                                                                             2, ENC_BIG_ENDIAN);
1329                                                         offset += 2;
1330                                                         proto_tree_add_item(sbus_tree,
1331                                                                             hf_sbus_rdwr_block_addr, tvb, offset,
1332                                                                             4, ENC_BIG_ENDIAN);
1333                                                         offset += 4;
1334                                                         /*do not display a field for block data (skip)*/
1335                                                         offset += (sbus_rdwr_length-8);
1336                                                  }
1337                                                  break;
1338                                           case SBUS_RD_BLOCK_START_OF_STREAM:
1339                                                  sbus_rdwr_block_type = tvb_get_guint8(tvb, 14);
1340                                                  proto_tree_add_item(sbus_tree,
1341                                                                      hf_sbus_rdwr_telegram_sequence, tvb, offset,
1342                                                                      1, ENC_BIG_ENDIAN);
1343                                                  offset += 1;
1344                                                  proto_tree_add_item(sbus_tree,
1345                                                                      hf_sbus_block_type, tvb, offset,
1346                                                                      1, ENC_BIG_ENDIAN);
1347                                                  offset += 1;
1348
1349                                                  /* Check for file or block download */
1350                                                  if ((sbus_rdwr_block_type == SBUS_RD_WR_CONFIGURATION_FILE) ||
1351                                                      (sbus_rdwr_block_type == SBUS_RD_WR_PROGRAM_BLOCK_FILE)) {
1352                                                         sbus_quint8_helper0=0;
1353                                                         /*find zero-termination of string*/
1354                                                         for (i=14; i<38; i++) { /*max length string is 24 char*/
1355                                                                if ((tvb_get_guint8(tvb, i)) == 0x00) {
1356                                                                       break;
1357                                                                }
1358                                                                sbus_quint8_helper0 += 1;
1359                                                         }
1360                                                         tmp_string = tvb_get_ephemeral_string(tvb, 14, sbus_quint8_helper0);
1361                                                         proto_tree_add_string(sbus_tree,
1362                                                                               hf_sbus_rdwr_file_name, tvb, offset,
1363                                                                               sbus_quint8_helper0, tmp_string);
1364                                                         offset += sbus_quint8_helper0;
1365                                                  } else { /* block write telegram, no file write*/
1366                                                         proto_tree_add_item(sbus_tree,
1367                                                                             hf_sbus_block_nr, tvb, offset,
1368                                                                             2, ENC_BIG_ENDIAN);
1369                                                         offset += 2;
1370                                                  }
1371                                                  break;
1372                                           case SBUS_RD_BLOCK_DATA_STREAM:
1373                                                  proto_tree_add_item(sbus_tree,
1374                                                                      hf_sbus_rdwr_telegram_sequence, tvb, offset,
1375                                                                      1, ENC_BIG_ENDIAN);
1376                                                  offset += 1;
1377                                                  break;
1378                                           case SBUS_RD_BLOCK_DATA_BYTES:
1379                                                  sbus_rdwr_block_type = tvb_get_guint8(tvb, 13);
1380                                                  proto_tree_add_item(sbus_tree,
1381                                                                      hf_sbus_block_type, tvb, offset,
1382                                                                      1, ENC_BIG_ENDIAN);
1383                                                  offset += 1;
1384                                                  /* Check for file or block read */
1385                                                  if ((sbus_rdwr_block_type == SBUS_RD_WR_CONFIGURATION_FILE) ||
1386                                                      (sbus_rdwr_block_type == SBUS_RD_WR_PROGRAM_BLOCK_FILE)) {
1387                                                         /*reading from a file*/
1388                                                         proto_tree_add_item(sbus_tree,
1389                                                                             hf_sbus_rdwr_block_addr, tvb, offset,
1390                                                                             4, ENC_BIG_ENDIAN);
1391                                                         offset += 4;
1392                                                         proto_tree_add_item(sbus_tree,
1393                                                                             hf_sbus_rdwr_block_size, tvb, offset,
1394                                                                             4, ENC_BIG_ENDIAN);
1395                                                         offset += 4;
1396                                                         sbus_quint8_helper0=0;
1397                                                         /*find zero-termination of string*/
1398                                                         for (i=22; i<46; i++) { /*max length string is 24 char*/
1399                                                                if ((tvb_get_guint8(tvb, i)) == 0x00) {
1400                                                                       break;
1401                                                                }
1402                                                                sbus_quint8_helper0 += 1;
1403                                                         }
1404                                                         tmp_string = tvb_get_ephemeral_string(tvb, 22, sbus_quint8_helper0);
1405                                                         proto_tree_add_string(sbus_tree,
1406                                                                               hf_sbus_rdwr_file_name, tvb, offset,
1407                                                                               sbus_quint8_helper0, tmp_string);
1408                                                         offset += sbus_quint8_helper0 + 1;
1409                                                  } else { /* block read telegram, no file read*/
1410                                                         proto_tree_add_item(sbus_tree,
1411                                                                             hf_sbus_block_nr, tvb, offset,
1412                                                                             2, ENC_BIG_ENDIAN);
1413                                                         offset += 2;
1414                                                         proto_tree_add_item(sbus_tree,
1415                                                                             hf_sbus_rdwr_block_addr, tvb, offset,
1416                                                                             4, ENC_BIG_ENDIAN);
1417                                                         offset += 4;
1418                                                         proto_tree_add_item(sbus_tree,
1419                                                                             hf_sbus_rdwr_block_size, tvb, offset,
1420                                                                             4, ENC_BIG_ENDIAN);
1421                                                         offset += 4;
1422                                                  }
1423                                                  break;
1424                                           case SBUS_DELETE_BLOCK:
1425                                           case SBUS_GET_BLOCK_SIZE:
1426                                                  sbus_rdwr_block_type = tvb_get_guint8(tvb, 13);
1427                                                  proto_tree_add_item(sbus_tree,
1428                                                                      hf_sbus_block_type, tvb, offset,
1429                                                                      1, ENC_BIG_ENDIAN);
1430                                                  offset += 1;
1431                                                  /* Check for file or block deletion */
1432                                                  if ((sbus_rdwr_block_type == SBUS_RD_WR_CONFIGURATION_FILE) ||
1433                                                      (sbus_rdwr_block_type == SBUS_RD_WR_PROGRAM_BLOCK_FILE)) {
1434                                                         /*delete a file*/
1435                                                         sbus_quint8_helper0=0;
1436                                                         /*find zero-termination of string*/
1437                                                         for (i=14; i<38; i++) { /*max length string is 24 char*/
1438                                                                if ((tvb_get_guint8(tvb, i)) == 0x00) {
1439                                                                       break;
1440                                                                }
1441                                                                sbus_quint8_helper0 += 1;
1442                                                         }
1443                                                         tmp_string = tvb_get_ephemeral_string(tvb, 14, sbus_quint8_helper0);
1444                                                         proto_tree_add_string(sbus_tree,
1445                                                                               hf_sbus_rdwr_file_name, tvb, offset,
1446                                                                               sbus_quint8_helper0, tmp_string);
1447                                                         offset += sbus_quint8_helper0 + 1;
1448                                                  } else { /* delete a block*/
1449                                                         proto_tree_add_item(sbus_tree,
1450                                                                             hf_sbus_block_nr, tvb, offset,
1451                                                                             2, ENC_BIG_ENDIAN);
1452                                                         offset += 2;
1453                                                  }
1454                                                  break;
1455                                           case SBUS_GET_PROGRAM_BLOCK_LIST:
1456                                                  proto_tree_add_item(sbus_tree,
1457                                                                      hf_sbus_rdwr_list_type, tvb, offset,
1458                                                                      1, ENC_BIG_ENDIAN);
1459                                                  offset += 1;
1460                                                  break;
1461
1462                                           default:
1463                                                  break;
1464                                    }
1465
1466                                    break;
1467
1468                             /*Inform that command was not dissected and add remaining length*/
1469                             default:
1470                                    if (sbus_eth_len > 13) { /*13 bytes is the minimal length of a request telegram...*/
1471                                           sbus_helper = sbus_eth_len - (offset + 2);
1472                                           proto_tree_add_text(sbus_tree, tvb, offset, sbus_helper,
1473                                                               "This telegram isn't implemented in the dissector.");
1474                                           offset = offset + sbus_helper;
1475                                    }
1476                                    break;
1477                      }
1478               }
1479
1480               /* Response dissection*/
1481               if (sbus_attribut == SBUS_RESPONSE && request_val) {
1482                      /*add response time*/
1483                      nstime_delta(&ns, &pinfo->fd->abs_ts, &request_val->req_time);
1484                      proto_tree_add_time(sbus_tree, hf_sbus_response_time,
1485                            tvb, 0, 0, &ns);
1486                      /*add reference to request telegram*/
1487                      proto_tree_add_uint(sbus_tree, hf_sbus_response_to, tvb, 0, 0,
1488                           request_val->req_frame);
1489
1490                      switch (request_val->cmd_code) {
1491                             /* Response: 32 bit values*/
1492                             case SBUS_RD_COUNTER:
1493                             case SBUS_RD_REGISTER:
1494                             case SBUS_RD_TIMER:
1495                             case SBUS_RD_USER_MEMORY:
1496                             case SBUS_RD_PROGRAM_LINE:
1497                             case SBUS_RD_USER_EEPROM_REGISTER:
1498                                    /*Add subtree for Data*/
1499                                    dt = proto_tree_add_text(sbus_tree, tvb, offset,
1500                                                             ((request_val->count) * 4),"Data");
1501                                    sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
1502                                    for (i=(request_val->count); i>0; i--) {
1503                                           proto_tree_add_item(sbusdata_tree,
1504                                                               hf_sbus_data_rtc, tvb, offset,
1505                                                               4, ENC_BIG_ENDIAN);
1506                                           offset += 4;
1507                                    }
1508                                    break;
1509
1510                                    /* Response: PCD Display register*/
1511                             case SBUS_RD_DISPLAY_REGISTER:
1512                                    proto_tree_add_item(sbus_tree,
1513                                                        hf_sbus_display_register, tvb, offset, 4, ENC_BIG_ENDIAN);
1514                                    offset += 4;
1515                                    break;
1516
1517                                    /* Add binary data I, O, F*/
1518                             case SBUS_RD_FLAG:
1519                             case SBUS_RD_INPUT:
1520                             case SBUS_RD_OUTPUT:
1521                                    /*Add subtree for Data*/
1522                                    dt = proto_tree_add_text(sbus_tree, tvb, offset,
1523                                                             (((request_val->count) + 7) / 8), "Data");
1524                                    sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
1525
1526                                    for (i=(((request_val->count) + 7) / 8); i>0; i--) {
1527                                           sbus_helper = 1;
1528                                           sbus_show_bin = 0;
1529                                           sbus_binarymasked = 0x01;
1530                                           sbus_binaries = tvb_get_guint8(tvb, offset);
1531                                           for (j=0; j<8; j++){
1532                                                  if ((sbus_binarymasked & sbus_binaries) != 0) {
1533                                                         sbus_show_bin = (sbus_show_bin + sbus_helper);
1534                                                  }
1535                                                  sbus_binarymasked = sbus_binarymasked<<1;
1536                                                  sbus_helper = 10 * sbus_helper;
1537                                           }
1538
1539                                           proto_tree_add_uint_format(sbusdata_tree,
1540                                                                      hf_sbus_data_iof, tvb, offset, 1, sbus_show_bin,
1541                                                                      "Binary data: %08u", sbus_show_bin);
1542                                           offset += 1;
1543                                    }
1544                                    break;
1545
1546                                    /* Response: Real time clock value*/
1547                             case SBUS_RD_RTC:
1548                                    sbus_helper = tvb_get_guint8(tvb, (offset +5));  /*hours*/
1549                                    sbus_helper1 = tvb_get_guint8(tvb, (offset +6)); /*minutes*/
1550                                    sbus_helper2 = tvb_get_guint8(tvb, (offset +7)); /*seconds*/
1551                                    proto_tree_add_text(sbus_tree, tvb, (offset +5), 3,
1552                                                        "Time (HH:MM:SS): %02x:%02x:%02x", sbus_helper, sbus_helper1, sbus_helper2);
1553                                    sbus_helper = tvb_get_guint8(tvb, (offset +2));  /*year*/
1554                                    sbus_helper1 = tvb_get_guint8(tvb, (offset +3)); /*month*/
1555                                    sbus_helper2 = tvb_get_guint8(tvb, (offset +4)); /*day*/
1556                                    proto_tree_add_text(sbus_tree, tvb, (offset +2), 3,
1557                                                        "Date (YY/MM/DD): %02x/%02x/%02x", sbus_helper, sbus_helper1, sbus_helper2);
1558                                    sbus_helper = tvb_get_guint8(tvb, (offset));  /*year-week*/
1559                                    sbus_helper1 = tvb_get_guint8(tvb, (offset +1)); /*week-day*/
1560                                    proto_tree_add_text(sbus_tree, tvb, offset, 2,
1561                                                        "Calendar week: %x, Week day: %x", sbus_helper, sbus_helper1);
1562                                    /*Add subtree for Data*/
1563                                    dt = proto_tree_add_text(sbus_tree, tvb, offset,
1564                                                             8, "Clock data");
1565                                    sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
1566
1567                                    proto_tree_add_item(sbusdata_tree,
1568                                                        hf_sbus_week_day, tvb, offset, 2, ENC_BIG_ENDIAN);
1569                                    offset += 2;
1570                                    proto_tree_add_item(sbusdata_tree,
1571                                                        hf_sbus_date, tvb, offset, 3, ENC_BIG_ENDIAN);
1572                                    offset += 3;
1573                                    proto_tree_add_item(sbusdata_tree,
1574                                                        hf_sbus_time, tvb, offset, 3, ENC_BIG_ENDIAN);
1575                                    offset += 3;
1576                                    break;
1577
1578                                    /* Response: CPU status, the command codes 14..1B are concerned*/
1579                             case SBUS_RD_PCD_STATUS_CPU0:
1580                             case SBUS_RD_PCD_STATUS_CPU1:
1581                             case SBUS_RD_PCD_STATUS_CPU2:
1582                             case SBUS_RD_PCD_STATUS_CPU3:
1583                             case SBUS_RD_PCD_STATUS_CPU4:
1584                             case SBUS_RD_PCD_STATUS_CPU5:
1585                             case SBUS_RD_PCD_STATUS_CPU6:
1586                             case SBUS_RD_PCD_STATUS_OWN:
1587                                    proto_tree_add_item(sbus_tree,
1588                                                        hf_sbus_cpu_status, tvb, offset, 1, ENC_BIG_ENDIAN);
1589                                    offset += 1;
1590                                    break;
1591
1592                                    /* Response: Station address*/
1593                             case SBUS_RD_SBUS_STN_NBR:
1594                                    proto_tree_add_item(sbus_tree,
1595                                                        hf_sbus_address, tvb, offset, 1, ENC_BIG_ENDIAN);
1596                                    offset += 1;
1597                                    break;
1598
1599                                    /* Response: Firmware version */
1600                             case SBUS_RD_PROGRAM_VERSION:
1601                                    /*PCD type*/
1602                                    tmp_string = tvb_get_ephemeral_string(tvb , offset, 5);
1603                                    proto_tree_add_string(sbus_tree,
1604                                                          hf_sbus_cpu_type, tvb, offset, 5, tmp_string);
1605                                    offset += 5;
1606                                    /*FW version*/
1607                                    tmp_string = tvb_get_ephemeral_string(tvb , offset, 3);
1608                                    proto_tree_add_string(sbus_tree,
1609                                                          hf_sbus_fw_version, tvb, offset, 3, tmp_string);
1610                                    offset += 4;
1611                                    break;
1612
1613                                    /* Response for Status Flags*/
1614                             case SBUS_RD_STATUSFLAG_ACCU:
1615                                    /*Add subtree for Data*/
1616                                    dt = proto_tree_add_text(sbus_tree, tvb, offset,
1617                                                             1,"ACCU and arithmetic status");
1618                                    sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
1619
1620                                    proto_tree_add_item(sbusdata_tree, hf_sbus_flags_accu,
1621                                                        tvb, offset, 1, ENC_BIG_ENDIAN);
1622                                    proto_tree_add_item(sbusdata_tree, hf_sbus_flags_error,
1623                                                        tvb, offset, 1, ENC_BIG_ENDIAN);
1624                                    proto_tree_add_item(sbusdata_tree, hf_sbus_flags_negative,
1625                                                        tvb, offset, 1, ENC_BIG_ENDIAN);
1626                                    proto_tree_add_item(sbusdata_tree, hf_sbus_flags_zero,
1627                                                        tvb, offset, 1, ENC_BIG_ENDIAN);
1628                                    offset +=1;
1629                                    break;
1630
1631                                    /* Response for Read byte */
1632                             case SBUS_RD_BYTE:
1633                                    /*Add subtree for Data*/
1634                                    dt = proto_tree_add_text(sbus_tree, tvb, offset,
1635                                                             (request_val->count),"Data (bytes)");
1636
1637                                    sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
1638                                    for (i=(request_val->count); i>0; i--) {
1639                                           proto_tree_add_item(sbusdata_tree,
1640                                                               hf_sbus_data_byte, tvb, offset,
1641                                                               1, ENC_BIG_ENDIAN);
1642                                           offset += 1;
1643                                    }
1644                                    break;
1645
1646                                    /* Response for Read Index register */
1647                             case SBUS_RD_INDEX_REGISTER:
1648                                    /*Add subtree for Data*/
1649                                    dt = proto_tree_add_text(sbus_tree, tvb, offset,
1650                                                             2,"Data (hex bytes)");
1651
1652                                    sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
1653                                    for (i=0; i<2; i++) { /*2 bytes*/
1654                                           proto_tree_add_item(sbusdata_tree,
1655                                                               hf_sbus_data_byte_hex, tvb, offset,
1656                                                               1, ENC_BIG_ENDIAN);
1657                                           offset += 1;
1658                                    }
1659                                    break;
1660
1661                                    /* Response: Instruction pointer*/
1662                             case SBUS_RD_INSTRUCTION_POINTER:
1663                                    proto_tree_add_item(sbus_tree,
1664                                                        hf_sbus_addr_prog, tvb, offset, 3, ENC_BIG_ENDIAN);
1665                                    offset += 3;
1666                                    break;
1667
1668                                    /*Response for Find History*/
1669                             case SBUS_FIND_HISTORY:
1670                                    proto_tree_add_item(sbus_tree,
1671                                                        hf_sbus_addr_68k, tvb, offset, 3, ENC_BIG_ENDIAN);
1672                                    offset += 3;
1673                                    proto_tree_add_item(sbus_tree,
1674                                                        hf_sbus_nbr_elements, tvb, offset, 2, ENC_BIG_ENDIAN);
1675                                    offset += 2;
1676                                    break;
1677
1678                                    /* Response: Read current block*/
1679                             case SBUS_RD_CURRENT_BLOCK:
1680                                    proto_tree_add_item(sbus_tree,
1681                                                        hf_sbus_block_type, tvb, offset, 1, ENC_BIG_ENDIAN);
1682                                    offset += 1;
1683                                    proto_tree_add_item(sbus_tree,
1684                                                        hf_sbus_block_nr, tvb, offset, 2, ENC_BIG_ENDIAN);
1685                                    offset += 2;
1686                                    break;
1687
1688                                    /* Response: Read system infomation (without interpretation of module info)*/
1689                             case SBUS_RD_SYSTEM_INFORMATION:
1690                                    if (request_val->sysinfo == 0x00){ /*sysinfo 0*/
1691                                           offset += 1; /* this byte is always 0x01*/
1692                                           /*Add subtree for Data*/
1693                                           dt = proto_tree_add_text(sbus_tree, tvb, offset,
1694                                                                    1,"System info");
1695                                           sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
1696
1697                                           proto_tree_add_item(sbusdata_tree, hf_sbus_sysinfo0_1,
1698                                                               tvb, offset, 1, ENC_BIG_ENDIAN);
1699                                           proto_tree_add_item(sbusdata_tree, hf_sbus_sysinfo0_2,
1700                                                               tvb, offset, 1, ENC_BIG_ENDIAN);
1701                                           proto_tree_add_item(sbusdata_tree, hf_sbus_sysinfo0_3,
1702                                                               tvb, offset, 1, ENC_BIG_ENDIAN);
1703                                           proto_tree_add_item(sbusdata_tree, hf_sbus_sysinfo0_4,
1704                                                               tvb, offset, 1, ENC_BIG_ENDIAN);
1705                                           proto_tree_add_item(sbusdata_tree, hf_sbus_sysinfo0_5,
1706                                                               tvb, offset, 1, ENC_BIG_ENDIAN);
1707                                           offset += 1;
1708                                    } else {
1709                                           /*do not dissect all system info telegrams as there is no need*/
1710                                           offset = (tvb_get_guint8(tvb,9) + 10);
1711                                    }
1712                                    break;
1713
1714                                    /* Response: Webserver request */
1715                             case SBUS_WEB_SERVER_SERIAL_COMM:
1716                                    sbus_web_size = tvb_get_guint8(tvb,offset);
1717                                    proto_tree_add_uint(sbus_tree,
1718                                                        hf_sbus_web_size, tvb, offset,
1719                                                        1, sbus_web_size);
1720                                    offset += 1;
1721
1722                                    sbus_web_aid = tvb_get_guint8(tvb,offset);
1723                                    proto_tree_add_uint(sbus_tree,
1724                                                        hf_sbus_web_aid, tvb, offset,
1725                                                        1, sbus_web_aid);
1726                                    offset += 1;
1727
1728                                    if (sbus_web_size > 1) {
1729                                           sbus_web_seq = tvb_get_guint8(tvb,offset);
1730                                           proto_tree_add_uint(sbus_tree,
1731                                                               hf_sbus_web_seq, tvb, offset,
1732                                                               1, sbus_web_seq);
1733                                           offset += 1;
1734
1735                                           dt = proto_tree_add_text(sbus_tree, tvb, offset,
1736                                                                    (sbus_web_size - 2),"Data (bytes)");
1737
1738                                           sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
1739                                           for (i=sbus_web_size - 2; i>0; i--) {
1740                                                  proto_tree_add_item(sbusdata_tree,
1741                                                                      hf_sbus_data_byte, tvb, offset,
1742                                                                      1, ENC_BIG_ENDIAN);
1743                                                  offset += 1;
1744                                           }
1745                                    }
1746                                    break;
1747                                    /* Response: Read/Write block data */
1748                             case SBUS_RD_WR_PCD_BLOCK:
1749                                    sbus_rdwr_block_tlg = request_val->block_tlg;
1750                                    sbus_rdwr_length = tvb_get_guint8(tvb,offset);
1751                                    proto_tree_add_uint(sbus_tree,
1752                                                        hf_sbus_rdwr_block_length, tvb, offset,
1753                                                        1, sbus_rdwr_length);
1754                                    offset += 1;
1755                                    hi = proto_tree_add_item(sbus_tree,
1756                                                             hf_sbus_rdwr_acknakcode, tvb, offset,
1757                                                             1, ENC_BIG_ENDIAN);
1758                                    if ((tvb_get_guint8(tvb, offset) >= SBUS_RD_WR_NAK)&&
1759                                        (tvb_get_guint8(tvb, offset) <= SBUS_RD_WR_NAK_INVALID_SIZE)) {
1760                                           expert_add_info_format(pinfo, hi, PI_RESPONSE_CODE, PI_CHAT,
1761                                                                  "Telegram not acknowledged by PCD");
1762                                    }
1763                                    offset += 1;
1764                                    switch(sbus_rdwr_block_tlg) {
1765                                           case SBUS_WR_START_OF_STREAM:
1766                                           case SBUS_WR_BLOCK_DATA_STREAM:
1767                                           case SBUS_WR_BLOCK_END_OF_STREAM:
1768                                                  proto_tree_add_item(sbus_tree,
1769                                                                      hf_sbus_rdwr_telegram_sequence, tvb, offset,
1770                                                                      1, ENC_BIG_ENDIAN);
1771                                                  offset += 1;
1772                                                  break;
1773                                           case SBUS_WR_ABORT_BLOCK_STREAM:
1774                                           case SBUS_RD_ABORT_BLOCK_STREAM:
1775                                           case SBUS_WR_BLOCK_DATA_BYTES:
1776                                           case SBUS_DELETE_BLOCK:
1777                                                  break;
1778                                           case SBUS_RD_BLOCK_START_OF_STREAM:
1779                                                  proto_tree_add_item(sbus_tree,
1780                                                                      hf_sbus_rdwr_telegram_sequence, tvb, offset,
1781                                                                      1, ENC_BIG_ENDIAN);
1782                                                  offset += 1;
1783                                                  proto_tree_add_item(sbus_tree,
1784                                                                      hf_sbus_rdwr_block_size, tvb, offset,
1785                                                                      4, ENC_BIG_ENDIAN);
1786                                                  offset += 4;
1787                                                  /*do not display a field for block data (skip)*/
1788                                                  offset += (sbus_rdwr_length-6);
1789                                                  break;
1790                                           case SBUS_RD_BLOCK_DATA_STREAM:
1791                                                  proto_tree_add_item(sbus_tree,
1792                                                                      hf_sbus_rdwr_telegram_sequence, tvb, offset,
1793                                                                      1, ENC_BIG_ENDIAN);
1794                                                  offset += 1;
1795                                                  /*do not display a field for block data (skip)*/
1796                                                  offset += (sbus_rdwr_length-2);
1797                                                  break;
1798                                           case SBUS_RD_BLOCK_DATA_BYTES:
1799                                                  /*do not display a field for block data (skip)*/
1800                                                  offset += (sbus_rdwr_length-1);
1801                                                  break;
1802                                           case SBUS_GET_BLOCK_SIZE:
1803                                                  sbus_rdwr_block_type = tvb_get_guint8(tvb, 10);
1804                                                  /* Check for unknown block type */
1805                                                  if (sbus_rdwr_block_type == SBUS_RD_WR_UNKNOWN_BLOCK_TYPE) {
1806                                                         /*unknown block, no more data follows*/
1807                                                  } else { /* add block size and CRC32 in case of known block*/
1808                                                         proto_tree_add_item(sbus_tree,
1809                                                                             hf_sbus_rdwr_block_size, tvb, offset,
1810                                                                             4, ENC_BIG_ENDIAN);
1811                                                         offset += 4;
1812                                                         /*Now the CRC32 follows, but I don't bother calculating it*/
1813                                                         offset += 4;
1814                                                  }
1815                                                  break;
1816                                           case SBUS_GET_PROGRAM_BLOCK_LIST:
1817                                                  proto_tree_add_item(sbus_tree,
1818                                                                      hf_sbus_block_type, tvb, offset,
1819                                                                      1, ENC_BIG_ENDIAN);
1820                                                  offset += 1;
1821                                                  proto_tree_add_item(sbus_tree,
1822                                                                      hf_sbus_block_nr, tvb, offset,
1823                                                                      2, ENC_BIG_ENDIAN);
1824                                                  offset += 2;
1825                                                  proto_tree_add_item(sbus_tree,
1826                                                                      hf_sbus_rdwr_block_size, tvb, offset,
1827                                                                      4, ENC_BIG_ENDIAN);
1828                                                  offset += 4;
1829                                                  /*do not display block_timestamp as no description is available*/
1830                                                  offset += (sbus_rdwr_length-8);
1831                                                  break;
1832                                           default:
1833                                                  break;
1834                                    }
1835                                    break;
1836
1837                             /*Inform that response was not dissected and add remaining length*/
1838                             default:
1839                                    sbus_helper = sbus_eth_len - (offset + 2);
1840                                    proto_tree_add_text(sbus_tree, tvb, offset, sbus_helper,
1841                                                        "This telegram isn't implemented in the dissector.");
1842                                    offset = offset + sbus_helper;
1843                                    break;
1844                      }
1845               } else if (sbus_attribut == SBUS_RESPONSE && (!request_val)) {
1846                      /*calculate the offset in case the request telegram was not found or was broadcasted*/
1847                      sbus_eth_len = tvb_get_ntohl(tvb,0);
1848                      sbus_helper = sbus_eth_len - 11;
1849                      proto_tree_add_text(sbus_tree, tvb, offset, sbus_helper,
1850                             "Not dissected, could not find request telegram");
1851                      offset = sbus_eth_len - 2;
1852               }
1853
1854               if (sbus_attribut == SBUS_ACKNAK) {
1855                      /*Add response time if possible*/
1856                      if (request_val) {
1857                            nstime_delta(&ns, &pinfo->fd->abs_ts, &request_val->req_time);
1858                            proto_tree_add_time(sbus_tree, hf_sbus_response_time,
1859                                                tvb, 0, 0, &ns);
1860                            /*add reference to request telegram*/
1861                            proto_tree_add_uint(sbus_tree, hf_sbus_response_to, tvb, 0, 0,
1862                                                request_val->req_frame);
1863                      }
1864                      hi = proto_tree_add_item(sbus_tree,
1865                          hf_sbus_acknackcode, tvb, offset, 2, ENC_BIG_ENDIAN);
1866                      if (tvb_get_guint8(tvb, (offset+1)) > 0) {
1867                             expert_add_info_format(pinfo, hi, PI_RESPONSE_CODE, PI_CHAT,
1868                                                    "Telegram not acknowledged by PCD");
1869                      }
1870                      offset += 2;
1871               }
1872
1873               /* Calclulate CRC */
1874               sbus_crc_calc = 0;
1875               for (i = 0; i < sbus_eth_len - 2; i++)
1876                      sbus_crc_calc = crc_calc (sbus_crc_calc, tvb_get_guint8(tvb, i));
1877               /*Show CRC and add hidden item for wrong CRC*/
1878               sbus_helper = tvb_get_ntohs(tvb, offset);
1879               if (sbus_helper == sbus_crc_calc) {
1880                      proto_tree_add_uint_format(sbus_tree,
1881                                                 hf_sbus_crc, tvb, offset, 2, sbus_helper,
1882                                                 "Checksum: 0x%04x (correct)", sbus_helper);
1883               } else {
1884                      cs = proto_tree_add_uint_format(sbus_tree,
1885                                                      hf_sbus_crc, tvb, offset, 2, sbus_helper,
1886                                                      "Checksum: 0x%04x (NOT correct)", sbus_helper);
1887                      expert_add_info_format(pinfo, cs, PI_CHECKSUM, PI_ERROR,
1888                                             "Bad checksum");
1889                      hi = proto_tree_add_boolean(sbus_tree,
1890                                                  hf_sbus_crc_bad, tvb, offset, 2, TRUE);
1891                      PROTO_ITEM_SET_HIDDEN(hi);
1892                      PROTO_ITEM_SET_GENERATED(hi);
1893               }
1894               offset += 2; /*now at the end of the telegram*/
1895        }
1896        return tvb_length(tvb);
1897 /*End of dissect_sbus*/
1898 }
1899
1900 /* Register the protocol with Wireshark */
1901
1902 void
1903 proto_register_sbus(void)
1904 {
1905
1906 /* Setup list of header fields  See Section 1.6.1 for details*/
1907        static hf_register_info hf[] = {
1908               { &hf_sbus_length,
1909                      { "Length (bytes)",           "sbus.len",
1910                      FT_UINT32, BASE_DEC, NULL, 0,
1911                      "SAIA Ether-S-Bus telegram length", HFILL }
1912               },
1913               { &hf_sbus_version,
1914                      { "Version",           "sbus.vers",
1915                      FT_UINT8, BASE_DEC, NULL, 0,
1916                      "SAIA Ether-S-Bus version", HFILL }
1917               },
1918               { &hf_sbus_protocol,
1919                      { "Protocol type",           "sbus.proto",
1920                      FT_UINT8, BASE_DEC, NULL, 0,
1921                      "SAIA Ether-S-Bus protocol type", HFILL }
1922               },
1923               { &hf_sbus_sequence,
1924                      { "Sequence",           "sbus.seq",
1925                      FT_UINT16, BASE_DEC, NULL, 0,
1926                      "SAIA Ether-S-Bus sequence number", HFILL }
1927               },
1928
1929               { &hf_sbus_attribut,
1930                      { "Telegram attribute",           "sbus.att",
1931                      FT_UINT8, BASE_HEX, VALS(sbus_att_vals), 0,
1932                      "SAIA Ether-S-Bus telegram attribute, indicating type of telegram", HFILL }
1933               },
1934
1935               { &hf_sbus_dest,
1936                      { "Destination",           "sbus.destination",
1937                      FT_UINT8, BASE_DEC, NULL, 0,
1938                      "SAIA S-Bus destination address", HFILL }
1939               },
1940
1941               { &hf_sbus_address,
1942                      { "S-Bus address",           "sbus.address",
1943                      FT_UINT8, BASE_DEC, NULL, 0,
1944                      "SAIA S-Bus station address", HFILL }
1945               },
1946
1947               { &hf_sbus_command,
1948                      { "Command",           "sbus.cmd",
1949                      FT_UINT8, BASE_HEX, VALS(sbus_command_vals), 0,
1950                      "SAIA S-Bus command", HFILL }
1951               },
1952
1953               { &hf_sbus_command_extension,
1954                      { "Command extension",           "sbus.cmd_extn",
1955                      FT_UINT8, BASE_HEX, NULL, 0,
1956                      "SAIA S-Bus command extension", HFILL }
1957               },
1958
1959               { &hf_sbus_rcount,
1960                      { "R-count",           "sbus.rcount",
1961                      FT_UINT8, BASE_DEC, NULL, 0,
1962                      "Number of elements expected in response", HFILL }
1963               },
1964
1965               { &hf_sbus_wcount,
1966                      { "W-count (raw)",           "sbus.wcount",
1967                      FT_UINT8, BASE_DEC, NULL, 0,
1968                      "Number of bytes to be written", HFILL }
1969               },
1970
1971               { &hf_sbus_wcount_calculated,
1972                      { "W-count (32 bit values)",           "sbus.wcount_calc",
1973                      FT_UINT8, BASE_DEC, NULL, 0,
1974                      "Number of elements to be written", HFILL }
1975               },
1976
1977               { &hf_sbus_fio_count,
1978                      { "FIO Count (amount of bits)",           "sbus.fio_count",
1979                      FT_UINT8, BASE_DEC, NULL, 0,
1980                      "Number of binary elements to be written", HFILL }
1981               },
1982
1983               { &hf_sbus_addr_rtc,
1984                      { "Base address RTC",           "sbus.addr_RTC",
1985                      FT_UINT16, BASE_DEC, NULL, 0,
1986                      "Base address of 32 bit elements to read", HFILL }
1987               },
1988
1989               { &hf_sbus_addr_iof,
1990                      { "Base address IOF",           "sbus.addr_IOF",
1991                      FT_UINT16, BASE_DEC, NULL, 0,
1992                      "Base address of binary elements to read", HFILL }
1993               },
1994
1995               { &hf_sbus_addr_eeprom,
1996                      { "Base address of EEPROM register",           "sbus.addr_EEPROM",
1997                      FT_UINT16, BASE_DEC, NULL, 0,
1998                      "Base address of 32 bit EEPROM register to read or write", HFILL }
1999               },
2000
2001               { &hf_sbus_addr_prog,
2002                      { "Base address of user memory or program lines",           "sbus.addr_prog",
2003                      FT_UINT24, BASE_DEC, NULL, 0,
2004                      "Base address of the user memory or program lines (read or write)", HFILL }
2005               },
2006
2007               { &hf_sbus_addr_68k,
2008                      { "Base address of bytes",           "sbus.addr_prog",
2009                      FT_UINT24, BASE_HEX, NULL, 0,
2010                      "Base address of bytes to read or write (68k address)", HFILL }
2011               },
2012
2013               { &hf_sbus_block_type,
2014                      { "Block type",           "sbus.block_type",
2015                      FT_UINT8, BASE_HEX, VALS(sbus_block_types), 0,
2016                      "Program block type", HFILL }
2017               },
2018
2019               { &hf_sbus_block_nr,
2020                      { "Block/Element nr",           "sbus.block_nr",
2021                      FT_UINT16, BASE_DEC, NULL, 0,
2022                      "Program block / DatatBlock number", HFILL }
2023               },
2024
2025               { &hf_sbus_nbr_elements,
2026                      { "Number of elements",           "sbus.nbr_elements",
2027                      FT_UINT16, BASE_DEC, NULL, 0,
2028                      "Number of elements or characters", HFILL }
2029               },
2030
2031               { &hf_sbus_display_register,
2032                      { "PCD Display register",      "sbus.data_display_register",
2033                      FT_UINT32, BASE_DEC, NULL, 0,
2034                      "The PCD display register (32 bit value)", HFILL }
2035               },
2036
2037               { &hf_sbus_data_rtc,
2038                      { "S-Bus 32-bit data",      "sbus.data_rtc",
2039                      FT_UINT32, BASE_DEC, NULL, 0,
2040                      "One regiser/timer of counter (32 bit value)", HFILL }
2041               },
2042
2043               { &hf_sbus_data_byte,
2044                      { "Data bytes",      "sbus.data_byte",
2045                      FT_UINT8, BASE_DEC, NULL, 0,
2046                      "One byte from PCD", HFILL }
2047               },
2048
2049               { &hf_sbus_data_byte_hex,
2050                      { "Data bytes (hex)",      "sbus.data_byte_hex",
2051                      FT_UINT8, BASE_HEX, NULL, 0,
2052                      "One byte from PCD (hexadecimal)", HFILL }
2053               },
2054
2055               { &hf_sbus_data_iof,
2056                      { "S-Bus binary data",      "sbus.data_iof",
2057                      FT_UINT32, BASE_DEC, NULL, 0,
2058                      "8 binaries", HFILL }
2059               },
2060
2061               { &hf_sbus_cpu_type,
2062                      { "PCD type",      "sbus.pcd_type",
2063                      FT_STRING, BASE_NONE, NULL, 0,
2064                      "PCD type (short form)", HFILL }
2065               },
2066
2067               { &hf_sbus_fw_version,
2068                      { "Firmware version",      "sbus.fw_version",
2069                      FT_STRING, BASE_NONE, NULL, 0,
2070                      "Firmware version of the PCD or module", HFILL }
2071               },
2072
2073               { &hf_sbus_sysinfo_nr,
2074                      { "System information number",           "sbus.sysinfo",
2075                      FT_UINT8, BASE_HEX, NULL, 0,
2076                      "System information number (extension to command code)", HFILL }
2077               },
2078
2079               { &hf_sbus_sysinfo0_1,
2080                      { "Mem size info",      "sbus.sysinfo0.mem",
2081                      FT_BOOLEAN, 8, TFS(&tfs_sbus_present), F_MEMSIZE,
2082                      "Availability of memory size information", HFILL }
2083               },
2084               { &hf_sbus_sysinfo0_2,
2085                      { "Trace buffer",      "sbus.sysinfo0.trace",
2086                      FT_BOOLEAN, 8, TFS(&tfs_sbus_present), F_TRACE,
2087                      "Availability of trace buffer feature", HFILL }
2088               },
2089               { &hf_sbus_sysinfo0_3,
2090                      { "Slot B1",      "sbus.sysinfo0.b1",
2091                      FT_BOOLEAN, 8, TFS(&tfs_sbus_present), F_INFO_B1,
2092                      "Presence of EEPROM information on slot B1", HFILL }
2093               },
2094               { &hf_sbus_sysinfo0_4,
2095                      { "Slot B2",      "sbus.sysinfo0.b2",
2096                      FT_BOOLEAN, 8, TFS(&tfs_sbus_present), F_INFO_B2,
2097                      "Presence of EEPROM information on slot B2", HFILL }
2098               },
2099               { &hf_sbus_sysinfo0_5,
2100                      { "PGU baud",      "sbus.sysinfo0.pgubaud",
2101                      FT_BOOLEAN, 8, TFS(&tfs_sbus_present), F_PGU_BAUD,
2102                      "Availability of PGU baud switch feature", HFILL }
2103               },
2104
2105               { &hf_sbus_sysinfo_length,
2106                      { "System information length",           "sbus.sysinfo_length",
2107                      FT_UINT8, BASE_HEX, NULL, 0,
2108                      "System information length in response", HFILL }
2109               },
2110
2111               { &hf_sbus_f_module_type,
2112                      { "F-module type",      "sbus.fmodule_type",
2113                      FT_STRING, BASE_NONE, NULL, 0,
2114                      "Module type mounted on B1/2 slot", HFILL }
2115               },
2116
2117               { &hf_sbus_harware_version,
2118                      { "Hardware version",      "sbus.hw_version",
2119                      FT_STRING, BASE_NONE, NULL, 0,
2120                      "Hardware version of the PCD or the module", HFILL }
2121               },
2122
2123               { &hf_sbus_hardware_modification,
2124                      { "Hardware modification",      "sbus.hw_modification",
2125                      FT_UINT8, BASE_DEC, NULL, 0,
2126                      "Hardware modification of the PCD or module", HFILL }
2127               },
2128
2129               { &hf_sbus_various,
2130                      { "Various data",      "sbus.various",
2131                      FT_NONE, BASE_NONE, NULL, 0,
2132                      "Various data contained in telegrams but nobody will search for it", HFILL }
2133               },
2134
2135               { &hf_sbus_acknackcode,
2136                      { "ACK/NAK code",      "sbus.nakcode",
2137                      FT_UINT16, BASE_HEX, VALS(sbus_ack_nak_vals), 0,
2138                      "SAIA S-Bus ACK/NAK response", HFILL }
2139               },
2140
2141               { &hf_sbus_cpu_status,
2142                      { "CPU status",      "sbus.CPU_status",
2143                      FT_UINT8, BASE_HEX, VALS(sbus_CPU_status), 0,
2144                      "SAIA PCD CPU status", HFILL }
2145               },
2146
2147               { &hf_sbus_week_day,
2148                      { "RTC calendar week and week day",           "sbus.rtc.week_day",
2149                      FT_UINT16, BASE_HEX, NULL, 0,
2150                      "Calendar week and week day number of the real time clock", HFILL }
2151               },
2152
2153               { &hf_sbus_date,
2154                      { "RTC date (YYMMDD)",           "sbus.rtc.date",
2155                      FT_UINT24, BASE_HEX, NULL, 0,
2156                      "Year, month and day of the real time clock", HFILL }
2157               },
2158
2159               { &hf_sbus_time,
2160                      { "RTC time (HHMMSS)",           "sbus.rtc.time",
2161                      FT_UINT24, BASE_HEX, NULL, 0,
2162                      "Time of the real time clock", HFILL }
2163               },
2164
2165               { &hf_sbus_web_size,
2166                      { "Web server packet size",      "sbus.web.size",
2167                      FT_UINT8, BASE_HEX, NULL, 0,
2168                      NULL, HFILL }
2169               },
2170
2171               { &hf_sbus_web_aid,
2172                      { "AID",      "sbus.web.aid",
2173                      FT_UINT8, BASE_HEX, NULL, 0,
2174                      "Web server command/status code (AID)", HFILL }
2175               },
2176
2177               { &hf_sbus_web_seq,
2178                      { "Sequence",      "sbus.web.seq",
2179                      FT_UINT8, BASE_HEX, NULL, 0,
2180                      "Web server sequence nr (PACK_N)", HFILL }
2181               },
2182
2183               { &hf_sbus_rdwr_block_length,
2184                      { "Read/write block telegram length",      "sbus.block.length",
2185                      FT_UINT8, BASE_DEC, NULL, 0,
2186                      NULL, HFILL }
2187               },
2188
2189               { &hf_sbus_rdwr_block_length_ext,
2190                      { "Extended length (bytes)",           "sbus.len_ext",
2191                      FT_UINT32, BASE_DEC, NULL, 0,
2192                      NULL, HFILL }
2193               },
2194
2195               { &hf_sbus_rdwr_telegram_type,
2196                      { "Read/write block telegram type",      "sbus.block.tlgtype",
2197                      FT_UINT8, BASE_HEX, VALS(rdwrblock_vals), 0,
2198                      "Type of RD/WR block telegram", HFILL }
2199               },
2200
2201               { &hf_sbus_rdwr_telegram_sequence,
2202                      { "Sequence",           "sbus.block.seq",
2203                      FT_UINT8, BASE_DEC, NULL, 0,
2204                      "Sequence number of block data stream telegram", HFILL }
2205               },
2206
2207               { &hf_sbus_rdwr_block_size,
2208                      { "Block size in bytes",      "sbus.block.size",
2209                      FT_UINT32, BASE_DEC, NULL, 0,
2210                      "The size of the block in bytes", HFILL }
2211               },
2212
2213               { &hf_sbus_rdwr_block_addr,
2214                      { "Address inside block",      "sbus.block.addr",
2215                      FT_UINT32, BASE_DEC, NULL, 0,
2216                      "The address inside a block", HFILL }
2217               },
2218
2219
2220               { &hf_sbus_rdwr_file_name,
2221                      { "File name",      "sbus.block.filename",
2222                      FT_STRING, BASE_NONE, NULL, 0,
2223                      "Name of file to in RD/WR block telegram", HFILL }
2224               },
2225
2226               { &hf_sbus_rdwr_list_type,
2227                      { "Get program block list, command type",      "sbus.block.getlisttype",
2228                      FT_UINT8, BASE_HEX, VALS(rdwrblock_list_type_vals), 0,
2229                      "Type of the Get Program Block list request", HFILL }
2230               },
2231
2232               { &hf_sbus_rdwr_acknakcode,
2233                      { "ACK/NAK code",      "sbus.block.nakcode",
2234                      FT_UINT8, BASE_HEX, VALS(rdwrblock_sts), 0,
2235                      "ACK/NAK response for block write requests", HFILL }
2236               },
2237
2238               { &hf_sbus_crc,
2239                      { "Checksum",      "sbus.crc",
2240                      FT_UINT16, BASE_HEX, NULL, 0,
2241                      "CRC 16", HFILL }
2242               },
2243
2244               { &hf_sbus_crc_bad,
2245                      { "Bad Checksum",      "sbus.crc_bad",
2246                      FT_BOOLEAN, BASE_NONE, NULL, 0x0,
2247                      "A bad checksum in the telegram", HFILL }},
2248
2249               { &hf_sbus_retry,
2250                      { "Retry",      "sbus.retry", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
2251                      "Repeated request telegram (due to wrong or missing answer)", HFILL }},
2252
2253               { &hf_sbus_flags_accu,
2254                      { "ACCU", "sbus.flags.accu",
2255                      FT_BOOLEAN, 8, TFS(&tfs_sbus_flags), F_ACCU,
2256                      "PCD Accumulator", HFILL }
2257               },
2258
2259               { &hf_sbus_flags_error,
2260                      { "Error flag", "sbus.flags.error",
2261                      FT_BOOLEAN, 8, TFS(&tfs_sbus_flags), F_ERROR,
2262                      "PCD error flag", HFILL }
2263               },
2264
2265               { &hf_sbus_flags_negative,
2266                      { "N-flag", "sbus.flags.nflag",
2267                      FT_BOOLEAN, 8, TFS(&tfs_sbus_flags), F_NEGATIVE,
2268                      "Negative status flag", HFILL }
2269               },
2270
2271               { &hf_sbus_flags_zero,
2272                      { "Z-flag", "sbus.flags.zflag",
2273                      FT_BOOLEAN, 8, TFS(&tfs_sbus_flags), F_ZERO,
2274                      "Zero status flag", HFILL }
2275               },
2276
2277               { &hf_sbus_response_in,
2278                      { "Response in frame nr.", "sbus.response_in",
2279                      FT_FRAMENUM, BASE_NONE, NULL, 0x0,
2280                      "The response to this Ether-S-Bus request is in this frame", HFILL }
2281               },
2282
2283               { &hf_sbus_response_to,
2284                      { "Request in frame nr.", "sbus.response_to",
2285                      FT_FRAMENUM, BASE_NONE, NULL, 0x0,
2286                      "This is a response to the Ether-S-Bus request in this frame", HFILL }
2287               },
2288
2289               { &hf_sbus_response_time,
2290                      { "Response time", "sbus.response_time",
2291                      FT_RELATIVE_TIME, BASE_NONE, NULL, 0x0,
2292                      "The time between the request and the response", HFILL }
2293               },
2294
2295               { &hf_sbus_timeout,
2296                      { "Time passed since first request", "sbus.timeout",
2297                      FT_RELATIVE_TIME, BASE_NONE, NULL, 0x0,
2298                      "The time between the first (identical) request and the repetition", HFILL }
2299               },
2300
2301               { &hf_sbus_request_in,
2302                      { "First request in frame nr.", "sbus.request_in",
2303                      FT_FRAMENUM, BASE_NONE, NULL, 0x0,
2304                      "The first request of this repeated request is in this frame", HFILL }
2305               }
2306
2307        };
2308
2309 /* Setup protocol subtree array */
2310        static gint *ett[] = {
2311               &ett_sbus,
2312               &ett_sbus_ether,
2313               &ett_sbus_data
2314        };
2315
2316 /* Register the protocol name and description */
2317        proto_sbus = proto_register_protocol("SAIA S-Bus", "SBUS", "sbus");
2318
2319 /* Required function calls to register the header fields and subtrees used */
2320        proto_register_field_array(proto_sbus, hf, array_length(hf));
2321        proto_register_subtree_array(ett, array_length(ett));
2322        register_init_routine(&sbus_init_protocol);
2323 }
2324
2325 void
2326 proto_reg_handoff_sbus(void)
2327 {
2328        dissector_handle_t sbus_handle;
2329
2330        sbus_handle = new_create_dissector_handle(dissect_sbus, proto_sbus);
2331        dissector_add_uint("udp.port", 5050, sbus_handle);
2332 }
2333
2334 /*
2335  * Editor modelines
2336  *
2337  * Local Variables:
2338  * c-basic-offset: 7
2339  * tab-width: 8
2340  * indent-tabs-mode: nil
2341  * End:
2342  *
2343  * ex: set shiftwidth=7 tabstop=8 expandtab:
2344  * :indentSize=7:tabSize=8:noTabs=true:
2345  */