-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKeyboardDevice.cpp
172 lines (159 loc) · 5.54 KB
/
KeyboardDevice.cpp
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "KeyboardDevice.h"
#include <QEventLoop>
KeyboardDevice::KeyboardDevice(QObject* parent):QIODevice(parent){
this->usbDevice = new QUsbDevice(this);
// pid,vid
//this->usbDevice->setId(QUsb::Id(0x4321,0x1234));
this->usbDevice->setId(QUsb::Id(ProductId,VendorId));
// config id,interface id,alternative
this->usbDevice->setConfig(QUsb::Config(1,InterfaceId,0));
// set the loglevel
this->usbDevice->setLogLevel(QUsb::logWarning);
this->inputEndpoint = new QUsbEndpoint(this->usbDevice,QUsbEndpoint::bulkEndpoint,InputEndPoint|EndPointId);
this->outputEndpoint = new QUsbEndpoint(this->usbDevice,QUsbEndpoint::bulkEndpoint,OutputEndPoint|EndPointId);
connect(this->inputEndpoint,&QUsbEndpoint::readyRead,this,&KeyboardDevice::readyReadDelegate);
connect(this->outputEndpoint,&QUsbEndpoint::bytesWritten,this,&KeyboardDevice::byteWrittenDelegate);
connect(this->inputEndpoint,&QUsbEndpoint::error,this,&KeyboardDevice::errorDelegate);
connect(this->outputEndpoint,&QUsbEndpoint::error,this,&KeyboardDevice::errorDelegate);
}
KeyboardDevice::~KeyboardDevice(){
}
bool KeyboardDevice::waitForReadyRead(int msecs){
return this->inputEndpoint->waitForReadyRead(msecs);
}
bool KeyboardDevice::waitForBytesWritten(int msecs){
return this->outputEndpoint->waitForBytesWritten(msecs);
}
qint64 KeyboardDevice::bytesAvailable() const{
return this->inputEndpoint->bytesAvailable();
}
qint64 KeyboardDevice::bytesToWrite() const{
return this->outputEndpoint->bytesToWrite();
}
void KeyboardDevice::close(){
if(this->inputEndpoint->isOpen()){
this->inputEndpoint->close();
}
if(this->outputEndpoint->isOpen()){
this->outputEndpoint->close();
}
if(this->usbDevice->isConnected()){
this->usbDevice->close();
}
}
bool KeyboardDevice::isSequential() const{
return true;
}
bool KeyboardDevice::open(OpenMode mode){
int ret;
// 打开USB设备
ret=this->usbDevice->open();
if(ret!=QUsbDevice::statusOK){
this->setErrorString(tr("failed to open usb device: %1").arg(ret));
return false;
}
// 打开输入接口
if(mode & ReadOnly){
if(this->inputEndpoint->open(ReadOnly)){
this->inputEndpoint->setPolling(true);
}else{
this->setErrorString(tr("failed to open input endpoint:%1.").arg(this->inputEndpoint->errorString()));
this->close();
return false;
}
}
// 打开输出接口
if(mode & WriteOnly){
if(!this->outputEndpoint->open(WriteOnly)){
this->setErrorString(tr("failed to open output endpoint:%1").arg(this->outputEndpoint->errorString()));
this->close();
return false;
}
}
return true;
}
qint64 KeyboardDevice::readData(char *data, qint64 maxlen){
// I am just a delegate, do nothing in real io
Q_UNUSED(data);
Q_UNUSED(maxlen);
return 0;
}
qint64 KeyboardDevice::writeData(const char *data, qint64 len){
// I am just a delegate, do nothing in real io
Q_UNUSED(data);
Q_UNUSED(len);
return 0;
}
qint64 KeyboardDevice::write(const char *data, qint64 maxSize){
return this->outputEndpoint->write(data,maxSize);
}
qint64 KeyboardDevice::write(const char *data){
return this->outputEndpoint->write(data);
}
qint64 KeyboardDevice::write(const QByteArray &byteArray){
return this->outputEndpoint->write(byteArray);
}
qint64 KeyboardDevice::read(char* data, qint64 maxSize){
return this->inputEndpoint->read(data,maxSize);
}
QByteArray KeyboardDevice::read(qint64 maxSize){
return this->inputEndpoint->read(maxSize);
}
QByteArray KeyboardDevice::readAll(){
return this->inputEndpoint->readAll();
}
qint64 KeyboardDevice::readLine(char *data, qint64 maxSize){
return this->inputEndpoint->readLine(data,maxSize);
}
QByteArray KeyboardDevice::readLine(qint64 maxSize){
return this->inputEndpoint->readLine(maxSize);
}
void KeyboardDevice::readyReadDelegate(){
emit readyRead();
}
void KeyboardDevice::byteWrittenDelegate(qint64 bytes){
emit bytesWritten(bytes);
}
void KeyboardDevice::errorDelegate(QUsbEndpoint::Status status){
qDebug()<<"error from "<<sender()<<":"<<status;
emit error((QUsbEndpoint*)sender(),status);
}
void KeyboardDevice::setLogLevel(QUsb::LogLevel logLevel){
this->usbDevice->setLogLevel(logLevel);
}
QUsb::LogLevel KeyboardDevice::logLevel(){
return this->usbDevice->logLevel();
}
QByteArray KeyboardDevice::request(quint8 command,QByteArray req,quint8* statusCode){
QByteArray requestPackage = QByteArray();
QByteArray responsePackage = QByteArray();
quint8 status;
quint16 packageLength = req.size();
QEventLoop loop;
if(statusCode==nullptr){
statusCode = &status;
}
// 命令
requestPackage.append(command);
// 参数区长度
requestPackage.append((char*)&packageLength,sizeof(packageLength));
// 参数区
requestPackage.append(req);
// 发送请求
this->write(requestPackage);
// 等待数据到来
connect(this->inputEndpoint,&QUsbEndpoint::readyRead,&loop,&QEventLoop::quit);
loop.exec();
disconnect(this->inputEndpoint,&QUsbEndpoint::readyRead,&loop,&QEventLoop::quit);
// 读取状态码
this->read((char*)statusCode,1);
// 读取包长度
this->read((char*)&packageLength,2);
// 读取响应主体
QByteArray responseBody;
while(responseBody.length()<packageLength){
this->waitForReadyRead(10);
responseBody.append(this->read(qMin(1000,packageLength-responseBody.length())));
}
return responseBody;
}