Arti3DPlayer / USBDeviceSwift

wrapper for IOKit.usb and IOKit.hid written on pure Swift that allows you convenient work with USB devices
MIT License
290 stars 55 forks source link

How to make same request as with PyUSB write method #7

Closed kolpav closed 6 years ago

kolpav commented 6 years ago

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

Arti3DPlayer commented 6 years ago

I guess you should take a look on this question: https://github.com/Arti3DPlayer/USBDeviceSwift/issues/5

You could write a class like I did for https://github.com/Arti3DPlayer/USBDeviceSwift/blob/master/STM32DeviceExample/STM32DeviceExample/STM32Device.swift

kolpav commented 6 years ago

Sorry I missed that issue because it is exactly what I am trying to achieve I played with STM32DeviceExample and also read through PyUSB code but I am still unable to replicate the write behaviour as I am new to swift and python.

This is modified example I am working with

enum STM32REQUEST_TYPE_DIRECTION:UInt8 {
    case USB_SETUP_HOST_TO_DEVICE = 0x00
    case USB_SETUP_DEVICE_TO_HOST = 0x80     
}
enum STM32REQUEST_TYPE_TYPE:UInt8 {
    case USB_SETUP_TYPE_STANDARD = 0x00      
    case USB_SETUP_TYPE_CLASS = 0x20        
    case USB_SETUP_TYPE_VENDOR = 0x40         
}
enum STM32REQUEST_TYPE_RECIPIENT:UInt8 {
    case USB_SETUP_RECIPIENT_DEVICE = 0x00    
    case USB_SETUP_RECIPIENT_INTERFACE = 0x01 
    case USB_SETUP_RECIPIENT_ENDPOINT = 0x02 
    case USB_SETUP_RECIPIENT_OTHER = 0x03     
}
        var kr:Int32 = 0
        var requestPtr:[UInt8] = [1, 2, 3];
        let bmRequest = STM32REQUEST.GETSTATUS.rawValue;
        let bmRequestType:UInt8 =
            STM32REQUEST_TYPE_DIRECTION.USB_SETUP_HOST_TO_DEVICE.rawValue |
            STM32REQUEST_TYPE_TYPE.USB_SETUP_TYPE_VENDOR.rawValue |
            STM32REQUEST_TYPE_RECIPIENT.USB_SETUP_RECIPIENT_DEVICE.rawValue;
        // Creating request
        var request = IOUSBDevRequest(bmRequestType: bmRequestType,
                                      bRequest: bmRequest,
                                      wValue: 0,
                                      wIndex: 0,
                                      wLength: UInt16(requestPtr.count),
                                      pData: &requestPtr,
                                      wLenDone: 255)

You were able to calculate bmRequestType value in your head here you just typed 161 that suggests you understand how it works can you please tell me what I am doing wrong? Or how it is possible to automatically calculate bmRequestType and bRequest? I looked at how the PyUSB library is doing it but I still don't understand.

Arti3DPlayer commented 6 years ago

Oh, yes you are right. I didn't calculate it with function, that implement here:

https://github.com/Arti3DPlayer/USBDeviceSwift/blob/master/Sources/USBDevice.swift#L33

So I received 161, when pass params to this function.

request.bmRequestType = USBmakebmRequestType(kUSBOut, kUSBVendor,
kUSBDevice);

https://developer.apple.com/library/content/documentation/DeviceDrivers/Conceptual/USBBook/USBDeviceInterfaces/USBDevInterfaces.html

Check Listing 2-6 Two functions to download firmware to the raw device

Arti3DPlayer commented 6 years ago

So you need to mix values based on what you want to do to get requestType