Fix some warnings/errors of type
[metze/wireshark/wip.git] / epan / dissectors / packet-at.c
1 /* packet-at.c
2  * Dissector for AT Commands
3  *
4  * Copyright 2011, Tyson Key <tyson.key@gmail.com>
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  *
24  */
25
26 #include "config.h"
27
28 #include <epan/packet.h>
29
30 void proto_register_at_command(void);
31 void proto_reg_handoff_at_command(void);
32
33 static int proto_at = -1;
34 static int hf_at_command = -1;
35
36 /* Subtree handles: set by register_subtree_array */
37 static gint ett_at = -1;
38
39 static gboolean allowed_chars(tvbuff_t *tvb)
40 {
41     gint offset, len;
42     guint8 val;
43
44     len = tvb_reported_length(tvb);
45     for (offset = 0; offset < len; offset++) {
46         val = tvb_get_guint8(tvb, offset);
47         if (!(g_ascii_isprint(val) || (val == 0x0a) || (val == 0x0d)))
48             return (FALSE);
49     }
50     return (TRUE);
51 }
52
53 /* The dissector itself */
54 static int dissect_at(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
55 {
56     proto_item *item;
57     proto_tree *at_tree;
58     gint len;
59
60     len = tvb_reported_length(tvb);
61     col_append_sep_str(pinfo->cinfo, COL_PROTOCOL, "/", "AT");
62     col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL, "AT Command: %s",
63         tvb_format_text_wsp(tvb, 0, len));
64
65     if (tree) {
66         /* Start with a top-level item to add everything else to */
67         item = proto_tree_add_item(tree, proto_at, tvb, 0, -1, ENC_NA);
68         at_tree = proto_item_add_subtree(item, ett_at);
69
70         /* Command */
71         proto_tree_add_item(at_tree, hf_at_command, tvb, 0, len, ENC_ASCII|ENC_NA);
72         proto_item_append_text(item, ": %s", tvb_format_text_wsp(tvb, 0, len));
73     }
74     return tvb_captured_length(tvb);
75 }
76
77
78 /* Experimental approach based upon the one used for PPP */
79 static gboolean heur_dissect_at(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
80 {
81     const gchar at_magic1[2] = {0x0d, 0x0a};
82     const gchar at_magic2[3] = {0x0d, 0x0d, 0x0a};
83     const gchar at_magic3[2] = {'A', 'T'};
84
85     if (((tvb_memeql(tvb, 0, at_magic1, sizeof(at_magic1)) == 0) ||
86          (tvb_memeql(tvb, 0, at_magic2, sizeof(at_magic2)) == 0) ||
87          (tvb_memeql(tvb, 0, at_magic3, sizeof(at_magic3)) == 0)) &&
88          allowed_chars(tvb)) {
89         dissect_at(tvb, pinfo, tree, data);
90         return (TRUE);
91     }
92     return (FALSE);
93 }
94
95 void
96 proto_register_at_command(void)
97 {
98     static hf_register_info hf[] = {
99         { &hf_at_command,
100             { "AT Command", "at.command", FT_STRING, BASE_NONE,
101               NULL, 0x0, NULL, HFILL }}
102     };
103
104     static gint *ett[] = {
105         &ett_at
106     };
107
108     proto_at = proto_register_protocol("AT Command", "AT", "at");
109     proto_register_field_array(proto_at, hf, array_length(hf));
110     proto_register_subtree_array(ett, array_length(ett));
111     register_dissector("at", dissect_at, proto_at);
112 }
113
114 /* Handler registration */
115 void
116 proto_reg_handoff_at_command(void)
117 {
118     heur_dissector_add("usb.bulk", heur_dissect_at, "AT Command USB bulk endpoint", "at_usb_bulk", proto_at, HEURISTIC_ENABLE);
119     heur_dissector_add("usb.control", heur_dissect_at, "AT Command USB control endpoint", "at_usb_control", proto_at, HEURISTIC_ENABLE);
120 }
121
122 /*
123  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
124  *
125  * Local variables:
126  * c-basic-offset: 4
127  * tab-width: 8
128  * indent-tabs-mode: nil
129  * End:
130  *
131  * vi: set shiftwidth=4 tabstop=8 expandtab:
132  * :indentSize=4:tabSize=8:noTabs=true:
133  */