3928becd04952dc57c06a68bb44bce1eaae8150c
[metze/wireshark/wip.git] / epan / dissectors / 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, "Partial 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        col_set_str(pinfo->cinfo, COL_PROTOCOL, "S-Bus");
632
633        if (check_col(pinfo->cinfo, COL_INFO)) 
634               col_clear(pinfo->cinfo, COL_INFO);
635        offset = 0;
636
637        if (check_col(pinfo->cinfo, COL_INFO)) {
638               switch (sbus_attribut){
639                      case SBUS_REQUEST:     
640                             sbus_cmd_code = tvb_get_guint8(tvb,10);
641                                           
642                             /* Special treatment of web server request 
643                              * as is is very helpful to see more information in the packetlist */
644                             if (sbus_cmd_code == SBUS_WEB_SERVER_SERIAL_COMM) {
645                                    sbus_web_aid = tvb_get_guint8(tvb,12);
646                                    sbus_web_seq = tvb_get_guint8(tvb,13);
647                                    
648                                    col_add_fstr(pinfo->cinfo, COL_INFO,
649                                           "Web Server Request: %s (Seq No: %d)", 
650                                           val_to_str(sbus_web_aid,
651                                                  webserver_aid_vals, "Unknown Request!"), 
652                                                  sbus_web_seq);
653                             } else {
654                                    /* All other requests */
655                                    col_add_fstr(pinfo->cinfo, COL_INFO,
656                                           "Request: %s", val_to_str(sbus_cmd_code,
657                                           sbus_command_vals, "Unknown Command!"));
658                             }
659                             break;
660                                    
661                      case SBUS_RESPONSE:    
662                             /* Special treatment of web server request 
663                              * as is is very helpful to see more information in the packetlist */
664                             if (request_val && ((request_val->cmd_code) == SBUS_WEB_SERVER_SERIAL_COMM)) { 
665                                    sbus_web_size = tvb_get_guint8(tvb,9);
666                                    sbus_web_aid = tvb_get_guint8(tvb,10);
667                                    col_add_fstr(pinfo->cinfo, COL_INFO,
668                                           "Response: %s", 
669                                           val_to_str(sbus_web_aid,
670                                                  webserver_aid_vals, "Unknown Request!"));
671                                    if (sbus_web_size > 1) {
672                                           sbus_web_seq = tvb_get_guint8(tvb,11);
673                                           col_append_fstr(pinfo->cinfo, COL_INFO,
674                                               " (Seq No: %d)",
675                                               sbus_web_seq);
676                                    }
677                             } else {
678                                    col_set_str(pinfo->cinfo, COL_INFO, "Response");
679                             }
680                             break;
681                                    
682                      case SBUS_ACKNAK:      
683                             sbus_ack_code = tvb_get_ntohs(tvb,9);
684                             col_add_fstr(pinfo->cinfo, COL_INFO,
685                                    "%s", val_to_str(sbus_ack_code,
686                                    sbus_ack_nak_vals, 
687                                    "Unknown NAK response code!"));
688                             break;
689                             
690                      default: 
691                             col_set_str(pinfo->cinfo, COL_INFO, "Unknown attribute");
692                             break;
693               }
694
695        }      
696 /* create display subtree for the protocol */
697        if (tree) { 
698
699               ti = proto_tree_add_item(tree, proto_sbus, tvb, offset, -1, FALSE);
700               sbus_tree = proto_item_add_subtree(ti, ett_sbus);
701               
702 /*Add subtree for Ether-S-Bus header*/
703               et = proto_tree_add_text(sbus_tree, tvb, offset, 8, "Ether-S-Bus header");
704               ethsbus_tree = proto_item_add_subtree(et, ett_sbus_ether);
705
706 /* add an item to the subtree*/
707               sbus_eth_len = tvb_get_ntohl(tvb,offset);
708               proto_tree_add_item(ethsbus_tree,
709                   hf_sbus_length, tvb, offset, 4, FALSE); 
710                   offset += 4;
711
712               sbus_version = tvb_get_guint8(tvb,offset);
713               proto_tree_add_item(ethsbus_tree,
714                   hf_sbus_version, tvb, offset, 1, FALSE); 
715                   offset += 1;
716
717               proto_tree_add_item(ethsbus_tree,
718                   hf_sbus_protocol, tvb, offset, 1, FALSE); 
719                   offset += 1;
720
721               proto_tree_add_item(ethsbus_tree,
722                   hf_sbus_sequence, tvb, offset, 2, FALSE); 
723                   offset += 2;
724
725 /* Continue adding stuff to the main tree*/
726               sbus_attribut = tvb_get_guint8(tvb,offset);
727               proto_tree_add_item(sbus_tree,
728                   hf_sbus_attribut, tvb, offset, 1, FALSE); 
729                   offset += 1;
730
731               if (sbus_attribut == SBUS_REQUEST) {
732                      proto_tree_add_item(sbus_tree,
733                          hf_sbus_dest, tvb, offset, 1, FALSE); 
734                          offset += 1;
735                      sbus_cmd_code = tvb_get_guint8(tvb,offset);
736                      proto_tree_add_item(sbus_tree,
737                          hf_sbus_command, tvb, offset, 1, FALSE); 
738                          offset += 1;
739                             
740                      switch (sbus_cmd_code) {
741                      /*Read Counter, Register or Timer*/
742                      case SBUS_RD_COUNTER:
743                      case SBUS_RD_REGISTER: 
744                      case SBUS_RD_TIMER:
745                             sbus_media_cnt = (tvb_get_guint8(tvb,offset))+1;
746                             proto_tree_add_uint(sbus_tree,
747                                 hf_sbus_rcount, tvb, offset, 1, sbus_media_cnt); 
748                             offset += 1;
749                             proto_tree_add_item(sbus_tree,
750                                 hf_sbus_addr_rtc, tvb, offset, 2, FALSE); 
751                             offset += 2;
752                             break;
753
754                      /*Read Flag, Input or Output*/
755                      case SBUS_RD_FLAG:
756                      case SBUS_RD_INPUT: 
757                      case SBUS_RD_OUTPUT:
758                             sbus_media_cnt = (tvb_get_guint8(tvb,offset))+1;
759                             proto_tree_add_uint(sbus_tree,
760                                 hf_sbus_rcount, tvb, offset, 1, sbus_media_cnt); 
761                             offset += 1;
762                             proto_tree_add_item(sbus_tree,
763                                 hf_sbus_addr_iof, tvb, offset, 2, FALSE); 
764                             offset += 2;
765                             break;
766
767                      /*Write Register Timer Counter*/
768                      case SBUS_WR_COUNTER:
769                      case SBUS_WR_REGISTER:
770                      case SBUS_WR_TIMER:
771                             sbus_media_cnt = (tvb_get_guint8(tvb,offset));
772                             sbus_media_cnt = ((sbus_media_cnt - 1)/4);
773                             proto_tree_add_uint(sbus_tree,
774                                 hf_sbus_wcount_calculated, tvb, offset,
775                                 1, sbus_media_cnt);
776                             proto_tree_add_item(sbus_tree,
777                                 hf_sbus_wcount, tvb, offset, 1, FALSE);
778                             offset += 1;
779                             proto_tree_add_item(sbus_tree,
780                                 hf_sbus_addr_rtc, tvb, offset, 2, FALSE); 
781                             offset += 2;
782                             /*Add subtree for Data*/
783                             dt = proto_tree_add_text(sbus_tree, tvb, offset,
784                                 ((sbus_media_cnt) * 4),"Data");
785                             
786                             sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
787                             for (i=((sbus_media_cnt)); i>0; i--) {
788                                    proto_tree_add_item(sbusdata_tree,
789                                        hf_sbus_data_rtc, tvb, offset, 
790                                        4, FALSE);
791                                    offset += 4;
792                             }
793                             break;
794
795                      /* Write flags and outputs*/
796                      case SBUS_WR_FLAG:
797                      case SBUS_WR_OUTPUT:
798                             sbus_media_cnt = (tvb_get_guint8(tvb,offset));
799                             sbus_media_cnt = (sbus_media_cnt - 2);
800                             proto_tree_add_uint(sbus_tree,
801                                 hf_sbus_wcount_calculated, tvb, offset,
802                                 1, sbus_media_cnt);
803                             proto_tree_add_item(sbus_tree,
804                                 hf_sbus_wcount, tvb, offset, 1, FALSE);
805                                 offset += 1;
806                             proto_tree_add_item(sbus_tree,
807                                 hf_sbus_addr_iof, tvb, offset, 2, FALSE); 
808                                 offset += 2;
809                             sbus_fio_cnt = (tvb_get_guint8(tvb,offset));
810                             sbus_fio_cnt = ((sbus_fio_cnt + 1));
811                             proto_tree_add_uint(sbus_tree,
812                                 hf_sbus_fio_count, tvb, offset, 1, sbus_fio_cnt); 
813                                 offset += 1;                  
814                             /*Add subtree for Data*/
815                             dt = proto_tree_add_text(sbus_tree, tvb, offset,
816                                 sbus_media_cnt,"Data");
817                             
818                             sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
819                             for (i=sbus_media_cnt; i>0; i--) {
820                                    sbus_helper = 1;
821                                    sbus_show_bin = 0;
822                                    sbus_binarymasked = 0x01; 
823                                    sbus_binaries = tvb_get_guint8(tvb, offset);
824                                    for (j=0; j<8; j++) {
825                                           if ((sbus_binarymasked & sbus_binaries) != 0) {
826                                                  sbus_show_bin = (sbus_show_bin + sbus_helper);
827                                           }
828                                           sbus_binarymasked = sbus_binarymasked<<1;
829                                           sbus_helper = 10 * sbus_helper; 
830                                    }
831                             
832                                    proto_tree_add_uint_format(sbusdata_tree,
833                                           hf_sbus_data_iof, tvb, offset, 1, sbus_show_bin,
834                                           "Binary data: %08u", sbus_show_bin); 
835                                    offset += 1;
836                             }
837                             break;
838
839                      /* Request: Write Real time clock*/
840                      case SBUS_WR_RTC:
841                             sbus_helper = tvb_get_guint8(tvb, (offset +5));  /*hours*/
842                             sbus_helper1 = tvb_get_guint8(tvb, (offset +6)); /*minutes*/
843                             sbus_helper2 = tvb_get_guint8(tvb, (offset +7)); /*seconds*/
844                             proto_tree_add_text(sbus_tree, tvb, (offset +5), 3, 
845                                 "Time (HH:MM:SS): %02x:%02x:%02x", sbus_helper, sbus_helper1, sbus_helper2);
846                             sbus_helper = tvb_get_guint8(tvb, (offset +2));  /*year*/
847                             sbus_helper1 = tvb_get_guint8(tvb, (offset +3)); /*month*/
848                             sbus_helper2 = tvb_get_guint8(tvb, (offset +4)); /*day*/
849                             proto_tree_add_text(sbus_tree, tvb, (offset +2), 3, 
850                                 "Date (YY/MM/DD): %02x/%02x/%02x", sbus_helper, sbus_helper1, sbus_helper2);
851                             sbus_helper = tvb_get_guint8(tvb, (offset));  /*year-week*/
852                             sbus_helper1 = tvb_get_guint8(tvb, (offset +1)); /*week-day*/
853                             proto_tree_add_text(sbus_tree, tvb, offset, 2, 
854                                 "Calendar week: %x, Week day: %x", sbus_helper, sbus_helper1);
855                             /*Add subtree for Data*/
856                             dt = proto_tree_add_text(sbus_tree, tvb, offset, 
857                                 8, "Clock data");
858                             sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
859                             
860                             proto_tree_add_item(sbusdata_tree,
861                                 hf_sbus_week_day, tvb, offset, 2, FALSE); 
862                             offset += 2;
863                             proto_tree_add_item(sbusdata_tree,
864                                 hf_sbus_date, tvb, offset, 3, FALSE); 
865                             offset += 3;
866                             proto_tree_add_item(sbusdata_tree,
867                                 hf_sbus_time, tvb, offset, 3, FALSE); 
868                             offset += 3;
869                             break;
870                      
871                      /* Read user memory or program line*/
872                      case SBUS_RD_USER_MEMORY:
873                      case SBUS_RD_PROGRAM_LINE:
874                             sbus_media_cnt = (tvb_get_guint8(tvb,offset))+1;
875                             proto_tree_add_uint(sbus_tree,
876                                 hf_sbus_rcount, tvb, offset, 1, sbus_media_cnt); 
877                             offset += 1;
878                             proto_tree_add_item(sbus_tree,
879                                 hf_sbus_addr_prog, tvb, offset, 3, FALSE); 
880                             offset += 3;
881                             break;
882                      
883                      /*Write user memory*/
884                      case SBUS_WR_USER_MEMORY:
885                             sbus_media_cnt = (tvb_get_guint8(tvb,offset));
886                             sbus_media_cnt = ((sbus_media_cnt - 2)/4);
887                             proto_tree_add_uint(sbus_tree,
888                                 hf_sbus_wcount_calculated, tvb, offset,
889                                 1, sbus_media_cnt);
890                             proto_tree_add_item(sbus_tree,
891                                 hf_sbus_wcount, tvb, offset, 1, FALSE);
892                                 offset += 1;
893                             proto_tree_add_item(sbus_tree,
894                                 hf_sbus_addr_68k, tvb, offset, 3, FALSE); 
895                                 offset += 3;
896                             /*Add subtree for Data*/
897                             dt = proto_tree_add_text(sbus_tree, tvb, offset,
898                                 ((sbus_media_cnt) * 4),"Program lines");
899                             
900                             sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
901                             for (i=((sbus_media_cnt)); i>0; i--) {
902                                    proto_tree_add_item(sbusdata_tree,
903                                        hf_sbus_data_rtc, tvb, offset, 
904                                        4, FALSE); 
905                                    offset += 4;    
906                             
907                             }
908                             break;
909                      
910                      /* Read byte*/
911                      case SBUS_RD_BYTE:
912                             sbus_media_cnt = (tvb_get_guint8(tvb,offset))+1;
913                             proto_tree_add_uint(sbus_tree,
914                                 hf_sbus_rcount, tvb, offset, 1, sbus_media_cnt); 
915                             offset += 1;
916                             proto_tree_add_item(sbus_tree,
917                                 hf_sbus_addr_68k, tvb, offset, 3, FALSE); 
918                             offset += 3;
919                             break;
920                      
921                      /* Write byte */
922                      case SBUS_WR_BYTE:
923                             sbus_media_cnt = (tvb_get_guint8(tvb,offset));
924                             sbus_media_cnt = (sbus_media_cnt - 2);
925                             proto_tree_add_uint(sbus_tree,
926                                 hf_sbus_wcount_calculated, tvb, offset,
927                                 1, sbus_media_cnt);
928                             proto_tree_add_item(sbus_tree,
929                                 hf_sbus_wcount, tvb, offset, 1, FALSE);
930                                 offset += 1;
931                             proto_tree_add_item(sbus_tree,
932                                 hf_sbus_addr_68k, tvb, offset, 3, FALSE); 
933                                 offset += 3;
934                             /*Add subtree for Data*/
935                             dt = proto_tree_add_text(sbus_tree, tvb, offset,
936                                 ((sbus_media_cnt) * 4),"Data (bytes)");
937                             
938                             sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
939                             for (i=sbus_media_cnt; i>0; i--) {
940                                    proto_tree_add_item(sbusdata_tree,
941                                        hf_sbus_data_byte, tvb, offset, 
942                                        1, FALSE); 
943                                    offset += 1;    
944                             }
945                             break;
946                                           
947                      /*Read EEPROM register*/
948                      case SBUS_RD_USER_EEPROM_REGISTER:
949                             proto_tree_add_item(sbus_tree,
950                                 hf_sbus_command_extension, tvb, offset, 1, FALSE); 
951                             offset += 1;
952                             sbus_media_cnt = (tvb_get_guint8(tvb,offset))+1;
953                             proto_tree_add_uint(sbus_tree,
954                                 hf_sbus_rcount, tvb, offset, 1, sbus_media_cnt); 
955                             offset += 1;
956                             proto_tree_add_item(sbus_tree,
957                                 hf_sbus_addr_eeprom, tvb, offset, 2, FALSE); 
958                             offset += 2;
959                             break;
960               
961                      /*Request for reading system info*/
962                      /*Syinfo 05 is not implemented as no serial baud is possible*/
963                      case SBUS_RD_SYSTEM_INFORMATION:
964                             sbus_sysinfo_number = (tvb_get_guint8(tvb,13));
965                             proto_tree_add_item(sbus_tree,
966                                 hf_sbus_sysinfo_nr, tvb, offset, 1, FALSE); 
967                                 offset += 1;
968                             sbus_sysinfo_number = (tvb_get_guint8(tvb,14));
969                             proto_tree_add_item(sbus_tree,
970                                 hf_sbus_sysinfo_nr, tvb, offset, 1, FALSE); 
971                             offset += 1;
972                             break;
973                      
974                      /* WebServer Request */
975                      case SBUS_WEB_SERVER_SERIAL_COMM:
976                             sbus_web_size = tvb_get_guint8(tvb,offset);
977                             proto_tree_add_item(sbus_tree,
978                                 hf_sbus_web_size, tvb, offset,
979                                 1, sbus_web_size);
980                             offset += 1;
981
982                             sbus_web_aid = tvb_get_guint8(tvb,offset);
983                             proto_tree_add_item(sbus_tree,
984                                 hf_sbus_web_aid, tvb, offset,
985                                 1, sbus_web_aid);
986                             offset += 1;
987
988                             sbus_web_seq = tvb_get_guint8(tvb,offset);
989                             proto_tree_add_item(sbus_tree,
990                                 hf_sbus_web_seq, tvb, offset,
991                                 1, sbus_web_seq);
992                             offset += 1;
993                             
994                             if (sbus_web_size > 1) {
995                                    dt = proto_tree_add_text(sbus_tree, tvb, offset,
996                                           (sbus_web_size - 1),"Data (bytes)");
997                                    
998                                    sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
999                                    for (i=sbus_web_size -1 ; i>0; i--) {
1000                                           proto_tree_add_item(sbusdata_tree,
1001                                               hf_sbus_data_byte, tvb, offset, 
1002                                               1, FALSE); 
1003                                           offset += 1;
1004                                    }      
1005                             }
1006                             break;
1007
1008 /*Inform that command was not dissected and add remaining length*/
1009                      default:
1010                             if (sbus_eth_len > 13) { /*13 bytes is the minimal length of a request telegram...*/
1011                                    sbus_helper = sbus_eth_len - (offset + 2);
1012                                    proto_tree_add_text(sbus_tree, tvb, offset, sbus_helper, 
1013                                        "This telegram isn't implemented in the dissector.");
1014                                    offset = offset + sbus_helper; 
1015                             }
1016                      }
1017               }
1018
1019 /* Response dissection*/
1020               if (sbus_attribut == SBUS_RESPONSE && request_val) {
1021                      switch (request_val->cmd_code) {
1022                      /* Response: 32 bit values*/
1023                      case SBUS_RD_COUNTER:
1024                      case SBUS_RD_REGISTER:
1025                      case SBUS_RD_TIMER:
1026                      case SBUS_RD_USER_MEMORY:
1027                      case SBUS_RD_PROGRAM_LINE:
1028                      case SBUS_RD_USER_EEPROM_REGISTER:
1029                             /*Add subtree for Data*/
1030                             dt = proto_tree_add_text(sbus_tree, tvb, offset,
1031                                    ((request_val->count) * 4),"Data");
1032                             sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
1033                             for (i=(request_val->count); i>0; i--) {
1034                                    proto_tree_add_item(sbusdata_tree,
1035                                        hf_sbus_data_rtc, tvb, offset, 
1036                                        4, FALSE); 
1037                                    offset += 4;
1038                             }
1039                             break;
1040
1041                      /* Response: PCD Display register*/
1042                      case SBUS_RD_DISPLAY_REGISTER:
1043                             proto_tree_add_item(sbus_tree,
1044                                 hf_sbus_display_register, tvb, offset, 4, FALSE); 
1045                             offset += 4;
1046                             break;
1047
1048                      /* Add binary data I, O, F*/
1049                      case SBUS_RD_FLAG:
1050                      case SBUS_RD_INPUT:
1051                      case SBUS_RD_OUTPUT:
1052                             /*Add subtree for Data*/
1053                             dt = proto_tree_add_text(sbus_tree, tvb, offset, 
1054                                    (((request_val->count) + 7) / 8), "Data");
1055                             sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
1056                             
1057                             for (i=(((request_val->count) + 7) / 8); i>0; i--) {
1058                                    sbus_helper = 1;
1059                                    sbus_show_bin = 0;
1060                                    sbus_binarymasked = 0x01; 
1061                                    sbus_binaries = tvb_get_guint8(tvb, offset);
1062                                    for (j=0; j<8; j++){
1063                                           if ((sbus_binarymasked & sbus_binaries) != 0) {
1064                                                  sbus_show_bin = (sbus_show_bin + sbus_helper);
1065                                           }
1066                                           sbus_binarymasked = sbus_binarymasked<<1;
1067                                           sbus_helper = 10 * sbus_helper; 
1068                                    }
1069                             
1070                                    proto_tree_add_uint_format(sbusdata_tree,
1071                                           hf_sbus_data_iof, tvb, offset, 1, sbus_show_bin,
1072                                           "Binary data: %08u", sbus_show_bin); 
1073                                    offset += 1;
1074                             }
1075                             break;
1076                      
1077                      /* Response: Real time clock value*/
1078                      case SBUS_RD_RTC:  
1079                             sbus_helper = tvb_get_guint8(tvb, (offset +5));  /*hours*/
1080                             sbus_helper1 = tvb_get_guint8(tvb, (offset +6)); /*minutes*/
1081                             sbus_helper2 = tvb_get_guint8(tvb, (offset +7)); /*seconds*/
1082                             proto_tree_add_text(sbus_tree, tvb, (offset +5), 3, 
1083                                 "Time (HH:MM:SS): %02x:%02x:%02x", sbus_helper, sbus_helper1, sbus_helper2);
1084                             sbus_helper = tvb_get_guint8(tvb, (offset +2));  /*year*/
1085                             sbus_helper1 = tvb_get_guint8(tvb, (offset +3)); /*month*/
1086                             sbus_helper2 = tvb_get_guint8(tvb, (offset +4)); /*day*/
1087                             proto_tree_add_text(sbus_tree, tvb, (offset +2), 3, 
1088                                 "Date (YY/MM/DD): %02x/%02x/%02x", sbus_helper, sbus_helper1, sbus_helper2);
1089                             sbus_helper = tvb_get_guint8(tvb, (offset));  /*year-week*/
1090                             sbus_helper1 = tvb_get_guint8(tvb, (offset +1)); /*week-day*/
1091                             proto_tree_add_text(sbus_tree, tvb, offset, 2, 
1092                                 "Calendar week: %x, Week day: %x", sbus_helper, sbus_helper1);
1093                             /*Add subtree for Data*/
1094                             dt = proto_tree_add_text(sbus_tree, tvb, offset, 
1095                                 8, "Clock data");
1096                             sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
1097                             
1098                             proto_tree_add_item(sbusdata_tree,
1099                                 hf_sbus_week_day, tvb, offset, 2, FALSE); 
1100                             offset += 2;
1101                             proto_tree_add_item(sbusdata_tree,
1102                                 hf_sbus_date, tvb, offset, 3, FALSE); 
1103                             offset += 3;
1104                             proto_tree_add_item(sbusdata_tree,
1105                                 hf_sbus_time, tvb, offset, 3, FALSE); 
1106                             offset += 3;
1107                             break;
1108                      
1109                      /* Response: CPU status, the command codes 14..1B are concerned*/
1110                      case SBUS_RD_PCD_STATUS_CPU0:
1111                      case SBUS_RD_PCD_STATUS_CPU1:
1112                      case SBUS_RD_PCD_STATUS_CPU2:
1113                      case SBUS_RD_PCD_STATUS_CPU3:
1114                      case SBUS_RD_PCD_STATUS_CPU4:
1115                      case SBUS_RD_PCD_STATUS_CPU5:
1116                      case SBUS_RD_PCD_STATUS_CPU6:
1117                      case SBUS_RD_PCD_STATUS_OWN:
1118                             proto_tree_add_item(sbus_tree,
1119                                 hf_sbus_cpu_status, tvb, offset, 1, FALSE); 
1120                             offset += 1;
1121                             break;
1122                      
1123                      /* Response: Station address*/
1124                      case SBUS_RD_SBUS_STN_NBR:
1125                             proto_tree_add_item(sbus_tree,
1126                                 hf_sbus_address, tvb, offset, 1, FALSE); 
1127                             offset += 1;
1128                             break;
1129                      
1130                      /* Response: Firmware version */
1131                      case SBUS_RD_PROGRAM_VERSION:
1132                             /*PCD type*/
1133                             tmp_string = tvb_get_string(tvb, offset, 5);
1134                             proto_tree_add_string(sbus_tree,
1135                                 hf_sbus_cpu_type, tvb, offset, 5, tmp_string);
1136                             offset += 5; 
1137                             g_free(tmp_string);
1138                             /*FW version*/
1139                             tmp_string = tvb_get_string(tvb , offset, 3);
1140                             proto_tree_add_string(sbus_tree,
1141                                 hf_sbus_fw_version, tvb, offset, 3, tmp_string);
1142                             offset += 4;
1143                             g_free(tmp_string);
1144                             break;
1145                      
1146                      /* Response for Status Flags*/
1147                      case SBUS_RD_STATUSFLAG_ACCU:
1148                             /*Add subtree for Data*/
1149                             dt = proto_tree_add_text(sbus_tree, tvb, offset,
1150                                 1,"ACCU and arithmetic status");
1151                             sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
1152                             
1153                             proto_tree_add_item(sbusdata_tree, hf_sbus_flags_accu,
1154                                 tvb, offset, 1, FALSE);
1155                             proto_tree_add_item(sbusdata_tree, hf_sbus_flags_error,
1156                                 tvb, offset, 1, FALSE);
1157                             proto_tree_add_item(sbusdata_tree, hf_sbus_flags_negative,
1158                                 tvb, offset, 1, FALSE);
1159                             proto_tree_add_item(sbusdata_tree, hf_sbus_flags_zero,
1160                                 tvb, offset, 1, FALSE);
1161                             offset +=1;
1162                             break;
1163                      
1164                      /* Response for Read byte */
1165                      case SBUS_RD_BYTE:
1166                             /*Add subtree for Data*/
1167                             dt = proto_tree_add_text(sbus_tree, tvb, offset,
1168                                    (request_val->count),"Data (bytes)");
1169                             
1170                             sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
1171                             for (i=(request_val->count); i>0; i--) {
1172                                    proto_tree_add_item(sbusdata_tree,
1173                                        hf_sbus_data_byte, tvb, offset, 
1174                                        1, FALSE); 
1175                                    offset += 1;
1176                             }
1177                             break;
1178                      
1179                      /* Response for Read Index register */
1180                      case SBUS_RD_INDEX_REGISTER:
1181                             /*Add subtree for Data*/
1182                             dt = proto_tree_add_text(sbus_tree, tvb, offset,
1183                                    2,"Data (hex bytes)");
1184                             
1185                             sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
1186                             for (i=0; i<2; i++) { /*2 bytes*/
1187                                    proto_tree_add_item(sbusdata_tree,
1188                                        hf_sbus_data_byte_hex, tvb, offset, 
1189                                        1, FALSE); 
1190                                    offset += 1;
1191                             }
1192                             break;
1193                      
1194                      /* Response: Instruction pointer*/
1195                      case SBUS_RD_INSTRUCTION_POINTER:
1196                             proto_tree_add_item(sbus_tree,
1197                                 hf_sbus_addr_prog, tvb, offset, 3, FALSE); 
1198                             offset += 3;
1199                             break;
1200                      
1201                      /*Response for Find History*/
1202                      case SBUS_FIND_HISTORY:
1203                             proto_tree_add_item(sbus_tree,
1204                                 hf_sbus_addr_68k, tvb, offset, 3, FALSE); 
1205                             offset += 3;
1206                             proto_tree_add_item(sbus_tree,
1207                                 hf_sbus_nbr_elements, tvb, offset, 2, FALSE); 
1208                             offset += 2;
1209                             break;
1210                      
1211                      /* Response: Read current block*/
1212                      case SBUS_RD_CURRENT_BLOCK:
1213                             proto_tree_add_item(sbus_tree,
1214                                 hf_sbus_block_type, tvb, offset, 1, FALSE); 
1215                             offset += 1;
1216                             proto_tree_add_item(sbus_tree,
1217                                 hf_sbus_block_nr, tvb, offset, 2, FALSE); 
1218                             offset += 2;
1219                             break;
1220
1221                      /* Response: Read system infomation (without interpretation of module info)*/
1222                      case SBUS_RD_SYSTEM_INFORMATION:
1223                             if (((request_val->sysinfo) == 0x00)){ /*sysinfo 0*/
1224                                    offset += 1; /* this byte is always 0x01*/
1225                                    /*Add subtree for Data*/
1226                                    dt = proto_tree_add_text(sbus_tree, tvb, offset,
1227                                        1,"System info");
1228                                    sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
1229                                    
1230                                    proto_tree_add_item(sbusdata_tree, hf_sbus_sysinfo0_1,
1231                                        tvb, offset, 1, FALSE);
1232                                    proto_tree_add_item(sbusdata_tree, hf_sbus_sysinfo0_2,
1233                                        tvb, offset, 1, FALSE);
1234                                    proto_tree_add_item(sbusdata_tree, hf_sbus_sysinfo0_3,
1235                                        tvb, offset, 1, FALSE);
1236                                    proto_tree_add_item(sbusdata_tree, hf_sbus_sysinfo0_4,
1237                                        tvb, offset, 1, FALSE);
1238                                    proto_tree_add_item(sbusdata_tree, hf_sbus_sysinfo0_5,
1239                                        tvb, offset, 1, FALSE);
1240                                    offset += 1;
1241                             }
1242                             break;
1243                      
1244                      /* Response: Webserver request */
1245                      case SBUS_WEB_SERVER_SERIAL_COMM:
1246                             sbus_web_size = tvb_get_guint8(tvb,offset);
1247                             proto_tree_add_item(sbus_tree,
1248                                 hf_sbus_web_size, tvb, offset,
1249                                 1, sbus_web_size);
1250                             offset += 1;
1251                             
1252                             sbus_web_aid = tvb_get_guint8(tvb,offset);
1253                             proto_tree_add_item(sbus_tree,
1254                                 hf_sbus_web_aid, tvb, offset,
1255                                 1, sbus_web_aid);
1256                             offset += 1;
1257
1258                             if (sbus_web_size > 1) {
1259                                    sbus_web_seq = tvb_get_guint8(tvb,offset);
1260                                    proto_tree_add_item(sbus_tree,
1261                                        hf_sbus_web_seq, tvb, offset,
1262                                        1, sbus_web_seq);
1263                                    offset += 1;
1264                             
1265                                    dt = proto_tree_add_text(sbus_tree, tvb, offset,
1266                                           (sbus_web_size - 2),"Data (bytes)");
1267                             
1268                                    sbusdata_tree = proto_item_add_subtree(dt, ett_sbus_data);
1269                                    for (i=sbus_web_size - 2; i>0; i--) {
1270                                           proto_tree_add_item(sbusdata_tree,
1271                                               hf_sbus_data_byte, tvb, offset, 
1272                                               1, FALSE); 
1273                                           offset += 1;    
1274                                    }
1275                             }
1276                             break;
1277                      
1278                      /*Inform that response was not dissected and add remaining length*/
1279                      default:
1280                             sbus_helper = sbus_eth_len - (offset + 2);
1281                             proto_tree_add_text(sbus_tree, tvb, offset, sbus_helper, 
1282                                 "This telegram isn't implemented in the dissector.");
1283                             offset = offset + sbus_helper;
1284                             break;
1285                      }
1286               }
1287               
1288               if (sbus_attribut == SBUS_ACKNAK) {
1289                      proto_tree_add_item(sbus_tree,
1290                          hf_sbus_acknackcode, tvb, offset, 2, FALSE); 
1291                          offset += 2;
1292               }
1293               
1294               /* Calclulate CRC */
1295               sbus_crc_calc = 0;
1296               for (i = 0; i < sbus_eth_len - 2; i++)
1297                      sbus_crc_calc = crc_calc (sbus_crc_calc, tvb_get_guint8(tvb, i));
1298               /*Show CRC and add hidden item for wrong CRC*/
1299               sbus_helper = tvb_get_ntohs(tvb, offset);
1300               if (sbus_helper == sbus_crc_calc) {
1301                      proto_tree_add_uint_format(sbus_tree,
1302                          hf_sbus_crc, tvb, offset, 2, sbus_helper, 
1303                          "Checksum: 0x%04x (correct)", sbus_helper);
1304               } else {
1305                      proto_tree_add_uint_format(sbus_tree,
1306                          hf_sbus_crc, tvb, offset, 2, sbus_helper, 
1307                          "Checksum: 0x%04x (NOT correct)", sbus_helper);
1308                      hi = proto_tree_add_boolean(sbus_tree, 
1309                          hf_sbus_crc_bad, tvb, offset + 2, 2, TRUE);
1310                                          PROTO_ITEM_SET_HIDDEN(hi);
1311               }
1312               offset += 2; /*now at the end of the telegram*/
1313        }
1314        return tvb_length(tvb);
1315 /*End of dissect_sbus*/
1316 }
1317
1318 /* Register the protocol with Wireshark */
1319
1320 void
1321 proto_register_sbus(void)
1322 {
1323
1324 /* Setup list of header fields  See Section 1.6.1 for details*/
1325        static hf_register_info hf[] = {              
1326               { &hf_sbus_length,
1327                      { "Length (bytes)",           "sbus.len",
1328                      FT_UINT32, BASE_DEC, NULL, 0,           
1329                      "SAIA Ether-S-Bus telegram length", HFILL }
1330               },
1331               { &hf_sbus_version,
1332                      { "Version",           "sbus.vers",
1333                      FT_UINT8, BASE_DEC, NULL, 0,           
1334                      "SAIA Ether-S-Bus version", HFILL }
1335               },
1336               { &hf_sbus_protocol,
1337                      { "Protocol type",           "sbus.proto",
1338                      FT_UINT8, BASE_DEC, NULL, 0,           
1339                      "SAIA Ether-S-Bus protocol type", HFILL }
1340               },
1341               { &hf_sbus_sequence,
1342                      { "Sequence",           "sbus.seq",
1343                      FT_UINT16, BASE_DEC, NULL, 0,           
1344                      "SAIA Ether-S-Bus sequence number", HFILL }
1345               },
1346
1347               { &hf_sbus_attribut,
1348                      { "Telegram attribute",           "sbus.att",
1349                      FT_UINT8, BASE_HEX, VALS(sbus_att_vals), 0,           
1350                      "SAIA Ether-S-Bus telegram attribute, indicating type of telegram", HFILL }
1351               },
1352
1353               { &hf_sbus_dest,
1354                      { "Destination",           "sbus.destination",
1355                      FT_UINT8, BASE_DEC, NULL, 0,           
1356                      "SAIA S-Bus destination address", HFILL }
1357               },
1358               
1359               { &hf_sbus_address,
1360                      { "S-Bus address",           "sbus.address",
1361                      FT_UINT8, BASE_DEC, NULL, 0,           
1362                      "SAIA S-Bus station address", HFILL }
1363               },
1364               
1365               { &hf_sbus_command,
1366                      { "Command",           "sbus.cmd",
1367                      FT_UINT8, BASE_HEX, VALS(sbus_command_vals), 0,           
1368                      "SAIA S-Bus command", HFILL }
1369               },
1370
1371               { &hf_sbus_command_extension,
1372                      { "Command extension",           "sbus.cmd_extn",
1373                      FT_UINT8, BASE_HEX, NULL, 0,           
1374                      "SAIA S-Bus command extension", HFILL }
1375               },
1376        
1377               { &hf_sbus_rcount,
1378                      { "R-count",           "sbus.rcount",
1379                      FT_UINT8, BASE_DEC, NULL, 0,           
1380                      "Number of elements expected in response", HFILL }
1381               },
1382               
1383               { &hf_sbus_wcount,
1384                      { "W-count (raw)",           "sbus.wcount",
1385                      FT_UINT8, BASE_DEC, NULL, 0,           
1386                      "Number of bytes to be written", HFILL }
1387               },
1388
1389               { &hf_sbus_wcount_calculated,
1390                      { "W-count (32 bit values)",           "sbus.wcount_calc",
1391                      FT_UINT8, BASE_DEC, NULL, 0,           
1392                      "Number of elements to be written", HFILL }
1393               },
1394
1395               { &hf_sbus_fio_count,
1396                      { "FIO Count (amount of bits)",           "sbus.fio_count",
1397                      FT_UINT8, BASE_DEC, NULL, 0,           
1398                      "Number of binary elements to be written", HFILL }
1399               },
1400                             
1401               { &hf_sbus_addr_rtc,
1402                      { "Base address RTC",           "sbus.addr_RTC",
1403                      FT_UINT16, BASE_DEC, NULL, 0,           
1404                      "Base address of 32 bit elements to read", HFILL }
1405               },
1406               
1407               { &hf_sbus_addr_iof,
1408                      { "Base address IOF",           "sbus.addr_IOF",
1409                      FT_UINT16, BASE_DEC, NULL, 0,           
1410                      "Base address of binary elements to read", HFILL }
1411               },
1412               
1413               { &hf_sbus_addr_eeprom,
1414                      { "Base address of EEPROM register",           "sbus.addr_EEPROM",
1415                      FT_UINT16, BASE_DEC, NULL, 0,           
1416                      "Base address of 32 bit EEPROM register to read or write", HFILL }
1417               },
1418               
1419               { &hf_sbus_addr_prog,
1420                      { "Base address of user memory or program lines",           "sbus.addr_prog",
1421                      FT_UINT24, BASE_DEC, NULL, 0,           
1422                      "Base address of the user memory or program lines (read or write)", HFILL }
1423               },
1424               
1425               { &hf_sbus_addr_68k,
1426                      { "Base address of bytes",           "sbus.addr_prog",
1427                      FT_UINT24, BASE_HEX, NULL, 0,           
1428                      "Base address of bytes to read or write (68k address)", HFILL }
1429               },
1430               
1431               { &hf_sbus_block_type,
1432                      { "Block type",           "sbus.block_type",
1433                      FT_UINT8, BASE_HEX, VALS(sbus_block_types), 0,           
1434                      "Program block type read", HFILL }
1435               },
1436               
1437               { &hf_sbus_block_nr,
1438                      { "Block/Element nr",           "sbus.block_nr",
1439                      FT_UINT16, BASE_DEC, NULL, 0,           
1440                      "Program block / DatatBlock number", HFILL }
1441               },
1442               
1443               { &hf_sbus_nbr_elements,
1444                      { "Number of elements",           "sbus.nbr_elements",
1445                      FT_UINT16, BASE_DEC, NULL, 0,           
1446                      "Number of elements or characters", HFILL }
1447               },
1448               
1449               { &hf_sbus_display_register,
1450                      { "PCD Display register",      "sbus.data_display_register",
1451                      FT_UINT32, BASE_DEC, NULL, 0,           
1452                      "The PCD display register (32 bit value)", HFILL }
1453               },
1454               
1455               { &hf_sbus_data_rtc,
1456                      { "S-Bus 32-bit data",      "sbus.data_rtc",
1457                      FT_UINT32, BASE_DEC, NULL, 0,           
1458                      "One regiser/timer of counter (32 bit value)", HFILL }
1459               },
1460               
1461               { &hf_sbus_data_byte,
1462                      { "Data bytes",      "sbus.data_byte",
1463                      FT_UINT8, BASE_DEC, NULL, 0,           
1464                      "One byte from PCD", HFILL }
1465               },
1466               
1467               { &hf_sbus_data_byte_hex,
1468                      { "Data bytes (hex)",      "sbus.data_byte_hex",
1469                      FT_UINT8, BASE_HEX, NULL, 0,           
1470                      "One byte from PCD (hexadecimal)", HFILL }
1471               },
1472               
1473               { &hf_sbus_data_iof,
1474                      { "S-Bus binary data",      "sbus.data_iof",
1475                      FT_UINT32, BASE_DEC, NULL, 0,           
1476                      "8 binaries", HFILL }
1477               },
1478               
1479               { &hf_sbus_cpu_type,
1480                      { "PCD type",      "sbus.pcd_type",
1481                      FT_STRING, BASE_NONE, NULL, 0,
1482                      "PCD type (short form)", HFILL }
1483               },
1484               
1485               { &hf_sbus_fw_version,
1486                      { "Firmware version",      "sbus.fw_version",
1487                      FT_STRING, BASE_NONE, NULL, 0,
1488                      "Firmware version of the PCD or module", HFILL }
1489               },
1490               
1491               { &hf_sbus_sysinfo_nr,
1492                      { "System information number",           "sbus.sysinfo",
1493                      FT_UINT8, BASE_HEX, NULL, 0,           
1494                      "System information number (extension to command code)", HFILL }
1495               },
1496               
1497               { &hf_sbus_sysinfo0_1,
1498                      { "Mem size info",      "sbus.sysinfo0.mem",
1499                      FT_BOOLEAN, 8, TFS(&tfs_sbus_present), F_MEMSIZE,           
1500                      "Availability of memory size information", HFILL }
1501               },
1502               { &hf_sbus_sysinfo0_2,
1503                      { "Trace buffer",      "sbus.sysinfo0.trace",
1504                      FT_BOOLEAN, 8, TFS(&tfs_sbus_present), F_TRACE,           
1505                      "Availability of trace buffer feature", HFILL }
1506               },
1507               { &hf_sbus_sysinfo0_3,
1508                      { "Slot B1",      "sbus.sysinfo0.b1",
1509                      FT_BOOLEAN, 8, TFS(&tfs_sbus_present), F_INFO_B1,           
1510                      "Presence of EEPROM information on slot B1", HFILL }
1511               },
1512               { &hf_sbus_sysinfo0_4,
1513                      { "Slot B2",      "sbus.sysinfo0.b2",
1514                      FT_BOOLEAN, 8, TFS(&tfs_sbus_present), F_INFO_B2,           
1515                      "Presence of EEPROM information on slot B2", HFILL }
1516               },
1517               { &hf_sbus_sysinfo0_5,
1518                      { "PGU baud",      "sbus.sysinfo0.pgubaud",
1519                      FT_BOOLEAN, 8, TFS(&tfs_sbus_present), F_PGU_BAUD,           
1520                      "Availability of PGU baud switch feature", HFILL }
1521               },
1522               
1523               { &hf_sbus_sysinfo_length,
1524                      { "System information length",           "sbus.sysinfo_length",
1525                      FT_UINT8, BASE_HEX, NULL, 0,           
1526                      "System information length in response", HFILL }
1527               },
1528               
1529               { &hf_sbus_f_module_type,
1530                      { "F-module type",      "sbus.fmodule_type",
1531                      FT_STRING, BASE_NONE, NULL, 0,
1532                      "Module type mounted on B1/2 slot", HFILL }
1533               },
1534               
1535               { &hf_sbus_harware_version,
1536                      { "Hardware version",      "sbus.hw_version",
1537                      FT_STRING, BASE_NONE, NULL, 0,
1538                      "Hardware version of the PCD or the module", HFILL }
1539               },
1540               
1541               { &hf_sbus_hardware_modification,
1542                      { "Hardware modification",      "sbus.hw_modification",
1543                      FT_UINT8, BASE_DEC, NULL, 0,           
1544                      "Hardware modification of the PCD or module", HFILL }
1545               },
1546        
1547               { &hf_sbus_various,
1548                      { "Various data",      "sbus.various",
1549                      FT_NONE, BASE_NONE, NULL, 0,           
1550                      "Various data contained in telegrams but nobody will search for it", HFILL }
1551               },
1552               
1553               { &hf_sbus_acknackcode,
1554                      { "ACK/NAK code",      "sbus.nakcode",
1555                      FT_UINT16, BASE_HEX, VALS(sbus_ack_nak_vals), 0,           
1556                      "SAIA S-Bus ACK/NAK response", HFILL }
1557               },
1558               
1559               { &hf_sbus_cpu_status,
1560                      { "CPU status",      "sbus.CPU_status",
1561                      FT_UINT8, BASE_HEX, VALS(sbus_CPU_status), 0,           
1562                      "SAIA PCD CPU status", HFILL }
1563               },
1564               
1565               { &hf_sbus_week_day,
1566                      { "RTC calendar week and week day",           "sbus.rtc.week_day",
1567                      FT_UINT16, BASE_HEX, NULL, 0,           
1568                      "Calendar week and week day number of the real time clock", HFILL }
1569               },
1570
1571               { &hf_sbus_date,
1572                      { "RTC date (YYMMDD)",           "sbus.rtc.date",
1573                      FT_UINT24, BASE_HEX, NULL, 0,           
1574                      "Year, month and day of the real time clock", HFILL }
1575               },
1576               
1577               { &hf_sbus_time,
1578                      { "RTC time (HHMMSS)",           "sbus.rtc.time",
1579                      FT_UINT24, BASE_HEX, NULL, 0,           
1580                      "Time of the real time clock", HFILL }
1581               },
1582               
1583               { &hf_sbus_web_size,
1584                      { "Web server packet size",      "sbus.web.size",
1585                      FT_UINT8, BASE_HEX, NULL, 0,           
1586                      NULL, HFILL }
1587               },
1588                      
1589               { &hf_sbus_web_aid,
1590                      { "AID",      "sbus.web.aid",
1591                      FT_UINT8, BASE_HEX, NULL, 0,           
1592                      "Web server command/status code (AID)", HFILL }
1593               },
1594                      
1595               { &hf_sbus_web_seq,
1596                      { "Sequence",      "sbus.web.seq",
1597                      FT_UINT8, BASE_HEX, NULL, 0,           
1598                      "Web server sequence nr (PACK_N)", HFILL }
1599               },
1600
1601               { &hf_sbus_crc,
1602                      { "Checksum",      "sbus.crc",
1603                      FT_UINT16, BASE_HEX, NULL, 0,           
1604                      "CRC 16", HFILL }
1605               },
1606
1607               { &hf_sbus_crc_bad,
1608                      { "Bad Checksum",      "sbus.crc_bad", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1609                      "A bad checksum in the telegram", HFILL }},                  
1610               
1611               { &hf_sbus_flags_accu,
1612                      { "ACCU", "sbus.flags.accu",
1613                      FT_BOOLEAN, 8, TFS(&tfs_sbus_flags), F_ACCU,
1614                      "PCD Accumulator", HFILL }
1615               },
1616               
1617               { &hf_sbus_flags_error,
1618                      { "Error flag", "sbus.flags.error",
1619                      FT_BOOLEAN, 8, TFS(&tfs_sbus_flags), F_ERROR,
1620                      "PCD error flag", HFILL }
1621               },
1622               
1623               { &hf_sbus_flags_negative,
1624                      { "N-flag", "sbus.flags.nflag",
1625                      FT_BOOLEAN, 8, TFS(&tfs_sbus_flags), F_NEGATIVE,
1626                      "Negative status flag", HFILL }
1627               },
1628               
1629               { &hf_sbus_flags_zero,
1630                      { "Z-flag", "sbus.flags.zflag",
1631                      FT_BOOLEAN, 8, TFS(&tfs_sbus_flags), F_ZERO,
1632                      "Zero status flag", HFILL }
1633               }
1634        };
1635
1636 /* Setup protocol subtree array */
1637        static gint *ett[] = {
1638               &ett_sbus,
1639               &ett_sbus_ether,
1640               &ett_sbus_data
1641        };
1642
1643 /* Register the protocol name and description */
1644        proto_sbus = proto_register_protocol("SAIA S-Bus", "SBUS", "sbus");
1645
1646 /* Required function calls to register the header fields and subtrees used */
1647        proto_register_field_array(proto_sbus, hf, array_length(hf));
1648        proto_register_subtree_array(ett, array_length(ett));
1649        register_init_routine(&sbus_init_protocol);
1650 }
1651  
1652 void
1653 proto_reg_handoff_sbus(void)
1654 {
1655        dissector_handle_t sbus_handle;
1656
1657        sbus_handle = new_create_dissector_handle(dissect_sbus, proto_sbus);
1658        dissector_add("udp.port", 5050, sbus_handle);
1659 }
1660