misc
This commit is contained in:
68
main.cpp
68
main.cpp
@ -8,19 +8,28 @@
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "serial_io.h"
|
||||
#include "Socket.h"
|
||||
|
||||
#define VERSION "v0.4"
|
||||
|
||||
bool stop = false;
|
||||
volatile bool stop = false;
|
||||
volatile bool resettSerialPort = false;
|
||||
|
||||
void intHandler(int dummy)
|
||||
void intHandler(int sig)
|
||||
{
|
||||
stop = true;
|
||||
}
|
||||
|
||||
void alarmHandler(int sig)
|
||||
{
|
||||
resettSerialPort = true;
|
||||
signal(sig, alarmHandler);
|
||||
alarm(600);
|
||||
}
|
||||
|
||||
static void printUsage()
|
||||
{
|
||||
std::cout<<"usage mulitplexer [option]\n\
|
||||
@ -30,6 +39,8 @@ static void printUsage()
|
||||
-P, --port tcp port to use\n\
|
||||
-b, --baud set baud rate with termios id\n\
|
||||
-r, --rates list Available baud rates\n\
|
||||
-v, --verbose Run in verbose mode\n\
|
||||
-i, --spreint Peridoicly reinit serialport\n\
|
||||
-s, --sinkless run without serial port\n";
|
||||
}
|
||||
|
||||
@ -40,6 +51,7 @@ struct Config
|
||||
int baud = B9600;
|
||||
bool noSerial = false;
|
||||
bool verbose = false;
|
||||
bool reinit = false;
|
||||
};
|
||||
|
||||
static int parseCmdArgs(int argc, char** argv, Config *config)
|
||||
@ -80,6 +92,10 @@ static int parseCmdArgs(int argc, char** argv, Config *config)
|
||||
{
|
||||
config->verbose=true;
|
||||
}
|
||||
else if (std::string(argv[i]) == "-i" || std::string(argv[i]) == "--spreint")
|
||||
{
|
||||
config->reinit=true;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -99,6 +115,24 @@ void acceptThreadFunction( TCPServerSocket* servSock, std::vector<TCPSocket*>*
|
||||
}
|
||||
}
|
||||
|
||||
int openSerialPort(const Config& config)
|
||||
{
|
||||
int serial = -1;
|
||||
if(!config.noSerial)
|
||||
{
|
||||
std::cout<<"Opeing serial port: "<<config.portFileName<<" at "<<config.baud<<" baud\n";
|
||||
serial = serialport_init(config.portFileName.c_str(), config.baud);
|
||||
if(serial == -1)
|
||||
{
|
||||
std::cout<<"Opeing serial port failed\n";
|
||||
stop = true;
|
||||
}
|
||||
tcflush(serial, TCIOFLUSH);
|
||||
}
|
||||
else std::cout<<"Sinkless mode\n";
|
||||
return serial;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
Config config;
|
||||
@ -107,7 +141,7 @@ int main(int argc, char* argv[])
|
||||
|
||||
std::cout<<"UVOS serial mulitplexer "<<VERSION<<'\n';
|
||||
|
||||
int serial = -1;
|
||||
int serial = openSerialPort(config);
|
||||
if(!config.noSerial)
|
||||
{
|
||||
std::cout<<"Using serial port: "<<config.portFileName<<" at "<<config.baud<<" baud\n";
|
||||
@ -139,6 +173,9 @@ int main(int argc, char* argv[])
|
||||
|
||||
signal(SIGINT, intHandler);
|
||||
signal(SIGTERM, intHandler);
|
||||
signal(SIGALRM, alarmHandler);
|
||||
|
||||
if(config.reinit) alarm(600);
|
||||
|
||||
std::cout<<"starting loop\n";
|
||||
|
||||
@ -170,8 +207,7 @@ int main(int argc, char* argv[])
|
||||
std::cout<<"rec: ";
|
||||
for( int j = 0; j < reclen; j++ )std::cout<<inBuffer[j];
|
||||
}
|
||||
|
||||
if(strncmp( inBuffer, "bcst: ", 6 ) == 0)
|
||||
if(strncmp( inBuffer, "bcst:", 5) == 0)
|
||||
{
|
||||
if(config.verbose)
|
||||
{
|
||||
@ -182,6 +218,21 @@ int main(int argc, char* argv[])
|
||||
for(unsigned int j = 0; j < clientSockets.size(); j++) if(i != j) clientSockets[j]->send(inBuffer+6, reclen-6);
|
||||
|
||||
}
|
||||
else if(strncmp( inBuffer, "reinitsp", 8) == 0 && serial != -1)
|
||||
{
|
||||
if(serialport_set_config(serial, config.baud) == 0)
|
||||
{
|
||||
char response[] = "reinit serial port succsesfull\n";
|
||||
std::cerr<<response;
|
||||
clientSockets[i]->send(response, sizeof(response));
|
||||
}
|
||||
else
|
||||
{
|
||||
char response[] = "reinit serial port failed!\n";
|
||||
std::cerr<<response;
|
||||
clientSockets[i]->send(response, sizeof(response));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(config.verbose)
|
||||
@ -192,7 +243,6 @@ int main(int argc, char* argv[])
|
||||
}
|
||||
sWrite(serial, inBuffer, reclen);
|
||||
}
|
||||
|
||||
if(config.verbose) std::cout<<std::endl;
|
||||
}
|
||||
else if(reclen == 0)
|
||||
@ -213,6 +263,12 @@ int main(int argc, char* argv[])
|
||||
if(i < 0) i=0;
|
||||
}
|
||||
}
|
||||
if(resettSerialPort)
|
||||
{
|
||||
close(serial);
|
||||
serial = openSerialPort(config);
|
||||
resettSerialPort = false;
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,6 @@
|
||||
void sWrite(int port, char string[], size_t length)
|
||||
{
|
||||
if(port != -1) write(port, string, length);
|
||||
|
||||
}
|
||||
|
||||
void sWrite(int port, const char string[], size_t length)
|
||||
@ -42,17 +41,9 @@ std::cout<<"Rates:\n"\
|
||||
}
|
||||
#endif
|
||||
|
||||
int serialport_init(const char* device, int baud)
|
||||
int serialport_set_config(int fd, int baud)
|
||||
{
|
||||
int fd;
|
||||
struct termios toptions;
|
||||
fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
|
||||
if (fd == -1)
|
||||
{
|
||||
perror("init_serialport: Unable to open port ");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tcgetattr(fd, &toptions) < 0)
|
||||
{
|
||||
perror("init_serialport: Couldn't get term attributes");
|
||||
@ -93,6 +84,24 @@ int serialport_init(const char* device, int baud)
|
||||
perror("init_serialport: Couldn't set term attributes");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int serialport_init(const char* device, int baud, bool block = false)
|
||||
{
|
||||
int fd;
|
||||
fd = open(device, O_RDWR | O_NOCTTY | (block ? 0 : O_NONBLOCK));
|
||||
if (fd == -1)
|
||||
{
|
||||
perror("init_serialport: Unable to open port ");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(serialport_set_config(fd, baud) != 0)
|
||||
{
|
||||
close(fd);
|
||||
fd = -1;
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
|
@ -20,6 +20,8 @@ ssize_t sRead(int port, void *buf, size_t count);
|
||||
void printRates();
|
||||
#endif
|
||||
|
||||
int serialport_set_config(int fd, int baud);
|
||||
|
||||
int serialport_init(const char* device, int baud = BAUDRATE);
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user