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,194 @@
//
// Track 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 "NmeaParser.h"
#include "UriParse.h"
#include "TrackDaemon.h"
#include "netlib.h"
//****************************************************************************
// defines
//***************************************************************************/
//****************************************************************************
// macros
//***************************************************************************/
//****************************************************************************
// structs & typedefs
//***************************************************************************/
//****************************************************************************
// global constants
//***************************************************************************/
//****************************************************************************
// global variables
//***************************************************************************/
//****************************************************************************
// static constants
//***************************************************************************/
//****************************************************************************
// static variables
//***************************************************************************/
//****************************************************************************
// static functions
//***************************************************************************/
//****************************************************************************
// C++ functions
//***************************************************************************/
TrackDaemon::TrackDaemon()
{
log_open( TRACKD_NAME );
gethostname( Id, sizeof(Id) );
}
TrackDaemon::~TrackDaemon()
{
}
/* UDP ***********************************************************************/
void TrackDaemon::PositionListener( const PositionFix &Pos, const ErrorInfo &Err )
{
char buf[256];
int n = 0;
char datetime[40];
// convert date & time (MySQL friendly)
struct tm *tmp = gmtime(&Pos.Time.tv_sec);
strftime( datetime, sizeof(datetime), "%Y-%m-%d,%T", tmp );
// convert to unicsv
n = snprintf( buf, sizeof(buf),
"%s,%s,%lf,%lf,%lf,%lf,%lf\n",
Id, // name
datetime, // time
Pos.Latitude, // lat
Pos.Longitude, // long
Pos.Altitude, // elev
Pos.Speed, // speed (m/sec -> kph)
Pos.Heading // heading
);
// send upstream
if ( TrackOut >= 0 )
Track.SendTo( buf, n );
// log locally
Log.LogWrite( buf, n );
}
void TrackDaemon::TimeListener( const struct timeval &tv )
{
}
int TrackDaemon::ServerSocket(void)
{
char buf[2048];
int n = 0;
if ( IsSet( GpsIn ) )
{
// Gpsd
if ( (n = Gpsd.Recv( buf, sizeof(buf) )) > 0 )
{
// use NMEA parser (should trigger on RMC or GGA)
Gps.Nmea.NmeaParse(buf);
}
}
return n;
}
int TrackDaemon::ClientInOut( char *buf, int n, int max )
{
// check
if ( n <= 0 )
return 0;
// parse $PVII
if ( buf[0] == '$' )
{
// use NMEA parser (should trigger on RMC or GGA)
Gps.Nmea.NmeaParse( buf );
}
else
{
// forward
if ( TrackOut >= 0 )
Track.SendTo( buf, n );
// log
Log.LogWrite( buf , n );
}
// no response
return 0;
}
/* GPS ***********************************************************************/
int TrackDaemon::GpsdServer( const char *uri )
{
UriParse parsed;
parsed.setProto( "gpsd" );
parsed.setService( "gpsd" );
parsed.setUri( uri );
bGpsd = 0;
const char *proto = parsed.getProto();
if ( !strlen(proto) )
proto = "gpsd";
GpsDeviceType type = Gps.Proto2Type( proto );
// open device
if ( strcmp( proto, "gpsd" ) == 0 || type == GPS_TYPE_UNDEF )
GpsIn = Gpsd.GpsdConnect( parsed.getHost() );
else
GpsIn = Gps.GpsOpen( parsed.getPath(), parsed.getRate(), type );
if ( GpsIn >= 0 )
AddFd( GpsIn );
return GpsIn;
}
int TrackDaemon::TrackOpen( const char *uri )
{
UriParse parsed;
parsed.setService( TRACKD_PORT );
parsed.setUri( uri );
// connect to remote web server to upload data
return TrackOut = Track.Connect( parsed.getHost(), TRACKD_PORT );
}
int TrackDaemon::LogOpen( const char *uri )
{
if ( Log.LogOpen( uri ) )
{
Log.LogFields( "name,date,time,lat,long,ele,speed,head" );
return 1;
}
return -1;
}

View File

@ -0,0 +1,89 @@
//
// Track Daemon
// Neal Probert
//
/* prevent multiple inclusions */
#ifndef __TrackDaemon__
#define __TrackDaemon__
/* includes *****************************************************************/
#include "bzlib.h"
#include "UdpDaemon.h"
#include "TcpClient.h"
#include "UdpClient.h"
#include "Logging.h"
#include "GpsdClient.h"
#include "GpsDevice.h"
/* defines ******************************************************************/
#define TRACKD_NAME "trackd"
#define TRACKD_SERVICE "trackd"
#define TRACKD_PORT "2948"
/* macros *******************************************************************/
/* structs & typedefs *******************************************************/
/* c class definitions ******************************************************/
//
// Listens to OBE HeartBeat messages
//
class TrackDaemon : public UdpDaemon, public GpsListener, public Logging {
// public data
public:
// private data
private:
// my name
char Id[32];
// Gpsd client
GpsdClient Gpsd; // direct from GPSd
GpsDevice Gps; // or from serial port
bool bGpsd; // true for Gpsd
int GpsIn; // Gpsd or Gps
// upstream trackd
UdpClient Track; // listener
int TrackOut;
// NMEA parsing
// private methods
// static data
// public methods
public:
// constructors
TrackDaemon();
// destructor
virtual ~TrackDaemon();
// virtual functions
int ServerSocket(void);
int ClientInOut( char *buf, int n, int max );
// public methods
int GpsdServer( const char *uri );
void PositionListener( const PositionFix &Pos, const ErrorInfo &Err );
void TimeListener( const struct timeval &Time );
void SetId( const char *id ) { strncpy(Id,id,sizeof(Id)-1); };
int TrackOpen( const char *uri );
int LogOpen( const char *uri );
// static methods
// private methods
private:
};
#endif

132
daemons/trackd/trackd.cpp Normal file
View File

@ -0,0 +1,132 @@
//
// Serial Daemon (similar to ser2net)
// 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 "TrackDaemon.h"
#include "netlib.h"
extern int optind, opterr, optopt;
//
// any port
//
static const char *helptext =
"stdiod [options]\n"
"\t-d debug (foreground with stdio)\n"
"\t-g host gpsd\n"
"\t-h help\n"
"\t-i id set my id\n"
"\t-c uri host:port source data (CardiacMonitor)\n"
"\t-o logfile format:/path/to/file.log\n"
"\t-r uri remote web host for upload\n"
"\t-S service TCP service\n";
int main(int argc, char **argv)
{
char c; /* we have character */
TrackDaemon Track;
/* options */
int debug = 0; // run in foreground, leaves stdio open
/* connections */
char *gpsd = NULL; // "localhost";
char *host = NULL;
char *id = NULL;
char *log = NULL;
char *service = TRACKD_PORT;
// add command line args
while ( (c = getopt( argc, argv, "dg:hi:o:r:S:" )) > 0 )
{
switch ( c )
{
case 'd':
debug = 1;
break;
case 'g':
gpsd = optarg;
break;
case 'h':
puts( helptext );
return 0;
case 'i':
id = optarg;
break;
case 'o':
log = optarg;
break;
case 'r':
host = optarg;
break;
case 'S':
service = optarg;
break;
default:
fprintf( stderr, "Unknown option -%c\n", c );
exit(1);
}
}
// set my id (hostname by default)
if ( id )
Track.SetId( id );
// monitor
if ( gpsd )
{
// GPSd
int remote = Track.GpsdServer( gpsd );
if ( remote < 0 )
{
fprintf( stderr, "Unable to connect to gpsd: %s\n", gpsd );
// exit(1);
}
}
// output
if ( host )
{
if ( Track.TrackOpen( host ) < 0 )
{
fprintf( stderr, "Unable to open vehicle location server: %s!\n", host );
// exit(1);
}
}
if ( log )
{
if ( !Track.LogOpen( log ) )
{
fprintf( stderr, "Unable to open log file: %s!\n", log );
exit(1);
}
}
// TCP listener
if ( Track.StartService( TRACKD_NAME, NULL, service, debug ) >= 0 )
return Track.RunService();
return 1;
}