proto_register/proto_reg_handoff; Various small cleanup and bug-fixing
[obnox/wireshark/wip.git] / plugins / sbus / packet-sbus.c
1 /* packet-sbus.c
2  * Routines for Ether-S-Bus dissection 
3  * Copyright 2006, Christian Durrer <christian.durrer@sensemail.ch>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  * 
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * 
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <glib.h>
34 #include <gmodule.h>
35 #include <epan/packet.h>
36 #include <epan/conversation.h>
37
38 /* Number of entries in the memeory chunk array (for conversation)*/
39 #define SBUS_MEMCHUNKSIZE      20
40 /* Attribut values*/
41 #define SBUS_REQUEST                 0x00
42 #define SBUS_RESPONSE                 0x01
43 #define SBUS_ACKNAK                          0x02
44
45 /*SBus command codes*/
46 #define SBUS_RD_COUNTER                             0x00
47 #define SBUS_RD_DISPLAY_REGISTER              0x01
48 #define SBUS_RD_FLAG                               0x02
49 #define SBUS_RD_INPUT                              0x03
50 #define SBUS_RD_RTC                                0x04
51 #define SBUS_RD_OUTPUT                      0x05
52 #define SBUS_RD_REGISTER                            0x06
53 #define SBUS_RD_TIMER                              0x07
54 #define SBUS_WR_COUNTER                             0x0A
55 #define SBUS_WR_FLAG                               0x0B
56 #define SBUS_WR_RTC                                0x0C
57 #define SBUS_WR_OUTPUT                      0x0D
58 #define SBUS_WR_REGISTER                            0x0E
59 #define SBUS_WR_TIMER                              0x0F
60 #define SBUS_RDWR_MULTI_MEDIAS               0x13
61 #define SBUS_RD_PCD_STATUS_CPU0               0x14
62 #define SBUS_RD_PCD_STATUS_CPU1               0x15
63 #define SBUS_RD_PCD_STATUS_CPU2               0x16
64 #define SBUS_RD_PCD_STATUS_CPU3               0x17
65 #define SBUS_RD_PCD_STATUS_CPU4               0x18
66 #define SBUS_RD_PCD_STATUS_CPU5               0x19
67 #define SBUS_RD_PCD_STATUS_CPU6               0x1A
68 #define SBUS_RD_PCD_STATUS_OWN        0x1B
69 #define SBUS_RD_SBUS_STN_NBR                 0x1D
70 #define SBUS_RD_USER_MEMORY                  0x1E
71 #define SBUS_RD_PROGRAM_LINE                 0x1F
72 #define SBUS_RD_PROGRAM_VERSION               0x20
73 #define SBUS_RD_TEXT                               0x21
74 #define SBUS_RD_ACTIVE_TRANSITION             0x22
75 #define SBUS_WR_USER_MEMORY                  0x23
76 #define SBUS_WR_PROGRAM_LINE                 0x24
77 #define SBUS_WR_TEXT                               0x25
78 #define SBUS_RUN_PROCEDURE_CPU0               0x28
79 #define SBUS_RUN_PROCEDURE_CPU1               0x29
80 #define SBUS_RUN_PROCEDURE_CPU2               0x2A
81 #define SBUS_RUN_PROCEDURE_CPU3               0x2B
82 #define SBUS_RUN_PROCEDURE_CPU4               0x2C
83 #define SBUS_RUN_PROCEDURE_CPU5               0x2D
84 #define SBUS_RUN_PROCEDURE_CPU6               0x2E
85 #define SBUS_RUN_PROCEDURE_OWN        0x2F
86 #define SBUS_RUN_PROCEDURE_ALL        0x30
87 #define SBUS_RESTART_COLD_CPU1               0x32
88 #define SBUS_RESTART_COLD_CPU2               0x33
89 #define SBUS_RESTART_COLD_CPU3               0x34
90 #define SBUS_RESTART_COLD_CPU4               0x35
91 #define SBUS_RESTART_COLD_CPU5               0x36
92 #define SBUS_RESTART_COLD_CPU6               0x37
93 #define SBUS_RESTART_COLD_OWN                0x38
94 #define SBUS_RESTART_COLD_ALL                0x39
95 #define SBUS_STOP_PROCEDURE_CPU0              0x3C
96 #define SBUS_STOP_PROCEDURE_CPU1              0x3D
97 #define SBUS_STOP_PROCEDURE_CPU2              0x3E
98 #define SBUS_STOP_PROCEDURE_CPU3              0x3F
99 #define SBUS_STOP_PROCEDURE_CPU4              0x40
100 #define SBUS_STOP_PROCEDURE_CPU5              0x41
101 #define SBUS_STOP_PROCEDURE_CPU6              0x42
102 #define SBUS_STOP_PROCEDURE_OWN               0x43
103 #define SBUS_STOP_PROCEDURE_ALL               0x44
104 #define SBUS_RD_STATUSFLAG_ACCU               0x46
105 #define SBUS_RD_BYTE                               0x47
106 #define SBUS_RD_HALT_FAILURE_REG              0x48
107 #define SBUS_RD_INDEX_REGISTER               0x49
108 #define SBUS_RD_INSTRUCTION_POINTER           0x4A
109 #define SBUS_FIND_HISTORY                           0x4B
110 #define SBUS_WR_STATUSFLAG_ACCU               0x50
111 #define SBUS_WR_BYTE                               0x51
112 #define SBUS_WR_INDEX_REGISTER               0x52
113 #define SBUS_WR_INSTRUCTION_POINTER           0x53
114 #define SBUS_CLEAR_ALL                             0x5A
115 #define SBUS_CLEAR_FLAGS                            0x5B
116 #define SBUS_CLEAR_OUTPUTS                   0x5C
117 #define SBUS_CLEAR_REGISTERS                 0x5D
118 #define SBUS_CLEAR_TIMERS                           0x5E
119 #define SBUS_RESTART_WARM_CPU1        0x64
120 #define SBUS_RESTART_WARM_CPU2        0x65
121 #define SBUS_RESTART_WARM_CPU3        0x66
122 #define SBUS_RESTART_WARM_CPU4        0x67
123 #define SBUS_RESTART_WARM_CPU5        0x68
124 #define SBUS_RESTART_WARM_CPU6        0x69
125 #define SBUS_RESTART_WARM_OWN         0x6A
126 #define SBUS_RESTART_WARM_ALL                0x6B
127 #define SBUS_CHANGE_BLOCK                           0x6E
128 #define SBUS_CLEAR_HISTORY_FAILURE            0x6F
129 #define SBUS_DELETE_PROGRAM_LINE              0x70
130 #define SBUS_GO_CONDITIONAL                  0x71
131 #define SBUS_INSERT_PROGRAM_LINE              0x72
132 #define SBUS_LOCAL_CYCLE                            0x73
133 #define SBUS_ALL_CYCLES                             0x74
134 #define SBUS_MAKE_TEXT                      0x75
135 #define SBUS_EXECUTE_SINGLE_INSTR             0x76
136 #define SBUS_SINGLE_STEP                            0x77
137 #define SBUS_XOB_17_INTERRUPT                0x82
138 #define SBUS_XOB_18_INTERRUPT                0x83
139 #define SBUS_XOB_19_INTERRUPT                0x84
140 #define SBUS_RD_HANGUP_TIMEOUT        0x91
141 #define SBUS_RD_DATA_BLOCK                          0x96
142 #define SBUS_WR_DATA_BLOCK                   0x97
143 #define SBUS_MAKE_DATA_BLOCK                 0x98
144 #define SBUS_CLEAR_DATA_BLOCK                0x99
145 #define SBUS_CLEAR_TEXT                             0x9A
146 #define SBUS_RD_BLOCK_ADDRESSES               0x9B
147 #define SBUS_RD_BLOCK_SIZES                  0x9C
148 #define SBUS_RD_CURRENT_BLOCK                0x9D
149 #define SBUS_RD_CALL_STACK                          0x9E
150 #define SBUS_RD_DBX                                0x9F
151 #define SBUS_RD_USER_EEPROM_REGISTER   0xA1
152 #define SBUS_WR_USER_EEPROM_REGISTER   0xA3
153 #define SBUS_ERASE_FLASH                            0xA5
154 #define SBUS_RESTART_COLD_FLAG               0xA6
155 #define SBUS_WR_SYSTEM_BUFFER                0xA7
156 #define SBUS_RD_SYSTEM_BUFFER                0xA8
157 #define SBUS_RD_WR_PCD_BLOCK                 0xA9
158 #define SBUS_GET_DIAGNOSTIC                  0xAA
159 #define SBUS_RD_SYSTEM_INFORMATION            0xAB
160 #define SBUS_CHANGE_BLOCKS_ON_RUN             0xAC
161 #define SBUS_FLASHCARD_TELEGRAM               0xAD
162 #define SBUS_DOWNLOAD_FIRMWARE        0xAE
163 #define SBUS_WEB_SERVER_SERIAL_COMM    0xAF
164
165 /* Bitfield in the arithmetic flags and accu*/
166 #define F_ACCU      (1<<0)           /* Accumulator of PCD             */
167 #define F_ERROR     (1<<1)          /* Error flag of PCD              */
168 #define F_NEGATIVE  (1<<2)        /* Negative arithmetic status flag*/
169 #define F_ZERO      (1<<3)          /* Zero arithmetic status flag    */
170
171 /* Bitfield in the system information*/
172 /*#define F_EMPTY      (1<<0)          always 0             */
173 #define F_MEMSIZE      (1<<1)  /* Memory size information*/
174 #define F_TRACE        (1<<2)         /* Trace buffer feature              */
175 #define F_INFO_B1      (1<<3)         /* EEPROM information of slot B1*/
176 #define F_INFO_B2      (1<<4)         /* EEPROM information of slot B2*/
177 #define F_PGU_BAUD (1<<5)             /* PGU baudrate can be switched*/
178
179 /* Initialize the protocol and registered fields */
180 static int proto_sbus = -1;
181 static int hf_sbus_length = -1;
182 static int hf_sbus_version = -1;
183 static int hf_sbus_protocol = -1;
184 static int hf_sbus_sequence = -1;
185 static int hf_sbus_attribut = -1;
186 static int hf_sbus_dest = -1;
187 static int hf_sbus_address = -1;
188 static int hf_sbus_command = -1;
189 static int hf_sbus_command_extension = -1;
190 static int hf_sbus_rcount = -1;
191 static int hf_sbus_wcount = -1;
192 static int hf_sbus_wcount_calculated = -1;
193 static int hf_sbus_fio_count = -1;
194 static int hf_sbus_addr_rtc = -1;
195 static int hf_sbus_addr_iof = -1;
196 static int hf_sbus_addr_eeprom = -1;
197 static int hf_sbus_addr_prog = -1;
198 static int hf_sbus_addr_68k = -1;
199 static int hf_sbus_block_type = -1;
200 static int hf_sbus_block_nr = -1;
201 static int hf_sbus_nbr_elements = -1;
202 static int hf_sbus_display_register = -1;
203 static int hf_sbus_data_rtc = -1;
204 static int hf_sbus_data_byte = -1;
205 static int hf_sbus_data_byte_hex = -1;
206 static int hf_sbus_data_iof = -1;
207 static int hf_sbus_cpu_type = -1;
208 static int hf_sbus_fw_version = -1;
209 static int hf_sbus_sysinfo_nr = -1;
210 static int hf_sbus_sysinfo0_1 = -1;
211 static int hf_sbus_sysinfo0_2 = -1;
212 static int hf_sbus_sysinfo0_3 = -1;
213 static int hf_sbus_sysinfo0_4 = -1;
214 static int hf_sbus_sysinfo0_5 = -1;
215 static int hf_sbus_sysinfo_length = -1;
216 static int hf_sbus_f_module_type = -1;
217 static int hf_sbus_harware_version = -1;
218 static int hf_sbus_hardware_modification = -1;
219 static int hf_sbus_various = -1;
220 static int hf_sbus_acknackcode = -1;
221 static int hf_sbus_cpu_status = -1;
222 static int hf_sbus_week_day = -1;
223 static int hf_sbus_date = -1;
224 static int hf_sbus_time = -1;
225 static int hf_sbus_crc = -1;
226 static int hf_sbus_crc_bad = -1;
227 static int hf_sbus_flags_accu = -1;
228 static int hf_sbus_flags_error = -1;
229 static int hf_sbus_flags_negative = -1;
230 static int hf_sbus_flags_zero = -1;
231 /* Web server telegram */
232 static int hf_sbus_web_size = -1;
233 static int hf_sbus_web_aid = -1;
234 static int hf_sbus_web_seq = -1;
235
236 /* Initialize the subtree pointers */
237 static gint ett_sbus = -1;
238 static gint ett_sbus_ether = -1;
239 static gint ett_sbus_data = -1;
240
241 /* True/False strings*/
242 static const true_false_string tfs_sbus_flags= {
243        "Is high",
244        "Is low"
245 };
246
247 static const true_false_string tfs_sbus_present= {
248        "Is present",
249        "Is not present"
250 };
251
252 /* value to string definitions*/
253 /* telegram types*/
254 static const value_string sbus_att_vals[] = {
255        {0, "Request"},
256        {1, "Response"},
257        {2, "ACK/NAK"},
258        {0, NULL}
259 };
260 /* Block types*/
261 static const value_string sbus_block_types[] = {
262        {0x00, "COB"},
263        {0x01, "XOB"},
264        {0x02, "PB"},
265        {0x03, "FB"},
266        {0x04, "ST"},
267        {0x05, "TR"},
268        {0x04, "TEXT"},
269        {0x05, "DB"},
270        {0x08, "SB"},
271        {0, NULL}
272 };
273 /* ACK NAK values*/
274 static const value_string sbus_CPU_status[] = {
275        {0x43, "C"},
276        {0x44, "D"},
277        {0x48, "Halt"},
278        {0x52, "Run"},
279        {0x53, "Stop"},
280        {0x58, "X, Exceptional Intermediate Status (MODEMS+)"},
281        {0, NULL}
282 };
283 /* CPU status*/
284 static const value_string sbus_ack_nak_vals[] = {
285        {0, "ACK (Acknowledged)"},
286        {1, "NAK, no reason specified"},
287        {2, "NAK, because of password"},
288        {3, "NAK, PGU port is in reduced protocol"},
289        {4, "NAK, PGU port is already used"},
290        {0, NULL}
291 };
292 /* S-Bus commands*/
293 static const value_string sbus_command_vals[] = {
294        {0x00, "Read counter(s)"},
295        {0x01, "Read display register"},
296        {0x02, "Read flag(s)"},
297        {0x03, "Read input(s)"},
298        {0x04, "Read real time clock"},
299        {0x05, "Read output(s)"},
300        {0x06, "Read register(s)"},
301        {0x07, "Read timer(s)"},
302        {0x0A, "Write counter(s)"},
303        {0x0B, "Write flag(s)"},
304        {0x0C, "Write real time clock"},
305        {0x0D, "Write output(s)"},
306        {0x0E, "Write register(s)"},
307        {0x0F, "Write timer(s)"},
308        {0x14, "Read PCD status, CPU 0"},
309        {0x15, "Read PCD status, CPU 1"},
310        {0x16, "Read PCD status, CPU 2"},
311        {0x17, "Read PCD status, CPU 3"},
312        {0x18, "Read PCD status, CPU 4"},
313        {0x19, "Read PCD status, CPU 5"},
314        {0x1A, "Read PCD status, CPU 6"},
315        {0x1B, "Read PCD status (own)"},
316        {0x1D, "Read S-Bus station number"},
317        {0x1E, "Read user memory*"},
318        {0x1F, "Read program line*"},
319        {0x20, "Read firmware version"},
320        {0x21, "Read text*"},
321        {0x22, "Read active transition*"},
322        {0x23, "Write user memeory*"},
323        {0x24, "Write program line*"},
324        {0x25, "Write text*"},
325        {0x28, "Run procedure*, CPU 0"},
326        {0x29, "Run procedure*, CPU 1"},
327        {0x2A, "Run procedure*, CPU 2"},
328        {0x2B, "Run procedure*, CPU 3"},
329        {0x2C, "Run procedure*, CPU 4"},
330        {0x2D, "Run procedure*, CPU 5"},
331        {0x2E, "Run procedure*, CPU 6"},
332        {0x2F, "Run procedure* (own CPU)"},
333        {0x30, "Run procedure* (All CPUs)"},
334        {0x32, "Restart cold CPU 1*"},
335        {0x33, "Restart cold CPU 2*"},
336        {0x34, "Restart cold CPU 3*"},
337        {0x35, "Restart cold CPU 4*"},
338        {0x36, "Restart cold CPU 5*"},
339        {0x37, "Restart cold CPU 6*"},
340        {0x38, "Restart cold own CPU*"},
341        {0x39, "Restart cold all CPUs*"},
342        {0x3C, "Stop procedure*, CPU 0"},
343        {0x3D, "Stop procedure*, CPU 1"},
344        {0x3E, "Stop procedure*, CPU 2"},
345        {0x3F, "Stop procedure*, CPU 3"},
346        {0x40, "Stop procedure*, CPU 4"},
347        {0x41, "Stop procedure*, CPU 5"},
348        {0x42, "Stop procedure*, CPU 6"},
349        {0x43, "Stop procedure*, (own CPU)"},
350        {0x44, "Stop procedure*, (All CPUs)"},
351        {0x46, "Read arithmetic status and ACCU*"},
352        {0x47, "Read byte"},
353        {0x48, "Read halt failure register*"},
354        {0x49, "Read index register*"},
355        {0x4A, "Read instruction pointer*"},
356        {0x4B, "Find history*"},
357        {0x50, "Write arithmetic staus and ACCU*"},
358        {0x51, "Write byte*"},
359        {0x52, "Write index register"},
360        {0x53, "Write instruction pointer*"},
361        {0x5A, "Clear all (F, O, R, T)*"},
362        {0x5B, "Clear flags*"},
363        {0x5C, "Clear outputs*"},
364        {0x5D, "Clear registers*"},
365        {0x5E, "Clear timers*"},
366        {0x64, "Restart warm CPU 1*"},
367        {0x65, "Restart warm CPU 2*"},
368        {0x66, "Restart warm CPU 3*"},
369        {0x67, "Restart warm CPU 4*"},
370        {0x68, "Restart warm CPU 5*"},
371        {0x69, "Restart warm CPU 6*"},
372        {0x6A, "Restart warm (own CPU)*"},
373        {0x6B, "Restart warm (All CPUs)*"},
374        {0x6E, "Change block*"},
375        {0x6F, "Clear history failure*"},
376        {0x70, "Delete program line*"},
377        {0x71, "Go conditional*"},
378        {0x72, "Insert program line*"},
379        {0x73, "Local cycles*"},
380        {0x74, "All cycles*"},
381        {0x75, "Make text*"},
382        {0x76, "Execute single instruction*"},
383        {0x77, "Single step*"},
384        {0x82, "XOB 17 interrupt"},
385        {0x83, "XOB 18 interrupt"},
386        {0x84, "XOB 19 interrupt"},
387        {0x91, "Read hangup timeout"},
388        {0x96, "Read data block"},
389        {0x97, "Write data block"},
390        {0x98, "Make data block*"},
391        {0x99, "Clear data block*"},
392        {0x9A, "Clear text*"},
393        {0x9B, "Read block address"},
394        {0x9C, "Read block sizes"},
395        {0x9D, "Read current block*"},
396        {0x9E, "Read call stack*"},
397        {0x9F, "Read DBX"},
398        {0xA1, "Read user EEPROM register"},
399        {0xA3, "Write user EEPROM register"},
400        {0xA5, "Erase flash*"},
401        {0xA6, "Restart cold flag*"},
402        {0xA7, "Write system buffer"},
403        {0xA8, "Read system buffer"},
404        {0xAA, "Get diagnostic*"},
405        {0xAB, "Read system information*"},
406        {0xAC, "Changes blocks on run*"},
407        {0xAD, "Flashcard telegram*"},
408        {0xAE, "Download FW*"},
409        {0xAF, "Web server serial communication*"},
410        {0, NULL}
411 };
412
413 static const value_string webserver_aid_vals[] = {
414        {0x01, "Partitial request"},
415        {0x02, "Request end"},
416        {0x07, "Get Data"},
417        {0x10, "Transfer OK"},
418        {0x11, "Partial answer"},
419        {0x12, "Last part of answer"},
420        {0x13, "Server not ready"},
421        {0, NULL}
422 };
423
424 static const guint crc_table[] = {
425        0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef,
426        0x1231,0x0210,0x3273,0x2252,0x52b5,0x4294,0x72f7,0x62d6,0x9339,0x8318,0xb37b,0xa35a,0xd3bd,0xc39c,0xf3ff,0xe3de,
427        0x2462,0x3443,0x0420,0x1401,0x64e6,0x74c7,0x44a4,0x5485,0xa56a,0xb54b,0x8528,0x9509,0xe5ee,0xf5cf,0xc5ac,0xd58d,
428        0x3653,0x2672,0x1611,0x0630,0x76d7,0x66f6,0x5695,0x46b4,0xb75b,0xa77a,0x9719,0x8738,0xf7df,0xe7fe,0xd79d,0xc7bc,
429        0x48c4,0x58e5,0x6886,0x78a7,0x0840,0x1861,0x2802,0x3823,0xc9cc,0xd9ed,0xe98e,0xf9af,0x8948,0x9969,0xa90a,0xb92b,
430        0x5af5,0x4ad4,0x7ab7,0x6a96,0x1a71,0x0a50,0x3a33,0x2a12,0xdbfd,0xcbdc,0xfbbf,0xeb9e,0x9b79,0x8b58,0xbb3b,0xab1a,
431        0x6ca6,0x7c87,0x4ce4,0x5cc5,0x2c22,0x3c03,0x0c60,0x1c41,0xedae,0xfd8f,0xcdec,0xddcd,0xad2a,0xbd0b,0x8d68,0x9d49,
432        0x7e97,0x6eb6,0x5ed5,0x4ef4,0x3e13,0x2e32,0x1e51,0x0e70,0xff9f,0xefbe,0xdfdd,0xcffc,0xbf1b,0xaf3a,0x9f59,0x8f78,
433        0x9188,0x81a9,0xb1ca,0xa1eb,0xd10c,0xc12d,0xf14e,0xe16f,0x1080,0x00a1,0x30c2,0x20e3,0x5004,0x4025,0x7046,0x6067,
434        0x83b9,0x9398,0xa3fb,0xb3da,0xc33d,0xd31c,0xe37f,0xf35e,0x02b1,0x1290,0x22f3,0x32d2,0x4235,0x5214,0x6277,0x7256,
435        0xb5ea,0xa5cb,0x95a8,0x8589,0xf56e,0xe54f,0xd52c,0xc50d,0x34e2,0x24c3,0x14a0,0x0481,0x7466,0x6447,0x5424,0x4405,
436        0xa7db,0xb7fa,0x8799,0x97b8,0xe75f,0xf77e,0xc71d,0xd73c,0x26d3,0x36f2,0x0691,0x16b0,0x6657,0x7676,0x4615,0x5634,
437        0xd94c,0xc96d,0xf90e,0xe92f,0x99c8,0x89e9,0xb98a,0xa9ab,0x5844,0x4865,0x7806,0x6827,0x18c0,0x08e1,0x3882,0x28a3,
438        0xcb7d,0xdb5c,0xeb3f,0xfb1e,0x8bf9,0x9bd8,0xabbb,0xbb9a,0x4a75,0x5a54,0x6a37,0x7a16,0x0af1,0x1ad0,0x2ab3,0x3a92,
439        0xfd2e,0xed0f,0xdd6c,0xcd4d,0xbdaa,0xad8b,0x9de8,0x8dc9,0x7c26,0x6c07,0x5c64,0x4c45,0x3ca2,0x2c83,0x1ce0,0x0cc1,
440        0xef1f,0xff3e,0xcf5d,0xdf7c,0xaf9b,0xbfba,0x8fd9,0x9ff8,0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,0x3eb2,0x0ed1,0x1ef0
441 };
442
443 /* Conversion values passing structure*/
444 typedef struct {
445        guint32 conversation;  /*Conversation ID*/
446        guint16 sequence;             /*Sequence number of request telegram*/
447 } sbus_request_key;
448
449 typedef struct {
450        guint8 cmd_code;              /*command code from request*/
451        guint8 count;                /*rcount value*/
452        guint8 sysinfo;                      /*system information number (or command in other telegrams)*/
453 } sbus_request_val;
454
455 /* The GMemChunk base structure (for conversations)*/
456 static GHashTable *sbus_request_hash = NULL;
457 static GMemChunk *sbus_request_keys = NULL;
458 static GMemChunk *sbus_request_vals = NULL;
459
460 static guint crc_calc (guint crc, guint val) 
461 {
462        int index;
463        guint ncrc;
464        
465        index = (((crc >> 8) ^ val) & 0xff);
466        ncrc = crc_table[index] ^ ((crc << 8) & 0xffff);
467
468        return ncrc;
469 }
470
471 /* Hash functions*/
472 static gint sbus_equal(gconstpointer v, gconstpointer w)
473 {
474        sbus_request_key *v1 = (sbus_request_key *)v;
475        sbus_request_key *v2 = (sbus_request_key *)w;
476
477        if (v1->conversation == v2->conversation &&
478            v1->sequence == v2->sequence) {
479               return 1;
480        }
481        return 0;
482 }
483
484 static guint sbus_hash(gconstpointer v)
485 {
486        sbus_request_key *key = (sbus_request_key *)v;
487        guint val;
488        val = key->conversation + key->sequence;
489        return val;
490 }
491
492 /*Protocol initialisation*/
493 static void sbus_init_protocol(void){
494        if (sbus_request_hash){
495               g_hash_table_destroy(sbus_request_hash);
496        }
497        if (sbus_request_keys){
498               g_mem_chunk_destroy(sbus_request_keys);
499        }      
500        if (sbus_request_vals){
501               g_mem_chunk_destroy(sbus_request_vals);
502        }
503        sbus_request_hash = g_hash_table_new(sbus_hash, sbus_equal);
504        sbus_request_keys = g_mem_chunk_new("sbus_request_keys",
505               sizeof(sbus_request_key),
506               SBUS_MEMCHUNKSIZE * sizeof(sbus_request_key),
507               G_ALLOC_AND_FREE);
508        sbus_request_vals = g_mem_chunk_new("sbus_request_vals",
509               sizeof(sbus_request_val),
510               SBUS_MEMCHUNKSIZE * sizeof(sbus_request_val),
511               G_ALLOC_AND_FREE);
512 }
513
514 /* check whether the packet looks like SBUS or not */
515 static gboolean
516 is_sbus_pdu(tvbuff_t *tvb)
517 {
518         guint32 length;
519
520         /* we need at least 8 bytes to determine whether this is sbus or
521            not
522         */
523         if(tvb_length(tvb)<8){
524                 return FALSE;
525         }
526
527         /* the length must be >= 8 bytes to accomodate the header,
528            it also must be <65536 to fit inside a udp packet
529          */
530         length=tvb_get_ntohl(tvb, 0);
531         if ( (length<8) || (length>65535) ) {
532                 return FALSE;
533         }
534         if (tvb_reported_length(tvb) != length) {
535                 return FALSE;
536         }
537
538         /* XXX */
539         /* We should also test version and protocol    but that requires
540            someone to look at the specification for SBUS
541         */
542         
543         return TRUE;
544 }
545
546 /*Dissect the telegram*/
547 static int
548 dissect_sbus(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
549 {
550
551 /* Set up structures needed to add the protocol subtree and manage it */
552        proto_item *ti, *et, *dt, *hi;
553        proto_tree *sbus_tree, *ethsbus_tree, *sbusdata_tree;
554        
555        gint i;        /*for CRC calculation*/
556        gint j;        /*for CRC calculation*/
557        gint offset;
558        gint sbus_eth_len;
559        guint sbus_crc_calc;
560        guint8 sbus_version;
561        guint8 sbus_attribut;
562        guint8 sbus_media_cnt;
563        guint8 sbus_fio_cnt;
564        guint8 sbus_cmd_code;
565        guint8 sbus_web_size;
566        guint8 sbus_web_aid;
567        guint8 sbus_web_seq;
568        guint8 sbus_sysinfo_number;
569        guint32 sbus_binarymasked;
570        guint32 sbus_binaries;
571        guint16 sbus_ack_code;
572        guint32 sbus_show_bin;
573        guint32 sbus_helper;
574        guint32 sbus_helper1;
575        guint32 sbus_helper2;
576        char *tmp_string;
577
578 /* Set up conversations*/
579        conversation_t *conversation = NULL;
580        sbus_request_key request_key, *new_request_key;
581        sbus_request_val *request_val = NULL;
582
583        /* does this look like an sbus pdu? */       
584        if(!is_sbus_pdu(tvb)){
585            return 0;
586        }
587
588        conversation = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst,
589               pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
590        
591        if (!conversation) {
592               /* create new conversation*/
593               conversation = conversation_new(pinfo->fd->num, &pinfo->src, &pinfo->dst,
594                     pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
595        }
596        
597        request_key.conversation = conversation->index;
598        request_key.sequence = tvb_get_ntohs(tvb,6);
599        
600        request_val = (sbus_request_val *) g_hash_table_lookup(sbus_request_hash, 
601                             &request_key);
602        /* Only allocate a new hash element when it's a request*/
603        sbus_attribut = tvb_get_guint8(tvb,8); 
604         
605        if ( !request_val && sbus_attribut == 0 ) {/* request telegram */
606               new_request_key = g_mem_chunk_alloc(sbus_request_keys);
607               *new_request_key = request_key;
608               
609               request_val = g_mem_chunk_alloc(sbus_request_vals);
610               request_val->cmd_code=tvb_get_guint8(tvb,10);
611               
612               if (((request_val->cmd_code) == SBUS_RD_USER_EEPROM_REGISTER) || 
613                   ((request_val->cmd_code) == SBUS_WR_USER_EEPROM_REGISTER)) {
614                      request_val->count=((tvb_get_guint8(tvb,12))+1);
615               } else {
616                      request_val->count=((tvb_get_guint8(tvb,11))+1);
617               }
618               
619               /*Enter system info*/
620               if ((request_val->cmd_code) == SBUS_RD_SYSTEM_INFORMATION) {
621                      request_val->sysinfo=(tvb_get_guint8(tvb,12));
622               } else {
623                      request_val->sysinfo=0x0;
624               }
625               
626               g_hash_table_insert(sbus_request_hash, new_request_key, request_val); 
627        }
628 /* End of attaching data to hash table*/
629        
630 /* Make entries in Protocol column and Info column on summary display */
631        if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
632               col_set_str(pinfo->cinfo, COL_PROTOCOL, "S-Bus");
633
634        if (check_col(pinfo->cinfo, COL_INFO)) 
635               col_clear(pinfo->cinfo, COL_INFO);
636        offset = 0;
637
638        if (check_col(pinfo->cinfo, COL_INFO)) {
639               switch (sbus_attribut){
640                      case SBUS_REQUEST:     
641                             sbus_cmd_code = tvb_get_guint8(tvb,10);
642                                           
643                             /* Special treatment of web server request 
644                              * as is is very helpful to see more information in the packetlist */
645                             if (sbus_cmd_code == SBUS_WEB_SERVER_SERIAL_COMM) {
646                                    sbus_web_aid = tvb_get_guint8(tvb,12);
647                                    sbus_web_seq = tvb_get_guint8(tvb,13);
648                                    
649                                    col_add_fstr(pinfo->cinfo, COL_INFO,
650                                           "Web Server Request: %s (Seq No: %d)", 
651                                           val_to_str(sbus_web_aid,
652                                                  webserver_aid_vals, "Unknown Request!"), 
653                                                  sbus_web_seq);
654                             } else {
655                                    /* All other requests */
656                                    col_add_fstr(pinfo->cinfo, COL_INFO,
657                                           "Request: %s", val_to_str(sbus_cmd_code,
658                                           sbus_command_vals, "Unknown Command!"));
659                             }
660                             break;
661                                    
662                      case SBUS_RESPONSE:    
663                             /* Special treatment of web server request 
664                              * as is is very helpful to see more information in the packetlist */
665                             if (request_val && ((request_val->cmd_code) == SBUS_WEB_SERVER_SERIAL_COMM)) { 
666                                    sbus_web_size = tvb_get_guint8(tvb,9);
667                                    sbus_web_aid = tvb_get_guint8(tvb,10);
668                                    col_add_fstr(pinfo->cinfo, COL_INFO,
669                                           "Response: %s", 
670                                           val_to_str(sbus_web_aid,
671                                                  webserver_aid_vals, "Unknown Request!"));
672                                    if (sbus_web_size > 1) {
673                                           sbus_web_seq = tvb_get_guint8(tvb,11);
674                                           col_append_fstr(pinfo->cinfo, COL_INFO,
675                                               " (Seq No: %d)",
676                                               sbus_web_seq);
677                                    }
678                             } else {
679                                    col_set_str(pinfo->cinfo, COL_INFO, "Response");
680                             }
681                             break;
682                                    
683                      case SBUS_ACKNAK:      
684                             sbus_ack_code = tvb_get_ntohs(tvb,9);
685                             col_add_fstr(pinfo->cinfo, COL_INFO,
686                                    "%s", val_to_str(sbus_ack_code,
687                                    sbus_ack_nak_vals, 
688                                    "Unknown NAK response code!"));
689                             break;
690                             
691                      default: 
692                             col_set_str(pinfo->cinfo, COL_INFO, "Unknown attribute");
693                             break;
694               }
695
696        }      
697 /* create display subtree for the protocol */
698        if (tree) { 
699
700               ti = proto_tree_add_item(tree, proto_sbus, tvb, offset, -1, FALSE);
701               sbus_tree = proto_item_add_subtree(ti, ett_sbus);
702               
703 /*Add subtree for Ether-S-Bus header*/
704               et = proto_tree_add_text(sbus_tree, tvb, offset, 8, "Ether-S-Bus header");
705               ethsbus_tree = proto_item_add_subtree(et, ett_sbus_ether);
706
707 /* add an item to the subtree*/
708               sbus_eth_len = tvb_get_ntohl(tvb,offset);
709               proto_tree_add_item(ethsbus_tree,
710                   hf_sbus_length, tvb, offset, 4, FALSE); 
711                   offset += 4;
712
713               sbus_version = tvb_get_guint8(tvb,offset);
714               proto_tree_add_item(ethsbus_tree,
715                   hf_sbus_version, tvb, offset, 1, FALSE); 
716                   offset += 1;
717
718               proto_tree_add_item(ethsbus_tree,
719                   hf_sbus_protocol, tvb, offset, 1, FALSE); 
720                   offset += 1;
721
722               proto_tree_add_item(ethsbus_tree,
723                   hf_sbus_sequence, tvb, offset, 2, FALSE); 
724                   offset += 2;
725
726 /* Continue adding stuff to the main tree*/
727               sbus_attribut = tvb_get_guint8(tvb,offset);
728               proto_tree_add_item(sbus_tree,
729                   hf_sbus_attribut, tvb, offset, 1, FALSE); 
730                   offset += 1;
731
732               if (sbus_attribut == SBUS_REQUEST) {
733                      proto_tree_add_item(sbus_tree,
734                          hf_sbus_dest, tvb, offset, 1, FALSE); 
735                          offset += 1;
736                      sbus_cmd_code = tvb_get_guint8(tvb,offset);
737                      proto_tree_add_item(sbus_tree,
738                          hf_sbus_command, tvb, offset, 1, FALSE); 
739                          offset += 1;
740                             
741                      switch (sbus_cmd_code) {
742                      /*Read Counter, Register or Timer*/
743                      case SBUS_RD_COUNTER:
744                      case SBUS_RD_REGISTER: 
745                      case SBUS_RD_TIMER:
746                             sbus_media_cnt = (tvb_get_guint8(tvb,offset))+1;
747                             proto_tree_add_uint(sbus_tree,
748                                 hf_sbus_rcount, tvb, offset, 1, sbus_media_cnt); 
749                             offset += 1;
750                             proto_tree_add_item(sbus_tree,
751                                 hf_sbus_addr_rtc, tvb, offset, 2, FALSE); 
752                             offset += 2;
753                             break;
754
755                      /*Read Flag, Input or Output*/
756                      case SBUS_RD_FLAG:
757                      case SBUS_RD_INPUT: 
758                      case SBUS_RD_OUTPUT:
759                             sbus_media_cnt = (tvb_get_guint8(tvb,offset))+1;
760                             proto_tree_add_uint(sbus_tree,
761                                 hf_sbus_rcount, tvb, offset, 1, sbus_media_cnt); 
762                             offset += 1;
763                             proto_tree_add_item(sbus_tree,
764                                 hf_sbus_addr_iof, tvb, offset, 2, FALSE); 
765                             offset += 2;
766                             break;
767
768                      /*Write Register Timer Counter*/
769                      case SBUS_WR_COUNTER:
770                      case SBUS_WR_REGISTER:
771                      case SBUS_WR_TIMER:
772                             sbus_media_cnt = (tvb_get_guint8(tvb,offset));
773                             sbus_media_cnt = ((sbus_media_cnt - 1)/4);
774                             proto_tree_add_uint(sbus_tree,
775                                 hf_sbus_wcount_calculated, tvb, offset,
776                                 1, sbus_media_cnt);
777                             proto_tree_add_item(sbus_tree,
778                                 hf_sbus_wcount, tvb, offset, 1, FALSE);
779                             offset += 1;
780                             proto_tree_add_item(sbus_tree,
781                                 hf_sbus_addr_rtc, tvb, offset, 2, FALSE); 
782                             offset += 2;
783                             /*Add subtree for Data*/
784                             dt = proto_tree_add_text(sbus_tree, tvb, offset,
785                                 ((sbus_media_cnt) * 4),"Data");
786                             
787                             sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
788                             for (i=((sbus_media_cnt)); i>0; i--) {
789                                    proto_tree_add_item(sbusdata_tree,
790                                        hf_sbus_data_rtc, tvb, offset, 
791                                        4, FALSE);
792                                    offset += 4;
793                             }
794                             break;
795
796                      /* Write flags and outputs*/
797                      case SBUS_WR_FLAG:
798                      case SBUS_WR_OUTPUT:
799                             sbus_media_cnt = (tvb_get_guint8(tvb,offset));
800                             sbus_media_cnt = (sbus_media_cnt - 2);
801                             proto_tree_add_uint(sbus_tree,
802                                 hf_sbus_wcount_calculated, tvb, offset,
803                                 1, sbus_media_cnt);
804                             proto_tree_add_item(sbus_tree,
805                                 hf_sbus_wcount, tvb, offset, 1, FALSE);
806                                 offset += 1;
807                             proto_tree_add_item(sbus_tree,
808                                 hf_sbus_addr_iof, tvb, offset, 2, FALSE); 
809                                 offset += 2;
810                             sbus_fio_cnt = (tvb_get_guint8(tvb,offset));
811                             sbus_fio_cnt = ((sbus_fio_cnt + 1));
812                             proto_tree_add_uint(sbus_tree,
813                                 hf_sbus_fio_count, tvb, offset, 1, sbus_fio_cnt); 
814                                 offset += 1;                  
815                             /*Add subtree for Data*/
816                             dt = proto_tree_add_text(sbus_tree, tvb, offset,
817                                 sbus_media_cnt,"Data");
818                             
819                             sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
820                             for (i=sbus_media_cnt; i>0; i--) {
821                                    sbus_helper = 1;
822                                    sbus_show_bin = 0;
823                                    sbus_binarymasked = 0x01; 
824                                    sbus_binaries = tvb_get_guint8(tvb, offset);
825                                    for (j=0; j<8; j++) {
826                                           if ((sbus_binarymasked & sbus_binaries) != 0) {
827                                                  sbus_show_bin = (sbus_show_bin + sbus_helper);
828                                           }
829                                           sbus_binarymasked = sbus_binarymasked<<1;
830                                           sbus_helper = 10 * sbus_helper; 
831                                    }
832                             
833                                    proto_tree_add_uint_format(sbusdata_tree,
834                                           hf_sbus_data_iof, tvb, offset, 1, sbus_show_bin,
835                                           "Binary data: %08u", sbus_show_bin); 
836                                    offset += 1;
837                             }
838                             break;
839
840                      /* Request: Write Real time clock*/
841                      case SBUS_WR_RTC:
842                             sbus_helper = tvb_get_guint8(tvb, (offset +5));  /*hours*/
843                             sbus_helper1 = tvb_get_guint8(tvb, (offset +6)); /*minutes*/
844                             sbus_helper2 = tvb_get_guint8(tvb, (offset +7)); /*seconds*/
845                             proto_tree_add_text(sbus_tree, tvb, (offset +5), 3, 
846                                 "Time (HH:MM:SS): %02x:%02x:%02x", sbus_helper, sbus_helper1, sbus_helper2);
847                             sbus_helper = tvb_get_guint8(tvb, (offset +2));  /*year*/
848                             sbus_helper1 = tvb_get_guint8(tvb, (offset +3)); /*month*/
849                             sbus_helper2 = tvb_get_guint8(tvb, (offset +4)); /*day*/
850                             proto_tree_add_text(sbus_tree, tvb, (offset +2), 3, 
851                                 "Date (YY/MM/DD): %02x/%02x/%02x", sbus_helper, sbus_helper1, sbus_helper2);
852                             sbus_helper = tvb_get_guint8(tvb, (offset));  /*year-week*/
853                             sbus_helper1 = tvb_get_guint8(tvb, (offset +1)); /*week-day*/
854                             proto_tree_add_text(sbus_tree, tvb, offset, 2, 
855                                 "Calendar week: %x, Week day: %x", sbus_helper, sbus_helper1);
856                             /*Add subtree for Data*/
857                             dt = proto_tree_add_text(sbus_tree, tvb, offset, 
858                                 8, "Clock data");
859                             sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
860                             
861                             proto_tree_add_item(sbusdata_tree,
862                                 hf_sbus_week_day, tvb, offset, 2, FALSE); 
863                             offset += 2;
864                             proto_tree_add_item(sbusdata_tree,
865                                 hf_sbus_date, tvb, offset, 3, FALSE); 
866                             offset += 3;
867                             proto_tree_add_item(sbusdata_tree,
868                                 hf_sbus_time, tvb, offset, 3, FALSE); 
869                             offset += 3;
870                             break;
871                      
872                      /* Read user memory or program line*/
873                      case SBUS_RD_USER_MEMORY:
874                      case SBUS_RD_PROGRAM_LINE:
875                             sbus_media_cnt = (tvb_get_guint8(tvb,offset))+1;
876                             proto_tree_add_uint(sbus_tree,
877                                 hf_sbus_rcount, tvb, offset, 1, sbus_media_cnt); 
878                             offset += 1;
879                             proto_tree_add_item(sbus_tree,
880                                 hf_sbus_addr_prog, tvb, offset, 3, FALSE); 
881                             offset += 3;
882                             break;
883                      
884                      /*Write user memory*/
885                      case SBUS_WR_USER_MEMORY:
886                             sbus_media_cnt = (tvb_get_guint8(tvb,offset));
887                             sbus_media_cnt = ((sbus_media_cnt - 2)/4);
888                             proto_tree_add_uint(sbus_tree,
889                                 hf_sbus_wcount_calculated, tvb, offset,
890                                 1, sbus_media_cnt);
891                             proto_tree_add_item(sbus_tree,
892                                 hf_sbus_wcount, tvb, offset, 1, FALSE);
893                                 offset += 1;
894                             proto_tree_add_item(sbus_tree,
895                                 hf_sbus_addr_68k, tvb, offset, 3, FALSE); 
896                                 offset += 3;
897                             /*Add subtree for Data*/
898                             dt = proto_tree_add_text(sbus_tree, tvb, offset,
899                                 ((sbus_media_cnt) * 4),"Program lines");
900                             
901                             sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
902                             for (i=((sbus_media_cnt)); i>0; i--) {
903                                    proto_tree_add_item(sbusdata_tree,
904                                        hf_sbus_data_rtc, tvb, offset, 
905                                        4, FALSE); 
906                                    offset += 4;    
907                             
908                             }
909                             break;
910                      
911                      /* Read byte*/
912                      case SBUS_RD_BYTE:
913                             sbus_media_cnt = (tvb_get_guint8(tvb,offset))+1;
914                             proto_tree_add_uint(sbus_tree,
915                                 hf_sbus_rcount, tvb, offset, 1, sbus_media_cnt); 
916                             offset += 1;
917                             proto_tree_add_item(sbus_tree,
918                                 hf_sbus_addr_68k, tvb, offset, 3, FALSE); 
919                             offset += 3;
920                             break;
921                      
922                      /* Write byte */
923                      case SBUS_WR_BYTE:
924                             sbus_media_cnt = (tvb_get_guint8(tvb,offset));
925                             sbus_media_cnt = (sbus_media_cnt - 2);
926                             proto_tree_add_uint(sbus_tree,
927                                 hf_sbus_wcount_calculated, tvb, offset,
928                                 1, sbus_media_cnt);
929                             proto_tree_add_item(sbus_tree,
930                                 hf_sbus_wcount, tvb, offset, 1, FALSE);
931                                 offset += 1;
932                             proto_tree_add_item(sbus_tree,
933                                 hf_sbus_addr_68k, tvb, offset, 3, FALSE); 
934                                 offset += 3;
935                             /*Add subtree for Data*/
936                             dt = proto_tree_add_text(sbus_tree, tvb, offset,
937                                 ((sbus_media_cnt) * 4),"Data (bytes)");
938                             
939                             sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
940                             for (i=sbus_media_cnt; i>0; i--) {
941                                    proto_tree_add_item(sbusdata_tree,
942                                        hf_sbus_data_byte, tvb, offset, 
943                                        1, FALSE); 
944                                    offset += 1;    
945                             }
946                             break;
947                                           
948                      /*Read EEPROM register*/
949                      case SBUS_RD_USER_EEPROM_REGISTER:
950                             proto_tree_add_item(sbus_tree,
951                                 hf_sbus_command_extension, tvb, offset, 1, FALSE); 
952                             offset += 1;
953                             sbus_media_cnt = (tvb_get_guint8(tvb,offset))+1;
954                             proto_tree_add_uint(sbus_tree,
955                                 hf_sbus_rcount, tvb, offset, 1, sbus_media_cnt); 
956                             offset += 1;
957                             proto_tree_add_item(sbus_tree,
958                                 hf_sbus_addr_eeprom, tvb, offset, 2, FALSE); 
959                             offset += 2;
960                             break;
961               
962                      /*Request for reading system info*/
963                      /*Syinfo 05 is not implemented as no serial baud is possible*/
964                      case SBUS_RD_SYSTEM_INFORMATION:
965                             sbus_sysinfo_number = (tvb_get_guint8(tvb,13));
966                             proto_tree_add_item(sbus_tree,
967                                 hf_sbus_sysinfo_nr, tvb, offset, 1, FALSE); 
968                                 offset += 1;
969                             sbus_sysinfo_number = (tvb_get_guint8(tvb,14));
970                             proto_tree_add_item(sbus_tree,
971                                 hf_sbus_sysinfo_nr, tvb, offset, 1, FALSE); 
972                             offset += 1;
973                             break;
974                      
975                      /* WebServer Request */
976                      case SBUS_WEB_SERVER_SERIAL_COMM:
977                             sbus_web_size = tvb_get_guint8(tvb,offset);
978                             proto_tree_add_item(sbus_tree,
979                                 hf_sbus_web_size, tvb, offset,
980                                 1, sbus_web_size);
981                             offset += 1;
982
983                             sbus_web_aid = tvb_get_guint8(tvb,offset);
984                             proto_tree_add_item(sbus_tree,
985                                 hf_sbus_web_aid, tvb, offset,
986                                 1, sbus_web_aid);
987                             offset += 1;
988
989                             sbus_web_seq = tvb_get_guint8(tvb,offset);
990                             proto_tree_add_item(sbus_tree,
991                                 hf_sbus_web_seq, tvb, offset,
992                                 1, sbus_web_seq);
993                             offset += 1;
994                             
995                             if (sbus_web_size > 1) {
996                                    dt = proto_tree_add_text(sbus_tree, tvb, offset,
997                                           (sbus_web_size - 1),"Data (bytes)");
998                                    
999                                    sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
1000                                    for (i=sbus_web_size -1 ; i>0; i--) {
1001                                           proto_tree_add_item(sbusdata_tree,
1002                                               hf_sbus_data_byte, tvb, offset, 
1003                                               1, FALSE); 
1004                                           offset += 1;
1005                                    }      
1006                             }
1007                             break;
1008
1009 /*Inform that command was not dissected and add remaining length*/
1010                      default:
1011                             if (sbus_eth_len > 13) { /*13 bytes is the minimal length of a request telegram...*/
1012                                    sbus_helper = sbus_eth_len - (offset + 2);
1013                                    proto_tree_add_text(sbus_tree, tvb, offset, sbus_helper, 
1014                                        "This telegram isn't implemented in the dissector.");
1015                                    offset = offset + sbus_helper; 
1016                             }
1017                      }
1018               }
1019
1020 /* Response dissection*/
1021               if (sbus_attribut == SBUS_RESPONSE && request_val) {
1022                      switch (request_val->cmd_code) {
1023                      /* Response: 32 bit values*/
1024                      case SBUS_RD_COUNTER:
1025                      case SBUS_RD_REGISTER:
1026                      case SBUS_RD_TIMER:
1027                      case SBUS_RD_USER_MEMORY:
1028                      case SBUS_RD_PROGRAM_LINE:
1029                      case SBUS_RD_USER_EEPROM_REGISTER:
1030                             /*Add subtree for Data*/
1031                             dt = proto_tree_add_text(sbus_tree, tvb, offset,
1032                                    ((request_val->count) * 4),"Data");
1033                             sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
1034                             for (i=(request_val->count); i>0; i--) {
1035                                    proto_tree_add_item(sbusdata_tree,
1036                                        hf_sbus_data_rtc, tvb, offset, 
1037                                        4, FALSE); 
1038                                    offset += 4;
1039                             }
1040                             break;
1041
1042                      /* Response: PCD Display register*/
1043                      case SBUS_RD_DISPLAY_REGISTER:
1044                             proto_tree_add_item(sbus_tree,
1045                                 hf_sbus_display_register, tvb, offset, 4, FALSE); 
1046                             offset += 4;
1047                             break;
1048
1049                      /* Add binary data I, O, F*/
1050                      case SBUS_RD_FLAG:
1051                      case SBUS_RD_INPUT:
1052                      case SBUS_RD_OUTPUT:
1053                             /*Add subtree for Data*/
1054                             dt = proto_tree_add_text(sbus_tree, tvb, offset, 
1055                                    (((request_val->count) + 7) / 8), "Data");
1056                             sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
1057                             
1058                             for (i=(((request_val->count) + 7) / 8); i>0; i--) {
1059                                    sbus_helper = 1;
1060                                    sbus_show_bin = 0;
1061                                    sbus_binarymasked = 0x01; 
1062                                    sbus_binaries = tvb_get_guint8(tvb, offset);
1063                                    for (j=0; j<8; j++){
1064                                           if ((sbus_binarymasked & sbus_binaries) != 0) {
1065                                                  sbus_show_bin = (sbus_show_bin + sbus_helper);
1066                                           }
1067                                           sbus_binarymasked = sbus_binarymasked<<1;
1068                                           sbus_helper = 10 * sbus_helper; 
1069                                    }
1070                             
1071                                    proto_tree_add_uint_format(sbusdata_tree,
1072                                           hf_sbus_data_iof, tvb, offset, 1, sbus_show_bin,
1073                                           "Binary data: %08u", sbus_show_bin); 
1074                                    offset += 1;
1075                             }
1076                             break;
1077                      
1078                      /* Response: Real time clock value*/
1079                      case SBUS_RD_RTC:  
1080                             sbus_helper = tvb_get_guint8(tvb, (offset +5));  /*hours*/
1081                             sbus_helper1 = tvb_get_guint8(tvb, (offset +6)); /*minutes*/
1082                             sbus_helper2 = tvb_get_guint8(tvb, (offset +7)); /*seconds*/
1083                             proto_tree_add_text(sbus_tree, tvb, (offset +5), 3, 
1084                                 "Time (HH:MM:SS): %02x:%02x:%02x", sbus_helper, sbus_helper1, sbus_helper2);
1085                             sbus_helper = tvb_get_guint8(tvb, (offset +2));  /*year*/
1086                             sbus_helper1 = tvb_get_guint8(tvb, (offset +3)); /*month*/
1087                             sbus_helper2 = tvb_get_guint8(tvb, (offset +4)); /*day*/
1088                             proto_tree_add_text(sbus_tree, tvb, (offset +2), 3, 
1089                                 "Date (YY/MM/DD): %02x/%02x/%02x", sbus_helper, sbus_helper1, sbus_helper2);
1090                             sbus_helper = tvb_get_guint8(tvb, (offset));  /*year-week*/
1091                             sbus_helper1 = tvb_get_guint8(tvb, (offset +1)); /*week-day*/
1092                             proto_tree_add_text(sbus_tree, tvb, offset, 2, 
1093                                 "Calendar week: %x, Week day: %x", sbus_helper, sbus_helper1);
1094                             /*Add subtree for Data*/
1095                             dt = proto_tree_add_text(sbus_tree, tvb, offset, 
1096                                 8, "Clock data");
1097                             sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
1098                             
1099                             proto_tree_add_item(sbusdata_tree,
1100                                 hf_sbus_week_day, tvb, offset, 2, FALSE); 
1101                             offset += 2;
1102                             proto_tree_add_item(sbusdata_tree,
1103                                 hf_sbus_date, tvb, offset, 3, FALSE); 
1104                             offset += 3;
1105                             proto_tree_add_item(sbusdata_tree,
1106                                 hf_sbus_time, tvb, offset, 3, FALSE); 
1107                             offset += 3;
1108                             break;
1109                      
1110                      /* Response: CPU status, the command codes 14..1B are concerned*/
1111                      case SBUS_RD_PCD_STATUS_CPU0:
1112                      case SBUS_RD_PCD_STATUS_CPU1:
1113                      case SBUS_RD_PCD_STATUS_CPU2:
1114                      case SBUS_RD_PCD_STATUS_CPU3:
1115                      case SBUS_RD_PCD_STATUS_CPU4:
1116                      case SBUS_RD_PCD_STATUS_CPU5:
1117                      case SBUS_RD_PCD_STATUS_CPU6:
1118                      case SBUS_RD_PCD_STATUS_OWN:
1119                             proto_tree_add_item(sbus_tree,
1120                                 hf_sbus_cpu_status, tvb, offset, 1, FALSE); 
1121                             offset += 1;
1122                             break;
1123                      
1124                      /* Response: Station address*/
1125                      case SBUS_RD_SBUS_STN_NBR:
1126                             proto_tree_add_item(sbus_tree,
1127                                 hf_sbus_address, tvb, offset, 1, FALSE); 
1128                             offset += 1;
1129                             break;
1130                      
1131                      /* Response: Firmware version */
1132                      case SBUS_RD_PROGRAM_VERSION:
1133                             /*PCD type*/
1134                             tmp_string = tvb_get_string(tvb, offset, 5);
1135                             proto_tree_add_string(sbus_tree,
1136                                 hf_sbus_cpu_type, tvb, offset, 5, tmp_string);
1137                             offset += 5; 
1138                             g_free(tmp_string);
1139                             /*FW version*/
1140                             tmp_string = tvb_get_string(tvb , offset, 3);
1141                             proto_tree_add_string(sbus_tree,
1142                                 hf_sbus_fw_version, tvb, offset, 3, tmp_string);
1143                             offset += 4;
1144                             g_free(tmp_string);
1145                             break;
1146                      
1147                      /* Response for Status Flags*/
1148                      case SBUS_RD_STATUSFLAG_ACCU:
1149                             /*Add subtree for Data*/
1150                             dt = proto_tree_add_text(sbus_tree, tvb, offset,
1151                                 1,"ACCU and arithmetic status");
1152                             sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
1153                             
1154                             proto_tree_add_item(sbusdata_tree, hf_sbus_flags_accu,
1155                                 tvb, offset, 1, FALSE);
1156                             proto_tree_add_item(sbusdata_tree, hf_sbus_flags_error,
1157                                 tvb, offset, 1, FALSE);
1158                             proto_tree_add_item(sbusdata_tree, hf_sbus_flags_negative,
1159                                 tvb, offset, 1, FALSE);
1160                             proto_tree_add_item(sbusdata_tree, hf_sbus_flags_zero,
1161                                 tvb, offset, 1, FALSE);
1162                             offset +=1;
1163                             break;
1164                      
1165                      /* Response for Read byte */
1166                      case SBUS_RD_BYTE:
1167                             /*Add subtree for Data*/
1168                             dt = proto_tree_add_text(sbus_tree, tvb, offset,
1169                                    (request_val->count),"Data (bytes)");
1170                             
1171                             sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
1172                             for (i=(request_val->count); i>0; i--) {
1173                                    proto_tree_add_item(sbusdata_tree,
1174                                        hf_sbus_data_byte, tvb, offset, 
1175                                        1, FALSE); 
1176                                    offset += 1;
1177                             }
1178                             break;
1179                      
1180                      /* Response for Read Index register */
1181                      case SBUS_RD_INDEX_REGISTER:
1182                             /*Add subtree for Data*/
1183                             dt = proto_tree_add_text(sbus_tree, tvb, offset,
1184                                    2,"Data (hex bytes)");
1185                             
1186                             sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
1187                             for (i=0; i<2; i++) { /*2 bytes*/
1188                                    proto_tree_add_item(sbusdata_tree,
1189                                        hf_sbus_data_byte_hex, tvb, offset, 
1190                                        1, FALSE); 
1191                                    offset += 1;
1192                             }
1193                             break;
1194                      
1195                      /* Response: Instruction pointer*/
1196                      case SBUS_RD_INSTRUCTION_POINTER:
1197                             proto_tree_add_item(sbus_tree,
1198                                 hf_sbus_addr_prog, tvb, offset, 3, FALSE); 
1199                             offset += 3;
1200                             break;
1201                      
1202                      /*Response for Find History*/
1203                      case SBUS_FIND_HISTORY:
1204                             proto_tree_add_item(sbus_tree,
1205                                 hf_sbus_addr_68k, tvb, offset, 3, FALSE); 
1206                             offset += 3;
1207                             proto_tree_add_item(sbus_tree,
1208                                 hf_sbus_nbr_elements, tvb, offset, 2, FALSE); 
1209                             offset += 2;
1210                             break;
1211                      
1212                      /* Response: Read current block*/
1213                      case SBUS_RD_CURRENT_BLOCK:
1214                             proto_tree_add_item(sbus_tree,
1215                                 hf_sbus_block_type, tvb, offset, 1, FALSE); 
1216                             offset += 1;
1217                             proto_tree_add_item(sbus_tree,
1218                                 hf_sbus_block_nr, tvb, offset, 2, FALSE); 
1219                             offset += 2;
1220                             break;
1221
1222                      /* Response: Read system infomation (without interpretation of module info)*/
1223                      case SBUS_RD_SYSTEM_INFORMATION:
1224                             if (((request_val->sysinfo) == 0x00)){ /*sysinfo 0*/
1225                                    offset += 1; /* this byte is always 0x01*/
1226                                    /*Add subtree for Data*/
1227                                    dt = proto_tree_add_text(sbus_tree, tvb, offset,
1228                                        1,"System info");
1229                                    sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
1230                                    
1231                                    proto_tree_add_item(sbusdata_tree, hf_sbus_sysinfo0_1,
1232                                        tvb, offset, 1, FALSE);
1233                                    proto_tree_add_item(sbusdata_tree, hf_sbus_sysinfo0_2,
1234                                        tvb, offset, 1, FALSE);
1235                                    proto_tree_add_item(sbusdata_tree, hf_sbus_sysinfo0_3,
1236                                        tvb, offset, 1, FALSE);
1237                                    proto_tree_add_item(sbusdata_tree, hf_sbus_sysinfo0_4,
1238                                        tvb, offset, 1, FALSE);
1239                                    proto_tree_add_item(sbusdata_tree, hf_sbus_sysinfo0_5,
1240                                        tvb, offset, 1, FALSE);
1241                                    offset += 1;
1242                             }
1243                             break;
1244                      
1245                      /* Response: Webserver request */
1246                      case SBUS_WEB_SERVER_SERIAL_COMM:
1247                             sbus_web_size = tvb_get_guint8(tvb,offset);
1248                             proto_tree_add_item(sbus_tree,
1249                                 hf_sbus_web_size, tvb, offset,
1250                                 1, sbus_web_size);
1251                             offset += 1;
1252                             
1253                             sbus_web_aid = tvb_get_guint8(tvb,offset);
1254                             proto_tree_add_item(sbus_tree,
1255                                 hf_sbus_web_aid, tvb, offset,
1256                                 1, sbus_web_aid);
1257                             offset += 1;
1258
1259                             if (sbus_web_size > 1) {
1260                                    sbus_web_seq = tvb_get_guint8(tvb,offset);
1261                                    proto_tree_add_item(sbus_tree,
1262                                        hf_sbus_web_seq, tvb, offset,
1263                                        1, sbus_web_seq);
1264                                    offset += 1;
1265                             
1266                                    dt = proto_tree_add_text(sbus_tree, tvb, offset,
1267                                           (sbus_web_size - 2),"Data (bytes)");
1268                             
1269                                    sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
1270                                    for (i=sbus_web_size - 2; i>0; i--) {
1271                                           proto_tree_add_item(sbusdata_tree,
1272                                               hf_sbus_data_byte, tvb, offset, 
1273                                               1, FALSE); 
1274                                           offset += 1;    
1275                                    }
1276                             }
1277                             break;
1278                      
1279                      /*Inform that response was not dissected and add remaining length*/
1280                      default:
1281                             sbus_helper = sbus_eth_len - (offset + 2);
1282                             proto_tree_add_text(sbus_tree, tvb, offset, sbus_helper, 
1283                                 "This telegram isn't implemented in the dissector.");
1284                             offset = offset + sbus_helper;
1285                             break;
1286                      }
1287               }
1288               
1289               if (sbus_attribut == SBUS_ACKNAK) {
1290                      proto_tree_add_item(sbus_tree,
1291                          hf_sbus_acknackcode, tvb, offset, 2, FALSE); 
1292                          offset += 2;
1293               }
1294               
1295               /* Calclulate CRC */
1296               sbus_crc_calc = 0;
1297               for (i = 0; i < sbus_eth_len - 2; i++)
1298                      sbus_crc_calc = crc_calc (sbus_crc_calc, tvb_get_guint8(tvb, i));
1299               /*Show CRC and add hidden item for wrong CRC*/
1300               sbus_helper = tvb_get_ntohs(tvb, offset);
1301               if (sbus_helper == sbus_crc_calc) {
1302                      proto_tree_add_uint_format(sbus_tree,
1303                          hf_sbus_crc, tvb, offset, 2, sbus_helper, 
1304                          "Checksum: 0x%04x (correct)", sbus_helper);
1305               } else {
1306                      proto_tree_add_uint_format(sbus_tree,
1307                          hf_sbus_crc, tvb, offset, 2, sbus_helper, 
1308                          "Checksum: 0x%04x (NOT correct)", sbus_helper);
1309                      hi = proto_tree_add_boolean(sbus_tree, 
1310                          hf_sbus_crc_bad, tvb, offset + 2, 2, TRUE);
1311                                          PROTO_ITEM_SET_HIDDEN(hi);
1312               }
1313               offset += 2; /*now at the end of the telegram*/
1314        }
1315        return tvb_length(tvb);
1316 /*End of dissect_sbus*/
1317 }
1318
1319 /* Register the protocol with Wireshark */
1320
1321 void
1322 proto_register_sbus(void)
1323 {
1324
1325 /* Setup list of header fields  See Section 1.6.1 for details*/
1326        static hf_register_info hf[] = {              
1327               { &hf_sbus_length,
1328                      { "Length (bytes)",           "sbus.len",
1329                      FT_UINT32, BASE_DEC, NULL, 0,           
1330                      "SAIA Ether-S-Bus telegram length", HFILL }
1331               },
1332               { &hf_sbus_version,
1333                      { "Version",           "sbus.vers",
1334                      FT_UINT8, BASE_DEC, NULL, 0,           
1335                      "SAIA Ether-S-Bus version", HFILL }
1336               },
1337               { &hf_sbus_protocol,
1338                      { "Protocol type",           "sbus.proto",
1339                      FT_UINT8, BASE_DEC, NULL, 0,           
1340                      "SAIA Ether-S-Bus protocol type", HFILL }
1341               },
1342               { &hf_sbus_sequence,
1343                      { "Sequence",           "sbus.seq",
1344                      FT_UINT16, BASE_DEC, NULL, 0,           
1345                      "SAIA Ether-S-Bus sequence number", HFILL }
1346               },
1347
1348               { &hf_sbus_attribut,
1349                      { "Telegram attribute",           "sbus.att",
1350                      FT_UINT8, BASE_HEX, VALS(sbus_att_vals), 0,           
1351                      "SAIA Ether-S-Bus telegram attribute, indicating type of telegram", HFILL }
1352               },
1353
1354               { &hf_sbus_dest,
1355                      { "Destination",           "sbus.destination",
1356                      FT_UINT8, BASE_DEC, NULL, 0,           
1357                      "SAIA S-Bus destination address", HFILL }
1358               },
1359               
1360               { &hf_sbus_address,
1361                      { "S-Bus address",           "sbus.address",
1362                      FT_UINT8, BASE_DEC, NULL, 0,           
1363                      "SAIA S-Bus station address", HFILL }
1364               },
1365               
1366               { &hf_sbus_command,
1367                      { "Command",           "sbus.cmd",
1368                      FT_UINT8, BASE_HEX, VALS(sbus_command_vals), 0,           
1369                      "SAIA S-Bus command", HFILL }
1370               },
1371
1372               { &hf_sbus_command_extension,
1373                      { "Command extension",           "sbus.cmd_extn",
1374                      FT_UINT8, BASE_HEX, NULL, 0,           
1375                      "SAIA S-Bus command extension", HFILL }
1376               },
1377        
1378               { &hf_sbus_rcount,
1379                      { "R-count",           "sbus.rcount",
1380                      FT_UINT8, BASE_DEC, NULL, 0,           
1381                      "Number of elements expected in response", HFILL }
1382               },
1383               
1384               { &hf_sbus_wcount,
1385                      { "W-count (raw)",           "sbus.wcount",
1386                      FT_UINT8, BASE_DEC, NULL, 0,           
1387                      "Number of bytes to be written", HFILL }
1388               },
1389
1390               { &hf_sbus_wcount_calculated,
1391                      { "W-count (32 bit values)",           "sbus.wcount_calc",
1392                      FT_UINT8, BASE_DEC, NULL, 0,           
1393                      "Number of elements to be written", HFILL }
1394               },
1395
1396               { &hf_sbus_fio_count,
1397                      { "FIO Count (amount of bits)",           "sbus.fio_count",
1398                      FT_UINT8, BASE_DEC, NULL, 0,           
1399                      "Number of binary elements to be written", HFILL }
1400               },
1401                             
1402               { &hf_sbus_addr_rtc,
1403                      { "Base address RTC",           "sbus.addr_RTC",
1404                      FT_UINT16, BASE_DEC, NULL, 0,           
1405                      "Base address of 32 bit elements to read", HFILL }
1406               },
1407               
1408               { &hf_sbus_addr_iof,
1409                      { "Base address IOF",           "sbus.addr_IOF",
1410                      FT_UINT16, BASE_DEC, NULL, 0,           
1411                      "Base address of binary elements to read", HFILL }
1412               },
1413               
1414               { &hf_sbus_addr_eeprom,
1415                      { "Base address of EEPROM register",           "sbus.addr_EEPROM",
1416                      FT_UINT16, BASE_DEC, NULL, 0,           
1417                      "Base address of 32 bit EEPROM register to read or write", HFILL }
1418               },
1419               
1420               { &hf_sbus_addr_prog,
1421                      { "Base address of user memory or program lines",           "sbus.addr_prog",
1422                      FT_UINT24, BASE_DEC, NULL, 0,           
1423                      "Base address of the user memory or program lines (read or write)", HFILL }
1424               },
1425               
1426               { &hf_sbus_addr_68k,
1427                      { "Base address of bytes",           "sbus.addr_prog",
1428                      FT_UINT24, BASE_HEX, NULL, 0,           
1429                      "Base address of bytes to read or write (68k address)", HFILL }
1430               },
1431               
1432               { &hf_sbus_block_type,
1433                      { "Block type",           "sbus.block_type",
1434                      FT_UINT8, BASE_HEX, VALS(sbus_block_types), 0,           
1435                      "Program block type read", HFILL }
1436               },
1437               
1438               { &hf_sbus_block_nr,
1439                      { "Block/Element nr",           "sbus.block_nr",
1440                      FT_UINT16, BASE_DEC, NULL, 0,           
1441                      "Program block / DatatBlock number", HFILL }
1442               },
1443               
1444               { &hf_sbus_nbr_elements,
1445                      { "Number of elements",           "sbus.nbr_elements",
1446                      FT_UINT16, BASE_DEC, NULL, 0,           
1447                      "Number of elements or characters", HFILL }
1448               },
1449               
1450               { &hf_sbus_display_register,
1451                      { "PCD Display register",      "sbus.data_display_register",
1452                      FT_UINT32, BASE_DEC, NULL, 0,           
1453                      "The PCD display register (32 bit value)", HFILL }
1454               },
1455               
1456               { &hf_sbus_data_rtc,
1457                      { "S-Bus 32-bit data",      "sbus.data_rtc",
1458                      FT_UINT32, BASE_DEC, NULL, 0,           
1459                      "One regiser/timer of counter (32 bit value)", HFILL }
1460               },
1461               
1462               { &hf_sbus_data_byte,
1463                      { "Data bytes",      "sbus.data_byte",
1464                      FT_UINT8, BASE_DEC, NULL, 0,           
1465                      "One byte from PCD", HFILL }
1466               },
1467               
1468               { &hf_sbus_data_byte_hex,
1469                      { "Data bytes (hex)",      "sbus.data_byte_hex",
1470                      FT_UINT8, BASE_HEX, NULL, 0,           
1471                      "One byte from PCD (hexadecimal)", HFILL }
1472               },
1473               
1474               { &hf_sbus_data_iof,
1475                      { "S-Bus binary data",      "sbus.data_iof",
1476                      FT_UINT32, BASE_DEC, NULL, 0,           
1477                      "8 binaries", HFILL }
1478               },
1479               
1480               { &hf_sbus_cpu_type,
1481                      { "PCD type",      "sbus.pcd_type",
1482                      FT_STRING, BASE_NONE, NULL, 0,
1483                      "PCD type (short form)", HFILL }
1484               },
1485               
1486               { &hf_sbus_fw_version,
1487                      { "Firmware version",      "sbus.fw_version",
1488                      FT_STRING, BASE_NONE, NULL, 0,
1489                      "Firmware version of the PCD or module", HFILL }
1490               },
1491               
1492               { &hf_sbus_sysinfo_nr,
1493                      { "System information number",           "sbus.sysinfo",
1494                      FT_UINT8, BASE_HEX, NULL, 0,           
1495                      "System information number (extension to command code)", HFILL }
1496               },
1497               
1498               { &hf_sbus_sysinfo0_1,
1499                      { "Mem size info",      "sbus.sysinfo0.mem",
1500                      FT_BOOLEAN, 8, TFS(&tfs_sbus_present), F_MEMSIZE,           
1501                      "Availability of memory size information", HFILL }
1502               },
1503               { &hf_sbus_sysinfo0_2,
1504                      { "Trace buffer",      "sbus.sysinfo0.trace",
1505                      FT_BOOLEAN, 8, TFS(&tfs_sbus_present), F_TRACE,           
1506                      "Availability of trace buffer feature", HFILL }
1507               },
1508               { &hf_sbus_sysinfo0_3,
1509                      { "Slot B1",      "sbus.sysinfo0.b1",
1510                      FT_BOOLEAN, 8, TFS(&tfs_sbus_present), F_INFO_B1,           
1511                      "Presence of EEPROM information on slot B1", HFILL }
1512               },
1513               { &hf_sbus_sysinfo0_4,
1514                      { "Slot B2",      "sbus.sysinfo0.b2",
1515                      FT_BOOLEAN, 8, TFS(&tfs_sbus_present), F_INFO_B2,           
1516                      "Presence of EEPROM information on slot B2", HFILL }
1517               },
1518               { &hf_sbus_sysinfo0_5,
1519                      { "PGU baud",      "sbus.sysinfo0.pgubaud",
1520                      FT_BOOLEAN, 8, TFS(&tfs_sbus_present), F_PGU_BAUD,           
1521                      "Availability of PGU baud switch feature", HFILL }
1522               },
1523               
1524               { &hf_sbus_sysinfo_length,
1525                      { "System information length",           "sbus.sysinfo_length",
1526                      FT_UINT8, BASE_HEX, NULL, 0,           
1527                      "System information length in response", HFILL }
1528               },
1529               
1530               { &hf_sbus_f_module_type,
1531                      { "F-module type",      "sbus.fmodule_type",
1532                      FT_STRING, BASE_NONE, NULL, 0,
1533                      "Module type mounted on B1/2 slot", HFILL }
1534               },
1535               
1536               { &hf_sbus_harware_version,
1537                      { "Hardware version",      "sbus.hw_version",
1538                      FT_STRING, BASE_NONE, NULL, 0,
1539                      "Hardware version of the PCD or the module", HFILL }
1540               },
1541               
1542               { &hf_sbus_hardware_modification,
1543                      { "Hardware modification",      "sbus.hw_modification",
1544                      FT_UINT8, BASE_DEC, NULL, 0,           
1545                      "Hardware modification of the PCD or module", HFILL }
1546               },
1547        
1548               { &hf_sbus_various,
1549                      { "Varous data",      "sbus.various",
1550                      FT_NONE, BASE_NONE, NULL, 0,           
1551                      "Various data contained in telegrams but nobody will search for it", HFILL }
1552               },
1553               
1554               { &hf_sbus_acknackcode,
1555                      { "ACK/NAK code",      "sbus.nakcode",
1556                      FT_UINT16, BASE_HEX, VALS(sbus_ack_nak_vals), 0,           
1557                      "SAIA S-Bus ACK/NAK response", HFILL }
1558               },
1559               
1560               { &hf_sbus_cpu_status,
1561                      { "CPU status",      "sbus.CPU_status",
1562                      FT_UINT8, BASE_HEX, VALS(sbus_CPU_status), 0,           
1563                      "SAIA PCD CPU status", HFILL }
1564               },
1565               
1566               { &hf_sbus_week_day,
1567                      { "RTC calendar week and week day",           "sbus.rtc.week_day",
1568                      FT_UINT16, BASE_HEX, NULL, 0,           
1569                      "Calendar week and week day number of the real time clock", HFILL }
1570               },
1571
1572               { &hf_sbus_date,
1573                      { "RTC date (YYMMDD)",           "sbus.rtc.date",
1574                      FT_UINT24, BASE_HEX, NULL, 0,           
1575                      "Year, month and day of the real time clock", HFILL }
1576               },
1577               
1578               { &hf_sbus_time,
1579                      { "RTC time (HHMMSS)",           "sbus.rtc.time",
1580                      FT_UINT24, BASE_HEX, NULL, 0,           
1581                      "Time of the real time clock", HFILL }
1582               },
1583               
1584               { &hf_sbus_web_size,
1585                      { "Web server packet size",      "sbus.web.size",
1586                      FT_UINT8, BASE_HEX, NULL, 0,           
1587                      "Web server packet size", HFILL }
1588               },
1589                      
1590               { &hf_sbus_web_aid,
1591                      { "AID",      "sbus.web.aid",
1592                      FT_UINT8, BASE_HEX, NULL, 0,           
1593                      "Web server command/status code (AID)", HFILL }
1594               },
1595                      
1596               { &hf_sbus_web_seq,
1597                      { "Sequence",      "sbus.web.seq",
1598                      FT_UINT8, BASE_HEX, NULL, 0,           
1599                      "Web server sequence nr (PACK_N)", HFILL }
1600               },
1601
1602               { &hf_sbus_crc,
1603                      { "Checksum",      "sbus.crc",
1604                      FT_UINT16, BASE_HEX, NULL, 0,           
1605                      "CRC 16", HFILL }
1606               },
1607
1608               { &hf_sbus_crc_bad,
1609                      { "Bad Checksum",      "sbus.crc_bad", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1610                      "A bad checksum in the telegram", HFILL }},                  
1611               
1612               { &hf_sbus_flags_accu,
1613                      { "ACCU", "sbus.flags.accu",
1614                      FT_BOOLEAN, 8, TFS(&tfs_sbus_flags), F_ACCU,
1615                      "PCD Accumulator", HFILL }
1616               },
1617               
1618               { &hf_sbus_flags_error,
1619                      { "Error flag", "sbus.flags.error",
1620                      FT_BOOLEAN, 8, TFS(&tfs_sbus_flags), F_ERROR,
1621                      "PCD error flag", HFILL }
1622               },
1623               
1624               { &hf_sbus_flags_negative,
1625                      { "N-flag", "sbus.flags.nflag",
1626                      FT_BOOLEAN, 8, TFS(&tfs_sbus_flags), F_NEGATIVE,
1627                      "Negative status flag", HFILL }
1628               },
1629               
1630               { &hf_sbus_flags_zero,
1631                      { "Z-flag", "sbus.flags.zflag",
1632                      FT_BOOLEAN, 8, TFS(&tfs_sbus_flags), F_ZERO,
1633                      "Zero status flag", HFILL }
1634               }
1635        };
1636
1637 /* Setup protocol subtree array */
1638        static gint *ett[] = {
1639               &ett_sbus,
1640               &ett_sbus_ether,
1641               &ett_sbus_data
1642        };
1643
1644 /* Register the protocol name and description */
1645        proto_sbus = proto_register_protocol("SAIA S-Bus", "SBUS", "sbus");
1646
1647 /* Required function calls to register the header fields and subtrees used */
1648        proto_register_field_array(proto_sbus, hf, array_length(hf));
1649        proto_register_subtree_array(ett, array_length(ett));
1650        register_init_routine(&sbus_init_protocol);
1651 }
1652  
1653 void
1654 proto_reg_handoff_sbus(void)
1655 {
1656        dissector_handle_t sbus_handle;
1657
1658        sbus_handle = new_create_dissector_handle(dissect_sbus, proto_sbus);
1659        dissector_add("udp.port", 5050, sbus_handle);
1660 }
1661