Now using poll(2).
authorheiko
Wed, 23 Jan 2002 19:23:14 +0100
changeset 17 b51d4c6816e7
parent 16 6b2a0de29990
child 18 d1686272f84d
Now using poll(2).
me8100_test_int/test.c
--- a/me8100_test_int/test.c	Tue Jan 22 20:36:00 2002 +0100
+++ b/me8100_test_int/test.c	Wed Jan 23 19:23:14 2002 +0100
@@ -1,33 +1,14 @@
-/*
- * Source File : me8100_test_int.c                                            
- * Destination : me8100_test_int.out                                              
- * Author      : GG (Guenter Gebhardt)                                 
- *    
- *                                                                     
- * File History: Version   Date       Editor   Action                  
- *---------------------------------------------------------------------
- *               1.00.00   01.07.12   GG       first release           
- *                                                                     
- *---------------------------------------------------------------------
- *                                                                     
- * Description:
- *   This program shows the usage of the driver and the interrupt
- *   facility of the me8100. First the board is configured, in order to
- *   generate an interrupt when a bit pattern of 0x0001 on port a and a bit
- *   pattern of 0x0100 an port b is pending. Then the board is configured, 
- *   in order to generate an interrupt with a bit mask of 0x0001 on port a 
- *   and a bit mask of 0x0100 on port b. 
- *   We install a signal handler, which is informed by the interrupt routine 
- *   by signalling of the driver, when a interrupt occures.
- */
 #include <stdio.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <sys/ioctl.h>
+#include <sys/poll.h>
 #include <signal.h>
 #include <stdlib.h>
 #include "me8100.h"
 
+const char* DEVICE = "/dev/me8100_0a";
+const int MAX_FDS = 2;
 
 /* Prototypes */
 static void signal_handler(int);
@@ -42,16 +23,18 @@
 static int file_handle = -1;
 
 int main(void){
-  int err = 0;
-  int oflags = 0;
+  int oflags;
+  int err;
+
+  struct pollfd fds[MAX_FDS];
 
   printf("IRQ Test %d\n", getpid());
 
-  file_handle = open("/dev/me8100_0a", O_RDONLY, 0);
+  file_handle = open(DEVICE, O_RDONLY, 0);
 
   if(file_handle < 0){
-    perror("Can't open path");
-    return 1;
+    fprintf(stderr, "Can't open path %s: %m\n", DEVICE);
+    exit(EXIT_FAILURE);
   }
 
   /*---------------------- general setup ------------------------------------*/
@@ -60,28 +43,62 @@
   signal(SIGIO, signal_handler);
 
   /* set current process as owner of the path */
-  fcntl(file_handle, F_SETOWN, getpid());
+  err = fcntl(file_handle, F_SETOWN, getpid());
+  if (err < 0) {
+    perror("fcntl SETOWN");
+    exit(EXIT_FAILURE);
+  }
 
   /* read the flags of the path */
   oflags = fcntl(file_handle, F_GETFL);
+  if (err < 0) {
+    perror("fcntl GETFL");
+    exit(EXIT_FAILURE);
+  }
 
   /* Inform the driver to put the current process on the fasync queue */
-  fcntl(file_handle, F_SETFL, oflags | FASYNC); 
+  err = fcntl(file_handle, F_SETFL, oflags | FASYNC); 
+  if (err < 0) {
+    perror("fcntl SETFL");
+    exit(EXIT_FAILURE);
+  }
+
+  fds[0].fd = file_handle;
+  fds[0].events = POLLIN;
 
   printf("Waiting for Interrupts\n\n");
 
-  for(;;) {
-    sleep(10);
+  for(;;sleep(2)) {
+    int ready;
+    int i;
+    unsigned short val;
+
+    fds[0].revents = 0;
+    printf("Polling on fd %d\n", file_handle);
+    ready = poll(fds, 1, 1000);
+    if (ready < 0) { perror("poll()"); exit(EXIT_FAILURE); }
+
+    printf("polled, %d fds are ready\n", ready);
+
+    for (i = 0; i < ready; ++i) {
+      printf("fd %d: 0x%0x\n", i, fds[i].revents);
+      if (fds[i].revents & POLLIN) {
+	read(file_handle, &val, sizeof(val));
+	printf("read: 0x%04x\n", val);
+      }
+    }
+
+    printf("Waiting...\n");
   }
 
-  printf("Close path to me8100_0\n");
+  printf("Close path to %s\n", DEVICE);
   err = close(file_handle);
   if(err){
-    printf("Kann Pfad nicht schliessen\n");
-    return 1;
+    fprintf(stderr, "Can't close %s: %m\n", DEVICE);
+    exit(EXIT_FAILURE);
   }
 
-  return 1;
+  exit(EXIT_SUCCESS);
 }