# OpenXC JSON Message Format Each JSON message published by a VI is delimited with a `\0 ` character. ## Table of Contents 1. [Vehicle Messages](#vehicle-messages) 2. [CAN Message](#can-message) 3. [Diagnostic Message](#diagnostic-message) 4. [Commands](#commands) 5. [Extra Values](#extra-values) ## Vehicle Messages ### Simple Vehicle Message There may not be a 1:1 relationship between input and output signals - i.e. engine timing CAN signals may be summarized in an "engine performance" metric on the abstract side of the interface. The expected format of a single valued message is: {"name": "steering_wheel_angle", "value": 45} ### Evented Simple Vehicle Message The expected format of an event message is: {"name": "button_event", "value": "up", "event": "pressed"} This format is good for something like a button event, where there are two discrete pieces of information in the measurement. ## CAN Message The format for a plain CAN message: {"bus": 1, "id": 1234, "data": "0x12345678"} **bus** - the numerical identifier of the CAN bus where this message originated, most likely 1 or 2 (for a vehicle interface with 2 CAN controllers). **id** - the CAN message ID **data** - up to 8 bytes of data from the CAN message's payload, represented as a hexidecimal number in a string. Many JSON parser cannot handle 64-bit integers, which is why we are not using a numerical data type. Each byte in the string *must* be represented with 2 characters, e.g. `0x1` is `0x01` - the complete string must have an even number of characters. The `0x` prefix is optional. **format** - (optional) explicitly set the frame format for the CAN message, one of `standard` or `extended`. If the `id` is greater than `0x7ff`, the extended frame format will be selected automatically. ## Diagnostic Message ### Requests A diagnostic request is added or cancelled with a JSON object like this example: { "command": "diagnostic_request", "action": "add", "diagnostic_request": { "bus": 1, "message_id": 1234, "mode": 1, "pid": 5, "payload": "0x1234", "multiple_responses": false, "frequency": 1, "name": "my_pid" } } } * The `command` must be `diagnostic_request.` * The `action` must be included, and must be one of: * `add` - create a new one-off or recurring diagnostic request. * `cancel` - cancel an existing request. * The details of the request must be included in the `request` field, using the sub-fields defined below. A diagnostic request's `bus`, `id`, `mode` and `pid` (or lack of a `pid`) combine to create a unique key to identify a request. These four fields will be referred to as the key of the diagnostic request. For example, to create a simple one-time diagnostic request: { "command": "diagnostic_request", "action": "add", "diagnostic_request": { "bus": 1, "message_id": 1234, "mode": 1, "pid": 5 } } } Requests are completed after any responses are received (unless `multiple_responses` is set), or the request has timed out after a certain number of seconds. After a request is completed, you can re-`create` the same key to make another request. Requests with a `frequency` are added as *recurring* requests, e.g. to add the previous example as a recurring request at 1Hz: { "command": "diagnostic_request", "action": "add", "diagnostic_request": { "bus": 1, "message_id": 1234, "mode": 1, "pid": 5, "frequency": 1 } } } To cancel a recurring request, send a `cancel` action with the same key, e.g.: { "command": "diagnostic_request", "action": "cancel", "diagnostic_request": { "bus": 1, "message_id": 1234, "mode": 1, "pid": 5 } } } Simultaneous recurring requests for the same key at different rates (e.g. 1Hz *and* 2Hz) is not supported. However, non-recurring ("one-off") requests may exist in parallel with a recurring request for the same key. **bus** - the numerical identifier of the CAN bus where this request should be sent, most likely 1 or 2 (for a vehicle interface with 2 CAN controllers). **message_id** - the CAN message ID for the request. **mode** - the OBD-II mode of the request - 0x1 through 0xff (1 through 9 are the standardized modes and 0x22 is a common proprietary mode). **pid** - (optional) the PID for the request, if applicable. **payload** - (optional) up to 7 bytes of data for the request's payload represented as a hexadecimal number in a string. Many JSON parser cannot handle 64-bit integers, which is why we are not using a numerical data type. Each byte in the string *must* be represented with 2 characters, e.g. `0x1` is `0x01` - the complete string must have an even number of characters. The `0x` prefix is optional. **name** - (optional, defaults to nothing) A human readable, string name for this request. If provided, the response will have a `name` field (much like a simple vehicle message) with this value in place of `bus`, `id`, `mode` and `pid`. **multiple_responses** - (optional, false by default) if true, request will stay active for a full 100ms, even after receiving a diagnostic response message. This is useful for requests to the functional broadcast message ID (`0x7df`) when you need to get responses from multiple modules. It's possible to set this to `true` for non-broadcast requests, but in practice you won't see any additional responses after the first and it will just take up memory in the VI for longer. **frequency** - (optional) Make this request a recurring request, at a this frequency in Hz. To send a single non-recurring request, leave this field out. **decoded_type** - (optional, defaults to "obd2" if the request is a recognized OBD-II mode 1 request, otherwise "none") If specified, the valid values are `"none"` and `"obd2
/*
 * Copyright (C) 2015, 2016 "IoT.bzh"
 * Author: José Bollo <jose.bollo@iot.bzh>
 *
 * Licensed under the A