Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions Examples/OCADevice/DeviceApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public enum DeviceApp {
public static func main() async throws {
var listenAddress = sockaddr_in()
listenAddress.sin_family = sa_family_t(AF_INET)
listenAddress.sin_addr.s_addr = 0 // INADDR_ANY equivalent
listenAddress.sin_addr.s_addr = 0 // INADDR_ANY equivalent
listenAddress.sin_port = port.bigEndian
#if canImport(Darwin) || os(FreeBSD) || os(OpenBSD)
listenAddress.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)
Expand All @@ -61,7 +61,8 @@ public enum DeviceApp {
}
let delegate = DeviceEventDelegate()
await device.setEventDelegate(delegate)
#if os(Linux)

#if os(Linux) && NonEmbeddedBuild
let streamEndpoint = try await Ocp1IORingStreamDeviceEndpoint(address: listenAddress.data)
let datagramEndpoint = try await Ocp1IORingDatagramDeviceEndpoint(address: listenAddress.data)
let stream6Endpoint = try await Ocp1IORingStreamDeviceEndpoint(address: listen6Address.data)
Expand All @@ -70,7 +71,7 @@ public enum DeviceApp {
try? await Ocp1IORingStreamDeviceEndpoint(path: "/tmp/oca-device.sock")
let domainSocketDatagramEndpoint =
try? await Ocp1IORingDatagramDeviceEndpoint(path: "/tmp/oca-device-dg.sock")
#elseif canImport(FlyingSocks)
#elseif canImport(FlyingSocks) && NonEmbeddedBuild
let streamEndpoint = try await Ocp1FlyingSocksStreamDeviceEndpoint(address: listenAddress.data)
let datagramEndpoint =
try await Ocp1FlyingSocksDatagramDeviceEndpoint(address: listenAddress.data)
Expand All @@ -87,9 +88,10 @@ public enum DeviceApp {
#else
let streamEndpoint = try await Ocp1StreamDeviceEndpoint(address: listenAddress.data)
#endif
#if canImport(FlyingSocks)

#if canImport(FlyingFox) && NonEmbeddedBuild
listenAddress.sin_family = sa_family_t(AF_INET)
listenAddress.sin_addr.s_addr = 0 // INADDR_ANY equivalent
listenAddress.sin_addr.s_addr = 0 // INADDR_ANY equivalent
listenAddress.sin_port = (port + 2).bigEndian
#if canImport(Darwin) || os(FreeBSD) || os(OpenBSD)
listenAddress.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)
Expand Down Expand Up @@ -132,7 +134,9 @@ public enum DeviceApp {
)
try await block.add(actionObject: gain)

#if NonEmbeddedBuild
try await serializeDeserialize(device.rootBlock)
#endif

let controlNetwork = try await SwiftOCADevice.OcaControlNetwork(deviceDelegate: device)
Task { @OcaDevice in controlNetwork.state = .running }
Expand All @@ -145,6 +149,7 @@ public enum DeviceApp {
}
}

#if NonEmbeddedBuild
try await withThrowingTaskGroup(of: Void.self) { taskGroup in
taskGroup.addTask {
print("Starting OCP.1 IPv4 stream endpoint \(streamEndpoint)...")
Expand Down Expand Up @@ -186,9 +191,14 @@ public enum DeviceApp {
#endif
try await taskGroup.next()
}
#else
print("Starting OCP.1 IPv4 stream endpoint \(streamEndpoint)...")
try await streamEndpoint.run()
#endif
}
}

#if NonEmbeddedBuild
func serializeDeserialize(
_ object: SwiftOCADevice
.OcaBlock<SwiftOCADevice.OcaRoot>
Expand All @@ -203,3 +213,4 @@ func serializeDeserialize(
debugPrint("serialization error: \(error)")
}
}
#endif
56 changes: 33 additions & 23 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,62 +15,64 @@ if EnableASAN {
ASANLinkerSettings.append(LinkerSetting.linkedLibrary("asan"))
}

let PlatformPackageDependencies: [Package.Dependency]
let PlatformTargetDependencies: [Target.Dependency]
var PlatformPackageDependencies: [Package.Dependency] = []
var PlatformTargetDependencies: [Target.Dependency] = []
let PlatformProducts: [Product]
let PlatformTargets: [Target]
let SwiftLanguageVersionSetting: [SwiftSetting]

PlatformPackageDependencies += [
.package(url: "https://github.com/PADL/FlyingFox", branch: "main"),
]

PlatformTargetDependencies += [
.product(
name: "FlyingSocks",
package: "FlyingFox"
),
.product(
name: "FlyingFox",
package: "FlyingFox",
condition: .when(traits: ["NonEmbeddedBuild"])
),
]

#if os(Linux)
PlatformPackageDependencies = [.package(url: "https://github.com/PADL/IORingSwift", from: "0.9.2")]
PlatformPackageDependencies += [.package(url: "https://github.com/PADL/IORingSwift", from: "0.9.2")]

PlatformTargetDependencies = [
PlatformTargetDependencies += [
.target(
name: "dnssd",
condition: .when(platforms: [.linux])
),
.product(
name: "IORing",
package: "IORingSwift",
condition: .when(platforms: [.linux])
condition: .when(platforms: [.linux], traits: ["NonEmbeddedBuild"])
),
.product(
name: "IORingUtils",
package: "IORingSwift",
condition: .when(platforms: [.linux])
condition: .when(platforms: [.linux], traits: ["NonEmbeddedBuild"])
),
.product(
name: "IORingFoundation",
package: "IORingSwift",
condition: .when(platforms: [.linux])
condition: .when(platforms: [.linux], traits: ["NonEmbeddedBuild"])
),
]

PlatformProducts = []
PlatformTargets = []
SwiftLanguageVersionSetting = []
#elseif os(macOS) || os(iOS)
PlatformPackageDependencies = [
.package(url: "https://github.com/swhitty/FlyingFox", from: "0.20.0"),
PlatformPackageDependencies += [
.package(
url: "https://github.com/spacenation/swiftui-sliders",
from: "2.1.0"
),
]

PlatformTargetDependencies = [
.product(
name: "FlyingFox",
package: "FlyingFox",
condition: .when(platforms: [.macOS, .iOS, .android])
),
.product(
name: "FlyingSocks",
package: "FlyingFox",
condition: .when(platforms: [.macOS, .iOS, .android])
),
]

PlatformProducts = [
.library(
name: "SwiftOCAUI",
Expand Down Expand Up @@ -147,7 +149,11 @@ let CommonTargets: [Target] = [
name: "SwiftOCA",
dependencies: [
"AsyncExtensions",
"AnyCodable",
.product(
name: "AnyCodable",
package: "AnyCodable",
condition: .when(traits: ["NonEmbeddedBuild"])
),
"SocketAddress",
.product(name: "AsyncAlgorithms", package: "swift-async-algorithms"),
.product(name: "SystemPackage", package: "swift-system"),
Expand Down Expand Up @@ -233,6 +239,10 @@ let package = Package(
.iOS(.v17),
],
products: CommonProducts + PlatformProducts,
traits: [
.default(enabledTraits: ["NonEmbeddedBuild"]),
.init(name: "NonEmbeddedBuild", description: "Default build footprint"),
],
dependencies: CommonPackageDependencies + PlatformPackageDependencies,
targets: CommonTargets + PlatformTargets
)
2 changes: 2 additions & 0 deletions Sources/SwiftOCA/OCC/ControlClasses/Root+JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.
//

#if NonEmbeddedBuild
import AnyCodable
#if canImport(FoundationEssentials)
import FoundationEssentials
Expand All @@ -37,3 +38,4 @@ enum OcaJSONPropertyKeys: String {
case type
case members
}
#endif
4 changes: 4 additions & 0 deletions Sources/SwiftOCA/OCC/ControlClasses/Root.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ open class OcaRoot: CustomStringConvertible, @unchecked Sendable, _OcaObjectKeyP
}
}

#if NonEmbeddedBuild
open func getJsonValue(
flags: OcaPropertyResolutionFlags = .defaultFlags
) async -> [String: any Sendable] {
Expand Down Expand Up @@ -178,6 +179,7 @@ open class OcaRoot: CustomStringConvertible, @unchecked Sendable, _OcaObjectKeyP
await getJsonValue(flags: .defaultFlags)
}
}
#endif

public func propertyKeyPath(for propertyID: OcaPropertyID) async -> AnyKeyPath? {
await OcaPropertyKeyPathCache.shared.lookupProperty(byID: propertyID, for: self)
Expand Down Expand Up @@ -335,13 +337,15 @@ public extension OcaRoot {
value
}

#if NonEmbeddedBuild
func getJsonValue(
_ object: OcaRoot,
keyPath: AnyKeyPath,
flags: OcaPropertyResolutionFlags = .defaultFlags
) async throws -> [String: any Sendable] {
try [keyPath.jsonKey: String(describing: value)]
}
#endif

@_spi(SwiftOCAPrivate)
public func _setValue(_ object: OcaRoot, _ anyValue: Any) async throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ Sendable {
true
}

#if NonEmbeddedBuild
override open func getJsonValue(
flags: OcaPropertyResolutionFlags = .defaultFlags
) async -> [String: any Sendable] {
Expand All @@ -516,6 +517,7 @@ Sendable {
.asyncMap { await $0.getJsonValue(flags: flags) }
return jsonObject
}
#endif
}

public extension OcaBlock {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ Sendable {
true
}

#if NonEmbeddedBuild
override open func getJsonValue(
flags: OcaPropertyResolutionFlags = .defaultFlags
) async -> [String: any Sendable] {
Expand All @@ -113,6 +114,7 @@ Sendable {
.reencodeAsValidJSONObject(membersJson)
return jsonObject
}
#endif
}

public extension OcaMatrix {
Expand Down
2 changes: 2 additions & 0 deletions Sources/SwiftOCA/OCC/PropertyTypes/BoundedProperty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ public struct OcaBoundedProperty<
try await _storage._getValue(object, flags: flags)
}

#if NonEmbeddedBuild
public func getJsonValue(
_ object: OcaRoot,
keyPath: AnyKeyPath,
Expand All @@ -204,6 +205,7 @@ public struct OcaBoundedProperty<
"\(jsonKey)": value.value,
]
}
#endif

@_spi(SwiftOCAPrivate)
public func _setValue(_ object: OcaRoot, _ anyValue: Any) async throws {
Expand Down
17 changes: 16 additions & 1 deletion Sources/SwiftOCA/OCC/PropertyTypes/Property.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (c) 2023 PADL Software Pty Ltd
// Copyright (c) 2023-2026 PADL Software Pty Ltd
//
// Licensed under the Apache License, Version 2.0 (the License);
// you may not use this file except in compliance with the License.
Expand All @@ -16,7 +16,11 @@

import AsyncAlgorithms
import AsyncExtensions
#if NonEmbeddedBuild || !canImport(FoundationEssentials)
import Foundation
#else
import FoundationEssentials
#endif

public struct OcaPropertyResolutionFlags: OptionSet, Sendable {
public typealias RawValue = UInt32
Expand Down Expand Up @@ -56,12 +60,14 @@ public protocol OcaPropertyRepresentable: CustomStringConvertible {
func refresh(_ object: OcaRoot) async
func subscribe(_ object: OcaRoot) async

#if NonEmbeddedBuild
func getJsonValue(
_ object: OcaRoot,
keyPath: AnyKeyPath,
flags: OcaPropertyResolutionFlags
) async throws
-> [String: any Sendable]
#endif
}

public extension OcaPropertyRepresentable {
Expand Down Expand Up @@ -432,14 +438,20 @@ public struct OcaProperty<Value: Codable & Sendable>: Codable, Sendable,
}
}

#if NonEmbeddedBuild
public func getJsonValue(
_ object: OcaRoot,
keyPath: AnyKeyPath,
flags: OcaPropertyResolutionFlags = .defaultFlags
) async throws -> [String: any Sendable] {
let value = try await _getValue(object, flags: flags)
let jsonValue: any Sendable = if isNil(value) {
#if canImport(Foundation) && !canImport(FoundationEssentials)
NSNull()
#else
(any Sendable)?.none
#endif

} else if JSONSerialization.isValidJSONObject(value) {
value
} else {
Expand All @@ -448,6 +460,7 @@ public struct OcaProperty<Value: Codable & Sendable>: Codable, Sendable,

return try [keyPath.jsonKey: jsonValue]
}
#endif

@_spi(SwiftOCAPrivate)
public func _setValue(_ object: OcaRoot, _ anyValue: Any) async throws {
Expand Down Expand Up @@ -486,6 +499,7 @@ extension OcaProperty.PropertyValue: Hashable where Value: Hashable & Codable {
}
}

#if NonEmbeddedBuild
extension AnyKeyPath {
var jsonKey: String {
get throws {
Expand All @@ -510,3 +524,4 @@ extension AnyKeyPath {
}
}
}
#endif
2 changes: 2 additions & 0 deletions Sources/SwiftOCA/OCC/PropertyTypes/VectorProperty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ public struct OcaVectorProperty<
try await _storage._getValue(object, flags: flags)
}

#if NonEmbeddedBuild
public func getJsonValue(
_ object: OcaRoot,
keyPath: AnyKeyPath,
Expand All @@ -173,6 +174,7 @@ public struct OcaVectorProperty<
let value = try await _getValue(object, flags: flags)
return try [keyPath.jsonKey: [value.x, value.y]]
}
#endif

@_spi(SwiftOCAPrivate)
public func _setValue(_ object: OcaRoot, _ anyValue: Any) async throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// limitations under the License.
//

#if canImport(CoreFoundation)
#if canImport(CoreFoundation) && NonEmbeddedBuild

import AsyncAlgorithms
import AsyncExtensions
Expand Down
Loading
Loading