Git Product home page Git Product logo

usbdeviceswift's Introduction

USBDeviceSwift

USBDeviceSwift - is a wrapper for IOKit.usb and IOKit.hid and IOKit.serial written on pure Swift that allows you convenient work with USB devices.

Working with IOKit.usb and IOKit.hid and IOKit.serial on Swift is a pain. A lot of not converted C code, pointers make your life harder. This library provides basic connect/disconnect events, converted functions to send and receive requests and examples.

Getting Started

Requirements

  • Mac OS X 10.10
  • Xcode 8+
  • Swift 4

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects.

Specify USBDeviceSwift into your project's Podfile:

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'testusb' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!

# Pods for testusb

pod 'USBDeviceSwift'

end

Then run the following command:

$ pod install

Swift Package Manager

Swift Package Manager

import PackageDescription

let package = Package(
    name: "Example project",
    dependencies: [
        .Package(url: "https://github.com/Arti3DPlayer/USBDeviceSwift.git", majorVersion: 1),
    ]
)

Examples

You will find all examples on Wiki page here

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Change Log

This can be found in the CHANGELOG.md file.

usbdeviceswift's People

Contributors

arti3dplayer avatar dids avatar emorydunn avatar jamesbeith avatar nickoneill avatar paulz avatar ptxmac avatar robbiet480 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

usbdeviceswift's Issues

memory leak in getDeviceProperty

we are getting a lot of strings leaking from running monitor just for a few seconds:

leaks Report Version: 4.0
Process 57082: 153864 nodes malloced for 26207 KB
Process 57082: 1036 leaks for 52400 total leaked bytes.
999 (49.9K) ROOT LEAK: CFString

memory graph helped to find the location:

https://github.com/risc604/USBDeviceSwift/blob/4f9ce757a1ac6c479c3114e6043d5d4d65458c29/Sources/SerialDeviceMonitor.swift#L31

to fix we used:
return propValue?.takeRetainedValue()

same issue as in risc604/USBDeviceSwift#1

CleanFlight Serial Demo always selects the 1st device in the combo box.

I believe there is a slight bug on lines 20-21 in the ViewController here...
https://github.com/Arti3DPlayer/USBDeviceSwift/blob/master/CleanFlightSerialExample/CleanFlightSerialExample/ViewController.swift#L20-L21

Instead I think it should be

if (self.devices.count > 0 && self.devicesComboBox.indexOfSelectedItem > -1) {
                let device = self.devices[self.devicesComboBox.indexOfSelectedItem]

I'm new to Swift (and IOKit), so there may be a better way to do this.

Thanks for your great repo !

I have just come into contact with Swift and MacOS technology, and I need to develop a software for keyboard communication. Several problems about HID、USB communication bothered me for many days until I found your project. Please allow me to express my sincerest thanks. Thank you very much.

Porting PyUSB logic to USBDeviceSwift

This is more of a request for help than an actual issue, but I feel like I'm fairly lost at this point.

I'm essentially trying to figure out how to port something like this, to USBDeviceSwift/IOKit.

As you can see from the protocol link above, the requests are fairly straightforward to construct (apart from wLenDone, which I'm still confused about), but the main issue I have is how I'd handle the way PyUSB provides a write function, as the first parameter is always referred to as an endpoint, which does not sound like a request type etc.

To make things more confusing, I don't actually have a Kraken X61, but an X62, which has an entirely different protocol. That said, I'm trying to at least replicate the logic here, which is specifically for the X62, but notice how there is no request logic there at all, only write calls, so I'm stumped trying to find a way to write byte data to a specific "endpoint", using USBDeviceSwift.

Every bit of information to help me along would be massively appreciated!

BSD Path of a mounted USB device

Hi, is it possible to get the BSD Path of a mounted USB device by using / adapting this code? I've researched various interconnected stackoverflow responses:

https://stackoverflow.com/questions/40008280/usb-device-path-in-swift/46319129#46319129
https://stackoverflow.com/questions/39003986/usb-connection-delegate-on-swift/39026607#39026607
https://stackoverflow.com/questions/34628464/how-to-implement-ioservicematchingcallback-in-swift/39662693

Some posts mention BSD path. I can't see a straightforward kUSB key for it, to add it.

Currently, app gets the path by associating it with a mount notification - but that fails when a device is already connected when the app launches.

Having access to the BSD path / mount path in USBDevice would be super useful?

Thanks,

Ian Grant

kr return -536854449 error

First, Thanks to the good repo.
I use the demo to send a command to USB device.
But the kr is -536854449.
`func getStatus() throws -> [UInt8] {
//Getting device interface from our pointer
guard let deviceInterface = self.deviceInfo.deviceInterfacePtrPtr?.pointee?.pointee else {
throw STM32DeviceError.DeviceInterfaceNotFound
}

    var kr:Int32 = 0
    let length:Int = 2
    var requestPtr:[UInt8] = [UInt8](repeating: 0, count: length)
    // Creating request
    var request = IOUSBDevRequest(bmRequestType: 161,
                                  bRequest: STM32REQUEST.DNLOAD.rawValue,
                                  wValue: 0,
                                  wIndex: 0,
                                  wLength: UInt16(length),
                                  pData: &requestPtr,
                                  wLenDone: 255)
    
    kr = deviceInterface.DeviceRequest(self.deviceInfo.deviceInterfacePtrPtr, &request)
    
    if (kr != kIOReturnSuccess) {
        throw STM32DeviceError.RequestError(desc: "Get device status request error: \(kr)")
    }
    
    return requestPtr
}`

Trying to send data to device using RFControllerHIDExample

Hi,

I am trying to send some data to a mouse using the RFControllerHIDExample. I can connect to the device and I can enter some data that the application will send. However, it isn't clear for me what data the application sends. I know how to do this in C# and I have some working hexcode that controls the light in the mouse, but when I paste that piece of code or the conversion of that hexcode in the field to send it to the mouse, nothing really happens. Do I need to send some data in a selected format, or can I use whatever I want? What kind of data does the application expect from me?
Thanks in advance,
Harmen

Unable to install swift package due to conflicting requirements.

When I try to install this swift package I get the following response:

$ swift build
Updating https://github.com/Arti3DPlayer/USBDeviceSwift.git
error: dependency graph is unresolvable; found these conflicting requirements:

Dependencies: 
    https://github.com/Arti3DPlayer/USBDeviceSwift.git @ 1.0.3..<2.0.0

My package file looks like this:

// swift-tools-version:4.2
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "WiredMouse",
    dependencies: [
        .package(url: "https://github.com/Arti3DPlayer/USBDeviceSwift.git", .upToNextMajor(from: "1.0.3")),
    ]
)```

when I, instead of "1.0.3", set the version to "2.0.0" I get the same result but one step higher: 
```shell
$ swift build
Updating https://github.com/Arti3DPlayer/USBDeviceSwift.git
error: dependency graph is unresolvable; found these conflicting requirements:

Dependencies: 
    https://github.com/Arti3DPlayer/USBDeviceSwift.git @ 2.0.0..<3.0.0

And so on

Can somebody please help me get forward? I'm new to swift package manager so I don't know a lot about it.

Thanks

Send Command

Hi there,

First of all, I would like to thank you for your effort to create this library. This is absolutely helpful!

I just want to ask for your support as I don't have idea how to send this command to a USB device:

Reboot command: 0xdf 0x40

I need your help on this. Thanks so much!

Receive disconnect notification but not "connected"

Hi, can you think of any reason why I can receive the disconnection notification but not the connected notification?

There's no errors whatsoever, I simply don't get the connected notification. I've tried a few different usb devices and all the same.

Using Swift 4 in High Sierra.

How to write a command with byte array and report size 9 byte.

Hello,

We are facing some issue with the write command, as we have a Report Size 9 Byte and we are using a Byte array instead of String.

We are getting response code 0, can you please clarify the meaning of this response code?

What we require is 9 Byte in Response of write command.

Please try to help us ASAP.

THANKS!!

Request proper release tag

Hi, to keep code compatibility, please include a proper release tag in the repo.

This allows for Swift Package Manager to use the .upToNextMajor(from: "0.1.0") and related version compatibility methods.

Question : How to handle multiple devices of the same type

Hi,

loving the code. I have a question about how it can be modified to handle multiple connections of the same device.

Let's say I have two USB Mice, both are made by the same manufacturer, so both have the same vendor id and product id. When a button on mouse 1 is pressed I want to print "Hello", and when a button on the other mouse is pressed I want to print "World".

The read function receives all the data from the press of a mouse button, but I can't work out how to tie this back to a device that was detected in the rawDeviceAdded function.

It there anyway of doing this?

Thanks

How to make same request as with PyUSB write method

Hello,

Can you please help me make same request using IOUSBDevRequest as if I was using PyUSB write function? I don't know if you are familiar with PyUSB but It has very simple api just dev.write('hello') and automatically calculates bmRequestType, bmRequest do you think this library could have same api or can you tell me how should I go about setting bmRequestType and bmRequest?

I know very little about USB so I don't know if it is even possible

HID support missing in Cocoa Pod

Hi @Arti3DPlayer,

It seems that support for HID device communication isn't included in the Cocoa Pods USBDeviceSwift package. Looking in my Pods folder, I can see the USBDevice struct and the USBDeviceMonitor class, but I don't see the HIDDevice struct or HIDDeviceMonitor class or the others referenced in your examples and documentation. When trying to follow the HID example from your documentation, I receive unresolved identifier errors.

screen shot 2017-08-03 at 10 17 42 am

Ability to enable/disable the device?

Is it possible to disable/enable a device with your codebase? We have actors being filmed using laptops, and need to prevent them from actually typing or using the trackpad, but keep an external mouse/keyboard active.

Alternatively, intercepting and disposing of any events from the devices would be usable as well. Would this codebase be suitable for that?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.