1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
/*
* Copyright (C) 2023, KylinSoft Co., Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
**/
#include "usbthread.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/un.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/types.h>
#include <linux/netlink.h>
#include <errno.h>
#include <unistd.h>
#include <regex>
#include <iostream>
#include <QDebug>
#include <QThread>
#include <QProcess>
#define UEVENT_BUFFER_SIZE 2048
UsbThread::UsbThread()
{
}
UsbThread::~UsbThread()
{
}
void UsbThread::run()
{
int hotplug_sock = init_sock();
while(1) {
char buf[UEVENT_BUFFER_SIZE*2] = {0};
// recv 会等待usb信号连入
recv(hotplug_sock, &buf, sizeof(buf), 0);
usbDeviceIdentify(QString(buf));
}
}
void UsbThread::usbDeviceIdentify(const QString &str)
{
if ( str.indexOf("bind") == 0
&& str.contains("pci")
&& !str.right( str.size() -1 - str.lastIndexOf('/') ).contains(":")
&& !str.right( str.size() -1 - str.lastIndexOf('/') ).contains(".")) {
QString path = QString("/sys") + str.right(str.size() - 1 - str.indexOf('@'));
QString deviceType = getDeivceTypeFromPath(path);
if (deviceType == "07")
emit addsignal();
}
// unbind@/devices/pci0000:00/0000:00:11.0/0000:02:01.0/usb1/1-1
if ( str.contains("unbind")
&& str.contains("pci")
&& !str.right( str.size() -1 - str.lastIndexOf('/') ).contains(":")
&& !str.right( str.size() -1 - str.lastIndexOf('/') ).contains(".")) {
emit removesignal();
}
return;
}
int UsbThread::init_sock()
{
struct sockaddr_nl snl;
const int buffersize = 16 * 1024 * 1024;
int retval;
memset(&snl, 0x00, sizeof(struct sockaddr_nl));
snl.nl_family = AF_NETLINK;
snl.nl_pid = getpid();
snl.nl_groups = 1;
int hotplug_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
if (hotplug_sock == -1)
{
printf("error getting socket: %s", strerror(errno));
return -1;
}
/* set receive buffersize */
setsockopt(hotplug_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));
retval = bind(hotplug_sock, (struct sockaddr *) &snl, sizeof(struct sockaddr_nl));
if (retval < 0) {
printf("bind failed: %s", strerror(errno));
close(hotplug_sock);
hotplug_sock = -1;
return -1;
}
return hotplug_sock;
}
QString UsbThread::getDeivceTypeFromPath(const QString &path)
{
QString res;
QString bInterfaceClass;
QStringList bInterfaceClassPathList = getRetFromCommand(QStringList{"find", path ,"-name", "bInterfaceClass"}).split("\n");
for (int i = 0; i < bInterfaceClassPathList.size(); i++) {
bInterfaceClass = getRetFromCommand(QStringList{"cat", bInterfaceClassPathList.at(i)});
if (bInterfaceClass == "00" || bInterfaceClass == "ff"){
continue;
}
res = bInterfaceClass;
}
return res;
}
QString UsbThread::getRetFromCommand(const QStringList &command)
{
QProcess proc;
QStringList options;
options << "-c"<< command.join(" ");
proc.closeWriteChannel();
proc.start("bash", options);
proc.waitForFinished();
QString res = QString(proc.readAll());
proc.close();
if(res.right(1) == "\n")
res.chop(1);
return res;
}
|