UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to use the resulting callback swift code

Forums > Swift

For this example, the result is returned: Event Mached or Event Terminated

I want to add my command if the result is Event Mached how do I do it Tks every body!!

import Darwin import IOKit import IOKit.usb import Foundation

class IOUSBDetector {

enum Event {
    case Matched
    case Terminated
}

let vendorID: Int
let productID: Int

var callbackQueue: DispatchQueue?

var callback: (
    ( _ detector: IOUSBDetector,  _ event: Event,
        _ service: io_service_t
    ) -> Void
)?

private
let internalQueue: DispatchQueue

private
let notifyPort: IONotificationPortRef

private
var matchedIterator: io_iterator_t = 0

private
var terminatedIterator: io_iterator_t = 0

private
func dispatchEvent (
    event: Event, iterator: io_iterator_t
) {
    repeat {
        let nextService = IOIteratorNext(iterator)
        guard nextService != 0 else { break }
        if let cb = self.callback, let q = self.callbackQueue {
            q.async {
                cb(self, event, nextService)
                IOObjectRelease(nextService)
            }
        } else {
            IOObjectRelease(nextService)
        }
    } while (true)
}

init? ( vendorID: Int, productID: Int ) {
    self.vendorID = vendorID
    self.productID = productID
    self.internalQueue = DispatchQueue(label: "IODetector")

    guard let notifyPort = IONotificationPortCreate(kIOMasterPortDefault) else {
        return nil
    }

    self.notifyPort = notifyPort
    IONotificationPortSetDispatchQueue(notifyPort, self.internalQueue)
}

deinit {
    self.stopDetection()
}

func startDetection ( ) -> Bool {
    guard matchedIterator == 0 else { return true }

    let matchingDict = IOServiceMatching(kIOUSBDeviceClassName)
        as NSMutableDictionary
    matchingDict[kUSBVendorID] = NSNumber(value: vendorID)
    matchingDict[kUSBProductID] = NSNumber(value: productID)

    let matchCallback: IOServiceMatchingCallback = {
        (userData, iterator) in
            let detector = Unmanaged<IOUSBDetector>
                .fromOpaque(userData!).takeUnretainedValue()
            detector.dispatchEvent(
                event: .Matched, iterator: iterator
            )
    };
    let termCallback: IOServiceMatchingCallback = {
        (userData, iterator) in
            let detector = Unmanaged<IOUSBDetector>
                .fromOpaque(userData!).takeUnretainedValue()
            detector.dispatchEvent(
                event: .Terminated, iterator: iterator
            )
    };

    let selfPtr = Unmanaged.passUnretained(self).toOpaque()

    let addMatchError = IOServiceAddMatchingNotification(
        self.notifyPort, kIOFirstMatchNotification,
        matchingDict, matchCallback, selfPtr, &self.matchedIterator
    )
    let addTermError = IOServiceAddMatchingNotification(
        self.notifyPort, kIOTerminatedNotification,
        matchingDict, termCallback, selfPtr, &self.terminatedIterator
    )

    guard addMatchError == 0 && addTermError == 0 else {
        if self.matchedIterator != 0 {
            IOObjectRelease(self.matchedIterator)
            self.matchedIterator = 0
        }
        if self.terminatedIterator != 0 {
            IOObjectRelease(self.terminatedIterator)
            self.terminatedIterator = 0
        }
        return false
    }

    // This is required even if nothing was found to "arm" the callback
    self.dispatchEvent(event: .Matched, iterator: self.matchedIterator)
    self.dispatchEvent(event: .Terminated, iterator: self.terminatedIterator)

    return true
}

func stopDetection ( ) {
    guard self.matchedIterator != 0 else { return }
    IOObjectRelease(self.matchedIterator)
    IOObjectRelease(self.terminatedIterator)
    self.matchedIterator = 0
    self.terminatedIterator = 0
}

}

let test = IOUSBDetector(vendorID: 0x05ac, productID: 0x12a8) test?.callbackQueue = DispatchQueue.global() test?.callback = { (detector, event, service) in print("Event (event)") }; _ = test?.startDetection() while true { sleep(1) }

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.