Skip to content

Commit

Permalink
Update EMV lib
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrilski committed Apr 4, 2019
1 parent 1f2e9a1 commit f358d88
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 41 deletions.
Binary file not shown.
21 changes: 14 additions & 7 deletions BeachyEMVReaderControl/BLE/BLE.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@ class BLE: NSObject {

var onBLEStateUpdate: ((_ data: String) -> Void)?
var onBLEAvailableDevicesListUpdate: ((_ devices: Set<BLEDevice>) -> Void)?


func stopScan() {
self.centralManager.stopScan()
}

func startScan() {
self.centralManager.scanForPeripherals(
withServices: nil,
options: nil)
}

override init() {
super.init()

Expand Down Expand Up @@ -43,14 +53,11 @@ extension BLE: CBCentralManagerDelegate, CBPeripheralDelegate {
advertisementData: [String : Any],
rssi RSSI: NSNumber) {
let prevCount = devices.count;

devices.insert(BLEDevice(
let device = BLEDevice(
name: peripheral.name ?? "unknown",
identifier: peripheral.identifier))
identifier: peripheral.identifier)

debugPrint("Device discovered: \(peripheral.name ?? "unknwn")")
debugPrint(" - device count: \(devices.count)")
debugPrint(" - prev Device count: \(prevCount)")
devices.insert(device)

if (devices.count != prevCount) {
onBLEAvailableDevicesListUpdate?(devices)
Expand Down
54 changes: 28 additions & 26 deletions BeachyEMVReaderControl/BeachyEMVReaderControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,30 @@ import Foundation
@objc public protocol BeachyEMVReaderControlProtocol {
func bluetoothStatusUpdate(status: String)
func bluetoothAvailableDevicesListUpdate(devices: Set<BLEDevice>)

func readerConnected()
func readerDisconnected()

func readerDataParseError(errorMessage: String)
func readerData(data: String)
func readerSendsMessage(message: String)
}

@objc open class BeachyEMVReaderControl: NSObject {

@objc open var delegate: BeachyEMVReaderControlProtocol?
@objc public static var shared = BeachyEMVReaderControl()

private var bluetoothControl = BLE()
private var emvDeviceControl = EmvDevice()

override init() {
super.init()

initializeBluetooth()
initializeEmv()
}

/// Configure EMV sleep and power off times
/// - Parameters:
/// - sleepTimeInSec: sleep time in seconds
Expand All @@ -47,12 +47,12 @@ import Foundation
return emvDeviceControl.setReaderSleepAndPowerOffTime(
sleepTimeInSec: sleepTimeInSec,
powerOffTimeInSec: powerOffTimeInSec)
}

}
@objc open func cancelReadCardData() -> Void {
emvDeviceControl.cancelTransaction()
}

/// Send a command to EMV reader to become active and
/// start waiting for swipe/contactless payment
/// - Parameters:
Expand All @@ -66,7 +66,7 @@ import Foundation
timeout: Int32 = 60) -> Int {
do {
try emvDeviceControl.readCC(amount, timeout: timeout)

return 0
} catch EmvError.cannotStartTransaction( _) {
return 1
Expand All @@ -76,24 +76,24 @@ import Foundation
return 3
}
}

/// Connect to nearest BLE Reader that matches
/// set friendly name.
/// - Parameter friendlyName: device friendly name, like IDT_*
/// - Returns: true if connecting
@objc open func connect(friendlyName: String) -> Bool {
return emvDeviceControl.connect(friendlyName: friendlyName)
}

/// Connect to BLE Reader using UUID.
///
/// - Parameter uuid: device UUID
/// - Returns: true if connecting
@objc open func connect(uuid: UUID) -> Bool {
@objc open func connect(uuid: UUID) -> Bool {
return emvDeviceControl.connect(uuid: uuid)
}


/// Initialize low-energy bluetooth handlers
private func initializeBluetooth() {
bluetoothControl.onBLEStateUpdate = {
Expand All @@ -102,7 +102,7 @@ import Foundation
.delegate?
.bluetoothStatusUpdate(status: message)
}

bluetoothControl.onBLEAvailableDevicesListUpdate = {
[weak self] (devices: Set<BLEDevice>) in
self?
Expand All @@ -111,38 +111,40 @@ import Foundation
devices: devices)
}
}

/// Initialize EMV handlers
private func initializeEmv() {
emvDeviceControl.onEmvConnected = {
[weak self] () in self?.delegate?.readerConnected()
self?.bluetoothControl.stopScan()
}

emvDeviceControl.onEmvDisconnected = {
[weak self] () in self?.delegate?.readerDisconnected()
self?.bluetoothControl.startScan()
}

emvDeviceControl.onEmvDataParseError = {
[weak self] (error: String) in
self?
.delegate?
.readerDataParseError(errorMessage: error)
self?
.delegate?
.readerDataParseError(errorMessage: error)
}

emvDeviceControl.onEmvTimeout = {
[weak self] () in
self?
.delegate?
.readerDataParseError(errorMessage: "Timed out")
}

emvDeviceControl.onEmvSendMessage = {
[weak self] (message: String) in
self?
.delegate?
.readerSendsMessage(message: message)
}

emvDeviceControl.onEmvDataReceived = {
[weak self] (data: String) in
self?
Expand Down
16 changes: 8 additions & 8 deletions BeachyEMVReaderControl/EMVReader/EMVDevice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

import Foundation
import IDTPrivate

enum EmvError: Error {
case deviceIsNotConnected
Expand Down Expand Up @@ -59,7 +58,7 @@ class EmvDevice: NSObject {
.device_cancelTransaction()
}

/// Enable Transaction Request
/// Enable Transaction Request
/// Enables CLTS and MSR, waiting for swipe or tap to occur.
/// Returns IDTEMVData to deviceDelegate::emvTransactionData:()
///
Expand Down Expand Up @@ -100,9 +99,10 @@ class EmvDevice: NSObject {
}

func connect(friendlyName: String) -> Bool {
IDT_VP3300
.sharedController()
.device_disableBLEDeviceSearch()

if IDT_VP3300.sharedController()?.isConnected() ?? false {
return true
}

IDT_VP3300
.sharedController()
Expand All @@ -114,9 +114,9 @@ class EmvDevice: NSObject {
}

func connect(uuid: UUID) -> Bool {
IDT_VP3300
.sharedController()
.device_disableBLEDeviceSearch()
if IDT_VP3300.sharedController()?.isConnected() ?? false {
return true
}

return IDT_VP3300
.sharedController()
Expand Down

0 comments on commit f358d88

Please sign in to comment.