5

How can the Pi detect if a host device has been connected, and gather information about that device, when being used as a gadget (such as mass storage). It would be useful if I could detect this within a python script.

As an example, if the Pi is to be used as a mass storage device, and I have a python script that's supposed to set that up, it would be useful if I could trigger certain behaviour when a device is actually connected, but not before. Further, it would be useful if the behaviour could depend on certain information about that device. For instance, if the data needed to be refreshed when a host was connected, or if a certain type of host needed a different set of data than another type of host.

zeel
  • 200
  • 1
  • 8

1 Answers1

1

Try checking /sys/class/udc/<udc>/state:

Indicates current state of the USB Device Controller. Valid states are: 'not-attached', 'attached', 'powered', 'reconnecting', 'unauthenticated', 'default', 'addressed', 'configured', and 'suspended'; however not all USB Device Controllers support reporting all states.

Apparently, this only changes to configured during the first connection, and then it remains configured even if you disconnect it. But it should at least allow you to detect the initial connection event.

Dmitry Grigoryev
  • 28,277
  • 6
  • 54
  • 147