Skip to content

Commit

Permalink
Merge branch 'NonBlockingL2CAPSocket'
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Sep 12, 2018
2 parents de0eb50 + 464c023 commit c8d25fb
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 21 deletions.
19 changes: 11 additions & 8 deletions Sources/ATTConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ public final class ATTConnection {
// MARK: - Methods

/// Performs the actual IO for recieving data.
public func read() throws {
public func read() throws -> Bool {

log?("Attempt read")
//log?("Attempt read")

let recievedData = try socket.recieve(Int(maximumTransmissionUnit.rawValue))
guard let recievedData = try socket.recieve(Int(maximumTransmissionUnit.rawValue))
else { return false } // no data availible to read

log?("Recieved data (\(recievedData.count) bytes)")
//log?("Recieved data (\(recievedData.count) bytes)")

// valid PDU data length
guard recievedData.count >= ATT.minimumPDULength
Expand All @@ -82,7 +83,7 @@ public final class ATTConnection {
guard let opcode = ATT.Opcode(rawValue: opcodeByte)
else { throw Error.garbageResponse(recievedData) }

log?("Recieved opcode \(opcode)")
//log?("Recieved opcode \(opcode)")

// Act on the received PDU based on the opcode type
switch opcode.type {
Expand All @@ -106,25 +107,27 @@ public final class ATTConnection {
// For all other opcodes notify the upper layer of the PDU and let them act on it.
try handle(notify: recievedData, opcode: opcode)
}

return true
}

/// Performs the actual IO for sending data.
public func write() throws -> Bool {

log?("Attempt write")
//log?("Attempt write")

guard let sendOperation = pickNextSendOpcode()
else { return false }

assert(sendOperation.data.count <= Int(maximumTransmissionUnit.rawValue), "Trying to send \(sendOperation.data.count) bytes when MTU is \(maximumTransmissionUnit)")

log?("Sending data... (\(sendOperation.data.count) bytes)")
//log?("Sending data... (\(sendOperation.data.count) bytes)")

try socket.send(sendOperation.data)

let opcode = sendOperation.opcode

log?("Did write \(opcode)")
//log?("Did write \(opcode)")

/* Based on the operation type, set either the pending request or the
* pending indication. If it came from the write queue, then there is
Expand Down
8 changes: 4 additions & 4 deletions Sources/GATTClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ public final class GATTClient {
// MARK: - Methods

/// Performs the actual IO for recieving data.
public func read() throws {
public func read() throws -> Bool {

try connection.read()
return try connection.read()
}

/// Performs the actual IO for sending data.
Expand Down Expand Up @@ -346,8 +346,8 @@ public final class GATTClient {
private func registerATTHandlers() {

// value notifications / indications
connection.register(notification)
connection.register(indication)
connection.register { [weak self] in self?.notification($0) }
connection.register { [weak self] in self?.indication($0) }
}

@inline(__always)
Expand Down
4 changes: 2 additions & 2 deletions Sources/GATTServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public final class GATTServer {
// MARK: - Methods

/// Performs the actual IO for sending data.
public func read() throws {
public func read() throws -> Bool {

try connection.read()
return try connection.read()
}

/// Performs the actual IO for recieving data.
Expand Down
2 changes: 1 addition & 1 deletion Sources/L2CAPSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public protocol L2CAPSocketProtocol {
var channelIdentifier: UInt16 { get }

/// Reads from the socket.
func recieve(_ bufferSize: Int) throws -> Data
func recieve(_ bufferSize: Int) throws -> Data?

/// Write to the socket.
func send(_ data: Data) throws
Expand Down
12 changes: 8 additions & 4 deletions Tests/BluetoothTests/GATTTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,8 @@ final class GATTTests: XCTestCase {
server.writePending = {
do {
while try server.write() {
try client.read()
let didRead = try client.read()
XCTAssert(didRead)
}
}
catch { XCTFail("Error: \(error)") }
Expand All @@ -661,7 +662,8 @@ final class GATTTests: XCTestCase {
client.writePending = {
do {
while try client.write() {
try server.read()
let didRead = try server.read()
XCTAssert(didRead)
}
}
catch { XCTFail("Error: \(error)") }
Expand Down Expand Up @@ -1022,7 +1024,8 @@ extension GATTTests {

while server.socket.input.isEmpty == false {

try server.gatt.read()
let didRead = try server.gatt.read()
assert(didRead)
}

while try server.gatt.write() {
Expand All @@ -1032,7 +1035,8 @@ extension GATTTests {

while client.socket.input.isEmpty == false {

try client.gatt.read()
let didRead = try client.gatt.read()
assert(didRead)
}

} while didWrite
Expand Down
5 changes: 3 additions & 2 deletions Tests/BluetoothTests/L2CAPSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ internal final class TestL2CAPSocket: L2CAPSocketProtocol {
}

/// Reads from the socket.
func recieve(_ bufferSize: Int) throws -> Data {
@discardableResult
func recieve(_ bufferSize: Int) throws -> Data? {

guard let sentData = self.input.popFirst()
else { throw POSIXError(code: .EBUSY) }
else { return nil }

let readData = Data(sentData.prefix(bufferSize))

Expand Down

0 comments on commit c8d25fb

Please sign in to comment.