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