summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2013-04-10 12:11:12 +0200
committerHarald Welte <laforge@gnumonks.org>2013-04-10 12:11:12 +0200
commita1aa41e8132133a5b84eafbddd0a5c08560ed2b4 (patch)
treeebe057d84d7d335aa58a70cee490e60c33860a4b
parent346b5d00a9785c74f111f1b8d44ec563687995d4 (diff)
downloadgpsdate-a1aa41e8132133a5b84eafbddd0a5c08560ed2b4.tar.gz
gpsdate-a1aa41e8132133a5b84eafbddd0a5c08560ed2b4.tar.bz2
gpsdate-a1aa41e8132133a5b84eafbddd0a5c08560ed2b4.tar.xz
gpsdate-a1aa41e8132133a5b84eafbddd0a5c08560ed2b4.zip
perform up to 60 attmepts to re-connect to gpsd and print more status
-rw-r--r--gpsdate.c24
1 files changed, 21 insertions, 3 deletions
diff --git a/gpsdate.c b/gpsdate.c
index 2b0b510..55a2af6 100644
--- a/gpsdate.c
+++ b/gpsdate.c
@@ -31,10 +31,15 @@
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
+#include <unistd.h>
+#include <string.h>
#include <sys/time.h>
#include <gps.h>
+#define NUM_RETRIES 60 /* Number of gpsd re-connects */
+#define RETRY_SLEEP 1 /* Seconds to sleep between re-connects */
+
static struct gps_data_t gpsdata;
static void callback(struct gps_data_t *gpsdata)
@@ -51,20 +56,33 @@ static void callback(struct gps_data_t *gpsdata)
rc = settimeofday(&tv, NULL);
gps_close(gpsdata);
- if (rc == 0)
+ if (rc == 0) {
+ printf("Successfully set RTC time to GPSD time\n");
exit(EXIT_SUCCESS);
- else
+ } else {
+ fprintf(stderr, "Error setting RTC: %d (%s)\n",
+ errno, strerror(errno));
exit(EXIT_FAILURE);
+ }
}
int main(int argc, char **argv)
{
char *host = "localhost";
+ int i, rc;
if (argc > 1)
host = argv[1];
- if (gps_open(host, DEFAULT_GPSD_PORT, &gpsdata)) {
+ for (i = 1; i <= NUM_RETRIES; i++) {
+ printf("Attempt #%d to connect to gpsd at %s...\n", i, host);
+ rc = gps_open(host, DEFAULT_GPSD_PORT, &gpsdata);
+ if (!rc)
+ break;
+ sleep(RETRY_SLEEP);
+ }
+
+ if (rc) {
fprintf(stderr, "no gpsd running or network error: %d, %s\n",
errno, gps_errstr(errno));
exit(EXIT_FAILURE);