diff --git a/connections/clients/swift/NearbyConnections/BUILD b/connections/clients/swift/NearbyConnections/BUILD index 9096cab8..5a00940e 100644 --- a/connections/clients/swift/NearbyConnections/BUILD +++ b/connections/clients/swift/NearbyConnections/BUILD @@ -25,6 +25,7 @@ swift_library( module_name = "NearbyConnections", deps = [ "//connections/clients/swift/NearbyCoreAdapter", + "//third_party/apple_frameworks:Foundation", ], ) diff --git a/connections/clients/swift/NearbyConnections/Sources/CancellationToken.swift b/connections/clients/swift/NearbyConnections/Sources/CancellationToken.swift new file mode 100644 index 00000000..362e7ccf --- /dev/null +++ b/connections/clients/swift/NearbyConnections/Sources/CancellationToken.swift @@ -0,0 +1,30 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import NearbyCoreAdapter + +/// Token used to cancel a transfer. +public class CancellationToken { + internal let payloadID: Int64 + + internal init(_ payloadID: Int64) { + self.payloadID = payloadID + } + + /// Cancel the ongoing transfer. + public func cancel() { + // TODO(b/257824412): Handle errors from completion handler. + GNCCoreAdapter.shared.cancelPayload(payloadID) + } +} diff --git a/connections/clients/swift/NearbyConnections/Sources/ConnectionState.swift b/connections/clients/swift/NearbyConnections/Sources/ConnectionState.swift new file mode 100644 index 00000000..f5ef019c --- /dev/null +++ b/connections/clients/swift/NearbyConnections/Sources/ConnectionState.swift @@ -0,0 +1,25 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/// Indicates the current status of the connection between a local and remote endpoint. +public enum ConnectionState { + /// A connection to the remote endpoint is currently being established. + case connecting + /// The local endpoint is currently connected to the remote endpoint. + case connected + /// The remote endpoint is no longer connected. + case disconnected + /// The local or remote endpoint has rejected the connection request. + case rejected +} diff --git a/connections/clients/swift/NearbyConnections/Sources/EndpointID.swift b/connections/clients/swift/NearbyConnections/Sources/EndpointID.swift new file mode 100644 index 00000000..24bea2fb --- /dev/null +++ b/connections/clients/swift/NearbyConnections/Sources/EndpointID.swift @@ -0,0 +1,16 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/// Used to represent an enpoint. +public typealias EndpointID = String diff --git a/connections/clients/swift/NearbyConnections/Sources/PayloadID.swift b/connections/clients/swift/NearbyConnections/Sources/PayloadID.swift new file mode 100644 index 00000000..ce411c88 --- /dev/null +++ b/connections/clients/swift/NearbyConnections/Sources/PayloadID.swift @@ -0,0 +1,25 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import NearbyCoreAdapter + +/// Used to represent a payload. +public typealias PayloadID = Int64 + +extension PayloadID { + /// A convenience method for generating a unique payload ID. + public static func unique() -> PayloadID { + return GNCPayload.generateID() + } +} diff --git a/connections/clients/swift/NearbyConnections/Sources/Strategy.swift b/connections/clients/swift/NearbyConnections/Sources/Strategy.swift new file mode 100644 index 00000000..87c98d87 --- /dev/null +++ b/connections/clients/swift/NearbyConnections/Sources/Strategy.swift @@ -0,0 +1,45 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +import NearbyCoreAdapter + +/// Indicates the connection strategy and restrictions for advertisement or discovery. +/// +/// - Note: Only a subset of mediums are supported by certain strategies as depicted in the table +/// below. Generally, more restrictive strategies have better medium support. +/// +/// | | BT Classic | BLE | LAN | Aware | NFC | USB | Hotspot | Direct | 5GHz WiFi | +/// |--------------|:----------:|:---:|:---:|:-----:|:---:|:---:|:-------:|:------:|:---------:| +/// | cluster | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | × | × | × | +/// | star | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | × | +/// | pointToPoint | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | +/// +/// - Important: In order to discover another endpoint, the remote endpoint must be advertising +/// using the same strategy. +public enum Strategy { + /// No restrictions on the amount of incoming and outgoing connections. + case cluster + /// Restricts the discoverer to a single connection, while advertisers have no restrictions on + /// amount of connections. + case star + /// Restricts both adverisers and discoverers to a single connection. + case pointToPoint + + internal var objc: NearbyCoreAdapter.Strategy { + switch self { + case .cluster: return .cluster + case .star: return .star + case .pointToPoint: return .pointToPoint + } + } +} diff --git a/connections/clients/swift/NearbyConnections/Sources/TransferUpdate.swift b/connections/clients/swift/NearbyConnections/Sources/TransferUpdate.swift new file mode 100644 index 00000000..ddacd02c --- /dev/null +++ b/connections/clients/swift/NearbyConnections/Sources/TransferUpdate.swift @@ -0,0 +1,27 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import Foundation + +/// Indicates the current status of the transfer. +public enum TransferUpdate { + /// The remote endpoint has successfully received the full transfer. + case success + /// Either the local or remote endpoint has canceled the transfer. + case canceled + /// The remote endpoint failed to receive the transfer with the associated error. + case failure(Error) + /// The the transfer is currently in progress with an associated progress value. + case progress(Progress) +}