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