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