FIXUP: WIP: verification_trailer
[wireshark-wip.git] / epan / dissectors / packet-at.c
blob06b6849485c524c11cda84de2a154f3c1adcbd4d
1 /* packet-at.c
2 * Dissector for AT Commands
4 * Copyright 2011, Tyson Key <tyson.key@gmail.com>
6 * $Id$
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "config.h"
30 #include <glib.h>
31 #include <epan/packet.h>
32 #include <ctype.h>
34 void proto_register_at_command(void);
35 void proto_reg_handoff_at_command(void);
37 static int proto_at = -1;
38 static int hf_at_command = -1;
40 /* Forward-declare the dissector functions */
41 static void dissect_at(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
43 /* Subtree handles: set by register_subtree_array */
44 static gint ett_at = -1;
46 static gboolean allowed_chars(tvbuff_t *tvb)
48 gint offset, len;
49 guint8 val;
51 len = tvb_length(tvb);
52 for (offset = 0; offset < len; offset++) {
53 val = tvb_get_guint8(tvb, offset);
54 if (!(isprint(val) || (val == 0x0a) || (val == 0x0d)))
55 return (FALSE);
57 return (TRUE);
60 /* Experimental approach based upon the one used for PPP */
61 static gboolean heur_dissect_at(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
63 const gchar at_magic1[2] = {0x0d, 0x0a};
64 const gchar at_magic2[3] = {0x0d, 0x0d, 0x0a};
65 const gchar at_magic3[2] = {'A', 'T'};
67 if (((tvb_memeql(tvb, 0, at_magic1, sizeof(at_magic1)) == 0) ||
68 (tvb_memeql(tvb, 0, at_magic2, sizeof(at_magic2)) == 0) ||
69 (tvb_memeql(tvb, 0, at_magic3, sizeof(at_magic3)) == 0)) &&
70 allowed_chars(tvb)) {
71 dissect_at(tvb, pinfo, tree);
72 return (TRUE);
74 return (FALSE);
77 /* The dissector itself */
78 static void dissect_at(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
80 proto_item *item;
81 proto_tree *at_tree;
82 gint len;
84 len = tvb_reported_length(tvb);
85 col_append_str(pinfo->cinfo, COL_PROTOCOL, "/AT");
86 col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL, "AT Command: %s",
87 tvb_format_text_wsp(tvb, 0, len));
89 if (tree) {
90 /* Start with a top-level item to add everything else to */
91 item = proto_tree_add_item(tree, proto_at, tvb, 0, -1, ENC_NA);
92 at_tree = proto_item_add_subtree(item, ett_at);
94 /* Command */
95 proto_tree_add_item(at_tree, hf_at_command, tvb, 0, len, ENC_ASCII|ENC_NA);
96 proto_item_append_text(item, ": %s", tvb_format_text_wsp(tvb, 0, len));
100 void
101 proto_register_at_command(void)
103 static hf_register_info hf[] = {
104 { &hf_at_command,
105 { "AT Command", "at.command", FT_STRING, BASE_NONE,
106 NULL, 0x0, NULL, HFILL }}
109 static gint *ett[] = {
110 &ett_at
113 proto_at = proto_register_protocol("AT Command", "AT", "at");
114 proto_register_field_array(proto_at, hf, array_length(hf));
115 proto_register_subtree_array(ett, array_length(ett));
116 register_dissector("at", dissect_at, proto_at);
119 /* Handler registration */
120 void
121 proto_reg_handoff_at_command(void)
123 heur_dissector_add("usb.bulk", heur_dissect_at, proto_at);
124 heur_dissector_add("usb.control", heur_dissect_at, proto_at);