目录
udev 规则的匹配键

记录下相关指令以及上图的识别词条,方便固件烧写后的调试

固件烧写

- a. 把手机接入开发板
- b. 安装adb工具,在终端输入adb安装指令: sudo apt-get install adb
- c. dmesg能查看到手机接入的信息,但是输入adb devices会出现提醒
- dinsufficient permissions for device: user in plugdev group; are your udev
- rules wrong?
- d. 配置文件,以支持USB设备的热拔插,支持UDEV的机制
- 在/etc/udev/rules.d 文件夹下创建规则文件
- cd /etc/udev/rules.d/
- sudo vim 51-android.rules
- 在文件中添加内容 SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", MODE="0666"
- e. 在手机开发者选项中,打开USB调试,重新拔插手机
- f. 手机弹出调试提醒,点确认手机调试模式
- adb shell input swipe 540 1300 540 500 100 向下滑动540是水平的,1300是竖直方向,下 是
- 500
- adb shell input swipe 540 500 540 1300 100 向上滑动
- adb shell "seq 3 | while read i;do input tap 350 1050 & input tap 350 1050 &
- sleep 0.01;done;" 点赞
- adb shell input keyevent 26 锁屏
uartTool.h
- int myserialGetchar (const int fd);
-
-
- int myserialOpen (const char *device, const int baud);
-
-
-
- void serialSendstring (const int fd, const char *s);
-
-
- int serialGetstring (const int fd,char *buffer);
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
-
- #include "wiringSerial.h"
-
- int myserialGetchar (const int fd)//新增加的 获取字符
- {
- char x ;
-
- if (read (fd, &x, 1) != 1)
- return -1 ;
-
- return x ;
- }
-
-
- int myserialOpen (const char *device, const int baud)
- {
- struct termios options ;
- speed_t myBaud ;
- int status, fd ;
-
- switch (baud){
- case 9600: myBaud = B9600 ; break ;
- case 115200: myBaud = B115200 ; break ;
- default:
- return -2 ;
-
- }
- if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)
- return -1 ;
-
- fcntl (fd, F_SETFL, O_RDWR) ;
-
- // Get and modify current options:
-
- tcgetattr (fd, &options) ;
-
- cfmakeraw (&options) ;
- cfsetispeed (&options, myBaud) ;
- cfsetospeed (&options, myBaud) ;
-
- options.c_cflag |= (CLOCAL | CREAD) ;
- options.c_cflag &= ~PARENB ;
- options.c_cflag &= ~CSTOPB ;
- options.c_cflag &= ~CSIZE ;
- options.c_cflag |= CS8 ;
- options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;
- options.c_oflag &= ~OPOST ;
-
- options.c_cc [VMIN] = 0 ;
- options.c_cc [VTIME] = 100 ; // Ten seconds (100 deciseconds)
-
- tcsetattr (fd, TCSANOW, &options) ;
-
- ioctl (fd, TIOCMGET, &status);
-
- status |= TIOCM_DTR ;
- status |= TIOCM_RTS ;
-
- ioctl (fd, TIOCMSET, &status);
-
- usleep (10000) ; // 10mS
-
- return fd ;
- }
-
-
-
- void serialSendstring (const int fd, const char *s)
- {
- int ret;
- ret = write (fd, s, strlen (s));
- if (ret < 0)
- printf("Serial Puts Error\n");
- }
-
- int serialGetstring (const int fd,char *buffer)
- {
- int n_read;
- n_read=read(fd,buffer,32);
- return n_read;
- }
uartTest.c
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
-
- #include "uartTool.h"
-
- int fd;
-
-
- void *readSerial()
- {
- char cmd;
- while(1){
-
- cmd=myserialGetchar(fd);
- switch(cmd){
- case 'N':
- printf("next\n");
- system("adb shell input swipe 540 1300 540 500 100");
- break;
- case 'P':
- printf("pre\n");
- system("adb shell input swipe 540 500 540 1300 100");
- break;
- case 'Z':
- printf("zan\n");
- system("adb shell \"seq 3 | while read i;do input tap 350 1050 &input tap 350 1050 & sleep 0.01;done;\"");
- break;
- case 'Q':
- printf("quit\n");
- system("adb shell input keyevent 26");
- break;
- }
-
- }
- }
-
-
- int main(int agrc,char **argv)
- {
- pthread_t readt;
- char deviceName[32]={'\0'};
- if(agrc < 2){
- printf("uage:%s /dev/ttyS?\n",argv[0]);
- return -1;
- }
- strcpy(deviceName,argv[1]);
- if((fd=myserialOpen(deviceName,115200))== 1){
- printf("open %s error!",deviceName);
- return -1;
- }
- pthread_create(&readt,NULL,readSerial,NULL);
-
- while(1){sleep(10);}
-
- }
udev 是一个设备管理工具, udev 以 守护进程 的形式运行,通过侦听内核发出来的 uevent 来管理 /dev 目录下的设备文件。 udev 在用户空间运行,而不在内核空间 运行。它能够根据系统中的硬件设备的状态动态更新设备文件,包括设备文件的创建,删除等。设备文件通常放在 /dev 目录下。使用 udev 后,在 /dev 目录下就只包含系统中真正存在的设备。

Linux Daemon (守护进程)是运行在后台的一种特殊进程。它独立于控制终端并且周期性地执行某种任务或等待处理某些发生的事件。它不需要用户输入就能运行而且提供某种服务,不是对整个系统就是对某个用户程序提供服务。Linux 系统的大多数服务器就是通过守护进程实现的。常见的守护进程包括系统日志进程syslogd 、 web 服务器 httpd 、邮件服务器 sendmail 和数据库服务器mysqld等。守护进程的名称通常以 d 结尾
直接借助damon()函数完成。
#include int daemon ( int nochdir , int noclose );函数参数:nochdir :为 0 时表示将当前目录更改至 “ / ”noclose :为 0 时表示将标准输入、标准输出、标准错误重定向至 “ / dev / null”返回值:成功则返回 0 ,失败返回 - 1
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
-
- //C 库函数 char *asctime(const struct tm *timeptr) 返回一个指向字符串的指针,它代表了结
- //构 struct timeptr 的日期和时间。
- //C 库函数 struct tm *localtime(const time_t *timer) 使用 timer 的值来填充 tm 结构。
- //timer 的值被分解为 tm 结构,并用本地时区表示。
- /*
- struct tm {
- int tm_sec; 秒,范围从 0 到 59
- int tm_min; 分,范围从 0 到 59
- int tm_hour; 小时,范围从 0 到 23
- int tm_mday; 一月中的第几天,范围从 1 到 31
- int tm_mon; 月份,范围从 0 到 11
- int tm_year; 自 1900 起的年数
- int tm_wday; 一周中的第几天,范围从 0 到 6
- int tm_yday; 一年中的第几天,范围从 0 到 365
- int tm_isdst; 夏令时
- };
- */
-
-
- static bool flag = true;
-
- void handler(int sig)
- {
- printf("I got a signal %d\nI'm quitting.\n", sig);
- flag = false;
- }
- int main()
- {
- time_t t;
- int fd;
- //创建守护进程
- if(-1 == daemon(0, 0))
- {
- printf("daemon error\n");
- exit(1);
- }
- //设置信号处理函数
- struct sigaction act;
- act.sa_handler = handler;
- sigemptyset(&act.sa_mask);
- act.sa_flags = 0;
- if(sigaction(SIGQUIT, &act, NULL))
- {
- printf("sigaction error.\n");
- exit(0);
- }
- //进程工作内容
- while(flag)
- {
- fd = open("/home/orangepi/daemon.log", O_WRONLY | O_CREAT | O_APPEND,0644);
- if(fd == -1)
- {
- printf("open error\n");
- }
-
- t = time(0);
- char *buf = asctime(localtime(&t));
- write(fd, buf, strlen(buf));
- close(fd);
- sleep(10);
- }
- return 0;
- }
查看绝对路径,tdaemon为运行程序的名字

sudo vi /etc/rc.local 开机自启动,绝对路径加程序名字
需求:要求语音刷手机的程序一直保持运行,防止应用程序崩溃意外
- #include
- #include
-
- int main()
- {
- FILE *file;
- char buffer[128] = {'\0'};
- char *cmd = "ps -elf |grep douyinUtils|grep -v grep";
- file = popen(cmd, "r");
- fgets(buffer, 128, file);
- if(strstr(buffer, "douyinUtils") != NULL){
- return 0;
- }else{
- return -1;
- }
- printf("BUFFER:%s\n",buffer);
- }
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
-
- //C 库函数 char *asctime(const struct tm *timeptr) 返回一个指向字符串的指针,它代表了结
- //构 struct timeptr 的日期和时间。
- //C 库函数 struct tm *localtime(const time_t *timer) 使用 timer 的值来填充 tm 结构。
- //timer 的值被分解为 tm 结构,并用本地时区表示。
- /*
- struct tm {
- int tm_sec; 秒,范围从 0 到 59
- int tm_min; 分,范围从 0 到 59
- int tm_hour; 小时,范围从 0 到 23
- int tm_mday; 一月中的第几天,范围从 1 到 31
- int tm_mon; 月份,范围从 0 到 11
- int tm_year; 自 1900 起的年数
- int tm_wday; 一周中的第几天,范围从 0 到 6
- int tm_yday; 一年中的第几天,范围从 0 到 365
- int tm_isdst; 夏令时
- };
- */
-
-
- static bool flag = true;
-
- int judMent()
- {
- FILE *file;
- char buffer[128] = {'\0'};
- char *cmd = "ps -elf |grep douyinUtils|grep -v grep";
- file = popen(cmd, "r");
- fgets(buffer, 128, file);
- if(strstr(buffer, "douyinUtils") != NULL){
- return 0;
- }else{
- return -1;
- }
- printf("BUFFER:%s\n",buffer);
- }
-
-
- void handler(int sig)
- {
- printf("I got a signal %d\nI'm quitting.\n", sig);
- flag = false;
- }
- int main()
- {
- time_t t;
- int fd;
- //创建守护进程
- if(-1 == daemon(0, 0))
- {
- printf("daemon error\n");
- exit(1);
- }
- //设置信号处理函数
- struct sigaction act;
- act.sa_handler = handler;
- sigemptyset(&act.sa_mask);
- act.sa_flags = 0;
- if(sigaction(SIGQUIT, &act, NULL))
- {
- printf("sigaction error.\n");
- exit(0);
- }
- //进程工作内容
- while(flag)
- {
- if( judMent() == -1){
- system("/home/orangepi/douyin/douyinUtils /dev/ttyS5 &");//&是后台运行的意思
- }
- sleep(2);
- }
-
- return 0;
- }
sudo vi / etc / rc . local/home/orangepi/douyin/douyinUtils /dev/ttyS5 &
/home/orangepi/douyin/shouhuDouyin
KERNEL=="sda", NAME="my_root_disk", MODE="0660"
进入规则文件修改id也是跟前面的效果一样cd / etc / udev / rules . d /sudo vim 51 - android . rules

查看设备的情况:
ls /dev/bus/usb/001

udevadm info --attribute-walk --name=/dev/ 设备名字SUBSYSTEM=="usb", ATTRS{idVendor}=="2a70", ATTRS{idProduct}=="4ee7",MODE="0666"
ACTION :事件( uevent )的行为,例如: add (添加设备)、 remove (删除设备);KERNEL :内核设备名称,例如: sda , cdrom ;DEVPATH :设备的 devpath 路径;SUBSYSTEM :设备的子系统名称,例如: sda 的系统为 block ;BUS :设备在 devpath 里的总线名称,例如: usb ;DRIVER :设备在 devpath 的设备驱动名称,例如: ide-cdrom ;ID :设备在 devpath 里的识别号;SYSFS{filename} :设备的 devpath 路径下,设备的属性文件 "filename" 里的内容;ENV{key} :环境变量。在一条规则中,可以设定最多五条环境变量的 匹配键;PROGRAM :调用外部命令;RESULT :外部命令 PROGRAM 的返回结果。
Linux不同于windows在使用u盘时需要挂载u盘,一般为手动挂载比较麻烦,本文介绍了利用udev规则自动挂载u盘。
mount /dev/sda /mnt //sda需要挂载的U盘 /mnt 需要挂载的的文件夹
umount /mnt 卸载u盘
创建udev规则
创建文件,命名任意但必须以.rules后缀结尾
sudo vim /etc/udev/rules.d/usbblock.rules
大多数udev规则都在 /etc/udev/rules.d下面
ACTION=="add", SUBSYSTEMS=="usb", SUBSYSTEM=="block", RUN{program}+="/bin/mkdir /media/%k", RUN{program}+="/usr/bin/systemd-mount --no-block --collect $devnode /media/%k" //==表示判断,RUN表示执行命令。该语句的意识是检测是否有硬件加入,判断该硬件的关键字,在/media目录下创建以sd开头的文件,最后挂载u盘到该目录。
重启udev服务
sudo service udev restart
最后:
tree /media/ //就可以看到挂载成功后的信息了

目录