Initial commit of files
This commit is contained in:
22
daemons/stdiod/CMakeLists.txt
Normal file
22
daemons/stdiod/CMakeLists.txt
Normal file
@ -0,0 +1,22 @@
|
||||
cmake_minimum_required(VERSION 3.7)
|
||||
set(NAME stdiod)
|
||||
project(Cnomicon-Daemon)
|
||||
set(TARGET ${NAME})
|
||||
set(CMAKE_INSTALL_PREFIX ..)
|
||||
|
||||
# includes
|
||||
include_directories(
|
||||
/usr/include/libxml2
|
||||
../include/libnet++ ../include)
|
||||
link_directories(../lib)
|
||||
link_libraries(net++ net pthread rt)
|
||||
|
||||
# sources
|
||||
set(SRCS
|
||||
StdioDaemon.cpp
|
||||
)
|
||||
|
||||
# executables
|
||||
add_executable(${TARGET} ${TARGET}.cpp ${SRCS})
|
||||
|
||||
install(TARGETS ${TARGET} DESTINATION sbin)
|
||||
162
daemons/stdiod/StdioDaemon.cpp
Normal file
162
daemons/stdiod/StdioDaemon.cpp
Normal file
@ -0,0 +1,162 @@
|
||||
//
|
||||
// Stdio 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 "StdioDaemon.h"
|
||||
#include "netlib.h"
|
||||
|
||||
//****************************************************************************
|
||||
// defines
|
||||
//***************************************************************************/
|
||||
|
||||
//****************************************************************************
|
||||
// macros
|
||||
//***************************************************************************/
|
||||
|
||||
//****************************************************************************
|
||||
// structs & typedefs
|
||||
//***************************************************************************/
|
||||
|
||||
//****************************************************************************
|
||||
// global constants
|
||||
//***************************************************************************/
|
||||
|
||||
//****************************************************************************
|
||||
// global variables
|
||||
//***************************************************************************/
|
||||
|
||||
//****************************************************************************
|
||||
// static constants
|
||||
//***************************************************************************/
|
||||
|
||||
//****************************************************************************
|
||||
// static variables
|
||||
//***************************************************************************/
|
||||
|
||||
//****************************************************************************
|
||||
// static functions
|
||||
//***************************************************************************/
|
||||
|
||||
//****************************************************************************
|
||||
// C++ functions
|
||||
//***************************************************************************/
|
||||
|
||||
StdioDaemon::StdioDaemon()
|
||||
{
|
||||
// logging
|
||||
log_open( STDIOD_NAME );
|
||||
|
||||
m_RemoteIn = m_DgramIn = -1;
|
||||
m_Stdin = 0;
|
||||
AddFd( m_Stdin );
|
||||
}
|
||||
|
||||
StdioDaemon::~StdioDaemon()
|
||||
{
|
||||
}
|
||||
|
||||
/* TCP ***********************************************************************/
|
||||
|
||||
int StdioDaemon::ServerSocket(void)
|
||||
{
|
||||
char buf[2048];
|
||||
int n = 0;
|
||||
|
||||
if ( IsSet( m_RemoteIn ) )
|
||||
{
|
||||
// from server (tbd)
|
||||
int n = Client.Recv( buf, sizeof(buf) );
|
||||
|
||||
if ( n > 0 )
|
||||
fputs( buf, stdout );
|
||||
}
|
||||
else if ( IsSet( m_Stdin ) )
|
||||
{
|
||||
// packet from GPS device
|
||||
while ( fgets( buf, sizeof(buf), stdin ) )
|
||||
{
|
||||
int n = strlen( buf );
|
||||
|
||||
if ( n>0 )
|
||||
ClientCast( buf, n );
|
||||
}
|
||||
}
|
||||
else if ( IsSet( m_DgramIn ) )
|
||||
{
|
||||
// RTCM packet from UDP broadcast (once a minute)
|
||||
int n = Dgram.RecvFrom( buf, sizeof(buf) );
|
||||
|
||||
// send to GPS device
|
||||
if ( n > 0 )
|
||||
fputs( buf, stdout );
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
int StdioDaemon::ClientIn( int sock, const char *buf, int n )
|
||||
{
|
||||
return fputs( buf, stdout );
|
||||
}
|
||||
|
||||
void StdioDaemon::ClientCast( const char *buf, int n )
|
||||
{
|
||||
// broadcast it
|
||||
Bcast.SendTo( buf, n );
|
||||
|
||||
// send to clients
|
||||
TcpServer::ClientCast( buf, n );
|
||||
}
|
||||
|
||||
/* GPS ***********************************************************************/
|
||||
|
||||
int StdioDaemon::TcpConnect( const char *uri )
|
||||
{
|
||||
UriParse parsed;
|
||||
parsed.setUri( uri );
|
||||
|
||||
const char *proto = parsed.getProto();
|
||||
|
||||
if ( strcasecmp( proto, "http" ) == 0 ||
|
||||
strcasecmp( proto, "https" ) == 0 )
|
||||
{
|
||||
// web site
|
||||
}
|
||||
else if ( strcasecmp( proto, "telnet" ) == 0 ||
|
||||
strlen( proto ) == 0 )
|
||||
{
|
||||
// straight TCP client
|
||||
m_RemoteIn = Client.Connect( parsed.getHost(), parsed.getService() );
|
||||
}
|
||||
|
||||
return m_RemoteIn;
|
||||
}
|
||||
|
||||
int StdioDaemon::UdpListen( const char *uri )
|
||||
{
|
||||
m_DgramIn = Dgram.ListenUri( uri );
|
||||
|
||||
if ( m_DgramIn > 0 )
|
||||
AddFd( m_DgramIn );
|
||||
|
||||
return m_DgramIn;
|
||||
}
|
||||
|
||||
int StdioDaemon::UdpBroadcast( const char *uri )
|
||||
{
|
||||
return Bcast.CasterUri( uri );
|
||||
}
|
||||
87
daemons/stdiod/StdioDaemon.h
Normal file
87
daemons/stdiod/StdioDaemon.h
Normal file
@ -0,0 +1,87 @@
|
||||
//
|
||||
// Stdio Daemon
|
||||
// Neal Probert
|
||||
//
|
||||
|
||||
/* prevent multiple inclusions */
|
||||
#ifndef __StdioDaemon__
|
||||
#define __StdioDaemon__
|
||||
|
||||
/* includes *****************************************************************/
|
||||
|
||||
#include "bzlib.h"
|
||||
|
||||
#include "TcpDaemon.h"
|
||||
#include "TcpClient.h"
|
||||
#include "UdpClient.h"
|
||||
#include "UdpCaster.h"
|
||||
|
||||
#include "LogData.h"
|
||||
|
||||
/* defines ******************************************************************/
|
||||
|
||||
#define STDIOD_NAME "stdiod"
|
||||
#define STDIOD_SERVICE "stdiod"
|
||||
#define STDIOD_PORT "2941"
|
||||
|
||||
/* macros *******************************************************************/
|
||||
|
||||
/* structs & typedefs *******************************************************/
|
||||
|
||||
/* c class definitions ******************************************************/
|
||||
|
||||
//
|
||||
// Uses the Peak Systems CAN Linux driver and Nissan CAN data
|
||||
//
|
||||
class StdioDaemon : public TcpDaemon {
|
||||
// public data
|
||||
public:
|
||||
|
||||
// private data
|
||||
private:
|
||||
// FDs
|
||||
int m_Stdin;
|
||||
int m_RemoteIn;
|
||||
int m_DgramIn;
|
||||
|
||||
// TCP client
|
||||
TcpClient Client;
|
||||
|
||||
// UDP listen
|
||||
UdpClient Dgram;
|
||||
|
||||
// UDP broadcast
|
||||
UdpCaster Bcast;
|
||||
|
||||
// misc
|
||||
|
||||
// private methods
|
||||
|
||||
// static data
|
||||
|
||||
// public methods
|
||||
public:
|
||||
// constructors
|
||||
StdioDaemon();
|
||||
|
||||
// destructor
|
||||
virtual ~StdioDaemon();
|
||||
|
||||
// virtual functions
|
||||
int ServerSocket(void);
|
||||
int ClientIn( int sock, const char *buf, int n );
|
||||
void ClientCast( const char *buf, int n );
|
||||
|
||||
// public methods
|
||||
int TcpConnect( const char *uri );
|
||||
|
||||
int UdpBroadcast( const char *uri );
|
||||
int UdpListen( const char *uri );
|
||||
|
||||
// static methods
|
||||
|
||||
// private methods
|
||||
private:
|
||||
};
|
||||
|
||||
#endif
|
||||
140
daemons/stdiod/stdiod.cpp
Normal file
140
daemons/stdiod/stdiod.cpp
Normal file
@ -0,0 +1,140 @@
|
||||
//
|
||||
// Stdio 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 "StdioDaemon.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-h help\n"
|
||||
"\t-l UDP listen for data\n"
|
||||
"\t-m network broadcast data\n"
|
||||
"\t-S service TCP service\n";
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
char c; /* we have character */
|
||||
|
||||
StdioDaemon Std;
|
||||
|
||||
/* options */
|
||||
int debug = 0; // run in foreground, leaves stdio open
|
||||
|
||||
/* dgpsip server */
|
||||
char *host = NULL;
|
||||
|
||||
/* udp broadcast */
|
||||
char *mcast = NULL;
|
||||
|
||||
/* udp listener */
|
||||
char *listen = NULL;
|
||||
|
||||
/* server */
|
||||
char *service = (char *)STDIOD_PORT;
|
||||
|
||||
// add command line args
|
||||
while ( (c = getopt( argc, argv, "dhm:l:r:S:" )) > 0 )
|
||||
{
|
||||
switch ( c )
|
||||
{
|
||||
case 'd':
|
||||
debug = 1;
|
||||
break;
|
||||
case 'h':
|
||||
puts( helptext );
|
||||
return 0;
|
||||
case 'l': // udp listen
|
||||
listen = optarg;
|
||||
break;
|
||||
case 'm': // udp broadcast
|
||||
mcast = optarg;
|
||||
break;
|
||||
case 'r': // remote host
|
||||
host = optarg;
|
||||
break;
|
||||
case 'S':
|
||||
service = optarg;
|
||||
break;
|
||||
default:
|
||||
fprintf( stderr, "Unknown option -%c\n", c );
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// check options
|
||||
if ( mcast && listen )
|
||||
{
|
||||
fprintf( stderr, "Cannot do both UDP broadcast/multicast and listening!\n" );
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// input
|
||||
|
||||
// corrections
|
||||
if ( host )
|
||||
{
|
||||
// RTCM server
|
||||
int remote = Std.TcpConnect( host );
|
||||
|
||||
if ( remote < 0 )
|
||||
{
|
||||
fprintf( stderr, "Unable to connect to remote host: %s\n", host );
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
else if ( listen )
|
||||
{
|
||||
// listen on UDP broadcasts for RTCM corrrections
|
||||
int dgram = Std.UdpListen( listen );
|
||||
|
||||
if ( dgram < 0 )
|
||||
{
|
||||
fprintf( stderr, "Unable to listen on UDP broadcast!\n" );
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// output
|
||||
if ( mcast )
|
||||
{
|
||||
int bcast = Std.UdpBroadcast( mcast );
|
||||
|
||||
if ( bcast < 0 )
|
||||
{
|
||||
fprintf( stderr, "Unable to create UDP broadcast!\n" );
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// TCP listener
|
||||
if ( Std.StartService( STDIOD_NAME, NULL, service, debug ) >= 0 )
|
||||
return Std.RunService();
|
||||
|
||||
return 1;
|
||||
}
|
||||
Reference in New Issue
Block a user