235 lines
5.6 KiB
C++
235 lines
5.6 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.6"
|
|
|
|
volatile bool stop = false;
|
|
volatile bool resettSerialPort = 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 = clients->size()-1;
|
|
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";
|
|
stop = true;
|
|
}
|
|
tcflush(serial, TCIOFLUSH);
|
|
}
|
|
else std::cout<<"Sinkless mode\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);
|
|
|
|
int serial = openSerialPort(config);
|
|
if(!config.noSerial)
|
|
{
|
|
std::cout<<"Using serial port: "<<config.portFileName<<" at "<<config.baud<<" baud\n";
|
|
serial = serialport_init(config.portFileName.c_str(), config.baud);
|
|
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::mutex clientsMutex;
|
|
std::vector<ClientHandler> clients;
|
|
|
|
std::thread* acceptThread;
|
|
TCPServerSocket* servSock;
|
|
|
|
std::cout<<"opening TCP socket on port "<<config.port<<'\n';
|
|
try
|
|
{
|
|
servSock = new TCPServerSocket(config.port, 5, true); // Server Socket object
|
|
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); //ignore SIGPIPE
|
|
|
|
std::cout<<"starting loop\n";
|
|
|
|
while(!stop)
|
|
{
|
|
struct epoll_event ev;
|
|
if( epoll_wait(pollQue, &ev, 1, 2000) == 1)
|
|
{
|
|
if(ev.data.fd != -1)
|
|
{
|
|
int i = ev.data.fd;
|
|
std::cout<<"client poll\n";
|
|
clientsMutex.lock();
|
|
if(ev.events & EPOLLIN) clients[i].run(&clients, serial, config.verbose);
|
|
if((ev.events & (EPOLLHUP | EPOLLERR)) || clients[i].isDisconnected())
|
|
{
|
|
clients[i].cleanUp();
|
|
clients.erase(clients.begin()+i);
|
|
std::cout<<"client "<<i<<" disconnected\n";
|
|
}
|
|
clientsMutex.unlock();
|
|
}
|
|
else
|
|
{
|
|
char buffer[4096];
|
|
int readlen = sRead(serial, buffer, 4096);
|
|
if(config.verbose)
|
|
{
|
|
std::cout<<"Sending \"";
|
|
for(size_t i = 0; i < readlen; ++i) std::cout<<buffer[i];
|
|
std::cout<<"\" to clients from serial\n";
|
|
}
|
|
for(ClientHandler& client : clients)
|
|
{
|
|
client.write(buffer, readlen);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
acceptThread->join();
|
|
delete acceptThread;
|
|
for(ClientHandler& client : clients) client.cleanUp();
|
|
servSock->cleanUp();
|
|
delete servSock;
|
|
return 0;
|
|
}
|