int startLocationServices() { if (working) { return 0.0; } gpsdata.status = STATUS_NO_FIX; gpsdata.satellites_used = 0; gps_clear_fix(&(gpsdata.fix)); gps_clear_dop(&(gpsdata.dop)); gpsdata.fix.latitude=0; gpsdata.fix.longitude=0; logfile=stdout; gpsd_source_spec(NULL, &source); unsigned int flags = WATCH_ENABLE; if (gps_open("127.0.0.1" , "2947", &gpsdata) != 0) { fprintf(stderr,"LoacationService : no gpsd running or network error: %d, %s\n", errno, gps_errstr(errno)); return 0; } if (source.device != NULL) flags |= WATCH_DEVICE; gps_stream(&gpsdata, flags, source.device); fprintf(stderr,MAGENTA "Polling location services to determine if they will be used in this run : \n" NORMAL); fprintf(stderr,MAGENTA "If you don't want to wait use `sudo service gpsd stop`\n" NORMAL); unsigned int tries=3; unsigned int success=0; unsigned int i=0; working=1; //lets assume that we are working so that pollLocationServices will work for (i=0; i<tries; i++) { success+=pollLocationServices(); usleep(1000*1000); } if (success==0) { fprintf(stderr,RED "Location services failed to provide gps coordinates beeing switched off\n" NORMAL); working=0; } else if (success<tries) { fprintf(stderr,YELLOW "Location services failed to provide gps coordinates beeing switched off\n" NORMAL); working=1; } else if (success==tries) { fprintf(stderr,GREEN "Location services working fine.. :) \n" NORMAL); working=1; } return working; }
int main (int argc, char** argv) { GMainLoop* mainloop; DBusError error; /* initializes the gpsfix data structure */ gps_clear_fix(&gpsfix); /* catch all interesting signals */ signal (SIGTERM, quit_handler); signal (SIGQUIT, quit_handler); signal (SIGINT, quit_handler); //openlog ("gpxlogger", LOG_PID | LOG_NDELAY , LOG_DAEMON); //syslog (LOG_INFO, "---------- STARTED ----------"); print_gpx_header (); mainloop = g_main_loop_new (NULL, FALSE); dbus_error_init (&error); connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error); if (dbus_error_is_set (&error)) { syslog (LOG_CRIT, "%s: %s", error.name, error.message); return 3; } dbus_bus_add_match (connection, "type='signal'", &error); if (dbus_error_is_set (&error)) { syslog (LOG_CRIT, "unable to add match for signals %s: %s", error.name, error.message); return 4; } if (!dbus_connection_add_filter (connection, (DBusHandleMessageFunction)signal_handler, NULL, NULL)) { syslog (LOG_CRIT, "unable to register filter with the connection"); return 5; } dbus_connection_setup_with_g_main (connection, NULL); g_main_loop_run (mainloop); return 0; }
/*@-mustfreefresh -globstate@*/ int main(int argc, char **argv) { int ch; bool daemonize = false; struct method_t *mp, *method = NULL; progname = argv[0]; logfile = stdout; while ((ch = getopt(argc, argv, "dD:e:f:hi:lm:V")) != -1) { switch (ch) { case 'd': openlog(basename(progname), LOG_PID | LOG_PERROR, LOG_DAEMON); daemonize = true; break; #ifdef CLIENTDEBUG_ENABLE case 'D': debug = atoi(optarg); gps_enable_debug(debug, logfile); break; #endif /* CLIENTDEBUG_ENABLE */ case 'e': for (mp = methods; mp < methods + NITEMS(methods); mp++) if (strcmp(mp->name, optarg) == 0) method = mp; if (method == NULL) { (void)fprintf(stderr, "%s: %s is not a known export method.\n", progname, optarg); exit(1); } break; case 'f': /* Output file name. */ { char fname[PATH_MAX]; time_t t; size_t s; t = time(NULL); s = strftime(fname, sizeof(fname), optarg, localtime(&t)); if (s == 0) { syslog(LOG_ERR, "Bad template \"%s\", logging to stdout.", optarg); break; } logfile = fopen(fname, "w"); if (logfile == NULL) syslog(LOG_ERR, "Failed to open %s: %s, logging to stdout.", fname, strerror(errno)); break; } case 'i': /* set polling interfal */ timeout = (time_t) atoi(optarg); if (timeout < 1) timeout = 1; if (timeout >= 3600) fprintf(stderr, "WARNING: track timeout is an hour or more!\n"); break; case 'l': for (method = methods; method < methods + NITEMS(methods); method++) (void)printf("%s: %s\n", method->name, method->description); exit(0); case 'm': minmove = (double )atoi(optarg); break; case 'V': (void)fprintf(stderr, "gpxlogger revision " REVISION "\n"); exit(0); default: usage(); /* NOTREACHED */ } } if (daemonize && logfile == stdout) { syslog(LOG_ERR, "Daemon mode with no valid logfile name - exiting."); exit(1); } if (optind < argc) { gpsd_source_spec(argv[optind], &source); } else gpsd_source_spec(NULL, &source); #if 0 (void)fprintf(logfile,"<!-- server: %s port: %s device: %s -->\n", source.server, source.port, source.device); #endif /* initializes the some gpsdata data structure */ gpsdata.status = STATUS_NO_FIX; gpsdata.satellites_used = 0; gps_clear_fix(&(gpsdata.fix)); gps_clear_dop(&(gpsdata.dop)); /* catch all interesting signals */ (void)signal(SIGTERM, quit_handler); (void)signal(SIGQUIT, quit_handler); (void)signal(SIGINT, quit_handler); /*@-unrecog@*/ /* might be time to daemonize */ if (daemonize) { /* not SuS/POSIX portable, but we have our own fallback version */ if (daemon(0, 0) != 0) (void) fprintf(stderr,"demonization failed: %s\n", strerror(errno)); } /*@+unrecog@*/ //syslog (LOG_INFO, "---------- STARTED ----------"); if (method != NULL) { exit((*method->method)()); } else if (NITEMS(methods)) { exit((methods[0].method)()); } else { (void)fprintf(stderr, "%s: no export methods.\n", progname); exit(1); } }
void gpsmm::clear_fix(void) { gps_clear_fix(&(gps_state()->fix)); }
/*@ -branchstate -usereleased -mustfreefresh -nullstate -usedef @*/ int gps_unpack(char *buf, struct gps_data_t *gpsdata) /* unpack a gpsd response into a status structure, buf must be writeable. * gps_unpack() currently returns 0 in all cases, but should it ever need to * return an error status, it must be < 0. */ { libgps_debug_trace((DEBUG_CALLS, "gps_unpack(%s)\n", buf)); /* detect and process a JSON response */ if (buf[0] == '{') { const char *jp = buf, **next = &jp; while (next != NULL && *next != NULL && next[0][0] != '\0') { libgps_debug_trace((DEBUG_CALLS,"gps_unpack() segment parse '%s'\n", *next)); if (libgps_json_unpack(*next, gpsdata, next) == -1) break; #ifdef LIBGPS_DEBUG if (libgps_debuglevel >= 1) libgps_dump_state(gpsdata); #endif /* LIBGPS_DEBUG */ } #ifdef OLDSTYLE_ENABLE if (PRIVATE(gpsdata) != NULL) PRIVATE(gpsdata)->newstyle = true; #endif /* OLDSTYLE_ENABLE */ } #ifdef OLDSTYLE_ENABLE else { /* * Get the decimal separator for the current application locale. * This looks thread-unsafe, but it's not. The key is that * character assignment is atomic. */ char *ns, *sp, *tp; static char decimal_point = '\0'; if (decimal_point == '\0') { struct lconv *locale_data = localeconv(); if (locale_data != NULL && locale_data->decimal_point[0] != '.') decimal_point = locale_data->decimal_point[0]; } for (ns = buf; ns; ns = strstr(ns + 1, "GPSD")) { if ( /*@i1@*/ strncmp(ns, "GPSD", 4) == 0) { bool eol = false; /* the following should execute each time we have a good next sp */ for (sp = ns + 5; *sp != '\0'; sp = tp + 1) { tp = sp + strcspn(sp, ",\r\n"); eol = *tp == '\r' || *tp == '\n'; if (*tp == '\0') tp--; else *tp = '\0'; /* * The daemon always emits the Anglo-American and SI * decimal point. Hack these into whatever the * application locale requires if it's not the same. * This has to happen *after* we grab the next * comma-delimited response, or we'll lose horribly * in locales where the decimal separator is comma. */ if (decimal_point != '\0') { char *cp; for (cp = sp; cp < tp; cp++) if (*cp == '.') *cp = decimal_point; } /* note, there's a bit of skip logic after the switch */ switch (*sp) { case 'F': /*@ -mustfreeonly */ if (sp[2] == '?') gpsdata->dev.path[0] = '\0'; else { /*@ -mayaliasunique @*/ (void)strlcpy(gpsdata->dev.path, sp + 2, sizeof(gpsdata->dev.path)); /*@ +mayaliasunique @*/ gpsdata->set |= DEVICE_SET; } /*@ +mustfreeonly */ break; case 'I': /*@ -mustfreeonly */ if (sp[2] == '?') gpsdata->dev.subtype[0] = '\0'; else { (void)strlcpy(gpsdata->dev.subtype, sp + 2, sizeof(gpsdata->dev.subtype)); gpsdata->set |= DEVICEID_SET; } /*@ +mustfreeonly */ break; case 'O': if (sp[2] == '?') { gpsdata->set = MODE_SET | STATUS_SET; gpsdata->status = STATUS_NO_FIX; gps_clear_fix(&gpsdata->fix); } else { struct gps_fix_t nf; char tag[MAXTAGLEN + 1], alt[20]; char eph[20], epv[20], track[20], speed[20], climb[20]; char epd[20], eps[20], epc[20], mode[2]; char timestr[20], ept[20], lat[20], lon[20]; int st = sscanf(sp + 2, "%8s %19s %19s %19s %19s %19s %19s %19s %19s %19s %19s %19s %19s %19s %1s", tag, timestr, ept, lat, lon, alt, eph, epv, track, speed, climb, epd, eps, epc, mode); if (st >= 14) { #define DEFAULT(val) (val[0] == '?') ? NAN : atof(val) /*@ +floatdouble @*/ nf.time = DEFAULT(timestr); nf.latitude = DEFAULT(lat); nf.longitude = DEFAULT(lon); nf.ept = DEFAULT(ept); nf.altitude = DEFAULT(alt); /* designed before we split eph into epx+epy */ nf.epx = nf.epy = DEFAULT(eph) / sqrt(2); nf.epv = DEFAULT(epv); nf.track = DEFAULT(track); nf.speed = DEFAULT(speed); nf.climb = DEFAULT(climb); nf.epd = DEFAULT(epd); nf.eps = DEFAULT(eps); nf.epc = DEFAULT(epc); /*@ -floatdouble @*/ #undef DEFAULT if (st >= 15) nf.mode = (mode[0] == '?') ? MODE_NOT_SEEN : atoi(mode); else nf.mode = (alt[0] == '?') ? MODE_2D : MODE_3D; if (alt[0] != '?') gpsdata->set |= ALTITUDE_SET | CLIMB_SET; if (isnan(nf.epx) == 0 && isnan(nf.epy) == 0) gpsdata->set |= HERR_SET; if (isnan(nf.epv) == 0) gpsdata->set |= VERR_SET; if (isnan(nf.track) == 0) gpsdata->set |= TRACK_SET | SPEED_SET; if (isnan(nf.eps) == 0) gpsdata->set |= SPEEDERR_SET; if (isnan(nf.epc) == 0) gpsdata->set |= CLIMBERR_SET; gpsdata->fix = nf; (void)strlcpy(gpsdata->tag, tag, MAXTAGLEN + 1); gpsdata->set |= TIME_SET | TIMERR_SET | LATLON_SET | MODE_SET; gpsdata->status = STATUS_FIX; gpsdata->set |= STATUS_SET; } } break; case 'X': if (sp[2] == '?') gpsdata->online = (timestamp_t)-1; else { (void)sscanf(sp, "X=%lf", &gpsdata->online); gpsdata->set |= ONLINE_SET; } break; case 'Y': if (sp[2] == '?') { gpsdata->satellites_visible = 0; } else { int j, i1, i2, i3, i5; int PRN[MAXCHANNELS]; int elevation[MAXCHANNELS], azimuth[MAXCHANNELS]; int used[MAXCHANNELS]; double ss[MAXCHANNELS], f4; char tag[MAXTAGLEN + 1], timestamp[21]; (void)sscanf(sp, "Y=%8s %20s %d ", tag, timestamp, &gpsdata->satellites_visible); (void)strlcpy(gpsdata->tag, tag, MAXTAGLEN); if (timestamp[0] != '?') { gpsdata->set |= TIME_SET; } for (j = 0; j < gpsdata->satellites_visible; j++) { PRN[j] = elevation[j] = azimuth[j] = used[j] = 0; ss[j] = 0.0; } for (j = 0, gpsdata->satellites_used = 0; j < gpsdata->satellites_visible; j++) { if ((sp != NULL) && ((sp = strchr(sp, ':')) != NULL)) { sp++; (void)sscanf(sp, "%d %d %d %lf %d", &i1, &i2, &i3, &f4, &i5); PRN[j] = i1; elevation[j] = i2; azimuth[j] = i3; ss[j] = f4; used[j] = i5; if (i5 == 1) gpsdata->satellites_used++; } } /*@ -compdef @*/ memcpy(gpsdata->PRN, PRN, sizeof(PRN)); memcpy(gpsdata->elevation, elevation, sizeof(elevation)); memcpy(gpsdata->azimuth, azimuth, sizeof(azimuth)); memcpy(gpsdata->ss, ss, sizeof(ss)); memcpy(gpsdata->used, used, sizeof(used)); /*@ +compdef @*/ } gpsdata->set |= SATELLITE_SET; break; } #ifdef LIBGPS_DEBUG if (libgps_debuglevel >= 1) libgps_dump_state(gpsdata); #endif /* LIBGPS_DEBUG */ /* * Skip to next GPSD when we see \r or \n; * we don't want to try interpreting stuff * in between that might be raw mode data. */ if (eol) break; } } } } #endif /* OLDSTYLE_ENABLE */ #ifndef USE_QT libgps_debug_trace((DEBUG_CALLS, "final flags: (0x%04x) %s\n", gpsdata->set,gps_maskdump(gpsdata->set))); #endif return 0; }