コード例 #1
0
ファイル: gpxlogger.c プロジェクト: idaohang/gpsd-3
/*@-mustfreefresh -compdestroy@*/
static int socket_mainloop(void)
{
    unsigned int flags = WATCH_ENABLE;

    if (gps_open(source.server, source.port, &gpsdata) != 0) {
	(void)fprintf(stderr,
		      "%s: no gpsd running or network error: %d, %s\n",
		      progname, errno, gps_errstr(errno));
	exit(1);
    }

    if (source.device != NULL)
	flags |= WATCH_DEVICE;
    (void)gps_stream(&gpsdata, flags, source.device);

    print_gpx_header();
    for (;;) {
	if (!gps_waiting(&gpsdata, 5000000)) {
	    (void)fprintf(stderr, "%s: error while waiting\n", progname);
	    break;
	} else {
	    (void)gps_read(&gpsdata);
	    conditionally_log_fix(&gpsdata);
	}
    }
    (void)gps_close(&gpsdata);
    return 0;
}
コード例 #2
0
ファイル: s52ais.c プロジェクト: MasterLivens/S52
static int           _gpsdClientReadLoop(void)
// start gpsd client - loop forever
// return TRUE if main is exiting
{
    int ret = 0;

    // debug
    //gps_enable_debug(3, stderr);
    //gps_enable_debug(8, stderr);

    // heart of the client
    for (;;) {
        GMUTEXLOCK(&_ais_list_mutex);
        if (NULL == _ais_list) {
            g_print("s52ais:_gpsdClientRead() no AIS list .. main exited .. terminate gpsRead thread\n");
            ret = TRUE;
            goto exit;
        }
        GMUTEXUNLOCK(&_ais_list_mutex);

        if (FALSE == gps_waiting(&_gpsdata,  500*1000)) {    // wait 0.5 sec     (500*1000 uSec)
            //g_print("s52ais:_gpsdClientRead():gps_waiting() timed out\n");

            GMUTEXLOCK(&_ais_list_mutex);
            _updateTimeTag();
            GMUTEXUNLOCK(&_ais_list_mutex);

        } else {
            errno = 0;

            ret = gps_read(&_gpsdata);
            if (0 < ret) {
                // no error
                //g_print("s52ais:_gpsdClientRead():gps_read() ..\n");

                // handle AIS data
                GMUTEXLOCK(&_ais_list_mutex);
                _updateAISdata(&_gpsdata);
                GMUTEXUNLOCK(&_ais_list_mutex);

                continue;
            }

            if (0 == ret) {
                g_print("s52ais:_gpsdClientRead():gps_read(): NO DATA .. [ret=0, errno=%i]\n", errno);
                continue;
            } else {
                g_print("s52ais:_gpsdClientRead():gps_read(): socket error 4 .. GPSD died [ret=%i, errno=%i]\n", ret, errno);

                goto exit;
            }
        }
    }

exit:
    // exit thread
    GMUTEXUNLOCK(&_ais_list_mutex);

    return ret;
}
コード例 #3
0
ファイル: GPS.cpp プロジェクト: wflk/WarPi
gps_data_t GPS::get_current_location() {
    struct gps_data_t gps_data;
    int rc;

    if ((rc = gps_open("localhost", this->gpsd_port.c_str(), &gps_data)) == -1) {
        this->last_error = "GPS_OPEN";
        this->all_errors.push_back(this->last_error);
        return gps_data;
    }
    gps_stream(&gps_data, WATCH_ENABLE | WATCH_JSON, NULL);

    if (gps_waiting(&gps_data, this->timeout)) {
        if ((rc = gps_read(&gps_data)) == -1) {
            this->last_error == "GPS_READ";
            this->all_errors.push_back(this->last_error);
            return gps_data;
        } else {
            if ((gps_data.status == STATUS_FIX) && (gps_data.fix.mode == MODE_2D || gps_data.fix.mode == MODE_3D) &&
                !std::isnan(gps_data.fix.longitude) && !std::isnan(gps_data.fix.latitude)) {
                return gps_data;
            } else {
                this->last_error == "NO_GPS_DATA";
                this->all_errors.push_back(this->last_error);
                return gps_data;
            }
        }
    } else {
        this->last_error = "TIMEOUT";
        this->all_errors.push_back(this->last_error);
        return gps_data;
    }
}
コード例 #4
0
ファイル: gpsd2ivy.c プロジェクト: achambers16/paparazzi
static gboolean gps_periodic(gpointer data __attribute__ ((unused)))
{
    if (gps_waiting (gpsdata, 500)) {
        if (gps_read (gpsdata) == -1) {
            perror("gps read error");
        } else {
            update_gps(gpsdata, NULL, 0);
        }
    }
    return TRUE;
}
コード例 #5
0
ファイル: gpsd2ivy.c プロジェクト: Aishwaryabr/paparazzi
static gboolean gps_periodic(gpointer data __attribute__ ((unused)))
{
    if (gps_waiting (gpsdata, 500)) {
        if (gps_read (gpsdata) == -1) {
            perror("gps read error");
            //exit 2;
            //ret = 2;
            //running = false;
        } else {
            update_gps(gpsdata, NULL, 0);
        }
    }
}
コード例 #6
0
ファイル: gpsd2ivy.c プロジェクト: AshuLara/lisa
static gboolean gps_periodic(gpointer data __attribute__ ((unused)))
{
    int ret = gps_waiting(gpsdata);

    if (ret == -1)
    {
        perror("socket error\n");
        exit(2);
    }
    else if (ret) gps_poll(gpsdata);

    return 1;
}
コード例 #7
0
ファイル: libgps_sock.cpp プロジェクト: nohal/qtVlm
int gps_sock_mainloop(struct gps_data_t *gpsdata, int timeout, 
			 void (*hook)(struct gps_data_t *gpsdata))
/* run a socket main loop with a specified handler */
{
    for (;;) {
	if (!gps_waiting(gpsdata, timeout)) {
	    return -1;
	} else {
	    (void)gps_read(gpsdata);
	    (*hook)(gpsdata);
	}
    }
    //return 0;
}
コード例 #8
0
ファイル: gps_test.c プロジェクト: jstacy4/KU-EECS-541-SWM
int main()
{
    char * hostName = "127.0.0.1";
    char * hostPort = "2947";

    // initializes a GPS-data structure to hold the data collected by
    // the GPS, and sets up access to gpsd
    if( -1 == gps_open(hostName, hostPort, &gpsdata) )
    {
        printf("ERROR: gps_open returned -1");
    }

    gps_stream(&gpsdata, WATCH_ENABLE | WATCH_JSON, NULL);

    /* Put this in a loop with a call to a high resolution sleep () in it. */
    while(1)
    {
        /* Used to check whether there is new data from the
         * daemon. The second argument is the maximum amount of time
         * to wait (in microseconds) on input before returning */
        if( gps_waiting(&gpsdata, 500) )
        {
            if( -1 == gps_read (&gpsdata) )
            {
                printf("ERROR: gps_read returned -1");
            }
            else
            {
                /* Display data from the GPS receiver. */
                printf("\nSatellites Used:%d\n", gpsdata.satellites_used);
                
                /* Error estimates are at 95% confidence. */

                printf("Time:%f, Error:%f\n", gpsdata.fix.time, gpsdata.fix.ept);
                printf("Latitude:%f, Error:%f\n", gpsdata.fix.latitude, gpsdata.fix.epy);
                printf("Longitude:%f, Error:%f\n", gpsdata.fix.longitude, gpsdata.fix.epx);
                printf("Altitude:%f, Error:%f\n", gpsdata.fix.altitude, gpsdata.fix.epv);
                printf("Track:%f, Error:%f\n", gpsdata.fix.track, gpsdata.fix.epd);
                printf("Speed:%f, Error:%f\n", gpsdata.fix.speed, gpsdata.fix.eps);
                printf("Climb:%f, Error:%f\n", gpsdata.fix.climb, gpsdata.fix.epc);
            }
        }
        
        usleep(250000);
    }

    /* When you are done... */
    (void) gps_stream(&gpsdata, WATCH_DISABLE, NULL);
    (void) gps_close (&gpsdata);
}
コード例 #9
0
ファイル: redisClient.c プロジェクト: fapfap69/FapCarDash
// This is the main polling function
int GPScollectData() {

	char buffer[128];
	int readData;

	if (gps_waiting(&gpsdata, 1000000)) {  // Are there data ?
		readData = 0;
		readData = gps_read(&gpsdata);
        if (readData > 0) { // Read data
            if(gpsdata.online != 0) { // GPS not online
	            if(gpsdata.status != STATUS_NO_FIX) { // We have a fix point
					__recordStringValue("FIXED", "GPSstatus"); // set into DB
				    // gpsdata.satellites_used
					if( gpsdata.fix.mode ==  MODE_2D || gpsdata.fix.mode ==  MODE_3D) {
						unix_to_iso8601(gpsdata.fix.time, buffer, sizeof(buffer));
						__recordStringValue(buffer, "GPStime");
						__recordFloatValue(gpsdata.fix.latitude, "GPSlatit");
						__recordFloatValue(gpsdata.fix.longitude, "GPSlongi");
						sprintf(buffer,"{\n\"time\": %f,\n\"lat\": %f,\n\"lon\": %f\n}",gpsdata.fix.time, gpsdata.fix.latitude, gpsdata.fix.longitude);
						__recordStringValue(buffer, "GPSposition");
					}
					if( gpsdata.fix.mode ==  MODE_3D) {
						__recordFloatValue(gpsdata.fix.altitude, "GPSaltit");
						__recordFloatValue(gpsdata.fix.climb, "GPSclimb");
						__recordFloatValue(gpsdata.fix.speed, "GPSspeed");
						__recordFloatValue(gpsdata.fix.track, "GPStrack");
					}
				} else {
					__recordStringValue("FIXING", "GPSstatus");
				}
			} else {
				__recordStringValue("OFFLINE", "GPSstatus");
			}
        } else {
        	if( readData == 0 ) {
        		__recordStringValue("NODATA", "GPSstatus");
        		isGpsd = true;
        	} else {
        		__recordStringValue("NODEVICE", "GPSstatus");
        		isGpsd = false;
        	}
        }
    } else {
	//	__recordStringValue("NODATA", "GPSstatus");
		isGpsd = true;
    }
	return(RC_DATAVALID);
}
コード例 #10
0
ファイル: gpsd_iface.cpp プロジェクト: excursionator/uhd
    void _thread_fcn()
    {
        while (not boost::this_thread::interruption_requested()) {
            if (!gps_waiting(&_gps_data, CLICK_RATE)) {
                if (TIMEOUT < _timeout_cnt++)
                    _detected = false;
            } else {
                boost::unique_lock<boost::shared_mutex> l(_d_mutex);

                _timeout_cnt = 0;
                _detected = true;

                if (gps_read(&_gps_data) < 0)
                    throw std::runtime_error("error while reading");
            }
        }
    }
コード例 #11
0
ファイル: gps-watchdog.c プロジェクト: synexxus/gpsdate
/* local copy, as the libgps official version ignores gps_read() result */
static int my_gps_mainloop(struct gps_data_t *gdata,
			   int timeout,
			   void (*hook)(struct gps_data_t *gdata))
{
	int rc;

	for (;;) {
		if (!gps_waiting(gdata, timeout)) {
			return -1;
		} else {
			rc = gps_read(gdata);
			if (rc < 0)
				return rc;
			(*hook)(gdata);
		}
	}
	return 0;
}
コード例 #12
0
int pollLocationServices()
{
 if (!working) { return 0; }

  if (!gps_waiting(&gpsdata, 5000000))
     {
	    fprintf(stderr, "LoacationService : error while waiting\n");
        return 0;
     } else
	 {
	    fprintf(stderr,"!");
	    (void)gps_read(&gpsdata);
	    // This is not needed => //conditionally_log_fix(&gpsdata);
        return 1;
	 }

 return 0;
}
コード例 #13
0
ファイル: gps-utils.cpp プロジェクト: neotimbo/DCommRSCT3
//Will read data from stream. Passed a stream pointer.
void read_data(void *ptr){
    gps_data_t *gpsdata = (gps_data_t)ptr;


    while(){
        if(!gps_waiting(gps_ptr, 500000)){
            Cleanup(GPS_TIMEOUT);
        }
        else{
            errno = 0;
            if(gps_read(gpsdata) == -1){
                //print failure
                Cleanup(errno == 0 ? GPS_GONE : GPS_ERROR);
            }
            else{
                if(check_errors(gps_data)){
                    print_data(gps_data);
                }
            }
        }
    }
}
コード例 #14
0
ファイル: ui.gps-utils.cpp プロジェクト: idaohang/qtgps
/*------------------------------------
-- Method: Thread::exec
-- Date:    November 12 2013
-- Designer:    Sam Youssef
-- Programmer: Sam Youssef
--
-- INTERFACE:   
--              int Thread::exec(void)
-- RETURNS:
--            int. On termination.
--
-- NOTES:
-- Enters loop which polls gps device. Reads gps data into gpsd structure.
-- On success, emits a signal which is captured by GpsWindow. In turn, GpsWindow
-- displays the data. 
--
-- Is aborted when Thread class receives the appropriate disconnect signal
-- from GpsWindow, either on program close or via the user clicking 
-- disconnect.
--------------------------------------*/
int Thread::exec()
{   
    bool ab = abort = false;
    while( !ab  )
    {   
        mutex.lock();
        ab = abort;
        mutex.unlock();
        
        if( ! gps_waiting(gpsd, 2000000) ) {
            fprintf(stderr, "\tmygps: GPS timeout\n");
            break;
        }
                    
        if(gps_read(gpsd) == -1) {
            fprintf(stderr, "\targs:socket error 4\n");
            break;
        }
        
        emit(read_true());
        
    }
    return 1;
}
コード例 #15
0
ファイル: logger.cpp プロジェクト: mupuf/gps_tracking
bool mainLoop(struct gps_data_t *gps_data, int sockfd)
{
    int rc;

     while (1) {
        /* wait for 2 seconds to receive data */
        if (gps_waiting (gps_data, 2000000)) {
            /* read data */
            if ((rc = gps_read(gps_data)) == -1) {
                return false;
            } else {
                /* Display data from the GPS receiver. */
                if ((gps_data->status == STATUS_FIX) && 
                    (gps_data->fix.mode == MODE_2D || gps_data->fix.mode == MODE_3D) &&
                    !isnan(gps_data->fix.latitude) &&
                    !isnan(gps_data->fix.longitude)) {
                        if (isPrevValid) {
                            distance += earth_distance(prev.latitude, 
                                                       prev.longitude,
                                                       gps_data->fix.latitude,
                                                       gps_data->fix.longitude);
                        }

                        logDataOut(&gps_data->fix);

                        prev = gps_data->fix;
                        isPrevValid = 1;
                } else {
                    printf("no GPS data available\n");
                }
            }
        }
    }

    return true;
}
コード例 #16
0
ファイル: libgpsmm.cpp プロジェクト: vyacht/carambola
bool gpsmm::waiting(int timeout)
{
    return gps_waiting(gps_state(), timeout);
}
コード例 #17
0
ファイル: qth-data.c プロジェクト: kopaniapawel/gpredict-1
/** \brief Update the qth data by whatever method is appropriate.
 * \param qth the qth data structure to update
 * \param qth the time at which the qth is to be computed. this may be ignored by gps updates.
 */
gboolean qth_data_update(qth_t * qth, gdouble t)
{
    gboolean retval = FALSE;

#ifdef HAS_LIBGPS
    guint num_loops = 0;
#endif

    switch (qth->type)
    {
    case QTH_STATIC_TYPE:
        /* never changes */
        break;
    case QTH_GPSD_TYPE:
        if (((t - qth->gpsd_update) > 30.0 / 86400.0) && (t - qth->gpsd_connected > 30.0 / 86400.0))
        {
            /* if needed restart the gpsd interface */
            qth_data_update_stop(qth);
            qth_data_update_init(qth);
            qth->gpsd_connected = t;

        }

        if (qth->gps_data != NULL)
        {

#ifdef HAS_LIBGPS
            switch (GPSD_API_MAJOR_VERSION)
            {
            case 4:
#if GPSD_API_MAJOR_VERSION==4
                while (gps_waiting(qth->gps_data) == TRUE)
                {
                    /* this is a watchdog in case there is a problem with the gpsd code. */
                    /* if the server was up and has failed then gps_waiting in 2.92 confirmed */
                    /* will return 1 (supposedly fixed in later versions.) */
                    /* if we do not do this as a while loop, the gpsd packets can backup  */
                    /*   and no longer be in sync with the gps receiver */
                    num_loops++;
                    if (num_loops > 1000)
                    {
                        retval = FALSE;
                        break;
                    }
                    if (gps_poll(qth->gps_data) == 0)
                    {
                        /* handling packet_set inline with 
                           http://gpsd.berlios.de/client-howto.html
                         */
                        if (qth->gps_data->set & PACKET_SET)
                        {
                            if (qth->gps_data->fix.mode >= MODE_2D)
                            {
                                if (qth->lat != qth->gps_data->fix.latitude)
                                {
                                    qth->lat = qth->gps_data->fix.latitude;
                                    retval = TRUE;
                                }
                                if (qth->lon != qth->gps_data->fix.longitude)
                                {
                                    qth->lon = qth->gps_data->fix.longitude;
                                    retval = TRUE;
                                }
                            }

                            if (qth->gps_data->fix.mode == MODE_3D)
                            {
                                if (qth->alt != qth->gps_data->fix.altitude)
                                {
                                    qth->alt = qth->gps_data->fix.altitude;
                                    retval = TRUE;
                                }
                            }
                            else
                            {
                                if (qth->alt != 0)
                                {
                                    qth->alt = 0;
                                    retval = TRUE;
                                }
                            }
                        }
                    }
                }
#endif
                break;
            case 5:
#if GPSD_API_MAJOR_VERSION==5
                while (gps_waiting(qth->gps_data, 0) == 1)
                {
                    /* see comment from above */
                    /* hopefully not needed but does not hurt anything. */
                    num_loops++;
                    if (num_loops > 1000)
                    {
                        retval = FALSE;
                        break;
                    }
                    if (gps_read(qth->gps_data) == 0)
                    {
                        /* handling packet_set inline with 
                           http://gpsd.berlios.de/client-howto.html
                         */
                        if (qth->gps_data->set & PACKET_SET)
                        {
                            if (qth->gps_data->fix.mode >= MODE_2D)
                            {
                                if (qth->lat != qth->gps_data->fix.latitude)
                                {
                                    qth->lat = qth->gps_data->fix.latitude;
                                    retval = TRUE;
                                }
                                if (qth->lon != qth->gps_data->fix.longitude)
                                {
                                    qth->lon = qth->gps_data->fix.longitude;
                                    retval = TRUE;
                                }
                            }

                            if (qth->gps_data->fix.mode == MODE_3D)
                            {
                                if (qth->alt != qth->gps_data->fix.altitude)
                                {
                                    qth->alt = qth->gps_data->fix.altitude;
                                    retval = TRUE;
                                }
                            }
                            else
                            {
                                if (qth->alt != 0)
                                {
                                    qth->alt = 0;
                                    retval = TRUE;
                                }
                            }
                        }
                    }
                }
#endif
                break;
            default:
                break;
            }
#endif
            if (retval == TRUE)
            {
                qth->gpsd_update = t;
            }
        }
        break;
    default:
        break;
    }
    /* check that data is valid */
    qth_validate(qth);

    /* update qra */
    if (longlat2locator(qth->lon, qth->lat, qth->qra, 2) != RIG_OK)
    {
        sat_log_log(SAT_LOG_LEVEL_ERROR,
                    _("%s: Could not set QRA for %s at %f, %f."),
                    __FUNCTION__, qth->name, qth->lon, qth->lat);
    }

    return retval;
}
コード例 #18
0
int main(){

	int gps_recv;
	struct gps_data_t gps_data;
	struct timeval tv;
	struct tm *tm_info;
	FILE *gps_file_ptr;
	
	int millisec;
	
	char file_path[] = "/home/odroid/Canon_camera/Canon_gps_log.csv";
	
	/* Open GPS device (GPSD) */
	gps_recv = gps_open("localhost", "2947", &gps_data);
	
	if (gps_recv == -1) {
		printf("code: %d, reason %s\n", gps_recv, gps_errstr(gps_recv) );
	}
	
	/* Start streaming GPS data */
	gps_stream(&gps_data, WATCH_ENABLE, NULL);
	
	char lat_marker;
	char lng_marker;

	double latitude;
	double longitude;
	double latitude_min;
	double longitude_min;
	
	gps_file_ptr = fopen (file_path, "a");

	int i;
	for (i=1;i<=50;i++) {
	
		if (gps_waiting(&gps_data, GPS_WAIT)) {
			/* Then GPS data is available, so read the data */
			gps_recv = gps_read(&gps_data);
			/* printf("%d\n", gps_recv); */
			
			/* Check for error in reading data */
			if (gps_recv == -1) {
				printf("Error reading GPS. code: %d, reason %s\n", gps_recv, gps_errstr(gps_recv) );
			}
			
			else {
				/* No problems in reading data, check for GPS fix*/
	
				/* printf("No errors %d -- fix: %d\n", MODE_3D, gps_data.fix.mode); */
				
				if ( (gps_data.status == STATUS_FIX) && (gps_data.fix.mode == MODE_2D || gps_data.fix.mode == MODE_3D) && !isnan(gps_data.fix.latitude) && !isnan(gps_data.fix.longitude) ) {
					
					latitude = fabs(gps_data.fix.latitude);
					longitude = fabs(gps_data.fix.longitude);

					latitude_min = (latitude - (int) latitude)*60;
					longitude_min = (longitude - (int) longitude)*60;
	
					if (gps_data.fix.latitude >= 0 ) { lat_marker = 'N'; }
					else { lat_marker = 'S'; }
					
					if (gps_data.fix.longitude >= 0 ) { lng_marker = 'E'; }
					else { lng_marker = 'W'; }
					
					printf("%f %c, %f %c \n", fabs(gps_data.fix.latitude), lat_marker, fabs(gps_data.fix.longitude), lng_marker);
					
					/* Write timestamp in YYYY-MM-DD_HH-MM-SS-sss */
					gettimeofday(&tv, NULL);
					char time_buffer[26];
					millisec = (long) (tv.tv_usec/1000.0 + 0.5); // Round to nearest millisec
					
					tm_info = localtime(&tv.tv_sec);
					
					strftime(time_buffer, 26, "%Y-%m-%d_%H-%M-%S-", tm_info);
					
					fprintf(gps_file_ptr, "%s", time_buffer);
					fprintf(gps_file_ptr, "%03d,", millisec);
					
					/* Write latititude and longitude in DD-MM-SS.sss */
					fprintf(gps_file_ptr, "%d-%d-%f,%c,", (int) latitude, (int) latitude_min, (latitude_min - (int) latitude_min )*60, lat_marker);
					fprintf(gps_file_ptr, "%d-%d-%f,%c,", (int) longitude, (int) longitude_min, (longitude_min - (int) longitude_min )*60, lng_marker);
					/* Write Altitude, Heading */
					if (gps_data.fix.mode == MODE_3D) {
						fprintf(gps_file_ptr, "%f,%f\n", gps_data.fix.altitude, gps_data.fix.track);
					}
					else {
						/* if not 3D fix, altitude is not defined */
						fprintf(gps_file_ptr, "%d,%f\n", 0, gps_data.fix.track);
					}
					
					/* fprintf(gps_file_ptr, "%f%c,%f%c\n", fabs(gps_data.fix.latitude), lat_marker, fabs(gps_data.fix.longitude), lng_marker); */
						
				}
			}
		}
		else {
		  printf("-\n");
			/* printf("\n ----- Did not wait long enough ----- \n");*/
		}
	}
	
	/* Close logging file */
	fclose(gps_file_ptr);
	
	/* Stop streaming data and close GPS connection */
	gps_stream(&gps_data, WATCH_DISABLE, NULL);
	gps_close(&gps_data);
	
	printf("GPS -- %d\n", gps_recv);
	
	return 0;
}
コード例 #19
0
ファイル: dwgps.c プロジェクト: glneo/direwolf
int dwgps_read (double *plat, double *plon, float *pspeed, float *pcourse, float *palt)
{
#if __WIN32__

	text_color_set(DW_COLOR_ERROR);
	dw_printf ("Internal error, dwgps_read, shouldn't be here.\n");
	return (-1);

#elif ENABLE_GPS

	int err;

	if (init_status != INIT_SUCCESS) {
	  text_color_set(DW_COLOR_ERROR);
	  dw_printf ("Internal error, dwgps_read without successful init.\n");
	  return (-1);
	}

#if USE_GPS_SHM

/*
 * Shared memory version.
 */

	err = gps_read (&gpsdata);

#if DEBUG
	dw_printf ("gps_read returns %d bytes\n", err);
#endif

#else

/* 
 * Socket version.
 */

	// Wait for up to 1000 milliseconds.
	// This should only happen in the beaconing thread so 
	// I'm not worried about other functions hanging.

        if (gps_waiting(&gpsdata, 1000)) {

	  err = gps_read (&gpsdata);
	}
	else {
	  gps_stream(&gpsdata, WATCH_ENABLE | WATCH_JSON, NULL);
	  sleep (1);
	}

#endif

	if (err > 0) {
	  /* Data is available. */

	  if (gpsdata.status >= STATUS_FIX && gpsdata.fix.mode >= MODE_2D) {

	     *plat = gpsdata.fix.latitude;
	     *plon = gpsdata.fix.longitude;
	     *pcourse = gpsdata.fix.track;		
	     *pspeed = MPS_TO_KNOTS * gpsdata.fix.speed; /* libgps uses meters/sec */

	     if (gpsdata.fix.mode >= MODE_3D) {
	       *palt = gpsdata.fix.altitude;
	       return (3);
	     }
	     return (2);
	   }

	   /* No fix.  Probably temporary condition. */
	   return (0);
	}
	else if (err == 0) {

	   /* No data available at the present time. */
	   return (0);
	}
	else {

	  /* More serious error. */
	  return (-1);
	}
#else 

	text_color_set(DW_COLOR_ERROR);
	dw_printf ("Internal error, dwgps_read, shouldn't be here.\n");
	return (-1);
#endif

} /* end dwgps_read */
コード例 #20
0
ファイル: argps.c プロジェクト: idaohang/argps
int main( int argc, char **argv )
{
	// Enforce proper command line arguments.
	if( argc != 2 )
	{
		fprintf( stderr, "Usage: %s <hostname>\n", argv[0] );
		exit( EXIT_FAILURE );
	}

	// Connect to remote host.
	int sockfd = createConnection( argv[1], PORT_NUMBER );
	if( sockfd < 0 )
	{
		fprintf( stderr, "Couldn't connect to %s.\n", argv[1] );
		exit( EXIT_FAILURE );
	}
	else
	{
		printf( "Connected to %s\n.", argv[0] );
	}

	// Connect to gpsd.
	if( gps_open( "localhost", DEFAULT_GPSD_PORT, &gpsData ) != 0 )
	{
		fprintf( stderr, "Couldn't connect to gpsd, errno = %d, %s.\n", errno, gps_errstr( errno ) );
		exit( EXIT_FAILURE );
	}
	else
	{
		printf( "Connected to gpsd.\n" );
	}

	// Register for updates from gpsd.
	gps_stream( &gpsData, WATCH_ENABLE | WATCH_JSON, NULL );

	printf( "Waiting for gps lock." );
	for(;;)
	{
		if( !gps_waiting( &gpsData, 5000000 ) )
		{
			fprintf( stderr, "GPS fix timed out.\n" );
			exit( EXIT_FAILURE );
		}
		else
		{
			if( gps_read( &gpsData ) == -1 )
			{
				fprintf( stderr, "gps_read() error, errno = %d\n", errno );
			}
			else
			{
				if( isnan( gpsData.fix.latitude ) || isnan( gpsData.fix.longitude ) )
				{
					fprintf( stderr, "Bad GPS fix.\n" );
				}
				else
				{
					printf( "Latitude: %f\n", gpsData.fix.latitude );
					printf( "Longitude: %f\n", gpsData.fix.longitude );
				}
			}
		}
	}

	close( sockfd );
	return 0;
}
コード例 #21
0
ファイル: s52ais.c プロジェクト: pcannon67/S52
static gpointer      _gpsdClientRead(gpointer dummy)
// start gpsd client - loop forever
// return if _ais_list is NULL or 10 failed attempt to connect
{
    int nWait = 0;

    g_print("s52ais:_gpsdClientRead(): start looping ..\n");
    __builtin_bzero(&_gpsdata, sizeof(_gpsdata));

    //*
    while (0 != gps_open(GPSD_HOST, GPSD_PORT, &_gpsdata)) {   // android (gpsd 2.96)
        g_print("s52ais:_gpsdClientRead(): no gpsd running or network error, wait 1 sec: %d, %s\n", errno, gps_errstr(errno));

        // try to connect to GPSD server, bailout after 10 failed attempt
        g_static_mutex_lock(&_ais_list_mutex);
        if ((NULL==_ais_list) || (10 <= ++nWait)) {
            g_print("s52ais:_gpsdClientRead() no AIS list (main exited) or no GPSD server.. terminate _gpsClientRead thread\n");

            g_static_mutex_unlock(&_ais_list_mutex);

            return NULL;
        }
        g_static_mutex_unlock(&_ais_list_mutex);

        g_usleep(1000 * 1000); // 1.0 sec
    }

    if (-1 == gps_stream(&_gpsdata, WATCH_ENABLE|WATCH_NEWSTYLE, NULL)) {
        g_print("s52ais:_gpsdClientRead():gps_stream() failed .. exiting\n");
        return NULL;
    }
    //*/


    // debug
    //gps_enable_debug(3, stderr);
    //gps_enable_debug(8, stderr);

    // heart of the client
    for (;;) {
        g_static_mutex_lock(&_ais_list_mutex);
        if (NULL == _ais_list) {
            g_print("s52ais:_gpsdClientRead() no AIS list .. main exited .. terminate gpsRead thread\n");
            goto exit;
        }
        g_static_mutex_unlock(&_ais_list_mutex);

        if (FALSE == gps_waiting(&_gpsdata,  500*1000)) {    // wait 0.5 sec     (500*1000 uSec)
            //g_print("s52ais:_gpsdClientRead():gps_waiting() timed out\n");

            g_static_mutex_lock(&_ais_list_mutex);
            _updateTimeTag();
            g_static_mutex_unlock(&_ais_list_mutex);

        } else {
            errno = 0;

            int ret = gps_read(&_gpsdata);
            if (0 < ret) {
                // no error
                //g_print("s52ais:_gpsdClientRead():gps_read() ..\n");

                // handle AIS data
                g_static_mutex_lock(&_ais_list_mutex);
                _updateAISdata(&_gpsdata);
                g_static_mutex_unlock(&_ais_list_mutex);

                continue;
            }

            if (0 == ret) {
                g_print("s52ais:_gpsdClientRead():gps_read(): NO DATA .. [ret=0, errno=%i]\n", errno);
                continue;
            } else {
                g_print("s52ais:_gpsdClientRead():gps_read(): socket error 4 .. GPSD died [ret=%i, errno=%i]\n", ret, errno);

                goto exit;
            }
        }
    }

exit:
    // exit thread
    g_static_mutex_unlock(&_ais_list_mutex);

    gps_stream(&_gpsdata, WATCH_DISABLE, NULL);
    gps_close(&_gpsdata);

    return dummy;
}
コード例 #22
0
ファイル: lcdgps.c プロジェクト: elfchief/gpsd
int main(int argc, char *argv[])
{
    int option, rc;
    struct sockaddr_in localAddr, servAddr;
    struct hostent *h;

    int n;
    for(n=0;n<CLIMB;n++) climb[n]=0.0;

    switch (gpsd_units())
    {
    case imperial:
	altfactor = METERS_TO_FEET;
	altunits = "ft";
	speedfactor = MPS_TO_MPH;
	speedunits = "mph";
	break;
    case nautical:
	altfactor = METERS_TO_FEET;
	altunits = "ft";
	speedfactor = MPS_TO_KNOTS;
	speedunits = "knots";
	break;
    case metric:
	altfactor = 1;
	altunits = "m";
	speedfactor = MPS_TO_KPH;
	speedunits = "kph";
	break;
    default:
	/* leave the default alone */
	break;
    }

    /* Process the options.  Print help if requested. */
    while ((option = getopt(argc, argv, "Vhl:su:")) != -1) {
	switch (option) {
	case 'V':
	    (void)fprintf(stderr, "lcdgs revision " REVISION "\n");
	    exit(EXIT_SUCCESS);
	case 'h':
	default:
	    usage(argv[0]);
	    break;
	case 'l':
	    switch ( optarg[0] ) {
	    case 'd':
		deg_type = deg_dd;
		continue;
	    case 'm':
		deg_type = deg_ddmm;
		continue;
	    case 's':
		deg_type = deg_ddmmss;
		continue;
	    default:
		(void)fprintf(stderr, "Unknown -l argument: %s\n", optarg);
	    }
	    break;
	case 's':
	    sleep(10);
	    continue;
	case 'u':
	    switch ( optarg[0] ) {
	    case 'i':
		altfactor = METERS_TO_FEET;
		altunits = "ft";
		speedfactor = MPS_TO_MPH;
		speedunits = "mph";
		continue;
	    case 'n':
		altfactor = METERS_TO_FEET;
		altunits = "ft";
		speedfactor = MPS_TO_KNOTS;
		speedunits = "knots";
		continue;
	    case 'm':
		altfactor = 1;
		altunits = "m";
		speedfactor = MPS_TO_KPH;
		speedunits = "kph";
		continue;
	    default:
		(void)fprintf(stderr, "Unknown -u argument: %s\n", optarg);
	    }
	}
    }

    /* Grok the server, port, and device. */
  if (optind < argc) {
      gpsd_source_spec(argv[optind], &source);
  } else
      gpsd_source_spec(NULL, &source);

    /* Daemonize... */
  if (daemon(0, 0) != 0)
      (void)fprintf(stderr,
		    "lcdgps: demonization failed: %s\n",
		    strerror(errno));

    /* Open the stream to gpsd. */
    if (gps_open(source.server, source.port, &gpsdata) != 0) {
	(void)fprintf( stderr,
		       "lcdgps: no gpsd running or network error: %d, %s\n",
		       errno, gps_errstr(errno));
	exit(EXIT_FAILURE);
    }

    /* Connect to LCDd */
    h = gethostbyname(LCDDHOST);
    if (h==NULL) {
	printf("%s: unknown host '%s'\n",argv[0],LCDDHOST);
	exit(EXIT_FAILURE);
    }

    servAddr.sin_family = h->h_addrtype;
    memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
    servAddr.sin_port = htons(LCDDPORT);

    /* create socket */
    sd = socket(AF_INET, SOCK_STREAM, 0);
    if (BAD_SOCKET(sd)) {
	perror("cannot open socket ");
	exit(EXIT_FAILURE);
    }

    /* bind any port number */
    localAddr.sin_family = AF_INET;
    localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    localAddr.sin_port = htons(0);

    /* coverity[uninit_use_in_call] */
    rc = bind(sd, (struct sockaddr *) &localAddr, sizeof(localAddr));
    if (rc == -1) {
	printf("%s: cannot bind port TCP %u\n",argv[0],LCDDPORT);
	perror("error ");
	exit(EXIT_FAILURE);
    }

    /* connect to server */
    /* coverity[uninit_use_in_call] */
    rc = connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr));
    if (rc == -1) {
	perror("cannot connect ");
	exit(EXIT_FAILURE);
    }

    /* Do the initial field label setup. */
    reset_lcd();

    /* Here's where updates go. */
    unsigned int flags = WATCH_ENABLE;
    if (source.device != NULL)
	flags |= WATCH_DEVICE;
    (void)gps_stream(&gpsdata, flags, source.device);

    for (;;) { /* heart of the client */
	if (!gps_waiting(&gpsdata, 50000000)) {
	    fprintf( stderr, "lcdgps: error while waiting\n");
	    exit(EXIT_FAILURE);
	} else {
	    (void)gps_read(&gpsdata);
	    update_lcd(&gpsdata);
	}

    }
}
コード例 #23
0
ファイル: cgps.c プロジェクト: bloatbear/petulant-ninja
int main(int argc, char *argv[])
{
    int option;
    unsigned int flags = WATCH_ENABLE;

    /*@ -observertrans @*/
    switch (gpsd_units()) {
    case imperial:
	altfactor = METERS_TO_FEET;
	altunits = "ft";
	speedfactor = MPS_TO_MPH;
	speedunits = "mph";
	break;
    case nautical:
	altfactor = METERS_TO_FEET;
	altunits = "ft";
	speedfactor = MPS_TO_KNOTS;
	speedunits = "knots";
	break;
    case metric:
	altfactor = 1;
	altunits = "m";
	speedfactor = MPS_TO_KPH;
	speedunits = "kph";
	break;
    default:
	/* leave the default alone */
	break;
    }
    /*@ +observertrans @*/

    /* Process the options.  Print help if requested. */
    while ((option = getopt(argc, argv, "hVl:smu:D:")) != -1) {
	switch (option) {
#ifdef CLIENTDEBUG_ENABLE
	case 'D':
	    debug = atoi(optarg);
	    gps_enable_debug(debug, stderr);
	    break;
#endif /* CLIENTDEBUG_ENABLE */
	case 'm':
	    magnetic_flag = true;
	    break;
	case 's':
	    silent_flag = true;
	    break;
	case 'u':
	    /*@ -observertrans @*/
	    switch (optarg[0]) {
	    case 'i':
		altfactor = METERS_TO_FEET;
		altunits = "ft";
		speedfactor = MPS_TO_MPH;
		speedunits = "mph";
		continue;
	    case 'n':
		altfactor = METERS_TO_FEET;
		altunits = "ft";
		speedfactor = MPS_TO_KNOTS;
		speedunits = "knots";
		continue;
	    case 'm':
		altfactor = 1;
		altunits = "m";
		speedfactor = MPS_TO_KPH;
		speedunits = "kph";
		continue;
	    default:
		(void)fprintf(stderr, "Unknown -u argument: %s\n", optarg);
	    }
	    break;
	    /*@ +observertrans @*/
	case 'V':
	    (void)fprintf(stderr, "cgps: %s (revision %s)\n",
			  VERSION, REVISION);
	    exit(EXIT_SUCCESS);
	case 'l':
	    switch (optarg[0]) {
	    case 'd':
		deg_type = deg_dd;
		continue;
	    case 'm':
		deg_type = deg_ddmm;
		continue;
	    case 's':
		deg_type = deg_ddmmss;
		continue;
	    default:
		(void)fprintf(stderr, "Unknown -l argument: %s\n", optarg);
		/*@ -casebreak @*/
	    }
	    break;
	case 'h':
	default:
	    usage(argv[0]);
	    break;
	}
    }

    /* Grok the server, port, and device. */
    if (optind < argc) {
	gpsd_source_spec(argv[optind], &source);
    } else
	gpsd_source_spec(NULL, &source);

    /* Open the stream to gpsd. */
    if (gps_open(source.server, source.port, &gpsdata) != 0) {
	(void)fprintf(stderr,
		      "cgps: no gpsd running or network error: %d, %s\n",
		      errno, gps_errstr(errno));
	exit(EXIT_FAILURE);
    }

    /* note: we're assuming BSD-style reliable signals here */
    (void)signal(SIGINT, die);
    (void)signal(SIGHUP, die);
    (void)signal(SIGWINCH, resize);

    windowsetup();

    status_timer = time(NULL);

    if (source.device != NULL)
	flags |= WATCH_DEVICE;
    (void)gps_stream(&gpsdata, flags, source.device);

    /* heart of the client */
    for (;;) {
	int c;

	if (!gps_waiting(&gpsdata, 5000000)) {
	    die(GPS_TIMEOUT);
	} else {
	    errno = 0;
	    if (gps_read(&gpsdata) == -1) {
		fprintf(stderr, "cgps: socket error 4\n");
		die(errno == 0 ? GPS_GONE : GPS_ERROR);
	    } else {
		/* Here's where updates go now that things are established. */
#ifdef TRUENORTH
		if (compass_flag)
		    update_compass_panel(&gpsdata);
		else
#endif /* TRUENORTH */
		    update_gps_panel(&gpsdata);
	    }
	}

	/* Check for user input. */
	c = wgetch(datawin);

	switch (c) {
	    /* Quit */
	case 'q':
	    die(CGPS_QUIT);
	    break;

	    /* Toggle spewage of raw gpsd data. */
	case 's':
	    silent_flag = !silent_flag;
	    break;

	    /* Clear the spewage area. */
	case 'c':
	    (void)werase(messages);
	    break;

	default:
	    break;
	}
    }
}
コード例 #24
0
ファイル: seegps.c プロジェクト: bkovitz/pru
void one_gps_cycle(struct gps_data_t *gps_data) {
  int err;

  if ((err = gps_waiting(gps_data, ONE_SECOND)) == 0) {
    if (errno == 0) {
      puts("timeout");
      return;
    } else {
      fprintf(stderr, gps_errstr(err));
      exit(1);
    }
  }

  if ((err = gps_read(gps_data)) < 0) {
    perror("gps_read");
    exit(1);
  }

  print_timestamp(gps_data->online);

  switch (gps_data->status) {
    case STATUS_NO_FIX:
      puts("No fix");
      break;

    case STATUS_FIX:
      puts("Have fix without DGPS");
      print_gps_fix(&gps_data->fix);
      break;

    case STATUS_DGPS_FIX:
      puts("Have fix with DGPS");
      print_gps_fix(&gps_data->fix);
      break;

    default:
      printf("status = %d\n", gps_data->status);
  }

  if (gps_data->set & ERROR_SET) 
    printf("ERROR_SET  error: %s\n", gps_data->error);

  if (gps_data->set & LOGMESSAGE_SET)
    puts("LOGMESSAGE_SET");

  if (gps_data->set & POLICY_SET)
    puts("POLICY_SET");

  if (gps_data->set & VERSION_SET)
    printf("VERSION_SET  \"%s\" \"%s\" %d.%d \"%s\"\n",
      gps_data->version.release,
      gps_data->version.rev,
      gps_data->version.proto_major,
      gps_data->version.proto_minor,
      gps_data->version.remote);

  if (gps_data->set & GST_SET) {
    puts("GST_SET  (pseudorange errors)");
    printf("  utctime %g\n", gps_data->gst.utctime);
    printf("  rms_deviation %g\n", gps_data->gst.rms_deviation);
    printf("  smajor_deviation %g\n", gps_data->gst.smajor_deviation);
    printf("  sminor_deviation %g\n", gps_data->gst.sminor_deviation);
    printf("  smajor_orientation %g\n", gps_data->gst.smajor_orientation);
    printf("  lat_err_deviation %g\n", gps_data->gst.lat_err_deviation);
    printf("  lon_err_deviation %g\n", gps_data->gst.lon_err_deviation);
    printf("  alt_err_deviation %g\n", gps_data->gst.alt_err_deviation);
  }

  if (gps_data->set & SUBFRAME_SET)
    puts("SUBFRAME_SET");

  if (gps_data->set & PACKET_SET)
    puts("PACKET_SET");

  if (gps_data->set & AIS_SET)
    puts("AIS_SET");

  if (gps_data->set & RTCM3_SET)
    puts("RTCM3_SET");

  if (gps_data->set & RTCM2_SET)
    puts("RTCM2_SET");

  if (gps_data->set & DEVICEID_SET)
    puts("DEVICEID_SET");

  if (gps_data->set & DEVICELIST_SET) {
    printf("DEVICELIST_SET\n  ");
    print_timestamp(gps_data->devices.time);
    for (int i = 0; i < gps_data->devices.ndevices; i++)
      print_devconfig(&gps_data->devices.list[i]);
  }

  if (gps_data->set & DEVICE_SET)
    puts("DEVICE_SET");

  if (gps_data->set & CLIMBERR_SET)
    puts("CLIMBERR_SET");

  if (gps_data->set & TRACKERR_SET)
    puts("TRACKERR_SET");

  if (gps_data->set & SPEEDERR_SET)
    puts("SPEEDERR_SET");

  if (gps_data->set & SATELLITE_SET)
    puts("SATELLITE_SET");

  if (gps_data->set & ATTITUDE_SET) {
    puts("ATTITUDE_SET");
    print_attitude(&gps_data->attitude);
  }

  if (gps_data->set & VERR_SET)
    puts("VERR_SET");

  if (gps_data->set & HERR_SET)
    puts("HERR_SET");

  if (gps_data->set & DOP_SET)
    puts("DOP_SET");

  if (gps_data->set & MODE_SET)
    puts("MODE_SET");

  if (gps_data->set & STATUS_SET)
    puts("STATUS_SET");

  if (gps_data->set & CLIMB_SET)
    puts("CLIMB_SET");

  if (gps_data->set & TRACK_SET)
    puts("TRACK_SET");

  if (gps_data->set & SPEED_SET)
    puts("SPEED_SET");

  if (gps_data->set & ALTITUDE_SET)
    puts("ALTITUDE_SET");

  if (gps_data->set & LATLON_SET)
    puts("LATLON_SET");

  if (gps_data->set & TIMERR_SET)
    puts("TIMERR_SET");

  if (gps_data->set & TIME_SET)
    puts("TIME_SET");

  if (gps_data->set & ONLINE_SET) {
    printf("ONLINE_SET  ");
    print_timestamp(gps_data->online);
  }

  //printf("0x%08x\n", (unsigned)gps_data->set);
  putchar('\n');
}
コード例 #25
0
ファイル: gps.c プロジェクト: NicolasJourden/collectd
/**
 * Thread reading from gpsd.
 */
static void * cgps_thread (void * pData)
{
  struct gps_data_t gpsd_conn;
  unsigned int err_count;
  cgps_thread_running = CGPS_TRUE;

  while (CGPS_TRUE)
  {
    pthread_mutex_lock (&cgps_thread_lock);
    if (cgps_thread_shutdown == CGPS_TRUE)
    {
      goto quit;
    }
    pthread_mutex_unlock (&cgps_thread_lock);

    err_count = 0;

#if GPSD_API_MAJOR_VERSION > 4
    int status = gps_open (cgps_config_data.host, cgps_config_data.port, &gpsd_conn);
#else
    int status = gps_open_r (cgps_config_data.host, cgps_config_data.port, &gpsd_conn);
#endif
    if (status < 0)
    {
      WARNING ("gps plugin: connecting to %s:%s failed: %s",
               cgps_config_data.host, cgps_config_data.port, gps_errstr (status));

      // Here we make a pause until a new tentative to connect, we check also if
      // the thread does not need to stop.
      if (cgps_thread_pause(cgps_config_data.pause_connect) == CGPS_FALSE)
      {
        goto quit;
      }

      continue;
    }

    gps_stream (&gpsd_conn, WATCH_ENABLE | WATCH_JSON | WATCH_NEWSTYLE, NULL);
    gps_send (&gpsd_conn, CGPS_CONFIG);

    while (CGPS_TRUE)
    {
      pthread_mutex_lock (&cgps_thread_lock);
      if (cgps_thread_shutdown == CGPS_TRUE)
      {
        goto stop;
      }
      pthread_mutex_unlock (&cgps_thread_lock);

#if GPSD_API_MAJOR_VERSION > 4
      long timeout_us = CDTIME_T_TO_US (cgps_config_data.timeout);
      if (!gps_waiting (&gpsd_conn, (int) timeout_us ))
#else
      if (!gps_waiting (&gpsd_conn))
#endif
      {
        continue;
      }

      if (gps_read (&gpsd_conn) == -1)
      {
        WARNING ("gps plugin: incorrect data! (err_count: %d)", err_count);
        err_count++;

        if (err_count > CGPS_MAX_ERROR)
        {
          // Server is not responding ...
          if (gps_send (&gpsd_conn, CGPS_CONFIG) == -1)
          {
            WARNING ("gps plugin: gpsd seems to be down, reconnecting");
            gps_close (&gpsd_conn);
            break;
          }
          // Server is responding ...
          else
          {
            err_count = 0;
          }
        }

        continue;
      }

      pthread_mutex_lock (&cgps_data_lock);

      // Number of sats in view:
      cgps_data.sats_used = (gauge_t) gpsd_conn.satellites_used;
      cgps_data.sats_visible = (gauge_t) gpsd_conn.satellites_visible;

      // dilution of precision:
      cgps_data.vdop = NAN;
      cgps_data.hdop = NAN;
      if (cgps_data.sats_used > 0)
      {
        cgps_data.hdop = gpsd_conn.dop.hdop;
        cgps_data.vdop = gpsd_conn.dop.vdop;
      }

      DEBUG ("gps plugin: %.0f sats used (of %.0f visible), hdop = %.3f, vdop = %.3f",
             cgps_data.sats_used, cgps_data.sats_visible, cgps_data.hdop, cgps_data.vdop);

      pthread_mutex_unlock (&cgps_data_lock);
    }
  }

stop:
  DEBUG ("gps plugin: thread closing gpsd connection ... ");
  gps_stream (&gpsd_conn, WATCH_DISABLE, NULL);
  gps_close (&gpsd_conn);
quit:
  DEBUG ("gps plugin: thread shutting down ... ");
  cgps_thread_running = CGPS_FALSE;
  pthread_mutex_unlock (&cgps_thread_lock);
  pthread_exit (NULL);
}