Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - How to access serial number of USB device?: Page 1 of 2 (11 Items)
   
How to access serial number of USB device?  
My application uses usbd_connect() to register callback functions with the USB stack.
When the insertion callback function is called, I would like it to read the serial number of the USB device.

The callback function is passed pointers to a struct usbd_connection and a usbd_device_instance_t,
neither of which directly gives me the serial number.  So I am trying to use them to get a pointer to a
structure that does contain this information.  usbd_attach() returns EBUSY, and usbd_device_lookup()
returns NULL.  Is there a better way to do this?
RE: How to access serial number of USB device?  
Probably another process has already attached to the device. If you slay that process, then does your attach work?

You can have a second connection to the stack where your connection funcs is NULL, making it 'non exclusive' which then 
allows you to also attach with this second connection to get the device descriptors, to find the serial number.



-----Original Message-----
From: E McKenney [mailto:community-noreply@qnx.com] 
Sent: Tuesday, June 17, 2014 4:07 PM
To: ostech-core_os
Subject: How to access serial number of USB device?

My application uses usbd_connect() to register callback functions with the USB stack.
When the insertion callback function is called, I would like it to read the serial number of the USB device.

The callback function is passed pointers to a struct usbd_connection and a usbd_device_instance_t,
neither of which directly gives me the serial number.  So I am trying to use them to get a pointer to a
structure that does contain this information.  usbd_attach() returns EBUSY, and usbd_device_lookup()
returns NULL.  Is there a better way to do this?




_______________________________________________

OSTech
http://community.qnx.com/sf/go/post110723
To cancel your subscription to this discussion, please e-mail ostech-core_os-unsubscribe@community.qnx.com
Re: RE: How to access serial number of USB device?  
I call usbd_connect() in my main function, where I register the callback functions, so funcs is not NULL there.

When either callback function is called, it is passed a pointer to a connection struct - is this the same connection
that I opened in the main function, or is it another one?  If it's another one, then I don't know what arguments
were passed to it.

My main function is still running in the background, so that the callback functions will continue to be called.
Is that the reason for the EBUSY error?

Something's not making sense here - I suspect I'm missing a key piece to the puzzle.
RE: RE: How to access serial number of USB device?  
Try, in main, calling usbd_connect() twice, the second one have funcs set to NULL, then in the insertion callback 
function, instead of using the connect passed in, use the other connect that was set up where funcs was NULL.

You'll want to establish this one first, to ensure that the connection pointer is not NULL due to a race condition where
 the insertion callback may run before both connections are established.

-----Original Message-----
From: E McKenney [mailto:community-noreply@qnx.com] 
Sent: Tuesday, June 17, 2014 5:02 PM
To: ostech-core_os
Subject: Re: RE: How to access serial number of USB device?

I call usbd_connect() in my main function, where I register the callback functions, so funcs is not NULL there.

When either callback function is called, it is passed a pointer to a connection struct - is this the same connection
that I opened in the main function, or is it another one?  If it's another one, then I don't know what arguments
were passed to it.

My main function is still running in the background, so that the callback functions will continue to be called.
Is that the reason for the EBUSY error?

Something's not making sense here - I suspect I'm missing a key piece to the puzzle.




_______________________________________________

OSTech
http://community.qnx.com/sf/go/post110726
To cancel your subscription to this discussion, please e-mail ostech-core_os-unsubscribe@community.qnx.com
Re: RE: RE: How to access serial number of USB device?  
Thanks, I'll try that tomorrow.

> Try, in main, calling usbd_connect() twice, the second one have funcs set to 
> NULL, then in the insertion callback function, instead of using the connect 
> passed in, use the other connect that was set up where funcs was NULL.
> 
> You'll want to establish this one first, to ensure that the connection pointer
>  is not NULL due to a race condition where the insertion callback may run 
> before both connections are established.

Re: RE: RE: How to access serial number of USB device?  
Thank you, that works!   I had seen that strategy used in devc-serusb but didn't
understand why until now.

One remaining question: the iSerialNumber field is a _Uint8t not a string.
How does one get from this value to the actual serial number on the device?


> Try, in main, calling usbd_connect() twice, the second one have funcs set to 
> NULL, then in the insertion callback function, instead of using the connect 
> passed in, use the other connect that was set up where funcs was NULL.
> 
> You'll want to establish this one first, to ensure that the connection pointer
>  is not NULL due to a race condition where the insertion callback may run 
> before both connections are established.
RE: RE: RE: How to access serial number of USB device?  
Good to hear.

Try this:

char *dup_usbd_string (struct usbd_device *device, uint8_t index, int langid)
{
	char *s = usbd_string(device, index, langid);
	if (s) {
		s = strdup(s);
	}
	return s;
}

where langid is probably 0

You should be able to get the string using 'usbd_string' providing iSerialNumber as the index.

-----Original Message-----
From: E McKenney [mailto:community-noreply@qnx.com] 
Sent: Wednesday, June 18, 2014 8:50 AM
To: ostech-core_os
Subject: Re: RE: RE: How to access serial number of USB device?

Thank you, that works!   I had seen that strategy used in devc-serusb but didn't
understand why until now.

One remaining question: the iSerialNumber field is a _Uint8t not a string.
How does one get from this value to the actual serial number on the device?


> Try, in main, calling usbd_connect() twice, the second one have funcs set to 
> NULL, then in the insertion callback function, instead of using the connect 
> passed in, use the other connect that was set up where funcs was NULL.
> 
> You'll want to establish this one first, to ensure that the connection pointer
>  is not NULL due to a race condition where the insertion callback may run 
> before both connections are established.




_______________________________________________

OSTech
http://community.qnx.com/sf/go/post110744
To cancel your subscription to this discussion, please e-mail ostech-core_os-unsubscribe@community.qnx.com
Re: How to access serial number of USB device?  
That's perfect, thank you very much.  (Same trick should also get me the manufacturer and product strings.)

The online docs are good for overall USB driver development, but they didn't happen to mention this bit.   :)

> 
> You should be able to get the string using 'usbd_string' providing 
> iSerialNumber as the index.
> 
Re: How to access serial number of USB device?  
Glad it was helpful to you.

> The online docs are good for overall USB driver development, but they didn't 
> happen to mention this bit.   :)

On how we can improve our docs: specifically the trick to use a second connection for the non-exclusive attach, or 
getting from iSerial, iProduct, etc., to the actual string, or both techniques?

Feel free if you'd like more help with USB for a general nature to post to the OS Meta forum:
http://community.qnx.com/sf/discussion/do/listTopics/projects.core_os/discussion.metawiki

If you have questions USB hardware drivers specific to your BSP, you can use the BSP forum:
http://community.qnx.com/sf/discussion/do/listTopics/projects.bsp/discussion.bsp
Re: How to access serial number of USB device?  
 
> On how we can improve our docs: specifically the trick to use a second 
> connection for the non-exclusive attach, or getting from iSerial, iProduct, 
> etc., to the actual string, or both techniques?

Both, I guess.

The docs already cover using NULL to get a R/O connection, but doesn't really explain why you might need it.  A brief 
note to this effect in the context of writing callback functions would be sufficient. 

And it would be good to mention usbd_string() on the page for usbd_device_descriptor(), since that's where the struct 
contents are spelled out.


Overall I have to say the online docs are very good.  They sometimes miss subtle bits as noted here, but what exists is 
generally quite clear and sometimes even fun to read.