Files
Cnomicon/daemons/gpsd/gpsd.cpp
2021-01-22 10:16:20 -05:00

227 lines
4.7 KiB
C++

//
// GPS Daemon
// Neal Probert
//
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <arpa/inet.h>
#include "GpsDaemon.h"
#include "netlib.h"
extern int optind, opterr, optopt;
//
// Written because gpsd (http://gpsd.berlios.de/) is too fat
//
//
// GPS Server (port 2947 by default)
// ----------
//
static const char *helptext =
"This is GPSd, not the open source software gpsd:\n"
"gpsd -s /dev/ttyS0:57600 [options]\n"
"\t-d debug (foreground with stdio)\n"
"\t-h help\n"
"\t-l UDP listen for RTCM corrections\n"
"\t-m network broadcast GPS data\n"
"\t-o outfile output message log\n"
"\t-r uri dgpsip://host or ntrip://user@pw:host/ rtcm:/dev/ttyS1\n"
"\t-s port nmea:/dev/ttyS0:baud or sirf:/dev/ttyS0:baud\n"
"\t-t uri OpenGTS server\n"
"\t-w WDM DSRC listen for RTCM corrections\n"
"\t-S service TCP service (default=gpsd)\n"
"\tUse OSS gpsd, gpspipe and gpsfake to create and replay NMEA log files!\n";
int main(int argc, char **argv)
{
char c; /* we have character */
GpsDaemon Gps;
/* options */
int debug = 0; // run in foreground, leaves stdio open
int dsrc_wsm = 0;
int timeout = 15;
/* dgpsip server */
char *host = NULL;
/* service/port */
char *service = (char *)"gpsd";
/* serial port */
char *primary = NULL;
char *secondary = NULL;
int baud = 4800;
/* logging */
char *outfile = NULL;
/* udp broadcast */
char *mcast = NULL;
/* udp listener */
int listen = 0;
/* data */
char szBuf[1600];
bzero( szBuf, sizeof(szBuf) );
// add command line args
while ( (c = getopt( argc, argv, "cdhi:jlm:o:r:s:t:S:T:v" )) > 0 )
{
switch ( c )
{
case 'c': // clock set
Gps.SetClock();
break;
case 'd':
debug = 1;
break;
case 'h':
puts( helptext );
return 0;
case 'l': // udp/wdm listen
listen = 1;
break;
case 'm': // udp broadcast
mcast = optarg;
break;
case 'o':
outfile = optarg;
break;
case 'r': // rtcm-sc104 host
host = optarg;
break;
case 's': // serial device
if ( !primary )
primary = optarg;
else
secondary = optarg;
break;
case 'S':
service = optarg;
break;
case 'T':
timeout = atoi(optarg);
if ( timeout == 0 )
timeout = 30;
break;
case 'w': // DSRC WSM
fprintf( stderr, "DSRC WSM support not available!\n" );
listen = 1;
dsrc_wsm = 1;
break;
default:
fprintf( stderr, "Unknown option -%c\n", c );
exit(1);
}
}
// check options
if ( !primary )
{
fprintf( stderr, "Must have a GPS device to communicate with!\n" );
exit(1);
}
if ( mcast && listen )
{
fprintf( stderr, "Cannot do both UDP broadcast/multicast and listening!\n" );
exit(1);
}
// input
if ( primary )
{
// open GPS device on serial port
int serial = Gps.GpsOpen( primary, baud );
if ( serial < 0 )
{
fprintf( stderr, "Unable to open primary serial device: %s @ %d !\n", primary, baud );
if ( secondary )
{
serial = Gps.GpsOpen( secondary, baud ); // won't open till primary dies
if ( serial < 0 )
{
fprintf( stderr, "Unable to open secondary serial device: %s @ %d !\n", secondary, baud );
exit(1);
}
}
}
}
else
{
fprintf( stderr, "No task specified: use -l, -i <logfile> or -s <dev>!\n" );
exit(1);
}
// corrections
if ( host )
{
// RTCM server
int remote = Gps.DgpsConnect( host );
if ( remote < 0 )
{
fprintf( stderr, "Unable to connect to rtcm-sc104 source host: %s\n", host );
// exit(1);
}
Gps.SetTimeout( timeout * 1000000L );
}
else if ( listen )
{
if ( dsrc_wsm )
{
// listen on WSM control channel for DSRC MSG_RTCM_Corrections
}
else
{
}
}
// output
if ( mcast )
{
int bcast = Gps.GpsBroadcast( mcast );
if ( bcast < 0 )
{
fprintf( stderr, "Unable to create UDP broadcast!\n" );
exit(1);
}
}
// logging
if ( outfile )
Gps.LogOpen( outfile );
// TCP listener
if ( Gps.StartService( GPSD_NAME, NULL, service, debug ) >= 0 )
{
return Gps.RunService();
}
return 1;
}