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 seriald)
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
SerialDaemon.cpp
)
# executables
add_executable(${TARGET} ${TARGET}.cpp ${SRCS})
install(TARGETS ${TARGET} DESTINATION sbin)

View File

@ -0,0 +1,235 @@
//
// Serial 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 "SerialDaemon.h"
#include "netlib.h"
//****************************************************************************
// defines
//***************************************************************************/
//****************************************************************************
// macros
//***************************************************************************/
//****************************************************************************
// structs & typedefs
//***************************************************************************/
//****************************************************************************
// global constants
//***************************************************************************/
//****************************************************************************
// global variables
//***************************************************************************/
//****************************************************************************
// static constants
//***************************************************************************/
//****************************************************************************
// static variables
//***************************************************************************/
//****************************************************************************
// static functions
//***************************************************************************/
//****************************************************************************
// C++ functions
//***************************************************************************/
SerialDaemon::SerialDaemon()
{
// logging
log_open( SERIALD_NAME );
m_SerialIn = m_RemoteIn = m_DgramIn = -1;
m_framed = 0;
bzero(m_Framing,sizeof(m_Framing));
}
SerialDaemon::~SerialDaemon()
{
if ( m_SerialIn >= 0 )
close( m_SerialIn );
}
/* TCP ***********************************************************************/
int SerialDaemon::ServerSocket(void)
{
char buf[2048];
int n = 0;
if ( IsSet( m_RemoteIn ) )
{
// from server
n = Client.Recv( buf, sizeof(buf) );
if ( n > 0 )
{
// send frame
if ( m_framed )
{
m_Framing[m_framed-2] = n >> 8;
m_Framing[m_framed-1] = n;
write( m_SerialIn, m_Framing, m_framed );
}
// send data
write( m_SerialIn, buf, n );
}
}
else if ( IsSet( m_SerialIn ) )
{
// data from device
n = read( m_SerialIn, buf, sizeof(buf) );
// parse data
if ( n>0 )
ClientCast( buf, n );
}
else if ( IsSet( m_DgramIn ) )
{
// packet from UDP broadcast
n = Dgram.RecvFrom( buf, sizeof(buf) );
// send to device
if ( n > 0 )
{
// send frame
if ( m_framed )
{
m_Framing[m_framed-2] = n >> 8;
m_Framing[m_framed-1] = n;
write( m_SerialIn, m_Framing, m_framed );
}
// send data
write( m_SerialIn, buf, n );
}
}
return n;
}
int SerialDaemon::ClientIn( int sock, const char *buf, int n )
{
return write( m_SerialIn, buf, n );
}
void SerialDaemon::ClientCast( const char *buf, int n )
{
// broadcast it
Bcast.SendTo( buf, n );
// send to clients
TcpServer::ClientCast( buf, n );
}
/* GPS ***********************************************************************/
int SerialDaemon::SerialOpen( const char *device, int baud )
{
UriParse parsed;
parsed.setRate( baud );
parsed.setUri( device );
int binary = 0;
// parse type
if ( strlen( parsed.getProto() ) )
{
if ( strcasecmp( parsed.getProto(), "binary" ) == 0 )
binary = 1;
}
// open device
m_SerialIn = serial_open( (char*)parsed.getPath(), parsed.getRate(), binary, binary?1:0 );
// check
if ( m_SerialIn < 0 )
return m_SerialIn;
// add listener
AddFd( m_SerialIn );
return m_SerialIn;
}
int SerialDaemon::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 SerialDaemon::UdpListen( const char *uri )
{
m_DgramIn = Dgram.ListenUri( uri );
if ( m_DgramIn > 0 )
AddFd( m_DgramIn );
return m_DgramIn;
}
int SerialDaemon::UdpBroadcast( const char *uri )
{
return Bcast.CasterUri( uri );
}
void SerialDaemon::SetFrame( const char *frame, int n )
{
if ( n )
{
// string
strncpy( m_Framing, frame, sizeof(m_Framing)-2 );
m_framed = strlen(m_Framing)+2;
}
else
{
// hex
unsigned long value = atol( frame );
m_framed = 6;
m_Framing[0] = value >> 24;
m_Framing[1] = value >> 16;
m_Framing[2] = value >> 8;
m_Framing[3] = value;
}
}

View File

@ -0,0 +1,92 @@
//
// Serial Daemon
// Neal Probert
//
/* prevent multiple inclusions */
#ifndef __SerialDaemon__
#define __SerialDaemon__
/* includes *****************************************************************/
#include "bzlib.h"
#include "TcpDaemon.h"
#include "TcpClient.h"
#include "UdpClient.h"
#include "UdpCaster.h"
#include "LogData.h"
/* defines ******************************************************************/
#define SERIALD_NAME "seriald"
#define SERIALD_SERVICE "seriald"
#define SERIALD_PORT "2942"
/* macros *******************************************************************/
/* structs & typedefs *******************************************************/
/* c class definitions ******************************************************/
//
// Uses the Peak Systems CAN Linux driver and Nissan CAN data
//
class SerialDaemon : public TcpDaemon {
// public data
public:
// private data
private:
// FDs
int m_SerialIn;
int m_RemoteIn;
int m_DgramIn;
// TCP client
TcpClient Client;
// UDP listen
UdpClient Dgram;
// UDP broadcast
UdpCaster Bcast;
// misc
int m_framed;
char m_Framing[8];
// private methods
// static data
// public methods
public:
// constructors
SerialDaemon();
// destructor
virtual ~SerialDaemon();
// virtual functions
int ServerSocket(void);
int ClientIn( int sock, const char *buf, int n );
void ClientCast( const char *buf, int n );
// public methods
int SerialOpen( const char *dev, int baud );
int TcpConnect( const char *uri );
int UdpBroadcast( const char *uri );
int UdpListen( const char *uri );
void SetFrame( const char *frame, int n=0 );
// static methods
// private methods
private:
};
#endif

175
daemons/seriald/seriald.cpp Normal file
View File

@ -0,0 +1,175 @@
//
// 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 "SerialDaemon.h"
#include "netlib.h"
extern int optind, opterr, optopt;
//
// port 2748
//
static const char *helptext =
"seriald -s /dev/ttyS0:57600 [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-o outfile output message log\n"
"\t-r uri http://host.domain.com/\n"
"\t-s port /dev/ttyS0:baud\n"
"\t-S service TCP service\n";
int main(int argc, char **argv)
{
char c; /* we have character */
SerialDaemon Ser;
/* options */
int debug = 0; // run in foreground, leaves stdio open
/* dgpsip server */
char *host = NULL;
/* service/port */
char *service = (char *)SERIALD_PORT;
/* serial port */
char *primary = NULL;
int baud = 57600;
/* udp broadcast */
char *mcast = NULL;
/* udp listener */
int listen = 0;
// add command line args
while ( (c = getopt( argc, argv, "df:hm:lr:s:S:" )) > 0 )
{
switch ( c )
{
case 'd':
debug = 1;
break;
case 'f':
Ser.SetFrame( optarg );
break;
case 'h':
puts( helptext );
return 0;
case 'l': // udp/wdm listen
listen = 1;
break;
case 'm': // udp broadcast
mcast = optarg;
break;
case 'r': // remote host
host = optarg;
break;
case 's': // serial device
primary = optarg;
break;
case 'S':
service = optarg;
break;
default:
fprintf( stderr, "Unknown option -%c\n", c );
exit(1);
}
}
// check options
if ( !primary )
{
fprintf( stderr, "Must have a serial 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 = Ser.SerialOpen( primary, baud );
if ( serial < 0 )
{
fprintf( stderr, "Unable to open primary serial device: %s @ %d !\n", primary, baud );
exit(1);
}
}
else
{
fprintf( stderr, "No task specified: use -l or -s <dev>!\n" );
exit(1);
}
// corrections
if ( host )
{
// RTCM server
int remote = Ser.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 = Ser.UdpListen( NULL );
if ( dgram < 0 )
{
fprintf( stderr, "Unable to listen on UDP broadcast!\n" );
exit(1);
}
}
// output
if ( mcast )
{
int bcast = Ser.UdpBroadcast( mcast );
if ( bcast < 0 )
{
fprintf( stderr, "Unable to create UDP broadcast!\n" );
exit(1);
}
}
// TCP listener
if ( Ser.StartService( SERIALD_NAME, NULL, service, debug ) >= 0 )
return Ser.RunService();
return 1;
}