From Chris Botje via https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=8917
[metze/wireshark/wip.git] / epan / dissectors / packet-selfm.c
1 /* packet-selfm.c
2  * Routines for Schweitzer Engineering Laboratories Fast Message Protocol (SEL FM) Dissection
3  * By Chris Bontje (cbontje[AT]gmail.com
4  * Copyright Nov/Dec 2012,
5  *
6  * $Id$
7
8  *
9  ************************************************************************************************
10  * Wireshark - Network traffic analyzer
11  * By Gerald Combs <gerald@wireshark.org>
12  * Copyright 1998 Gerald Combs
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27  *
28  ************************************************************************************************
29  * Schweitzer Engineering Labs ("SEL") manufactures and sells digital protective relay equipment
30  * for use in industrial high-voltage installations.  SEL FM protocol evolved over time as a
31  * (semi)proprietary method for auto-configuration of connected SEL devices for retrieval of
32  * analog and digital status data.  The protocol itself supports embedded binary messages
33  * (which are what this dissector looks for) slip-streamed in the data stream with normal
34  * ASCII text data.  A combination of both are used for full auto-configuration of devices,
35  * but a wealth of information can be extracted from the binary messages alone.
36  *
37  * Documentation on Fast Meter and Fast SER messages available from www.selinc.com in
38  * SEL Application Guides AG95-10_20091109.pdf and AG_200214.pdf
39  ************************************************************************************************
40  * Dissector Notes:
41  *
42  * 1) SEL Fast Message protocol over TCP is normally tunneled via a Telnet connection.  As Telnet
43  * has special handling for the 0xFF character ("IAC"), normally a pair of 0xFF's are inserted
44  * to represent an actual payload byte of 0xFF.  A function from the packet-telnet.c dissector has
45  * been borrowed to automatically pre-process any Ethernet-based packet and remove these 'extra'
46  * 0xFF bytes.  Wireshark Notes on Telnet 0xFF doubling are discussed here:
47  * http://www.wireshark.org/lists/wireshark-bugs/201204/msg00198.html
48  *
49  * 2) The auto-configuration process itself will exchange several "configuration" messages that
50  * describe various data regions (METER, DEMAND, PEAK, etc) that will later have corresponding
51  * "data" messages.  This dissector code will currently save and accurately retrieve the 3 sets
52  * of these exchanges:
53  *             0xA5C1, 0xA5D1, "METER" region
54  *             0xA5C2, 0xA5D2, "DEMAND" region
55  *             0xA5C3, 0xA5D3, "PEAK" region
56  * The configuration messages are stored in structs that are managed using the wmem library and
57  * the Wireshark conversation functionality.
58  */
59
60 #include "config.h"
61
62 #include <epan/packet.h>
63 #include "packet-tcp.h"
64 #include <epan/prefs.h>
65 #include <epan/reassemble.h>
66 #include <epan/expert.h>
67 #include <epan/conversation.h>
68 #include <epan/wmem/wmem.h>
69
70 /* Initialize the protocol and registered fields */
71 static int proto_selfm                        = -1;
72 static int hf_selfm_msgtype                   = -1;
73 static int hf_selfm_relaydef_len              = -1;
74 static int hf_selfm_relaydef_numproto         = -1;
75 static int hf_selfm_relaydef_numfm            = -1;
76 static int hf_selfm_relaydef_numflags         = -1;
77 static int hf_selfm_relaydef_fmcfg_cmd        = -1;
78 static int hf_selfm_relaydef_fmdata_cmd       = -1;
79 static int hf_selfm_relaydef_statbit          = -1;
80 static int hf_selfm_relaydef_statbit_cmd      = -1;
81 static int hf_selfm_relaydef_proto            = -1;
82 static int hf_selfm_fmconfig_len              = -1;
83 static int hf_selfm_fmconfig_numflags         = -1;
84 static int hf_selfm_fmconfig_loc_sf           = -1;
85 static int hf_selfm_fmconfig_num_sf           = -1;
86 static int hf_selfm_fmconfig_num_ai           = -1;
87 static int hf_selfm_fmconfig_num_samp         = -1;
88 static int hf_selfm_fmconfig_num_dig          = -1;
89 static int hf_selfm_fmconfig_num_calc         = -1;
90 static int hf_selfm_fmconfig_ofs_ai           = -1;
91 static int hf_selfm_fmconfig_ofs_ts           = -1;
92 static int hf_selfm_fmconfig_ofs_dig          = -1;
93 static int hf_selfm_fmconfig_ai_type          = -1;
94 static int hf_selfm_fmconfig_ai_sf_type       = -1;
95 static int hf_selfm_fmconfig_ai_sf_ofs        = -1;
96 static int hf_selfm_fmconfig_cblk_rot         = -1;
97 static int hf_selfm_fmconfig_cblk_vconn       = -1;
98 static int hf_selfm_fmconfig_cblk_iconn       = -1;
99 static int hf_selfm_fmconfig_cblk_ctype       = -1;
100 static int hf_selfm_fmconfig_cblk_deskew_ofs  = -1;
101 static int hf_selfm_fmconfig_cblk_rs_ofs      = -1;
102 static int hf_selfm_fmconfig_cblk_xs_ofs      = -1;
103 static int hf_selfm_fmconfig_cblk_ia_idx      = -1;
104 static int hf_selfm_fmconfig_cblk_ib_idx      = -1;
105 static int hf_selfm_fmconfig_cblk_ic_idx      = -1;
106 static int hf_selfm_fmconfig_cblk_va_idx      = -1;
107 static int hf_selfm_fmconfig_cblk_vb_idx      = -1;
108 static int hf_selfm_fmconfig_cblk_vc_idx      = -1;
109 static int hf_selfm_fmdata_len                = -1;
110 static int hf_selfm_fmdata_flagbyte           = -1;
111 static int hf_selfm_fmdata_dig_b0             = -1;
112 static int hf_selfm_fmdata_dig_b1             = -1;
113 static int hf_selfm_fmdata_dig_b2             = -1;
114 static int hf_selfm_fmdata_dig_b3             = -1;
115 static int hf_selfm_fmdata_dig_b4             = -1;
116 static int hf_selfm_fmdata_dig_b5             = -1;
117 static int hf_selfm_fmdata_dig_b6             = -1;
118 static int hf_selfm_fmdata_dig_b7             = -1;
119 static int hf_selfm_fmdata_ai_sf_fp           = -1;
120 static int hf_selfm_foconfig_len              = -1;
121 static int hf_selfm_foconfig_num_brkr         = -1;
122 static int hf_selfm_foconfig_num_rb           = -1;
123 static int hf_selfm_foconfig_prb_supp         = -1;
124 static int hf_selfm_foconfig_reserved         = -1;
125 static int hf_selfm_foconfig_brkr_open        = -1;
126 static int hf_selfm_foconfig_brkr_close       = -1;
127 static int hf_selfm_foconfig_rb_cmd           = -1;
128 static int hf_selfm_fastop_len                = -1;
129 static int hf_selfm_fastop_rb_code            = -1;
130 static int hf_selfm_fastop_br_code            = -1;
131 static int hf_selfm_fastop_valid              = -1;
132 static int hf_selfm_alt_foconfig_len          = -1;
133 static int hf_selfm_alt_foconfig_num_ports    = -1;
134 static int hf_selfm_alt_foconfig_num_brkr     = -1;
135 static int hf_selfm_alt_foconfig_num_rb       = -1;
136 static int hf_selfm_alt_foconfig_funccode     = -1;
137 static int hf_selfm_alt_fastop_len            = -1;
138 static int hf_selfm_alt_fastop_code           = -1;
139 static int hf_selfm_alt_fastop_valid          = -1;
140
141 static int hf_selfm_fastser_len                    = -1;
142 static int hf_selfm_fastser_routing_addr           = -1;
143 static int hf_selfm_fastser_status                 = -1;
144 static int hf_selfm_fastser_funccode               = -1;
145 static int hf_selfm_fastser_seq                    = -1;
146 static int hf_selfm_fastser_seq_fir                = -1;
147 static int hf_selfm_fastser_seq_fin                = -1;
148 static int hf_selfm_fastser_seq_cnt                = -1;
149 static int hf_selfm_fastser_resp_num               = -1;
150 static int hf_selfm_fastser_crc16                  = -1;
151 static int hf_selfm_fastser_def_route_sup          = -1;
152 static int hf_selfm_fastser_def_rx_stat            = -1;
153 static int hf_selfm_fastser_def_tx_stat            = -1;
154 static int hf_selfm_fastser_def_rx_maxfr           = -1;
155 static int hf_selfm_fastser_def_tx_maxfr           = -1;
156 static int hf_selfm_fastser_def_rx_num_fc          = -1;
157 static int hf_selfm_fastser_def_rx_fc              = -1;
158 static int hf_selfm_fastser_def_tx_num_fc          = -1;
159 static int hf_selfm_fastser_def_tx_fc              = -1;
160 static int hf_selfm_fastser_uns_en_fc              = -1;
161 static int hf_selfm_fastser_uns_en_fc_data         = -1;
162 static int hf_selfm_fastser_uns_dis_fc             = -1;
163 static int hf_selfm_fastser_uns_dis_fc_data        = -1;
164 static int hf_selfm_fastser_baseaddr               = -1;
165 static int hf_selfm_fastser_numwords               = -1;
166 static int hf_selfm_fastser_flags                  = -1;
167 static int hf_selfm_fastser_datafmt_resp_numitem   = -1;
168 static int hf_selfm_fastser_dataitem_qty           = -1;
169 static int hf_selfm_fastser_dataitem_type          = -1;
170 static int hf_selfm_fastser_dataitem_uint16        = -1;
171 static int hf_selfm_fastser_dataitem_int16         = -1;
172 static int hf_selfm_fastser_dataitem_uint32        = -1;
173 static int hf_selfm_fastser_dataitem_int32         = -1;
174 static int hf_selfm_fastser_dataitem_float         = -1;
175 static int hf_selfm_fastser_devdesc_num_region     = -1;
176 static int hf_selfm_fastser_devdesc_num_ctrl       = -1;
177 static int hf_selfm_fastser_unsresp_orig           = -1;
178 static int hf_selfm_fastser_unsresp_doy            = -1;
179 static int hf_selfm_fastser_unsresp_year           = -1;
180 static int hf_selfm_fastser_unsresp_todms          = -1;
181 static int hf_selfm_fastser_unsresp_num_elmt       = -1;
182 static int hf_selfm_fastser_unsresp_elmt_idx       = -1;
183 static int hf_selfm_fastser_unsresp_elmt_ts_ofs    = -1;
184 static int hf_selfm_fastser_unsresp_elmt_status    = -1;
185 static int hf_selfm_fastser_unsresp_eor            = -1;
186 static int hf_selfm_fastser_unsresp_elmt_statword  = -1;
187 static int hf_selfm_fastser_unswrite_addr1         = -1;
188 static int hf_selfm_fastser_unswrite_addr2         = -1;
189 static int hf_selfm_fastser_unswrite_num_reg       = -1;
190 static int hf_selfm_fastser_unswrite_reg_val       = -1;
191 static int hf_selfm_fastser_soe_req_orig           = -1;
192 static int hf_selfm_fastser_soe_resp_numblks       = -1;
193 static int hf_selfm_fastser_soe_resp_orig          = -1;
194 static int hf_selfm_fastser_soe_resp_numbits       = -1;
195 static int hf_selfm_fastser_soe_resp_pad           = -1;
196 static int hf_selfm_fastser_soe_resp_doy           = -1;
197 static int hf_selfm_fastser_soe_resp_year          = -1;
198 static int hf_selfm_fastser_soe_resp_tod           = -1;
199 /* static int hf_selfm_fastser_soe_resp_data          = -1; */
200
201
202 /* Initialize the subtree pointers */
203 static gint ett_selfm                       = -1;
204 static gint ett_selfm_relaydef              = -1;
205 static gint ett_selfm_relaydef_fm           = -1;
206 static gint ett_selfm_relaydef_proto        = -1;
207 static gint ett_selfm_relaydef_flags        = -1;
208 static gint ett_selfm_fmconfig              = -1;
209 static gint ett_selfm_fmconfig_ai           = -1;
210 static gint ett_selfm_fmconfig_calc         = -1;
211 static gint ett_selfm_foconfig              = -1;
212 static gint ett_selfm_foconfig_brkr         = -1;
213 static gint ett_selfm_foconfig_rb           = -1;
214 static gint ett_selfm_fastop                = -1;
215 static gint ett_selfm_fmdata                = -1;
216 static gint ett_selfm_fmdata_ai             = -1;
217 static gint ett_selfm_fmdata_dig            = -1;
218 static gint ett_selfm_fmdata_ai_ch          = -1;
219 static gint ett_selfm_fmdata_dig_ch         = -1;
220 static gint ett_selfm_fastser               = -1;
221 static gint ett_selfm_fastser_seq           = -1;
222 static gint ett_selfm_fastser_def_fc        = -1;
223 static gint ett_selfm_fastser_datareg       = -1;
224 static gint ett_selfm_fastser_tag           = -1;
225 static gint ett_selfm_fastser_element_list  = -1;
226 static gint ett_selfm_fastser_element       = -1;
227
228 #define PORT_SELFM    0
229
230 #define CMD_FAST_SER            0xA546
231 #define CMD_CLEAR_STATBIT       0xA5B9
232 #define CMD_RELAY_DEF           0xA5C0
233 #define CMD_FM_CONFIG           0xA5C1
234 #define CMD_DFM_CONFIG          0xA5C2
235 #define CMD_PDFM_CONFIG         0xA5C3
236 #define CMD_FASTOP_RESETDEF     0xA5CD
237 #define CMD_FASTOP_CONFIG       0xA5CE
238 #define CMD_ALT_FASTOP_CONFIG   0xA5CF
239 #define CMD_FM_DATA             0xA5D1
240 #define CMD_DFM_DATA            0xA5D2
241 #define CMD_PDFM_DATA           0xA5D3
242 #define CMD_FASTOP_RB_CTRL      0xA5E0
243 #define CMD_FASTOP_BR_CTRL      0xA5E3
244 #define CMD_ALT_FASTOP_OPEN     0xA5E5
245 #define CMD_ALT_FASTOP_CLOSE    0xA5E6
246 #define CMD_ALT_FASTOP_SET      0xA5E7
247 #define CMD_ALT_FASTOP_CLEAR    0xA5E8
248 #define CMD_ALT_FASTOP_PULSE    0xA5E9
249 #define CMD_FASTOP_RESET        0xA5ED
250
251 #define FM_CONFIG_SF_LOC_FM             0
252 #define FM_CONFIG_SF_LOC_CFG            1
253
254 #define FM_CONFIG_ANA_CHNAME_LEN        6
255 #define FM_CONFIG_ANA_CHTYPE_INT16      0x00
256 #define FM_CONFIG_ANA_CHTYPE_FP         0x01
257 #define FM_CONFIG_ANA_CHTYPE_FPD        0x02
258 #define FM_CONFIG_ANA_CHTYPE_TS         0x03
259 #define FM_CONFIG_ANA_CHTYPE_TS_LEN     8
260
261 #define FM_CONFIG_ANA_SFTYPE_INT16      0x00
262 #define FM_CONFIG_ANA_SFTYPE_FP         0x01
263 #define FM_CONFIG_ANA_SFTYPE_FPD        0x02
264 #define FM_CONFIG_ANA_SFTYPE_TS         0x03
265 #define FM_CONFIG_ANA_SFTYPE_NONE       0xFF
266
267
268 /* Fast SER Function Codes, "response" or "ACK" messages are the same as the request, but have the MSB set */
269 #define FAST_SER_MESSAGE_DEF            0x00
270 #define FAST_SER_EN_UNS_DATA            0x01
271 #define FAST_SER_DIS_UNS_DATA           0x02
272 #define FAST_SER_PING                   0x05
273 #define FAST_SER_READ_REQ               0x10
274 #define FAST_SER_GEN_UNS_DATA           0x12
275 #define FAST_SER_SOE_STATE_REQ          0x16
276 #define FAST_SER_UNS_RESP               0x18
277 #define FAST_SER_UNS_WRITE              0x20
278 #define FAST_SER_UNS_WRITE_REQ          0x21
279 #define FAST_SER_DEVDESC_REQ            0x30
280 #define FAST_SER_DATAFMT_REQ            0x31
281 #define FAST_SER_UNS_DATAFMT_RESP       0x32
282 #define FAST_SER_BITLABEL_REQ           0x33
283 #define FAST_SER_MGMT_REQ               0x40
284 #define FAST_SER_MESSAGE_DEF_ACK        0x80
285 #define FAST_SER_EN_UNS_DATA_ACK        0x81
286 #define FAST_SER_DIS_UNS_DATA_ACK       0x82
287 #define FAST_SER_PING_ACK               0x85
288 #define FAST_SER_READ_RESP              0x90
289 #define FAST_SER_SOE_STATE_RESP         0x96
290 #define FAST_SER_UNS_RESP_ACK           0x98
291 #define FAST_SER_DEVDESC_RESP           0xB0
292 #define FAST_SER_DATAFMT_RESP           0xB1
293 #define FAST_SER_BITLABEL_RESP          0xB3
294
295
296 /* Fast SER Sequence Byte Masks */
297 #define FAST_SER_SEQ_FIR     0x80
298 #define FAST_SER_SEQ_FIN     0x40
299 #define FAST_SER_SEQ_CNT     0x3f
300
301 /* Fast SER Tag Data Types */
302 #define FAST_SER_TAGTYPE_CHAR8        0x0011   /* 1 x 8-bit character per item */
303 #define FAST_SER_TAGTYPE_CHAR16       0x0012   /* 2 x 8-bit characters per item */
304 #define FAST_SER_TAGTYPE_DIGWORD8_BL  0x0021   /* 8-bit binary item, with labels */
305 #define FAST_SER_TAGTYPE_DIGWORD8     0x0022   /* 8-bit binary item, without labels */
306 #define FAST_SER_TAGTYPE_DIGWORD16_BL 0x0023   /* 16-bit binary item, with labels */
307 #define FAST_SER_TAGTYPE_DIGWORD16    0x0024   /* 16-bit binary item, without labels */
308 #define FAST_SER_TAGTYPE_INT16        0x0031   /* 16-bit signed integer */
309 #define FAST_SER_TAGTYPE_UINT16       0x0032   /* 16-bit unsigned integer */
310 #define FAST_SER_TAGTYPE_INT32        0x0033   /* 32-bit signed integer */
311 #define FAST_SER_TAGTYPE_UINT32       0x0034   /* 32-bit unsigned integer */
312 #define FAST_SER_TAGTYPE_FLOAT        0x0041   /* 32-bit floating point */
313
314
315 /* Globals for SEL Protocol Preferences */
316 static gboolean selfm_desegment = TRUE;
317 static gboolean selfm_telnet_clean = TRUE;
318 static guint global_selfm_tcp_port = PORT_SELFM; /* Port 0, by default */
319
320 /***************************************************************************************/
321 /* Fast Meter Message structs */
322 /***************************************************************************************/
323 /* Holds Configuration Information required to decode a Fast Meter analog value        */
324 typedef struct {
325     gchar    name[FM_CONFIG_ANA_CHNAME_LEN+1];    /* Name of Analog Channel, 6 char + a null */
326     guint8  type;                                 /* Analog Channel Type, Int, FP, etc */
327     guint8  sf_type;                              /* Analog Scale Factor Type, none, etc */
328     guint16 sf_offset;                            /* Analog Scale Factor Offset */
329 } fm_analog_info;
330
331 /* Holds Information from a single "Fast Meter Configuration" frame.  Required to dissect subsequent "Data" frames. */
332 typedef struct {
333     guint32  fnum;                   /* frame number */
334     guint16  cfg_cmd;                /* holds ID of config command, ie: 0xa5c1 */
335     guint8   num_flags;              /* Number of Flag Bytes           */
336     guint8   num_ai;                 /* Number of Analog Inputs        */
337     guint8   num_ai_samples;         /* Number samples per Analog Input */
338     guint16  offset_ai;              /* Start Offset of Analog Inputs  */
339     guint8   num_dig;                /* Number of Digital Input Blocks */
340     guint16  offset_dig;             /* Start Offset of Digital Inputs */
341     guint16  offset_ts;              /* Start Offset of Time Stamp     */
342     guint8   num_calc;               /* Number of Calculations         */
343     fm_analog_info *analogs;         /* Array of fm_analog_infos       */
344 } fm_config_frame;
345
346 /**************************************************************************************/
347 /* Fast SER Message Data Item struct */
348 /**************************************************************************************/
349 /* Holds Configuration Information required to decode a Fast SER Data Item            */
350 /* Each data region format is returned as a sequential list of tags, w/o reference to */
351 /* an absolute address.  The format information will consist of a name, a data type   */
352 /* and a quantity of values contained within the data item.  We will retrieve this    */
353 /* format information later while attempting to dissect Read Response frames          */
354 typedef struct {
355     guint32  fnum;                              /* frame number */
356     guint32  base_address;                      /* Base address of Data Item Region                         */
357     guint8   index_pos;                         /* Index Offset Position within data format message (1-16)  */
358     gchar    name[10+1];                        /* Name of Data Item, 10 chars, null-terminated             */
359     guint16  quantity;                          /* Quantity of values within Data Item                      */
360     guint16  data_type;                         /* Data Item Type, Char, Int, FP, etc                       */
361 } fastser_dataitem;
362
363 /**************************************************************************************/
364 /* Fast SER Message Data Region struct */
365 /**************************************************************************************/
366 /* Holds Configuration Information required to decode a Fast SER Data Region          */
367 /* Each data region format is returned as a sequential list of tags, w/o reference to */
368 typedef struct {
369     gchar    name[10+1];                        /* Name of Data Region, 10 chars, null-terminated              */
370 } fastser_dataregion;
371
372 /**************************************************************************************/
373 /* Fast Message Conversation struct */
374 /**************************************************************************************/
375 typedef struct {
376     wmem_slist_t *fm_config_frames;      /* List contains a fm_config_data struct for each Fast Meter configuration frame */
377     wmem_slist_t *fastser_dataitems;     /* List contains a fastser_dataitem struct for each Fast SER Data Item */
378     wmem_tree_t  *fastser_dataregions;   /* Tree contains a fastser_dataregion struct for each Fast SER Data Region */
379 } fm_conversation;
380
381
382 static const value_string selfm_msgtype_vals[] = {
383     { CMD_FAST_SER,              "Fast SER Block"                                  },  /* 0xA546 */
384     { CMD_CLEAR_STATBIT,         "Clear Status Bits Command"                       },  /* 0xA5B9 */
385     { CMD_RELAY_DEF,             "Relay Definition Block"                          },  /* 0xA5C0 */
386     { CMD_FM_CONFIG,             "Fast Meter Configuration Block"                  },  /* 0xA5C1 */
387     { CMD_DFM_CONFIG,            "Demand Fast Meter Configuration Block"           },  /* 0xA5C2 */
388     { CMD_PDFM_CONFIG,           "Peak Demand Fast Meter Configuration Block"      },  /* 0xA5C3 */
389     { CMD_FASTOP_RESETDEF,       "Fast Operate Reset Definition"                   },  /* 0xA5CD */
390     { CMD_FASTOP_CONFIG,         "Fast Operate Configuration"                      },  /* 0xA5CE */
391     { CMD_ALT_FASTOP_CONFIG,     "Alternate Fast Operate Configuration"            },  /* 0xA5CF */
392     { CMD_FM_DATA,               "Fast Meter Data Block"                           },  /* 0xA5D1 */
393     { CMD_DFM_DATA,              "Demand Fast Meter Data Block"                    },  /* 0xA5D2 */
394     { CMD_PDFM_DATA,             "Peak Demand Fast Meter Data Block"               },  /* 0xA5D3 */
395     { CMD_FASTOP_RB_CTRL,        "Fast Operate Remote Bit Control"                 },  /* 0xA5E0 */
396     { CMD_FASTOP_BR_CTRL,        "Fast Operate Breaker Bit Control"                },  /* 0xA5E3 */
397     { CMD_ALT_FASTOP_OPEN,       "Alternate Fast Operate Open Breaker Control"     },  /* 0xA5E5 */
398     { CMD_ALT_FASTOP_CLOSE,      "Alternate Fast Operate Close Breaker Control"    },  /* 0xA5E6 */
399     { CMD_ALT_FASTOP_SET,        "Alternate Fast Operate Set Remote Bit Control"   },  /* 0xA5E7 */
400     { CMD_ALT_FASTOP_CLEAR,      "Alternate Fast Operate Clear Remote Bit Control" },  /* 0xA5E8 */
401     { CMD_ALT_FASTOP_PULSE,      "Alternate Fast Operate Pulse Remote Bit Control" },  /* 0xA5E9 */
402     { CMD_FASTOP_RESET,          "Fast Operate Reset"                              },  /* 0xA5ED */
403     { 0,                         NULL }
404 };
405 static value_string_ext selfm_msgtype_vals_ext = VALUE_STRING_EXT_INIT(selfm_msgtype_vals);
406
407 static const value_string selfm_relaydef_proto_vals[] = {
408     { 0x0000,  "SEL Fast Meter" },
409     { 0x0001,  "SEL Limited Multidrop (LMD)" },
410     { 0x0002,  "Modbus" },
411     { 0x0003,  "SY/MAX" },
412     { 0x0004,  "SEL Relay-to-Relay" },
413     { 0x0005,  "DNP 3.0" },
414     { 0x0006,  "SEL Mirrored Bits" },
415     { 0x0007,  "IEEE 37.118 Synchrophasors" },
416     { 0x0008,  "IEC 61850" },
417     { 0x0100,  "SEL Fast Meter w/ Fast Operate" },
418     { 0x0101,  "SEL Limited Multidrop (LMD) w/ Fast Operate" },
419     { 0x0200,  "SEL Fast Meter w/ Fast SER" },
420     { 0x0300,  "SEL Fast Meter w/ Fast Operate and Fast SER" },
421     { 0x0301,  "SEL Limited Multidrop (LMD) w/ Fast Operate and Fast SER" },
422     { 0,                         NULL }
423 };
424 static value_string_ext selfm_relaydef_proto_vals_ext = VALUE_STRING_EXT_INIT(selfm_relaydef_proto_vals);
425
426 static const value_string selfm_fmconfig_ai_chtype_vals[] = {
427     { FM_CONFIG_ANA_CHTYPE_INT16,  "16-Bit Integer" },
428     { FM_CONFIG_ANA_CHTYPE_FP,     "IEEE Floating Point" },
429     { FM_CONFIG_ANA_CHTYPE_FPD,    "IEEE Floating Point (Double)" },
430     { FM_CONFIG_ANA_CHTYPE_TS,     "8-byte Time Stamp" },
431     { 0,                           NULL }
432 };
433
434 static const value_string selfm_fmconfig_ai_sftype_vals[] = {
435     { FM_CONFIG_ANA_SFTYPE_INT16,  "16-Bit Integer" },
436     { FM_CONFIG_ANA_SFTYPE_FP,     "IEEE Floating Point" },
437     { FM_CONFIG_ANA_SFTYPE_FPD,    "IEEE Floating Point (Double)" },
438     { FM_CONFIG_ANA_SFTYPE_TS,     "8-byte Time Stamp" },
439     { FM_CONFIG_ANA_SFTYPE_NONE,   "None" },
440     { 0,                           NULL }
441 };
442
443 static const value_string selfm_fmconfig_sfloc_vals[] = {
444     { FM_CONFIG_SF_LOC_FM,  "In Fast Meter Message" },
445     { FM_CONFIG_SF_LOC_CFG, "In Configuration Message" },
446     { 0,                           NULL }
447 };
448
449 /* Depending on number of analog samples present in Fast Meter Messages, identification of data will change */
450 static const value_string selfm_fmconfig_numsamples1_vals[] = {
451     { 1,              "Magnitudes Only" },
452     { 0,                           NULL }
453 };
454
455 static const value_string selfm_fmconfig_numsamples2_vals[] = {
456     { 1,              "Imaginary Components" },
457     { 2,              "Real Components" },
458     { 0,                           NULL }
459 };
460
461 static const value_string selfm_fmconfig_numsamples4_vals[] = {
462     { 1,              "1st Quarter Cycle Data" },
463     { 2,              "2nd Quarter Cycle Data" },
464     { 3,              "5th Quarter-Cycle Data" },
465     { 4,              "6th Quarter-Cycle Data" },
466     { 0,                           NULL }
467 };
468
469 /* Calculation Block lookup values */
470 static const value_string selfm_fmconfig_cblk_rot_vals[] = {
471     { 0x00,      "ABC Rotation" },
472     { 0x01,      "ACB Rotation" },
473     { 0,         NULL           }
474 };
475
476 static const value_string selfm_fmconfig_cblk_vconn_vals[] = {
477     { 0x00,      "Y-Connected" },
478     { 0x01,      "Delta-Connected (in seq. Vab, Vbc, Vca)" },
479     { 0x02,      "Delta-Connected (in seq. Vac, Vba, Vcb)" },
480     { 0,         NULL           }
481 };
482
483 static const value_string selfm_fmconfig_cblk_iconn_vals[] = {
484     { 0x00,      "Y-Connected" },
485     { 0x01,      "Delta-Connected (in seq. Iab, Ibc, Ica)" },
486     { 0x02,      "Delta-Connected (in seq. Iac, Iba, Icb)" },
487     { 0,         NULL           }
488 };
489
490 static const value_string selfm_fmconfig_cblk_ctype_vals[] = {
491     { 0,      "Standard Power Calculations" },
492     { 1,      "2-1/2 Element Delta Power Calculation" },
493     { 2,      "Voltages-Only" },
494     { 3,      "Currents-Only" },
495     { 4,      "Single-Phase Ia and Va Only" },
496     { 5,      "Standard Power Calcs with 2 sets of Currents" },
497     { 6,      "2-1/2 Element Delta Power Calcs with 2 sets of Currents" },
498     { 0,         NULL           }
499 };
500
501 /* Fast Operate Remote Bit 'Pulse Supported' Lookup */
502 static const value_string selfm_foconfig_prb_supp_vals[] = {
503     { 0x00,  "No" },
504     { 0x01,  "Yes" },
505     { 0,                      NULL }
506 };
507
508 /* SER Status Value Lookup */
509 static const value_string selfm_ser_status_vals[] = {
510     { 0x00,  "Deasserted" },
511     { 0x01,  "Asserted" },
512     { 0,  NULL }
513 };
514
515 /* Fast Operate Remote Bit Lookup */
516 static const value_string selfm_fo_rb_vals[] = {
517     { 0x00,  "RB01 Clear" },
518     { 0x20,  "RB01 Set" },
519     { 0x40,  "RB01 Pulse" },
520     { 0x01,  "RB02 Clear" },
521     { 0x21,  "RB02 Set" },
522     { 0x41,  "RB02 Pulse" },
523     { 0x02,  "RB03 Clear" },
524     { 0x22,  "RB03 Set" },
525     { 0x42,  "RB03 Pulse" },
526     { 0x03,  "RB04 Clear" },
527     { 0x23,  "RB04 Set" },
528     { 0x43,  "RB04 Pulse" },
529     { 0x04,  "RB05 Clear" },
530     { 0x24,  "RB05 Set" },
531     { 0x44,  "RB05 Pulse" },
532     { 0x05,  "RB06 Clear" },
533     { 0x25,  "RB06 Set" },
534     { 0x45,  "RB06 Pulse" },
535     { 0x06,  "RB07 Clear" },
536     { 0x26,  "RB07 Set" },
537     { 0x46,  "RB07 Pulse" },
538     { 0x07,  "RB08 Clear" },
539     { 0x27,  "RB08 Set" },
540     { 0x47,  "RB08 Pulse" },
541     { 0x08,  "RB09 Clear" },
542     { 0x28,  "RB09 Set" },
543     { 0x48,  "RB09 Pulse" },
544     { 0x09,  "RB10 Clear" },
545     { 0x29,  "RB10 Set" },
546     { 0x49,  "RB10 Pulse" },
547     { 0x0A,  "RB11 Clear" },
548     { 0x2A,  "RB11 Set" },
549     { 0x4A,  "RB11 Pulse" },
550     { 0x0B,  "RB12 Clear" },
551     { 0x2B,  "RB12 Set" },
552     { 0x4B,  "RB12 Pulse" },
553     { 0x0C,  "RB13 Clear" },
554     { 0x2C,  "RB13 Set" },
555     { 0x4C,  "RB13 Pulse" },
556     { 0x0D,  "RB14 Clear" },
557     { 0x2D,  "RB14 Set" },
558     { 0x4D,  "RB14 Pulse" },
559     { 0x0E,  "RB15 Clear" },
560     { 0x2E,  "RB15 Set" },
561     { 0x4E,  "RB15 Pulse" },
562     { 0x0F,  "RB16 Clear" },
563     { 0x2F,  "RB16 Set" },
564     { 0x4F,  "RB16 Pulse" },
565     { 0x10,  "RB17 Clear" },
566     { 0x30,  "RB17 Set" },
567     { 0x50,  "RB17 Pulse" },
568     { 0x11,  "RB18 Clear" },
569     { 0x31,  "RB18 Set" },
570     { 0x51,  "RB18 Pulse" },
571     { 0x12,  "RB19 Clear" },
572     { 0x32,  "RB19 Set" },
573     { 0x52,  "RB19 Pulse" },
574     { 0x13,  "RB20 Clear" },
575     { 0x33,  "RB20 Set" },
576     { 0x53,  "RB20 Pulse" },
577     { 0x14,  "RB21 Clear" },
578     { 0x34,  "RB21 Set" },
579     { 0x54,  "RB21 Pulse" },
580     { 0x15,  "RB22 Clear" },
581     { 0x35,  "RB22 Set" },
582     { 0x55,  "RB22 Pulse" },
583     { 0x16,  "RB23 Clear" },
584     { 0x36,  "RB23 Set" },
585     { 0x56,  "RB23 Pulse" },
586     { 0x17,  "RB24 Clear" },
587     { 0x37,  "RB24 Set" },
588     { 0x57,  "RB24 Pulse" },
589     { 0x18,  "RB25 Clear" },
590     { 0x38,  "RB25 Set" },
591     { 0x58,  "RB25 Pulse" },
592     { 0x19,  "RB26 Clear" },
593     { 0x39,  "RB26 Set" },
594     { 0x59,  "RB26 Pulse" },
595     { 0x1A,  "RB27 Clear" },
596     { 0x3A,  "RB27 Set" },
597     { 0x5A,  "RB27 Pulse" },
598     { 0x1B,  "RB28 Clear" },
599     { 0x3B,  "RB28 Set" },
600     { 0x5B,  "RB28 Pulse" },
601     { 0x1C,  "RB29 Clear" },
602     { 0x3C,  "RB29 Set" },
603     { 0x5C,  "RB29 Pulse" },
604     { 0x1D,  "RB30 Clear" },
605     { 0x3D,  "RB30 Set" },
606     { 0x5D,  "RB30 Pulse" },
607     { 0x1E,  "RB31 Clear" },
608     { 0x3E,  "RB31 Set" },
609     { 0x5E,  "RB31 Pulse" },
610     { 0x1F,  "RB32 Clear" },
611     { 0x3F,  "RB32 Set" },
612     { 0x5F,  "RB32 Pulse" },
613     { 0,             NULL }
614 };
615
616 /* Fast Operate Breaker Bit Lookup */
617 static const value_string selfm_fo_br_vals[] = {
618     { 0x31, "Breaker Bit 1 Open (OC/OC1)" },
619     { 0x11, "Breaker Bit 1 Close (CC/CC1)" },
620     { 0x32, "Breaker Bit 2 Open (OC2)" },
621     { 0x12, "Breaker Bit 2 Close (CC2)" },
622     { 0x33, "Breaker Bit 3 Open (OC3)" },
623     { 0x13, "Breaker Bit 3 Close (CC3)" },
624     { 0x34, "Breaker Bit 4 Open (OC4)" },
625     { 0x14, "Breaker Bit 4 Close (CC4)" },
626     { 0x35, "Breaker Bit 5 Open (OC5)" },
627     { 0x15, "Breaker Bit 5 Close (CC5)" },
628     { 0x36, "Breaker Bit 6 Open (OC6)" },
629     { 0x16, "Breaker Bit 6 Close (CC6)" },
630     { 0x37, "Breaker Bit 7 Open (OC7)" },
631     { 0x17, "Breaker Bit 7 Close (CC7)" },
632     { 0x38, "Breaker Bit 8 Open (OC8)" },
633     { 0x18, "Breaker Bit 8 Close (CC8)" },
634     { 0x39, "Breaker Bit 9 Open (OC9)" },
635     { 0x19, "Breaker Bit 9 Close (CC9)" },
636     { 0x3A, "Breaker Bit 10 Open (OC10)" },
637     { 0x1A, "Breaker Bit 10 Close (CC10)" },
638     { 0x3B, "Breaker Bit 11 Open (OC11)" },
639     { 0x1B, "Breaker Bit 11 Close (CC11)" },
640     { 0x3C, "Breaker Bit 12 Open (OC12)" },
641     { 0x1C, "Breaker Bit 12 Close (CC12)" },
642     { 0x3D, "Breaker Bit 13 Open (OC13)" },
643     { 0x1D, "Breaker Bit 13 Close (CC13)" },
644     { 0x3E, "Breaker Bit 14 Open (OC14)" },
645     { 0x1E, "Breaker Bit 14 Close (CC14)" },
646     { 0x3F, "Breaker Bit 15 Open (OC15)" },
647     { 0x1F, "Breaker Bit 15 Close (CC15)" },
648     { 0x40, "Breaker Bit 16 Open (OC16)" },
649     { 0x20, "Breaker Bit 16 Close (CC16)" },
650     { 0x41, "Breaker Bit 17 Open (OC17)" },
651     { 0x21, "Breaker Bit 17 Close (CC17)" },
652     { 0x42, "Breaker Bit 18 Open (OC18)" },
653     { 0x22, "Breaker Bit 18 Close (CC18)" },
654     { 0,                           NULL }
655 };
656
657 /* Alternate Fast Operate Function Code Lookup */
658 static const value_string selfm_foconfig_alt_funccode_vals[] = {
659     { 0xE5, "Open Breaker Bit"  },
660     { 0xE6, "Close Breaker Bit" },
661     { 0xE7, "Set Remote Bit"    },
662     { 0xE8, "Clear Remote Bit"  },
663     { 0xE9, "Pulse Remote Bit"  },
664     { 0x00, "Unsupported"       },
665     { 0,                   NULL }
666 };
667
668 /* Fast SER Message Function Codes */
669 static const value_string selfm_fastser_func_code_vals[] = {
670     { FAST_SER_MESSAGE_DEF,       "Fast SER Message Definition Block" },
671     { FAST_SER_MESSAGE_DEF_ACK,   "Fast SER Message Definition Block ACK" },
672     { FAST_SER_EN_UNS_DATA,       "Enable Unsolicited Data" },
673     { FAST_SER_EN_UNS_DATA_ACK,   "Enable Unsolicited Data ACK" },
674     { FAST_SER_DIS_UNS_DATA,      "Disable Unsolicited Data" },
675     { FAST_SER_DIS_UNS_DATA_ACK,  "Disable Unsolicited Data ACK" },
676     { FAST_SER_PING,              "Ping Message" },
677     { FAST_SER_PING_ACK,          "Ping Message ACK" },
678     { FAST_SER_READ_REQ,          "Read Request" },
679     { FAST_SER_READ_RESP,         "Read Response" },
680     { FAST_SER_GEN_UNS_DATA,      "Generic Unsolicited Data" },
681     { FAST_SER_SOE_STATE_REQ,     "SOE Present State Request" },
682     { FAST_SER_SOE_STATE_RESP,    "SOE Present State Response" },
683     { FAST_SER_UNS_RESP,          "Unsolicited Fast SER Data Response" },
684     { FAST_SER_UNS_RESP_ACK,      "Unsolicited Fast SER Data Response ACK" },
685     { FAST_SER_UNS_WRITE,         "Unsolicited Write" },
686     { FAST_SER_UNS_WRITE_REQ,     "Unsolicited Write Request" },
687     { FAST_SER_DEVDESC_REQ,       "Device Description Request" },
688     { FAST_SER_DEVDESC_RESP,      "Device Description Response" },
689     { FAST_SER_DATAFMT_REQ,       "Data Format Request" },
690     { FAST_SER_DATAFMT_RESP,      "Data Format Response" },
691     { FAST_SER_UNS_DATAFMT_RESP,  "Unsolicited Data Format Response" },
692     { FAST_SER_BITLABEL_REQ,      "Bit Label Request" },
693     { FAST_SER_BITLABEL_RESP,     "Bit Label Response" },
694     { FAST_SER_MGMT_REQ,          "Management Request" },
695     { 0,                           NULL }
696 };
697
698 static const value_string selfm_fastser_seq_vals[] = {
699   { FAST_SER_SEQ_FIN,  "FIN" },
700   { FAST_SER_SEQ_FIR,  "FIR" },
701   { 0,  NULL }
702 };
703
704 static const value_string selfm_fastser_tagtype_vals[] = {
705   { FAST_SER_TAGTYPE_CHAR8,        "1 x 8-bit character per item" },
706   { FAST_SER_TAGTYPE_CHAR16,       "2 x 8-bit characters per item" },
707   { FAST_SER_TAGTYPE_DIGWORD8_BL,  "8-bit binary item, with labels" },
708   { FAST_SER_TAGTYPE_DIGWORD8,     "8-bit binary item, without labels" },
709   { FAST_SER_TAGTYPE_DIGWORD16_BL, "16-bit binary item, with labels" },
710   { FAST_SER_TAGTYPE_DIGWORD16,    "16-bit binary item, without labels" },
711   { FAST_SER_TAGTYPE_INT16,        "16-bit Signed Integer" },
712   { FAST_SER_TAGTYPE_UINT16,       "16-bit Unsigned Integer" },
713   { FAST_SER_TAGTYPE_INT32,        "32-bit Signed Integer" },
714   { FAST_SER_TAGTYPE_UINT32,       "32-bit Unsigned Integer" },
715   { FAST_SER_TAGTYPE_FLOAT,        "IEEE Floating Point" },
716   { 0,  NULL }
717 };
718
719
720 /* Fast Message Unsolicited Write COM Port Codes */
721 static const value_string selfm_fastser_unswrite_com_vals[] = {
722   { 0x0100,   "COM01" },
723   { 0x0200,   "COM02" },
724   { 0x0300,   "COM03" },
725   { 0x0400,   "COM04" },
726   { 0x0500,   "COM05" },
727   { 0x0600,   "COM06" },
728   { 0x0700,   "COM07" },
729   { 0x0800,   "COM08" },
730   { 0x0900,   "COM09" },
731   { 0x0A00,   "COM10" },
732   { 0x0B00,   "COM11" },
733   { 0x0C00,   "COM12" },
734   { 0x0D00,   "COM13" },
735   { 0x0E00,   "COM14" },
736   { 0x0F00,   "COM15" },
737   { 0,  NULL }
738 };
739
740 /* Tables for reassembly of fragments. */
741 static reassembly_table selfm_reassembly_table;
742
743 /* ************************************************************************* */
744 /*                   Header values for reassembly                            */
745 /* ************************************************************************* */
746 static int   hf_selfm_fragment  = -1;
747 static int   hf_selfm_fragments = -1;
748 static int   hf_selfm_fragment_overlap = -1;
749 static int   hf_selfm_fragment_overlap_conflict = -1;
750 static int   hf_selfm_fragment_multiple_tails = -1;
751 static int   hf_selfm_fragment_too_long_fragment = -1;
752 static int   hf_selfm_fragment_error = -1;
753 static int   hf_selfm_fragment_count = -1;
754 static int   hf_selfm_fragment_reassembled_in = -1;
755 static int   hf_selfm_fragment_reassembled_length = -1;
756 static gint ett_selfm_fragment  = -1;
757 static gint ett_selfm_fragments = -1;
758
759 static const fragment_items selfm_frag_items = {
760   &ett_selfm_fragment,
761   &ett_selfm_fragments,
762   &hf_selfm_fragments,
763   &hf_selfm_fragment,
764   &hf_selfm_fragment_overlap,
765   &hf_selfm_fragment_overlap_conflict,
766   &hf_selfm_fragment_multiple_tails,
767   &hf_selfm_fragment_too_long_fragment,
768   &hf_selfm_fragment_error,
769   &hf_selfm_fragment_count,
770   &hf_selfm_fragment_reassembled_in,
771   &hf_selfm_fragment_reassembled_length,
772   /* Reassembled data field */
773   NULL,
774   "SEL Fast Message fragments"
775 };
776
777 /**********************************************************************************************************/
778 /* Clean all instances of 0xFFFF from Telnet payload to compensate for IAC control code (replace w/ 0xFF) */
779 /* Function Duplicated from packet-telnet.c (unescape_and_tvbuffify_telnet_option)                        */
780 /**********************************************************************************************************/
781 static tvbuff_t *
782 clean_telnet_iac(packet_info *pinfo, tvbuff_t *tvb, int offset, int len)
783 {
784   tvbuff_t     *telnet_tvb;
785   guint8       *buf;
786   const guint8 *spos;
787   guint8       *dpos;
788   int           skip_byte, len_remaining;
789
790   spos=tvb_get_ptr(tvb, offset, len);
791   buf=(guint8 *)g_malloc(len);
792   dpos=buf;
793   skip_byte = 0;
794   len_remaining = len;
795   while(len_remaining > 0){
796
797     /* Only analyze two sequential bytes of source tvb if we have at least two bytes left */
798     if (len_remaining > 1) {
799         /* If two sequential 0xFF's exist, increment skip_byte counter, decrement  */
800         /* len_remaining by 2 and copy a single 0xFF to dest tvb. */
801         if((spos[0]==0xff) && (spos[1]==0xff)){
802             skip_byte++;
803             len_remaining -= 2;
804             *(dpos++)=0xff;
805             spos+=2;
806             continue;
807         }
808     }
809     /* If we only have a single byte left, or there were no sequential 0xFF's, copy byte from src tvb to dest tvb */
810     *(dpos++)=*(spos++);
811     len_remaining--;
812   }
813   telnet_tvb = tvb_new_child_real_data(tvb, buf, len-skip_byte, len-skip_byte);
814   tvb_set_free_cb(telnet_tvb, g_free);
815   add_new_data_source(pinfo, telnet_tvb, "Processed Telnet Data");
816
817   return telnet_tvb;
818 }
819
820 /******************************************************************************************************/
821 /* Execute dissection of Fast Meter configuration frames independent of any GUI access of said frames */
822 /* Load configuration information into fm_config_frame struct */
823 /******************************************************************************************************/
824 static fm_config_frame* fmconfig_frame_fast(tvbuff_t *tvb)
825 {
826     /* Set up structures needed to add the protocol subtree and manage it */
827     guint           count, offset = 0;
828     fm_config_frame *frame;
829
830     /* get a new frame and initialize it */
831     frame = wmem_new(wmem_file_scope(), fm_config_frame);
832
833     /* Get data packet setup information from config message and copy into ai_info (if required) */
834     frame->cfg_cmd        = tvb_get_ntohs(tvb, offset);
835     /* skip length byte, position offset+2 */
836     frame->num_flags      = tvb_get_guint8(tvb, offset+3);
837     /* skip scale factor location, position offset+4 */
838     /* skip number of scale factors, position offset+5 */
839     frame->num_ai         = tvb_get_guint8(tvb, offset+6);
840     frame->num_ai_samples = tvb_get_guint8(tvb, offset+7);
841     frame->num_dig        = tvb_get_guint8(tvb, offset+8);
842     frame->num_calc       = tvb_get_guint8(tvb, offset+9);
843
844     /* Update offset pointer */
845     offset += 10;
846
847     /* Get data packet analog/timestamp/digital offsets and copy into ai_info */
848     frame->offset_ai  = tvb_get_ntohs(tvb, offset);
849     frame->offset_ts  = tvb_get_ntohs(tvb, offset+2);
850     frame->offset_dig = tvb_get_ntohs(tvb, offset+4);
851
852     /* Update offset pointer */
853     offset += 6;
854
855     frame->analogs = (fm_analog_info *)wmem_alloc(wmem_file_scope(), frame->num_ai * sizeof(fm_analog_info));
856
857     /* Get AI Channel Details and copy into ai_info */
858     for (count = 0; count < frame->num_ai; count++) {
859         fm_analog_info *analog = &(frame->analogs[count]);
860         tvb_memcpy(tvb, analog->name, offset, FM_CONFIG_ANA_CHNAME_LEN);
861         analog->name[FM_CONFIG_ANA_CHNAME_LEN] = '\0'; /* Put a terminating null onto the end of the AI Channel name */
862         analog->type = tvb_get_guint8(tvb, offset+6);
863         analog->sf_type = tvb_get_guint8(tvb, offset+7);
864         analog->sf_offset = tvb_get_ntohs(tvb, offset+8);
865
866         offset += 10;
867     }
868
869     return frame;
870
871 }
872
873 /******************************************************************************************************/
874 /* Execute dissection of Data Item definition info before loading GUI tree                            */
875 /* Load configuration information into fastser_dataitem struct                                        */
876 /******************************************************************************************************/
877 static fastser_dataitem* fastser_dataitem_save(tvbuff_t *tvb, int offset)
878 {
879     fastser_dataitem *dataitem;
880
881     /* get a new dataitem and initialize it */
882     dataitem = wmem_new(wmem_file_scope(), fastser_dataitem);
883
884     /* retrieve data item name and terminate with a null */
885     tvb_memcpy(tvb, dataitem->name, offset, 10);
886     dataitem->name[10] = '\0'; /* Put a terminating null onto the end of the string */
887
888     /* retrieve data item quantity and type */
889     dataitem->quantity = tvb_get_ntohs(tvb, offset+10);
890     dataitem->data_type = tvb_get_ntohs(tvb, offset+12);
891
892     return dataitem;
893
894 }
895
896 /******************************************************************************************************/
897 /* Execute dissection of Data Region definition info before loading GUI tree                          */
898 /* Load configuration information into fastser_dataregion struct                                      */
899 /******************************************************************************************************/
900 static fastser_dataregion* fastser_dataregion_save(tvbuff_t *tvb, int offset)
901 {
902     fastser_dataregion *dataregion;
903
904     /* get a new dataregion and initialize it */
905     dataregion = wmem_new(wmem_file_scope(), fastser_dataregion);
906
907     /* retrieve data region name and terminate with a null */
908     tvb_memcpy(tvb, dataregion->name, offset, 10);
909     dataregion->name[10] = '\0'; /* Put a terminating null onto the end of the string */
910
911     return dataregion;
912
913 }
914
915 /********************************************************************************************************/
916 /* Lookup region name using current base address & saved conversation data.  Return ptr to gchar string */
917 /********************************************************************************************************/
918 const gchar*
919 region_lookup(packet_info *pinfo, guint32 base_addr)
920 {
921     fm_conversation    *conv;
922     fastser_dataregion *dataregion;
923
924     conv = (fm_conversation *)p_get_proto_data(pinfo->fd, proto_selfm, 0);
925
926     dataregion = (fastser_dataregion*)wmem_tree_lookup32(conv->fastser_dataregions, base_addr);
927
928     if (dataregion) {
929         return dataregion->name;
930     }
931
932     /* If we couldn't identify the region using the current base address, return a default string */
933     return "Unknown Region";
934 }
935
936 /******************************************************************************************************/
937 /* Code to Dissect Relay Definition Frames */
938 /******************************************************************************************************/
939 static int
940 dissect_relaydef_frame(tvbuff_t *tvb, proto_tree *tree, int offset)
941 {
942 /* Set up structures needed to add the protocol subtree and manage it */
943     proto_item    *relaydef_item, *relaydef_fm_item, *relaydef_flags_item, *relaydef_proto_item;
944     proto_tree    *relaydef_tree, *relaydef_fm_tree, *relaydef_flags_tree, *relaydef_proto_tree;
945     guint8        len, num_proto, num_fm, num_flags;
946     int           count;
947
948     len = tvb_get_guint8(tvb, offset);
949     num_proto = tvb_get_guint8(tvb, offset+1);
950     num_fm = tvb_get_guint8(tvb, offset+2);
951     num_flags = tvb_get_guint8(tvb, offset+3);
952
953     /* Add items to protocol tree specific to Relay Definition Block */
954     relaydef_item = proto_tree_add_text(tree, tvb, offset, len-2, "Relay Definition Block Details");
955     relaydef_tree = proto_item_add_subtree(relaydef_item, ett_selfm_relaydef);
956
957     /* Reported length */
958     proto_tree_add_item(relaydef_tree, hf_selfm_relaydef_len, tvb, offset, 1, ENC_BIG_ENDIAN);
959
960     /* Reported Number of Protocols Supported */
961     relaydef_proto_item = proto_tree_add_item(relaydef_tree, hf_selfm_relaydef_numproto, tvb, offset+1, 1, ENC_BIG_ENDIAN);
962     relaydef_proto_tree = proto_item_add_subtree(relaydef_proto_item, ett_selfm_relaydef_proto);
963
964     /* Reported Number of Fast Meter Commands Supported */
965     relaydef_fm_item = proto_tree_add_item(relaydef_tree, hf_selfm_relaydef_numfm, tvb, offset+2, 1, ENC_BIG_ENDIAN);
966     relaydef_fm_tree = proto_item_add_subtree(relaydef_fm_item, ett_selfm_relaydef_fm);
967
968     /* Reported Number of Status Bit Flags Supported */
969     relaydef_flags_item = proto_tree_add_item(relaydef_tree, hf_selfm_relaydef_numflags, tvb, offset+3, 1, ENC_BIG_ENDIAN);
970     relaydef_flags_tree = proto_item_add_subtree(relaydef_flags_item, ett_selfm_relaydef_flags);
971
972     /* Get our offset up-to-date */
973     offset += 4;
974
975     /* Add each reported Fast Meter cfg/data message */
976     for (count = 1; count <= num_fm; count++) {
977         proto_tree_add_item(relaydef_fm_tree, hf_selfm_relaydef_fmcfg_cmd, tvb, offset, 2, ENC_BIG_ENDIAN);
978         proto_tree_add_item(relaydef_fm_tree, hf_selfm_relaydef_fmdata_cmd, tvb, offset+2, 2, ENC_BIG_ENDIAN);
979         offset += 4;
980     }
981
982     /* Add each reported status bit flag, along with corresponding response command */
983     for (count = 1; count <= num_flags; count++) {
984         proto_tree_add_item(relaydef_flags_tree, hf_selfm_relaydef_statbit, tvb, offset, 2, ENC_BIG_ENDIAN);
985         proto_tree_add_item(relaydef_flags_tree, hf_selfm_relaydef_statbit_cmd, tvb, offset+2, 6, ENC_NA);
986         offset += 8;
987     }
988
989     /* Add each supported protocol */
990     for (count = 1; count <= num_proto; count++) {
991         proto_tree_add_item(relaydef_proto_tree, hf_selfm_relaydef_proto, tvb, offset, 2, ENC_BIG_ENDIAN);
992         offset += 2;
993     }
994
995     return tvb_length(tvb);
996
997 }
998
999 /******************************************************************************************************/
1000 /* Code to dissect Fast Meter Configuration Frames */
1001 /******************************************************************************************************/
1002 static int
1003 dissect_fmconfig_frame(tvbuff_t *tvb, proto_tree *tree, int offset)
1004 {
1005     /* Set up structures needed to add the protocol subtree and manage it */
1006     proto_item    *fmconfig_item, *fmconfig_ai_item=NULL, *fmconfig_calc_item=NULL;
1007     proto_tree    *fmconfig_tree, *fmconfig_ai_tree=NULL, *fmconfig_calc_tree=NULL;
1008     guint         count;
1009     guint8        len, num_ai, num_calc;
1010     gchar         ai_name[FM_CONFIG_ANA_CHNAME_LEN+1]; /* 6 Characters + a Null */
1011
1012     len = tvb_get_guint8(tvb, offset);
1013     /* skip num_flags, position offset+1 */
1014     /* skip sf_loc,    position offset+2 */
1015     /* skip num_sf,    position offset+3 */
1016     num_ai = tvb_get_guint8(tvb, offset+4);
1017     /* skip num_samp,  position offset+5 */
1018     /* skip num_dig,   position offset+6 */
1019     num_calc = tvb_get_guint8(tvb, offset+7);
1020
1021     fmconfig_item = proto_tree_add_text(tree, tvb, offset, len, "Fast Meter Configuration Details");
1022     fmconfig_tree = proto_item_add_subtree(fmconfig_item, ett_selfm_fmconfig);
1023
1024     /* Add items to protocol tree specific to Fast Meter Configuration Block */
1025
1026     /* Get Setup Information for FM Config Block */
1027     proto_tree_add_item(fmconfig_tree, hf_selfm_fmconfig_len, tvb, offset, 1, ENC_BIG_ENDIAN);
1028     proto_tree_add_item(fmconfig_tree, hf_selfm_fmconfig_numflags, tvb, offset+1, 1, ENC_BIG_ENDIAN);
1029     proto_tree_add_item(fmconfig_tree, hf_selfm_fmconfig_loc_sf, tvb, offset+2, 1, ENC_BIG_ENDIAN);
1030     proto_tree_add_item(fmconfig_tree, hf_selfm_fmconfig_num_sf, tvb, offset+3, 1, ENC_BIG_ENDIAN);
1031     proto_tree_add_item(fmconfig_tree, hf_selfm_fmconfig_num_ai, tvb, offset+4, 1, ENC_BIG_ENDIAN);
1032     proto_tree_add_item(fmconfig_tree, hf_selfm_fmconfig_num_samp, tvb, offset+5, 1, ENC_BIG_ENDIAN);
1033     proto_tree_add_item(fmconfig_tree, hf_selfm_fmconfig_num_dig, tvb, offset+6, 1, ENC_BIG_ENDIAN);
1034     proto_tree_add_item(fmconfig_tree, hf_selfm_fmconfig_num_calc, tvb, offset+7, 1, ENC_BIG_ENDIAN);
1035
1036     /* Update offset pointer */
1037     offset += 8;
1038
1039     /* Add data packet offsets to tree and update offset pointer */
1040     proto_tree_add_item(fmconfig_tree, hf_selfm_fmconfig_ofs_ai, tvb, offset, 2, ENC_BIG_ENDIAN);
1041     proto_tree_add_item(fmconfig_tree, hf_selfm_fmconfig_ofs_ts, tvb, offset+2, 2, ENC_BIG_ENDIAN);
1042     proto_tree_add_item(fmconfig_tree, hf_selfm_fmconfig_ofs_dig, tvb, offset+4, 2, ENC_BIG_ENDIAN);
1043     offset += 6;
1044
1045     /* Get AI Channel Details */
1046     for (count = 0; count < num_ai; count++) {
1047         tvb_memcpy(tvb, &ai_name, offset, 6);
1048         ai_name[FM_CONFIG_ANA_CHNAME_LEN] = '\0'; /* Put a terminating null onto the end of the AI name, in case none exists */
1049
1050         fmconfig_ai_item = proto_tree_add_text(fmconfig_tree, tvb, offset, 10, "Analog Channel: %s", ai_name);
1051         fmconfig_ai_tree = proto_item_add_subtree(fmconfig_ai_item, ett_selfm_fmconfig_ai);
1052
1053         /* Add Channel Name, Channel Data Type, Scale Factor Type and Scale Factor Offset to tree */
1054         proto_tree_add_text(fmconfig_ai_tree, tvb, offset, 6, "Analog Channel Name: %s", ai_name);
1055         proto_tree_add_item(fmconfig_ai_tree, hf_selfm_fmconfig_ai_type, tvb, offset+6, 1, ENC_BIG_ENDIAN);
1056         proto_tree_add_item(fmconfig_ai_tree, hf_selfm_fmconfig_ai_sf_type, tvb, offset+7, 1, ENC_BIG_ENDIAN);
1057         proto_tree_add_item(fmconfig_ai_tree, hf_selfm_fmconfig_ai_sf_ofs, tvb, offset+8, 2, ENC_BIG_ENDIAN);
1058
1059         /* Update Offset Pointer */
1060         offset += 10;
1061     }
1062
1063     /* 14-byte Calculation block instances based on num_calc */
1064     for (count = 0; count < num_calc; count++) {
1065         fmconfig_calc_item = proto_tree_add_text(fmconfig_tree, tvb, offset, 14, "Calculation Block: %d", count+1);
1066         fmconfig_calc_tree = proto_item_add_subtree(fmconfig_calc_item, ett_selfm_fmconfig_calc);
1067
1068         /* Rotation, Voltage Connection and Current Connection are all bit-masked on the same byte */
1069         proto_tree_add_item(fmconfig_calc_tree, hf_selfm_fmconfig_cblk_rot, tvb, offset, 1, ENC_BIG_ENDIAN);
1070         proto_tree_add_item(fmconfig_calc_tree, hf_selfm_fmconfig_cblk_vconn, tvb, offset, 1, ENC_BIG_ENDIAN);
1071         proto_tree_add_item(fmconfig_calc_tree, hf_selfm_fmconfig_cblk_iconn, tvb, offset, 1, ENC_BIG_ENDIAN);
1072
1073         proto_tree_add_item(fmconfig_calc_tree, hf_selfm_fmconfig_cblk_ctype, tvb, offset+1, 1, ENC_BIG_ENDIAN);
1074         proto_tree_add_item(fmconfig_calc_tree, hf_selfm_fmconfig_cblk_deskew_ofs, tvb, offset+2, 2, ENC_BIG_ENDIAN);
1075         proto_tree_add_item(fmconfig_calc_tree, hf_selfm_fmconfig_cblk_rs_ofs, tvb, offset+4, 2, ENC_BIG_ENDIAN);
1076         proto_tree_add_item(fmconfig_calc_tree, hf_selfm_fmconfig_cblk_xs_ofs, tvb, offset+6, 2, ENC_BIG_ENDIAN);
1077         proto_tree_add_item(fmconfig_calc_tree, hf_selfm_fmconfig_cblk_ia_idx, tvb, offset+8, 1, ENC_BIG_ENDIAN);
1078         proto_tree_add_item(fmconfig_calc_tree, hf_selfm_fmconfig_cblk_ib_idx, tvb, offset+9, 1, ENC_BIG_ENDIAN);
1079         proto_tree_add_item(fmconfig_calc_tree, hf_selfm_fmconfig_cblk_ic_idx, tvb, offset+10, 1, ENC_BIG_ENDIAN);
1080         proto_tree_add_item(fmconfig_calc_tree, hf_selfm_fmconfig_cblk_va_idx, tvb, offset+11, 1, ENC_BIG_ENDIAN);
1081         proto_tree_add_item(fmconfig_calc_tree, hf_selfm_fmconfig_cblk_vb_idx, tvb, offset+12, 1, ENC_BIG_ENDIAN);
1082         proto_tree_add_item(fmconfig_calc_tree, hf_selfm_fmconfig_cblk_vc_idx, tvb, offset+13, 1, ENC_BIG_ENDIAN);
1083     }
1084
1085
1086     return tvb_length(tvb);
1087
1088 }
1089
1090 /******************************************************************************************************/
1091 /* Code to dissect Fast Meter Data Frames */
1092 /* Formatting depends heavily on previously-encountered Configuration Frames so search array instances for them */
1093 /******************************************************************************************************/
1094 static int
1095 dissect_fmdata_frame(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo, int offset, guint16 config_cmd_match)
1096 {
1097 /* Set up structures needed to add the protocol subtree and manage it */
1098     proto_item       *fmdata_item, *fmdata_ai_item=NULL, *fmdata_dig_item=NULL, *fmdata_ai_ch_item=NULL, *fmdata_dig_ch_item=NULL;
1099     proto_tree       *fmdata_tree, *fmdata_ai_tree=NULL, *fmdata_dig_tree=NULL, *fmdata_ai_ch_tree=NULL, *fmdata_dig_ch_tree=NULL;
1100     guint8           len, i=0, j=0, ts_mon, ts_day, ts_year, ts_hour, ts_min, ts_sec;
1101     guint16          config_cmd, ts_msec, ai_int16val;
1102     gfloat           ai_fpval, ai_sf_fp;
1103     gdouble          ai_fpd_val;
1104     gboolean         config_found = FALSE;
1105     fm_conversation  *conv;
1106     fm_config_frame  *cfg_data;
1107     gint             cnt = 0, ch_size=0;
1108
1109     len = tvb_get_guint8(tvb, offset);
1110
1111     fmdata_item = proto_tree_add_text(tree, tvb, offset, len-2, "Fast Meter Data Details");
1112     fmdata_tree = proto_item_add_subtree(fmdata_item, ett_selfm_fmdata);
1113
1114     /* Reported length */
1115     proto_tree_add_item(fmdata_tree, hf_selfm_fmdata_len, tvb, offset, 1, ENC_BIG_ENDIAN);
1116     offset += 1;
1117
1118     /* Search for previously-encountered Configuration information to dissect the frame */
1119     {
1120         conv = (fm_conversation *)p_get_proto_data(pinfo->fd, proto_selfm, 0);
1121
1122         if (conv) {
1123             wmem_slist_frame_t *frame = wmem_slist_front(conv->fm_config_frames);
1124             /* Cycle through possible instances of multiple fm_config_data_blocks, looking for match */
1125             while (frame && !config_found) {
1126                 cfg_data = (fm_config_frame *)wmem_slist_frame_data(frame);
1127                 config_cmd = cfg_data->cfg_cmd;
1128
1129                 /* If the stored config_cmd matches the expected one we are looking for, mark that the config data was found */
1130                 if (config_cmd == config_cmd_match) {
1131                     proto_item_append_text(fmdata_item, ", using frame number %"G_GUINT32_FORMAT" as Configuration Frame",
1132                                    cfg_data->fnum);
1133                     config_found = TRUE;
1134                 }
1135
1136                 frame = wmem_slist_frame_next(frame);
1137             }
1138
1139             if (config_found) {
1140
1141                 /* Retrieve number of Status Flag bytes and setup tree */
1142                 if (cfg_data->num_flags == 1){
1143                     proto_tree_add_item(fmdata_tree, hf_selfm_fmdata_flagbyte, tvb, offset, 1, ENC_BIG_ENDIAN);
1144                     /*offset += 1;*/
1145                 }
1146
1147                 cnt = cfg_data->num_ai; /* actual number of analog values to available to dissect */
1148
1149                 /* Update our current tvb offset to the actual AI offset saved from the Configuration message */
1150                 offset = cfg_data->offset_ai;
1151
1152                 /* Check that we actually have analog data to dissect */
1153                 if (cnt > 0) {
1154
1155                     /* Include decoding for each Sample provided for the Analog Channels */
1156                     for (j=0; j < cfg_data->num_ai_samples; j++) {
1157
1158                         /* Use different lookup strings, depending on how many samples are available per Analog Channel */
1159                         if (cfg_data->num_ai_samples == 1) {
1160                             fmdata_ai_item = proto_tree_add_text(fmdata_tree, tvb, offset, ((cfg_data->offset_ts - cfg_data->offset_ai)/cfg_data->num_ai_samples),
1161                                 "Analog Channels (%d), Sample: %d (%s)",
1162                                 cfg_data->num_ai, j+1, val_to_str_const(j+1, selfm_fmconfig_numsamples1_vals, "Unknown"));
1163                             fmdata_ai_tree = proto_item_add_subtree(fmdata_ai_item, ett_selfm_fmdata_ai);
1164                         }
1165                         else if (cfg_data->num_ai_samples == 2) {
1166                             fmdata_ai_item = proto_tree_add_text(fmdata_tree, tvb, offset, ((cfg_data->offset_ts - cfg_data->offset_ai)/cfg_data->num_ai_samples),
1167                                 "Analog Channels (%d), Sample: %d (%s)",
1168                                 cfg_data->num_ai, j+1, val_to_str_const(j+1, selfm_fmconfig_numsamples2_vals, "Unknown"));
1169                             fmdata_ai_tree = proto_item_add_subtree(fmdata_ai_item, ett_selfm_fmdata_ai);
1170                         }
1171                         else if (cfg_data->num_ai_samples == 4) {
1172                             fmdata_ai_item = proto_tree_add_text(fmdata_tree, tvb, offset, ((cfg_data->offset_ts - cfg_data->offset_ai)/cfg_data->num_ai_samples),
1173                                 "Analog Channels (%d), Sample: %d (%s)",
1174                                 cfg_data->num_ai, j+1, val_to_str_const(j+1, selfm_fmconfig_numsamples4_vals, "Unknown"));
1175                             fmdata_ai_tree = proto_item_add_subtree(fmdata_ai_item, ett_selfm_fmdata_ai);
1176                         }
1177
1178                         /* For each analog channel we encounter... */
1179                         for (i = 0; i < cnt; i++) {
1180
1181                             fm_analog_info *ai = &(cfg_data->analogs[i]);
1182
1183                             /* Channel size (in bytes) determined by data type */
1184                             switch (ai->type) {
1185                                 case FM_CONFIG_ANA_CHTYPE_INT16:
1186                                     ch_size = 2;    /* 2 bytes */
1187                                     break;
1188                                 case FM_CONFIG_ANA_CHTYPE_FP:
1189                                     ch_size = 4;    /* 4 bytes */
1190                                     break;
1191                                 case FM_CONFIG_ANA_CHTYPE_FPD:
1192                                     ch_size = 8;    /* 8 bytes */
1193                                     break;
1194                                 default:
1195                                     break;
1196                             }
1197
1198                             /* Build sub-tree for each Analog Channel */
1199                             fmdata_ai_ch_item = proto_tree_add_text(fmdata_ai_tree, tvb, offset, ch_size, "Analog Channel %d: %s", i+1, ai->name);
1200                             fmdata_ai_ch_tree = proto_item_add_subtree(fmdata_ai_ch_item, ett_selfm_fmdata_ai_ch);
1201
1202                             /* XXX - Need more decoding options here for different data types, but I need packet capture examples first */
1203                             /* Decode analog value appropriately, according to data type */
1204                             switch (ai->type) {
1205                                 /* Channel type is 16-bit Integer */
1206                                 case FM_CONFIG_ANA_CHTYPE_INT16:
1207                                     ai_int16val = tvb_get_ntohs(tvb, offset);
1208
1209                                     /* If we've got a scale factor offset, apply it before printing the analog */
1210                                     if ((ai->sf_offset != 0) && (ai->sf_type == FM_CONFIG_ANA_SFTYPE_FP)){
1211                                         ai_sf_fp = tvb_get_ntohieee_float(tvb, ai->sf_offset);
1212                                         proto_tree_add_float(fmdata_ai_ch_tree, hf_selfm_fmdata_ai_sf_fp, tvb, ai->sf_offset, 4, ai_sf_fp);
1213                                     }
1214                                     else {
1215                                         ai_sf_fp = 1;
1216                                     }
1217
1218                                     proto_tree_add_text(fmdata_ai_ch_tree, tvb, offset, ch_size, "Value (Raw): %d", ai_int16val);
1219                                     proto_tree_add_text(fmdata_ai_ch_tree, tvb, offset, ch_size, "Value (w/ Scale Factor): %f", ((gfloat)ai_int16val*ai_sf_fp));
1220                                     offset += ch_size;
1221                                     break;
1222                                 /* Channel type is IEEE Floating point */
1223                                 case FM_CONFIG_ANA_CHTYPE_FP:
1224                                     ai_fpval = tvb_get_ntohieee_float(tvb, offset);
1225                                     proto_tree_add_text(fmdata_ai_ch_tree, tvb, offset, ch_size, "Value: %f", ai_fpval);
1226                                     offset += ch_size;
1227                                     break;
1228                                 /* Channel type is Double IEEE Floating point */
1229                                 case FM_CONFIG_ANA_CHTYPE_FPD:
1230                                     ai_fpd_val = tvb_get_ntohieee_double(tvb, offset);
1231                                     proto_tree_add_text(fmdata_ai_ch_tree, tvb, offset, ch_size, "Value: %f", ai_fpd_val);
1232                                     offset += ch_size;
1233                                     break;
1234
1235                             } /* channel type */
1236
1237                         } /* number of analog channels */
1238
1239                     } /* number of samples */
1240
1241                 } /* there were analogs */
1242
1243                 /* Check if we have a time-stamp in this message */
1244                 if (cfg_data->offset_ts != 0xFFFF) {
1245                     /* Retrieve timestamp from 8-byte format                         */
1246                     /* Stored as: month, day, year (xx), hr, min, sec, msec (16-bit) */
1247                     ts_mon  = tvb_get_guint8(tvb, offset);
1248                     ts_day  = tvb_get_guint8(tvb, offset+1);
1249                     ts_year = tvb_get_guint8(tvb, offset+2);
1250                     ts_hour = tvb_get_guint8(tvb, offset+3);
1251                     ts_min  = tvb_get_guint8(tvb, offset+4);
1252                     ts_sec  = tvb_get_guint8(tvb, offset+5);
1253                     ts_msec = tvb_get_ntohs(tvb, offset+6);
1254                     proto_tree_add_text(fmdata_tree, tvb, offset, 8, "Timestamp: %.2d/%.2d/%.2d %.2d:%.2d:%.2d.%.3d", ts_mon, ts_day, ts_year, ts_hour, ts_min, ts_sec, ts_msec);
1255
1256                     offset += 8;
1257                 }
1258
1259                 /* Check that we actually have digital data */
1260                 if (cfg_data->num_dig > 0) {
1261
1262                     fmdata_dig_item = proto_tree_add_text(fmdata_tree, tvb, offset, cfg_data->num_dig, "Digital Channels (%d)", cfg_data->num_dig);
1263                     fmdata_dig_tree = proto_item_add_subtree(fmdata_dig_item, ett_selfm_fmdata_dig);
1264
1265                     for (i=0; i < cfg_data->num_dig; i++) {
1266
1267                         fmdata_dig_ch_item = proto_tree_add_text(fmdata_dig_tree, tvb, offset, 1, "Digital Word Bit Row: %d", i+1);
1268                         fmdata_dig_ch_tree = proto_item_add_subtree(fmdata_dig_ch_item, ett_selfm_fmdata_dig_ch);
1269
1270                         proto_tree_add_item(fmdata_dig_ch_tree, hf_selfm_fmdata_dig_b0, tvb, offset, 1, ENC_BIG_ENDIAN);
1271                         proto_tree_add_item(fmdata_dig_ch_tree, hf_selfm_fmdata_dig_b1, tvb, offset, 1, ENC_BIG_ENDIAN);
1272                         proto_tree_add_item(fmdata_dig_ch_tree, hf_selfm_fmdata_dig_b2, tvb, offset, 1, ENC_BIG_ENDIAN);
1273                         proto_tree_add_item(fmdata_dig_ch_tree, hf_selfm_fmdata_dig_b3, tvb, offset, 1, ENC_BIG_ENDIAN);
1274                         proto_tree_add_item(fmdata_dig_ch_tree, hf_selfm_fmdata_dig_b4, tvb, offset, 1, ENC_BIG_ENDIAN);
1275                         proto_tree_add_item(fmdata_dig_ch_tree, hf_selfm_fmdata_dig_b5, tvb, offset, 1, ENC_BIG_ENDIAN);
1276                         proto_tree_add_item(fmdata_dig_ch_tree, hf_selfm_fmdata_dig_b6, tvb, offset, 1, ENC_BIG_ENDIAN);
1277                         proto_tree_add_item(fmdata_dig_ch_tree, hf_selfm_fmdata_dig_b7, tvb, offset, 1, ENC_BIG_ENDIAN);
1278
1279                         offset += 1;
1280                     }
1281
1282                 } /* digital data was available */
1283
1284             } /* matching config frame message was found */
1285
1286         } /* config data found */
1287
1288         if (!config_found) {
1289             proto_item_append_text(fmdata_item, ", No Fast Meter Configuration frame found");
1290             return 0;
1291         }
1292     }
1293
1294     return tvb_length(tvb);
1295
1296 }
1297
1298 /******************************************************************************************************/
1299 /* Code to Dissect Fast Operate Configuration Frames */
1300 /******************************************************************************************************/
1301 static int
1302 dissect_foconfig_frame(tvbuff_t *tvb, proto_tree *tree, int offset)
1303 {
1304 /* Set up structures needed to add the protocol subtree and manage it */
1305     proto_item    *foconfig_item, *foconfig_brkr_item, *foconfig_rb_item;
1306     proto_tree    *foconfig_tree, *foconfig_brkr_tree=NULL, *foconfig_rb_tree=NULL;
1307     guint         count;
1308     guint8        len, num_brkr, prb_supp;
1309     guint16       num_rb;
1310
1311     len = tvb_get_guint8(tvb, offset);
1312     num_brkr = tvb_get_guint8(tvb, offset+1);
1313     num_rb = tvb_get_ntohs(tvb, offset+2);
1314     prb_supp = tvb_get_guint8(tvb, offset+4);
1315
1316     foconfig_item = proto_tree_add_text(tree, tvb, offset, len-2, "Fast Operate Configuration Details");
1317     foconfig_tree = proto_item_add_subtree(foconfig_item, ett_selfm_foconfig);
1318
1319     /* Add items to protocol tree specific to Fast Operate Configuration Block */
1320
1321     /* Reported length */
1322     proto_tree_add_item(foconfig_tree, hf_selfm_foconfig_len, tvb, offset, 1, ENC_BIG_ENDIAN);
1323
1324     /* Supported Breaker Bits */
1325     foconfig_brkr_item = proto_tree_add_item(foconfig_tree, hf_selfm_foconfig_num_brkr, tvb, offset+1, 1, ENC_BIG_ENDIAN);
1326
1327     /* Supported Remote Bits */
1328     foconfig_rb_item = proto_tree_add_item(foconfig_tree, hf_selfm_foconfig_num_rb, tvb, offset+2, 2, ENC_BIG_ENDIAN);
1329
1330     /* Add "Remote Bit Pulse Supported?" and "Reserved Bit" to Tree */
1331     proto_tree_add_item(foconfig_tree, hf_selfm_foconfig_prb_supp, tvb, offset+4, 1, ENC_BIG_ENDIAN);
1332     proto_tree_add_item(foconfig_tree, hf_selfm_foconfig_reserved, tvb, offset+5, 1, ENC_BIG_ENDIAN);
1333
1334     /* Update offset pointer */
1335     offset += 6;
1336
1337     /* Get Breaker Bit Command Details */
1338     for (count = 1; count <= num_brkr; count++) {
1339
1340         foconfig_brkr_tree = proto_item_add_subtree(foconfig_brkr_item, ett_selfm_foconfig_brkr);
1341
1342         /* Add Breaker Open/Close commands to tree */
1343         proto_tree_add_item(foconfig_brkr_tree, hf_selfm_foconfig_brkr_open, tvb, offset, 1, ENC_BIG_ENDIAN);
1344         proto_tree_add_item(foconfig_brkr_tree, hf_selfm_foconfig_brkr_close, tvb, offset+1, 1, ENC_BIG_ENDIAN);
1345
1346         offset += 2;
1347     }
1348
1349     /* Get Remote Bit Command Details */
1350     for (count = 1; count <= num_rb; count++) {
1351
1352         foconfig_rb_tree = proto_item_add_subtree(foconfig_rb_item, ett_selfm_foconfig_rb);
1353
1354         /* Add "Remote Bit Set" command to tree */
1355         proto_tree_add_item(foconfig_rb_tree, hf_selfm_foconfig_rb_cmd, tvb, offset, 1, ENC_BIG_ENDIAN);
1356
1357         /* Print "Remote Bit Clear" command to tree */
1358         proto_tree_add_item(foconfig_rb_tree, hf_selfm_foconfig_rb_cmd, tvb, offset+1, 1, ENC_BIG_ENDIAN);
1359
1360         /* If Remote Bit "pulse" is supported, retrieve that command as well */
1361         if (prb_supp) {
1362             proto_tree_add_item(foconfig_rb_tree, hf_selfm_foconfig_rb_cmd, tvb, offset+2, 1, ENC_BIG_ENDIAN);
1363             offset += 3;
1364         }
1365         else{
1366             offset += 2;
1367         }
1368     }
1369
1370
1371     return tvb_length(tvb);
1372
1373 }
1374
1375 /******************************************************************************************************/
1376 /* Code to Dissect Alternate Fast Operate (AFO) Configuration Frames */
1377 /******************************************************************************************************/
1378 static int
1379 dissect_alt_fastop_config_frame(tvbuff_t *tvb, proto_tree *tree, int offset)
1380 {
1381 /* Set up structures needed to add the protocol subtree and manage it */
1382     proto_item    *foconfig_item=NULL;
1383     proto_tree    *foconfig_tree=NULL;
1384     guint8        len;
1385
1386     len = tvb_get_guint8(tvb, offset);
1387
1388     foconfig_item = proto_tree_add_text(tree, tvb, offset, len-2, "Alternate Fast Operate Configuration Details");
1389     foconfig_tree = proto_item_add_subtree(foconfig_item, ett_selfm_foconfig);
1390
1391     /* Add items to protocol tree specific to Fast Operate Configuration Block */
1392
1393     /* Reported length */
1394     proto_tree_add_item(foconfig_tree, hf_selfm_alt_foconfig_len, tvb, offset, 1, ENC_BIG_ENDIAN);
1395
1396     /* Number of Ports */
1397     proto_tree_add_item(foconfig_tree, hf_selfm_alt_foconfig_num_ports, tvb, offset+1, 1, ENC_BIG_ENDIAN);
1398
1399     /* Number of Breaker Bits */
1400     proto_tree_add_item(foconfig_tree, hf_selfm_alt_foconfig_num_brkr, tvb, offset+2, 1, ENC_BIG_ENDIAN);
1401
1402     /* Number of Remote Bits */
1403     proto_tree_add_item(foconfig_tree, hf_selfm_alt_foconfig_num_rb, tvb, offset+3, 1, ENC_BIG_ENDIAN);
1404
1405     /* Function Code(s) Supported */
1406     proto_tree_add_item(foconfig_tree, hf_selfm_alt_foconfig_funccode, tvb, offset+4, 1, ENC_BIG_ENDIAN);
1407     proto_tree_add_item(foconfig_tree, hf_selfm_alt_foconfig_funccode, tvb, offset+5, 1, ENC_BIG_ENDIAN);
1408     proto_tree_add_item(foconfig_tree, hf_selfm_alt_foconfig_funccode, tvb, offset+6, 1, ENC_BIG_ENDIAN);
1409     proto_tree_add_item(foconfig_tree, hf_selfm_alt_foconfig_funccode, tvb, offset+7, 1, ENC_BIG_ENDIAN);
1410     proto_tree_add_item(foconfig_tree, hf_selfm_alt_foconfig_funccode, tvb, offset+8, 1, ENC_BIG_ENDIAN);
1411
1412     return tvb_length(tvb);
1413
1414 }
1415
1416 /******************************************************************************************************/
1417 /* Code to Dissect Fast Operate (Remote Bit or Breaker Bit) Frames */
1418 /******************************************************************************************************/
1419 static int
1420 dissect_fastop_frame(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo, int offset)
1421 {
1422 /* Set up structures needed to add the protocol subtree and manage it */
1423     proto_item    *fastop_item;
1424     proto_tree    *fastop_tree;
1425     guint8        len, opcode;
1426     guint16       msg_type;
1427
1428     msg_type = tvb_get_ntohs(tvb, offset-2);
1429     len = tvb_get_guint8(tvb, offset);
1430
1431     fastop_item = proto_tree_add_text(tree, tvb, offset, len-2, "Fast Operate Details");
1432     fastop_tree = proto_item_add_subtree(fastop_item, ett_selfm_fastop);
1433
1434     /* Add Reported length to tree*/
1435     proto_tree_add_item(fastop_tree, hf_selfm_fastop_len, tvb, offset, 1, ENC_BIG_ENDIAN);
1436     offset += 1;
1437
1438     /* Operate Code */
1439     opcode = tvb_get_guint8(tvb, offset);
1440
1441     /* Use different lookup table for different msg_type */
1442     if (msg_type == CMD_FASTOP_RB_CTRL) {
1443         proto_tree_add_item(fastop_tree, hf_selfm_fastop_rb_code, tvb, offset, 1, ENC_BIG_ENDIAN);
1444
1445         /* Append Column Info w/ Control Code Code */
1446         col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL, "%s", val_to_str_const(opcode, selfm_fo_rb_vals, "Unknown Control Code"));
1447     }
1448     else if (msg_type == CMD_FASTOP_BR_CTRL) {
1449         proto_tree_add_item(fastop_tree, hf_selfm_fastop_br_code, tvb, offset, 1, ENC_BIG_ENDIAN);
1450
1451         /* Append Column Info w/ Control Code Code */
1452         col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL, "%s", val_to_str_const(opcode, selfm_fo_br_vals, "Unknown Control Code"));
1453     }
1454     offset += 1;
1455
1456     /* Operate Code Validation */
1457     proto_tree_add_item(fastop_tree, hf_selfm_fastop_valid, tvb, offset, 1, ENC_BIG_ENDIAN);
1458
1459     return tvb_length(tvb);
1460
1461 }
1462
1463 /******************************************************************************************************/
1464 /* Code to Dissect Alternate Fast Operate (AFO) Command Frames */
1465 /******************************************************************************************************/
1466 static int
1467 dissect_alt_fastop_frame(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo, int offset)
1468 {
1469 /* Set up structures needed to add the protocol subtree and manage it */
1470     proto_item    *fastop_item;
1471     proto_tree    *fastop_tree;
1472     guint8        len;
1473     guint16       opcode;
1474
1475     len = tvb_get_guint8(tvb, offset);
1476
1477     fastop_item = proto_tree_add_text(tree, tvb, offset, len-2, "Alternate Fast Operate Details");
1478     fastop_tree = proto_item_add_subtree(fastop_item, ett_selfm_fastop);
1479
1480     /* Add Reported length to tree */
1481     proto_tree_add_item(fastop_tree, hf_selfm_alt_fastop_len, tvb, offset, 1, ENC_BIG_ENDIAN);
1482     offset += 1;
1483
1484     /* Operate Code */
1485     opcode = tvb_get_ntohs(tvb, offset);
1486
1487     /* Append Column Info w/ Control Code Code */
1488     col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL, "%#x", opcode);
1489
1490     proto_tree_add_item(fastop_tree, hf_selfm_alt_fastop_code, tvb, offset, 2, ENC_BIG_ENDIAN);
1491
1492     offset += 2;
1493
1494     /* Operate Code Validation */
1495     proto_tree_add_item(fastop_tree, hf_selfm_alt_fastop_valid, tvb, offset, 2, ENC_BIG_ENDIAN);
1496
1497     return tvb_length(tvb);
1498
1499 }
1500
1501 /**************************************************************************************************************************/
1502 /* Code to dissect Fast SER Read Response Messages  */
1503 /**************************************************************************************************************************/
1504 /* Each Read Response frame can have a maximum data size of 117 x 16-bit words (or 234 bytes) -  this is due to the 20    */
1505 /* the 20 bytes of overhead and 254 max frame size. In the event of a larger data payload than 234 bytes, the FIR and FIN */
1506 /* bits will be used to indicate either the first frame, last frame, or a neither/middle frame.                           */
1507 /* We can use the FIN bit to attempt a reassembly of the data payload since all messages will arrive sequentially.        */
1508 /**************************************************************************************************************************/
1509
1510 static int
1511 dissect_fastser_readresp_frame(tvbuff_t *tvb, proto_tree *fastser_tree, packet_info *pinfo, int offset, guint8 seq_byte)
1512 {
1513     proto_item        *fastser_tag_item=NULL, *fastser_tag_value_item=NULL, *fmdata_dig_item=NULL;
1514     proto_item        *pi_baseaddr=NULL, *pi_fnum=NULL, *pi_type=NULL, *pi_qty=NULL;
1515     proto_tree        *fastser_tag_tree=NULL, *fmdata_dig_tree=NULL;
1516     guint32           base_addr;
1517     guint16           data_size, num_addr, cnt;
1518     guint8            *item_val_str_ptr;
1519     guint8            seq_cnt;
1520     gboolean          seq_fir, seq_fin, save_fragmented;
1521     int               payload_offset=0;
1522     fm_conversation   *conv;
1523     fastser_dataitem  *dataitem;
1524     tvbuff_t          *data_tvb, *payload_tvb;
1525
1526     /* Decode sequence byte components */
1527     seq_cnt = seq_byte & FAST_SER_SEQ_CNT;
1528     seq_fir = ((seq_byte & FAST_SER_SEQ_FIR) >> 7);
1529     seq_fin = ((seq_byte & FAST_SER_SEQ_FIN) >> 6);
1530
1531     base_addr = tvb_get_ntohl(tvb, offset);  /* 32-bit field with base address to read */
1532     num_addr = tvb_get_ntohs(tvb, offset+4); /* 16-bit field with number of 16-bit addresses to read */
1533
1534     /* Append Column Info w/ Base Address */
1535     col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL, "%#x [%s]", base_addr, region_lookup(pinfo, base_addr));
1536
1537     pi_baseaddr = proto_tree_add_item(fastser_tree, hf_selfm_fastser_baseaddr, tvb, offset, 4, ENC_BIG_ENDIAN);
1538     proto_item_append_text(pi_baseaddr, " [%s]", region_lookup(pinfo, base_addr));
1539
1540     proto_tree_add_item(fastser_tree, hf_selfm_fastser_numwords, tvb, offset+4, 2, ENC_BIG_ENDIAN);
1541     offset += 6;
1542
1543     /* Setup a new tvb representing just the data payload of this particular message */
1544     data_tvb = tvb_new_subset( tvb, offset, (tvb_reported_length_remaining(tvb, offset)-2), (tvb_reported_length_remaining(tvb, offset)-2));
1545
1546     save_fragmented = pinfo->fragmented;
1547
1548     /* Check for fragmented packet by looking at the FIR and FIN bits */
1549     if (! (seq_fir && seq_fin)) {
1550         fragment_data         *frag_msg;
1551
1552         /* This is a fragmented packet, mark it as such */
1553         pinfo->fragmented = TRUE;
1554
1555         frag_msg = fragment_add_seq_next(&selfm_reassembly_table,
1556             data_tvb, 0, pinfo, 0, NULL,
1557             tvb_reported_length(data_tvb),
1558             !seq_fin);
1559
1560         payload_tvb = process_reassembled_data(data_tvb, 0, pinfo,
1561             "Reassembled Data Response Payload", frag_msg, &selfm_frag_items,
1562             NULL, fastser_tree);
1563
1564         if (payload_tvb) { /* Reassembled */
1565           /* We have the complete payload */
1566           col_append_sep_str(pinfo->cinfo, COL_INFO, NULL, "Reassembled Data Response");
1567         }
1568         else
1569         {
1570           /* We don't have the complete reassembled payload. */
1571           col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL, "Response Data Fragment %u" , seq_cnt);
1572         }
1573
1574     }
1575
1576     /* No re-assembly required, setup the payload_tvb based on the single-frame data payload tvb */
1577     else {
1578         payload_tvb = data_tvb;
1579         add_new_data_source(pinfo, payload_tvb, "Data Response Payload");
1580     }
1581
1582     pinfo->fragmented = save_fragmented;
1583
1584     /* If we had no need to re-assemble or this is the final packet of a reassembly, let's attempt to dissect the */
1585     /* data payload using any previously-captured data format information */
1586     if (payload_tvb) {
1587
1588         /* Search for previously-encountered data format reference information to dissect the frame */
1589         conv = (fm_conversation *)p_get_proto_data(pinfo->fd, proto_selfm, 0);
1590
1591         if (conv) {
1592             /* Start at front of list and cycle through possible instances of multiple fastser_dataitem frames, looking for match */
1593             wmem_slist_frame_t *frame = wmem_slist_front(conv->fastser_dataitems);
1594
1595             while (frame) {
1596                 dataitem = (fastser_dataitem *)wmem_slist_frame_data(frame);
1597
1598                 /* If the stored base address of the current data item matches the current base address of this response frame */
1599                 /* mark that the config data was found and attempt further dissection */
1600                 if (dataitem->base_address == base_addr) {
1601
1602                     /* Data Item size (in bytes) determined by data type and quantity within item */
1603                     switch (dataitem->data_type) {
1604                         case FAST_SER_TAGTYPE_CHAR8:
1605                         case FAST_SER_TAGTYPE_DIGWORD8_BL:
1606                         case FAST_SER_TAGTYPE_DIGWORD8:
1607                             data_size = 1 * dataitem->quantity;    /* 1 byte per qty */
1608                             break;
1609                         case FAST_SER_TAGTYPE_CHAR16:
1610                         case FAST_SER_TAGTYPE_DIGWORD16_BL:
1611                         case FAST_SER_TAGTYPE_DIGWORD16:
1612                         case FAST_SER_TAGTYPE_INT16:
1613                         case FAST_SER_TAGTYPE_UINT16:
1614                             data_size = 2 * dataitem->quantity;    /* 2 bytes per qty */
1615                             break;
1616                         case FAST_SER_TAGTYPE_INT32:
1617                         case FAST_SER_TAGTYPE_UINT32:
1618                         case FAST_SER_TAGTYPE_FLOAT:
1619                             data_size = 4 * dataitem->quantity;    /* 4 bytes per qty */
1620                             break;
1621
1622                         default:
1623                             data_size = 0;
1624                             break;
1625                     }
1626
1627                     fastser_tag_item = proto_tree_add_text(fastser_tree, payload_tvb, payload_offset, data_size, "Data Item Name: %s", dataitem->name);
1628                     fastser_tag_tree = proto_item_add_subtree(fastser_tag_item, ett_selfm_fastser_tag);
1629
1630                     /* Load some information from the stored Data Format Response message into the tree for reference */
1631                     pi_fnum = proto_tree_add_text(fastser_tag_tree, payload_tvb, payload_offset, data_size, "Using frame number %d (Index Pos: %d) as Data Format Reference",dataitem->fnum, dataitem->index_pos );
1632                     pi_type = proto_tree_add_text(fastser_tag_tree, payload_tvb, payload_offset, data_size, "Data_Type: %s (%#x)",
1633                                       val_to_str_const(dataitem->data_type, selfm_fastser_tagtype_vals, "Unknown Data Type"), dataitem->data_type);
1634                     pi_qty = proto_tree_add_text(fastser_tag_tree, payload_tvb, payload_offset, data_size, "Quantity: %d",dataitem->quantity );
1635
1636                     PROTO_ITEM_SET_GENERATED(pi_fnum);
1637                     PROTO_ITEM_SET_GENERATED(pi_type);
1638                     PROTO_ITEM_SET_GENERATED(pi_qty);
1639
1640                     /* Data Item Type determines how to decode */
1641                     switch (dataitem->data_type) {
1642
1643                         case FAST_SER_TAGTYPE_DIGWORD8_BL:
1644                         case FAST_SER_TAGTYPE_DIGWORD8:
1645
1646                             for (cnt=1; cnt <= dataitem->quantity; cnt++) {
1647
1648                                 fmdata_dig_item = proto_tree_add_text(fastser_tag_tree, payload_tvb, payload_offset, 1, "8-bit Binary Items (Row: %d)", cnt);
1649                                 fmdata_dig_tree = proto_item_add_subtree(fmdata_dig_item, ett_selfm_fmdata_dig);
1650
1651                                 proto_tree_add_item(fmdata_dig_tree, hf_selfm_fmdata_dig_b0, payload_tvb, payload_offset, 1, ENC_BIG_ENDIAN);
1652                                 proto_tree_add_item(fmdata_dig_tree, hf_selfm_fmdata_dig_b1, payload_tvb, payload_offset, 1, ENC_BIG_ENDIAN);
1653                                 proto_tree_add_item(fmdata_dig_tree, hf_selfm_fmdata_dig_b2, payload_tvb, payload_offset, 1, ENC_BIG_ENDIAN);
1654                                 proto_tree_add_item(fmdata_dig_tree, hf_selfm_fmdata_dig_b3, payload_tvb, payload_offset, 1, ENC_BIG_ENDIAN);
1655                                 proto_tree_add_item(fmdata_dig_tree, hf_selfm_fmdata_dig_b4, payload_tvb, payload_offset, 1, ENC_BIG_ENDIAN);
1656                                 proto_tree_add_item(fmdata_dig_tree, hf_selfm_fmdata_dig_b5, payload_tvb, payload_offset, 1, ENC_BIG_ENDIAN);
1657                                 proto_tree_add_item(fmdata_dig_tree, hf_selfm_fmdata_dig_b6, payload_tvb, payload_offset, 1, ENC_BIG_ENDIAN);
1658                                 proto_tree_add_item(fmdata_dig_tree, hf_selfm_fmdata_dig_b7, payload_tvb, payload_offset, 1, ENC_BIG_ENDIAN);
1659
1660                                 payload_offset += 1;
1661
1662                             }
1663
1664                             break;
1665
1666                         case FAST_SER_TAGTYPE_CHAR8:
1667                         case FAST_SER_TAGTYPE_CHAR16:
1668                             item_val_str_ptr = tvb_get_ephemeral_string(payload_tvb, payload_offset, data_size);
1669                             proto_tree_add_text(fastser_tag_tree, payload_tvb, payload_offset, data_size, "Value: %s", item_val_str_ptr);
1670                             payload_offset += data_size;
1671                             break;
1672
1673                         case FAST_SER_TAGTYPE_INT16:
1674                             for (cnt=1; cnt <= dataitem->quantity; cnt++) {
1675                                 fastser_tag_value_item = proto_tree_add_item(fastser_tag_tree, hf_selfm_fastser_dataitem_int16, payload_tvb, payload_offset, data_size/dataitem->quantity, ENC_BIG_ENDIAN);
1676                                 proto_item_prepend_text(fastser_tag_value_item, "Value %d ", cnt);
1677                                 payload_offset += data_size/dataitem->quantity;
1678                             }
1679                             break;
1680
1681                         case FAST_SER_TAGTYPE_UINT16:
1682                             for (cnt=1; cnt <= dataitem->quantity; cnt++) {
1683                                 fastser_tag_value_item = proto_tree_add_item(fastser_tag_tree, hf_selfm_fastser_dataitem_uint16, payload_tvb, payload_offset, data_size/dataitem->quantity, ENC_BIG_ENDIAN);
1684                                 proto_item_prepend_text(fastser_tag_value_item, "Value %d ", cnt);
1685                                 payload_offset += data_size/dataitem->quantity;
1686                             }
1687                             break;
1688
1689                         case FAST_SER_TAGTYPE_INT32:
1690                             for (cnt=1; cnt <= dataitem->quantity; cnt++) {
1691                                 fastser_tag_value_item = proto_tree_add_item(fastser_tag_tree, hf_selfm_fastser_dataitem_int32, payload_tvb, payload_offset, data_size/dataitem->quantity, ENC_BIG_ENDIAN);
1692                                 proto_item_prepend_text(fastser_tag_value_item, "Value %d ", cnt);
1693                                 payload_offset += data_size/dataitem->quantity;
1694                             }
1695                             break;
1696
1697                         case FAST_SER_TAGTYPE_UINT32:
1698                             for (cnt=1; cnt <= dataitem->quantity; cnt++) {
1699                                 fastser_tag_value_item = proto_tree_add_item(fastser_tag_tree, hf_selfm_fastser_dataitem_uint32, payload_tvb, payload_offset, data_size/dataitem->quantity, ENC_BIG_ENDIAN);
1700                                 proto_item_prepend_text(fastser_tag_value_item, "Value %d ", cnt);
1701                                 payload_offset += data_size/dataitem->quantity;
1702                             }
1703                             break;
1704
1705                         case FAST_SER_TAGTYPE_FLOAT:
1706                             for (cnt=1; cnt <= dataitem->quantity; cnt++) {
1707                                 fastser_tag_value_item = proto_tree_add_item(fastser_tag_tree, hf_selfm_fastser_dataitem_float, payload_tvb, payload_offset, data_size/dataitem->quantity, ENC_BIG_ENDIAN);
1708                                 proto_item_prepend_text(fastser_tag_value_item, "Value %d ", cnt);
1709                                 payload_offset += data_size/dataitem->quantity;
1710                             }
1711                             break;
1712
1713                         default:
1714                             break;
1715                     } /* data item type switch */
1716
1717                 } /* base address is correct */
1718
1719                 /* After processing this frame/data item, proceed to the next */
1720                 frame = wmem_slist_frame_next(frame);
1721
1722             } /* while (frame) */
1723
1724         } /* if (conv) found */
1725
1726     } /* if payload_tvb */
1727
1728     /* Update the offset field before we leave this frame */
1729     offset += num_addr*2;
1730
1731     return offset;
1732
1733 }
1734
1735
1736 /******************************************************************************************************/
1737 /* Code to dissect Fast SER Frames       */
1738 /******************************************************************************************************/
1739 static int
1740 dissect_fastser_frame(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo, int offset)
1741 {
1742 /* Set up structures needed to add the protocol subtree and manage it */
1743     proto_item    *fastser_item, *fastser_def_fc_item=NULL, *fastser_seq_item=NULL, *fastser_elementlist_item=NULL;
1744     proto_item    *fastser_element_item=NULL, *fastser_datareg_item=NULL, *fastser_tag_item=NULL;
1745     proto_item    *pi_baseaddr=NULL;
1746     proto_tree    *fastser_tree, *fastser_def_fc_tree=NULL, *fastser_seq_tree=NULL, *fastser_elementlist_tree=NULL;
1747     proto_tree    *fastser_element_tree=NULL, *fastser_datareg_tree=NULL, *fastser_tag_tree=NULL;
1748     gint          cnt, num_elements, elmt_status32_ofs=0, elmt_status, null_offset;
1749     guint8        len, funccode, seq, rx_num_fc, tx_num_fc;
1750     guint8        seq_cnt, seq_fir, seq_fin, elmt_idx, fc_enable;
1751     guint8        *fid_str_ptr, *rid_str_ptr, *region_name_ptr, *tag_name_ptr;
1752     guint16       base_addr, num_addr, num_reg, addr1, addr2;
1753     guint32       tod_ms, elmt_status32, elmt_ts_offset;
1754
1755
1756     len = tvb_get_guint8(tvb, offset);
1757
1758     fastser_item = proto_tree_add_text(tree, tvb, offset, len-2, "Fast SER Message Details");
1759     fastser_tree = proto_item_add_subtree(fastser_item, ett_selfm_fastser);
1760
1761     /* Reported length */
1762     proto_tree_add_item(fastser_tree, hf_selfm_fastser_len, tvb, offset, 1, ENC_BIG_ENDIAN);
1763
1764     /* 5-byte Future Routing Address */
1765     proto_tree_add_item(fastser_tree, hf_selfm_fastser_routing_addr, tvb, offset+1, 5, ENC_NA);
1766     offset += 6;
1767
1768     /* Add Status Byte to tree */
1769     proto_tree_add_item(fastser_tree, hf_selfm_fastser_status, tvb, offset, 1, ENC_BIG_ENDIAN);
1770     offset += 1;
1771
1772     /* Get Function Code, add to tree */
1773     funccode = tvb_get_guint8(tvb, offset);
1774     proto_tree_add_item(fastser_tree, hf_selfm_fastser_funccode, tvb, offset, 1, ENC_BIG_ENDIAN);
1775
1776     /* Append Column Info w/ Function Code */
1777     col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL, "%s", val_to_str_const(funccode, selfm_fastser_func_code_vals, "Unknown Function Code"));
1778
1779     offset += 1;
1780
1781     /* Get Sequence Byte, add to Tree */
1782     seq = tvb_get_guint8(tvb, offset);
1783     seq_cnt = seq & FAST_SER_SEQ_CNT;
1784     seq_fir = seq & FAST_SER_SEQ_FIR;
1785     seq_fin = seq & FAST_SER_SEQ_FIN;
1786
1787     fastser_seq_item = proto_tree_add_uint_format(fastser_tree, hf_selfm_fastser_seq, tvb, offset, 1, seq, "Sequence Byte: 0x%02x (", seq);
1788     if (seq_fir) proto_item_append_text(fastser_seq_item, "FIR, ");
1789     if (seq_fin) proto_item_append_text(fastser_seq_item, "FIN, ");
1790     proto_item_append_text(fastser_seq_item, "Count %u)", seq_cnt);
1791
1792     fastser_seq_tree = proto_item_add_subtree(fastser_seq_item, ett_selfm_fastser_seq);
1793     proto_tree_add_boolean(fastser_seq_tree, hf_selfm_fastser_seq_fir, tvb, offset, 1, seq);
1794     proto_tree_add_boolean(fastser_seq_tree, hf_selfm_fastser_seq_fin, tvb, offset, 1, seq);
1795     proto_tree_add_item(fastser_seq_tree, hf_selfm_fastser_seq_cnt, tvb, offset, 1, ENC_BIG_ENDIAN);
1796     offset += 1;
1797
1798     /* Add Response Number to tree */
1799     proto_tree_add_item(fastser_tree, hf_selfm_fastser_resp_num, tvb, offset, 1, ENC_BIG_ENDIAN);
1800     offset += 1;
1801
1802     /* Depending on Function Code used, remaining section of packet will be handled differently. */
1803     switch (funccode) {
1804
1805         case FAST_SER_EN_UNS_DATA:   /* 0x01 - Enabled Unsolicited Data Transfers */
1806
1807             /* Function code to enable */
1808             fc_enable = tvb_get_guint8(tvb, offset);
1809             proto_tree_add_item(fastser_tree, hf_selfm_fastser_uns_en_fc, tvb, offset, 1, ENC_BIG_ENDIAN);
1810
1811             /* Append Column Info w/ "Enable" Function Code */
1812             col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL, "Function to Enable (%#x)", fc_enable);
1813
1814             /* 3-byte Function Code data */
1815             proto_tree_add_item(fastser_tree, hf_selfm_fastser_uns_en_fc_data, tvb, offset+1, 3, ENC_NA);
1816
1817             offset += 4;
1818
1819             break;
1820
1821         case FAST_SER_DIS_UNS_DATA:   /* 0x02 - Disable Unsolicited Data Transfers */
1822
1823             /* Function code to disable */
1824             fc_enable = tvb_get_guint8(tvb, offset);
1825             proto_tree_add_item(fastser_tree, hf_selfm_fastser_uns_dis_fc, tvb, offset, 1, ENC_BIG_ENDIAN);
1826
1827             /* Append Column Info w/ "Disable" Function Code */
1828             col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL, "Function to Disable (%#x)", fc_enable);
1829
1830             /* 1-byte Function Code data */
1831             proto_tree_add_item(fastser_tree, hf_selfm_fastser_uns_dis_fc_data, tvb, offset+1, 1, ENC_NA);
1832
1833             offset += 2;
1834
1835             break;
1836
1837
1838         case FAST_SER_READ_REQ:     /* 0x10 - Read Request */
1839
1840             base_addr = tvb_get_ntohl(tvb, offset); /* 32-bit field with base address to read */
1841
1842             /* Append Column Info w/ Base Address */
1843             col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL, "%#x [%s]", base_addr, region_lookup(pinfo, base_addr));
1844
1845             pi_baseaddr = proto_tree_add_item(fastser_tree, hf_selfm_fastser_baseaddr, tvb, offset, 4, ENC_BIG_ENDIAN);
1846             proto_item_append_text(pi_baseaddr, " [%s]", region_lookup(pinfo, base_addr));
1847
1848             proto_tree_add_item(fastser_tree, hf_selfm_fastser_numwords, tvb, offset+4, 2, ENC_BIG_ENDIAN);
1849             offset += 6;
1850             break;
1851
1852         case FAST_SER_GEN_UNS_DATA: /* 0x12 - Generic Unsolicited Data */
1853
1854             num_addr = len - 14; /* 12 header bytes + 2-byte CRC, whatever is left is the data portion of this message */
1855             num_reg = num_addr / 2;
1856
1857             /* For the number of registers, step through and retrieve/print each 16-bit component */
1858             for (cnt=0; cnt < num_reg; cnt++) {
1859                 proto_tree_add_item(fastser_tree, hf_selfm_fastser_unswrite_reg_val, tvb, offset, 2, ENC_BIG_ENDIAN);
1860                 offset += 2;
1861             }
1862
1863             break;
1864
1865         case FAST_SER_SOE_STATE_REQ: /* 0x16 - SOE Present State Request */
1866
1867             /* 4 bytes - "Origination Path" */
1868             proto_tree_add_item(fastser_tree, hf_selfm_fastser_soe_req_orig, tvb, offset, 4, ENC_NA);
1869             offset += 4;
1870
1871             break;
1872
1873         case FAST_SER_UNS_RESP:     /* 0x18 - Unsolicited Fast SER Data Response */
1874
1875             /* 4 bytes - "Origination Path" */
1876             proto_tree_add_item(fastser_tree, hf_selfm_fastser_unsresp_orig, tvb, offset, 4, ENC_NA);
1877             offset += 4;
1878
1879             /* Timestamp: 2-byte day-of-year, 2-byte year, 4-byte time-of-day in milliseconds  */
1880             /* XXX - We can use a built-in function to convert the tod_ms to a readable time format, is there anything for day_of_year? */
1881             tod_ms = tvb_get_ntohl(tvb, offset+4);
1882
1883             proto_tree_add_item(fastser_tree, hf_selfm_fastser_unsresp_doy, tvb, offset, 2, ENC_BIG_ENDIAN);
1884             proto_tree_add_item(fastser_tree, hf_selfm_fastser_unsresp_year, tvb, offset+2, 2, ENC_BIG_ENDIAN);
1885             proto_tree_add_item(fastser_tree, hf_selfm_fastser_unsresp_todms, tvb, offset+4, 4, ENC_BIG_ENDIAN);
1886             proto_tree_add_text(fastser_tree, tvb, offset+4, 4, "Time of Day (decoded): %s", time_msecs_to_str(tod_ms));
1887             offset += 8;
1888
1889             /* Build element tree */
1890             /* Determine the number of elements returned in this unsolicited message */
1891             /* The general formula is: (Length - 34) / 4 */
1892             num_elements = (len-34) / 4;
1893
1894             fastser_elementlist_item = proto_tree_add_uint(fastser_tree, hf_selfm_fastser_unsresp_num_elmt, tvb, offset, (4*num_elements), num_elements);
1895             fastser_elementlist_tree = proto_item_add_subtree(fastser_elementlist_item, ett_selfm_fastser_element_list);
1896
1897             /* "Reported New Status" word for up to 32 index elements is following the upcoming 0xFFFFFFFE End-of-record indicator
1898                Search for that indicator and use the detected tvb offset+4 to retrieve the proper 32-bit status word.
1899                Save this word for use in the element index printing but don't print the word itself until the end of the tree dissection */
1900             for (cnt = offset; cnt < len; cnt++) {
1901
1902                 if (tvb_memeql(tvb, cnt, "\xFF\xFF\xFF\xFE", 4) == 0) {
1903                     elmt_status32_ofs = cnt+4;
1904                 }
1905             }
1906             elmt_status32 = tvb_get_ntohl(tvb, elmt_status32_ofs );
1907
1908             /* Cycle through each element we have detected that exists in the SER record */
1909             for (cnt=0; cnt<num_elements; cnt++) {
1910
1911                 /* Get Element Index and Timestamp Offset (in uSec) */
1912                 elmt_idx = tvb_get_guint8(tvb, offset);
1913                 elmt_ts_offset = (guint32)((tvb_get_guint8(tvb, offset+1) << 16) | (tvb_get_guint8(tvb, offset+2) << 8) | (tvb_get_guint8(tvb, offset+3)));
1914
1915                 /* Bit shift the appropriate element from the 32-bit elmt_status word to position 0 and get the bit state for use in the tree */
1916                 elmt_status = ((elmt_status32 >> cnt) & 0x01);
1917
1918                 /* Build the tree */
1919                 fastser_element_item = proto_tree_add_text(fastser_elementlist_tree, tvb, offset, 4,
1920                     "Reported Event %d (Index: %d, New State: %s)", cnt+1, elmt_idx, val_to_str_const(elmt_status, selfm_ser_status_vals, "Unknown"));
1921                 fastser_element_tree = proto_item_add_subtree(fastser_element_item, ett_selfm_fastser_element);
1922
1923                 /* Add Index Number and Timestamp offset to tree */
1924                 proto_tree_add_item(fastser_element_tree, hf_selfm_fastser_unsresp_elmt_idx, tvb, offset, 1, ENC_BIG_ENDIAN);
1925                 proto_tree_add_item(fastser_element_tree, hf_selfm_fastser_unsresp_elmt_ts_ofs, tvb, offset+1, 3, ENC_NA);
1926                 proto_tree_add_text(fastser_element_tree, tvb, offset+1, 3,
1927                     "SER Element Timestamp Offset (decoded): %s", time_msecs_to_str(tod_ms + (elmt_ts_offset/1000)));
1928                 proto_tree_add_uint(fastser_element_tree, hf_selfm_fastser_unsresp_elmt_status, tvb, elmt_status32_ofs, 4, elmt_status);
1929
1930                 offset += 4;
1931
1932             }
1933
1934             /* 4-byte End-of-Record Terminator 0xFFFFFFFE */
1935             proto_tree_add_item(fastser_tree, hf_selfm_fastser_unsresp_eor, tvb, offset, 4, ENC_NA);
1936             offset += 4;
1937
1938             /* 4-byte Element Status word */
1939             proto_tree_add_item(fastser_tree, hf_selfm_fastser_unsresp_elmt_statword, tvb, offset, 4, ENC_BIG_ENDIAN);
1940             offset += 4;
1941
1942             break;
1943
1944
1945         case FAST_SER_UNS_WRITE:    /* 0x20 - Unsolicited Write */
1946
1947             /* Write Address Region #1 and #2, along with number of 16-bit registers */
1948             addr1 =   tvb_get_ntohs(tvb, offset);
1949             addr2 =   tvb_get_ntohs(tvb, offset+2);
1950             num_reg = tvb_get_ntohs(tvb, offset+4);
1951
1952             /* Append Column Info w/ Address Information */
1953             col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL, "%#x, %#x", addr1, addr2);
1954
1955             proto_tree_add_item(fastser_tree, hf_selfm_fastser_unswrite_addr1, tvb, offset, 2, ENC_BIG_ENDIAN);
1956             proto_tree_add_item(fastser_tree, hf_selfm_fastser_unswrite_addr2, tvb, offset+2, 2, ENC_BIG_ENDIAN);
1957             proto_tree_add_item(fastser_tree, hf_selfm_fastser_unswrite_num_reg, tvb, offset+4, 2, ENC_BIG_ENDIAN);
1958
1959             offset += 6;
1960
1961             /* For the number of registers, step through and retrieve/print each 16-bit component */
1962             for (cnt=0; cnt < num_reg; cnt++) {
1963                 proto_tree_add_item(fastser_tree, hf_selfm_fastser_unswrite_reg_val, tvb, offset, 2, ENC_BIG_ENDIAN);
1964                 offset += 2;
1965             }
1966
1967             break;
1968
1969         case FAST_SER_DATAFMT_REQ:   /* 0x31 - Data Format Request */
1970
1971             base_addr = tvb_get_ntohl(tvb, offset); /* 32-bit field with base address to read */
1972
1973             /* Append Column Info w/ Base Address */
1974             col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL, "%#x [%s]", base_addr, region_lookup(pinfo, base_addr));
1975
1976             /* Add Base Address to Tree */
1977             pi_baseaddr = proto_tree_add_item(fastser_tree, hf_selfm_fastser_baseaddr, tvb, offset, 4, ENC_BIG_ENDIAN);
1978             proto_item_append_text(pi_baseaddr, " [%s]", region_lookup(pinfo, base_addr));
1979
1980             offset += 4;
1981
1982             break;
1983
1984         case FAST_SER_BITLABEL_REQ:  /* 0x33 - Bit Label Request */
1985
1986             base_addr = tvb_get_ntohl(tvb, offset); /* 32-bit field with base address to read */
1987             proto_tree_add_item(fastser_tree, hf_selfm_fastser_baseaddr, tvb, offset, 4, ENC_BIG_ENDIAN);
1988             offset += 4;
1989
1990             /* Append Column Info w/ Base Address */
1991             col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL, "%#x", base_addr);
1992
1993             break;
1994
1995
1996         case FAST_SER_MESSAGE_DEF_ACK: /* 0x80 (resp to 0x00) - Fast SER Message Definition Acknowledge */
1997
1998              /* Routing Support */
1999              proto_tree_add_item(fastser_tree, hf_selfm_fastser_def_route_sup, tvb, offset, 1, ENC_BIG_ENDIAN);
2000              offset += 1;
2001
2002              /* RX / TX Status */
2003              proto_tree_add_item(fastser_tree, hf_selfm_fastser_def_rx_stat, tvb, offset, 1, ENC_BIG_ENDIAN);
2004              proto_tree_add_item(fastser_tree, hf_selfm_fastser_def_tx_stat, tvb, offset+1, 1, ENC_BIG_ENDIAN);
2005              offset += 2;
2006
2007              /* Max Frames RX/TX */
2008              proto_tree_add_item(fastser_tree, hf_selfm_fastser_def_rx_maxfr, tvb, offset, 1, ENC_BIG_ENDIAN);
2009              proto_tree_add_item(fastser_tree, hf_selfm_fastser_def_tx_maxfr, tvb, offset+1, 1, ENC_BIG_ENDIAN);
2010              offset += 2;
2011
2012              /* 6 bytes of reserved space */
2013              offset += 6;
2014
2015              /* Number of Supported RX Function Codes */
2016              rx_num_fc = tvb_get_guint8(tvb, offset);
2017              fastser_def_fc_item = proto_tree_add_item(fastser_tree, hf_selfm_fastser_def_rx_num_fc, tvb, offset, 1, ENC_BIG_ENDIAN);
2018              fastser_def_fc_tree = proto_item_add_subtree(fastser_def_fc_item, ett_selfm_fastser_def_fc);
2019              offset += 1;
2020
2021              /* Add Supported RX Function Codes to tree */
2022              for (cnt=0; cnt<rx_num_fc; cnt++) {
2023                  proto_tree_add_item(fastser_def_fc_tree, hf_selfm_fastser_def_rx_fc, tvb, offset, 1, ENC_BIG_ENDIAN);
2024                  offset += 2;
2025              }
2026
2027              /* Number of Supported TX Function Codes */
2028              tx_num_fc = tvb_get_guint8(tvb, offset);
2029              fastser_def_fc_item = proto_tree_add_item(fastser_tree, hf_selfm_fastser_def_tx_num_fc, tvb, offset, 1, ENC_BIG_ENDIAN);
2030              fastser_def_fc_tree = proto_item_add_subtree(fastser_def_fc_item, ett_selfm_fastser_def_fc);
2031              offset += 1;
2032
2033              /* Add Supported TX Function Codes to tree */
2034              for (cnt=0; cnt<tx_num_fc; cnt++) {
2035                  proto_tree_add_item(fastser_def_fc_tree, hf_selfm_fastser_def_tx_fc, tvb, offset, 1, ENC_BIG_ENDIAN);
2036                  offset += 2;
2037              }
2038
2039              break;
2040
2041         case FAST_SER_READ_RESP:     /* 0x90 (resp to 0x10) - Read Response */
2042
2043             offset = dissect_fastser_readresp_frame( tvb, fastser_tree, pinfo, offset, seq);
2044
2045             break;
2046
2047         case FAST_SER_SOE_STATE_RESP: /* 0x96 - (resp to 0x16) SOE Present State Response */
2048
2049
2050             /* 16-bit field with number of blocks of present state data */
2051             proto_tree_add_item(fastser_tree, hf_selfm_fastser_soe_resp_numblks, tvb, offset, 2, ENC_BIG_ENDIAN);
2052             offset += 2;
2053
2054             /* XXX - With examples, need to loop through each one of these items based on the num_blocks */
2055             proto_tree_add_item(fastser_tree, hf_selfm_fastser_soe_resp_orig, tvb, offset, 4, ENC_BIG_ENDIAN);
2056             proto_tree_add_item(fastser_tree, hf_selfm_fastser_soe_resp_numbits, tvb, offset+4, 1, ENC_BIG_ENDIAN);
2057             proto_tree_add_item(fastser_tree, hf_selfm_fastser_soe_resp_pad, tvb, offset+5, 1, ENC_BIG_ENDIAN);
2058             proto_tree_add_item(fastser_tree, hf_selfm_fastser_soe_resp_doy, tvb, offset+6, 2, ENC_BIG_ENDIAN);
2059             proto_tree_add_item(fastser_tree, hf_selfm_fastser_soe_resp_year, tvb, offset+8, 2, ENC_BIG_ENDIAN);
2060             proto_tree_add_item(fastser_tree, hf_selfm_fastser_soe_resp_tod, tvb, offset+10, 4, ENC_BIG_ENDIAN);
2061             /* proto_tree_add_item(fastser_tree, hf_selfm_fastser_soe_resp_data, tvb, offset+14, 2, ENC_BIG_ENDIAN); */
2062
2063             offset += 14;
2064
2065             break;
2066
2067         case FAST_SER_DEVDESC_RESP:  /* 0xB0 (resp to 0x30) - Device Description Response */
2068
2069             fid_str_ptr = tvb_get_ephemeral_string(tvb, offset, 50);  /* Add FID / RID ASCII data to tree */
2070             rid_str_ptr = tvb_get_ephemeral_string(tvb, offset+50, 40);
2071             proto_tree_add_text(fastser_tree, tvb, offset, 50, "FID: %s", fid_str_ptr);
2072             proto_tree_add_text(fastser_tree, tvb, offset+50, 40, "RID: %s", rid_str_ptr);
2073             offset += 90;
2074
2075             /* 16-bit field with number of data areas */
2076             num_reg = tvb_get_ntohs(tvb, offset);
2077             proto_tree_add_item(fastser_tree, hf_selfm_fastser_devdesc_num_region, tvb, offset, 2, ENC_BIG_ENDIAN);
2078             offset += 2;
2079
2080             /* Maximum size of 7 regions per message, check the seq_cnt to determine if we have stepped into
2081                the next sequential message where the remaining regions would be described */
2082             if ((num_reg >= 8) && (seq_cnt == 0)) {
2083                 num_reg = 7;
2084             }
2085             else{
2086                 num_reg = num_reg - (seq_cnt * 7);
2087             }
2088
2089             /* 16-bit field with number of control areas */
2090             proto_tree_add_item(fastser_tree, hf_selfm_fastser_devdesc_num_ctrl, tvb, offset, 2, ENC_BIG_ENDIAN);
2091             offset += 2;
2092
2093             /* Each 18-byte data area description has a 10 byte region name, followed by 32-bit base, */
2094             /* 16-bit message word count and 16-bit flag field */
2095             for (cnt=0; cnt<num_reg; cnt++) {
2096
2097                 fastser_datareg_item = proto_tree_add_text(fastser_tree, tvb, offset, 18, "Fast SER Data Region #%d", cnt+1);
2098                 fastser_datareg_tree = proto_item_add_subtree(fastser_datareg_item, ett_selfm_fastser_datareg);
2099
2100                 /* 10-Byte Region description */
2101                 region_name_ptr = tvb_get_ephemeral_string(tvb, offset, 10);
2102                 proto_tree_add_text(fastser_datareg_tree, tvb, offset, 10, "Data Region Name: %s", region_name_ptr);
2103                 offset += 10;
2104
2105                 /* 32-bit field with base address of data region */
2106                 proto_tree_add_item(fastser_datareg_tree, hf_selfm_fastser_baseaddr, tvb, offset, 4, ENC_BIG_ENDIAN);
2107                 offset += 4;
2108
2109                 /* 16-bit field with number of 16-bit words in region */
2110                 proto_tree_add_item(fastser_datareg_tree, hf_selfm_fastser_numwords, tvb, offset, 2, ENC_BIG_ENDIAN);
2111                 offset += 2;
2112
2113                 /* 16-bit flag field */
2114                 proto_tree_add_item(fastser_datareg_tree, hf_selfm_fastser_flags, tvb, offset, 2, ENC_BIG_ENDIAN);
2115                 offset += 2;
2116
2117             }
2118
2119             /* Some relays (4xx) don't follow the standard here and include an 8-byte sequence of all 0x00's to represent */
2120             /* 'reserved' space for the control regions.  Detect these and skip if they are present */
2121             for (cnt = offset; cnt < len; cnt++) {
2122
2123                 if (tvb_memeql(tvb, cnt, "\x00\x00\x00\x00\x00\x00\x00\x00", 8) == 0) {
2124                     offset = cnt+8;
2125                 }
2126             }
2127
2128             break;
2129
2130         case FAST_SER_DATAFMT_RESP: /* 0xB1 (resp to 0x31) - Data Format Response */
2131
2132             base_addr = tvb_get_ntohl(tvb, offset); /* 32-bit field with base address to read */
2133
2134             /* Add Base Address to Tree */
2135             pi_baseaddr = proto_tree_add_item(fastser_tree, hf_selfm_fastser_baseaddr, tvb, offset, 4, ENC_BIG_ENDIAN);
2136             proto_item_append_text(pi_baseaddr, " [%s]", region_lookup(pinfo, base_addr));
2137
2138             offset += 4;
2139
2140             /* Append Column Info w/ Base Address */
2141             col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL, "%#x [%s]", base_addr, region_lookup(pinfo, base_addr));
2142
2143             /* 16-bit field with number of data items to follow */
2144             proto_tree_add_item(fastser_tree, hf_selfm_fastser_datafmt_resp_numitem, tvb, offset, 2, ENC_BIG_ENDIAN);
2145             offset += 2;
2146
2147             while ((tvb_reported_length_remaining(tvb, offset)) > 2) {
2148                 tag_name_ptr = tvb_get_ephemeral_string(tvb, offset, 10);  /* Data Item record name 10 bytes */
2149                 fastser_tag_item = proto_tree_add_text(fastser_tree, tvb, offset, 14, "Data Item Record Name: %s", tag_name_ptr);
2150                 fastser_tag_tree = proto_item_add_subtree(fastser_tag_item, ett_selfm_fastser_tag);
2151
2152                 /* Data item qty and type */
2153                 proto_tree_add_item(fastser_tag_tree, hf_selfm_fastser_dataitem_qty, tvb, offset+10, 2, ENC_BIG_ENDIAN);
2154                 proto_tree_add_item(fastser_tag_tree, hf_selfm_fastser_dataitem_type, tvb, offset+12, 2, ENC_BIG_ENDIAN);
2155
2156                 offset += 14;
2157             }
2158             break;
2159
2160         case FAST_SER_BITLABEL_RESP: /* 0xB3 (resp to 0x33) - Bit Label Response */
2161
2162             /* The data in this response is a variable length string containing the names of 8 digital bits. */
2163             /* Each name is max 8 chars and each is null-seperated */
2164             cnt=1;
2165
2166             /* find the null separators and add the bit label text strings to the tree */
2167             for (null_offset = offset; null_offset < len; null_offset++) {
2168                 if ((tvb_memeql(tvb, null_offset, "\x00", 1) == 0) && (tvb_reported_length_remaining(tvb, offset) > 2)) {
2169                     proto_tree_add_text(fastser_tree, tvb, offset, (null_offset-offset), "Bit Label #%d Name: %s", cnt,
2170                        tvb_format_text(tvb, offset, (null_offset-offset)));
2171                     offset = null_offset+1; /* skip the null */
2172                     cnt++;
2173                 }
2174             }
2175
2176             break;
2177
2178         default:
2179             break;
2180     } /* func_code */
2181
2182     /* XXX - Should eventually get a function here to validate this CRC16 */
2183     proto_tree_add_item(fastser_tree, hf_selfm_fastser_crc16, tvb, offset, 2, ENC_BIG_ENDIAN);
2184
2185     return tvb_length(tvb);
2186
2187 }
2188
2189
2190 /******************************************************************************************************/
2191 /* Code to dissect SEL Fast Message Protocol packets */
2192 /* Will call other sub-dissectors, as needed         */
2193 /******************************************************************************************************/
2194 static void
2195 dissect_selfm(tvbuff_t *selfm_tvb, packet_info *pinfo, proto_tree *tree)
2196 {
2197 /* Set up structures needed to add the protocol subtree and manage it */
2198     proto_item    *selfm_item=NULL;
2199     proto_tree    *selfm_tree=NULL;
2200     int           offset=0, cnt=0;
2201     guint32       base_addr;
2202     guint16       msg_type, len, num_items;
2203     guint8        seq, seq_cnt;
2204
2205     /* Make entries in Protocol column on summary display */
2206     col_set_str(pinfo->cinfo, COL_PROTOCOL, "SEL Fast Msg");
2207     col_clear(pinfo->cinfo, COL_INFO);
2208
2209     len = tvb_length(selfm_tvb);
2210
2211     msg_type = tvb_get_ntohs(selfm_tvb, offset);
2212
2213     /* On first pass through the packets we have 4 tasks to complete - they are each noted below */
2214     if (!pinfo->fd->flags.visited) {
2215         conversation_t       *conversation;
2216         fm_conversation      *fm_conv_data;
2217
2218         /* Find a conversation, create a new if no one exists */
2219         conversation = find_or_create_conversation(pinfo);
2220
2221         fm_conv_data = (fm_conversation *)conversation_get_proto_data(conversation, proto_selfm);
2222
2223         if (fm_conv_data == NULL) {
2224             fm_conv_data = wmem_new(wmem_file_scope(), fm_conversation);
2225             fm_conv_data->fm_config_frames = wmem_slist_new(wmem_file_scope());
2226             fm_conv_data->fastser_dataitems = wmem_slist_new(wmem_file_scope());
2227             fm_conv_data->fastser_dataregions = wmem_tree_new(wmem_file_scope());
2228             conversation_add_proto_data(conversation, proto_selfm, (void *)fm_conv_data);
2229         }
2230
2231         p_add_proto_data(pinfo->fd, proto_selfm, 0, fm_conv_data);
2232
2233         /* 1. Configuration frames (0xA5C1, 0xA5C2, 0xA5C3) need special treatment during the first run         */
2234         /* For each Fast Meter Configuration frame (0xA5Cx), a 'fm_config_frame' struct is created to hold the  */
2235         /* information necessary to decode subsequent matching Fast Meter Data frames (0xA5Dx). A pointer to    */
2236         /* this struct is saved in the conversation and is copied to the per-packet information if a            */
2237         /* Fast Meter Data frame is dissected.                                                                  */
2238         if ((CMD_FM_CONFIG == msg_type) || (CMD_DFM_CONFIG == msg_type) || (CMD_PDFM_CONFIG == msg_type)) {
2239             /* Fill the fm_config_frame */
2240             fm_config_frame *frame_ptr = fmconfig_frame_fast(selfm_tvb);
2241             frame_ptr->fnum = pinfo->fd->num;
2242             wmem_slist_prepend(fm_conv_data->fm_config_frames, frame_ptr);
2243         }
2244
2245         /* 2. Fill conversation data array with Fast SER Data Item info from Data Format Response Messages.   */
2246         /* These format definitions will later be retrieved to decode Read Response messages.                 */
2247         if ((CMD_FAST_SER == msg_type) && (tvb_get_guint8(selfm_tvb, offset+9) == FAST_SER_DATAFMT_RESP)) {
2248
2249             seq = tvb_get_guint8(selfm_tvb, offset+10);
2250             seq_cnt = seq & FAST_SER_SEQ_CNT;
2251
2252             base_addr = tvb_get_ntohl(selfm_tvb, offset+12); /* 32-bit field with base address to read */
2253             num_items = tvb_get_ntohs(selfm_tvb, offset+16);
2254
2255             /* When dealing with Data Format Response messages, there are a maximum of 16 items per frame */
2256             /* Use the sequence count if we have more 16 items to determine how many to expect in each frame */
2257             if ((num_items > 16) && (seq_cnt == 0)) {
2258                 num_items = 16;
2259             }
2260             else {
2261                 num_items = num_items - (seq_cnt * 16);
2262             }
2263
2264             /* Set offset to start of data items */
2265             offset = 18;
2266
2267             /* Enter the single frame multiple times, retrieving a single dataitem per entry */
2268             for (cnt = 1; (cnt <= num_items); cnt++) {
2269                 fastser_dataitem *dataitem_ptr = fastser_dataitem_save(selfm_tvb, offset);
2270                 dataitem_ptr->fnum = pinfo->fd->num;
2271                 dataitem_ptr->base_address = base_addr;
2272                 dataitem_ptr->index_pos = cnt;
2273
2274                 /* Store the data item configuration info in the fastser_dataitems slist */
2275                 wmem_slist_append(fm_conv_data->fastser_dataitems, dataitem_ptr);
2276                 offset += 14;
2277             }
2278         }
2279
2280         /* 3. Attempt re-assembly during first pass with Read Response Messages data payloads that span multiple */
2281         /* packets.  The final data payload will be assembled on the packet with the seq_fin bit set.            */
2282         if ((CMD_FAST_SER == msg_type) && (tvb_get_guint8(selfm_tvb, offset+9) == FAST_SER_READ_RESP)) {
2283
2284             seq = tvb_get_guint8(selfm_tvb, offset+10);
2285
2286             /* Set offset to where the dissect_fastser_readresp_frame function would normally be called, */
2287             /* right before base address & num_items */
2288             offset = 12;
2289
2290             /* Call the same read response function that will be called during GUI dissection */
2291             offset = dissect_fastser_readresp_frame( selfm_tvb, tree, pinfo, offset, seq);
2292
2293         }
2294
2295         /* 4. Fill conversation data array with Fast SER Data Region info from Device Desc Response Messages. This */
2296         /*    will retrieve a data region name (associated to an address) that can later be displayed in the tree. */
2297         if ((CMD_FAST_SER == msg_type) && (tvb_get_guint8(selfm_tvb, offset+9) == FAST_SER_DEVDESC_RESP)) {
2298
2299             seq = tvb_get_guint8(selfm_tvb, offset+10);
2300             seq_cnt = seq & FAST_SER_SEQ_CNT;
2301
2302             num_items = tvb_get_ntohs(selfm_tvb, offset+102);
2303
2304             /* When dealing with Device Description Response messages, there are a maximum of 7 regions per frame */
2305             /* Use the sequence count if we have more 7 items to determine how many to expect in each frame */
2306             if ((num_items >= 8) && (seq_cnt == 0)) {
2307                 num_items = 7;
2308             }
2309             else{
2310                 num_items = num_items - (seq_cnt * 7);
2311             }
2312
2313             /* Set offset to start of data regions */
2314             offset = 106;
2315
2316             /* Enter the single frame multiple times, retrieving a single data region per entry */
2317             for (cnt = 1; (cnt <= num_items); cnt++) {
2318                 guint32 base_address = tvb_get_ntohl(selfm_tvb, offset+10);
2319                 fastser_dataregion *dataregion_ptr = fastser_dataregion_save(selfm_tvb, offset);
2320
2321                 /* Store the data region info in the fastser_dataregions tree */
2322                 wmem_tree_insert32(fm_conv_data->fastser_dataregions, base_address, dataregion_ptr);
2323                 offset += 18;
2324             }
2325         }
2326
2327
2328      } /* if (!visited) */
2329
2330     if (tree) {
2331
2332         selfm_item = proto_tree_add_protocol_format(tree, proto_selfm, selfm_tvb, 0, len, "SEL Fast Message");
2333         selfm_tree = proto_item_add_subtree(selfm_item, ett_selfm);
2334
2335         /* Set INFO column with SEL Protocol Message Type */
2336         col_clear(pinfo->cinfo, COL_INFO); /* clear out stuff in the info column */
2337         col_add_fstr(pinfo->cinfo, COL_INFO, "%s", val_to_str_const(msg_type, selfm_msgtype_vals, "Unknown Message Type"));
2338
2339         /* Add Message Type to Protocol Tree */
2340         proto_tree_add_item(selfm_tree, hf_selfm_msgtype, selfm_tvb, offset, 2, ENC_BIG_ENDIAN);
2341         offset += 2;
2342
2343         /* Determine correct message type and call appropriate dissector */
2344         if (tvb_reported_length_remaining(selfm_tvb, offset) > 0) {
2345                 switch (msg_type) {
2346                     case CMD_RELAY_DEF:
2347                         dissect_relaydef_frame(selfm_tvb, selfm_tree, offset);
2348                         break;
2349                     case CMD_FM_CONFIG:
2350                     case CMD_DFM_CONFIG:
2351                     case CMD_PDFM_CONFIG:
2352                         dissect_fmconfig_frame(selfm_tvb, selfm_tree, offset);
2353                         break;
2354                     case CMD_FM_DATA:
2355                         dissect_fmdata_frame(selfm_tvb, selfm_tree, pinfo, offset, CMD_FM_CONFIG);
2356                         break;
2357                     case CMD_DFM_DATA:
2358                         dissect_fmdata_frame(selfm_tvb, selfm_tree, pinfo, offset, CMD_DFM_CONFIG);
2359                         break;
2360                     case CMD_PDFM_DATA:
2361                         dissect_fmdata_frame(selfm_tvb, selfm_tree, pinfo, offset, CMD_PDFM_CONFIG);
2362                         break;
2363                     case CMD_FASTOP_CONFIG:
2364                         dissect_foconfig_frame(selfm_tvb, selfm_tree, offset);
2365                         break;
2366                     case CMD_FAST_SER:
2367                         dissect_fastser_frame(selfm_tvb, selfm_tree, pinfo, offset);
2368                         break;
2369                     case CMD_FASTOP_RB_CTRL:
2370                     case CMD_FASTOP_BR_CTRL:
2371                         dissect_fastop_frame(selfm_tvb, selfm_tree, pinfo, offset);
2372                         break;
2373                     case CMD_ALT_FASTOP_CONFIG:
2374                         dissect_alt_fastop_config_frame(selfm_tvb, selfm_tree, offset);
2375                         break;
2376                     case CMD_ALT_FASTOP_OPEN:
2377                     case CMD_ALT_FASTOP_CLOSE:
2378                     case CMD_ALT_FASTOP_SET:
2379                     case CMD_ALT_FASTOP_CLEAR:
2380                     case CMD_ALT_FASTOP_PULSE:
2381                         dissect_alt_fastop_frame(selfm_tvb, selfm_tree, pinfo, offset);
2382                         break;
2383                     default:
2384                         break;
2385                 } /* msg_type */
2386         } /* remaining length > 0 */
2387     } /* tree */
2388
2389 }
2390
2391 /******************************************************************************************************/
2392 /* Return length of SEL Protocol over TCP message (used for re-assembly)                               */
2393 /* SEL Protocol "Scan" messages are generally 2-bytes in length and only include a 16-bit message type */
2394 /* SEL Protocol "Response" messages include a "length" byte in offset 2 of each response message       */
2395 /******************************************************************************************************/
2396 static guint
2397 get_selfm_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset _U_)
2398 {
2399     guint message_len=0;  /* message length, inclusive of header, data, crc */
2400
2401     /* Get length byte from message */
2402     if (tvb_length(tvb) > 2) {
2403         message_len = tvb_get_guint8(tvb, 2);
2404     }
2405     /* for 2-byte poll messages, set the length to 2 */
2406     else if (tvb_length(tvb) == 2) {
2407         message_len = 2;
2408     }
2409
2410     return message_len;
2411 }
2412
2413 /******************************************************************************************************/
2414 /* Dissect (and possibly Re-assemble) SEL protocol payload data */
2415 /******************************************************************************************************/
2416 static gboolean
2417 dissect_selfm_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
2418 {
2419
2420     tvbuff_t      *selfm_tvb;
2421     gint length = tvb_length(tvb);
2422
2423     /* Check for a SEL FM packet.  It should begin with 0xA5 */
2424     if(length < 2 || tvb_get_guint8(tvb, 0) != 0xA5) {
2425         /* Not a SEL Protocol packet, just happened to use the same port */
2426         return FALSE;
2427     }
2428
2429     /* If this is a Telnet-encapsulated Ethernet, let's clean out the IAC 0xFF instances */
2430     /* before we attempt any kind of re-assembly of the message */
2431     if ((pinfo->srcport) && selfm_telnet_clean) {
2432         selfm_tvb = clean_telnet_iac(pinfo, tvb, 0, length);
2433     }
2434     else {
2435         selfm_tvb = tvb_new_subset( tvb, 0, length, length);
2436     }
2437
2438
2439     tcp_dissect_pdus(selfm_tvb, pinfo, tree, selfm_desegment, 2,
2440                    get_selfm_len, dissect_selfm);
2441
2442     return TRUE;
2443 }
2444
2445 /******************************************************************************************************/
2446 /* Dissect "simple" SEL protocol payload (no TCP re-assembly) */
2447 /******************************************************************************************************/
2448 static gboolean
2449 dissect_selfm_simple(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
2450 {
2451     gint length = tvb_length(tvb);
2452
2453     /* Check for a SEL FM packet.  It should begin with 0xA5 */
2454     if(length < 2 || tvb_get_guint8(tvb, 0) != 0xA5) {
2455         /* Not a SEL Protocol packet, just happened to use the same port */
2456         return FALSE;
2457     }
2458
2459     dissect_selfm(tvb, pinfo, tree);
2460
2461     return TRUE;
2462 }
2463
2464 /******************************************************************************************************/
2465 /* SEL Fast Message Dissector initialization */
2466 /******************************************************************************************************/
2467 static void
2468 selfm_init(void)
2469 {
2470
2471   reassembly_table_init(&selfm_reassembly_table,
2472                         &addresses_reassembly_table_functions);
2473 }
2474
2475 /******************************************************************************************************/
2476 /* Register the protocol with Wireshark */
2477 /******************************************************************************************************/
2478 void proto_reg_handoff_selfm(void);
2479
2480 void
2481 proto_register_selfm(void)
2482 {
2483     /* SEL Protocol header fields */
2484     static hf_register_info selfm_hf[] = {
2485         { &hf_selfm_msgtype,
2486         { "Message Type", "selfm.msgtype", FT_UINT16, BASE_HEX|BASE_EXT_STRING, &selfm_msgtype_vals_ext, 0x0, NULL, HFILL }},
2487         /* "Relay Definition" specific fields */
2488         { &hf_selfm_relaydef_len,
2489         { "Length", "selfm.relaydef.len", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2490         { &hf_selfm_relaydef_numproto,
2491         { "Number of Protocols", "selfm.relaydef.numproto", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2492         { &hf_selfm_relaydef_numfm,
2493         { "Number of Fast Meter Messages", "selfm.relaydef.numfm", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2494         { &hf_selfm_relaydef_numflags,
2495         { "Number of Status Flags", "selfm.relaydef.numflags", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2496         { &hf_selfm_relaydef_fmcfg_cmd,
2497         { "Fast Meter Config Command", "selfm.relaydef.fmcfg_cmd", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }},
2498         { &hf_selfm_relaydef_fmdata_cmd,
2499         { "Fast Meter Data Command", "selfm.relaydef.fmdata_cmd", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }},
2500         { &hf_selfm_relaydef_statbit,
2501         { "Status Flag Bit", "selfm.relaydef.status_bit", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }},
2502         { &hf_selfm_relaydef_statbit_cmd,
2503         { "Status Flag Bit Response Command", "selfm.relaydef.status_bit_cmd", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
2504         { &hf_selfm_relaydef_proto,
2505         { "Supported Protocol", "selfm.relaydef.proto", FT_UINT16, BASE_HEX|BASE_EXT_STRING, &selfm_relaydef_proto_vals_ext, 0x0, NULL, HFILL }},
2506         /* "Fast Meter Configuration" specific fields */
2507         { &hf_selfm_fmconfig_len,
2508         { "Length", "selfm.fmconfig.len", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2509         { &hf_selfm_fmconfig_numflags,
2510         { "Number of Status Flags", "selfm.fmconfig.numflags", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2511         { &hf_selfm_fmconfig_loc_sf,
2512         { "Location of Scale Factor", "selfm.fmconfig.loc_sf", FT_UINT8, BASE_DEC, VALS(selfm_fmconfig_sfloc_vals), 0x0, NULL, HFILL }},
2513         { &hf_selfm_fmconfig_num_sf,
2514         { "Number of Scale Factors", "selfm.fmconfig.num_sf", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2515         { &hf_selfm_fmconfig_num_ai,
2516         { "Number of Analog Input Channels", "selfm.fmconfig.num_ai", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2517         { &hf_selfm_fmconfig_num_samp,
2518         { "Number of Samples per AI Channel", "selfm.fmconfig.num_samp", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2519         { &hf_selfm_fmconfig_num_dig,
2520         { "Number of Digital Banks", "selfm.fmconfig.num_dig", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2521         { &hf_selfm_fmconfig_num_calc,
2522         { "Number of Calculation Blocks", "selfm.fmconfig.num_calc", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2523         { &hf_selfm_fmconfig_ofs_ai,
2524         { "First Analog Channel Offset", "selfm.fmconfig.ofs_ai", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2525         { &hf_selfm_fmconfig_ofs_ts,
2526         { "Timestamp Offset", "selfm.fmconfig.ofs_ts", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2527         { &hf_selfm_fmconfig_ofs_dig,
2528         { "First Digital Bank Offset", "selfm.fmconfig.ofs_dig", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2529         { &hf_selfm_fmconfig_ai_type,
2530         { "Analog Channel Type", "selfm.fmconfig.ai_type", FT_UINT8, BASE_DEC, VALS(selfm_fmconfig_ai_chtype_vals), 0x0, NULL, HFILL }},
2531         { &hf_selfm_fmconfig_ai_sf_type,
2532         { "Analog Channel Scale Factor Type", "selfm.fmconfig.ai_sf_type", FT_UINT8, BASE_DEC, VALS(selfm_fmconfig_ai_sftype_vals), 0x0, NULL, HFILL }},
2533         { &hf_selfm_fmconfig_ai_sf_ofs,
2534         { "Analog Channel Scale Factor Offset", "selfm.fmconfig.ai_sf_ofs", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2535         { &hf_selfm_fmconfig_cblk_rot,
2536         { "Rotation", "selfm.fmconfig.cblk_rot", FT_UINT8, BASE_HEX, VALS(selfm_fmconfig_cblk_rot_vals), 0x01, NULL, HFILL }},
2537         { &hf_selfm_fmconfig_cblk_vconn,
2538         { "Voltage Connection", "selfm.fmconfig.cblk_vconn", FT_UINT8, BASE_HEX, VALS(selfm_fmconfig_cblk_vconn_vals), 0x06, NULL, HFILL }},
2539         { &hf_selfm_fmconfig_cblk_iconn,
2540         { "Current Connection", "selfm.fmconfig.cblk_iconn", FT_UINT8, BASE_HEX, VALS(selfm_fmconfig_cblk_iconn_vals), 0x18, NULL, HFILL }},
2541         { &hf_selfm_fmconfig_cblk_ctype,
2542         { "Calculation Type", "selfm.fmconfig.cblk_ctype", FT_UINT8, BASE_DEC, VALS(selfm_fmconfig_cblk_ctype_vals), 0x0, NULL, HFILL }},
2543         { &hf_selfm_fmconfig_cblk_deskew_ofs,
2544         { "Skew Correction Offset", "selfm.fmconfig.cblk_deskew_ofs", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2545         { &hf_selfm_fmconfig_cblk_rs_ofs,
2546         { "Rs Offset", "selfm.fmconfig.cblk_rs_ofs", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2547         { &hf_selfm_fmconfig_cblk_xs_ofs,
2548         { "Xs Offset", "selfm.fmconfig.cblk_xs_ofs", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2549         { &hf_selfm_fmconfig_cblk_ia_idx,
2550         { "Analog Record Ia Index Position", "selfm.fmconfig.cblk_ia_idx", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2551         { &hf_selfm_fmconfig_cblk_ib_idx,
2552         { "Analog Record Ib Index Position", "selfm.fmconfig.cblk_ib_idx", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2553         { &hf_selfm_fmconfig_cblk_ic_idx,
2554         { "Analog Record Ic Index Position", "selfm.fmconfig.cblk_ic_idx", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2555         { &hf_selfm_fmconfig_cblk_va_idx,
2556         { "Analog Record Va/Vab Index Position", "selfm.fmconfig.cblk_va_idx", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2557         { &hf_selfm_fmconfig_cblk_vb_idx,
2558         { "Analog Record Vb/Vbc Index Position", "selfm.fmconfig.cblk_vb_idx", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2559         { &hf_selfm_fmconfig_cblk_vc_idx,
2560         { "Analog Record Vc/Vca Index Position", "selfm.fmconfig.cblk_vc_idx", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2561         /* "Fast Meter Data" specific fields */
2562         { &hf_selfm_fmdata_len,
2563         { "Length", "selfm.fmdata.len", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2564         { &hf_selfm_fmdata_flagbyte,
2565         { "Status Flags Byte", "selfm.fmdata.flagbyte", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2566         { &hf_selfm_fmdata_ai_sf_fp,
2567         { "Using IEEE FP Format Scale Factor", "selfm.fmdata.ai.sf_fp",FT_FLOAT, BASE_NONE, NULL, 0x0, NULL, HFILL }},
2568         { &hf_selfm_fmdata_dig_b0,
2569         { "Bit 0", "selfm.fmdata.dig_b0", FT_BOOLEAN, 8, NULL, 0x01, NULL, HFILL }},
2570         { &hf_selfm_fmdata_dig_b1,
2571         { "Bit 1", "selfm.fmdata.dig_b1", FT_BOOLEAN, 8, NULL, 0x02, NULL, HFILL }},
2572         { &hf_selfm_fmdata_dig_b2,
2573         { "Bit 2", "selfm.fmdata.dig_b2", FT_BOOLEAN, 8, NULL, 0x04, NULL, HFILL }},
2574         { &hf_selfm_fmdata_dig_b3,
2575         { "Bit 3", "selfm.fmdata.dig_b3", FT_BOOLEAN, 8, NULL, 0x08, NULL, HFILL }},
2576         { &hf_selfm_fmdata_dig_b4,
2577         { "Bit 4", "selfm.fmdata.dig_b4", FT_BOOLEAN, 8, NULL, 0x10, NULL, HFILL }},
2578         { &hf_selfm_fmdata_dig_b5,
2579         { "Bit 5", "selfm.fmdata.dig_b5", FT_BOOLEAN, 8, NULL, 0x20, NULL, HFILL }},
2580         { &hf_selfm_fmdata_dig_b6,
2581         { "Bit 6", "selfm.fmdata.dig_b6", FT_BOOLEAN, 8, NULL, 0x40, NULL, HFILL }},
2582         { &hf_selfm_fmdata_dig_b7,
2583         { "Bit 7", "selfm.fmdata.dig_b7", FT_BOOLEAN, 8, NULL, 0x80, NULL, HFILL }},
2584         /* "Fast Operate Configuration" specific fields */
2585         { &hf_selfm_foconfig_len,
2586         { "Length", "selfm.foconfig.len", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2587         { &hf_selfm_foconfig_num_brkr,
2588         { "Number of Breaker Bits", "selfm.foconfig.num_brkr", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2589         { &hf_selfm_foconfig_num_rb,
2590         { "Number of Remote Bits", "selfm.foconfig.num_rb", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2591         { &hf_selfm_foconfig_prb_supp,
2592         { "Remote Bit Pulse Supported", "selfm.foconfig.prb_supp", FT_UINT8, BASE_DEC, VALS(selfm_foconfig_prb_supp_vals), 0x0, NULL, HFILL }},
2593         { &hf_selfm_foconfig_reserved,
2594         { "Reserved Bit (Future)", "selfm.foconfig.reserved", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2595         { &hf_selfm_foconfig_brkr_open,
2596         { "Breaker Bit Open Command", "selfm.foconfig.brkr_open", FT_UINT8, BASE_HEX, VALS(selfm_fo_br_vals), 0x0, NULL, HFILL }},
2597         { &hf_selfm_foconfig_brkr_close,
2598         { "Breaker Bit Close Command", "selfm.foconfig.brkr_close", FT_UINT8, BASE_HEX, VALS(selfm_fo_br_vals), 0x0, NULL, HFILL }},
2599         { &hf_selfm_foconfig_rb_cmd,
2600         { "Remote Bit Command", "selfm.foconfig.rb_cmd", FT_UINT8, BASE_HEX, VALS(selfm_fo_rb_vals), 0x0, NULL, HFILL }},
2601         /* "Alternate Fast Operate Configuration" specific fields */
2602         { &hf_selfm_alt_foconfig_len,
2603         { "Length", "selfm.alt_foconfig.len", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2604         { &hf_selfm_alt_foconfig_num_ports,
2605         { "Number of Ports Available", "selfm.alt_foconfig.num_ports", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2606         { &hf_selfm_alt_foconfig_num_brkr,
2607         { "Number of Breaker Bits per Port", "selfm.alt_foconfig.num_brkr", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2608         { &hf_selfm_alt_foconfig_num_rb,
2609         { "Number of Remote Bits per Port", "selfm.alt_foconfig.num_rb", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2610         { &hf_selfm_alt_foconfig_funccode,
2611         { "Supported Function Code", "selfm.alt_foconfig.funccode", FT_UINT8, BASE_HEX, VALS(selfm_foconfig_alt_funccode_vals), 0x0, NULL, HFILL }},
2612         /* "Fast Operate Command" specific fields */
2613         { &hf_selfm_fastop_len,
2614         { "Length", "selfm.fastop.len", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2615         { &hf_selfm_fastop_rb_code,
2616         { "Remote Bit Operate Code", "selfm.fastop.rb_code", FT_UINT8, BASE_HEX, VALS(selfm_fo_rb_vals), 0x0, NULL, HFILL }},
2617         { &hf_selfm_fastop_br_code,
2618         { "Breaker Bit Operate Code", "selfm.fastop.br_code", FT_UINT8, BASE_HEX, VALS(selfm_fo_br_vals), 0x0, NULL, HFILL }},
2619         { &hf_selfm_fastop_valid,
2620         { "Operate Code Validation", "selfm.fastop.valid", FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL }},
2621         /* "Alternate Fast Operate Command" specific fields */
2622         { &hf_selfm_alt_fastop_len,
2623         { "Length", "selfm.alt_fastop.len", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2624         { &hf_selfm_alt_fastop_code,
2625         { "Operate Code", "selfm.alt_fastop.code", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }},
2626         { &hf_selfm_alt_fastop_valid,
2627         { "Operate Code Validation", "selfm.alt_fastop.valid", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }},
2628         /* "Fast SER Message" specific fields */
2629         { &hf_selfm_fastser_len,
2630         { "Length", "selfm.fastser.len", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2631         { &hf_selfm_fastser_routing_addr,
2632         { "Routing Address (future)", "selfm.fastser.routing_addr", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
2633         { &hf_selfm_fastser_status,
2634         { "Status Byte", "selfm.fastser.status", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2635         { &hf_selfm_fastser_funccode,
2636         { "Function Code", "selfm.fastser.funccode", FT_UINT8, BASE_HEX, VALS(selfm_fastser_func_code_vals), 0x0, NULL, HFILL }},
2637         { &hf_selfm_fastser_seq,
2638         { "Sequence Byte", "selfm.fastser.seq", FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL }},
2639         { &hf_selfm_fastser_seq_fir,
2640         { "FIR", "selfm.fastser.seq_fir", FT_BOOLEAN, 8, NULL, FAST_SER_SEQ_FIR, NULL, HFILL }},
2641         { &hf_selfm_fastser_seq_fin,
2642         { "FIN", "selfm.fastser.seq_fin", FT_BOOLEAN, 8, NULL, FAST_SER_SEQ_FIN, NULL, HFILL }},
2643         { &hf_selfm_fastser_seq_cnt,
2644         { "Count", "selfm.fastser.seq_cnt", FT_UINT8, BASE_DEC, NULL, FAST_SER_SEQ_CNT, "Frame Count Number", HFILL }},
2645         { &hf_selfm_fastser_resp_num,
2646         { "Response Number", "selfm.fastser.resp_num", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2647         { &hf_selfm_fastser_crc16,
2648         { "CRC-16", "selfm.fastser.crc16", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }},
2649         { &hf_selfm_fastser_def_route_sup,
2650         { "Routing Support", "selfm.fastser.def_route_sup", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2651         { &hf_selfm_fastser_def_rx_stat,
2652         { "Status RX", "selfm.fastser.def_rx_stat", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2653         { &hf_selfm_fastser_def_tx_stat,
2654         { "Status TX", "selfm.fastser.def_tx_stat", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2655         { &hf_selfm_fastser_def_rx_maxfr,
2656         { "Max Frames RX", "selfm.fastser.def_rx_maxfr", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2657         { &hf_selfm_fastser_def_tx_maxfr,
2658         { "Max Frames TX", "selfm.fastser.def_tx_maxfr", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2659         { &hf_selfm_fastser_def_rx_num_fc,
2660         { "Number of Supported RX Function Codes", "selfm.fastser.def_rx_num_fc", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2661         { &hf_selfm_fastser_def_rx_fc,
2662         { "Receive Function Code", "selfm.fastser.def_rx_fc", FT_UINT8, BASE_HEX, VALS(selfm_fastser_func_code_vals), 0x0, NULL, HFILL }},
2663         { &hf_selfm_fastser_def_tx_num_fc,
2664         { "Number of Supported TX Function Codes", "selfm.fastser.def_tx_num_fc", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2665         { &hf_selfm_fastser_def_tx_fc,
2666         { "Transmit Function Code", "selfm.fastser.def_tx_fc", FT_UINT8, BASE_HEX, VALS(selfm_fastser_func_code_vals), 0x0, NULL, HFILL }},
2667         { &hf_selfm_fastser_uns_en_fc,
2668         { "Function Code to Enable", "selfm.fastser.uns_en_fc", FT_UINT8, BASE_HEX, VALS(selfm_fastser_func_code_vals), 0x0, NULL, HFILL }},
2669         { &hf_selfm_fastser_uns_en_fc_data,
2670         { "Function Code Data", "selfm.fastser.uns_en_fc_data", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
2671         { &hf_selfm_fastser_uns_dis_fc,
2672         { "Function Code to Disable", "selfm.fastser.uns_dis_fc", FT_UINT8, BASE_HEX, VALS(selfm_fastser_func_code_vals), 0x0, NULL, HFILL }},
2673         { &hf_selfm_fastser_uns_dis_fc_data,
2674         { "Function Code Data", "selfm.fastser.uns_dis_fc_data", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
2675         { &hf_selfm_fastser_unsresp_orig,
2676         { "Origination path", "selfm.fastser.unsresp_orig", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
2677         { &hf_selfm_fastser_unsresp_doy,
2678         { "Day of Year", "selfm.fastser.unsresp_doy", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2679         { &hf_selfm_fastser_unsresp_year,
2680         { "Year", "selfm.fastser.unsresp_year", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2681         { &hf_selfm_fastser_unsresp_todms,
2682         { "Time of Day (in ms)", "selfm.fastser.unsresp_todms", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2683         { &hf_selfm_fastser_unsresp_num_elmt,
2684         { "Number of SER Elements", "selfm.fastser.unsresp_num_elmt", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2685         { &hf_selfm_fastser_unsresp_elmt_idx,
2686         { "SER Element Index", "selfm.fastser.unsresp_elmt_idx", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2687         { &hf_selfm_fastser_unsresp_elmt_ts_ofs,
2688         { "SER Element Timestamp Offset (us)", "selfm.fastser.unsresp_elmt_ts_ofs", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2689         { &hf_selfm_fastser_unsresp_elmt_status,
2690         { "SER Element Status", "selfm.fastser.unsresp_elmt_status", FT_UINT8, BASE_DEC, VALS(selfm_ser_status_vals), 0x0, NULL, HFILL }},
2691         { &hf_selfm_fastser_unsresp_eor,
2692         { "End of Record Indicator", "selfm.fastser.unsresp_eor", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
2693         { &hf_selfm_fastser_unsresp_elmt_statword,
2694         { "SER Element Status Word", "selfm.fastser.unsresp_elmt_statword", FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL }},
2695         { &hf_selfm_fastser_unswrite_addr1,
2696         { "Write Address Region #1", "selfm.fastser.unswrite_addr1", FT_UINT16, BASE_HEX, VALS(selfm_fastser_unswrite_com_vals), 0x0, NULL, HFILL }},
2697         { &hf_selfm_fastser_unswrite_addr2,
2698         { "Write Address Region #2", "selfm.fastser.unswrite_addr2", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }},
2699         { &hf_selfm_fastser_unswrite_num_reg,
2700         { "Number of Registers", "selfm.fastser.unswrite_num_reg", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2701         { &hf_selfm_fastser_unswrite_reg_val,
2702         { "Register Value", "selfm.fastser.unswrite_reg_val", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2703         { &hf_selfm_fastser_baseaddr,
2704         { "Base Address", "selfm.fastser.baseaddr", FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL }},
2705         { &hf_selfm_fastser_numwords,
2706         { "Number of 16-bit Words", "selfm.fastser.numwords", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2707         { &hf_selfm_fastser_flags,
2708         { "Flag Word", "selfm.fastser.flags", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }},
2709         { &hf_selfm_fastser_datafmt_resp_numitem,
2710         { "Number of Data Items Records", "selfm.fastser.datafmt_resp_numitem", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2711         { &hf_selfm_fastser_dataitem_qty,
2712         { "Data Item Quantity", "selfm.fastser.dataitem_qty", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2713         { &hf_selfm_fastser_dataitem_type,
2714         { "Data Item Type", "selfm.fastser.dataitem_type", FT_UINT16, BASE_HEX, VALS(selfm_fastser_tagtype_vals), 0x0, NULL, HFILL }},
2715         { &hf_selfm_fastser_dataitem_uint16,
2716         { "(uint16)", "selfm.fastser.dataitem_uint16", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2717         { &hf_selfm_fastser_dataitem_int16,
2718         { "(int16)", "selfm.fastser.dataitem_int16", FT_INT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2719         { &hf_selfm_fastser_dataitem_uint32,
2720         { "(uint32)", "selfm.fastser.dataitem_uint32", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2721         { &hf_selfm_fastser_dataitem_int32,
2722         { "(int32)", "selfm.fastser.dataitem_int32", FT_INT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2723         { &hf_selfm_fastser_dataitem_float,
2724         { "(float)", "selfm.fastser.dataitem_float", FT_FLOAT, BASE_NONE, NULL, 0x0, NULL, HFILL }},
2725         { &hf_selfm_fastser_devdesc_num_region,
2726         { "Number of Data Regions", "selfm.fastser.devdesc_num_region", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2727         { &hf_selfm_fastser_devdesc_num_ctrl,
2728         { "Number of Control Regions", "selfm.fastser.devdesc_num_ctrl", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2729         { &hf_selfm_fastser_soe_req_orig,
2730         { "Origination path", "selfm.fastser.soe_req_orig", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
2731         { &hf_selfm_fastser_soe_resp_numblks,
2732         { "Number of Blocks", "selfm.fastser.soe_resp_numblks", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2733         { &hf_selfm_fastser_soe_resp_orig,
2734         { "Origination path", "selfm.fastser.soe_resp_orig", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
2735         { &hf_selfm_fastser_soe_resp_numbits,
2736         { "Number of Bits", "selfm.fastser.soe_resp_numbits", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2737         { &hf_selfm_fastser_soe_resp_pad,
2738         { "Pad Byte", "selfm.fastser.soe_resp_pad", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2739         { &hf_selfm_fastser_soe_resp_doy,
2740         { "Day of Year", "selfm.fastser.soe_resp_doy", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2741         { &hf_selfm_fastser_soe_resp_year,
2742         { "Year", "selfm.fastser.soe_resp_year", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2743         { &hf_selfm_fastser_soe_resp_tod,
2744         { "Time of Day (ms)", "selfm.fastser.soe_resp_tod", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2745         /* { &hf_selfm_fastser_soe_resp_data,
2746         { "Packed Binary State Data", "selfm.fastser.soe_resp_data", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }}, */
2747
2748         /* "Fast SER Message" Re-assembly header fields */
2749         { &hf_selfm_fragment,
2750         { "SEL Fast Msg Response Data Fragment", "selfm.respdata.fragment", FT_FRAMENUM, BASE_NONE, NULL, 0x0, "SEL Fast Message Response Data Fragment", HFILL }},
2751         { &hf_selfm_fragments,
2752         { "SEL Fast Msg Response Data Fragments", "selfm.respdata.fragments", FT_NONE, BASE_NONE, NULL, 0x0, "SEL Fast Message Response Data Fragments", HFILL }},
2753         { &hf_selfm_fragment_overlap,
2754         { "Fragment overlap", "selfm.respdata.fragment.overlap", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "Fragment overlaps with other fragments", HFILL }},
2755         { &hf_selfm_fragment_overlap_conflict,
2756         { "Conflicting data in fragment overlap", "selfm.respdata.fragment.overlap.conflict", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "Overlapping fragments contained conflicting data", HFILL }},
2757         { &hf_selfm_fragment_multiple_tails,
2758         { "Multiple tail fragments found", "selfm.respdata.fragment.multipletails", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "Several tails were found when defragmenting the packet", HFILL }},
2759         { &hf_selfm_fragment_too_long_fragment,
2760         { "Fragment too long", "selfm.respdata.fragment.toolongfragment", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "Fragment contained data past end of packet", HFILL }},
2761         { &hf_selfm_fragment_error,
2762         { "Defragmentation error", "selfm.respdata.fragment.error", FT_FRAMENUM, BASE_NONE, NULL, 0x0, "Defragmentation error due to illegal fragments", HFILL }},
2763         { &hf_selfm_fragment_count,
2764         { "Fragment count", "selfm.respdata.fragment.count", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2765         { &hf_selfm_fragment_reassembled_in,
2766         { "Reassembled PDU In Frame", "selfm.respdata.fragment.reassembled_in", FT_FRAMENUM, BASE_NONE, NULL, 0x0, "This PDU is reassembled in this frame", HFILL }},
2767         { &hf_selfm_fragment_reassembled_length,
2768         { "Reassembled SEL Fast Msg length", "selfm.respdata.fragment.reassembled.length", FT_UINT32, BASE_DEC, NULL, 0x0, "The total length of the reassembled payload", HFILL }
2769     }
2770     };
2771
2772     /* Setup protocol subtree array */
2773     static gint *ett[] = {
2774         &ett_selfm,
2775         &ett_selfm_relaydef,
2776         &ett_selfm_relaydef_fm,
2777         &ett_selfm_relaydef_proto,
2778         &ett_selfm_relaydef_flags,
2779         &ett_selfm_fmconfig,
2780         &ett_selfm_fmconfig_ai,
2781         &ett_selfm_fmconfig_calc,
2782         &ett_selfm_foconfig,
2783         &ett_selfm_foconfig_brkr,
2784         &ett_selfm_foconfig_rb,
2785         &ett_selfm_fastop,
2786         &ett_selfm_fmdata,
2787         &ett_selfm_fmdata_ai,
2788         &ett_selfm_fmdata_dig,
2789         &ett_selfm_fmdata_ai_ch,
2790         &ett_selfm_fmdata_dig_ch,
2791         &ett_selfm_fastser,
2792         &ett_selfm_fastser_seq,
2793         &ett_selfm_fastser_def_fc,
2794         &ett_selfm_fastser_tag,
2795         &ett_selfm_fastser_element_list,
2796         &ett_selfm_fastser_element,
2797         &ett_selfm_fastser_datareg,
2798         &ett_selfm_fragment,
2799         &ett_selfm_fragments
2800
2801    };
2802
2803     module_t *selfm_module;
2804
2805     /* Register protocol init routine */
2806     register_init_routine(&selfm_init);
2807
2808     /* Register the protocol name and description */
2809     proto_selfm = proto_register_protocol("SEL Fast Message", "SEL Fast Message", "selfm");
2810
2811     /* Registering protocol to be called by another dissector */
2812     new_register_dissector("selfm", dissect_selfm_simple, proto_selfm);
2813
2814     /* Required function calls to register the header fields and subtrees used */
2815     proto_register_field_array(proto_selfm, selfm_hf, array_length(selfm_hf));
2816     proto_register_subtree_array(ett, array_length(ett));
2817
2818
2819     /* Register required preferences for SEL Protocol register decoding */
2820     selfm_module = prefs_register_protocol(proto_selfm, proto_reg_handoff_selfm);
2821
2822     /*  SEL Protocol - Desegmentmentation; defaults to TRUE for TCP desegmentation*/
2823     prefs_register_bool_preference(selfm_module, "desegment",
2824                                   "Desegment all SEL Fast Message Protocol packets spanning multiple TCP segments",
2825                                   "Whether the SEL Protocol dissector should desegment all messages spanning multiple TCP segments",
2826                                   &selfm_desegment);
2827
2828     /* SEL Protocol - Telnet protocol IAC (0xFF) processing; defaults to TRUE to allow Telnet Encapsulated Data */
2829     prefs_register_bool_preference(selfm_module, "telnetclean",
2830                                   "Enable Automatic pre-processing of Telnet-encapsulated data to remove extra 0xFF (IAC) bytes",
2831                                   "Whether the SEL Protocol dissector should automatically pre-process Telnet data to remove IAC bytes",
2832                                   &selfm_telnet_clean);
2833
2834     /* SEL Protocol Preference - Default TCP Port, allows for "user" port either than 0. */
2835     prefs_register_uint_preference(selfm_module, "tcp.port", "SEL Protocol Port",
2836                        "Set the TCP port for SEL FM Protocol packets (if other"
2837                        " than the default of 0)",
2838                        10, &global_selfm_tcp_port);
2839
2840 }
2841
2842 /******************************************************************************************************/
2843 /* If this dissector uses sub-dissector registration add a registration routine.
2844    This format is required because a script is used to find these routines and
2845    create the code that calls these routines.
2846  */
2847 /******************************************************************************************************/
2848 void
2849 proto_reg_handoff_selfm(void)
2850 {
2851     static int selfm_prefs_initialized = FALSE;
2852     static dissector_handle_t selfm_handle;
2853     static unsigned int selfm_port;
2854
2855     /* Make sure to use SEL FM Protocol Preferences field to determine default TCP port */
2856     if (! selfm_prefs_initialized) {
2857         selfm_handle = new_create_dissector_handle(dissect_selfm_tcp, proto_selfm);
2858         selfm_prefs_initialized = TRUE;
2859     }
2860     else {
2861         dissector_delete_uint("tcp.port", selfm_port, selfm_handle);
2862     }
2863
2864     selfm_port = global_selfm_tcp_port;
2865
2866     dissector_add_uint("tcp.port", selfm_port, selfm_handle);
2867 }
2868
2869 /*
2870  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
2871  *
2872  * Local variables:
2873  * c-basic-offset: 4
2874  * tab-width: 8
2875  * indent-tabs-mode: nil
2876  * End:
2877  *
2878  * vi: set shiftwidth=4 tabstop=8 expandtab:
2879  * :indentSize=4:tabSize=8:noTabs=true:
2880  */