Skip to content

Commit

Permalink
Merge pull request #155 from PureSwift/feature/embedded-swift
Browse files Browse the repository at this point in the history
Add Embedded Swift support
  • Loading branch information
colemancda authored Nov 4, 2024
2 parents 004b27a + b52b74f commit 1c0fdbc
Show file tree
Hide file tree
Showing 34 changed files with 409 additions and 179 deletions.
27 changes: 0 additions & 27 deletions .github/workflows/buildroot.yml

This file was deleted.

35 changes: 15 additions & 20 deletions .github/workflows/swift-arm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,18 @@ name: Swift ARM
on: [push]
jobs:

linux-armv7-crosscompile-build:
name: Crosscompile for Linux Armv7
runs-on: ubuntu-latest
container: colemancda/swift-armv7:latest-prebuilt
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Swift Version
run: swift --version
- name: Build (Release)
run: |
cd $SRC_ROOT
export SWIFT_PACKAGE_SRCDIR=$GITHUB_WORKSPACE
export SWIFT_PACKAGE_BUILDDIR=$SWIFT_PACKAGE_SRCDIR/.build
$SRC_ROOT/build-swift-package.sh
- name: Archive Build artifacts
uses: actions/upload-artifact@v3
with:
name: linux-armv7-crosscompile-test
path: .build/*/*.xctest
macos:
name: Baremetal Embedded ARM
runs-on: macos-latest
steps:
- name: Install Swift
uses: slashmo/[email protected]
with:
version: swift-DEVELOPMENT-SNAPSHOT-2024-10-30-a
- name: Checkout
uses: actions/checkout@v2
- name: Swift Version
run: swift --version
- name: Build
run: swift build --triple armv6m-apple-none-macho --configuration release --verbose -Xswiftc -enable-experimental-feature -Xswiftc Embedded -Xswiftc -disable-stack-protector -Xcc -D__MACH__ -Xcc -ffreestanding -Xcc -mcpu=cortex-m0plus -Xcc -mthumb --target Bluetooth

4 changes: 2 additions & 2 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
- name: Install Swift
uses: slashmo/[email protected]
with:
version: 5.8
version: 6.0
- name: Checkout
uses: actions/checkout@v2
- name: Swift Version
Expand All @@ -25,7 +25,7 @@ jobs:
name: Linux
strategy:
matrix:
swift: [5.6.3, 5.7.3, 5.8]
swift: [5.9, 6.0]
runs-on: ubuntu-20.04
steps:
- name: Install Swift
Expand Down
114 changes: 67 additions & 47 deletions Sources/Bluetooth/Address.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,13 @@
// Copyright © 2015 PureSwift. All rights reserved.
//

#if canImport(Foundation)
import Foundation
#endif

/// Bluetooth address.
@frozen
public struct BluetoothAddress: ByteValue, Sendable {

// MARK: - ByteValueType

/// Raw Bluetooth Address 6 byte (48 bit) value.
public typealias ByteValue = (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8)

public static var bitWidth: Int { return 48 }
public struct BluetoothAddress: Sendable {

// MARK: - Properties

Expand All @@ -44,22 +39,38 @@ public extension BluetoothAddress {
static var zero: BluetoothAddress { return .min }
}

// MARK: - Data
// MARK: - ByteValue

public extension BluetoothAddress {
extension BluetoothAddress: ByteValue {

static var length: Int { return 6 }
/// Raw Bluetooth Address 6 byte (48 bit) value.
public typealias ByteValue = (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8)

init?(data: Data) {

guard data.count == type(of: self).length
else { return nil }

self.bytes = (data[0], data[1], data[2], data[3], data[4], data[5])
public static var bitWidth: Int { return 48 }

public static var length: Int { return 6 }
}

// MARK: - Equatable

extension BluetoothAddress: Equatable {

public static func == (lhs: BluetoothAddress, rhs: BluetoothAddress) -> Bool {
return lhs.bytes.0 == rhs.bytes.0
&& lhs.bytes.1 == rhs.bytes.1
&& lhs.bytes.2 == rhs.bytes.2
&& lhs.bytes.3 == rhs.bytes.3
&& lhs.bytes.4 == rhs.bytes.4
&& lhs.bytes.5 == rhs.bytes.5
}
}

// MARK: - Hashable

extension BluetoothAddress: Hashable {

var data: Data {
return Data(self)
public func hash(into hasher: inout Hasher) {
withUnsafeBytes(of: bytes) { hasher.combine(bytes: $0) }
}
}

Expand All @@ -69,32 +80,33 @@ extension BluetoothAddress: ByteSwap {

/// A representation of this address with the byte order swapped.
public var byteSwapped: BluetoothAddress {

return BluetoothAddress(bytes: (bytes.5, bytes.4, bytes.3, bytes.2, bytes.1, bytes.0))
}
}

// MARK: - RawRepresentable

#if !hasFeature(Embedded)
extension BluetoothAddress: RawRepresentable {

/// Initialize a Bluetooth Address from its big endian string representation (e.g. `00:1A:7D:DA:71:13`).
public init?(rawValue: String) {

// verify string length
guard rawValue.utf8.count == 17
guard rawValue.count == 17
else { return nil }

var bytes: ByteValue = (0, 0, 0, 0, 0, 0)

let components = rawValue.components(separatedBy: ":")
let components = rawValue.split(whereSeparator: { $0 == ":" })

guard components.count == 6
else { return nil }

for (index, string) in components.enumerated() {

guard let byte = UInt8(string, radix: 16)
guard string.count == 2,
let byte = UInt8(string, radix: 16)
else { return nil }

withUnsafeMutablePointer(to: &bytes) {
Expand All @@ -109,44 +121,52 @@ extension BluetoothAddress: RawRepresentable {

/// Convert a Bluetooth Address to its big endian string representation (e.g. `00:1A:7D:DA:71:13`).
public var rawValue: String {

let bytes = self.bigEndian.bytes

return String(format: "%02X:%02X:%02X:%02X:%02X:%02X", bytes.0, bytes.1, bytes.2, bytes.3, bytes.4, bytes.5)
_description
}
}
#endif

// MARK: - Equatable
// MARK: - CustomStringConvertible

extension BluetoothAddress: Equatable {
extension BluetoothAddress: CustomStringConvertible {

public static func == (lhs: BluetoothAddress, rhs: BluetoothAddress) -> Bool {

return lhs.bytes.0 == rhs.bytes.0
&& lhs.bytes.1 == rhs.bytes.1
&& lhs.bytes.2 == rhs.bytes.2
&& lhs.bytes.3 == rhs.bytes.3
&& lhs.bytes.4 == rhs.bytes.4
&& lhs.bytes.5 == rhs.bytes.5
public var description: String { _description }

/// Convert a Bluetooth Address to its big endian string representation (e.g. `00:1A:7D:DA:71:13`).
internal var _description: String {
let bytes = self.bigEndian.bytes
return bytes.0.toHexadecimal()
+ ":" + bytes.1.toHexadecimal()
+ ":" + bytes.2.toHexadecimal()
+ ":" + bytes.3.toHexadecimal()
+ ":" + bytes.4.toHexadecimal()
+ ":" + bytes.5.toHexadecimal()
}
}

// MARK: - Hashable
// MARK: - Data

extension BluetoothAddress: Hashable {
#if canImport(Foundation)

public extension BluetoothAddress {

init?(data: Data) {

guard data.count == type(of: self).length
else { return nil }

self.bytes = (data[0], data[1], data[2], data[3], data[4], data[5])
}

public func hash(into hasher: inout Hasher) {
withUnsafeBytes(of: bytes) { hasher.combine(bytes: $0) }
var data: Data {
return Data(self)
}
}

// MARK: - CustomStringConvertible

extension BluetoothAddress: CustomStringConvertible {

public var description: String { return rawValue }
}
#endif

// MARK: - Codable

#if !hasFeature(Embedded)
extension BluetoothAddress: Codable { }
#endif
2 changes: 2 additions & 0 deletions Sources/Bluetooth/AsyncIndefiniteStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Created by Alsey Coleman Miller on 4/12/22.
//

#if canImport(Foundation)
import Foundation

/// Async Stream that will produce values until `stop()` is called or task is cancelled.
Expand Down Expand Up @@ -164,3 +165,4 @@ internal extension AsyncIndefiniteStream {
}
}
}
#endif
4 changes: 4 additions & 0 deletions Sources/Bluetooth/BitMaskOption.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ extension BitMaskOptionSet: Equatable {

// MARK: - CustomStringConvertible

#if !hasFeature(Embedded)
extension BitMaskOptionSet: CustomStringConvertible {

public var description: String {
Expand All @@ -138,6 +139,7 @@ extension BitMaskOptionSet: CustomStringConvertible {
.description
}
}
#endif

// MARK: - Hashable

Expand Down Expand Up @@ -177,6 +179,7 @@ extension BitMaskOptionSet: Sequence {

// MARK: - Codable

#if !hasFeature(Embedded)
extension BitMaskOptionSet: Decodable where Self.RawValue: Decodable {

public init(from decoder: Decoder) throws {
Expand All @@ -191,3 +194,4 @@ extension BitMaskOptionSet: Encodable where Self.RawValue: Encodable {
try rawValue.encode(to: encoder)
}
}
#endif
Loading

0 comments on commit 1c0fdbc

Please sign in to comment.