Device: Raspberry Pi Zero W v1.1
State: Pi Zero is running in gadget mode, acting as a USB Mass Storage device (seen by / mounted on a MacBook Pro).
Question: What is the best way to detect whether or not the MacBook Pro is still connected / using the Pi as a Mass Storage device?
Ideally, either by polling or event trigger, I would like to know (within about ~5 seconds) if the User has either ejected the USB device or shut down the MacBook Pro ... at which point, I'd be able to execute some shell commands.
There is a similar question here, but the author asks about collecting information about the connected host, which is unnecessary in my use-case.
I've tried using:
cat /sys/class/udc/20980000.usb/state ... and, while it seems to pick up the change from
not attached ==>
configured (when I plug Pi into my MacBook), it doesn't pick up the change when I eject/disconnect (it seems to stay stuck in
configured and never "drops back down" to
not attached). This seems to be related to a known issue (mentioned in here).