//
//  Licensed to the Apache Software Foundation (ASF) under one or more
//  contributor license agreements.  See the NOTICE file distributed with
//  this work for additional information regarding copyright ownership.
//  The ASF licenses this file to You under the Apache License, Version 2.0
//  (the "License"); you may not use this file except in compliance with
//  the License.  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//
:imagesdir: ../../images/users/protocols
:icons: font

== Generic CAN
=== CAN Bus semantics

This driver is a generic purpose driver.
It allows implementing a basic CAN bus listening or writing scenarios.

CAN bus open is a specific protocol built on top of CAN bus.

[cols="2,2a,5a"]
|===
|Name |Value |Description

|Code
2+|`genericcan`

|Name
2+|Generic CAN

|Maven Dependency
2+|
----
<dependency>
  <groupId>org.apache.plc4x</groupId>
  <artifactId>plc4j-driver-can</artifactId>
  <version>{current-last-released-version}</version>
</dependency>
----

|Default Transport:
2+|`socketcan`

|Compatible Transports:
2+|Any `can` transport, see link:../transports/can.html[CAN transport]

3+|Supported Operations

| publish
| `write`
| Allows to publish a CAN frame via CAN transport.

| receive
| `subscribe`
| This operation allows listening for incoming data or requests.

3+|Options

3+^|none

|===

=== More details on the driver

Given popularity of CAN in multiple market segments there is variety of protocols which can't be published.
Also, for many cases which are data acquisition oriented it is necessary to tap only parts of communications.

This driver allows to model incoming and outgoing communication using plain Apache PLC4X api.
The written CAN data is constructed from fields submitted via write request builder.
The receiving data is transformed in similar fashion, based on subscribed fields.

==== Address format

[cols="1,1a,1a,2a"]
|===
|Service | Format |Supported operations |Description

| n/a
| nodeId:type[arraySize]
| - `write`
- `subscribe`
| There is only one syntax which is currently supported by this driver.
|===

Pay attention.
You can use multiple fields to write and subscribe actual data.
All fields must sum to up to 64 bits.
If you subscribe less, then remaining data is discarded and not available via PLC4X api.

The data will be sent in the same order as fields.

Below table contains type mapping defined in Generic CAN driver.

[cols="2,2,2"]
|===
|CANopen Type        |Length (in bits)| PLC4X Type
| BOOLEAN            |        1 | BOOL
| UNSIGNED8          |        8 | USINT
| UNSIGNED16         |       16 | UINT
| UNSIGNED24         |       24 | UDINT
| UNSIGNED32         |       32 | UDINT
| UNSIGNED40         |       40 | ULINT
| UNSIGNED48         |       48 | ULINT
| UNSIGNED56         |       56 | ULINT
| UNSIGNED64         |       64 | ULINT
| INTEGER8           |        8 | SINT
| INTEGER16          |       16 | INT
| INTEGER24          |       24 | DINT
| INTEGER32          |       32 | DINT
| INTEGER40          |       40 | LINT
| INTEGER48          |       48 | LINT
| INTEGER56          |       56 | LINT
| INTEGER64          |       64 | LINT
| REAL32             |       32 | REAL
| REAL64             |       64 | LREAL
|===

The type table is similar to CANopen.
Main difference is lack of support for string types.