Poll mechanism and linuxpoll Mechanism

Source: Internet
Author: User

Poll mechanism and linuxpoll Mechanism

Use the POLL mechanism to replace the asynchronous notifications in the input subsystem of linux and the asynchronous notifications in the LED control to achieve the same effect.

1. Code

Only the input_subsys_test.c and input_subsys_drv.c remain unchanged.

Input_subsys_test.c

1 # include <sys/types. h> 2 # include <sys/stat. h> 3 # include <fcntl. h> 4 # include <stdio. h> 5 # include <poll. h> 6 # include <signal. h> 7 # include <sys/types. h> 8 # include <unistd. h> 9 # include <fcntl. h> 10 11 # include <linux/input. h> 12 13 14 15 int fd; 16 17 void my_signal_fun (int signum) 18 {19 struct input_event buttons_event, leds_event; 20 21/* [cgw]: data returned when an asynchronous notification is generated */22 read (fd, & buttons_event, sizeo F (struct input_event); 23 24/* [cgw]: print the event type, Event code, event value */25 printf ("type: 0x % x code: 0x % x value: 0x % x \ n ", 26 buttons_event.type, 27 buttons_event.code, 28 buttons_event.value); 29 30/* [cgw]: the returned KEY_L or KEY_S value */31 if (buttons_event.code = KEY_L | buttons_event.code = KEY_S) {32/* [cgw]: press the button to pop up */33 if (buttons_event.value = 0) {34 35/* [cgw]: Construct an EV_LED event */36 37 // leds_event.type = EV_SND; 38 l Eds_event.type = EV_LED; 39 // leds_event.code = SND_BELL; 40 leds_event.code = LED_MUTE; 41 42/* [cgw]: KEY_L and KEY_S control the LED light off */43 if (buttons_event.code = KEY_L) {44 then = 0xAA; 45} else if (buttons_event.code = KEY_S) {46 leds_event.value = 0xEE; 47} 48 49/* [cgw]: Send LED control event */50 write (fd, & leds_event, sizeof (struct input_event); 51 52 printf ("led write! \ N "); 53} 54} 55} 56 57 int main (int argc, char ** argv) 58 {59 int ret, arg; 60 struct pollfd fds [1]; 61 62 fd = open ("/dev/event1", O_RDWR | O_NONBLOCK); 63 64 // printf ("fd = 0x % x \ n", fd ); 65 66 if (fd <0) 67 {68 printf ("can't open! \ N "); 69} 70 71/* [cgw]: sets the File Identifier */72 fds [0]. fd = fd; 73/* [cgw]: Set the event to be responded by the application */74 fds [0]. events = POLLIN; 75 76 while (1) 77 {78/* [cgw]: Sleep for 5 S */79 ret = poll (fds, 1, 5000 ); 80 81/* [cgw]: wake-up or timeout */82 printf ("wake up! \ N "); 83 if (ret = 0) 84 {85 printf (" time out \ n "); 86} 87 else88 {89 my_signal_fun (arg ); 90} 91} 92 93 return 0; 94}


2. Experiment

2.1

Install the driver:

Insmod input_subsys_drv.ko

1 # insmod input_subsys_drv.ko2 input: input_subsys_dev as /class/input/input13 input subsys open!4 input subsys init!

 

Run the application

./Input_subsys_test

 1 # ./input_subsys_test  2 wake up! 3 type: 0x1 code: 0x26 value: 0x1 4 wake up! 5 type: 0x1 code: 0x26 value: 0x0 6 led event! 7 value: 0xaa 8 led write! 9 wake up!10 type: 0x11 code: 0x7 value: 0xaa11 wake up!12 type: 0x1 code: 0x1f value: 0x113 wake up!14 type: 0x1 code: 0x1f value: 0x015 led event!16 value: 0xee17 led write!18 wake up!19 type: 0x11 code: 0x7 value: 0xee20 wake up!21 type: 0x1 code: 0x1c value: 0x122 wake up!23 type: 0x1 code: 0x1c value: 0x024 wake up!25 time out26 wake up!27 time out


3. Symptom Analysis

Click KEY_L and the terminal outputs:

1 wake up!2 type: 0x1 code: 0x26 value: 0x13 wake up!4 type: 0x1 code: 0x26 value: 0x05 led event!6 value: 0xaa7 led write!8 wake up!9 type: 0x11 code: 0x7 value: 0xaa

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.