How to react to a USB stick plugged? Or how to check if a file exists in /dev/?

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
User avatar
Parduz
Experienced Solver
Experienced Solver
Posts: 71
Joined: Fri Jan 30, 2015 1:48 pm
Location: Bologna, Italy

How to react to a USB stick plugged? Or how to check if a file exists in /dev/?

Post by Parduz » Wed Nov 13, 2019 10:46 am

I'm trying to determine if and when an USB stick is plugged in a BeagleBoneBlack USB port.

The stick will always be "/dev/sda1", so i thought that i could poll that file with a 1s timer (i feel this method really ... primitive? but i can't sort out anything better).

But wxFileExist("/dev/sda1") always fails, 'cause it returns wxFileSystemObjectExists(filePath, wxFILE_EXISTS_REGULAR) while seems that i should check for wxFILE_EXISTS_DEVICE, if not for wxFILE_EXISTS_ANY.

Problem is that wxFileSystemObjectExists is'nt declared in any .h file, so i can't call it directly.

1a) How am i supposed to check if a file in /dev/ exists?
Or, if my approach is wrong:
1b) How should i check for a device?

Or/and, if the whole logic is bad:

2) How can i "react" to the insertion of a USB stick?

Ruro
Experienced Solver
Experienced Solver
Posts: 62
Joined: Wed May 23, 2007 2:20 pm
Location: Verona, Italy

Re: How to react to a USB stick plugged? Or how to check if a file exists in /dev/?

Post by Ruro » Wed Nov 13, 2019 11:08 am

Hello,

wouldn't be better to use wxDir to check the item inside the directory?

if you check the content of /dev it should return the item inside it.

Maybe there is a better way to check if an usb is plugged in...

Best Regards,

Nicola.

Big Muscle
Experienced Solver
Experienced Solver
Posts: 73
Joined: Sun Jun 27, 2010 6:18 pm

Re: How to react to a USB stick plugged? Or how to check if a file exists in /dev/?

Post by Big Muscle » Wed Nov 13, 2019 11:27 am

We periodically check this and never had any problems with it:

Code: Select all

wxFindFirstFile("/dev/sd??")

DavidHart
Site Admin
Site Admin
Posts: 3907
Joined: Thu Jan 12, 2006 6:23 pm
Location: IoW, UK

Re: How to react to a USB stick plugged? Or how to check if a file exists in /dev/?

Post by DavidHart » Wed Nov 13, 2019 11:48 am

Hi,

In my code I periodically check /sys/bus/scsi/devices. However that was written >10 years ago and, while it still works, I'm sure that nowadays there are better ways; one of which is probably libusb, which someone on #wxwidgets mentioned recently.

Regards,

David

User avatar
Parduz
Experienced Solver
Experienced Solver
Posts: 71
Joined: Fri Jan 30, 2015 1:48 pm
Location: Bologna, Italy

Re: How to react to a USB stick plugged? Or how to check if a file exists in /dev/?

Post by Parduz » Wed Nov 13, 2019 1:58 pm

Big Muscle wrote:
Wed Nov 13, 2019 11:27 am
We periodically check this and never had any problems with it:

Code: Select all

wxFindFirstFile("/dev/sd??")
THANKS!!!
That works... i did'nt found that function.

I "solved" by using

Code: Select all

	wxFileName fn("/etc/sda1");	
	if ( fn.Exists(wxFILE_EXISTS_ANY) ) {
just 'cause Exists allows that flag, but wxFindFirstFile makes it cleaner.
Thanks again =D> .

DavidHart wrote:
Wed Nov 13, 2019 11:48 am
I'm sure that nowadays there are better ways; one of which is probably libusb, which someone on #wxwidgets mentioned recently.
Seems interesting, i'll take a look at it when i can spend some time on it. Thanks.

User avatar
doublemax
Moderator
Moderator
Posts: 14314
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to react to a USB stick plugged? Or how to check if a file exists in /dev/?

Post by doublemax » Wed Nov 13, 2019 4:57 pm

I don't know if wxFileSystemWatcher works for virtual filesystems, but it might be worth a try:

https://docs.wxwidgets.org/trunk/classw ... tcher.html
Use the source, Luke!

User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2282
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania
Contact:

Re: How to react to a USB stick plugged? Or how to check if a file exists in /dev/?

Post by evstevemd » Sat Nov 16, 2019 4:25 pm

would suggest you look at libusb. I have not checked it myself but it seems have such an event. See http://libusb.sourceforge.net/api-1.0/hotplug.html
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
[Ubuntu 19.04/Windows 10 Pro/MacOS 10.13 - GCC/MinGW/Clang, CodeLite IDE]

Post Reply