ca4013635b8ae3cc74e66ead9e220ce995658176
[apps/low-level-can-service.git] / README.md
1 # OpenXC Message Format Specification
2
3 Version: v0.4-dev
4
5 This specification is a part of the [OpenXC platform][OpenXC].
6
7 An OpenXC vehicle interface sends generic vehicle data over one or more output
8 interfaces (e.g. USB or Bluetooth) as JSON or Protocol Buffers (protobuf).
9
10 ## Binary (Protocol Buffers)
11
12 The binary format is encoded using [Google Protocol
13 Buffers](https://code.google.com/p/protobuf/). The format is specified in the
14 file `openxc.proto`. Those are published using the standard length-delimited
15 method (any protobuf library should support this).
16
17 The binary format is best if you need to maximize the amount of data that can be
18 sent from the VI, trading off flexibility for efficiency.
19
20 ## JSON
21
22 This document describes the JSON format and includes a high level description of
23 each type and field. Each JSON message published by a VI is delimited with a
24 `\0 ` character.
25
26 The JSON format is best for most developers, as it is fairly efficient and very
27 flexible.
28
29 ### Extra Values
30
31 Any of the following JSON objects may optionally include an `extras`
32 field. The value may be any valid JSON object or array. The client libraries
33 will do their best to parse this information into a generic format and pass it
34 to your application. For example:
35
36     {"name": "steering_wheel_angle",
37         "value": 45,
38         "extras": {
39             "calibrated": false
40         }
41     }
42
43 ### Single Valued
44
45 There may not be a 1:1 relationship between input and output signals - i.e. raw
46 engine timing CAN signals may be summarized in an "engine performance" metric on
47 the abstract side of the interface.
48
49 The expected format of a single valued message is:
50
51     {"name": "steering_wheel_angle", "value": 45}
52
53 ### Evented
54
55 The expected format of an event message is:
56
57     {"name": "button_event", "value": "up", "event": "pressed"}
58
59 This format is good for something like a button event, where there are two
60 discrete pieces of information in the measurement.
61
62 ### Raw CAN Message format
63
64 The format for a raw CAN message:
65
66     {"bus": 1, "id": 1234, "data": "0x12345678"}
67
68 **bus** - the numerical identifier of the CAN bus where this message originated,
69   most likely 1 or 2 (for a vehicle interface with 2 CAN controllers).
70
71 **id** - the CAN message ID
72
73 **data** - up to 8 bytes of data from the CAN message's payload, represented as
74   a hexidecimal number in a string. Many JSON parser cannot handle 64-bit
75   integers, which is why we are not using a numerical data type. Each byte in
76   the string *must* be represented with 2 characters, e.g. `0x1` is `0x01` - the
77   complete string must have an even number of characters.
78
79 ### Diagnostic Messages
80
81 #### Requests
82
83 A diagnostic request is created, update and deleted with a JSON object like this
84 example:
85
86     { "command": "diagnostic_request",
87       "action": "create",
88       "request": {
89           "bus": 1,
90           "id": 1234,
91           "mode": 1,
92           "pid": 5,
93           "payload": "0x1234",
94           "multiple_responses": false,
95           "frequency": 1,
96           "name": "my_pid"
97         }
98       }
99     }
100
101 * The `command` must be `diagnostic_request.`
102 * The `action` must be included, and must be one of:
103     * `create` - create a new one-off or recurring diagnostic request.
104     * `update` - update an existing request.
105     * `delete` - delete an existing request.
106 * The details of the request must be included in the `request` field, using
107   the sub-fields defined below.
108
109 A diagnostic request's `bus`, `id`, `mode` and `pid` (or lack of a `pid`)
110 combine to create a unique key to identify a request. These four fields will be
111 referred to as the key of the diagnostic request. For example, to create a
112 simple one-time diagnostic request:
113
114     { "command": "diagnostic_request",
115       "action": "create",
116       "request": {
117           "bus": 1,
118           "id": 1234,
119           "mode": 1,
120           "pid": 5
121         }
122       }
123     }
124
125 Requests are completed after any responses are received (unless
126 `multiple_responses` is set), or the request has timed out after a certain
127 number of seconds. After a request is completed, you can re-`create` the same
128 key to make another request.
129
130 Requests with a `frequency` are added as *recurring* requests, e.g. to add the
131 previous example as a recurring request at 1Hz:
132
133     { "command": "diagnostic_request",
134       "action": "create",
135       "request": {
136           "bus": 1,
137           "id": 1234,
138           "mode": 1,
139           "pid": 5,
140           "frequency": 1
141         }
142       }
143     }
144
145 To cancel a recurring request, send a `cancel` action with the same key, e.g.:
146
147     { "command": "diagnostic_request",
148       "action": "delete",
149       "request": {
150           "bus": 1,
151           "id": 1234,
152           "mode": 1,
153           "pid": 5
154         }
155       }
156     }
157
158 To update one of the fields of a recurring request, send an `update` action with
159 the same key, plus the field to update. For example, to change the frequency of
160 the example request to 2Hz:
161
162     { "command": "diagnostic_request",
163       "action": "update",
164       "request": {
165           "bus": 1,
166           "id": 1234,
167           "mode": 1,
168           "pid": 5,
169           "frequency": 2
170         }
171       }
172     }
173
174 Simultaneous recurring requests for the same key at different rates (e.g. 1Hz
175 *and* 2Hz) is not supported. However, non-recurring ("one-off") requests may
176 exist in parallel with a recurring request for the same key.
177
178 **bus** - the numerical identifier of the CAN bus where this request should be
179     sent, most likely 1 or 2 (for a vehicle interface with 2 CAN controllers).
180
181 **id** - the CAN arbitration ID for the request.
182
183 **mode** - the OBD-II mode of the request - 1 through 255 (1 through 9 are the
184     standardized modes and 0x22 is a common proprietary mode).
185
186 **pid** - (optional) the PID for the request, if applicable.
187
188 **payload** - (optional) up to 7 bytes of data for the request's payload
189     represented as a hexadecimal number in a string. Many JSON parser cannot
190     handle 64-bit integers, which is why we are not using a numerical data type.
191     Each byte in the string *must* be represented with 2 characters, e.g. `0x1`
192     is `0x01` - the complete string must have an even number of characters.
193
194 **name** - (optional, defaults to nothing) A human readable, string name for
195   this request. If provided, the response will have a `name` field (much like a
196   normal translated message) with this value in place of `bus`, `id`, `mode` and
197   `pid`.
198
199 **multiple_responses** - (optional, false by default) if true, request will stay
200   active for a full 100ms, even after receiving a diagnostic response message.
201   This is useful for requests to the functional broadcast arbitration ID
202   (`0x7df`) when you need to get responses from multiple modules. It's possible
203   to set this to `true` for non-broadcast requests, but in practice you won't
204   see any additional responses after the first and it will just take up memory
205   in the VI for longer.
206
207 **frequency** - (optional) Make this request a recurring request, at a this
208   frequency in Hz. To send a single non-recurring request, leave this field out.
209
210 **decoded_type** - (optional, defaults to "obd2" if the request is a recognized
211 OBD-II mode 1 request, otherwise "none") If specified, the valid values are
212 `"none"` and `"obd2"`. If `obd2`, the payload will be decoded according to the
213 OBD-II specification and returned in the `value` field. Set this to `none` to
214 manually override the OBD-II decoding feature for a known PID.
215
216 #### Responses
217
218 The response to a successful request:
219
220     {"bus": 1,
221       "id": 1234,
222       "mode": 1,
223       "pid": 5,
224       "success": true,
225       "payload": "0x1234",
226       "value": 4660}
227
228 and to an unsuccessful request, with the `negative_response_code` and no `pid`
229 echo:
230
231     {"bus": 1,
232       "id": 1234,
233       "mode": 1,
234       "success": false,
235       "negative_response_code": 17}
236
237 **bus** - the numerical identifier of the CAN bus where this response was
238     received.
239
240 **id** - the CAN arbitration ID for this response.
241
242 **mode** - the OBD-II mode of the original diagnostic request.
243
244 **pid** - (optional) the PID for the request, if applicable.
245
246 **success** -  true if the response received was a positive response. If this
247   field is false, the remote node returned an error and the
248   `negative_response_code` field should be populated.
249
250 **negative_response_code** - (optional)  If requested node returned an error,
251     `success` will be `false` and this field will contain the negative response
252     code (NRC).
253
254 Finally, the `payload` and `value` fields are mutually exclusive:
255
256 **payload** - (optional) up to 7 bytes of data returned in the response,
257     represented as a hexadecimal number in a string. Many JSON parser cannot
258     handle 64-bit integers, which is why we are not using a numerical data type.
259
260 **value** - (optional) if the response had a payload, this may be the
261     payload interpreted as an integer.
262
263 The response to a simple PID request would look like this:
264
265     {"success": true, "bus": 1, "id": 1234, "mode": 1, "pid": 5, "payload": "0x2"}
266
267 ### Commands
268
269 In addition to the `diagnostic_request` command described earlier, there are
270 other possible values for the `command` field.
271
272 #### Version Query
273
274 The `version` command triggers the VI to inject a firmware version identifier
275 response into the outgoing data stream.
276
277 **Request**
278
279     { "command": "version"}
280
281 **Response**
282
283     { "command_response": "version", "message": "v6.0-dev (default)"}
284
285 #### Device ID Query
286
287 The `device_id` command triggers the VI to inject a unique device ID (e.g. the
288 MAC address of an included Bluetooth module) into into the outgoing data stream.
289
290 **Request**
291
292     { "command": "device_id"}
293
294 **Response**
295
296     { "command_response": "device_id", "message": "0012345678"}
297
298 ### Trace File Format
299
300 An OpenXC vehicle trace file is a plaintext file that contains JSON objects,
301 separated by newlines (which may be either `\r\n` or `\n`, depending on the
302 platform the trace file was recorded).
303
304 The first line may be a metadata object, although this is optional:
305
306 ```
307 {"metadata": {
308     "version": "v3.0",
309     "vehicle_interface_id": "7ABF",
310     "vehicle": {
311         "make": "Ford",
312         "model": "Mustang",
313         "trim": "V6 Premium",
314         "year": 2013
315     },
316     "description": "highway drive to work",
317     "driver_name": "TJ Giuli",
318     "vehicle_id": "17N1039247929"
319 }
320 ```
321
322 The following lines are OpenXC messages with a `timestamp` field added, e.g.:
323
324     {"timestamp": 1385133351.285525, "name": "steering_wheel_angle", "value": 45}
325
326 The timestamp is in [UNIX time](http://en.wikipedia.org/wiki/Unix_time)
327 (i.e. seconds since the UNIX epoch, 00:00:00 UTC, 1/1/1970).
328
329 ## Official Signals
330
331 These signal names are a part of the OpenXC specification, although some
332 manufacturers may support custom message names.
333
334 * steering_wheel_angle
335     * numerical, -600 to +600 degrees
336     * 10Hz
337 * torque_at_transmission
338     * numerical, -500 to 1500 Nm
339     * 10Hz
340 * engine_speed
341     * numerical, 0 to 16382 RPM
342     * 10Hz
343 * vehicle_speed
344     * numerical, 0 to 655 km/h (this will be positive even if going in reverse
345       as it's not a velocity, although you can use the gear status to figure out
346       direction)
347     * 10Hz
348 * accelerator_pedal_position
349     * percentage
350     * 10Hz
351 * parking_brake_status
352     * boolean, (true == brake engaged)
353     * 1Hz, but sent immediately on change
354 * brake_pedal_status
355     * boolean (True == pedal pressed)
356     * 1Hz, but sent immediately on change
357 * transmission_gear_position
358     * states: first, second, third, fourth, fifth, sixth, seventh, eighth,
359       ninth, tenth, reverse, neutral
360     * 1Hz, but sent immediately on change
361 * gear_lever_position
362     * states: neutral, park, reverse, drive, sport, low, first, second, third,
363       fourth, fifth, sixth, seventh, eighth, ninth, tenth
364     * 1Hz, but sent immediately on change
365 * odometer
366     * Numerical, km
367         0 to 16777214.000 km, with about .2m resolution
368     * 10Hz
369 * ignition_status
370     * states: off, accessory, run, start
371     * 1Hz, but sent immediately on change
372 * fuel_level
373     * percentage
374     * 2Hz
375 * fuel_consumed_since_restart
376     * numerical, 0 - 4294967295.0 L (this goes to 0 every time the vehicle
377       restarts, like a trip meter)
378     * 10Hz
379 * door_status
380     * Value is State: driver, passenger, rear_left, rear_right.
381     * Event is boolean: true == ajar
382     * 1Hz, but sent immediately on change
383 * headlamp_status
384     * boolean, true is on
385     * 1Hz, but sent immediately on change
386 * high_beam_status
387     * boolean, true is on
388     * 1Hz, but sent immediately on change
389 * windshield_wiper_status
390     * boolean, true is on
391     * 1Hz, but sent immediately on change
392 * latitude
393     * numerical, -89.0 to 89.0 degrees with standard GPS accuracy
394     * 1Hz
395 * longitude
396     * numerical, -179.0 to 179.0 degrees with standard GPS accuracy
397     * 1Hz
398
399 ### Signals from Diagnostics Messages
400
401 This set of signals is often retreived from OBD-II requests. The units can be
402 found in the [OBD-II standard](http://en.wikipedia.org/wiki/OBD-II_PIDs#Mode_01).
403
404 * engine_load
405 * engine_coolant_temperature
406 * barometric_pressure
407 * commanded_throttle_position
408 * throttle_position
409 * fuel_level
410 * intake_air_temperature
411 * intake_manifold_pressure
412 * running_time
413 * fuel_pressure
414 * mass_airflow
415 * accelerator_pedal_position
416 * ethanol_fuel_percentage
417 * engine_oil_temperature
418 * engine_torque
419
420 License
421 =======
422
423 Copyright (c) 2012-2014 Ford Motor Company
424
425 Licensed under the BSD license.
426
427 [OpenXC]: http://openxcplatform.com