2015-07-27 01:05:48 +00:00
|
|
|
//
|
|
|
|
// This file is part of Dire Wolf, an amateur radio packet TNC.
|
|
|
|
//
|
2015-11-08 01:57:02 +00:00
|
|
|
// Copyright (C) 2015 John Langner, WB2OSZ
|
2015-07-27 01:05:48 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 2 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* Module: dwgps.c
|
|
|
|
*
|
2015-11-08 01:57:02 +00:00
|
|
|
* Purpose: Interface for obtaining location from GPS.
|
2015-07-27 01:05:48 +00:00
|
|
|
*
|
2015-11-08 01:57:02 +00:00
|
|
|
* Description: This is a wrapper for two different implementations:
|
2015-07-27 01:05:48 +00:00
|
|
|
*
|
2015-11-08 01:57:02 +00:00
|
|
|
* (1) Read NMEA sentences from a serial port (or USB
|
|
|
|
* that looks line one). Available for all platforms.
|
2015-07-27 01:05:48 +00:00
|
|
|
*
|
2015-11-08 01:57:02 +00:00
|
|
|
* (2) Read from gpsd. Not available for Windows.
|
|
|
|
* Including this is optional because it depends
|
|
|
|
* on another external software component.
|
2015-07-27 01:05:48 +00:00
|
|
|
*
|
2015-11-08 01:57:02 +00:00
|
|
|
*
|
|
|
|
* API: dwgps_init Connect to data stream at start up time.
|
|
|
|
*
|
|
|
|
* dwgps_read Return most recent location to application.
|
|
|
|
*
|
|
|
|
* dwgps_print Print contents of structure for debugging.
|
|
|
|
*
|
|
|
|
* dwgps_term Shutdown on exit.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* from below: dwgps_set_data Called from other two implementations to
|
|
|
|
* save data until it is needed.
|
2015-07-27 01:05:48 +00:00
|
|
|
*
|
|
|
|
*---------------------------------------------------------------*/
|
|
|
|
|
2016-07-03 22:09:34 +00:00
|
|
|
#include "direwolf.h"
|
|
|
|
|
2015-07-27 01:05:48 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2015-11-08 01:57:02 +00:00
|
|
|
#include <time.h>
|
2015-07-27 01:05:48 +00:00
|
|
|
|
|
|
|
#include "textcolor.h"
|
|
|
|
#include "dwgps.h"
|
2015-11-08 01:57:02 +00:00
|
|
|
#include "dwgpsnmea.h"
|
|
|
|
#include "dwgpsd.h"
|
2015-07-27 01:05:48 +00:00
|
|
|
|
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
static int s_dwgps_debug = 0; /* Enable debug output. */
|
|
|
|
/* >= 2 show updates from GPS. */
|
|
|
|
/* >= 1 show results from dwgps_read. */
|
2015-07-27 01:05:48 +00:00
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
/*
|
|
|
|
* The GPS reader threads deposit current data here when it becomes available.
|
|
|
|
* dwgps_read returns it to the requesting application.
|
|
|
|
*
|
|
|
|
* A critical region to avoid inconsistency between fields.
|
|
|
|
*/
|
2015-07-27 01:05:48 +00:00
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
static dwgps_info_t s_dwgps_info = {
|
|
|
|
.timestamp = 0,
|
|
|
|
.fix = DWFIX_NOT_INIT, /* to detect read without init. */
|
|
|
|
.dlat = G_UNKNOWN,
|
|
|
|
.dlon = G_UNKNOWN,
|
|
|
|
.speed_knots = G_UNKNOWN,
|
|
|
|
.track = G_UNKNOWN,
|
|
|
|
.altitude = G_UNKNOWN
|
|
|
|
};
|
2015-07-27 01:05:48 +00:00
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
static dw_mutex_t s_gps_mutex;
|
2015-07-27 01:05:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* Name: dwgps_init
|
|
|
|
*
|
2021-09-19 18:51:18 +00:00
|
|
|
* Purpose: Initialize the GPS interface.
|
2015-07-27 01:05:48 +00:00
|
|
|
*
|
2015-11-08 01:57:02 +00:00
|
|
|
* Inputs: pconfig Configuration settings. This might include
|
|
|
|
* serial port name for direct connect and host
|
|
|
|
* name or address for network connection.
|
|
|
|
*
|
|
|
|
* debug - If >= 1, print results when dwgps_read is called.
|
|
|
|
* (In this file.)
|
2015-07-27 01:05:48 +00:00
|
|
|
*
|
2015-11-08 01:57:02 +00:00
|
|
|
* If >= 2, location updates are also printed.
|
|
|
|
* (In other two related files.)
|
|
|
|
*
|
|
|
|
* Returns: none
|
|
|
|
*
|
|
|
|
* Description: Call corresponding functions for implementations.
|
|
|
|
* Normally we would expect someone to use either GPSNMEA or
|
|
|
|
* GPSD but there is nothing to prevent use of both at the
|
|
|
|
* same time.
|
2015-07-27 01:05:48 +00:00
|
|
|
*
|
|
|
|
*--------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
void dwgps_init (struct misc_config_s *pconfig, int debug)
|
|
|
|
{
|
2015-07-27 01:05:48 +00:00
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
s_dwgps_debug = debug;
|
2015-07-27 01:05:48 +00:00
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
dw_mutex_init (&s_gps_mutex);
|
2015-07-27 01:05:48 +00:00
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
dwgpsnmea_init (pconfig, debug);
|
2015-07-27 01:05:48 +00:00
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
#if ENABLE_GPSD
|
2015-07-27 01:05:48 +00:00
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
dwgpsd_init (pconfig, debug);
|
2015-07-27 01:05:48 +00:00
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
#endif
|
2015-07-27 01:05:48 +00:00
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
SLEEP_MS(500); /* So receive thread(s) can clear the */
|
|
|
|
/* not init status before it gets checked. */
|
2015-07-27 01:05:48 +00:00
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
} /* end dwgps_init */
|
2015-07-27 01:05:48 +00:00
|
|
|
|
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
/*-------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* Name: dwgps_clear
|
|
|
|
*
|
|
|
|
* Purpose: Clear the gps info structure.
|
|
|
|
*
|
|
|
|
*--------------------------------------------------------------------*/
|
2015-07-27 01:05:48 +00:00
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
void dwgps_clear (dwgps_info_t *gpsinfo)
|
|
|
|
{
|
|
|
|
gpsinfo->timestamp = 0;
|
|
|
|
gpsinfo->fix = DWFIX_NOT_SEEN;
|
|
|
|
gpsinfo->dlat = G_UNKNOWN;
|
|
|
|
gpsinfo->dlon = G_UNKNOWN;
|
|
|
|
gpsinfo->speed_knots = G_UNKNOWN;
|
|
|
|
gpsinfo->track = G_UNKNOWN;
|
|
|
|
gpsinfo->altitude = G_UNKNOWN;
|
|
|
|
}
|
2015-07-27 01:05:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* Name: dwgps_read
|
|
|
|
*
|
2015-11-08 01:57:02 +00:00
|
|
|
* Purpose: Return most recent location data available.
|
2015-07-27 01:05:48 +00:00
|
|
|
*
|
2015-11-08 01:57:02 +00:00
|
|
|
* Outputs: gpsinfo - Structure with latitude, longitude, etc.
|
|
|
|
*
|
|
|
|
* Returns: Position fix quality. Same as in structure.
|
2015-07-27 01:05:48 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
*--------------------------------------------------------------------*/
|
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
dwfix_t dwgps_read (dwgps_info_t *gpsinfo)
|
2015-07-27 01:05:48 +00:00
|
|
|
{
|
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
dw_mutex_lock (&s_gps_mutex);
|
2015-07-27 01:05:48 +00:00
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
memcpy (gpsinfo, &s_dwgps_info, sizeof(*gpsinfo));
|
2015-07-27 01:05:48 +00:00
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
dw_mutex_unlock (&s_gps_mutex);
|
2015-07-27 01:05:48 +00:00
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
if (s_dwgps_debug >= 1) {
|
|
|
|
text_color_set (DW_COLOR_DEBUG);
|
|
|
|
dwgps_print ("gps_read: ", gpsinfo);
|
2015-07-27 01:05:48 +00:00
|
|
|
}
|
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
// TODO: Should we check timestamp and complain if very stale?
|
|
|
|
// or should we leave that up to the caller?
|
2015-07-27 01:05:48 +00:00
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
return (s_dwgps_info.fix);
|
|
|
|
}
|
2015-07-27 01:05:48 +00:00
|
|
|
|
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
/*-------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* Name: dwgps_print
|
|
|
|
*
|
|
|
|
* Purpose: Print gps information for debugging.
|
|
|
|
*
|
|
|
|
* Inputs: msg - Message for prefix on line.
|
|
|
|
* gpsinfo - Structure with latitude, longitude, etc.
|
|
|
|
*
|
|
|
|
* Description: Caller is responsible for setting text color.
|
|
|
|
*
|
|
|
|
*--------------------------------------------------------------------*/
|
2015-07-27 01:05:48 +00:00
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
void dwgps_print (char *msg, dwgps_info_t *gpsinfo)
|
|
|
|
{
|
2015-07-27 01:05:48 +00:00
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
dw_printf ("%stime=%d fix=%d lat=%.6f lon=%.6f trk=%.0f spd=%.1f alt=%.0f\n",
|
|
|
|
msg,
|
|
|
|
(int)gpsinfo->timestamp, (int)gpsinfo->fix,
|
|
|
|
gpsinfo->dlat, gpsinfo->dlon,
|
|
|
|
gpsinfo->track, gpsinfo->speed_knots,
|
|
|
|
gpsinfo->altitude);
|
2015-07-27 01:05:48 +00:00
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
} /* end dwgps_set_data */
|
2015-07-27 01:05:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* Name: dwgps_term
|
|
|
|
*
|
|
|
|
* Purpose: Shut down GPS interface before exiting from application.
|
|
|
|
*
|
|
|
|
* Inputs: none.
|
|
|
|
*
|
|
|
|
* Returns: none.
|
|
|
|
*
|
|
|
|
*--------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
void dwgps_term (void) {
|
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
dwgpsnmea_term ();
|
2015-07-27 01:05:48 +00:00
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
#if ENABLE_GPSD
|
|
|
|
dwgpsd_term ();
|
2015-07-27 01:05:48 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
} /* end dwgps_term */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------
|
|
|
|
*
|
2015-11-08 01:57:02 +00:00
|
|
|
* Name: dwgps_set_data
|
2015-07-27 01:05:48 +00:00
|
|
|
*
|
2015-11-08 01:57:02 +00:00
|
|
|
* Purpose: Called by the GPS interfaces when new data is available.
|
2015-07-27 01:05:48 +00:00
|
|
|
*
|
2015-11-08 01:57:02 +00:00
|
|
|
* Inputs: gpsinfo - Structure with latitude, longitude, etc.
|
2015-07-27 01:05:48 +00:00
|
|
|
*
|
|
|
|
*--------------------------------------------------------------------*/
|
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
void dwgps_set_data (dwgps_info_t *gpsinfo)
|
2015-07-27 01:05:48 +00:00
|
|
|
{
|
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
/* Debug print is handled by the two callers so */
|
|
|
|
/* we can distinguish the source. */
|
2015-07-27 01:05:48 +00:00
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
dw_mutex_lock (&s_gps_mutex);
|
2015-07-27 01:05:48 +00:00
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
memcpy (&s_dwgps_info, gpsinfo, sizeof(s_dwgps_info));
|
2015-07-27 01:05:48 +00:00
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
dw_mutex_unlock (&s_gps_mutex);
|
2015-07-27 01:05:48 +00:00
|
|
|
|
2015-11-08 01:57:02 +00:00
|
|
|
} /* end dwgps_set_data */
|
2015-07-27 01:05:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* end dwgps.c */
|
|
|
|
|
|
|
|
|
|
|
|
|