How to find the device is connected to whether USB port 2.0 or USB port 3.00 ? - winapi

I just want to know how to find a usb device is connected to USB3.0 or USB2.0 port in window . I am using c++ and win32 API. please let me know your thoughts.

Based on how your question is phrased, I assume that you know when a device is connected, and need to know if the port it's connected is capable of USB 3.0 speeds.
Take a look at the USB_NODE_CONNECTION_INFORMATION_EX structure, and specifically the field Speed in it, which could correspond to a value defined in the enum USB_DEVICE_SPEED. For USB 3.0, the value of Speed would be UsbSuperSpeed.
The documentation page says that you could use IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX request to obtain information about the connection associated with the indicated USB port.
You may also find the following remarks from the documentation helpful:
If there is no device connected, IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX just returns information about the port.
If a device is connected to the port IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX returns information about both the port and the connected device.
The USB_NODE_CONNECTION_INFORMATION_EX structure is an extended version of USB_NODE_CONNECTION_INFORMATION. The two structures are identical, except for one member. In the extended structure, the Speed member indicates the device speed.
I'd also encourage you to single-step through the usbview project which is part of the windows driver samples available in Github.

Related

USB stack confusion/serial emulator

I am looking at implementing USB communication on a MCU which has a USB engine built into it. Basically you have access to the pipes/endpoints.
I am a little bit confused on the USB stack now. It appears that drivers operate on another level above the pipe/endpoint setup, so the pipe/endpoint is like a middle level layer that drivers are built on. Is this correct?
Secondly, I am interested in simulating serial communication over USB. It appears windows has a premade driver so on the computer side I do not need to program the pipe level.
How do I find out what I need to implement on the MCU to make it behave correctly with the generic serial driver?
This is an answer to your second question regarding the serial communication.
The USB standard defines a communication device class (CDC) for serial communication. The required drivers on the host side are implemented by Windows, macOS, Linux and many more operation systems.
The relevant CDC subclass is PSTN. The relevant documents are found in Class definition for Communication Devices 1.2.
The device basically implements four endpoints:
Control endpoint for configuration requests (baud rate, DTR state etc.). Have a look at SetLineCodeing, GetLineCoding and SetControlLineState.
Bulk endpoint for USB to serial transmission
Bulk endpoint for serial to USB transmission
Interrupt endpoint for notifications (DCD state, errors). See SerialState.
And of course you need to get the device descriptor right.
On top of that, you need to implement all the standard USB requests.
Chances are high that this has already been written for your MCU, both the standard requests and the serial communication. So why not use the existing code?

How can I get specific information about the serial (COM) port in Windows?

Since I have dozens of virtual serial (COM) ports installed and the half of them are Bluetooth devices, I'd like to know what port belong to what device and whether it's connecting directly or via Bluetooth.
So in particular I'm not interested in the trivial enumeration of all serial ports, which would only reveal a list of 'COM' + the corresponding number, but the real name of that device and probably its device ID (where information can be found about whether this is connecting via USB or Bluetooth.
While I could get these information via WMI, there are a plenty of problems related to this method. In particular it's bloody slow, but also it does only list connected devices (except for Bluetooth devices, that can potentially be connected and are shown regardless of a real connection)
I'd prefer a solution written in Delphi / Object Pascal, but any other language is also fine for me (the WMI access code was copied from C#).
If you use a programming language/API that can read the registry, check the subtree HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum.
This has subkeys containing device type, device class, instance ID. Each Instance ID has a subkey Device Parameters\PortName that has the COM name.
For example, on my system
ACPI\PNP0501\1\Device Parameters\PortName = COM1
BTHENUM\{GUID}{ID}\Device Parameters\PortName = COM4
The keys in the ID Part have additional information, for example
ACPI\PNP0501\1\FriendlyName = Kommunikationsanschluss (COM1) (german windows)
ACPI\PNP0501\1\Service = Serial
BTHENUM\{GUID}{ID}\Service = BTHMODEM
Use regedit to check for yourself which parts are interesting to you. But this should give you all information you need.

Mac - Virtual Serial Port

I need to create a Cocoa app that will create a virtual serial port available to other apps, meaning registered in the IO Kit Registry.
Gist of the app:
Create a virtual serial port (listed in /dev and registered with the IOKit Registry)
Initiate a tcp connection out to another computer
Proxy everything received on the virtual serial port out to the
network and vice versa.
This app will be used by third party apps that talk to serial ports on the computer, allowing for the particular serial device to be located across the network. The Cocoa and network part is no problem, I've written several apps that talk over the network. My hangup is the serial port.
I've done the test with socat/netcat/minicom to verify that it all works to proxy pty/tty traffic over the network but the tty I use doesn't show up as usable by random applications because it's not registered in the IO Kit Registry.
While I can use a pty/tty master/slave for the communication, I need this slave tty to show up to Mac applications. What would be very handy is a way to register a tty in the IO Kit Registry.
Do I really need to create a custom IOKit kext driver that gets registered at Cocoa app runtime? If so, I have a big learning curve ahead of me. Where should I start reading? Or, can I use IOKit to create a virtual serial port and register it as a usable serial port for applications without having to load any kernel extensions?
Thank you for any help you can provide,
Stateful
First of all, have you checked if you can borrow a solution from this app? It's not obvious from the website if they've managed to get their virtual serial ports fully integrated into the system.
If there is a way to do it from user space, I'm not aware of it. The user-space IOKit API generally doesn't let you create class instances, let alone new device driver classes. Maybe you can somehow otherwise persuade the Cocoa libraries to find it despite not being registered in the kernel.
I don't know if you could get away with creating a "dummy" serial port in the kernel and then move your tty into its place in /dev from your userspace daemon. Maybe that's an option.
In case you do have to do it all in the kernel:
The virtual driver itself shouldn't be too much work, at least, though it will require some time to get up to speed with kernel dev. Unfortunately, the documentation is pretty thin for serial port drivers - the key is subclassing the IOSerialDriverSync abstract class. Just about the only description I've seen is in Ole Henry Halvorsen's OSX and iOS Kernel Programming book. It also has a fragment of an example for the reading & writing operations. (disclosure: I was one of the tech reviewers for this book; I don't receive any incentives for recommending it - in this case it's literally the only documentation I know of) You can find the source for a complete serial port driver in Apple's USBCDC driver, AppleUSBCDCDMM is the class that actually represents the serial port node.
It's relatively straightforward to open a so-called "kernel control" socket in the kernel, the individual APIs are documented here; from user space you use the normal BSD socket send/recv APIs. (this is also described in the aforementioned book) Your daemon can then connect to that, and all you'd need to do is push the data between the socket and the virtual serial port device. You'll need to handle disconnect events and such correctly of course.
Still, I think this is achievable as a first kernel project for an experienced C programmer (with some C++).
I hope that helps!

How to detect Network type on Win Mobile

I spend lot of time searching how to determine the current network type such as WIFI, 3G, Ethernet.
But I cannot find any information online in windows platform.
I want to detect the Ethernet network type, but I cannot find any API that can retrieve the network type.
I've find Connection Manager for keyword but there's less information.
Did anyone have any idea?
Please help me...I was blocked by this for 2 weeks.
Thanks.
Look in the registry key HKLM\system\state\connections, there is are entries for each of the possible network types, for instance:
When connected to a wireless network:
[HKLM\system\state\Connections\Network]
Adapters="BCMSDDHD1"
Count=dword:00000001
Descriptions="Business World"
when connected to activesync:
[HKLM\system\state\Connections\Desktop]
Adapters=""
Count=dword:00000001
Descriptions="CurrentDTPTNetwork"
The Count value under HKLM\system\state\connections gives you the total number of active connections.
In HKLM\system\state\Hardware you can find the current power state of the various devices.
There is an API on Windows Mobile that makes it easier to access these values vs. going directly to the registry. See the SystemState class documentation, specifically the ConnectionsNetworkCount and ConnectionsCount properties.

bluetooth device to windows API via com port

So I have a bluetooth device, this device uses SPP to transfer data between the PC and itself. It connects fine through Windows as a bluetooth device. I can find it, enter the paring code and assign it to a COM port. Now I want to be able to send data through the com port using Windows API but it is refusing to do so.
I suspect that I need to setup the COMMCONFIG Structure correctly (see below)
http://msdn.microsoft.com/en-us/library/aa363188(VS.85).aspx
Unfortunately I have no idea what is the proper setting. I know SPP is supposed to emulate the RS-232 communication... so maybe I have to study up on that to figure out the right setting? Or is there some automatic way to set the COMMCONFIG structure.
I seriously doubt it. If it would be used then you'll have no chance at guessing at the custom provider data without docs from the driver author. Pay attention to the handshake signals, serial port devices routinely ignore anything sent to them when the DTR signal is turned off. And not send anything back with DTR off. A driver would emulate that. Use EscapeCommFunction() to turn them on. Also try a serial comm program like HyperTerminal or Putty to test this so you can isolate the source of the problem.
Why not use the Bluetooth sockets API? No need for troublesome (virtual) COM ports then.
If you're using managed code then see my library 32feet.NET
If using native code, use SOCKADDR_BTH with Winsock connect etc, see e.g. Bluetooth and connect (Windows) Then you can use the standard Winsock send/recv API
Ok, I found that you can use the
GetCommConfig and GetCommState functions to figure out the settings.

Resources