udev example -- detect usb and write test file
之前学习了下Udev,就随便做了个测试小程序.....设计什么的也没考虑,就实现了一个基本功能,插入U盘,识别,循环检测到有特定文件后,就然后往U盘里面写数据,插拔多次,都能正常工作。
里面的warning和不规范的写法请自己修改。
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <getopt.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/select.h>
#include <linux/types.h>
#include <linux/netlink.h>
#include <sys/types.h>
#include <libudev.h>
#include <iostream>
#include <pthread.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <dirent.h>
#include <time.h>
using namespace std;
static std::string record_path=""; static bool FindRecord=false;
void File_Opreation();
void GetUsbFolderLocation(char *basePath);
void MountTheSystem(char *basePath);
static bool NeedToCheck=false;
int ListDevice()
{
udev *udev;
struct udev_enumerate *enumerate;
struct udev_list_entry *devices, *dev_list_entry;
struct udev_device *dev; /* Create the udev object */
udev = udev_new();
if (!udev)
{
printf("Can't create udev\n");
exit();
} /* Create a list of the devices in the 'hidraw' subsystem. */
enumerate = udev_enumerate_new(udev);
udev_enumerate_add_match_subsystem(enumerate, "block");
udev_enumerate_scan_devices(enumerate);
devices = udev_enumerate_get_list_entry(enumerate);
/* For each item enumerated, print out its information.
udev_list_entry_foreach is a macro which expands to
a loop. The loop will be executed for each member in
devices, setting dev_list_entry to a list entry
which contains the device's path in /sys. */
udev_list_entry_foreach(dev_list_entry, devices)
{
const char *path; /* Get the filename of the /sys entry for the device
and create a udev_device object (dev) representing it */
path = udev_list_entry_get_name(dev_list_entry);
dev = udev_device_new_from_syspath(udev, path); /* usb_device_get_devnode() returns the path to the device node
itself in /dev. */
printf("Device Node Path: %s\n", udev_device_get_devnode(dev)); /* The device pointed to by dev contains information about
the hidraw device. In order to get information about the
USB device, get the parent device with the
subsystem/devtype pair of "usb"/"usb_device". This will
be several levels up the tree, but the function will find
it.*/
dev = udev_device_get_parent_with_subsystem_devtype(
dev,
"usb",
"usb_device");
if (!dev)
{
cout<<"Unable to find parent usb device"<<endl;
exit();
} /* From here, we can call get_sysattr_value() for each file
in the device's /sys entry. The strings passed into these
functions (idProduct, idVendor, serial, etc.) correspond
directly to the files in the directory which represents
the USB device. Note that USB strings are Unicode, UCS2
encoded, but the strings returned from
udev_device_get_sysattr_value() are UTF-8 encoded. */
printf(" VID/PID: %s %s\n",
udev_device_get_sysattr_value(dev,"idVendor"),
udev_device_get_sysattr_value(dev, "idProduct"));
printf(" %s\n %s\n",
udev_device_get_sysattr_value(dev,"manufacturer"),
udev_device_get_sysattr_value(dev,"product"));
printf(" serial: %s\n",
udev_device_get_sysattr_value(dev, "serial"));
udev_device_unref(dev);
}
/* Free the enumerator object */
udev_enumerate_unref(enumerate); udev_unref(udev); return ;
} void Udev_Enumrate()
{
struct udev* udev_ancestor=NULL;
struct udev_enumerate* udev_enum=NULL;
struct udev_list_entry* device_fistentry=NULL;
struct udev_list_entry *dev_list_entry=NULL; //entry to store the current position
struct udev_device *dev=NULL;
udev_ancestor=udev_new();
udev_enum=udev_enumerate_new(udev_ancestor);
if(udev_enumerate_add_match_subsystem (udev_enum, "block")==)
{
cout<<"add block device to match subsystem successful"<<endl;
} if(udev_enumerate_add_match_subsystem (udev_enum, "usb")==)
{
cout<<"add usb device to match subsystem successful"<<endl;
} if(udev_enumerate_add_match_subsystem (udev_enum, "scsi")==)
{
cout<<"add scsi device to match subsystem successful"<<endl;
} //Scan the system under /sys/
udev_enumerate_scan_devices(udev_enum); //get the first entry of the device list
device_fistentry=udev_enumerate_get_list_entry(udev_enum); /* For each item enumerated, print out its information.
udev_list_entry_foreach is a macro which expands to
a loop. The loop will be executed for each member in
devices, setting dev_list_entry to a list entry
which contains the device's path in /sys. */
udev_list_entry_foreach(dev_list_entry, device_fistentry)
{
const char *path; /* Get the filename of the /sys entry for the device
and create a udev_device object (dev) representing it */
path = udev_list_entry_get_name(dev_list_entry);
dev = udev_device_new_from_syspath(udev_ancestor, path); /* usb_device_get_devnode() returns the path to the device node
itself in /dev. */
printf("Test Device Node Path: %s\n", udev_device_get_devnode(dev)); /* The device pointed to by dev contains information about
the hidraw device. In order to get information about the
USB device, get the parent device with the
subsystem/devtype pair of "usb"/"usb_device". This will
be several levels up the tree, but the function will find
it.*/
dev = udev_device_get_parent_with_subsystem_devtype(
dev,
"usb",
"usb_device");
if (!dev)
{
cout<<"Test Unable to find parent usb device"<<endl;
//exit(1);
}
else
{
printf(" VID/PID: %s %s\n",udev_device_get_sysattr_value(dev,"idVendor"), udev_device_get_sysattr_value(dev, "idProduct"));
printf(" %s\n %s\n",udev_device_get_sysattr_value(dev,"manufacturer"), udev_device_get_sysattr_value(dev,"product"));
printf(" serial: %s\n",udev_device_get_sysattr_value(dev, "serial"));
} udev_device_unref(dev);
}
udev_enumerate_unref(udev_enum);
udev_unref(udev_ancestor); } void* udev_Monitor(void*)
{
struct udev* udev=NULL;
struct udev_monitor * mon=NULL;
struct udev_device *dev;
int fd;
fd_set fds;
struct timeval tv;
static int flag=; udev=udev_new();
mon=udev_monitor_new_from_netlink(udev,"udev"); udev_monitor_filter_add_match_subsystem_devtype(mon, "sound", "usb_device");
udev_monitor_filter_add_match_subsystem_devtype(mon, "usb", "usb_device");
udev_monitor_filter_add_match_subsystem_devtype(mon, "block", "disk");
udev_monitor_filter_add_match_subsystem_devtype(mon, "block", "partition");
udev_monitor_filter_add_match_subsystem_devtype(mon, "usb", "usb_interface");
udev_monitor_enable_receiving(mon);
fd = udev_monitor_get_fd(mon);
while()
{ fd_set fds;
struct timeval tv;
int ret; FD_ZERO(&fds);
FD_SET(fd, &fds);
tv.tv_sec = ;
tv.tv_usec = ; ret = select(fd+, &fds, NULL, NULL, &tv);
//ret means there's an event fd_isset means fd is readable
if(ret> & FD_ISSET(fd,&fds))
{
//cout<<"There's a change with Num="<<flag<<endl;
//flag++;
/* Make the call to receive the device.
select() ensured that this will not block. */
dev = udev_monitor_receive_device(mon);
if (dev)
{
const char* sysPath = udev_device_get_syspath(dev);
const char* action = udev_device_get_action(dev);
const char* subsystem = udev_device_get_subsystem(dev);
const char* devType = udev_device_get_devtype(dev);
if ( == strncmp(action, "add", strlen("add")))
{ const char* devClass = udev_device_get_sysattr_value(dev, "bDeviceClass");
const char* devInterfaceClass = udev_device_get_sysattr_value(dev, "bInterfaceClass"); cout<<"The devClass: "<<devClass<<endl;
cout<<"The devInterfaceClass:"<<devInterfaceClass<<endl; NeedToCheck=true; }
printf("Got Device\n");
printf(" Node: %s\n", udev_device_get_devnode(dev));
printf(" Subsystem: %s\n", udev_device_get_subsystem(dev));
printf(" Devtype: %s\n", udev_device_get_devtype(dev));
printf(" Action: %s\n",udev_device_get_action(dev));
printf(" Path: %s\n",udev_device_get_syspath(dev)); udev_device_unref(dev);
}
else
{
printf("No Device from receive_device(). An error occured.\n");
}
}
}
} int LoopileList(char *basePath)
{
DIR *dir;
struct dirent *ptr;
char base[]; if ((dir=opendir(basePath)) == NULL)
{
perror("Open dir error...");
exit();
} while ((ptr=readdir(dir)) != NULL)
{
if(strcmp(ptr->d_name,".")== || strcmp(ptr->d_name,"..")==) ///current dir OR parrent dir
{
continue;
}
else if(ptr->d_type == ) ///file
{
printf("d_name:%s/%s\n",basePath,ptr->d_name);
}
else if(ptr->d_type == ) ///link file
{
printf("d_name:%s/%s\n",basePath,ptr->d_name);
}
else if(ptr->d_type == ) ///dir
{
memset(base,'\0',sizeof(base));
strcpy(base,basePath);
strcat(base,"/");
strcat(base,ptr->d_name);
LoopileList(base);
}
}
closedir(dir);
return ;
} static bool stopLoopflag=false;
void GetUsbFolderLocation(char *basePath)
{
bool ret=false;
DIR *dir;
struct dirent *ptr;
char base[];
MountTheSystem(basePath);
//cout<<"Loop check file "<<basePath<<endl;
if ((dir=opendir(basePath)) == NULL)
{
//perror("Open dir error...");
//exit(1);
}
else
{
while ((ptr=readdir(dir)) != NULL && !stopLoopflag)
{ if(strcmp(ptr->d_name,".")== || strcmp(ptr->d_name,"..")==) ///current dir OR parrent dir
{
continue;
}
else if(ptr->d_type == ) ///file
{
if(strcmp(ptr->d_name,"Record_HFC")==)
{
record_path=basePath;
printf("File :%s/%s\n",basePath,ptr->d_name);
strcat(basePath,"/");
record_path=basePath;
cout<<"Record Path is="<<record_path<<endl;
FindRecord=true;
stopLoopflag=true;
break;
} }
else if(ptr->d_type == ) ///link file
{
printf("Link:%s/%s\n",basePath,ptr->d_name);
}
else if(ptr->d_type == ) ///dir
{
memset(base,'\0',sizeof(base));
strcpy(base,basePath);
strcat(base,"/");
strcat(base,ptr->d_name);
GetUsbFolderLocation(base);
}
}
if(dir!=NULL)
{
closedir(dir);
}
} } string GetLocalTime()
{
//string str="/usr/bin/sgm/Conn/tmp/out_";
string str="";
time_t rawtime;
struct tm* timeinfo;
char timE[];
time(&rawtime);
timeinfo=localtime(&rawtime);
strftime(timE,sizeof(timE),"%Y_%m_%d_%I_%M_%S",timeinfo);
printf("%s",timE); str=str+timE;
return str;
}
void MountTheSystem(char *basePath)
{
//cout<<"mount"<<record_path<<endl;
//mount(NULL, record_path.c_str(), NULL, MS_REMOUNT, "-o, rw");
//cout<<"mount"<<record_path<<endl;
mount(NULL, basePath, NULL, MS_REMOUNT, "-o, rw");
}
void WriteFileToUSB()
{
record_path=record_path+"test.txt";
FILE* fusb_Ubuntu=NULL;
fusb_Ubuntu=fopen(record_path.c_str(),"w+");
cout<<"path is="<<record_path<<endl;
if(fusb_Ubuntu==NULL)
{
cout<<"cant't write"<<endl;
}
else
{
cout<<"start write"<<endl;
std::string str_Ubuntu="This is test";
str_Ubuntu=str_Ubuntu+GetLocalTime();
char* arr_Ubuntu=new char[str_Ubuntu.length()+];
for(int i=;i<str_Ubuntu.length();i++)
{
arr_Ubuntu[i]=str_Ubuntu[i]; }
cout<<str_Ubuntu<<endl;
fwrite(arr_Ubuntu,str_Ubuntu.length(),sizeof(char),fusb_Ubuntu);
fclose(fusb_Ubuntu);
record_path="";
NeedToCheck=false;
stopLoopflag=false;
delete []arr_Ubuntu;
arr_Ubuntu=NULL;
} }
void File_Opreation()
{
cout<<"start to do file operation"<<endl;
} void* writeFile(void*)
{
while()
{
if(NeedToCheck)
{
char* path="/media";
GetUsbFolderLocation(path); if(FindRecord)
{
WriteFileToUSB();
}
}
else
{
cout<<"sleep for 5s"<<endl;
usleep(*);
}
} }
//int main(int argc, char *argv[])
int main()
{
#if 0
char* path="/media";
GetUsbFolderLocation(path); if(FindRecord)
{
File_Opreation();
}
#endif
pthread_t monitor_thread=;
pthread_t write_thread=; int err=;
err=pthread_create(&monitor_thread, NULL,udev_Monitor, NULL);
if(err!=)
{
cout<<"create thread error"<<endl;
}
else
{
cout<<"create thread monitor success "<<endl;
} err=pthread_create(&write_thread, NULL,writeFile, NULL);
if(err!=)
{
cout<<"create thread error"<<endl;
}
else
{
cout<<"create thread writeFile success "<<endl;
} if(monitor_thread!=)
{
pthread_join(monitor_thread,NULL);
}
if(write_thread!=)
{
pthread_join(write_thread,NULL);
} return ;
}
Linux 下运行,如果要在ARM下运行就编一个arm版本的。
Makefile
# specify the compiler
CC=/usr/bin/g++ # specify library INCFLAGS=-I ./ # specify library
LIBFLAGS=-l pthread -l udev # specify additional compile flags
FLAGS= -lm -g -Wall -Wextra # List of files specific
SRC:= Udev_Monitor.cpp testapp:
${CC} -o Udev_Monitor ${SRC} ${LIBFLAGS} ${INCFLAGS} ${FLAGS} clean:
rm -f Udev_Monitor
只是基本的识别插拔U盘,检测特定的文件,检测到后往U盘里面写数据,可以用来帮助cp或者记录log文件。
虽然我的blog也没人会看,但是还是希望能帮到和我一样的菜鸟。
udev example -- detect usb and write test file的更多相关文章
- cannot open /proc/bus/usb/devices, No such file or directory
由于kernel config中没有打开对应的配置. make menuconfig 选择: Device Drivers ---> [*] USB support ---> [*] US ...
- Linux自动共享USB设备:udev+Samba
一.概述 公司最近要我实现USB设备插入Ubuntu后,自动共享到网络上,能像Windows共享一样(如\\192.168.1.10)访问里面的内容,不需要写入权限.当时听完这需求,我这新人表示惊呆了 ...
- 转://UDEV简介及配置过程
在Linux环境下安装Oracle11g RAC时,OS层面配置好多路径软件后(multipath),下一步就需要配置udev或asmlib来处理共享分区(Lun),以便Orace ASM能够看到这些 ...
- udev规则以及编写
主要内容: udev简介 如何配置和使用udev 如何编写udev规则 字符串替换和匹配 udev主要作用 编写udev规则实例 难点解析 1. udev简介 1.1 什么是udev? udev是Li ...
- Security arrangements for extended USB protocol stack of a USB host system
Security arrangements for a universal serial bus (USB) protocol stack of a USB host system are provi ...
- 嵌入式Linux驱动学习之路(二十)USB设备驱动
USB在接入系统的时候,以0的设备ID和主机通信,然后由主机为其分配新的ID. 在主机端,D+和D-都是下拉接地的.而设备端的D-接上拉时,表明此设备为高速设备:12M/s. D+接上拉时则是全速设备 ...
- [OrangePi] Booting from USB drive
You can also boot from USB drive partition. The file named cmdline.txt must exist on BOOT (fat) part ...
- Qt 获取usb设备信息 hacking
/************************************************************************** * Qt 获取usb设备信息 hacking * ...
- 如何忽略usb host 模式设备连接确认对话框
<li class="alt"><span><span>package android.hardware.usb; </span> ...
随机推荐
- 各大型网站架构分析收集-原网址http://blog.csdn.net/lovingprince/article/details/3379710
1. PlentyOfFish 网站架构学习http://www.dbanotes.net/arch/plentyoffish_arch.html 采取 Windows 技术路线的 Web 2.0 站 ...
- tensorFlow(二)线性回归
需要TensorFlow基础,见TensorFlow(一) 原理默认了解不赘述 实例: 模型创建: #!/usr/bin/python # -*- coding: utf-8 -* import te ...
- MySQL免安装版错误解决方案
在进行mysql zip版本的安装时,遇到如下的错误 一.提示 无法启动此程序,丢失 msvcr140.dll 解决方法 下载Visual C++ Redistributable for Visual ...
- vscode 完全支持zeng code的写法
一.快速编写HTML代码 1. 初始化 HTML文档需要包含一些固定的标签,比如<html>.<head>.<body>等,现在你只需要1秒钟就可以输入这些标签. ...
- 支持续传功能的ASP.NET WEB API文件下载服务
先把原文地址放上来,随后翻译
- python day19--面向对象,接口,封装
#1.接口类,对象类.规范类中方法的统一. # 第一版:三个类,每个类中有相同的方法 # class Alipay: # def __init__(self,money): # self.money= ...
- 剑指Offer 23. 二叉搜索树的后序遍历序列 (二叉搜索树)
题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 题目地址 https://www.nowcoder ...
- Python之DataFrame常用方法小结
https://blog.csdn.net/a786150017/article/details/78573055
- VUE开发SPA之微信授权登录
SPA单页应用中微信授权登录的一点思路 单页应用应该如何解决微信授权登录的尴尬跳转?后退无法返回?主要遇到的问题就是 先进入单页应用,一边渲染页面一边判断用户有没有登录,当判断到没有登录时异步数据请求 ...
- 常用解压包命令----tar--rar
systemctl stop firewalld --关闭 linux服务器防火墙 1.*.tar 用 tar –xvf 解压 2.*.gz 用 gzip -d或者gunzip 解压 3.*. ...