Files
Cnomicon/daemons/seriald/seriald.cpp
2021-01-22 10:16:20 -05:00

176 lines
3.4 KiB
C++

//
// 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;
}