Skip to content

Commit

Permalink
Add L2CAPConnection.destination
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Nov 13, 2024
1 parent c2f4ef2 commit e84cf7e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
8 changes: 5 additions & 3 deletions Sources/Bluetooth/L2CAPSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

/// L2CAP Socket protocol.
public protocol L2CAPSocket {

associatedtype Data: DataContainer


associatedtype Error: Swift.Error

/// Socket address
Expand Down Expand Up @@ -39,13 +37,17 @@ public protocol L2CAPServer: L2CAPSocket {

public protocol L2CAPConnection: L2CAPSocket {

associatedtype Data: DataContainer

/// Creates a new socket connected to the remote address specified.
static func lowEnergyClient(
address: BluetoothAddress,
destination: BluetoothAddress,
isRandom: Bool
) throws(Self.Error) -> Self

var destination: BluetoothAddress { get }

/// Write to the socket.
func send(_ data: Data) throws(Self.Error)

Expand Down
23 changes: 19 additions & 4 deletions Tests/BluetoothTests/L2CAPSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import Foundation
@testable import BluetoothGATT

internal final class TestL2CAPServer: L2CAPServer {

typealias Data = Foundation.Data


typealias Error = POSIXError

enum Cache {
Expand Down Expand Up @@ -63,6 +61,10 @@ internal final class TestL2CAPServer: L2CAPServer {
self.address = address
}

deinit {
close()
}

static func lowEnergyServer(
address: BluetoothAddress,
isRandom: Bool,
Expand All @@ -79,7 +81,11 @@ internal final class TestL2CAPServer: L2CAPServer {
guard let client = Cache.dequeue(server: address) else {
throw POSIXError(.EAGAIN)
}
let newConnection = TestL2CAPSocket(address: client.address, name: "Server connection")
let newConnection = TestL2CAPSocket(
address: client.address,
destination: self.address,
name: "Server connection"
)
// connect sockets
newConnection.connect(to: client)
client.connect(to: newConnection)
Expand All @@ -105,6 +111,7 @@ internal final class TestL2CAPSocket: L2CAPConnection {
) throws(POSIXError) -> TestL2CAPSocket {
let socket = TestL2CAPSocket(
address: address,
destination: destination,
name: "Client"
)
TestL2CAPServer.Cache.queue(client: socket, server: destination)
Expand All @@ -117,6 +124,8 @@ internal final class TestL2CAPSocket: L2CAPConnection {

let address: BluetoothAddress

let destination: Bluetooth.BluetoothAddress

var status: L2CAPSocketStatus<POSIXError> {
.init(
send: target != nil,
Expand Down Expand Up @@ -148,12 +157,18 @@ internal final class TestL2CAPSocket: L2CAPConnection {

init(
address: BluetoothAddress = .zero,
destination: Bluetooth.BluetoothAddress,
name: String
) {
self.address = address
self.destination = destination
self.name = name
}

deinit {
close()
}

// MARK: - Methods

func close() {
Expand Down

0 comments on commit e84cf7e

Please sign in to comment.