使用POLL機制代替Linux輸入子系統(input subsystem)之按鍵輸入和LED控制中的異步通知,實現同樣的效果。
1.代碼
只簡單修改input_subsys_test.c, input_subsys_drv.c不變
input_subsys_test.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/input.h>
int fd;
void my_signal_fun(int signum)
{
struct input_event buttons_event, leds_event;
/* [cgw]: 異步通知產生時返回的數據 */
read(fd, &buttons_event, sizeof(struct input_event));
/* [cgw]: 打印事件類型,事件碼,事件值 */
printf("type: 0x%x code: 0x%x value: 0x%x\n",
buttons_event.type,
buttons_event.code,
buttons_event.value);
/* [cgw]: 返回的是KEY_L或KEY_S值 */
if (buttons_event.code == KEY_L || buttons_event.code == KEY_S) {
/* [cgw]: 按鍵彈起 */
if (buttons_event.value == 0) {
/* [cgw]: 構造一個EV_LED事件 */
//leds_event.type = EV_SND;
leds_event.type = EV_LED;
//leds_event.code = SND_BELL;
leds_event.code = LED_MUTE;
/* [cgw]: KEY_L和KEY_S控制LED的亮滅 */
if (buttons_event.code == KEY_L) {
leds_event.value = 0xAA;
} else if (buttons_event.code == KEY_S) {
leds_event.value = 0xEE;
}
/* [cgw]: 發送LED控制事件 */
write(fd, &leds_event, sizeof(struct input_event));
printf("led write!\n");
}
}
}
int main(int argc, char **argv)
{
int ret, arg;
struct pollfd fds[1];
fd = open("/dev/event1", O_RDWR | O_NONBLOCK);
//printf("fd = 0x%x\n", fd);
if (fd < 0)
{
printf("can't open!\n");
}
/* [cgw]: 設置文件標識符 */
fds[0].fd = fd;
/* [cgw]: 設置應用程序要響應的事件 */
fds[0].events = POLLIN;
while (1)
{
/* [cgw]: 休眠5S */
ret = poll(fds, 1, 5000);
/* [cgw]: 喚醒或超時 */
printf("wake up!\n");
if (ret == 0)
{
printf("time out\n");
}
else
{
my_signal_fun(arg);
}
}
return 0;
}
2. 實驗
2.1
安裝驅動程序:
insmod input_subsys_drv.ko
1 # insmod input_subsys_drv.ko 2 input: input_subsys_dev as /class/input/input1 3 input subsys open! 4 input subsys init!
運行應用程序
./input_subsys_test
# ./input_subsys_test
wake up!
type: 0x1 code: 0x26 value: 0x1
wake up!
type: 0x1 code: 0x26 value: 0x0
led event!
value: 0xaa
led write!
wake up!
type: 0x11 code: 0x7 value: 0xaa
wake up!
type: 0x1 code: 0x1f value: 0x1
wake up!
type: 0x1 code: 0x1f value: 0x0
led event!
value: 0xee
led write!
wake up!
type: 0x11 code: 0x7 value: 0xee
wake up!
type: 0x1 code: 0x1c value: 0x1
wake up!
type: 0x1 code: 0x1c value: 0x0
wake up!
time out
wake up!
time out
3. 現象分析
按一下按鍵KEY_L,終端輸出:
wake up!
type: 0x1 code: 0x26 value: 0x1
wake up!
type: 0x1 code: 0x26 value: 0x0
led event!
value: 0xaa
led write!
wake up!
type: 0x11 code: 0x7 value: 0xaa