Swiftpack.co - Cornucopia-Swift/CornucopiaStreams as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by Cornucopia-Swift.
Cornucopia-Swift/CornucopiaStreams 0.9.6
Cornucopia Streams Library
⭐️ 6
🕓 20 weeks ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/Cornucopia-Swift/CornucopiaStreams.git", from: "0.9.6")

CornucopiaStreams

:shell: The "horn of plenty" – a symbol of abundance.

SwiftPM Swift

Introduction

This library provides a convenient and extensible way to get an I/O stream pair to an URL – supporting various schemes, such as TCP, TTY, BLE, and EA.

Foundation comes with getStreamsToHost(withName:port:inputStream:outputStream:), which is clumsy to use and limited to TCP. CornucopiaStreams adds support for communicating with TTYs, external accessories (using the ExternalAccessory framework), and Bluetooth Low Energy (BLE) devices (using the CoreBluetooth framework).

On non-Apple-platforms, only TCP and TTY are supported, as both ExternalAccessory and CoreBluetooth are private Apple frameworks – it might be interesting to evaluate BluetoothLinux.

Usage

Open a connection to a TTY:

import CornucopiaStreams

let url = URL(string: "tty:///dev/cu.serial-123456")!
Stream.CC_getStreamPair(to: url) { result in
    guard case .success(let (inputStream, outputStream)) = result else { fatalError() }
    … do something with the streams …
}

Open a connection to a TCP host:

import CornucopiaStreams

let url = URL(string: "tcp://192.168.0.10:35000")!
Stream.CC_getStreamPair(to: url) { result in
    guard case .success(let (inputStream, outputStream)) = result else { fatalError() }
    … do something with the streams …
}

Open a connection to an external accessory:

import CornucopiaStreams

let url = URL(string: "ea://com.obdlink")!
Stream.CC_getStreamPair(to: url) { result in
    guard case .success(let (inputStream, outputStream)) = result else { fatalError() }
    … do something with the streams …
}

Open a connection to a BLE device providing service FFF0:

import CornucopiaStreams

let url = URL(string: "ble://FFF0")!
Stream.CC_getStreamPair(to: url) { result in
    guard case .success(let (inputStream, outputStream)) = result else { fatalError() }
    … do something with the streams …
}

Open a connection to the BLE device E32E4466-A24A-E46B-EE79-436569D6FC6D that provides service FFF0:

import CornucopiaStreams

let url = URL(string: "ble://FFF0/E32E4466-A24A-E46B-EE79-436569D6FC6D")!
Stream.CC_getStreamPair(to: url) { result in
    guard case .success(let (inputStream, outputStream)) = result else { fatalError() }
    … do something with the streams …
}

Since version 0.9.3, you can alternatively use an async call to connect to a stream:

Open a connection to the BLE device E32E4466-A24A-E46B-EE79-436569D6FC6D that provides service FFF0:

import CornucopiaStreams

let url = URL(string: "ble://FFF0/E32E4466-A24A-E46B-EE79-436569D6FC6D")!
let streams = try await Stream.CC_getStreamPair(to: url)
… do something with the streams …

Metadata

Some of the streams provide metadata, e.g., the name for BLE devices, which you can access via the CC_meta property.

Roadmap to 1.0 and beyond

Before the big 1.0, this project wants to

  • ☐ Provide a comprehensive testsuite.
  • ☐ Support cancelling a pending connection.
  • ☐ Support force-closing an active connection.
  • ☐ Support Task cancellation for pending connections.

After 1.0, we might tackle additional connection mechanisms, perhaps

  • Bluetooth 3.x (rfcomm)?
  • Direct access to L2CAP?
  • SSL sockets?

Contributions

Feel free to use under the terms of the MIT, if you find anything helpful here. Contributions are always welcome! Stay safe and sound!

GitHub

link
Stars: 6
Last commit: 23 hours ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

CornucopiaStreams 0.9.2
39 weeks ago

Last release pre-iOS15/tvOS15/watchOS8/macOS12

Due to a source-incompatible change in the Apple CoreBluetooth API, further versions of this library will require the very latest set of Apple platforms.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics