1 /* packet-zbee-zcl-ha.c
2 * Dissector routines for the ZigBee ZCL SE clusters like
4 * By Fabio Tarabelloni <fabio.tarabelloni@reloc.it>
5 * Copyright 2013 RELOC s.r.l.
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
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.
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.
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.
31 #include <epan/packet.h>
32 #include <epan/to_str.h>
34 #include "packet-zbee.h"
35 #include "packet-zbee-aps.h"
36 #include "packet-zbee-zcl.h"
38 /* ########################################################################## */
39 /* #### (0x0703) MESSAGING CLUSTER ########################################## */
40 /* ########################################################################## */
42 /*************************/
44 /*************************/
46 #define ZBEE_ZCL_MSG_NUM_GENERIC_ETT 2
47 #define ZBEE_ZCL_MSG_NUM_ETT (ZBEE_ZCL_MSG_NUM_GENERIC_ETT)
49 /* Attributes - None */
51 /* Server Commands Received */
52 #define ZBEE_ZCL_CMD_ID_MSG_GET_LAST_MSG 0x00 /* Get Last Message */
53 #define ZBEE_ZCL_CMD_ID_MSG_MSG_CONFIRM 0x01 /* Message Confirmation */
55 /* Server Commands Generated */
56 #define ZBEE_ZCL_CMD_ID_MSG_DISPLAY_MSG 0x00 /* Display Message */
57 #define ZBEE_ZCL_CMD_ID_MSG_CANCEL_MSG 0x01 /* Cancel Message */
59 /* Message Control Field Bit Map */
60 #define ZBEE_ZCL_MSG_CTRL_TX_MASK 0x03
61 #define ZBEE_ZCL_MSG_CTRL_IMPORTANCE_MASK 0x0C
62 #define ZBEE_ZCL_MSG_CTRL_RESERVED_MASK 0x70
63 #define ZBEE_ZCL_MSG_CTRL_CONFIRM_MASK 0x80
65 #define ZBEE_ZCL_MSG_CTRL_TX_NORMAL_ONLY 0x00 /* Normal Transmission Only */
66 #define ZBEE_ZCL_MSG_CTRL_TX_NORMAL_ANON_INTERPAN 0x01 /* Normal and Anonymous Inter-PAN Transmission Only */
67 #define ZBEE_ZCL_MSG_CTRL_TX_ANON_INTERPAN_ONLY 0x02 /* Anonymous Inter-PAN Transmission Only */
69 #define ZBEE_ZCL_MSG_CTRL_IMPORTANCE_LOW 0x00 /* Low */
70 #define ZBEE_ZCL_MSG_CTRL_IMPORTANCE_MEDIUM 0x01 /* Medium */
71 #define ZBEE_ZCL_MSG_CTRL_IMPORTANCE_HIGH 0x02 /* High */
72 #define ZBEE_ZCL_MSG_CTRL_IMPORTANCE_CRITICAL 0x03 /* Critical */
74 #define ZBEE_ZCL_MSG_START_TIME_NOW 0x00000000 /* Now */
76 /*************************/
77 /* Function Declarations */
78 /*************************/
79 void proto_register_zbee_zcl_msg(void);
80 void proto_reg_handoff_zbee_zcl_msg(void);
82 /* Command Dissector Helpers */
83 static void dissect_zcl_msg_display (tvbuff_t *tvb, proto_tree *tree, guint *offset);
84 static void dissect_zcl_msg_cancel (tvbuff_t *tvb, proto_tree *tree, guint *offset);
85 static void dissect_zcl_msg_confirm (tvbuff_t *tvb, proto_tree *tree, guint *offset);
86 static void dissect_zcl_msg_cmd_id (proto_tree *tree, tvbuff_t *tvb, guint *offset, guint8 cmd_dir);
88 /* Private functions prototype */
89 static void decode_zcl_msg_duration (gchar *s, guint16 value);
91 /*************************/
92 /* Global Variables */
93 /*************************/
95 /* Initialize the protocol and registered fields */
96 static int proto_zbee_zcl_msg = -1;
98 static int hf_zbee_zcl_msg_srv_tx_cmd_id = -1;
99 static int hf_zbee_zcl_msg_srv_rx_cmd_id = -1;
100 static int hf_zbee_zcl_msg_message_id = -1;
101 static int hf_zbee_zcl_msg_ctrl_tx = -1;
102 static int hf_zbee_zcl_msg_ctrl_importance = -1;
103 static int hf_zbee_zcl_msg_ctrl_reserved = -1;
104 static int hf_zbee_zcl_msg_ctrl_confirm = -1;
105 static int hf_zbee_zcl_msg_start_time = -1;
106 static int hf_zbee_zcl_msg_duration = -1;
107 static int hf_zbee_zcl_msg_message_length =- 1;
108 static int hf_zbee_zcl_msg_message = -1;
109 static int hf_zbee_zcl_msg_confirm_time = -1;
111 /* Initialize the subtree pointers */
112 static gint ett_zbee_zcl_msg = -1;
113 static gint ett_zbee_zcl_msg_message_control = -1;
115 /* Server Commands Received */
116 static const value_string zbee_zcl_msg_srv_rx_cmd_names[] = {
117 { ZBEE_ZCL_CMD_ID_MSG_GET_LAST_MSG, "Get Last Message" },
118 { ZBEE_ZCL_CMD_ID_MSG_MSG_CONFIRM, "Message Confirmation" },
122 /* Server Commands Generated */
123 static const value_string zbee_zcl_msg_srv_tx_cmd_names[] = {
124 { ZBEE_ZCL_CMD_ID_MSG_DISPLAY_MSG, "Display Message" },
125 { ZBEE_ZCL_CMD_ID_MSG_CANCEL_MSG, "Cancel Message" },
129 /* Message Control Transmission */
130 static const value_string zbee_zcl_msg_ctrl_tx_names[] = {
131 { ZBEE_ZCL_MSG_CTRL_TX_NORMAL_ONLY, "Normal Transmission Only" },
132 { ZBEE_ZCL_MSG_CTRL_TX_NORMAL_ANON_INTERPAN, "Normal and Anonymous Inter-PAN Transmission Only" },
133 { ZBEE_ZCL_MSG_CTRL_TX_ANON_INTERPAN_ONLY, "Anonymous Inter-PAN Transmission Only" },
137 /* Message Control Importance */
138 static const value_string zbee_zcl_msg_ctrl_importance_names[] = {
139 { ZBEE_ZCL_MSG_CTRL_IMPORTANCE_LOW, "Low" },
140 { ZBEE_ZCL_MSG_CTRL_IMPORTANCE_MEDIUM, "Medium" },
141 { ZBEE_ZCL_MSG_CTRL_IMPORTANCE_HIGH, "High" },
142 { ZBEE_ZCL_MSG_CTRL_IMPORTANCE_CRITICAL, "Critical" },
147 /*************************/
148 /* Function Bodies */
149 /*************************/
151 /*FUNCTION:------------------------------------------------------
153 * dissect_zbee_zcl_msg
155 * ZigBee ZCL Messaging cluster dissector for wireshark.
157 * tvbuff_t *tvb - pointer to buffer containing raw packet.
158 * packet_info *pinfo - pointer to packet information fields
159 * proto_tree *tree - pointer to data tree Wireshark uses to display packet.
162 *---------------------------------------------------------------
165 dissect_zbee_zcl_msg(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data)
167 proto_item *payload_root;
168 proto_tree *payload_tree;
169 zbee_zcl_packet *zcl;
174 /* Reject the packet if data is NULL */
177 zcl = (zbee_zcl_packet *)data;
178 cmd_id = zcl->cmd_id;
180 /* Create a subtree for the ZCL Command frame, and add the command ID to it. */
181 if (zcl->direction == ZBEE_ZCL_FCF_TO_SERVER) {
182 /* Append the command name to the info column. */
183 col_append_fstr(pinfo->cinfo, COL_INFO, "%s, Seq: %u",
184 val_to_str_const(cmd_id, zbee_zcl_msg_srv_rx_cmd_names, "Unknown Command"),
187 /* Add the command ID. */
188 proto_tree_add_item(tree, hf_zbee_zcl_msg_srv_rx_cmd_id, tvb, offset, 1, cmd_id);
190 /* Check is this command has a payload, than add the payload tree */
191 rem_len = tvb_reported_length_remaining(tvb, ++offset);
193 payload_root = proto_tree_add_text(tree, tvb, offset, rem_len, "Payload");
194 payload_tree = proto_item_add_subtree(payload_root, ett_zbee_zcl_msg);
196 /* Call the appropriate command dissector */
199 case ZBEE_ZCL_CMD_ID_MSG_GET_LAST_MSG:
203 case ZBEE_ZCL_CMD_ID_MSG_MSG_CONFIRM:
204 dissect_zcl_msg_confirm(tvb, payload_tree, &offset);
212 else { /* ZBEE_ZCL_FCF_TO_CLIENT */
213 /* Append the command name to the info column. */
214 col_append_fstr(pinfo->cinfo, COL_INFO, "%s, Seq: %u",
215 val_to_str_const(cmd_id, zbee_zcl_msg_srv_tx_cmd_names, "Unknown Command"),
218 /* Add the command ID. */
219 proto_tree_add_item(tree, hf_zbee_zcl_msg_srv_tx_cmd_id, tvb, offset, 1, cmd_id);
221 /* Check is this command has a payload, than add the payload tree */
222 rem_len = tvb_reported_length_remaining(tvb, ++offset);
224 payload_root = proto_tree_add_text(tree, tvb, offset, rem_len, "Payload");
225 payload_tree = proto_item_add_subtree(payload_root, ett_zbee_zcl_msg);
227 /* Call the appropriate command dissector */
230 case ZBEE_ZCL_CMD_ID_MSG_DISPLAY_MSG:
231 dissect_zcl_msg_display(tvb, payload_tree, &offset);
234 case ZBEE_ZCL_CMD_ID_MSG_CANCEL_MSG:
235 dissect_zcl_msg_cancel(tvb, payload_tree, &offset);
244 return tvb_length(tvb);
245 } /*dissect_zbee_zcl_msg*/
247 /*FUNCTION:------------------------------------------------------
249 * dissect_zcl_msg_display
251 * This function manages the Display Message payload
253 * tvbuff_t *tvb - pointer to buffer containing raw packet.
254 * proto_tree *tree - pointer to data tree Wireshark uses to display packet.
258 *---------------------------------------------------------------
261 dissect_zcl_msg_display(tvbuff_t *tvb, proto_tree *tree, guint *offset)
263 proto_tree *sub_tree = NULL;
269 /* Retrieve "Message ID" field */
270 proto_tree_add_item(tree, hf_zbee_zcl_msg_message_id, tvb, *offset, 4, ENC_LITTLE_ENDIAN);
273 /* Retrieve "Message Control" field */
274 control = tvb_get_guint8(tvb, *offset);
275 ti = proto_tree_add_text(tree, tvb, *offset, 1, "Message Control: 0x%02x", control);
276 sub_tree = proto_item_add_subtree(ti, ett_zbee_zcl_msg_message_control);
277 proto_tree_add_item(sub_tree, hf_zbee_zcl_msg_ctrl_tx, tvb, *offset, 1, ENC_NA);
278 proto_tree_add_item(sub_tree, hf_zbee_zcl_msg_ctrl_importance, tvb, *offset, 1, ENC_NA);
279 proto_tree_add_item(sub_tree, hf_zbee_zcl_msg_ctrl_reserved, tvb, *offset, 1, ENC_NA);
280 proto_tree_add_item(sub_tree, hf_zbee_zcl_msg_ctrl_confirm, tvb, *offset, 1, ENC_NA);
283 /* Retrieve "Start Time" field */
284 proto_tree_add_item(tree, hf_zbee_zcl_msg_start_time, tvb, *offset, 4, ENC_LITTLE_ENDIAN);
287 /* Retrieve "Duration In Minutes" field */
288 proto_tree_add_item(tree, hf_zbee_zcl_msg_duration, tvb, *offset, 2, ENC_LITTLE_ENDIAN);
291 /* Retrieve "Message Length" field */
292 msg_len = tvb_get_guint8(tvb, *offset); /* string length */
293 proto_tree_add_item(tree, hf_zbee_zcl_msg_message_length, tvb, *offset, 1, ENC_NA);
296 /* Retrieve "Message" field */
297 msg_data = tvb_get_string_enc(wmem_packet_scope(), tvb, *offset, msg_len, ENC_LITTLE_ENDIAN);
298 proto_tree_add_string(tree, hf_zbee_zcl_msg_message, tvb, *offset, msg_len, msg_data);
301 } /*dissect_zcl_msg_display*/
303 /*FUNCTION:------------------------------------------------------
305 * dissect_zcl_msg_cancel
307 * This function manages the Cancel Message payload
309 * tvbuff_t *tvb - pointer to buffer containing raw packet.
310 * proto_tree *tree - pointer to data tree Wireshark uses to display packet.
314 *---------------------------------------------------------------
317 dissect_zcl_msg_cancel(tvbuff_t *tvb, proto_tree *tree, guint *offset)
319 proto_tree *sub_tree = NULL;
323 /* Retrieve "Message ID" field */
324 proto_tree_add_item(tree, hf_zbee_zcl_msg_message_id, tvb, *offset, 4, ENC_LITTLE_ENDIAN);
327 /* Retrieve "Message Control" field */
328 control = tvb_get_guint8(tvb, *offset);
329 ti = proto_tree_add_text(tree, tvb, *offset, 1, "Message Control: 0x%02x", control);
330 sub_tree = proto_item_add_subtree(ti, ett_zbee_zcl_msg_message_control);
331 proto_tree_add_item(sub_tree, hf_zbee_zcl_msg_ctrl_tx, tvb, *offset, 1, ENC_NA);
332 proto_tree_add_item(sub_tree, hf_zbee_zcl_msg_ctrl_importance, tvb, *offset, 1, ENC_NA);
333 proto_tree_add_item(sub_tree, hf_zbee_zcl_msg_ctrl_reserved, tvb, *offset, 1, ENC_NA);
334 proto_tree_add_item(sub_tree, hf_zbee_zcl_msg_ctrl_confirm, tvb, *offset, 1, ENC_NA);
338 /*FUNCTION:------------------------------------------------------
340 * dissect_zcl_msg_confirm
342 * This function manages the Message Confirmation payload
344 * tvbuff_t *tvb - pointer to buffer containing raw packet.
345 * proto_tree *tree - pointer to data tree Wireshark uses to display packet.
349 *---------------------------------------------------------------
352 dissect_zcl_msg_confirm(tvbuff_t *tvb, proto_tree *tree, guint *offset)
354 nstime_t confirm_time;
356 /* Retrieve "Message ID" field */
357 proto_tree_add_item(tree, hf_zbee_zcl_msg_message_id, tvb, *offset, 4, ENC_LITTLE_ENDIAN);
360 /* Retrieve "Confirmation Time" field */
361 confirm_time.secs = tvb_get_letohl(tvb, *offset) + ZBEE_ZCL_NSTIME_UTC_OFFSET;
362 confirm_time.nsecs = 0;
363 proto_tree_add_time(tree, hf_zbee_zcl_msg_confirm_time, tvb, *offset, 4, &confirm_time);
367 /*FUNCTION:------------------------------------------------------
369 * decode_zcl_msg_duration
371 * this function decodes duration in minute type variable
375 *---------------------------------------------------------------
378 decode_zcl_msg_duration(gchar *s, guint16 value)
381 g_snprintf(s, ITEM_LABEL_LENGTH, "Until changed");
383 g_snprintf(s, ITEM_LABEL_LENGTH, "%d minutes", value);
385 } /*decode_zcl_msg_duration*/
387 /*FUNCTION:------------------------------------------------------
389 * decode_zcl_msg_start_time
391 * this function decodes start time, with peculiarity case for
392 * messaging specifications.
394 * guint *s - string to display
395 * guint32 value - value to decode
398 *---------------------------------------------------------------
401 decode_zcl_msg_start_time(gchar *s, guint32 value)
403 if (value == ZBEE_ZCL_MSG_START_TIME_NOW)
404 g_snprintf(s, ITEM_LABEL_LENGTH, "Now");
406 value += ZBEE_ZCL_NSTIME_UTC_OFFSET;
407 g_snprintf(s, ITEM_LABEL_LENGTH, "%s", abs_time_secs_to_str (wmem_packet_scope(), value, ABSOLUTE_TIME_LOCAL, TRUE));
409 } /* decode_zcl_msg_start_time */
411 /*FUNCTION:------------------------------------------------------
413 * dissect_zcl_msg_cmd_id
415 * this function is called by ZCL foundation dissector in order to decode
416 * specific cluster command identifier.
418 * proto_tree *tree - pointer to data tree Wireshark uses to display packet.
419 * tvbuff_t *tvb - pointer to buffer containing raw packet.
420 * guint *offset - pointer to buffer offset
421 * guint8 cmd_dir - command direction
425 *---------------------------------------------------------------
428 dissect_zcl_msg_cmd_id(proto_tree *tree, tvbuff_t *tvb, guint *offset, guint8 cmd_dir)
430 if (cmd_dir == ZBEE_ZCL_FCF_TO_CLIENT)
431 proto_tree_add_item(tree, hf_zbee_zcl_msg_srv_rx_cmd_id, tvb, *offset, 1, ENC_NA);
433 proto_tree_add_item(tree, hf_zbee_zcl_msg_srv_tx_cmd_id, tvb, *offset, 1, ENC_NA);
435 } /*dissect_zcl_msg_cmd_id*/
437 /*FUNCTION:------------------------------------------------------
439 * proto_register_zbee_zcl_msg
441 * this function registers the ZCL Messaging dissector
442 * and all its information.
447 *---------------------------------------------------------------
450 proto_register_zbee_zcl_msg(void)
452 static hf_register_info hf[] = {
454 { &hf_zbee_zcl_msg_srv_tx_cmd_id,
455 { "Command", "zbee_zcl_se.msg.cmd.srv_tx.id", FT_UINT8, BASE_HEX, VALS(zbee_zcl_msg_srv_tx_cmd_names),
456 0x00, NULL, HFILL } },
458 { &hf_zbee_zcl_msg_srv_rx_cmd_id,
459 { "Command", "zbee_zcl_se.msg.cmd.srv_rx.id", FT_UINT8, BASE_HEX, VALS(zbee_zcl_msg_srv_rx_cmd_names),
460 0x00, NULL, HFILL } },
462 { &hf_zbee_zcl_msg_message_id,
463 { "Message ID", "zbee_zcl_se.msg.message.id", FT_UINT32, BASE_HEX, NULL,
464 0x00, NULL, HFILL } },
466 /* Start of 'Message Control' fields */
467 { &hf_zbee_zcl_msg_ctrl_tx,
468 { "Transmission", "zbee_zcl_se.msg.message.ctrl.tx", FT_UINT8, BASE_HEX, VALS(zbee_zcl_msg_ctrl_tx_names),
469 ZBEE_ZCL_MSG_CTRL_TX_MASK, NULL, HFILL } },
471 { &hf_zbee_zcl_msg_ctrl_importance,
472 { "Importance", "zbee_zcl_se.msg.message.ctrl.importance", FT_UINT8, BASE_HEX, VALS(zbee_zcl_msg_ctrl_importance_names),
473 ZBEE_ZCL_MSG_CTRL_IMPORTANCE_MASK, NULL, HFILL } },
475 { &hf_zbee_zcl_msg_ctrl_reserved,
476 { "Reserved", "zbee_zcl_se.msg.message.ctrl.reserved", FT_UINT8, BASE_HEX, NULL,
477 ZBEE_ZCL_MSG_CTRL_RESERVED_MASK, NULL, HFILL } },
479 { &hf_zbee_zcl_msg_ctrl_confirm,
480 { "Confirmation", "zbee_zcl_se.msg.message.ctrl.confirm", FT_BOOLEAN, 8, TFS(&tfs_requested_not_requested),
481 ZBEE_ZCL_MSG_CTRL_CONFIRM_MASK, NULL, HFILL } },
482 /* End of 'Message Control' fields */
484 { &hf_zbee_zcl_msg_start_time,
485 { "Start Time", "zbee_zcl_se.msg.message.start_time", FT_UINT32, BASE_CUSTOM, decode_zcl_msg_start_time,
486 0x00, NULL, HFILL } },
488 { &hf_zbee_zcl_msg_duration,
489 { "Duration", "zbee_zcl_se.msg.message.duration", FT_UINT16, BASE_CUSTOM, decode_zcl_msg_duration,
490 0x00, NULL, HFILL } },
492 { &hf_zbee_zcl_msg_message_length,
493 { "Message Length", "zbee_zcl_se.msg.message.length", FT_UINT8, BASE_DEC, NULL,
494 0x00, NULL, HFILL } },
496 { &hf_zbee_zcl_msg_message,
497 { "Message", "zbee_zcl_se.msg.message", FT_STRING, BASE_NONE, NULL,
498 0x00, NULL, HFILL } },
500 { &hf_zbee_zcl_msg_confirm_time,
501 { "Confirmation Time", "zbee_zcl_se.msg.message.confirm_time", FT_ABSOLUTE_TIME, ABSOLUTE_TIME_LOCAL, NULL,
506 /* ZCL Messaging subtrees */
507 gint *ett[ZBEE_ZCL_MSG_NUM_ETT];
509 ett[0] = &ett_zbee_zcl_msg;
510 ett[1] = &ett_zbee_zcl_msg_message_control;
512 /* Register the ZigBee ZCL Messaging cluster protocol name and description */
513 proto_zbee_zcl_msg = proto_register_protocol("ZigBee ZCL Messaging", "ZCL Messaging", ZBEE_PROTOABBREV_ZCL_MSG);
514 proto_register_field_array(proto_zbee_zcl_msg, hf, array_length(hf));
515 proto_register_subtree_array(ett, array_length(ett));
517 /* Register the ZigBee ZCL Messaging dissector. */
518 new_register_dissector(ZBEE_PROTOABBREV_ZCL_MSG, dissect_zbee_zcl_msg, proto_zbee_zcl_msg);
520 } /*proto_register_zbee_zcl_msg*/
523 /*FUNCTION:------------------------------------------------------
525 * proto_reg_handoff_zbee_zcl_msg
527 * Hands off the Zcl Messaging dissector.
532 *---------------------------------------------------------------
535 proto_reg_handoff_zbee_zcl_msg(void)
537 dissector_handle_t msg_handle;
539 /* Register our dissector with the ZigBee application dissectors. */
540 msg_handle = find_dissector(ZBEE_PROTOABBREV_ZCL_MSG);
541 dissector_add_uint("zbee.zcl.cluster", ZBEE_ZCL_CID_MESSAGE, msg_handle);
543 zbee_zcl_init_cluster( proto_zbee_zcl_msg,
545 ZBEE_ZCL_CID_MESSAGE,
548 (zbee_zcl_fn_cmd_id)dissect_zcl_msg_cmd_id
550 } /*proto_reg_handoff_zbee_zcl_msg*/