2

Good afternoon,

I'm running a simple loop for checking whether SD card has been inserted or removed, but it doesn't seem to work correctly all the time.

The problem is, when the program starts with SD card inserted, the if(f.good()) statement is True.

When SD card is removed this statement is False.

However, when the SD card is re-inserted, this statement is still False.

Is there a more reliable way in C++ to detect SD card presence? I'm Running Linux Yocto, based on OpenEmbedded. I would prefer to avoid using any external libraries, and use file IO or system calls if possible.

Thanks.

My loop is shown below,

    while (running)
    {
        ifstream f("/dev/mmcblk1");
        if (f.good()) {
            f.close();
            if (!mounted)
            {
                system("mount /dev/mmcblk1p1 /mnt/Storage");   
                mounted = true;
            }
            sdPresent = true;
        }
        else {
            f.close();
            sdPresent = false;
            mounted = false;
        }
        std::this_thread::sleep_for(std::chrono::milliseconds(500));
    }
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Mich
  • 3,188
  • 4
  • 37
  • 85
  • I think when a `ifstream` goes bad (!good) it won't come good again. And although `ifstream f("/dev/mmcblk1")` is inside the loop it is initialized only once. Try declaring `ifstream f` outside the loop and doing f.open("/dev/mmcblk1");` at the beginning and `f.close()` when you finish all the checks. – nsilent22 Feb 23 '16 at 21:57

2 Answers2

7

The proper direct way to know if a removable device has a media inserted is reading the device size from sysfs.

In your case it would be somewhere like /sys/block/mmcblk1/size. That file should exist always, and contain a unsigned long long value. If it is 0 then no media, if it is >0 then you have a media of that size.

bool has_media()
{
    std::ifstream fsize("/sys/block/mmcblk1/size");
    unsigned long long size;
    return (fsize >> size) && (size > 0);
}

If, instead of polling and sleeping, you want proper notifications of the media insertion/removal, and you don't want / cannot use libudev, then you would need to use uevent and a netlink socket. See this excelent answer with the details.

Community
  • 1
  • 1
rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • Thanks, your answer works well. I checked that I can use libudev, but its very complex and I havent found any comprehensive examples on how to use it with Sd cards, except for another StackOverflow question found here: http://stackoverflow.com/questions/10979123/how-to-get-notifications-for-sd-card-events – Mich Feb 23 '16 at 23:39
  • @user2056201: Yes, libudev quite complex and I have no experience aobut it. What I do know is that you can use the command `udevadm monitor`, then insert and remove your media and see what uevents to expect. – rodrigo Feb 24 '16 at 08:36
  • @user2056201: If you could use udisks2, it has a quite nice dbus interface. If not, well, you can study its source code and learn how it works. – rodrigo Feb 24 '16 at 08:37
  • `mmcblk` directories in the `/sys/` folder are deleted when the card is removed on Raspbian Jessie (Running from USB drive). This answer did not work for me, although it did compile. – Zimano May 03 '17 at 12:30
0

if the sd-card is the only FAT32 partition you can simply do:

fdisk -l | grep FAT32 | cut -c 1-8
Cris
  • 2,824
  • 24
  • 23