Remove all $Id$ from top of file
[metze/wireshark/wip.git] / epan / dissectors / packet-j1939.c
1 /* packet-j1939.c
2  * Routines for dissection of SAE J1939
3  *
4  * Michael Mann
5  * Copyright 2013
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 #include "config.h"
26
27 #include <glib.h>
28 #include <epan/packet.h>
29
30 void proto_register_j1939(void);
31
32 #define J1939_CANID_MASK        0x1FFFFFFF
33 #define J1939_11BIT_ID          0x000003FF
34
35 static int proto_j1939 = -1;
36
37 static int hf_j1939_can_id = -1;
38 static int hf_j1939_priority = -1;
39 static int hf_j1939_pgn = -1;
40 static int hf_j1939_data_page = -1;
41 static int hf_j1939_extended_data_page = -1;
42 static int hf_j1939_pdu_format = -1;
43 static int hf_j1939_pdu_specific = -1;
44 static int hf_j1939_src_addr = -1;
45 static int hf_j1939_dst_addr = -1;
46 static int hf_j1939_group_extension = -1;
47 static int hf_j1939_data = -1;
48
49 static gint ett_j1939 = -1;
50 static gint ett_j1939_can = -1;
51 static gint ett_j1939_message = -1;
52
53 static dissector_table_t   subdissector_pgn_table;
54
55 static const value_string j1939_address_vals[] = {
56     {0,"Engine #1"},
57     {1,"Engine #2"},
58     {2,"Turbocharger"},
59     {3,"Transmission #1"},
60     {4,"Transmission #2"},
61     {5,"Shift Console - Primary"},
62     {6,"Shift Console - Secondary"},
63     {7,"Power TakeOff - (Main or Rear)"},
64     {8,"Axle - Steering"},
65     {9,"Axle - Drive #1"},
66     {10,"Axle - Drive #2"},
67     {11,"Brakes - System Controller"},
68     {12,"Brakes - Steer Axle"},
69     {13,"Brakes - Drive axle #1"},
70     {14,"Brakes - Drive Axle #2"},
71     {15,"Retarder - Engine"},
72     {16,"Retarder - Driveline"},
73     {17,"Cruise Control"},
74     {18,"Fuel System"},
75     {19,"Steering Controller"},
76     {20,"Suspension - Steer Axle"},
77     {21,"Suspension - Drive Axle #1"},
78     {22,"Suspension - Drive Axle #2"},
79     {23,"Instrument Cluster #1"},
80     {24,"Trip Recorder"},
81     {25,"Passenger-Operator Climate Control #1"},
82     {26,"Alternator/Electrical Charging System"},
83     {27,"Aerodynamic Control"},
84     {28,"Vehicle Navigation"},
85     {29,"Vehicle Security"},
86     {30,"Electrical System"},
87     {31,"Starter System"},
88     {32,"Tractor-Trailer Bridge #1"},
89     {33,"Body Controller"},
90     {34,"Auxiliary Valve Control or Engine Air System Valve Control"},
91     {35,"Hitch Control"},
92     {36,"Power TakeOff (Front or Secondary)"},
93     {37,"Off Vehicle Gateway"},
94     {38,"Virtual Terminal (in cab)"},
95     {39,"Management Computer #1"},
96     {40,"Cab Display #1"},
97     {41,"Retarder, Exhaust, Engine #1"},
98     {42,"Headway Controller"},
99     {43,"On-Board Diagnostic Unit"},
100     {44,"Retarder, Exhaust, Engine #2"},
101     {45,"Endurance Braking System"},
102     {46,"Hydraulic Pump Controller"},
103     {47,"Suspension - System Controller #1"},
104     {48,"Pneumatic - System Controller"},
105     {49,"Cab Controller - Primary"},
106     {50,"Cab Controller - Secondary"},
107     {51,"Tire Pressure Controller"},
108     {52,"Ignition Control Module #1"},
109     {53,"Ignition Control Module #2"},
110     {54,"Seat Control #1"},
111     {55,"Lighting - Operator Controls"},
112     {56,"Rear Axle Steering Controller #1"},
113     {57,"Water Pump Controller"},
114     {58,"Passenger-Operator Climate Control #2"},
115     {59,"Transmission Display - Primary"},
116     {60,"Transmission Display - Secondary"},
117     {61,"Exhaust Emission Controller"},
118     {62,"Vehicle Dynamic Stability Controller"},
119     {63,"Oil Sensor"},
120     {64,"Suspension - System Controller #2"},
121     {65,"Information System Controller #1"},
122     {66,"Ramp Control"},
123     {67,"Clutch/Converter Unit"},
124     {68,"Auxiliary Heater #1"},
125     {69,"Auxiliary Heater #2"},
126     {70,"Engine Valve Controller"},
127     {71,"Chassis Controller #1"},
128     {72,"Chassis Controller #2"},
129     {73,"Propulsion Battery Charger"},
130     {74,"Communications Unit, Cellular"},
131     {75,"Communications Unit, Satellite"},
132     {76,"Communications Unit, Radio"},
133     {77,"Steering Column Unit"},
134     {78,"Fan Drive Controller"},
135     {79,"Seat Control #2"},
136     {80,"Parking brake controller"},
137     {81,"Aftertreatment #1 system gas intake"},
138     {82,"Aftertreatment #1 system gas outlet"},
139     {83,"Safety Restraint System"},
140     {84,"Cab Display #2"},
141     {85,"Diesel Particulate Filter Controller"},
142     {86,"Aftertreatment #2 system gas intake"},
143     {87,"Aftertreatment #2 system gas outlet"},
144     {88,"Safety Restraint System #2"},
145     {89,"Atmospheric Sensor"},
146     {248,"File Server / Printer"},
147     {249,"Off Board Diagnostic-Service Tool #1"},
148     {250,"Off Board Diagnostic-Service Tool #2"},
149     {251,"On-Board Data Logger"},
150     {252,"Reserved for Experimental Use"},
151     {253,"Reserved for OEM"},
152     {254,"Null Address"},
153     {255,"GLOBAL"},
154     { 0, NULL }
155 };
156
157 value_string_ext j1939_address_vals_ext = VALUE_STRING_EXT_INIT(j1939_address_vals);
158
159 static void
160 j1939_fmt_address(gchar *result, guint32 addr )
161 {
162     if ((addr < 128) || (addr > 247))
163         g_snprintf(result, ITEM_LABEL_LENGTH, "%d (%s)", addr, val_to_str_ext_const(addr, &j1939_address_vals_ext, "Reserved"));
164     else
165         g_snprintf(result, ITEM_LABEL_LENGTH, "%d (Arbitrary)", addr);
166 }
167
168 struct can_identifier
169 {
170     guint32 id;
171 };
172
173 static int dissect_j1939(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data)
174 {
175     proto_item *ti, *can_id_item;
176     proto_tree *j1939_tree, *can_tree, *msg_tree;
177
178     gint offset = 0;
179     struct can_identifier can_id;
180     guint32 data_length = tvb_reported_length(tvb);
181     guint32 pgn;
182     guint8 *src_addr, *dest_addr;
183
184     DISSECTOR_ASSERT(data);
185     can_id = *((struct can_identifier*)data);
186
187     if (can_id.id & (~J1939_CANID_MASK))
188     {
189         /* Not for us */
190         return 0;
191     }
192
193     col_set_str(pinfo->cinfo, COL_PROTOCOL, "J1939");
194     col_clear(pinfo->cinfo, COL_INFO);
195
196     ti = proto_tree_add_item(tree, proto_j1939, tvb, offset, -1, ENC_NA);
197     j1939_tree = proto_item_add_subtree(ti, ett_j1939);
198
199     ti = proto_tree_add_text(j1939_tree, tvb, 0, 0, "CAN Identifier: 0x%08x", can_id.id);
200     can_tree = proto_item_add_subtree(ti, ett_j1939_can);
201     can_id_item = proto_tree_add_uint(can_tree, hf_j1939_can_id, tvb, 0, 0, can_id.id);
202     PROTO_ITEM_SET_GENERATED(can_id_item);
203     ti = proto_tree_add_uint(can_tree, hf_j1939_priority, tvb, 0, 0, can_id.id);
204     PROTO_ITEM_SET_GENERATED(ti);
205     ti = proto_tree_add_uint(can_tree, hf_j1939_extended_data_page, tvb, 0, 0, can_id.id);
206     PROTO_ITEM_SET_GENERATED(ti);
207     ti = proto_tree_add_uint(can_tree, hf_j1939_data_page, tvb, 0, 0, can_id.id);
208     PROTO_ITEM_SET_GENERATED(ti);
209     ti = proto_tree_add_uint(can_tree, hf_j1939_pdu_format, tvb, 0, 0, can_id.id);
210     PROTO_ITEM_SET_GENERATED(ti);
211     ti = proto_tree_add_uint(can_tree, hf_j1939_pdu_specific, tvb, 0, 0, can_id.id);
212     PROTO_ITEM_SET_GENERATED(ti);
213     ti = proto_tree_add_uint(can_tree, hf_j1939_src_addr, tvb, 0, 0, can_id.id);
214     PROTO_ITEM_SET_GENERATED(ti);
215
216     /* Set source address */
217     src_addr = (guint8*)wmem_alloc(pinfo->pool, 1);
218     *src_addr = (guint8)(can_id.id & 0xFF);
219     SET_ADDRESS(&pinfo->src, AT_J1939, 1, (const void*)src_addr);
220
221     pgn = (can_id.id & 0x3FFFF00) >> 8;
222
223     /* If PF < 240, PS is destination address, last byte of PGN is cleared */
224     if (((can_id.id & 0xFF00) >> 8) < 240)
225     {
226         pgn &= 0x3FF00;
227
228         ti = proto_tree_add_uint(can_tree, hf_j1939_dst_addr, tvb, 0, 0, can_id.id);
229         PROTO_ITEM_SET_GENERATED(ti);
230     }
231     else
232     {
233         ti = proto_tree_add_uint(can_tree, hf_j1939_group_extension, tvb, 0, 0, can_id.id);
234         PROTO_ITEM_SET_GENERATED(ti);
235     }
236
237     /* Fill in "destination" address even if its "broadcast" */
238     dest_addr = (guint8*)wmem_alloc(pinfo->pool, 1);
239     *dest_addr = (guint8)((can_id.id & 0xFF00) >> 8);
240     SET_ADDRESS(&pinfo->dst, AT_J1939, 1, (const void*)dest_addr);
241
242     col_add_fstr(pinfo->cinfo, COL_INFO, "PGN: %d", pgn);
243
244     /* For now just include raw bytes */
245     col_append_fstr(pinfo->cinfo, COL_INFO, "   %s", tvb_bytes_to_ep_str_punct(tvb, 0, data_length, ' '));
246
247     ti = proto_tree_add_text(j1939_tree, tvb, 0, -1, "Message");
248     msg_tree = proto_item_add_subtree(ti, ett_j1939_message);
249
250     ti = proto_tree_add_uint(msg_tree, hf_j1939_pgn, tvb, 0, 0, pgn);
251     PROTO_ITEM_SET_GENERATED(ti);
252
253     if (!dissector_try_uint_new(subdissector_pgn_table, pgn, tvb, pinfo, msg_tree, TRUE, data))
254     {
255         proto_tree_add_item(msg_tree, hf_j1939_data, tvb, 0, -1, ENC_NA);
256     }
257
258     return tvb_length(tvb);
259 }
260
261 void proto_register_j1939(void)
262 {
263     static hf_register_info hf[] = {
264         { &hf_j1939_can_id,
265             {"CAN Identifier", "j1939.can_id",
266             FT_UINT32, BASE_HEX, NULL, J1939_CANID_MASK, NULL, HFILL }
267         },
268         { &hf_j1939_priority,
269             {"Priority", "j1939.priority",
270             FT_UINT32, BASE_DEC, NULL, 0x1C000000, NULL, HFILL }
271         },
272         { &hf_j1939_pgn,
273             {"PGN", "j1939.pgn",
274             FT_UINT32, BASE_DEC, NULL, 0x3FFFFFF, NULL, HFILL }
275         },
276         { &hf_j1939_extended_data_page,
277             {"Extended Data Page", "j1939.ex_data_page",
278             FT_UINT32, BASE_DEC, NULL, 0x02000000, NULL, HFILL }
279         },
280         { &hf_j1939_data_page,
281             {"Data Page", "j1939.data_page",
282             FT_UINT32, BASE_DEC, NULL, 0x01000000, NULL, HFILL }
283         },
284         { &hf_j1939_pdu_format,
285             {"PDU Format", "j1939.pdu_format",
286             FT_UINT32, BASE_DEC, NULL, 0x00FF0000, NULL, HFILL }
287         },
288         { &hf_j1939_pdu_specific,
289             {"PDU Specific", "j1939.pdu_specific",
290             FT_UINT32, BASE_DEC, NULL, 0x0000FF00, NULL, HFILL }
291         },
292         { &hf_j1939_src_addr,
293             {"Source Address", "j1939.src_addr",
294             FT_UINT32, BASE_CUSTOM, j1939_fmt_address, 0x000000FF, NULL, HFILL }
295         },
296         { &hf_j1939_dst_addr,
297             {"Destination Address", "j1939.dst_addr",
298             FT_UINT32, BASE_CUSTOM, j1939_fmt_address, 0x0000FF00, NULL, HFILL }
299         },
300         { &hf_j1939_group_extension,
301             {"Group Extension", "j1939.group_extension",
302             FT_UINT32, BASE_DEC, NULL, 0x0000FF00, NULL, HFILL }
303         },
304         { &hf_j1939_data,
305             {"Data", "j1939.data",
306             FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }
307         },
308     };
309
310     static gint *ett[] = {
311         &ett_j1939,
312         &ett_j1939_can,
313         &ett_j1939_message
314     };
315
316     proto_j1939 = proto_register_protocol("SAE J1939", "J1939", "j1939");
317
318     proto_register_field_array(proto_j1939, hf, array_length(hf));
319     proto_register_subtree_array(ett, array_length(ett));
320
321     new_register_dissector("j1939", dissect_j1939, proto_j1939);
322
323     subdissector_pgn_table = register_dissector_table("j1939.pgn", "PGN Handle", FT_UINT32, BASE_DEC);
324 }
325
326 /*
327  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
328  *
329  * Local variables:
330  * c-basic-offset: 4
331  * tab-width: 8
332  * indent-tabs-mode: nil
333  * End:
334  *
335  * vi: set shiftwidth=4 tabstop=8 expandtab:
336  * :indentSize=4:tabSize=8:noTabs=true:
337  */