SwiftCANLib is a library used to process Controller Area Network (CAN) frames utilizing the Linux kernel open source library SOCKETCAN.

Overview

SwiftCANLib

SwiftCANLib is a library used to process Controller Area Network (CAN) frames utilizing the Linux kernel open source library SOCKETCAN. This library has been tested with Rapsbian Buster, arm32 & arm64, on a Raspberry Pi 4. It will not compile and run on macOS/iOS/iPadOS/tvOS because SOCKETCAN is not distributed on these platforms.

This is not an officially supported Google product.

How to add SwiftCANLib to your project

In your Package.swift file add the following dependency and add SwiftCANLib to all targets that will use the library:

https://github.com/leisurehound/swiftcanlib.git

Import SwiftCANLib in files where your project will interact with the CAN interface on your device

#import SwiftCANLib

Create a set of signals for the data parameters you're interested in:

let speedSignal = CANCalibrations.Signal(name: "Speed",
                                            unit: "km/h",
                                            dataLength: 16, startBit: 32,
                                            endianness: .bigEndian,
                                            isSigned: false,
                                            offset: -100.0,
                                            gain: 0.01)
let rpmSignal = CANCalibrations.Signal(name: "Engine Speed",
                                            unit: "rpm",
                                            dataLength: 0, startBit: 16,
                                            endianness: .bigEndian,
                                            isSigned: false,
                                            offset: 0.0,
                                            gain: 1.0)

Add signals to a frame calibration:

let frame100Calibration = CANCalibrations.FrameDefinition(frameID: 0x100, signals: [speedSignal])
let frame120Calibration = CANCalibrations.FrameDefinition(frameID: 0x120, signals: [rpmSignal])

Add the frame calibrations to the CANCalibrations object:

let calibrations = CANCalibrations(frames: [frame100Calibration, frame120Calibration], delegate: self)

Create the CANInterface object, injecting the calibrations you're interested in processing from that interface and start listening:

let primaryCAN = try? CANInterface(name: "can1", filters: [0x100,0x120], calibrations: calibrations)
primaryCAN?.startListening()

Create a delegate object that conforms the CANCalibrationsListenerDelegate and implement the processCalibratedData method:

func processCalibratedData(_ calibrations: CANCalibrations, calibratedData: CANCalibrations.CalibratedData) {

  for (signalName, datum) in calibratedData.signals {
    print("Received data for \(signalName) of value \(datum.value) \(datum.unit)")
  }

}

Thats it! This has been tested with Swift 5.4 on arm64 Raspberry Pi 4 and with Swift 5.1 on arm32 Raspberry Pi 3B.

SwiftCANLib performs its listening on each CAN interface on its own queue; you can direct SwiftCANLib to return data to you on an thread you specify, or it will default to calling the delegate on DispatchQueue.main.

Caveats

The library supports both little and big endian frames on big and little endian platforms. However, there are currently no big endian platforms that support Swift, thus running on a big endian platform is completely untested. Big and little endian frames on a little endian platform is supported and tested by unit tests.

Note that SwiftCANLib does not use SOCKETCAN directly. Swift currently will not link correctly against a CSocketCAN module on Linux thus a C language bridge module is utlized to make the SOCKETCAN calls.

SwiftCANLib will not compile and run on macOS/iOS/iPadOS/tvOS because SOCKETCAN is not distributed on these platforms.

Utlizing Combine for feeding CAN frames to client applications would be awesome. However Apple has not open sourced Combine thus it has not been ported to Linux and SwiftCANLib uses a simple delegate pattern for providing data the client application.

License

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with the License.

You might also like...
Open source SDK to quickly integrate subscriptions, stop worring about code maintenance, and getting advanced real-time data.  Javascript / iOS glue framework
Open source SDK to quickly integrate subscriptions, stop worring about code maintenance, and getting advanced real-time data. Javascript / iOS glue framework

Open source SDK to quickly integrate subscriptions, stop worring about code maintenance, and getting advanced real-time data. Javascript / iOS glue framework

Lightweight library for web server applications in Swift on macOS and Linux powered by coroutines.
Lightweight library for web server applications in Swift on macOS and Linux powered by coroutines.

Why Zewo? • Support • Community • Contributing Zewo Zewo is a lightweight library for web applications in Swift. What sets Zewo apart? Zewo is not a w

Super lightweight async HTTP server library in pure Swift runs in iOS / MacOS / Linux

Embassy Super lightweight async HTTP server in pure Swift. Please read: Embedded web server for iOS UI testing. See also: Our lightweight web framewor

SwiftSoup: Pure Swift HTML Parser, with best of DOM, CSS, and jquery (Supports Linux, iOS, Mac, tvOS, watchOS)
SwiftSoup: Pure Swift HTML Parser, with best of DOM, CSS, and jquery (Supports Linux, iOS, Mac, tvOS, watchOS)

SwiftSoup is a pure Swift library, cross-platform (macOS, iOS, tvOS, watchOS and Linux!), for working with real-world HTML. It provides a very conveni

A Ruby on Rails inspired Web Framework for Swift that runs on Linux and OS X

IMPORTANT! We don't see any way how to make web development as great as Ruby on Rails or Django with a very static nature of current Swift. We hope th

Socket framework for Swift using the Swift Package Manager. Works on iOS, macOS, and Linux.

BlueSocket Socket framework for Swift using the Swift Package Manager. Works on iOS, macOS, and Linux. Prerequisites Swift Swift Open Source swift-5.1

Swift backend / server framework (Pure Swift, Supports Linux)

NetworkObjects NetworkObjects is a #PureSwift backend. This framework compiles for OS X, iOS and Linux and serves as the foundation for building power

A tool to build projects on MacOS and a remote linux server with one command

DualBuild DualBuild is a command line tool for building projects on MacOS and a remote Linux server. ##Setup Install the repository git clone https://

A lightweight, one line setup, iOS / OSX network debugging library! 🦊
A lightweight, one line setup, iOS / OSX network debugging library! 🦊

Netfox provides a quick look on all executed network requests performed by your iOS or OSX app. It grabs all requests - of course yours, requests from

Comments
  • Add support for OBDII querying

    Add support for OBDII querying

    OBDII querying is doable with the library, but pushing that logic onto the client app is not necessary. Allowing a client app to create a querying schedule and managing the timeouts and sequencing the queries appropriately would be more convenient than having the clients implement that logic.

    opened by leisurehound 0
Releases(v0.1)
  • v0.1(Oct 23, 2021)

    Initial public release for SwiftCANLib. The library performs the basic bridge from CAN interface to Swift objects using SOCKETCAN. Future improvements include OBDII querying and DBC file parsing for signal and frame configuration.

    Source code(tar.gz)
    Source code(zip)
Owner
Tim Wise
Tim Wise
Another network wrapper for URLSession. Built to be simple, small and easy to create tests at the network layer of your application.

Another network wrapper for URLSession. Built to be simple, small and easy to create tests at the network layer of your application. Install Carthage

Ronan Rodrigo Nunes 89 Dec 26, 2022
A network extension app to block a user input URI. Meant as a network extension filter proof of concept.

URIBlockNE A network extension app to block a user input URI. Meant as a network extension filter proof of concept. This is just a research effort to

Charles Edge 5 Oct 17, 2022
Say goodbye to the Fat ugly singleton Network Manager with this Network Layer

MHNetwork Protocol Oriented Network Layer Aim to avoid having bloated singleton NetworkManager Philosophy the main philosophy behind MHNetwork is to h

Mohamed Emad Hegab 19 Nov 19, 2022
Easy and lightweight network layer for creating different set of network requests like GET, POST, PUT, DELETE customizable with coders conforming to TopLevelDecoder, TopLevelEncoder

Easy and lightweight network layer for creating different set of network requests like GET, POST, PUT, DELETE customizable with coders conforming to TopLevelDecoder, TopLevelEncoder

Igor 2 Sep 16, 2022
Menet is a TCP network middleware that can be dynamically modified through HTTP requests.

Menet Menet is a TCP network middleware that can be dynamically modified through HTTP requests. This is an experimental project, do NOT use it in prod

Nik 2 May 19, 2022
An open-source Swift framework for building event-driven, zero-config Multipeer Connectivity apps

PeerKit An open-source Swift framework for building event-driven, zero-config Multipeer Connectivity apps Usage // Automatically detect and attach to

JP Simard 861 Dec 23, 2022
SkyWite is an open-source and highly versatile multi-purpose frameworks.

SkyWite is an open-source and highly versatile multi-purpose frameworks. Clean code and sleek features make SkyWite an ideal choice. Powerful high-level networking abstractions built into Cocoa. It has a modular architecture with well-designed, feature-rich APIs that are a joy to use.

SkyWite 24 Apr 22, 2022
Metatext A free, open-source iOS Mastodon client.

Metatext A free, open-source iOS Mastodon client. Contributing Bug Reports GitHub is used for bug tracking. Please search existing issues and create a

Metabolist 702 Jan 9, 2023
Login-screen-using-Swift - Firebase Apple Open Source Development

Firebase Apple Open Source Development This repository contains all Apple platfo

Kushal Shingote 1 Feb 3, 2022
Open source Reddit client for iOS built entirely in Swift

Area51 Area51 is an open source Reddit client for iOS built entirely in Swift! Get the public beta on TestFlight Join the public Slack channel to coll

Kris 141 Dec 26, 2022