Welcome 微信登录

首页 / 操作系统 / Linux / Linux poll机制简析

使用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 ",
         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! ");
        }
    }
}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 ", fd);
   
    if (fd < 0)
    {
        printf("can"t open! ");
    }    /* [cgw]: 设置文件标识符 */
    fds[0].fd   = fd;
    /* [cgw]: 设置应用程序要响应的事件 */
    fds[0].events = POLLIN;    while (1)
    {
        /* [cgw]: 休眠5S */
        ret = poll(fds, 1, 5000);
       
        /* [cgw]: 唤醒或超时 */
        printf("wake up! ");
        if (ret == 0)
        {
            printf("time out ");
        }
        else
        {
            my_signal_fun(arg);
        }
    }
   
    return 0;
}2. 实验2.1安装驱动程序:insmod input_subsys_drv.ko1 # insmod input_subsys_drv.ko2 input: input_subsys_dev as /class/input/input13 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 out3. 现象分析按一下按键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本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-10/136249.htm