-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHotKeyService.cpp
More file actions
355 lines (295 loc) · 9.87 KB
/
HotKeyService.cpp
File metadata and controls
355 lines (295 loc) · 9.87 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include "HotKeyService.h"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/epoll.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <stddef.h>
#include <linux/input.h>
#include <list>
#include <signal.h>
#include <string.h>
using std::list;
#define DEV_PATH1 "/dev/input/event4"
#define DEV_PATH2 "/dev/input/event5"
#define UART_DEVICE_NAME "/dev/ttyUSB1"
static THREAD_TYPE staticThreadOfEpollGuard(void* arg) {
return ((HotKeyService*)arg)->ThreadOfEpollGuard();
}
HotKeyService::HotKeyService(){
for(int i=0;i<DEVICE_LEN;i++){
this->driver_fd[i]=-1;
}
this->epoller=new Epoller(1000,10);
}
void HotKeyService::set_speed(int fd, int baud){
unsigned char i;
int status;
struct termios Opt;
for(i=0; i<sizeof(speed_arr)/sizeof(int); i++)
{
if(speed_name[i] == baud)
{
tcflush(fd, TCIOFLUSH); //刷新输入输出数据,但是不读也不传送
cfsetispeed(&Opt, speed_arr[i]); //设置输入波特率
cfsetospeed(&Opt, speed_arr[i]); //设置输出波特率
//用于设置终端的相关参数
status = tcsetattr(fd, TCSANOW, &Opt); //TCSANOW:不等数据传输完毕就立即改变属性
if (status != 0)
{
perror("tcsetattr fd");
return;
}
tcflush(fd,TCIOFLUSH);
}
}
}
int HotKeyService::set_Parity(int fd, int databits, int stopbits, int parity){
struct termios options;
long vdisable;
if(tcgetattr(fd,&options)!=0)
{
perror("SetupSerial 1");
return(FALSE);
}
//设置数据位
options.c_cflag &= ~CSIZE;
switch(databits)
{
case 5:
options.c_cflag |= CS5;
break;
case 6:
options.c_cflag |= CS6;
break;
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
printf("Unsupported data size\n");
return (FALSE);
}
//设置检验位
switch (parity)
{
case 'n':
case 'N':
options.c_cflag &= ~PARENB; /* Clear parity enable */
options.c_iflag &= ~INPCK; /* Disable parity checking */
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/
options.c_iflag |= INPCK; /* Enable parity checking */
break;
case 'e':
case 'E':
options.c_cflag |= PARENB; /* Enable parity */
options.c_cflag &= ~PARODD; /* 转换为偶效验*/
options.c_iflag |= INPCK; /* Enable parity checking */
break;
default:
printf("Unsupported parity\n");
return (FALSE);
}
//设置停止位
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
printf("Unsupported stop bits\n");
return (FALSE);
}
tcflush(fd,TCIOFLUSH);
//控制字符设置
options.c_cc[VTIME] = 50; /* 设置超时5 seconds*/
options.c_cc[VMIN] = 0; /* 在计时器超时前,read返回[1,nbytes];若计时器超时,read返回0 */
options.c_cc[VINTR] = vdisable; /**//* Ctrl-c */
options.c_cc[VQUIT] = vdisable; /**//* Ctrl- */
options.c_cc[VERASE] = vdisable; /**//* del */
options.c_cc[VKILL] = vdisable; /**//* @ */
options.c_cc[VEOF] = vdisable; /**//* Ctrl-d */
options.c_cc[VSWTC] = vdisable; /**//* '' */
options.c_cc[VSTART] = vdisable; /**//* Ctrl-q */
options.c_cc[VSTOP] = vdisable; /**//* Ctrl-s */
options.c_cc[VSUSP] = vdisable; /**//* Ctrl-z */
options.c_cc[VEOL] = vdisable; /**//* '' */
options.c_cc[VREPRINT] = vdisable; /**//* Ctrl-r */
options.c_cc[VDISCARD] = vdisable; /**//* Ctrl-u */
options.c_cc[VWERASE] = vdisable; /**//* Ctrl-w */
options.c_cc[VLNEXT] = vdisable; /**//* Ctrl-v */
options.c_cc[VEOL2] = vdisable; /**//* '' */
/* 设置其他终端属性 */
#if 0
options.c_cflag |= ( CLOCAL | CREAD); //忽略DCD信号
options.c_cflag &= ~CRTSCTS; //关硬件流控制
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); //关回显,设置为noncanonical mode
options.c_oflag &= ~OPOST; //行式输出
options.c_iflag &= ~(IXON | IXOFF | IXANY); //关软件流控制
options.c_iflag |= IGNBRK;
#endif
/*
* Echo off, canonical mode off, extended input
* processing off, signal chars off.
*/
options.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
/*
* No SIGINT on BREAK, CR-to-NL off, input parity
* check off, don't strip 8th bit on input, output
* flow control off.
*/
options.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
/*
* Output processing off.
*/
options.c_oflag &= ~(OPOST);
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
perror("SetupSerial 3");
return (FALSE);
}
return (TRUE);
}
int HotKeyService::start(){
if(!this->openDevice(DEV_PATH1,&this->driver_fd[0])){
printf("HotKeyService::start - fail to open device\n");
return -1;
}
this->epoller->add(this->driver_fd[0]);
printf("DEV_PATH1 fd is %d\n",this->driver_fd[0]);
/*
if(!this->openDevice(DEV_PATH2,&this->driver_fd[1])){
printf("HotKeyService::start - fail to open device\n");
return -1;
}
this->epoller->add(this->driver_fd[1]);
printf("DEV_PATH2 fd is %d\n",this->driver_fd[1]);
*/
if(!this->openDevice(UART_DEVICE_NAME,&this->driver_fd[1])){
printf("HotKeyService::start - fail to open device\n");
return -1;
}
this->set_speed(this->driver_fd[1],9600); //设置串口波特率
if (this->set_Parity(this->driver_fd[1],8,1,'n') == FALSE) //设置8位数据位,1位停止位,无校验等其他设置。
{
printf("Set Parity Error\n");
}
this->epoller->add(this->driver_fd[1]);
printf("DEV_PATH2 fd is %d\n",this->driver_fd[1]);
if(!this->createThread()){
printf("HotKeyService::start - fail to create thread\n");
this->stop();
return -3;
}
printf("HotKeyService::start - done\n");
return 0;
}
void HotKeyService::stop(){
this->destroyThread();
for(int i=0;i<DEVICE_LEN;i++){
this->epoller->remove(this->driver_fd[i]);
this->closeDevice(&this->driver_fd[i]);
}
printf("HotKeyService::stop - done\n");
}
static bool set_non_blocking(int fd) {
int opts;
opts = fcntl(fd, F_GETFL);
if (opts < 0)
return false;
opts = (opts | O_NONBLOCK);
if (fcntl(fd, F_SETFL, opts) < 0)
return false;
return true;
}
bool HotKeyService::openDevice(char *name,int *fd){
if(*fd>0)
this->closeDevice(fd);
*fd=open(name, O_RDWR);
if(*fd<=0)
return false;
if(!set_non_blocking(*fd)){
printf("HotKeyService::openDevice - fail to set non blocking mode\n");
return false;
}
return true;
}
void HotKeyService::closeDevice(int *fd){
if(*fd>0){
close(*fd);
*fd=-1;
}
}
bool HotKeyService::createThread(){
this->isThreadRunning=true;
if(!CREATE_THREAD(&this->threadIdOfEpoll,staticThreadOfEpollGuard,this)){
this->isThreadRunning=false;
return false;
}
return true;
}
void HotKeyService::destroyThread(){
this->isThreadRunning=false;
pthread_join(this->threadIdOfEpoll,0);
}
void HotKeyService::destroyKeyPressThread(){
this->is_key_press_=false;
}
THREAD_TYPE HotKeyService::ThreadOfEpollGuard(){
struct input_event event;
int sockfd;
long long number=0;
int n=0;
list<int> sockfds;
while(this->isThreadRunning){
this->epoller->getSockfds(sockfds);
for(list<int>::iterator iter=sockfds.begin();iter!=sockfds.end();++iter){
if(*iter<=0) continue;
printf("read data fd is %d\n",*iter);
if(*iter==this->driver_fd[1]){
int nread;
char buff[512] = {0};
if((nread = read(*iter,buff,255))>0) //接收数据
{
printf("[RECEIVE] Len is %d,content is :\n",nread);
buff[nread]='\0';
printf("%s\n",buff);
}
}
else{
memset(&event,0,sizeof(event));
while(read(*iter, &event, sizeof(event))!=-1){
if(event.code!=0){
printf("event.code is %d event.type %d\n",event.code,event.type);
if (event.type == EV_KEY && event.value==1){
if(event.code==28){
n=(int)(number&0xffffff);
printf("card=%d\n",n);
number=0;
}else if(event.code==11){
number=number*10;
}else{
number=number*10+event.code-1;
}
}
}
}
}
}
}
pthread_exit(0);
return NULL;
}