Initial commit of files

This commit is contained in:
2021-01-22 10:16:20 -05:00
parent 32d165ec8f
commit ed92211680
534 changed files with 68563 additions and 19 deletions

View File

@ -0,0 +1,26 @@
cmake_minimum_required(VERSION 3.7)
set(NAME gpsd)
project(Cnomicon-Daemon)
set(TARGET ${NAME})
set(CMAKE_INSTALL_PREFIX ..)
# includes
include_directories(
/usr/include/libxml2
../include/libgps++
../include/libnet++
../include)
link_directories(../lib)
link_libraries(gps++ net++ net emb kmlbase kmldom kmlengine xml2 pthread rt)
# sources
set(SRCS
GpsDaemon.cpp
)
# executables
add_executable(${TARGET} ${TARGET}.cpp ${SRCS})
install(TARGETS ${TARGET} DESTINATION sbin)

384
daemons/gpsd/GpsDaemon.cpp Normal file
View File

@ -0,0 +1,384 @@
//
// GPS Daemon
// Neal Probert
//
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>
#include <sys/timeb.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "UriParse.h"
#include "GpsDaemon.h"
#include "NmeaParser.h"
#include "ApplanixParser.h"
#include "FastraxParser.h"
#include "GarminParser.h"
#include "SirfParser.h"
#include "UbloxParser.h"
#include "netlib.h"
//****************************************************************************
// defines
//***************************************************************************/
//****************************************************************************
// macros
//***************************************************************************/
//****************************************************************************
// structs & typedefs
//***************************************************************************/
//****************************************************************************
// global constants
//***************************************************************************/
//****************************************************************************
// global variables
//***************************************************************************/
//****************************************************************************
// static constants
//***************************************************************************/
//****************************************************************************
// static variables
//***************************************************************************/
//****************************************************************************
// static functions
//***************************************************************************/
//****************************************************************************
// C++ functions
//***************************************************************************/
GpsDaemon::GpsDaemon()
{
log_open( GPSD_NAME );
clockset = 0;
msgcount = 0;
dgps_corr = true;
SourceIn = CorrIn = -1;
strcpy( Uri, "" );
nClients = 0;
SetClientTimeout(false); // don't reset timeout on client activity
}
GpsDaemon::~GpsDaemon()
{
Gps.GpsClose();
Log.LogClose();
}
/* NMEA **********************************************************************/
void GpsDaemon::PositionListener( const GpsPoint &Pos, const ErrorInfo &Err )
{
}
void GpsDaemon::TimeListener( const struct timeval &tv )
{
// good fix and time
if ( clockset && tv.tv_sec )
{
if ( getuid() == 0 )
settimeofday( &tv, NULL );
clockset = 0;
}
}
/* TCP ***********************************************************************/
int GpsDaemon::ServerTimeout(void)
{
// timeout (a bad thing)
DgpsReconnect();
return 0;
}
int GpsDaemon::ServerSocket(void)
{
char buf[2048];
int n = 0;
if ( IsSet( CorrIn ) )
{
// corrections
if ( dgps_corr )
// from RTCM server (once a minute)
n = Dgps.DgpsRead( buf, sizeof(buf) );
else
// from NTRIP server (once a minute)
n = Ntrip.NtripRead( (unsigned char *)buf, sizeof(buf) );
// xmit
if ( n > 0 )
Gps.GpsWrite( buf, n );
else if ( n == 0 )
{
if ( dgps_corr )
log_error( "Remote RTCM server closed!" );
else
log_error( "Remote NTRIP server closed!" );
DgpsReconnect();
}
ResetServerTimeout();
}
else if ( IsSet( SourceIn ) )
{
if ( gps_source )
// packet from GPS device
n = Gps.GpsRead( buf, sizeof(buf) );
else
// remote gpsd/glty
n = Gpsd.Recv( buf, sizeof(buf) );
if ( n > 0 && Parser && Parser->GpsParse(buf, n) > 0 )
{
msgcount++;
// 'cast
ClientCast( buf, n );
}
}
return n;
}
int GpsDaemon::ClientIn( int sock, const char *buf, int n )
{
// allows for NMEA playback via netcat
if ( *buf == '$' )
{
// broadcast it
Bcast.SendTo( buf, n );
// send to clients
TcpServer::ClientCast( buf, n );
}
else
{
// parse for commands?
// just log it
log_write( buf );
}
return n;
}
int GpsDaemon::ClientUp( int sock )
{
if ( nClients++ == 0 )
;
return 1;
}
void GpsDaemon::ClientDown( int sock )
{
// disconnect source
if ( --nClients <= 0 )
nClients = 0;
}
void GpsDaemon::ClientCast( const char *buf, int n )
{
// broadcast it
Bcast.SendTo( buf, n );
// send to clients
TcpServer::ClientCast( buf, n );
// log NMEA data with time stamp
Log.LogWrite( buf, n );
}
/* GPS ***********************************************************************/
int GpsDaemon::GpsOpen( const char *device, int baud )
{
UriParse parsed;
parsed.setRate( baud );
parsed.setUri( device );
const char *proto = parsed.getProto();
SourceIn = -1;
if ( !proto )
proto = "nmea";
// connect to gpsd?
if ( strcmp( proto, "gpsd" ) == 0 )
{
// does r mode for NMEA
SourceIn = Gpsd.GpsdConnect( parsed.getHost() );
proto = "nmea";
}
else if ( strcmp( proto, "glty" ) == 0 )
{
// put in NMEA mode
SourceIn = Gpsd.GpsdConnect( parsed.getHost() );
Gpsd.Send( "$PGLTY,NMEA\n", 12 );
proto = "pvii";
}
else
{
// open device
SourceIn = Gps.GpsOpen( parsed.getPath(), parsed.getRate() );
gps_source = true;
}
// check
if ( SourceIn < 0 )
return SourceIn;
bool binary = false;
// attach parser (should probably use a table)
if ( strcasecmp( proto, "nmea") == 0 )
{
// ascii
NmeaParser *parser = new NmeaParser();
Parser = (GpsParser *)parser;
}
else if ( strcasecmp( proto, "applanix") == 0 )
{
// binary
binary = true;
ApplanixParser *parser = new ApplanixParser();
Parser = (GpsParser *)parser;
}
else if ( strcasecmp( proto, "fastrax") == 0 )
{
// ascii
FastraxParser *parser = new FastraxParser();
Parser = (GpsParser *)parser;
}
else if ( strcasecmp( proto, "garmin") == 0 )
{
// ascii/binary
GarminParser *parser = new GarminParser();
Parser = (GpsParser *)parser;
}
else if ( strcasecmp( proto, "sirf") == 0 )
{
// binary
SirfParser *parser = new SirfParser();
Parser = (GpsParser *)parser;
}
else if ( strcasecmp( proto, "ublox") == 0 )
{
// ascii/binary
UbloxParser *parser = new UbloxParser();
Parser = (GpsParser *)parser;
}
else
{
// ascii
NmeaParser *parser = new NmeaParser();
Parser = (GpsParser *)parser;
}
// set mode
if ( binary )
Gps.SetCanonical( false, 0, 0 );
// subscribe
Parser->Subscribe( (GpsListener*)this );
// add listener
AddFd( SourceIn );
return SourceIn;
}
int GpsDaemon::DgpsConnect( const char *uri )
{
if ( !uri )
return -1;
// already connected?
if ( CorrIn >= 0 )
return CorrIn;
UriParse parsed;
parsed.setService( "2101" );
parsed.setUri( uri );
const char *proto = parsed.getProto();
if ( strcmp( proto, "dgps" ) == 0 || strcmp( proto, "dgpsip" ) == 0
|| strcmp( proto, "rtcmd" ) == 0 )
{
// RTCM server (dgpsip)
CorrIn = Dgps.DgpsConnect( parsed.getHost(), parsed.getService() );
dgps_corr = true;
}
else if ( strcmp( proto, "ntrip" ) == 0 )
{
// NTRIP caster
CorrIn = Ntrip.NtripConnect( uri );
dgps_corr = false;
}
// add listener
if ( CorrIn >= 0 )
{
strcpy( Uri, uri );
AddFd( CorrIn );
}
return CorrIn;
}
void GpsDaemon::DgpsDisconnect( void )
{
if ( CorrIn >= 0 )
;
else
return;
Dgps.DgpsClose();
Ntrip.NtripClose();
DelFd( CorrIn );
CorrIn = -1;
}
void GpsDaemon::DgpsReconnect( void )
{
if ( nClients == 0 || strlen(Uri) == 0 )
return;
log_printf( "Remote RTCM/NTRIP timed out, reconnecting" );
Dgps.DgpsClose();
Ntrip.NtripClose();
DelFd( CorrIn );
CorrIn = -1;
DgpsConnect( Uri );
}
int GpsDaemon::GpsBroadcast( const char *host )
{
const char *port = GPSD_SERVICE;
int sock = Bcast.Caster( host, port );
return sock;
}

121
daemons/gpsd/GpsDaemon.h Normal file
View File

@ -0,0 +1,121 @@
//
// GPS Daemon
// Neal Probert
//
/* prevent multiple inclusions */
#ifndef __GpsDaemon__
#define __GpsDaemon__
/* includes *****************************************************************/
#include "bzlib.h"
#include "TcpDaemon.h"
#include "UdpClient.h"
#include "UdpCaster.h"
#include "GpsDevice.h"
#include "GpsdClient.h"
#include "NmeaParser.h"
#include "SirfParser.h"
#include "ApplanixParser.h"
#include "DgpsClient.h"
#include "NtripClient.h"
#include "Logging.h"
/* defines ******************************************************************/
/* software details */
#define GPSD_NAME "gpsd"
#define GPSD_VERSION "0.1"
/* default service and port */
#define GPSD_SERVICE "gpsd"
#define GPSD_PORT "2947"
/* macros *******************************************************************/
/* structs & typedefs *******************************************************/
/* c class definitions ******************************************************/
//
// Uses the Peak Systems CAN Linux driver and Nissan CAN data
//
class GpsDaemon : public TcpDaemon, public GpsListener, public Logging {
// public data
public:
GpsdClient Gpsd;
GpsDevice Gps;
GpsParser *Parser;
DgpsClient Dgps;
NtripClient Ntrip;
// TrackClient Track;
// for corrections reconnect
char Uri[1024];
int nClients;
// private data
private:
bool gps_source; // 1=gps, 0=gpsd
bool dgps_corr; // 1=dpgs, 0=ntrip
// FDs
int SourceIn; // GpsdClient or GpsDevice
int CorrIn; // DpgsIn or NtripIn
int TrackOut; //
// UDP broadcast (data)
UdpCaster Bcast;
// misc
int clockset; // set to set clock from GPS
int msgcount;
// private methods
// static data
// public methods
public:
// constructors
GpsDaemon();
// destructor
virtual ~GpsDaemon();
// virtual functions
int ServerTimeout(void);
int ServerSocket(void);
int ClientIn( int sock, const char *buf, int n );
int ClientUp( int sock );
void ClientDown( int sock );
void ClientCast( const char *buf, int n );
// parser notifications
void PositionListener( const GpsPoint &Pos, const ErrorInfo &Err );
void TimeListener( const struct timeval &Time );
// public methods
void SetClock( void ) {clockset=1;};
int GpsOpen( const char *dev, int baud=38400 );
int GpsBroadcast( const char *host = NULL );
// corrections
int DgpsConnect( const char *host );
void DgpsDisconnect( void );
void DgpsReconnect( void );
int DgpsListen( const char *host = NULL );
// static methods
// private methods
private:
};
#endif

226
daemons/gpsd/gpsd.cpp Normal file
View File

@ -0,0 +1,226 @@
//
// 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;
}