forked from eclipse-kuksa/kuksa-incubation
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[eclipse-kuksa#14] can-protocol-adapter initial version
- Loading branch information
1 parent
ebea398
commit b78af09
Showing
2,600 changed files
with
253,169 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
######################################################################## | ||
# Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
# | ||
# See the NOTICE file(s) distributed with this work for additional | ||
# information regarding copyright ownership. | ||
# | ||
# This program and the accompanying materials are made available under the | ||
# terms of the Apache License 2.0 which is available at | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
######################################################################## | ||
|
||
[package] | ||
name = "can-protocol-adapter" | ||
version = "0.1.0" | ||
edition = "2021" | ||
license = "Apache-2.0" | ||
|
||
[profile.release] | ||
strip = true | ||
opt-level = "z" # Optimize for size | ||
lto = true #Enable link time Optimization | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
log = "0.4.21" | ||
env_logger = "0.10.2" | ||
tokio = { version = "1.0", features = ["full"] } | ||
clap = { version = "4", features = ["derive"] } | ||
serde = { version = "1.0", features = ["derive"] } | ||
serde_json = "1.0" | ||
socketcan-isotp = "1.0.2" | ||
tonic = "0.11.0" | ||
databroker-proto = { path = "src/databroker-proto"} | ||
kuksa-common = { path = "lib/common"} | ||
kuksa = { path = "lib/kuksa"} | ||
can-dbc="6.0.0" | ||
codegen = "0.2" | ||
byteorder = "1.3" | ||
http = "0.2.8" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
######################################################################## | ||
# Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
# | ||
# See the NOTICE file(s) distributed with this work for additional | ||
# information regarding copyright ownership. | ||
# | ||
# This program and the accompanying materials are made available under the | ||
# terms of the Apache License 2.0 which is available at | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
######################################################################## | ||
|
||
FROM ghcr.io/rust-cross/rust-musl-cross:x86_64-musl AS builder-amd64 | ||
ENV BUILDTARGET="x86_64-unknown-linux-musl" | ||
|
||
|
||
FROM ghcr.io/rust-cross/rust-musl-cross:aarch64-musl AS builder-arm64 | ||
ENV BUILDTARGET="aarch64-unknown-linux-musl" | ||
|
||
FROM builder-$TARGETARCH AS builder | ||
ARG TARGETARCH | ||
|
||
# This will speed up fetching the crate.io index in the future, see | ||
# https://blog.rust-lang.org/2022/06/22/sparse-registry-testing.html | ||
ENV CARGO_UNSTABLE_SPARSE_REGISTRY=true | ||
|
||
RUN echo "Building for $TARGETARCH" \ | ||
mkdir build | ||
COPY . build/ | ||
WORKDIR /home/rust/src/build | ||
|
||
RUN cargo build --package can-protocol-adapter --release --target $BUILDTARGET | ||
RUN mv target/${BUILDTARGET}/release/can-protocol-adapter /home/rust | ||
|
||
FROM scratch AS final | ||
|
||
COPY --from=builder /home/rust/can-protocol-adapter can-protocol-adapter | ||
|
||
ENTRYPOINT [ "/can-protocol-adapter" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
![Rust](https://img.shields.io/badge/rust-000000.svg?style=for-the-badge&logo=rust&logoColor=white)![Docker](https://img.shields.io/badge/docker-1D63ED.svg?style=for-the-badge&logo=docker&logoColor=white)![CAN](https://img.shields.io/badge/CAN-005BBB.svg?style=for-the-badge&logo=generic&logoColor=white) | ||
|
||
|
||
# CAN Protocol Adapter | ||
|
||
The CAN Protocol Adapter is a **Rust-based solution** that offers communication between CAN devices and the Kuksa DataBroker using the Socket-CAN interface and ISO-TP (ISO Transport Protocol). | ||
|
||
It can be used for various applications, including communication between CAN bus systems, supporting ISO-TP for segmented data transfer and reception of large payloads beyond the standard 8-byte limit and reading vehicle OBD-II PID data for diagnostics and monitoring. | ||
|
||
## Key Features | ||
|
||
- **User Configuration** : JSON-based user configuration for setting up CAN and Socket CAN interfaces, defining CAN request-response details, specifying protocol parameters, and mapping VSS and PID signal names. | ||
|
||
|
||
- **Socket CAN Integration** : Compatible with Socket CAN interfaces for CAN bus communication in Linux-based environments. | ||
|
||
|
||
- **Read CAN Frame and Decode**: Read CAN frames from the bus and decode them using DBC files, mapping the signals to their corresponding values. | ||
|
||
|
||
- **Register and Feed Data Points to Data Broker**: Register VSS signal data points with Kuksa Data Broker and feed the decoded or interpreted values into the data broker. | ||
|
||
## The basic functionality works as follows: | ||
|
||
The application connects to the SocketCAN interface and sends all CAN requests, as specified in the PID table of the config file, based on a defined interval. The responses are read, and the raw CAN data is parsed according to the DBC file signals. The CAN signals are then translated into VSS data points based on the VSS and DBC signal mappings described in the config file. The respective VSS data points are then sent to the Kuksa Databroker. | ||
|
||
![CAN-Protocol-adapter]() | ||
|
||
## Initial Version (v0.1.0) Features | ||
|
||
The details of the features, planned enhancements and known issues for the initial version (v0.1.0) can be found [here](). | ||
|
||
|
||
## Getting started | ||
|
||
- **Prerequisites** | ||
|
||
Install CAN utils, e.g. in Ubuntu machine | ||
|
||
sudo apt update | ||
sudo apt install can-utils | ||
|
||
Linux Kernel Requirements | ||
|
||
The minimum required Linux kernel version is 5.10, and can-isotp has been part of the Linux mainline since version 5.10. | ||
|
||
- **Configuration** | ||
|
||
The CAN Protocol Adapter offers different configuration options. For detailed instructions on how to use the CAN Adapter in various scenarios, please refer to [CAN Provider Configuration]() | ||
|
||
- **Sample ISOTP DBC File** | ||
|
||
The DBC file [obd2_isotp.dbc]() can be used for testing purposes. The repository contains a sample obd2_isotp DBC file with several signals as examples, which can be used to test OBD2 PIDs. | ||
|
||
- **Kuksa APIs supported** | ||
|
||
CAN protocol adapter implements kuksa.val.v1.VAL gRPC service interface currently. | ||
|
||
## Building the application | ||
**Using cargo to Build:** | ||
|
||
To compile the project in release mode using cargo | ||
|
||
cargo build --release | ||
|
||
**Using the build.sh Script:** | ||
|
||
Alternatively, you can use the provided build.sh script to build the project. The script performs additional security checks using tools like Clippy, Audit, and Deny before building the project. | ||
|
||
./build.sh --release | ||
|
||
The compiled binary is built as target/release/can-protocol-adapter. | ||
|
||
## Running the application | ||
|
||
In order to run the application you need to pass the config.json file as an argument like this: | ||
|
||
./target/release/can-protocol-adapter –-config config.json | ||
|
||
To enable logging use | ||
|
||
RUST_LOG=debug ./target/release/can-protocol-adapter –-config config.json | ||
|
||
## Build Docker Image | ||
|
||
Build the can-protocol-adapter as a Docker container: | ||
|
||
docker build -f Dockerfile --progress=plain -t can-protocol-adapter:latest . | ||
|
||
## Build for a Different Architecture | ||
|
||
To build for a different architecture, pass TARGETARCH as build argument currently Dockerfile supports amd64 and arm64 architectures: | ||
|
||
docker build -f Dockerfile --progress=plain -t can-protocol-adapter:latest --build-arg TARGETARCH=arm64 . | ||
|
||
## Running the Docker Container | ||
|
||
Run the container with the following command: | ||
|
||
docker run --network=host -v /<path_to_config>/config.json:/config/config.json -v /<path_to_dbc>/<dbc_file_name>.dbc:/data/<dbc_file_name>.dbc can-protocol-adapter:latest --config /config/config.json | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#!/bin/bash | ||
# | ||
# Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
# | ||
# See the NOTICE file(s) distributed with this work for additional | ||
# information regarding copyright ownership. | ||
# | ||
# This program and the accompanying materials are made available under the | ||
# terms of the Apache License 2.0 which is available at | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
|
||
# This script builds the can-protocol-adapter for specified architectures in either debug or release mode. | ||
# It also runs basic cargo quality checks like clippy, audit, and deny for code quality and security. | ||
|
||
# Define the target directory | ||
TARGET_DIR="target/logs" | ||
|
||
# Create the target directory if it doesn't exist | ||
mkdir -p $TARGET_DIR | ||
|
||
CLIPPY_LOG="$TARGET_DIR/clippy_report.log" | ||
AUDIT_LOG="$TARGET_DIR/audit_report.log" | ||
DENY_LOG="$TARGET_DIR/deny_report.log" | ||
BUILD_LOG="$TARGET_DIR/build_output.log" | ||
|
||
display_help() { | ||
echo "Usage: $0 [TARGET] [--release]" | ||
echo " TARGET: The target platform (e.g. x86_64-unknown-linux-gnu, aarch64-unknown-linux-gnu)." | ||
echo " --release: Build in release mode.If omitted, builds in debug mode." | ||
} | ||
|
||
if [ -z "$1" ] || [[ "$1" == "--help" ]]; then | ||
display_help | ||
exit 0 | ||
fi | ||
|
||
TARGET="$1" | ||
RELEASE="$2" | ||
|
||
# Run cargo clippy with -D warnings | ||
echo "Running cargo clippy..." | ||
cargo clippy -- -D warnings 2>&1 | tee $CLIPPY_LOG | ||
if [ $? -ne 0 ]; then | ||
echo "Clippy failed! Check $CLIPPY_LOG for details." | ||
exit 1 | ||
fi | ||
|
||
# Run cargo audit | ||
echo "Running cargo audit..." | ||
cargo audit 2>&1 | tee $AUDIT_LOG | ||
if [ $? -ne 0 ]; then | ||
echo "Cargo audit failed! Check $AUDIT_LOG for details." | ||
exit 1 | ||
fi | ||
|
||
# If all checks passed, build the project | ||
echo "Building the project..." | ||
cargo build --target $TARGET $RELEASE 2>&1 | tee $BUILD_LOG | ||
if [ $? -ne 0 ]; then | ||
echo "Build failed! Check $BUILD_LOG for details." | ||
exit 1 | ||
fi | ||
|
||
echo "All tasks completed successfully!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
{ | ||
"general_config": { | ||
"broker_ip": "localhost", | ||
"broker_port": "55555", | ||
"dbcfile" : "/dbc/obd2-isotp.dbc" | ||
}, | ||
"can_config": { | ||
"can_interface": "can0", | ||
"use_extended_id": false, | ||
"tx_id": "0x7DF", | ||
"rx_id": "0x7E8", | ||
"socket_can_type": "SOCK_DGRAM", | ||
"socket_can_protocol": "CAN_ISOTP" | ||
}, | ||
"pid_table": [ | ||
{ | ||
"request_pid": "01 0D", | ||
"response_pid": "41 0D", | ||
"response_timeout_ms": 100, | ||
"description": "Vehicle Speed", | ||
"expected_response_length": 4, | ||
"interval_ms": 500, | ||
"dbc_signal_name" : "S1PID0D_VehicleSpeed", | ||
"vss_signal": { | ||
"signal_name": "Vehicle.OBD.Speed", | ||
"datatype": "float", | ||
"unit": "km/h" | ||
} | ||
}, | ||
{ | ||
"request_pid": "01 0C", | ||
"response_pid": "41 0C", | ||
"response_timeout_ms": 100, | ||
"description": "Engine RPM", | ||
"expected_response_length": 3, | ||
"interval_ms": 500, | ||
"dbc_signal_name" : "S1PID0C_EngineRPM", | ||
"vss_signal": { | ||
"signal_name": "Vehicle.OBD.EngineSpeed", | ||
"datatype": "float", | ||
"unit": "rpm" | ||
} | ||
}, | ||
{ | ||
"request_pid": "01 05", | ||
"response_pid": "41 05", | ||
"response_timeout_ms": 100, | ||
"description": "Engine Coolant Temperature", | ||
"expected_response_length": 3, | ||
"interval_ms": 3000, | ||
"dbc_signal_name" : "S1PID05_EngineCoolantTemp", | ||
"vss_signal": { | ||
"signal_name": "Vehicle.OBD.CoolantTemperature", | ||
"datatype": "float", | ||
"unit": "celsius" | ||
} | ||
} | ||
] | ||
} |
Oops, something went wrong.