[LinuxPPS] PPS next steps...
George Spelvin
linux at horizon.com
Fri Feb 6 23:57:11 CET 2009
Just an addendum, I tested the poll() function, and it seems to work.
My ppstest.c changes follow.
--- ppstest.c 2009-02-03 09:05:56.000000000 -0500
+++ ppstest2.c 2009-02-06 16:54:44.000000000 -0500
@@ -4,6 +4,7 @@
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <poll.h>
#include <timepps.h>
@@ -15,7 +16,7 @@
printf("trying PPS source \"%s\"\n", path);
/* Try to find the source by using the supplied "path" name */
- ret = open(path, O_RDWR);
+ ret = open(path, O_RDONLY);
if (ret < 0) {
fprintf(stderr, "unable to open device \"%s\" (%m)\n", path);
return ret;
@@ -64,25 +65,45 @@
return 0;
}
+#define TIMEOUT 3 /* Seconds */
int fetch_source(int i, pps_handle_t *handle, int *avail_mode)
{
struct timespec timeout;
pps_info_t infobuf;
int ret;
+#if 1
+ struct pollfd pfd = { *handle, POLLIN, 0 };
+
+retry:
+ ret = poll(&pfd, 1, TIMEOUT * 1000);
+ if (ret < 0) {
+ if (errno == EINTR) {
+ fprintf(stderr, "time_pps_fetch() got a signal!\n");
+ goto retry;
+ }
+ perror("time_pps_fetch poll():");
+ return ret;
+ }
+ if (ret == 0) {
+ fprintf(stderr, "time_pps_fetch poll timeout:");
+ return -1;
+ }
+ /* Okay, ready to read */
+ fprintf(stderr, "time_pps_fetch poll success %d\n", ret);
/* create a zero-valued timeout */
- timeout.tv_sec = 3;
+ timeout.tv_sec = 0;
+ timeout.tv_nsec = 0;
+#else
+
+ timeout.tv_sec = TIMEOUT;
timeout.tv_nsec = 0;
retry:
- if (*avail_mode & PPS_CANWAIT) /* waits for the next event */
- ret = time_pps_fetch(*handle, PPS_TSFMT_TSPEC, &infobuf,
- &timeout);
- else {
+ if (!(*avail_mode & PPS_CANWAIT)) /* waits for the next event */
sleep(1);
- ret = time_pps_fetch(*handle, PPS_TSFMT_TSPEC, &infobuf,
- &timeout);
- }
+#endif
+ ret = time_pps_fetch(*handle, PPS_TSFMT_TSPEC, &infobuf, &timeout);
if (ret < 0) {
if (ret == -EINTR) {
fprintf(stderr, "time_pps_fetch() got a signal!\n");
@@ -93,9 +114,7 @@
return -1;
}
- printf("source %d - "
- "assert %ld.%09ld, sequence: %ld - "
- "clear %ld.%09ld, sequence: %ld\n",
+ printf("source %d - assert %ld.%09ld #%ld - clear %ld.%09ld #%ld\n",
i,
infobuf.assert_timestamp.tv_sec,
infobuf.assert_timestamp.tv_nsec,
@@ -117,7 +136,7 @@
int num;
pps_handle_t handle[4];
int avail_mode[4];
- int i = 0;
+ int i;
int ret;
/* Check the command line */
More information about the LinuxPPS
mailing list