direwolf/dwgps.c

328 lines
7.2 KiB
C
Raw Normal View History

Version 1.0 - Initial commit Changes to be committed: new file: .gitattributes new file: .gitignore new file: APRStt-Implementation-Notes.pdf new file: CHANGES.txt new file: LICENSE-dire-wolf.txt new file: LICENSE-other.txt new file: Makefile.linux new file: Makefile.win new file: Quick-Start-Guide-Windows.pdf new file: Raspberry-Pi-APRS.pdf new file: User-Guide.pdf new file: aclients.c new file: aprs_tt.c new file: aprs_tt.h new file: atest.c new file: audio.c new file: audio.h new file: audio_win.c new file: ax25_pad.c new file: ax25_pad.h new file: beacon.c new file: beacon.h new file: config.c new file: config.h new file: decode_aprs.c new file: decode_aprs.h new file: dedupe.c new file: dedupe.h new file: demod.c new file: demod.h new file: demod_9600.c new file: demod_9600.h new file: demod_afsk.c new file: demod_afsk.h new file: digipeater.c new file: digipeater.h new file: direwolf.c new file: direwolf.conf new file: direwolf.desktop new file: direwolf.h new file: dsp.c new file: dsp.h new file: dtmf.c new file: dtmf.h new file: dw-icon.ico new file: dw-icon.png new file: dw-icon.rc new file: dw-start.sh new file: dwgps.c new file: dwgps.h new file: encode_aprs.c new file: encode_aprs.h new file: fcs_calc.c new file: fcs_calc.h new file: fsk_demod_agc.h new file: fsk_demod_state.h new file: fsk_filters.h new file: fsk_gen_filter.h new file: gen_packets.c new file: gen_tone.c new file: gen_tone.h new file: hdlc_rec.c new file: hdlc_rec.h new file: hdlc_rec2.c new file: hdlc_rec2.h new file: hdlc_send.c new file: hdlc_send.h new file: igate.c new file: igate.h new file: kiss.c new file: kiss.h new file: kiss_frame.c new file: kiss_frame.h new file: kissnet.c new file: kissnet.h new file: latlong.c new file: latlong.h new file: ll2utm.c new file: misc/README-dire-wolf.txt new file: misc/strcasestr.c new file: misc/strsep.c new file: misc/strtok_r.c new file: morse.c new file: multi_modem.c new file: multi_modem.h new file: ptt.c new file: ptt.h new file: pttest.c new file: rdq.c new file: rdq.h new file: redecode.c new file: redecode.h new file: regex/COPYING new file: regex/INSTALL new file: regex/LICENSES new file: regex/NEWS new file: regex/README new file: regex/README-dire-wolf.txt new file: regex/re_comp.h new file: regex/regcomp.c new file: regex/regex.c new file: regex/regex.h new file: regex/regex_internal.c new file: regex/regex_internal.h new file: regex/regexec.c new file: rrbb.c new file: rrbb.h new file: server.c new file: server.h new file: symbols-new.txt new file: symbols.c new file: symbols.h new file: symbolsX.txt new file: textcolor.c new file: textcolor.h new file: tocalls.txt new file: tq.c new file: tq.h new file: tt_text.c new file: tt_text.h new file: tt_user.c new file: tt_user.h new file: tune.h new file: udp_test.c new file: utm/LatLong-UTMconversion.c new file: utm/LatLong-UTMconversion.h new file: utm/README.txt new file: utm/SwissGrid.cpp new file: utm/UTMConversions.cpp new file: utm/constants.h new file: utm2ll.c new file: version.h new file: xmit.c new file: xmit.h
2015-07-27 00:35:07 +00:00
//
// This file is part of Dire Wolf, an amateur radio packet TNC.
//
// Copyright (C) 2013 John Langner, WB2OSZ
//
// 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
*
* Purpose: Interface to location data, i.e. GPS receiver.
*
* Description: Tracker beacons need to know the current location.
* At this time, I can't think of any other reason why
* we would need this information.
*
* For Linux, we use gpsd and libgps.
* This has the extra benefit that the system clock can
* be set from the GPS signal.
*
* Not yet implemented for Windows. Not sure how yet.
* The Windows location API is new in Windows 7.
* At the end of 2013, about 1/3 of Windows users are
* still using XP so that still needs to be supported.
*
* Reference:
*
*---------------------------------------------------------------*/
#if TEST
#define ENABLE_GPS 1
#endif
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
#if __WIN32__
#include <windows.h>
#else
#if ENABLE_GPS
#include <gps.h>
#if GPSD_API_MAJOR_VERSION != 5
#error libgps API version might be incompatible.
#endif
#endif
#endif
#include "direwolf.h"
#include "textcolor.h"
#include "dwgps.h"
/* Was init successful? */
static enum { INIT_NOT_YET, INIT_SUCCESS, INIT_FAILED } init_status = INIT_NOT_YET;
#if __WIN32__
#include <windows.h>
#else
#if ENABLE_GPS
static struct gps_data_t gpsdata;
#endif
#endif
/*-------------------------------------------------------------------
*
* Name: dwgps_init
*
* Purpose: Intialize the GPS interface.
*
* Inputs: none.
*
* Returns: 0 = success
* -1 = failure
*
* Description: For Linux, this maps into gps_open.
* Not yet implemented for Windows.
*
*--------------------------------------------------------------------*/
int dwgps_init (void)
{
#if __WIN32__
text_color_set(DW_COLOR_ERROR);
dw_printf ("GPS interface not yet available in Windows version.\n");
init_status = INIT_FAILED;
return (-1);
#elif ENABLE_GPS
int err;
err = gps_open (GPSD_SHARED_MEMORY, NULL, &gpsdata);
if (err != 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Unable to connect to GPS receiver.\n");
if (err == NL_NOHOST) {
dw_printf ("Shared memory interface is not enabled in libgps.\n");
dw_printf ("Download the gpsd source and build with 'shm_export=True' option.\n");
}
else {
dw_printf ("%s\n", gps_errstr(errno));
}
init_status = INIT_FAILED;
return (-1);
}
init_status = INIT_SUCCESS;
return (0);
#else
text_color_set(DW_COLOR_ERROR);
dw_printf ("GPS interface not enabled in this version.\n");
dw_printf ("See documentation on how to rebuild with ENABLE_GPS.\n");
init_status = INIT_FAILED;
return (-1);
#endif
} /* end dwgps_init */
/*-------------------------------------------------------------------
*
* Name: dwgps_read
*
* Purpose: Obtain current location from GPS receiver.
*
* Outputs: *plat - Latitude.
* *plon - Longitude.
* *pspeed - Speed, knots.
* *pcourse - Course over ground, degrees.
* *palt - Altitude, meters.
*
* Returns: -1 = error
* 0 = data not available (no fix)
* 2 = 2D fix, lat/lon, speed, and course are set.
* 3 - 3D fix, altitude is also set.
*
*--------------------------------------------------------------------*/
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);
}
err = gps_read (&gpsdata);
#if DEBUG
dw_printf ("gps_read returns %d bytes\n", err);
#endif
if (err > 0) {
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 */
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 */
/*-------------------------------------------------------------------
*
* Name: dwgps_term
*
* Purpose: Shut down GPS interface before exiting from application.
*
* Inputs: none.
*
* Returns: none.
*
*--------------------------------------------------------------------*/
void dwgps_term (void) {
#if __WIN32__
#elif ENABLE_GPS
if (init_status == INIT_SUCCESS) {
gps_close (&gpsdata);
}
#else
#endif
} /* end dwgps_term */
/*-------------------------------------------------------------------
*
* Name: main
*
* Purpose: Simple unit test for other functions in this file.
*
* Description: Compile with -DTEST option.
*
* gcc -DTEST dwgps.c textcolor.c -lgps
*
*--------------------------------------------------------------------*/
#if TEST
int main (int argc, char *argv[])
{
#if __WIN32__
printf ("Not in win32 version yet.\n");
#elif ENABLE_GPS
int err;
int fix;
double lat;
double lon;
float speed;
float course;
float alt;
err = dwgps_init ();
if (err != 0) exit(1);
while (1) {
fix = dwgps_read (&lat, &lon, &speed, &course, &alt) ;
switch (fix) {
case 3:
case 2:
dw_printf ("%.6f %.6f", lat, lon);
dw_printf (" %.1f knots %.0f degrees", speed, course);
if (fix==3) dw_printf (" altitude = %.1f meters", alt);
dw_printf ("\n");
break;
case 0:
dw_printf ("location currently not available.\n");
break;
default:
dw_printf ("ERROR getting GPS information.\n");
}
sleep (3);
}
#else
printf ("Test: Shouldn't be here.\n");
#endif
} /* end main */
#endif
/* end dwgps.c */