Windows form application for USB port - c++

Previously I have work with Windows Form application to establish some RS232 connection. I used the already provided serial port component (SerialPort), and I was able to establish RS232 communication relatively easy.
Now, I was wondering if there will be something similar in Winodows Form application to establish a USB communication ?
It seems there is this WinUSB API that provides a very low level interfacing with the device.
However, I am not sure how easy will that be? Also, not sure how easy will it be to integrate into Windows Form application ?!
Will there be a simpler version of such USB interface API?
I don't have to stick to Visual Studio. Is there other c++ USB API, besides WinUSB, that is more standard that people use? I would like to develop a GUI API that does some communication over USB. If need be, I can use Python or some other tools if it facilitates the process?
Thanks in advance.

Although USB is a serial protocol, you can't treat USB like a serial port:
It's dependant on what the actual device is. For example a mobile phone, may provide several "endpoints" for USB, one being a serial port to use the phone as a modem, one as a storage device allowing you to transfer photos and music files to/from the phones storage, and as a camera device that you can take photos with. All of these have different behaviour and need a USB driver-plugin to make it behave correctly - these are typically shipped with Windows, and your phone will appear as COM5:, the E: or "Samsung Galaxy S3 Mini" drives and as a camera under the "cameras and scanners".
Of course, you can programmatically open all these devices, but it is done as the device-type that they present as on the inside of windows (so you use serial port functions or file functions or camera functions).
You CAN also write a device driver for a device, if you have sufficient details of how it works.
But there's no real way to "open the port". The USB API is a driver API, not a user-mode API. Here's a page to start from to understand USB drivers:
http://msdn.microsoft.com/en-gb/library/windows/hardware/ff540215%28v=vs.85%29.aspx
There is a WinUSB driver, which allows a single application to access a single device, assuming you know how to operate that device.

Related

Reading files from Android devices using WebUSB on Windows

I was wondering whether it is possible implement a subset of MTP on top of WebUSB, connect to an Android device from Windows, and retrieve files from the device?
If I install a WinUSB driver with Zadig, I can claim the interface. However, when I try to use the default "MTP USB Device" driver, I'm not able to claim the interface. I'm assuming that this is because the Windows MTP stack automatically claims the interface, even if the "MTP USB Device" is also a WinUSB driver?
The reason why I can't just use the WinUSB driver from Zadig, is that it breaks MTP access for programs that use the Windows Portable Device API. Is it at all possible to create a USB driver that will allow both MTP access and USB access, to allow for a user-space MTP driver, as there's no MTP functionality built into Chrome?
Trying to share access to the device between two low-level USB drivers (MTP and Chrome) is likely to cause data corruption if the device cannot differentiate between requests from each. I am not too familiar with the MTP driver built into Windows but this is a place where the experimental Native File System API should be able to provide access to the device at a high level where access can be shared.

How to connect hardware device to an Iphone application over Bluetooth

I have a hardware device which would communicate with a third party application on Android and IOS over Bluetooth to send to and fro data.
With Android I am able to achieve this, however in case of IOS I don't have any information.
Till now the information gathered to work with IOS devices is:
1. Application runs on Iphone 5
2. Hardware is MFI enabled
3. IAP2 is supported
Please guide how to connect the hardware to an application and what other information is essential?
Is AppName, BundleID, BundleSeedID and ProtocolName required to connect? If yes how we can get these details?
I tried unzipping the application but the Info.plist file looks garbled and not able to get much info from that.
Currently I am able to connect the hardware with IPhone over Bluetooth, but my real concern is how to communicate with a specific application on it.
Also if you can provide some link or pseudo code to initiate a session between hardware and IOS application.
I assume you've already connected the SPP profile on Android. However for iOS the SPP(BLE is another story and do not need MFi) was hidden but need MFi authentication for raw data links.
Since you want to establish the raw data link between your hardware device and your iPhone, you need:
Make sure your hardware has MFi chip, generally this chip is connected with your hardware by I2C.
After step 1 make sure you can communicate with this chip at your hardware, fortunately the hardware vendor or MFi vendor will support this kind of code or binary.
The MFi channel have a specific UUID(which may defined by Apple?) to replace the SPP official(which defined by Bluetooth SIG) UUID, hence if you want to make connection between your hardware and your iPhone, you need register the iOS defined UUID and make them as same as the normal SPP profile.
After the RFCOMM channel created, iOS would first authenticate the link, just like 3-way handshaking or something, so your hardware just communicate with the MFi chip and transfer the token to iOS.
After that iOS would set this RFCOMM channel(same as SPP actually) for your hardware, then you can send/recv data between them.

What usb device is connected to a usb port

I'll try to keep this simple.
I have a hub connected to my PC. This hub has several USB interfaces. I'm listening to WM_DEVICECHANGE event and I get the USB interface path. How can I know what port it was connected to? Looking for a non-WMI solution in c++ or c# for a windows environment.
I tried using IOCTL_USB_GET_NODE_CONNECTION_NAME with USB_NODE_CONNECTION_NAME (where USB_NODE_CONNECTION_NAME.NodeName will hold the path to the device) but this only works if the device connected to the port is a hub as well.
Any help will be much appreciated.
The primary issue in C++ is that there is no standard functions for detecting USB ports.
USB Port identification and implementation is a platform specific issue. For example, Linux handles USB ports quite differently than Windows and many embedded systems don't have USB ports.
So you'll have to look for a 3rd party library or find some OS API to use for your platform.

how to read certificate from USB device and send it to browser like firefox

I want to write a c++ dll that read certificate from my USB device and send that to browsers like Firefox.And I should mention that i have built my USB device with an ATMega32A which have 2K EEPROM and i stored a sample certificate on that.
I have read some about PKCS#11 standard but i cant figure out where to start. Could anyone help me on this?
It sounds like you're creating a PKI hardware device. I think your biggest hurdle will be accessing the ATMega32. It'll be up to you to implement an interface on the ATMega side, be that a RS232-usb bridge, USB Mass Storage, or proprietary with a custom driver.
A simple solution might be to use a USB Mass Storage interface to present a certificate as file on a pseudo disk.
Taking the custom driver route, Firefox (and other Mozilla products) use libraries that have interfaces for PKI hardware. See NSS and OpenSC.
At a guess it's possible that there's a PKI / PKCS11 driver API for Windows that you can implement.
You can not "add to Firefox" but you can expose your data to firefox and make them available for use. To do this you need to implement PKCS#11 API and create so-called PKCS#11 driver (the user-mode DLL which implements those 70 or so functions defined in PKCS#11 API). Those functions would talk to the hardware device in order to perform certain operations. Then you plug this PKCS#11 DLL to Firefox and Firefox can use certificates on your device.
Step 1: write a dll that can read data from the USB device. You could use Winusb. This will require the firmware to support it.
Step 2: write a dll that can add a certificate to Firefox
Step 3: combine previous steps into one dll
If you get stuck on something specific, write a new question and show us what you have done so far.

Trigger an exe once My device is connected via USB

Once my embedded device is connected to USB port of my PC, it should trigger an exe as an event. How can I achieve this??
Should I create a service to keep monitoring the USB connector bus or is there any default API's available in Windows to achieve this??
thanks.
A simple exe which is started on connect is not possible. But you can write a service or user mode application which listens for device arrival events. WM_DEVICECHANGE is sent to all (registered) applications with a device interface guid which represents which device is plugged in. You can then use this id with the setupapi to see if its your device.
On receiving that event, you can then start your executable.
Depending on your version of Windows it might be possible with a workaround using a AutoRun.inf file in the root folder of a USB drive. For security reasons this is by default turned off, and in Windows 7 not allowed at all.
To achieve the same effect in a more robust way, you need to create a service that monitors whether your device is connected or not (e.g. iTunesHelper that monitors for connected Apple devices).
The easiest solution is probably a trivial UMDF driver. That's basically a small COM component called when your device is connected.