319 lines
7.2 KiB
C++
319 lines
7.2 KiB
C++
#include <iostream>
|
|
#include <string.h>
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/un.h>
|
|
#include <sys/epoll.h>
|
|
#include <string>
|
|
#include <cstdlib>
|
|
#include <vector>
|
|
#include <thread>
|
|
#include <mutex>
|
|
#include <signal.h>
|
|
#include <unistd.h>
|
|
|
|
#include "serial_io.h"
|
|
#include "Socket.h"
|
|
#include "clienthandler.h"
|
|
|
|
#define VERSION "v0.7"
|
|
|
|
sig_atomic_t stop = false;
|
|
|
|
void intHandler(int sig)
|
|
{
|
|
stop = true;
|
|
}
|
|
|
|
static void printUsage()
|
|
{
|
|
std::cout<<"usage mulitplexer [option]\n\
|
|
Available options:\n\
|
|
-h, --help print this help\n\
|
|
-p, --serialport serial port device to use\n\
|
|
-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";
|
|
}
|
|
|
|
struct Config
|
|
{
|
|
std::string portFileName = "/dev/ttyUSB0";
|
|
unsigned short port = 6856;
|
|
int baud = B9600;
|
|
bool noSerial = false;
|
|
bool verbose = false;
|
|
bool reinit = false;
|
|
};
|
|
|
|
static int parseCmdArgs(int argc, char** argv, Config *config)
|
|
{
|
|
for (int i = 1; i < argc; i++)
|
|
{
|
|
if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h")
|
|
{
|
|
printUsage();
|
|
return -1;
|
|
}
|
|
else if (std::string(argv[i]) == "--serialport" || std::string(argv[i]) == "-p")
|
|
{
|
|
if(argc > i)
|
|
config->portFileName = argv[i+1];
|
|
else
|
|
return -1;
|
|
}
|
|
else if (std::string(argv[i]) == "--port" || std::string(argv[i]) == "-P")
|
|
{
|
|
if(argc > i)
|
|
config->port = atoi(argv[i+1]);
|
|
else
|
|
return -1;
|
|
}
|
|
else if (std::string(argv[i]) == "--baud" || std::string(argv[i]) == "-b")
|
|
{
|
|
if(argc > i)
|
|
config->baud = atoi(argv[i+1]);
|
|
else
|
|
return -1;
|
|
}
|
|
else if (std::string(argv[i]) == "--sinkless" || std::string(argv[i]) == "-s" )
|
|
{
|
|
config->noSerial=true;
|
|
}
|
|
else if (std::string(argv[i]) == "-r" || std::string(argv[i]) == "--rates")
|
|
{
|
|
printRates();
|
|
return -1;
|
|
}
|
|
else if (std::string(argv[i]) == "-v" || std::string(argv[i]) == "--verbose")
|
|
{
|
|
config->verbose=true;
|
|
}
|
|
else if (std::string(argv[i]) == "-i" || std::string(argv[i]) == "--spreint")
|
|
{
|
|
config->reinit=true;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void acceptThreadFunction( TCPServerSocket* servSock, std::vector<ClientHandler>* clients, std::mutex* clientsMutex,
|
|
int pollQue )
|
|
{
|
|
while(!stop)
|
|
{
|
|
TCPSocket* newSock = servSock->accept();
|
|
if(newSock != nullptr)
|
|
{
|
|
clientsMutex->lock();
|
|
clients->push_back(ClientHandler(newSock)); // Wait for a client to connect
|
|
struct epoll_event ev;
|
|
ev.events = EPOLLIN;
|
|
ev.data.fd = newSock->getFD();
|
|
clientsMutex->unlock();
|
|
epoll_ctl(pollQue, EPOLL_CTL_ADD, newSock->getFD(), &ev);
|
|
char welcomeMesg[] = "UVOS serial multiplexer " VERSION "\n";
|
|
clients->back().write(welcomeMesg, sizeof(welcomeMesg)-1);
|
|
std::cout<<"got client\n";
|
|
}
|
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
|
}
|
|
}
|
|
|
|
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";
|
|
tcflush(serial, TCIOFLUSH);
|
|
}
|
|
else
|
|
std::cout<<"Sinkless mode\n";
|
|
return serial;
|
|
}
|
|
|
|
int serialPortReconnect(Config& config, std::string err)
|
|
{
|
|
std::cout<<"serial port execption "<<err<<"\ntrying to reopen\n";
|
|
|
|
sleep(10);
|
|
int serial = openSerialPort(config);
|
|
if(serial == -1)
|
|
{
|
|
|
|
if(config.portFileName == "/dev/ttyUSB0")
|
|
{
|
|
std::cout<<"Could not reopen "<<config.portFileName<<" trying /dev/ttyUSB1\n";
|
|
config.portFileName = "/dev/ttyUSB1";
|
|
serial = openSerialPort(config);
|
|
}
|
|
else if(config.portFileName == "/dev/ttyUSB1")
|
|
{
|
|
std::cout<<"Could not reopen "<<config.portFileName<<" trying /dev/ttyUSB0\n";
|
|
config.portFileName = "/dev/ttyUSB0";
|
|
serial = openSerialPort(config);
|
|
}
|
|
|
|
if(serial == -1)
|
|
std::cout<<"Could not reopen "<<config.portFileName<<'\n';
|
|
}
|
|
return serial;
|
|
}
|
|
|
|
int main(int argc, char* argv[])
|
|
{
|
|
Config config;
|
|
|
|
if(parseCmdArgs(argc, argv, &config) != 0)
|
|
return -1;
|
|
|
|
std::cout<<"UVOS serial mulitplexer "<<VERSION<<'\n';
|
|
|
|
int pollQue = epoll_create1(0);
|
|
|
|
std::mutex clientsMutex;
|
|
std::vector<ClientHandler> clients;
|
|
|
|
int serial = openSerialPort(config);
|
|
if(!config.noSerial)
|
|
{
|
|
if(serial == -1)
|
|
{
|
|
return 1;
|
|
}
|
|
else
|
|
{
|
|
struct epoll_event ev = {};
|
|
ev.events = EPOLLIN;
|
|
ev.data.fd = -1;
|
|
epoll_ctl(pollQue, EPOLL_CTL_ADD, serial, &ev);
|
|
}
|
|
tcflush(serial, TCIOFLUSH);
|
|
}
|
|
else
|
|
{
|
|
std::cout<<"Sinkless mode\n";
|
|
}
|
|
|
|
std::thread* acceptThread;
|
|
TCPServerSocket* servSock;
|
|
|
|
std::cout<<"opening TCP socket on port "<<config.port<<'\n';
|
|
try
|
|
{
|
|
servSock = new TCPServerSocket(config.port, 5, true);
|
|
servSock->setBlocking(false);
|
|
acceptThread = new std::thread(acceptThreadFunction, servSock, &clients, &clientsMutex, pollQue);
|
|
}
|
|
catch(SocketException &e)
|
|
{
|
|
std::cerr<<"Could not open port"<<std::endl;
|
|
return 1;
|
|
}
|
|
|
|
signal(SIGINT, intHandler);
|
|
signal(SIGTERM, intHandler);
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
std::cout<<"starting loop\n";
|
|
|
|
while(!stop)
|
|
{
|
|
struct epoll_event ev;
|
|
if(epoll_wait(pollQue, &ev, 1, 2000) == 1)
|
|
{
|
|
if(ev.data.fd != -1)
|
|
{
|
|
std::vector<ClientHandler>::iterator client =
|
|
std::find(clients.begin(), clients.end(), ev.data.fd);
|
|
if(client == clients.end())
|
|
continue;
|
|
clientsMutex.lock();
|
|
std::cout<<"client poll\n";
|
|
if(ev.events & EPOLLIN)
|
|
{
|
|
try
|
|
{
|
|
client->run(&clients, serial, config.verbose);
|
|
}
|
|
catch(serialIoException& ex)
|
|
{
|
|
close(serial);
|
|
serial = serialPortReconnect(config, ex.what());
|
|
if(serial < 0)
|
|
{
|
|
std::cerr<<"Serial port connection has failed with: "<<strerror(errno)<<'\n';
|
|
return 2;
|
|
}
|
|
|
|
struct epoll_event ev = {};
|
|
ev.events = EPOLLIN;
|
|
ev.data.fd = -1;
|
|
epoll_ctl(pollQue, EPOLL_CTL_ADD, serial, &ev);
|
|
}
|
|
}
|
|
if((ev.events & (EPOLLHUP | EPOLLERR)) || client->isDisconnected())
|
|
{
|
|
client->cleanUp();
|
|
clients.erase(client);
|
|
std::cout<<"client disconnected\n";
|
|
}
|
|
clientsMutex.unlock();
|
|
}
|
|
else
|
|
{
|
|
char buffer[4096];
|
|
ssize_t readlen = sRead(serial, buffer, 4096);
|
|
if(readlen < 0 && (errno != EAGAIN || errno != EWOULDBLOCK))
|
|
{
|
|
close(serial);
|
|
serial = serialPortReconnect(config, strerror(errno));
|
|
if(serial < 0)
|
|
{
|
|
std::cerr<<"Serial port connection has failed with: "<<strerror(errno)<<'\n';
|
|
return 2;
|
|
}
|
|
|
|
struct epoll_event ev = {};
|
|
ev.events = EPOLLIN;
|
|
ev.data.fd = -1;
|
|
epoll_ctl(pollQue, EPOLL_CTL_ADD, serial, &ev);
|
|
}
|
|
if(config.verbose)
|
|
{
|
|
std::cout<<"Sending \"";
|
|
for(ssize_t i = 0; i < readlen; ++i)
|
|
{
|
|
if(buffer[i] == '\n')
|
|
std::cout<<"\\n";
|
|
else
|
|
std::cout<<buffer[i];
|
|
}
|
|
std::cout<<"\" to clients from serial\n";
|
|
}
|
|
clientsMutex.lock();
|
|
for(ClientHandler& client : clients)
|
|
{
|
|
client.write(buffer, readlen);
|
|
}
|
|
clientsMutex.unlock();
|
|
}
|
|
}
|
|
}
|
|
|
|
acceptThread->join();
|
|
delete acceptThread;
|
|
for(ClientHandler& client : clients)
|
|
client.cleanUp();
|
|
servSock->cleanUp();
|
|
delete servSock;
|
|
return 0;
|
|
}
|