Hello
My name is françois. I work with imx6dl sabre. I want to detect sdcard. I used udev to automount the sdcard and its work. But I want also that my personnal application detect the sdcard and I don't know how to do . Have you got a solution for me.
Thanks for All Help.
Best regards
François.
You need to study linux netlink knowledge.
Hello
Thanks to reply.
I found the same solution:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <mqueue.h> // required for messages
#include <asm/types.h>
#include <sys/socket.h>
#include <linux/rtnetlink.h>
#include <linux/netlink.h>
void* SDcardManager_thread(void*)
{
int ns = -1;
struct sockaddr_nl sa;
char buf[4096];
struct iovec iov = { buf, sizeof(buf) };
struct msghdr msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
int len;
memset (&sa, 0, sizeof(sa));
sa.nl_family = AF_NETLINK;
sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
ns = socket(AF_NETLINK, SOCK_RAW, NETLINK_KOBJECT_UEVENT);
if (ns < 0) {
perror("socket()");
ret=0;
}//end if (ns < 0)
if (bind(ns, (struct sockaddr*)&sa, sizeof(sa))) {
perror("bind()");
ret=0;
}//end if (bind(ns, (struct sockaddr*)&sa, sizeof(sa)))
while (ret) {
len = recvmsg (ns, &msg, 0);
if (len < 0) {
perror("recvmsg()");
break;
}//fi(len < 0)
buf[4095] = 0;
if((strstr(buf,NAME_SD))!=NULL)
{
//sdcard add
if((strstr(buf,NAME_ADD))!=NULL)
{
//todo here your code
}//if((strstr(buf,NAME_ADD))!=NULL)
else
{
//sdcard remove
if((strstr(buf,NAME_REMOVE))!=NULL)
{
//todo here your code
}//if((strstr(buf,NAME_REMOVE))!=NULL)
}//else((strstr(buf,NAME_ADD))!=NULL)
}//fi((strstr(buf,NAME_SD))!=NULL)
}//end while
return ((void*)&ret);
}
Best regards
François.
Sorry, I don't understand your question. As you can mount the sdcard, then sd card can be found in /dev/. (e.g. /dev/mmcblk0) Would you tell me more detail about your problem? Have a nice day.
Hello
Sorry to my question. Currently I used the udev system. I wrote a script to mount or umount sdcard. This script is called by udev. its work.
But now my application must know (to display an sdcard icone) if the sdcard is present or no. Indeed I can use the file /dev/mmcblk0. But I would like that my application is prevented by an interrupt or by a signal and I don't know that interrupt or signal use.
Have you got a solution for me
Thanks for All Help.
Best regards
François.
Netlink is how the udev is working. Which is the hook for the linux driver sending message to user space such as udev or mdev in busybox.
That is what I reply to you " You need to study linux netlink knowledge".
You can read udev source code, but big.
Also you can read the mdev source code in busybox, that is simple and small.