perform up to 60 attmepts to re-connect to gpsd and print more status
This commit is contained in:
parent
346b5d00a9
commit
a1aa41e813
24
gpsdate.c
24
gpsdate.c
|
@ -31,10 +31,15 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
|
|
||||||
#include <gps.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 struct gps_data_t gpsdata;
|
||||||
|
|
||||||
static void callback(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);
|
rc = settimeofday(&tv, NULL);
|
||||||
gps_close(gpsdata);
|
gps_close(gpsdata);
|
||||||
if (rc == 0)
|
if (rc == 0) {
|
||||||
|
printf("Successfully set RTC time to GPSD time\n");
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
else
|
} else {
|
||||||
|
fprintf(stderr, "Error setting RTC: %d (%s)\n",
|
||||||
|
errno, strerror(errno));
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
char *host = "localhost";
|
char *host = "localhost";
|
||||||
|
int i, rc;
|
||||||
|
|
||||||
if (argc > 1)
|
if (argc > 1)
|
||||||
host = argv[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",
|
fprintf(stderr, "no gpsd running or network error: %d, %s\n",
|
||||||
errno, gps_errstr(errno));
|
errno, gps_errstr(errno));
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
|
|
Loading…
Reference in New Issue