2 * Routines for CANopen dissection
3 * Copyright 2011, Yegor Yefremov <yegorslists@googlemail.com>
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include <epan/packet.h>
30 void proto_register_canopen(void);
32 /* Initialize the protocol and registered fields */
33 static int proto_canopen = -1;
34 static int hf_canopen_cob_id = -1;
35 static int hf_canopen_function_code = -1;
36 static int hf_canopen_node_id = -1;
37 static int hf_canopen_pdo_data = -1;
38 static int hf_canopen_pdo_data_string = -1;
39 static int hf_canopen_sdo_cmd = -1;
40 static int hf_canopen_sdo_main_idx = -1;
41 static int hf_canopen_sdo_sub_idx = -1;
42 static int hf_canopen_sdo_data = -1;
43 static int hf_canopen_em_err_code = -1;
44 static int hf_canopen_em_err_reg = -1;
45 static int hf_canopen_em_err_field = -1;
46 static int hf_canopen_nmt_ctrl_cs = -1;
47 static int hf_canopen_nmt_ctrl_node_id = -1;
48 static int hf_canopen_nmt_guard_state = -1;
49 static int hf_canopen_time_stamp = -1;
50 static int hf_canopen_time_stamp_ms = -1;
51 static int hf_canopen_time_stamp_days = -1;
53 /* Initialize the subtree pointers */
54 static gint ett_canopen = -1;
56 /* broadcast messages */
59 #define FC_TIME_STAMP 0x2
61 /* point-to-point messages */
62 #define FC_EMERGENCY 0x1
63 #define FC_PDO1_TX 0x3
64 #define FC_PDO1_RX 0x4
65 #define FC_PDO2_TX 0x5
66 #define FC_PDO2_RX 0x6
67 #define FC_PDO3_TX 0x7
68 #define FC_PDO3_RX 0x8
69 #define FC_PDO4_TX 0x9
70 #define FC_PDO4_RX 0xA
71 #define FC_DEFAULT_SDO_TX 0xB
72 #define FC_DEFAULT_SDO_RX 0xC
73 #define FC_NMT_ERR_CONTROL 0xE
75 static const value_string CAN_open_bcast_msg_type_vals[] = {
76 { FC_NMT, "EMERGENCY"},
78 { FC_TIME_STAMP, "TIME STAMP"},
82 static const value_string CAN_open_p2p_msg_type_vals[] = {
83 { FC_EMERGENCY, "EMERGENCY"},
84 { FC_PDO1_TX, "PDO1 (tx)"},
85 { FC_PDO1_RX, "PDO1 (rx)"},
86 { FC_PDO2_TX, "PDO2 (tx)"},
87 { FC_PDO2_RX, "PDO2 (rx)"},
88 { FC_PDO3_TX, "PDO3 (tx)"},
89 { FC_PDO3_RX, "PDO3 (rx)"},
90 { FC_PDO4_TX, "PDO4 (tx)"},
91 { FC_PDO4_RX, "PDO4 (rx)"},
92 { FC_DEFAULT_SDO_TX, "Default-SDO (tx)"},
93 { FC_DEFAULT_SDO_RX, "Default-SDO (rx)"},
94 { FC_NMT_ERR_CONTROL, "NMT Error Control"},
100 #define MT_NMT_CTRL 1
102 #define MT_TIME_STAMP 3
103 #define MT_EMERGENCY 4
106 #define MT_NMT_GUARD 7
108 /* TIME STAMP conversion defines */
109 #define TS_DAYS_BETWEEN_1970_AND_1984 5113
110 #define TS_SECONDS_IN_PER_DAY 86400
111 #define TS_NANOSEC_PER_MSEC 1000000
113 /* NMT command specifiers */
114 static const value_string nmt_ctrl_cs[] = {
115 { 0x01, "Start remote node"},
116 { 0x02, "Stop remote node"},
117 { 0x80, "Enter pre-operational state"},
118 { 0x81, "Reset node"},
119 { 0x82, "Reset communication"},
124 static const value_string nmt_guard_state[] = {
125 { 0x00, "Initialising"},
126 { 0x01, "Disconnected"},
127 { 0x02, "Connecting"},
128 { 0x03, "Preparing"},
130 { 0x05, "Operational"},
131 { 0x7F, "Pre-operational"},
136 canopen_detect_msg_type(guint function_code, guint node_id)
138 switch (function_code) {
150 return MT_TIME_STAMP;
176 case FC_DEFAULT_SDO_TX:
179 case FC_DEFAULT_SDO_RX:
182 case FC_NMT_ERR_CONTROL:
191 struct can_identifier
196 /* Code to actually dissect the packets */
198 dissect_canopen(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data)
202 guint32 time_stamp_msec;
203 guint32 time_stamp_days;
204 struct can_identifier can_id;
207 gint can_data_len = tvb_reported_length(tvb);
208 const gchar *function_code_str;
211 DISSECTOR_ASSERT(data);
212 can_id = *((struct can_identifier*)data);
214 col_set_str(pinfo->cinfo, COL_PROTOCOL, "CANopen");
215 col_clear(pinfo->cinfo, COL_INFO);
217 node_id = can_id.id & 0x7F;
218 function_code = (can_id.id >> 7) & 0x0F;
220 msg_type_id = canopen_detect_msg_type(function_code, node_id);
224 function_code_str = val_to_str(function_code, CAN_open_bcast_msg_type_vals, "Unknown (%u)");
225 col_add_fstr(pinfo->cinfo, COL_INFO, "%s", function_code_str);
228 function_code_str = val_to_str(function_code, CAN_open_p2p_msg_type_vals, "Unknown (%u)");
229 col_add_fstr(pinfo->cinfo, COL_INFO, "p2p %s", function_code_str);
231 col_append_fstr(pinfo->cinfo, COL_INFO, " %s",
232 tvb_bytes_to_ep_str_punct(tvb, offset, can_data_len, ' '));
235 proto_item *ti, *cob_ti, *type_ti;
236 proto_tree *canopen_tree;
237 proto_tree *canopen_cob_tree;
238 proto_tree *canopen_type_tree;
240 ti = proto_tree_add_item(tree, proto_canopen, tvb, 0, -1, ENC_NA);
241 canopen_tree = proto_item_add_subtree(ti, ett_canopen);
243 /* add COB-ID with function code and node id */
244 cob_ti = proto_tree_add_uint(canopen_tree, hf_canopen_cob_id, tvb, 0, 0, can_id.id);
245 canopen_cob_tree = proto_item_add_subtree(cob_ti, ett_canopen);
247 /* add function code */
248 ti = proto_tree_add_uint(canopen_cob_tree, hf_canopen_function_code, tvb, 0, 0, can_id.id);
249 PROTO_ITEM_SET_GENERATED(ti);
252 ti = proto_tree_add_uint(canopen_cob_tree, hf_canopen_node_id, tvb, 0, 0, can_id.id);
253 PROTO_ITEM_SET_GENERATED(ti);
255 /* add CANopen frame type */
257 type_ti = proto_tree_add_text(canopen_tree, tvb, 0,
258 (msg_type_id != MT_SYNC) ? -1 : 0,
259 "Type: %s", function_code_str);
260 canopen_type_tree = proto_item_add_subtree(type_ti, ett_canopen);
265 proto_tree_add_item(canopen_type_tree,
266 hf_canopen_nmt_ctrl_cs, tvb, offset, 1, ENC_BIG_ENDIAN);
269 proto_tree_add_item(canopen_type_tree,
270 hf_canopen_nmt_ctrl_node_id, tvb, offset, 1, ENC_BIG_ENDIAN);
273 proto_tree_add_item(canopen_type_tree,
274 hf_canopen_nmt_guard_state, tvb, offset, 1, ENC_BIG_ENDIAN);
279 /* calculate the real time stamp */
280 time_stamp_msec = tvb_get_letohl(tvb, offset);
281 time_stamp_days = tvb_get_ntohs(tvb, offset + 4);
282 time_stamp.secs = (time_stamp_days + TS_DAYS_BETWEEN_1970_AND_1984)
283 * TS_SECONDS_IN_PER_DAY + (time_stamp_msec / 1000);
284 time_stamp.nsecs = (time_stamp_msec % 1000) * TS_NANOSEC_PER_MSEC;
286 proto_tree_add_time(canopen_type_tree,
287 hf_canopen_time_stamp, tvb, offset, 6, &time_stamp);
289 proto_tree_add_uint(canopen_type_tree,
290 hf_canopen_time_stamp_ms, tvb, offset, 4, time_stamp_msec);
293 proto_tree_add_uint(canopen_type_tree,
294 hf_canopen_time_stamp_days, tvb, offset, 2, time_stamp_days);
298 proto_tree_add_item(canopen_type_tree,
299 hf_canopen_em_err_code, tvb, offset, 2, ENC_BIG_ENDIAN);
302 proto_tree_add_item(canopen_type_tree,
303 hf_canopen_em_err_reg, tvb, offset, 1, ENC_BIG_ENDIAN);
306 proto_tree_add_item(canopen_type_tree,
307 hf_canopen_em_err_field, tvb, offset, 4, ENC_NA);
310 if (can_data_len != 0) {
311 proto_tree_add_item(canopen_type_tree,
312 hf_canopen_pdo_data, tvb, offset, can_data_len, ENC_NA);
315 proto_tree_add_string(canopen_type_tree,
316 hf_canopen_pdo_data_string, tvb, offset, 0, "empty");
320 proto_tree_add_item(canopen_type_tree,
321 hf_canopen_sdo_cmd, tvb, offset, 1, ENC_BIG_ENDIAN);
324 proto_tree_add_item(canopen_type_tree,
325 hf_canopen_sdo_main_idx, tvb, offset, 2, ENC_BIG_ENDIAN);
328 proto_tree_add_item(canopen_type_tree,
329 hf_canopen_sdo_sub_idx, tvb, offset, 1, ENC_BIG_ENDIAN);
332 proto_tree_add_item(canopen_type_tree,
333 hf_canopen_sdo_data, tvb, offset, 4, ENC_NA);
338 return tvb_length(tvb);
342 /* Register the protocol with Wireshark */
344 proto_register_canopen(void)
346 static hf_register_info hf[] = {
347 { &hf_canopen_cob_id,
348 { "COB-ID", "canopen.cob_id",
349 FT_UINT32, BASE_HEX, NULL, 0x0,
352 { &hf_canopen_function_code,
353 { "Function code", "canopen.function_code",
354 FT_UINT32, BASE_HEX, NULL, 0x780,
357 { &hf_canopen_node_id,
358 { "Node-ID", "canopen.node_id",
359 FT_UINT32, BASE_HEX, NULL, 0x7F,
362 { &hf_canopen_pdo_data,
363 { "Data", "canopen.pdo.data",
364 FT_BYTES, BASE_NONE, NULL, 0x0,
367 { &hf_canopen_pdo_data_string,
368 { "Data", "canopen.pdo.data",
369 FT_STRINGZ, BASE_NONE, NULL, 0x0,
372 { &hf_canopen_sdo_cmd,
373 { "SDO command byte", "canopen.sdo.cmd",
374 FT_UINT8, BASE_HEX, NULL, 0x0,
377 { &hf_canopen_sdo_main_idx,
378 { "OD main-index", "canopen.sdo.main_idx",
379 FT_UINT16, BASE_HEX, NULL, 0x0,
382 { &hf_canopen_sdo_sub_idx,
383 { "OD sub-index", "canopen.sdo.sub_idx",
384 FT_UINT8, BASE_HEX, NULL, 0x0,
387 { &hf_canopen_sdo_data,
388 { "Data", "canopen.sdo.data",
389 FT_BYTES, BASE_NONE, NULL, 0x0,
392 { &hf_canopen_em_err_code,
393 { "Error code", "canopen.em.err_code",
394 FT_UINT16, BASE_HEX, NULL, 0x0,
397 { &hf_canopen_em_err_reg,
398 { "Error register", "canopen.em.err_reg",
399 FT_UINT8, BASE_HEX, NULL, 0x0,
402 { &hf_canopen_em_err_field,
403 { "Manufacture specific error field", "canopen.em.err_field",
404 FT_BYTES, BASE_NONE, NULL, 0x0,
407 { &hf_canopen_nmt_ctrl_cs,
408 { "Command specifier", "canopen.nmt_ctrl.cd",
409 FT_UINT8, BASE_HEX, VALS(nmt_ctrl_cs), 0xFF,
412 { &hf_canopen_nmt_ctrl_node_id,
413 { "Node-ID", "canopen.nmt_ctrl.node_id",
414 FT_UINT8, BASE_HEX, NULL, 0x0,
417 { &hf_canopen_nmt_guard_state,
418 { "Node-ID", "canopen.nmt_guard.state",
419 FT_UINT8, BASE_HEX, VALS(nmt_guard_state), 0x7F,
422 { &hf_canopen_time_stamp,
423 { "Time stamp", "canopen.time_stamp",
424 FT_ABSOLUTE_TIME, ABSOLUTE_TIME_UTC, NULL, 0x0,
427 { &hf_canopen_time_stamp_ms,
428 { "Time, after Midnight in Milliseconds", "canopen.time_stamp_ms",
429 FT_UINT32, BASE_DEC, NULL, 0x0,
432 { &hf_canopen_time_stamp_days,
433 { "Current day since 1 Jan 1984", "canopen.time_stamp_days",
434 FT_UINT16, BASE_DEC, NULL, 0x0,
439 static gint *ett[] = {
443 proto_canopen = proto_register_protocol("CANopen",
447 new_register_dissector("canopen", dissect_canopen, proto_canopen);
449 proto_register_field_array(proto_canopen, hf, array_length(hf));
450 proto_register_subtree_array(ett, array_length(ett));