CAN Bus Protocol Adapter
This section presents the capabilities and configuration options of the CAN Bus Protocol Adapter, which translates the CAN Bus communication protocol into objects understandable by the Gateway Agent.
For a step-by-step guide on how to install the CAN Bus Protocol Adapter, see the Installation Guide.
Configuring the CAN Bus Protocol Adapter
There are two configuration files of the CAN Bus Protocol Adapter:
Deployment configuration file - Configuration of the MQTT connection, PMQ connection and health monitoring.
Application configuration file - Configuration of connections, global adapter properties and device data (measurements, alerts, configuration parameters and metadata).
When you modify the CAN Bus Protocol Adapter configuration, restart the adapter to apply the changes by means of the following command:
systemctl restart gwa-canbus-adapter-c
The adapter configuration is then updated.
For an example of the CAN Bus Protocol Adapter configuration files, see here.
Deployment Configuration File
The deployment configuration file of the CAN Bus Protocol Adapter is located at:
/etc/relayr/gwa-canbus-adapter-c/gwa-canbus-adapter-config.json
To modify the file, edit it in a text editor.
The following configuration settings are available:
transport
- Communication channel used by the adapter. Available options:mqtt
orpmq
. By default,mqtt
.mqtt_connection
- Connection settings of the MQTT message broker:
Configuration Setting | Description | Default Value |
---|---|---|
host | IP address of the MQTT message broker. | localhost |
port | Port number for connecting to the MQTT broker. | 1883 |
keepalive | Keepalive value of the MQTT connection. | 20 |
max_inflight_messages | Maximum number of QoS 1 or 2 messages that can be in the process of being transmitted simultaneously. This includes messages currently going through handshakes and messages that are being retried. If set to 1 , it guarantees an in-order delivery of messages. | 100 |
qos_levels | Quality of Service level for each traffic type. | measurements: 0 , alerts: 1 . configuration: 1 , metadata: 1 |
health_monitor
- Health monitoring settings:
Configuration Setting | Description | Default Value |
---|---|---|
heartbeat_interval | Time interval at which the periodic heartbeat messages are sent, given in seconds. | 60 |
pmq_connection
- Configuration of the High Speed Bus (PMQ) connection. Configure this section to use thepmq
transport
:
Configuration Setting | Description | Default Value |
---|---|---|
output_queues | List of output PMQ queues, where the adapter reports all data, except for health monitoring messages, which are always reported via MQTT. Queue names must begin with / . | ["/canbus_out_queue"] |
input_queues | List of PMQ queues to monitor for inbound messages, e.g. configuration update tasks. Queue names must begin with / . | ["/canbus_in_queue"] |
message_size | Maximum size of a PMQ message, given in bytes. | 8192 |
message_count | Maximum number of messages in a PMQ queue. | 10 |
Application Configuration File
The application configuration file of the CAN Bus Protocol Adapter is located at:
/etc/relayr/gwa-canbus-adapter-c/gwa-canbus-adapter-application-config.json
If you have the Configuration Manager installed, it becomes the primary way of editing application configuration files. If you do not have the Configuration Manager, edit the application configuration file in a text editor.
The file consists of the following sections:
Connections - List of connections to devices.
Global Adapter Properties – Configuration of a global sampling interval and a custom prefix.
Devices - List of connected devices and their measurements, alerts, configuration parameters and metadata.
For an example of the application configuration file, see here.
Configuring CAN Bus Connections
The connections
section of the configuration file allows you to define a list of device connections. For each configured connection, provide the following information:
Configuration Setting | Description |
---|---|
id | Unique connection identifier, which is then used to identify a device connection. See the Configuring a Device Connection section for more information. |
ifname | Name of a network interface used to receive/send data. |
read_timeout | Time limit, given in milliseconds, defining how long the adapter should wait for incoming data in its every cycle. This global setting may be overridden by the object configuration. |
Here is an example of the connections
configuration:
"connections": [
{
"id": "0",
"ifname": "can0",
"read_timeout": 50
}
],
Global Adapter Properties
You can configure the following global settings that refer to the whole Protocol Adapter configuration:
Configuring Sampling Interval
The sampling interval determines how often the Agent gathers data from devices. It is a global setting that refers to retrieving data from all the connected devices.
The interval value is defined in milliseconds:
"sampling_interval" : 4000
Optionally, you can also define a local sampling interval for each object defined in the devices
section of the configuration file. In such a case, a local sampling interval overrides the global one.
The global sampling interval is set to
4000
and the sampling interval for a temperature measurement object is2000
, so the Agent retrieves data about this temperature object every two seconds.
If you configure a local sampling interval for each object defined in the
devices
section, you do not need to configure a global sampling interval. However, if at least one object has no local sampling interval defined, the global one must be provided. Otherwise, the adapter configuration is invalid.
Configuring Custom Prefix
The custom prefix determines where to report data. If not defined, all data is reported to v1/sb
, by default.
The custom_prefix
option enables setting a different prefix to publish data e.g. to a custom MQTT topic for local data processing, or directly to the Gateway Agent northbound interface.
It is a global setting that refers to all data retrieved from devices.
Example:
"custom_prefix": "v1/nb"
Optionally, you can also define a local custom prefix for each measurement, alert, configuration parameter and metadata configured in the devices section of the configuration file. In such a case, a local custom prefix overrides the global one.
Instead of the
custom_prefix
option, you can also use thecustom_mqtt_prefix
option, which was implemented in the older adapter versions. However, it is recommended to usecustom_prefix
.
Configuring a List of Devices
In the devices
section of the configuration file, you can configure a list of devices. For each device, the following configuration sections are available:
connection - Selecting a device connection.
measurements - List of measurements that can be reported by a device.
alerts - List of alerts that can be reported by a device.
configuration - Configuration parameters of a device.
metadata - List of metadata of a device.
The devices
section has the following structure:
"devices" :
[
{
"connection" : { ... },
"measurements" : [ ... ],
"alerts" : [ ... ],
"configuration" : [ ... ],
"metadata" : [ ... ]
}
]
Configuring a Device Connection
In the connection.id
field, enter the connection identifier for a given device. Possible ID values are defined in the connections
section of the configuration file. See the Configuring CAN Bus Connections section for details.
It is mandatory to configure a connection ID for each device.
Here is an example of the device connection configuration:
"devices": [
{
"connection": {
"id": "dev"
},
You can also configure a local connection ID for a specific measurement, alert, configuration parameter and metadata. If defined, the local connection ID overrides the global one, set in the
connection.id
field.
Configuring Measurements
You can define multiple measurements for a device, such as: temperature, humidity, pressure, etc.
To configure a measurement, define the following mandatory settings:
Configuration Setting | Description |
---|---|
id | Measurement identifier. A value provided here must match the measurement ID set in the device model version created in the relayr Cloud. For more information on creating device models and versions in the relayr Cloud, refer to the relayr Cloud API documentation. |
object | Protocol-specific object definition. See the CAN Bus Object Definition section for details. |
Optionally, you can define the following advanced settings for a measurement:
Configuration Setting | Description |
---|---|
filters.cov | Change of value functionality. If it is set to enabled , measurements are reported by a device only if their value differs from the latest reported value by at least a cov_delta value. |
filters.cov_delta | Delta for the change of value functionality. It specifies the minimum change of value (in relation to the latest reported value) that is required to report the measurement to the Agent. |
sampling_interval | Local sampling interval value, given in milliseconds, that overrides the global sampling interval. |
custom_prefix | Prefix of the MQTT topic that the object is reported to. An option to publish data to a custom MQTT topic for a local data processing or directly to the northbound interface. For example, if it is set to analytics for a temperature measurement, all data is published to the topic analytics/{did}/measurements/temperature . It overrides the global custom prefix. |
connection | Local connection ID used for a given measurement. It overrides the global connection set for the device. Possible ID values are defined in the connections section of the configuration file. See the Configuring CAN Bus Connections section for details. |
complex | Add this configuration section to configure a complex measurement. Specify how many data samples a complex measurement contains and, optionally, a collection_interval . For more information, see the Configuring Complex Measurements section. |
Here is an example of the measurement configuration:
{
"id" : "temperature",
"object" : { ... },
"sampling_interval" : 4000,
"filters" : {
"cov" : "enabled",
"cov_delta" : 0.04
}
},
In the example presented above, the temperature reading is reported by the device every four seconds (as defined in the
sampling_interval
field). The change of value (cov
) is enabled, and thecov_delta
is set to0.04
. It means that the temperature is reported only if it is different from the latest reported value by at least0.04
.
Configuring Complex Measurements
A complex measurement is a batch of measurement data, in which several samples of collected data values are published as a single message.
Complex measurements can be consumed by the Rule Engine and the GWA Analytics for further data processing and can be stored in the Storage Service. The Cloud Adapter supports publishing complex measurements to the relayr Cloud.
You can configure the size of a complex measurement by specifying how many data samples each batch contains. Optionally, you can also specify a collection interval. If specified, the adapter starts collecting data at every collection interval (e.g. 1h) and publishes a complex measurement when a configured number of samples are collected (e.g. 1000). Then, the adapter waits for the next collection interval and starts to acquire data again.
To configure a complex measurement, add the complex
section to the measurement configuration with the following settings:
Configuration Setting | Description |
---|---|
samples | Number of collected data samples in a complex measurement. For complex measurements, this setting is mandatory. |
collection_interval | Optional setting. Time interval, given in seconds, at which the adapter starts to acquire data for a complex measurement. |
Example:
{
"object": {},
"complex":
{
"samples": 10,
"collection_interval": 3600
}
}
If the
collection_interval
is lower than needed to collect the configured number of samples, the adapter collects data continuously. It starts to acquire data immediately after publishing the previous complex measurement.
If sensors stop sending data in the middle of data collection, e.g. due to connectivity problems, the adapter waits until the required number of samples are collected, despite the collection intervals. It publishes a complex measurement that contains data points not evenly distributed in time, e.g. with a 5h gap. Such a complex measurement may be considered as incorrect and may be discarded by the Rule Engine and/or the GWA Analytics, based on the
startTs
andendTs
values.
Complex Measurement Message
The collected data samples of a configured complex measurement are published as a complex measurement message. The Protocol Adapters support acquiring and publishing complex measurements in the time domain, but you can also use complex measurement messages to publish measurements in other domains, e.g. the frequency domain.
Complex measurements in the frequency domain may be reported by the Pico Adapter, or you may use the Rule Engine or the GWA Analytics to calculate them from the time-based measurements.
In a complex measurement message:
startTs
is a moment when data acquisition begins.endTs
is a moment when data acquisition finishes.xStart
is the first value on the X axis. ThexStart
unit depends on the measured value. For measurements in the time domain, it is a timestamp indicating the moment when the first data sample is collected. For measurements in the frequency domain, it is the first frequency on the X axis.xStep
represents a resolution of data on the X axis. For measurements in the time domain,xStep
is a time interval between consecutive data samples, given in milliseconds (you configure this interval in thesampling_interval
setting of the Protocol Adapters' configuration). ThexStep
value allows you to know the precise moment when each data sample was collected. For measurements in the frequency domain,xStep
represents the frequency resolution of data representation on the X axis.yValues
is an array of ordered measurement values.
Example 1: time-based measurement:
v1/sb/{UUID}/complex_measurements/temperature {
“id”:“temperature”,
“startTs”: 1492515087123,
“endTs”: 1492515092123,
“xStart”: 1492515087123,
“xStep”: 1000,
“yValues”: [42, 43, 39, 37, 40, 48, 45, 40, 38, 33, 30, 31...]
}
In this example,
temperature
is a time-based measurement. ThexStart
value is1492515087123
, which shows the moment when the first data sample (42
) was collected. ThexStep
value is1000
, which means that consecutive data samples were collected at the interval of 1000 milliseconds.
Example 2: frequency-based measurement:
v1/sb/{UUID}/complex_measurements/acceleration_xaxis_fft {
“id”:“acceleration_xaxis_fft”,
“startTs”: 1492515087123,
“endTs”: 1492515092123,
“xStart”: 90,
“xStep”: 10,
“yValues”: [3, 12, 48, 579, 644, 689, 711, 647, 78, 644, 669, 112, 638, 652, 617, 48...]
}
In this example,
acceleration_xaxis_fft
is a frequency-based measurement. ThexStart
value is90
, which shows that the first frequency on the X axis is 90 Hz. ThexStep
value is10
, which means that the frequency step on the X axis, between consecutive data samples, is 10 Hz.
Configuring Alerts
In the alerts
configuration section, you can define alerts for the configured devices.
To configure an alert, define the following mandatory settings:
Configuration Setting | Description |
---|---|
id | Alert identifier. A value provided here must match the alert ID of the device model created in the relayr Cloud. For more information on creating device models and versions in the relayr Cloud, refer to the relayr Cloud API documentation. |
measurement_id | Identifier of a measurement associated with the alert. The ID provided here must be configured in the measurements section of the configuration file. See the section above for details. Alternatively, you can configure the object section for an alert. See here for details. |
threshold_set | Threshold value based on which the alert is activated. |
logic_set | Logical relation for alert activation. Possible values are: "<", ">", "==", "">=", "<=". |
message_set | Message sent when the alert is activated. It is a free-form string. |
threshold_clear | Threshold value based on which the alert is cleared. |
logic_clear | Logical relation for clearing the alert. Possible values are: "<", ">", "==", "">=", "<=". |
message_clear | Message displayed when the alert is cleared. It is a free-form string. |
If both the
logic_clear
andlogic_set
conditions are fulfilled at the same time, the alert is set.
Optionally, you can define the following advanced settings for an alert:
Configuration Setting | Description |
---|---|
sampling_interval | Local sampling interval value, given in milliseconds, that overrides the global sampling interval. |
custom_prefix | Prefix of the MQTT topic that the object is reported to. An option to publish data to a custom MQTT topic for a local data processing or directly to the northbound interface. For example, if it is set to analytics for a temperature-based alert, all data is published to the topic analytics/{did}/alerts/alert_temp_high . It overrides the global custom prefix. |
connection | Local connection ID used for a given alert. It overrides the global connection set for the device. Possible ID values are defined in the connections section of the configuration file. See the Configuring CAN Bus Connections section for details. |
Here is an example of the alert configuration:
{
"id" : "alert_temp_high",
"measurement_id" : "temperature",
"threshold_set" : 38.5,
"logic_set" : ">=",
"message_set" : "TEMPERATURE IS TOO HIGH",
"threshold_clear" : 36.5,
"logic_clear" : "<=",
"message_clear" : "ALERT IS CLEAR",
"sampling_interval" : 4000
}
In the example presented above, the alert is activated if the temperature reaches a level defined in the
threshold_set
andlogic_set
fields, that is: if it is greater than or equal to 38.5. The message "TEMPERATURE IS TOO HIGH" is sent to the relayr Cloud when the alert is set.
When the temperature decreases and it is smaller than or equal to 36.5 (as specified in thethreshold_clear
andlogic_clear
fields), the alert is deactivated and the message "ALERT IS CLEAR" is sent to the relayr Cloud when the alert is deactivated.
The value provided in thesampling_interval
field means that the system verifies the alert data every four seconds.
Configuring Configuration Parameters
In the configuration
section, you can define the device configuration parameters.
To define a configuration parameter, define the following mandatory settings:
Configuration Setting | Description |
---|---|
id | Configuration object identifier. |
object | Protocol-specific object definition. See the CAN Bus Object Definition section for details. |
Optionally, you can define the following advanced settings for a configuration parameter:
Configuration Setting | Description |
---|---|
ro | This setting determines if a given configuration parameter is read-only (if set to true ) or the read/write permissions are granted to it (if set to false ). If no ro value is defined, the object has read/write permissions, by default. |
sampling_interval | Local sampling interval value, given in milliseconds, that overrides the global sampling interval. |
custom_prefix | Prefix of the MQTT topic that the object is reported to. An option to publish data to a custom MQTT topic for a local data processing or directly to the northbound interface. For example, if it is set to analytics for a configuration parameter xyz , all data is published to the topic analytics/{did}/configuration/xyz . It overrides the global custom prefix. |
connection | Local connection ID used for a given configuration parameter. It overrides the global connection set for the device. Possible ID values are defined in the connections section of the configuration file. See the Configuring CAN Bus Connections section for details. |
Here is an example of the configuration settings:
{
"id" : "parameter2045",
"object" : { ... },
"sampling_interval" : 4000,
"ro" : true
},
Configuring Metadata
The metadata
section of the configuration file allows you to define device attributes, such as the device ID, model, version, location, owner's email address, etc.
There are two ways to configure metadata:
Static metadata - metadata is an
id
andvalue
pair. It is sent only once, at the application startup.Dynamic metadata - metadata has a defined
object
. It is reported not only at the application startup, but also when the metadata value changes.
Static Metadata
Static metadata is configured as an id
and value
pair.
Static metadata is reported only once, at the application startup. To provide support for reporting a changing metadata value, configure dynamic metadata.
To configure the device static metadata, define the following values:
Configuration Setting | Description |
---|---|
id | Metadata identifier. |
value | Metadata value. |
Optionally, you can configure the following metadata settings:
Configuration Setting | Description |
---|---|
custom_prefix | Prefix of the MQTT topic that the object is reported to. An option to publish data to a custom MQTT topic for a local data processing or directly to the northbound interface. For example, if it is set to analytics for metadata serial_number , all data is published to the topic analytics/{did}/metadata/serial_number . It overrides the global custom prefix. |
It is mandatory to define the device ID (
did
) for each device. Thedid
value must match the UUID of the device created in the relayr Cloud.
For more information on creating devices in the Cloud, refer to the relayr Cloud API documentation.
The device
did
cannot contain spaces and plus (+) characters.
Here is an example of the device static metadata:
"metadata": [
{
"id": "did",
"value": "f33c6bah-b31e-4f5a-964h-13a935a8b25f"
}
]
Dynamic Metadata
Dynamic metadata is configured with a protocol-specific object
.
Dynamic metadata is published only if its value changes.
To report a metadata value dynamically, configure the following settings:
Configuration Setting | Description |
---|---|
id | Metadata identifier. |
object | Protocol-specific object definition. See the Canbus Object Definition section for details. |
Optionally, you can configure the following dynamic metadata settings:
Configuration Setting | Description |
---|---|
custom_prefix | Prefix of the MQTT topic that the object is reported to. An option to publish data to a custom MQTT topic for a local data processing or directly to the northbound interface. For example, if it is set to analytics for metadata serial_number , all data is published to the topic analytics/{did}/metadata/serial_number . It overrides the global custom prefix. |
sampling_interval | Local sampling interval value, given in milliseconds, that overrides the global sampling interval. |
connection | Local connection ID used for given metadata. It overrides the global connection set for the device. Possible ID values are defined in the connections section of the configuration file. See the Configuring CAN Bus Connections section for details. |
Here is an example of the device dynamic metadata:
"metadata": [
{
"id" : "serial_number",
"object" : { ... },
"sampling_interval" : 4000,
"custom_prefix" : "v1/nb"
},
CAN Bus Object Definition
For each configured measurement, configuration parameter and dynamic metadata, there is a protocol-specific object
configuration section with the following configuration options:
Configuration Setting | Description |
---|---|
id | Identifier used in CAN frames (given as hexadecimal string, e.g. "07E8" ). |
type | Data type which CAN frame payload is encoded to. It can be: integer , float , bool or string . Depending on the data type entered here, different manipulators can be specified. See here for a list of manipulators and their dependencies. |
remote_request | Flag which determines if a particular object should be requested from ECU by sending the RTR frame (if set to true ) or only passively gathered from the bus by listening to it (if set to false ). The default value is false . Note: RTR frames are not supported by CAN FD controllers, so this setting, if defined, is silently ignored. |
read_timeout | Timeout value (given in milliseconds), defining how long the adapter should wait for incoming data in its every cycle. This setting overrides the global one (defined in the connections section). |
eventdriven | By default, it is false for all objects. Configuration objects with ro set to false , however, should have it set to true . It allows to handle configuration task requests (more information can be found here. |
trigger_request_id | Identifier used in CAN request frame, e.g. in J1939 needed to get data for particular PID. |
trigger_request_data | Data payload (given as a hexadecimal string, e.g. "02010D5555555555" ) to send in the request frame. |
Here is an example of the CAN Bus object definition:
"object": {
"id": "07B",
"type": "integer",
"byte_start": 0,
"byte_count": 4,
"remote_request": true,
"read_timeout": 100
}
Data Type Manipulators and Their Dependencies
For each data type, specified in the type
field in the object definition, you can specify the following manipulators:
Data Type | Manipulators |
---|---|
integer | byte_start AND byte_count |
byte AND bit_start AND bit_count | |
float | byte_start (to read exactly 4 bytes starting from byte_start ) |
bool | byte AND bit |
string | encoding : (ascii or hex ) |
Here is a description of the manipulators listed above:
Configuration Setting | Description |
---|---|
byte | Which byte to operate on (0..63) |
byte_start | Which byte to start operating on (0..63) |
byte_count | How many bytes to operate on (1..64) |
bit | Which bit to operate on (0..7) |
bit_start | Which bit to start operating on (0..7) |
bit_count | How many bits to operate on, starting from bit_start (1..8) |
encoding | It is relevant only for string data type. It defines how a string should be encoded: ascii -- for ASCII characters encoding only, or hex -- for a hexadecimal string. |
For numeric types, data frame payload is interpreted in little endian, i.e. the first byte of the data payload becomes the least significant byte in the result value.
Each data type manipulator has a different set of dependencies. Here is a list detailing the dependencies between the manipulators, data types and other settings in the object
definition:
encoding
depends on thestring
data type.bit
depends on thebool
data type, and onbyte
.bit_start
depends onbyte
andbit_count
.bit_count
depends onbit_start
.byte_start
depends on thefloat
data type, OR onbyte_count
.byte_count
depends onbyte_start
.trigger_request_id
depends ontrigger_request_data
.trigger_request_data
depends ontrigger_request_id
.
Here is a template of configuration definition, presenting the available options:
"configuration": [
{
"object": {
"id": "<CAN_ID_in_hex>",
"type": "<integer|float|bool|string>",
"remote_request": <false|true>,
"read_timeout": <integer in milliseconds>,
"encoding": <ascii|hex>,
"< <byte <bit | <bit_start><bit_count>>> | <<byte_start>[byte_count]> >"
"byte": <integer from 0..63>,
"bit": <integer from 0..7>,
"byte_start": <integer from 0..63>,
"byte_count": <integer from 1..64>,
"bit_start": <integer from 0..7>,
"bit_count": <integer from 1..8>,
"trigger_request_id": "<CAN_ID_in_hex>",
"trigger_request_data": "<hexstring>",
"eventdriven": <false|true>
},
"id": "<ID_string>",
"ro": <false|true>
}
],
Configuration Task Handling
To handle configuration tasks, the configuration object has to be configured
as eventdriven (the eventdriven
setting in the object definition set to true
) and read-write (the ro
parameter set to false
), with its type set to string
.
Here is an example of the configuration
definition:
"configuration": [
{
"object": {
"id": "07B",
"type": "string",
"read_timeout": 100,
"eventdriven": true
},
"id": "999",
"ro": false
}
],
Configuration data has to be in two hex digits encoded hexstring format, i.e.
"0A12"
. Such approach simplifies usability - what the adapter gets is packed into the data frame and sent, without any need for conversions, changing order of bytes, etc.
Examples of Configuration Files
Deployment Configuration File
{
"transport": "mqtt",
"mqtt_connection": {
"host": "localhost",
"port": 1883,
"keepalive": 20,
"max_inflight_messages": 100,
"qos_levels": {
"measurements": 0,
"alerts": 1,
"configuration": 1,
"metadata": 1
}
},
"health_monitor": {
"heartbeat_interval": 60
},
"pmq_connection":
{
"output_queues": ["/canbus_out_queue"],
"input_queues": ["/canbus_in_queue"],
"message_size": 8192,
"message_count": 10
}
}
Application Configuration File
{
"connections": [
{
"id": "0",
"ifname": "vcan0",
"read_timeout": 50
}
],
"sampling_interval": 4000,
"devices": [
{
"connection": {
"id": "0"
},
"measurements": [
{
"object": {
"id": "01A",
"type": "integer",
"byte_start": 0,
"byte_count": 2,
"remote_request": false,
"read_timeout": 5000
},
"id": "1",
"sampling_interval": 5000
},
{
"object": {
"id": "01A",
"type": "integer",
"byte_start": 2,
"byte_count": 2,
"remote_request": false,
"read_timeout": 5000
},
"id": "2",
"sampling_interval": 5000
}
],
"alerts": [
],
"configuration": [
],
"metadata": [
{
"id": "did",
"value": "abcdef-123456-aaaaaa-bbbbbb"
}
]
}
]
}