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,22 @@
cmake_minimum_required(VERSION 3.7)
set(NAME rtcmd)
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 pthread rt)
# sources
set(SRCS
RtcmDaemon.cpp
)
# executables
add_executable(${TARGET} ${TARGET}.cpp ${SRCS})
install(TARGETS ${TARGET} DESTINATION sbin)

View File

@ -0,0 +1,307 @@
//
// RTCM Daemon
// Neal Probert
//
//****************************************************************************
// includes
//***************************************************************************/
#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 "RtcmDaemon.h"
#include "netlib.h"
//****************************************************************************
// defines
//***************************************************************************/
//****************************************************************************
// macros
//***************************************************************************/
//****************************************************************************
// structs & typedefs
//***************************************************************************/
//****************************************************************************
// global constants
//***************************************************************************/
//****************************************************************************
// global variables
//***************************************************************************/
//****************************************************************************
// static constants
//***************************************************************************/
//****************************************************************************
// static variables
//***************************************************************************/
//****************************************************************************
// static functions
//***************************************************************************/
//****************************************************************************
// C++ functions
//***************************************************************************/
RtcmDaemon::RtcmDaemon()
{
log_open( RTCMD_NAME );
strcpy( Uri, "" );
SerialIn = DgpsIn = NtripIn = DgramIn = -1;
nClients = 0;
SetClientTimeout(false); // don't reset timeout on client activity
}
RtcmDaemon::~RtcmDaemon()
{
}
int RtcmDaemon::ServerTimeout(void)
{
// should not timeout, try reconnecting
if ( nClients > 0 )
RtcmReconnect();
else
RtcmDisconnect();
return 0;
}
int RtcmDaemon::ServerSocket(void)
{
char buf[2048];
int n = 0, xmit = 0;
if ( IsSet( DgpsIn ) )
{
// from RTCM server
n = Dgps.DgpsRead( buf, sizeof(buf) );
// xmit
if ( n > 0 )
xmit = n;
else if ( n == 0 )
{
log_error( "Remote RTCM server closed!" );
RtcmDisconnect();
}
else
{
log_error( "Remote RTCM server read error!" );
n = 0;
}
ResetServerTimeout();
}
else if ( IsSet( NtripIn ) )
{
// from NTRIP server
n = Ntrip.NtripRead( (unsigned char *)buf, sizeof(buf) );
// xmit
if ( n > 0 )
xmit = n;
else if ( n == 0 )
{
log_error( "Remote NTRIP server closed us!" );
RtcmDisconnect();
}
else
{
log_error( "Remote NTRIP server read error!" );
n = 0;
}
ResetServerTimeout();
}
else if ( IsSet( SerialIn ) )
{
// from base station
n = Rtcm.RtcmRead( buf, sizeof(buf) );
// xmit
if ( n > 0 )
xmit = n;
}
// pass on RTCM data
if ( xmit > 0 )
{
ClientCast( buf, xmit );
}
return n;
}
void RtcmDaemon::ClientCast( const char *buf, int n )
{
// broadcast it
Bcast.SendTo( buf, n );
// send to clients
TcpServer::ClientCast( buf, n );
}
int RtcmDaemon::ClientIn( int sock, const char *buf, int n )
{
if ( n <= 0 )
return 0;
// log messages from client, no responses
if ( *buf == '$' )
{
// update to OpenGTS
// logging
char name[128];
char out[1024];
// need to add ip address
SockName( sock, name, sizeof(name) );
n = snprintf( out, sizeof(out), "[%s] %s", name, buf );
Log.LogWrite( out, n );
}
else if ( n > 0 )
log_write( buf );
return 0;
}
int RtcmDaemon::ClientUp( int sock )
{
// connect source on demand
if ( nClients++ == 0 )
{
// re-connect
RtcmConnect( (const char *)Uri );
}
return 1;
}
void RtcmDaemon::ClientDown( int sock )
{
// disconnect source
if ( --nClients <= 0 )
{
RtcmDisconnect();
nClients = 0;
}
}
int RtcmDaemon::RtcmOpen( const char *uri )
{
// already connected?
if ( SerialIn >= 0 )
Rtcm.RtcmClose();
UriParse parsed;
parsed.setRate( 38400 );
parsed.setUri( uri );
// serial device
SerialIn = Rtcm.RtcmOpen( parsed.getPath(), parsed.getRate() );
// add listener
if ( SerialIn >= 0 )
{
strcpy( Uri, uri );
AddFd( SerialIn );
}
return SerialIn;
}
int RtcmDaemon::RtcmConnect( const char *uri )
{
// already connected?
if ( DgpsIn >= 0 )
return DgpsIn;
else if ( NtripIn >= 0 )
return NtripIn;
UriParse parsed;
parsed.setService( RTCMD_PORT );
parsed.setRate( 57600 );
parsed.setUri( uri );
const char *proto = parsed.getProto();
if ( strcmp( proto, "dgps" ) == 0 || strcmp( proto, "dgpsip" ) == 0 )
{
// RTCM server (dgpsip)
DgpsIn = Dgps.DgpsConnect( parsed.getHost(), parsed.getService() );
// add listener
if ( DgpsIn >= 0 )
{
strcpy( Uri, uri );
AddFd( DgpsIn );
}
return DgpsIn;
}
else if ( strcmp( proto, "ntrip" ) == 0 )
{
// NTRIP caster
NtripIn = Ntrip.NtripConnect( uri );
// add listener
if ( NtripIn >= 0 )
{
strcpy( Uri, uri );
AddFd( NtripIn );
}
return NtripIn;
}
return -1;
}
void RtcmDaemon::RtcmDisconnect( void )
{
if ( DgpsIn >= 0 || NtripIn >= 0 )
log_printf( "Remote RTCM/NTRIP dis-connected, no clients listening" );
else
return;
Dgps.DgpsClose();
DelFd( DgpsIn );
Ntrip.NtripClose();
DelFd( NtripIn );
DgpsIn = NtripIn = -1;
}
void RtcmDaemon::RtcmReconnect( void )
{
if ( nClients == 0 )
return;
log_printf( "Remote RTCM/NTRIP timed out, reconnecting" );
Dgps.DgpsClose();
DelFd( DgpsIn );
Ntrip.NtripClose();
DelFd( NtripIn );
DgpsIn = NtripIn = -1;
RtcmConnect( Uri );
}
int RtcmDaemon::RtcmBroadcast( const char *host, const char *port )
{
return Bcast.Caster( host, port );
}

View File

@ -0,0 +1,94 @@
//
// RTCM Daemon
// Neal Probert
//
/* prevent multiple inclusions */
#ifndef __RtcmDaemon__
#define __RtcmDaemon__
/* includes *****************************************************************/
#include "TcpDaemon.h"
#include "UdpClient.h"
#include "UdpCaster.h"
#include "GpsDevice.h"
#include "RtcmDevice.h"
#include "DgpsClient.h"
#include "NtripClient.h"
#include "Logging.h"
/* defines ******************************************************************/
/* software details */
#define RTCMD_NAME "rtcmd"
#define RTCMD_VERSION "0.1"
/* default service and port */
#define RTCMD_SERVICE "rtcm-sc104"
#define RTCMD_PORT "2101"
/* macros *******************************************************************/
/* structs & typedefs *******************************************************/
/* c class definitions ******************************************************/
//
// Uses the Peak Systems CAN Linux driver and Nissan CAN data
//
class RtcmDaemon : public TcpDaemon, public Logging {
// public data
public:
DgpsClient Dgps;
NtripClient Ntrip;
RtcmDevice Rtcm;
// private data
private:
char Uri[1024];
// FDs
int DgpsIn;
int SerialIn;
int DgramIn;
int NtripIn;
// UDP broadcast
UdpCaster Bcast;
int nClients;
// static data
// public methods
public:
// constructors
RtcmDaemon();
// destructor
virtual ~RtcmDaemon();
// virtual functions
int ServerTimeout(void);
int ServerSocket(void);
int ClientIn( int sock, const char *buf, int n );
void ClientCast( const char *buf, int n );
int ClientUp( int sock );
void ClientDown( int sock );
// public methods
int RtcmOpen( const char *uri ); // serial device
int RtcmConnect( const char *uri );
void RtcmDisconnect( void );
void RtcmReconnect( void );
int RtcmBroadcast( const char *host = NULL, const char *port = RTCMD_SERVICE );
// static methods
// private methods
private:
};
#endif

145
daemons/rtcmd/rtcmd.cpp Normal file
View File

@ -0,0 +1,145 @@
//
// RTCM Daemon (rtcmd.cpp)
// 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 "RtcmDaemon.h"
#include "netlib.h"
extern int optind, opterr, optopt;
//
// This was written because gpsd didn't have working RTCM104_SERVICE
// or the ability to do UDP broadcasts.
//
static const char *helptext =
"rtcm -r dgpsip://host [options]\n"
"\t-d debug (foreground with stdio)\n"
"\t-h help\n"
"\t-m network UDP broadcast RTCM data\n"
"\t-r uri dgpsip://host or ntrip://user@pass:host:port/mount\n"
"\t-t uri OpenGTS server\n"
"\t-w WDM DSRC broadcast RTCM corrections\n"
"\t-S service TCP service (default=2101)\n"
"\t-T timeout reconnect timeout in seconds (default=15)\n";
static RtcmDaemon Rtcm;
int main(int argc, char **argv)
{
char c; /* we have character */
/* 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 *)"2101"; // "rtcm-sc104"
/* udp broadcast */
char *mcast = NULL;
/* log */
char *outfile = NULL;
// add command line args
while ( (c = getopt( argc, argv, "dhm:o:r:s:t:S:T:" )) > 0 )
{
switch ( c )
{
case 'd':
debug = 1;
break;
case 'h':
puts( helptext );
return 0;
case 'm': // udp broadcast
mcast = optarg;
break;
case 'o':
break;
case 'r': // rtcm-sc104 host
host = optarg;
break;
case 't':
break;
case 'S':
service = optarg;
break;
case 'T':
timeout = atoi(optarg);
if ( timeout == 0 )
timeout = 30;
break;
case 'w': // DSRC WSM broadcast or listen
fprintf( stderr, "DSRC WSM support not yet available!\n" );
// dsrc_wsm = 1;
break;
default:
fprintf( stderr, "Unknown option -%c\n", c );
exit(1);
}
}
// input
if ( host )
{
Rtcm.SetTimeout( timeout * 1000000L );
// RTCM server
int remote = Rtcm.RtcmConnect( host );
if ( remote < 0 )
{
fprintf( stderr, "Unable to connect to rtcm-sc104 source host: %s\n", host );
if ( debug )
exit(1);
}
}
// output
if ( mcast )
{
int bcast = Rtcm.RtcmBroadcast( mcast );
if ( bcast < 0 )
{
fprintf( stderr, "Unable to create UDP broadcast!\n" );
exit(1);
}
}
else if ( dsrc_wsm )
{
}
if ( outfile )
Rtcm.LogOpen( outfile, 0 );
// TCP listener
if ( Rtcm.StartService( RTCMD_NAME, NULL, service, debug ) >= 0 )
return Rtcm.RunService();
return 1;
}

7
daemons/rtcmd/test-mili.sh Executable file
View File

@ -0,0 +1,7 @@
!/bin/sh
rm /var/log/cnomicon/rtcmd.log
../../build/daemons/rtcmd/rtcmd -d -r ntrip://nprobert:p3x-mdotcors@148.149.0.87:10002/LIVONIA_RTCM2.3GPS
more /var/log/cnomicon/rtcmd.log