0

I have a usb device that will not use SERIAL or HID device communication. It is in DFU mode. How do i can detect it for beginning ? I read a lot of articles and look on examples but they not helped for me.

As i understand i should use IOKit.usb library for this? It will be great if someone will show me how do i can detect this device:(

enter image description here

Arti
  • 7,356
  • 12
  • 57
  • 122
  • This could give you ideas, I guess: http://stackoverflow.com/a/39026607/2227743 – Eric Aya Dec 07 '16 at 14:06
  • @EricAya thank you. I wrote some code: http://pastebin.com/Gj0Hhtih but this not working. Could you provide some suggestions. Why it doesn't detecting my device :( – Arti Dec 07 '16 at 16:01
  • I have created [USBDeviceSwift](https://github.com/Arti3DPlayer/USBDeviceSwift) library for convenient work with `IOKit.usb` and `IOKit.hid` – Arti Jun 29 '17 at 10:32

1 Answers1

3

Thanks for answers, i build my own solution:

class DFUDevice: NSObject {
    let vendorId = 0x0483
    let productId = 0xdf11

    static let sharedInstance = DFUDevice()

    private func reloadMonitor(iterator:io_iterator_t) {
        repeat {
            let nextService = IOIteratorNext(iterator)
            guard nextService != 0 else { break }
            IOObjectRelease(nextService)

        } while (true)
    }

    func connected(iterator:io_iterator_t) {
        self.reloadMonitor(iterator: iterator)
    }

    func disconnected(iterator:io_iterator_t) {
        self.reloadMonitor(iterator: iterator)
    }

    func initUsb() {
        var matchedIterator:io_iterator_t = 0
        var removalIterator:io_iterator_t = 0
        let notifyPort:IONotificationPortRef = IONotificationPortCreate(kIOMasterPortDefault)
        IONotificationPortSetDispatchQueue(notifyPort, DispatchQueue(label: "IODetector"))

        let matchingDict = IOServiceMatching(kIOUSBDeviceClassName)
            as NSMutableDictionary
        matchingDict[kUSBVendorID] = NSNumber(value: self.vendorId)
        matchingDict[kUSBProductID] = NSNumber(value: self.productId)

        let matchingCallback:IOServiceMatchingCallback = { (userData, iterator) in
            let this = Unmanaged<DFUDevice>
                .fromOpaque(userData!).takeUnretainedValue()
            this.connected(iterator: iterator)
        }

        let removalCallback: IOServiceMatchingCallback = {
            (userData, iterator) in
            let this = Unmanaged<DFUDevice>
                .fromOpaque(userData!).takeUnretainedValue()
            this.disconnected(iterator: iterator)
        };

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

        IOServiceAddMatchingNotification(notifyPort, kIOFirstMatchNotification, matchingDict, matchingCallback, selfPtr, &matchedIterator)
        IOServiceAddMatchingNotification(notifyPort, kIOTerminatedNotification, matchingDict, removalCallback, selfPtr, &removalIterator)

        if matchedIterator != 0 {
            self.connected(iterator: matchedIterator)
            matchedIterator = 0
        }

        if removalIterator != 0 {
            self.reloadMonitor(iterator: removalIterator)
            removalIterator = 0
        }

        self.reloadMonitor(iterator: matchedIterator)
        self.reloadMonitor(iterator: removalIterator)

        RunLoop.current.run();
    }
}

To run it:

let DFUDeviceDaemon = Thread(target: DFUDevice.sharedInstance, selector:#selector(DFUDevice.initUsb), object: nil)
            DFUDeviceDaemon.start()
Arti
  • 7,356
  • 12
  • 57
  • 122
  • Interesting. I guess I could extrapolate over this version in order to udpate back http://stackoverflow.com/a/39026607/2227743 in Swift 3, which is giving me a hard time. :) – Eric Aya Dec 07 '16 at 21:09
  • @EricAya now could you help me how do i can send data to this device:) – Arti Dec 08 '16 at 10:52
  • http://stackoverflow.com/questions/41038150/usb-device-send-receive-data – Arti Dec 08 '16 at 13:14
  • I have created [USBDeviceSwift](https://github.com/Arti3DPlayer/USBDeviceSwift) library for convenient work with `IOKit.usb` and `IOKit.hid` – Arti Jun 29 '17 at 10:32
  • I'm getting "Cannot convert value of type '(DFUDevice) -> () -> ()' to expected argument type 'Selector". Any idea? – Number1 Oct 21 '18 at 01:32
  • @Number1 did you try lib ? – Arti Oct 21 '18 at 09:28