add serialsock

This commit is contained in:
2022-01-19 17:31:04 +03:00
parent 786ed54d03
commit 9192ee09a3
78 changed files with 5244 additions and 0 deletions

45
serialsock/Makefile Normal file
View File

@@ -0,0 +1,45 @@
# run `make DEF=...` to add extra defines
PROGRAM := serialsock
LDFLAGS := -fdata-sections -ffunction-sections -Wl,--gc-sections -Wl,--discard-all
LDFLAGS += -lusefull_macros
SRCS := $(wildcard *.c)
DEFINES := $(DEF) -D_GNU_SOURCE -D_XOPEN_SOURCE=1111
OBJDIR := mk
CFLAGS += -O3 -Wno-trampolines -std=gnu99
OBJS := $(addprefix $(OBJDIR)/, $(SRCS:%.c=%.o))
DEPS := $(OBJS:.o=.d)
CC = gcc
#CXX = g++
all : $(PROGRAM)
debug: CFLAGS += -DEBUG -Werror -Wall -Wextra
debug: all
$(OBJS): $(OBJDIR)
$(PROGRAM) : $(OBJS)
@echo -e "\t\tLD $(PROGRAM)"
$(CC) $(LDFLAGS) $(OBJS) -o $(PROGRAM)
$(OBJDIR):
mkdir $(OBJDIR)
ifneq ($(MAKECMDGOALS),clean)
-include $(DEPS)
endif
$(OBJDIR)/%.o: %.c
@echo -e "\t\tCC $<"
$(CC) -MD -c $(LDFLAGS) $(CFLAGS) $(DEFINES) -o $@ $<
clean:
@echo -e "\t\tCLEAN"
@rm -f $(OBJS) $(DEPS)
@rmdir $(OBJDIR) 2>/dev/null || true
xclean: clean
@rm -f $(PROGRAM)
.PHONY: clean xclean

1
serialsock/Readme Normal file
View File

@@ -0,0 +1 @@
Socket server for my USB-CAN

View File

@@ -0,0 +1 @@
-std=c17

View File

@@ -0,0 +1,6 @@
// Add predefined macros for your project here. For example:
// #define THE_ANSWER 42
#define EBUG
#define _GNU_SOURCE
#define _XOPEN_SOURCE=1111

View File

@@ -0,0 +1 @@
[General]

View File

@@ -0,0 +1 @@
-std=c++17

View File

@@ -0,0 +1,5 @@
cmdlnopts.c
cmdlnopts.h
main.c
sersock.c
sersock.h

View File

@@ -0,0 +1 @@
.

95
serialsock/cmdlnopts.c Normal file
View File

@@ -0,0 +1,95 @@
/*
* This file is part of the canserver project.
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include "cmdlnopts.h"
#include "usefull_macros.h"
// default PID filename:
#define DEFAULT_PIDFILE "/tmp/usbsock.pid"
#define DEFAULT_PORT "1234"
#define DEFAULT_SOCKPATH "\0canbus"
static int help;
static glob_pars G = {
.pidfile = DEFAULT_PIDFILE,
.speed = 9600,
.port = DEFAULT_PORT,
.path = DEFAULT_SOCKPATH,
.logfile = NULL // don't save logs
};
glob_pars *GP = &G;
/*
* Define command line options by filling structure:
* name has_arg flag val type argptr help
*/
static myoption cmdlnopts[] = {
{"help", NO_ARGS, NULL, 'h', arg_int, APTR(&help), _("show this help")},
{"devpath", NEED_ARG, NULL, 'd', arg_string, APTR(&G.devpath), _("serial device path")},
{"speed", NEED_ARG, NULL, 's', arg_int, APTR(&G.speed), _("serial device speed (default: 9600)")},
{"logfile", NEED_ARG, NULL, 'l', arg_string, APTR(&G.logfile), _("file to save logs (default: none)")},
{"pidfile", NEED_ARG, NULL, 'p', arg_string, APTR(&G.pidfile), _("pidfile (default: " DEFAULT_PIDFILE ")")},
{"client", NO_ARGS, NULL, 'c', arg_int, APTR(&G.client), _("run as client")},
{"sockpath",NEED_ARG, NULL, 'f', arg_string, APTR(&G.path), _("socket path (start from \\0 for no files)")},
{"port", NEED_ARG, NULL, 'P', arg_string, APTR(&G.port), _("port to connect (default: " DEFAULT_PORT ")")},
{"verbose", NO_ARGS, NULL, 'v', arg_none, APTR(&G.verbose), _("increase log verbose level (default: LOG_WARN) and messages (default: none)")},
end_option
};
/**
* Parse command line options and return dynamically allocated structure
* to global parameters
* @param argc - copy of argc from main
* @param argv - copy of argv from main
* @return allocated structure with global parameters
*/
void parse_args(int argc, char **argv){
int i;
size_t hlen = 1024;
char helpstring[1024], *hptr = helpstring;
snprintf(hptr, hlen, "Usage: %%s [args]\n\n\tWhere args are:\n");
// format of help: "Usage: progname [args]\n"
change_helpstring(helpstring);
// parse arguments
parseargs(&argc, &argv, cmdlnopts);
for(i = 0; i < argc; i++)
printf("Ignore parameter\t%s\n", argv[i]);
if(help) showhelp(-1, cmdlnopts);
}
/**
* @brief verbose - print additional messages depending of G.verbose (add '\n' at end)
* @param levl - message level
* @param fmt - message
*/
void verbose(int levl, const char *fmt, ...){
va_list ar;
if(levl > G.verbose) return;
//printf("%s: ", __progname);
va_start(ar, fmt);
vprintf(fmt, ar);
va_end(ar);
printf("\n");
}

42
serialsock/cmdlnopts.h Normal file
View File

@@ -0,0 +1,42 @@
/*
* This file is part of the canserver project.
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef CMDLNOPTS_H__
#define CMDLNOPTS_H__
/*
* here are some typedef's for global data
*/
typedef struct{
char *devpath; // path to serial device
char *pidfile; // name of PID file
char *logfile; // logging to this file
char *port; // network port
char *path; // path to socket file
int speed; // connection speed
int verbose; // verbose level: for messages & logging
int client; // ==1 if application runs in client mode
} glob_pars;
extern glob_pars *GP;
void parse_args(int argc, char **argv);
void verbose(int levl, const char *fmt, ...);
#endif // CMDLNOPTS_H__

115
serialsock/main.c Normal file
View File

@@ -0,0 +1,115 @@
/*
* This file is part of the canserver project.
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <arpa/inet.h>
#include <limits.h>
#include <netdb.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/prctl.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <usefull_macros.h>
#include "cmdlnopts.h"
#include "sersock.h"
static TTY_descr *dev = NULL;
static pid_t childpid = 0;
int server = 1;
void signals(int sig){
if(childpid){ // slave process
DBG("Child killed with sig=%d", sig);
LOGWARN("Child killed with sig=%d", sig);
exit(sig);
}
// master process
DBG("Master process");
if(!server) restore_console();
else if(dev) close_tty(&dev);
if(sig){
DBG("Exit with signal %d", sig);
signal(sig, SIG_IGN);
LOGERR("Exit with signal %d", sig);
}else LOGERR("Exit");
if(GP->pidfile && server){
DBG("Unlink pid file");
unlink(GP->pidfile);
}
exit(sig);
}
int main(int argc, char **argv){
char *self = strdup(argv[0]);
initial_setup();
parse_args(argc, argv);
if(GP->logfile){
int lvl = LOGLEVEL_WARN + GP->verbose;
DBG("level = %d", lvl);
if(lvl > LOGLEVEL_ANY) lvl = LOGLEVEL_ANY;
verbose(1, "Log file %s @ level %d\n", GP->logfile, lvl);
OPENLOG(GP->logfile, lvl, 1);
if(!globlog) WARNX("Can't create log file");
}
if(GP->client) server = 0;
else if(!GP->devpath){
LOGERR("You should point serial device path");
ERRX("You should point serial device path");
}
int port = atoi(GP->port);
if(port < 1024 || port > 65535){
LOGERR("Wrong port value: %d", port);
WARNX("Wrong port value: %d", port);
return 1;
}
if(server) check4running(self, GP->pidfile);
// signal reactions:
signal(SIGTERM, signals); // kill (-15) - quit
signal(SIGHUP, SIG_IGN); // hup - ignore
signal(SIGINT, signals); // ctrl+C - quit
signal(SIGQUIT, signals); // ctrl+\ - quit
signal(SIGTSTP, SIG_IGN); // ignore ctrl+Z
LOGMSG("Started");
#ifndef EBUG
if(server){
unsigned int pause = 5;
while(1){
childpid = fork();
if(childpid){ // master
double t0 = dtime();
LOGMSG("Created child with pid %d", childpid);
wait(NULL);
LOGWARN("Child %d died", childpid);
if(dtime() - t0 < 1.) pause += 5;
else pause = 1;
if(pause > 900) pause = 900;
sleep(pause); // wait a little before respawn
}else{ // slave
prctl(PR_SET_PDEATHSIG, SIGTERM); // send SIGTERM to child when parent dies
break;
}
}
}
#endif
return start_socket(server, GP->path, &dev);
}

386
serialsock/sersock.c Normal file
View File

@@ -0,0 +1,386 @@
/*
* This file is part of the canserver project.
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <arpa/inet.h>
#include <limits.h>
#include <netdb.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h> // unix socket
#include <usefull_macros.h>
#include "cmdlnopts.h"
#include "sersock.h"
/**
* wait for answer from socket
* @param sock - socket fd
* @return 0 in case of error or timeout, 1 in case of socket ready
*/
static int waittoread(int sock){
fd_set fds;
struct timeval timeout;
int rc;
timeout.tv_sec = 0;
timeout.tv_usec = 100;
FD_ZERO(&fds);
FD_SET(sock, &fds);
do{
rc = select(sock+1, &fds, NULL, NULL, &timeout);
if(rc < 0){
if(errno != EINTR){
WARN("select()");
return 0;
}
continue;
}
break;
}while(1);
if(FD_ISSET(sock, &fds)){
//DBG("FD_ISSET");
return 1;
}
return 0;
}
// work with single client, return FALSE if disconnected
static int handle_socket(int sock, TTY_descr *d){
char buff[BUFLEN];
ssize_t rd = read(sock, buff, BUFLEN-1);;
DBG("Got %zd bytes", rd);
if(rd <= 0){ // error or disconnect
DBG("Nothing to read from fd %d (ret: %zd)", sock, rd);
return FALSE;
}
// add trailing zero to be on the safe side
buff[rd] = 0;
DBG("GOT: %s", buff);
ssize_t blen = strlen(buff);
if(blen != write(d->comfd, buff, blen)){
LOGWARN("write()");
WARN("write()");
}
if(buff[blen-1] == '\n') buff[blen-1] = 0;
LOGMSG("CLIENT_%d: %s", sock, buff);
return TRUE;
}
/**
* check data from fd
* @param fd - file descriptor
* @return 0 in case of timeout, 1 in case of fd have data, -1 if error
*/
static int canberead(int fd){
fd_set fds;
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 100;
FD_ZERO(&fds);
FD_SET(fd, &fds);
do{
int rc = select(fd+1, &fds, NULL, NULL, &timeout);
if(rc < 0){
if(errno != EINTR){
LOGWARN("select()");
WARN("select()");
return -1;
}
continue;
}
break;
}while(1);
if(FD_ISSET(fd, &fds)){
DBG("FD_ISSET");
return 1;
}
return 0;
}
/**
* @brief getserdata - read data (ending by '\n') from serial device
* @param D (i) - device
* @param len (o) - amount of butes read (-1 if disconnected)
* @return pointer to data buffer or NULL if none
*/
static char *getserdata(TTY_descr *D, int *len){
static char serbuf[BUFLEN], *ptr = serbuf;
static size_t blen = BUFLEN - 1;
if(!D || D->comfd < 0) return NULL;
char *nl = NULL;
do{
int s = canberead(D->comfd);
if(s == 0) break;
if(s < 0){ // interrupted?
if(len) *len = 0;
return NULL;
}
ssize_t l = read(D->comfd, ptr, blen);
if(l < 1){ // disconnected
DBG("device disconnected");
if(len) *len = -1;
return NULL;
}
ptr[l] = 0;
DBG("GOT %zd bytes: '%s'", l, ptr);
nl = strchr(ptr, '\n');
ptr += l;
blen -= l;
if(nl){
DBG("Got newline");
break;
}
}while(blen);
// recalculate newline from the beginning (what if old data stays there?)
nl = strchr(serbuf, '\n');
if(blen && !nl){
if(len) *len = 0;
return NULL;
}
// in case of overflow send buffer without trailing '\n'
int L;
if(nl) L = nl - serbuf + 1; // get line to '\n'
else L = strlen(serbuf); // get all buffer
if(len) *len = L;
memcpy(D->buf, serbuf, L); // copy all + trailing zero
D->buflen = L;
D->buf[L] = 0;
DBG("Put to buf %d bytes: '%s'", L, D->buf);
if(nl){
L = ptr - nl - 1; // symbols after newline
if(L > 0){ // there's some data after '\n' -> put it into the beginning
memmove(serbuf, nl+1, L);
blen = BUFLEN - 1 - L;
ptr = serbuf + L;
*ptr = 0;
DBG("now serbuf is '%s'", serbuf);
}else{
blen = BUFLEN - 1;
ptr = serbuf;
*ptr = 0;
}
}else{
blen = BUFLEN - 1;
ptr = serbuf;
*ptr = 0;
}
return D->buf;
}
static void server_(int sock, TTY_descr *d){
if(listen(sock, MAXCLIENTS) == -1){
WARN("listen");
LOGWARN("listen");
return;
}
int enable = 1;
if(ioctl(sock, FIONBIO, (void *)&enable) < 0){ // make socket nonblocking
LOGERR("Can't make socket nonblocking");
ERRX("ioctl()");
}
int nfd = 1; // only one socket @start
struct pollfd poll_set[MAXCLIENTS+1];
bzero(poll_set, sizeof(poll_set));
// ZERO - listening server socket
poll_set[0].fd = sock;
poll_set[0].events = POLLIN;
while(1){
poll(poll_set, nfd, 1); // max timeout - 1ms
if(poll_set[0].revents & POLLIN){ // check main for accept()
struct sockaddr_in addr;
socklen_t len = sizeof(addr);
int client = accept(sock, (struct sockaddr*)&addr, &len);
DBG("New connection");
LOGMSG("Connection, fd=%d", client);
if(nfd == MAXCLIENTS + 1){
LOGWARN("Max amount of connections, disconnect fd=%d", client);
WARNX("Limit of connections reached");
close(client);
}else{
memset(&poll_set[nfd], 0, sizeof(struct pollfd));
poll_set[nfd].fd = client;
poll_set[nfd].events = POLLIN;
++nfd;
}
}
int l = -1;
char *serdata = getserdata(d, &l);
if(l < 0){
LOGERR("Serial device disconnected");
ERRX("Serial device disconnected");
}
if(serdata){
for(int i = 1; i < nfd; ++i)
if(l != write(poll_set[i].fd, serdata, l)){
LOGWARN("write()");
WARN("write()");
}
if(serdata[l-1] == '\n') serdata[l-1] = 0;
LOGMSG("SERIAL: %s", serdata);
}
// scan connections
for(int fdidx = 1; fdidx < nfd; ++fdidx){
if((poll_set[fdidx].revents & POLLIN) == 0) continue;
int fd = poll_set[fdidx].fd;
if(!handle_socket(fd, d)){ // socket closed
DBG("Client fd=%d disconnected", fd);
LOGMSG("Client fd=%d disconnected", fd);
close(fd);
// move last FD to current position
poll_set[fdidx] = poll_set[nfd - 1];
--nfd;
}
}
}
}
// read console char - for client
static int rc(){
int rb;
struct timeval tv;
int retval;
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(STDIN_FILENO, &rfds);
tv.tv_sec = 0; tv.tv_usec = 100;
retval = select(1, &rfds, NULL, NULL, &tv);
if(!retval) rb = 0;
else {
if(FD_ISSET(STDIN_FILENO, &rfds)) rb = getchar();
else rb = 0;
}
return rb;
}
/**
* @brief mygetline - silently and non-blocking getline
* @return zero-terminated string with '\n' at end (or without in case of overflow)
*/
static char *mygetline(){
static char buf[BUFLEN+1];
static int i = 0;
while(i < BUFLEN){
char rd = rc();
if(!rd) return NULL;
if(rd == 0x7f && i){ // backspace
buf[--i] = 0;
printf("\b \b");
}else{
buf[i++] = rd;
printf("%c", rd);
}
fflush(stdout);
if(rd == '\n') break;
}
buf[i] = 0;
i = 0;
return buf;
}
static void client_(int sock){
setup_con(); // convert console mode into non-canon
int Bufsiz = BUFLEN;
char *recvBuff = MALLOC(char, Bufsiz);
while(1){
char *msg = mygetline();
if(msg){
ssize_t L = strlen(msg);
if(send(sock, msg, L, 0) != L){
WARN("send");
WARN("send");
}
if(msg[L-1] == '\n') msg[L-1] = 0;
LOGMSG("TERMINAL: %s", msg);
}
if(!waittoread(sock)) continue;
int n = read(sock, recvBuff, Bufsiz-1);
if(n == 0){
WARNX("Server disconnected");
signals(0);
}
recvBuff[n] = 0;
printf("%s", recvBuff);
if(recvBuff[n-1] == '\n') recvBuff[n-1] = 0;
LOGMSG("SERIAL: %s", recvBuff);
}
}
/**
* @brief openserialdev - open connection to serial device
* @param path (i) - path to device
* @param speed - connection speed
* @return pointer to device structure if all OK
*/
static TTY_descr *openserialdev(char *path, int speed){
TTY_descr *d = new_tty(path, speed, BUFLEN);
DBG("Device created");
if(!d || !(tty_open(d, 1))){
WARN("Can't open device %s", path);
LOGWARN("Can't open device %s", path);
return NULL;
}
DBG("device opened");
return d;
}
int start_socket(int server, char *path, TTY_descr **dev){
if(server){
if(!dev) return 1;
if(!(*dev = openserialdev(GP->devpath, GP->speed))){
LOGERR("Can't open serial device %s", GP->devpath);
ERR("Can't open serial device %s", GP->devpath);
}
}
int sock = -1;
int reuseaddr = 1;
struct sockaddr_un saddr = {0};
saddr.sun_family = AF_UNIX;
strncpy(saddr.sun_path, path, 106); // if sun_path[0] == 0 we don't create a file
if((sock = socket(AF_UNIX, SOCK_SEQPACKET, 0)) < 0){ // or SOCK_STREAM?
LOGERR("socket()");
ERR("socket()");
}
if(server){
if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(int)) == -1){
LOGWARN("setsockopt");
WARN("setsockopt");
}
if(bind(sock, &saddr, sizeof(saddr)) == -1){
close(sock);
LOGERR("bind");
ERR("bind");
}
}else{
if(connect(sock, &saddr, sizeof(saddr)) == -1){
LOGERR("connect()");
ERR("connect()");
}
}
if(server) server_(sock, *dev);
else client_(sock);
close(sock);
signals(0);
return 0;
}

31
serialsock/sersock.h Normal file
View File

@@ -0,0 +1,31 @@
/*
* This file is part of the canserver project.
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef SERSOCK_H__
#define SERSOCK_H__
#define BUFLEN (1024)
// Max amount of connections
#define MAXCLIENTS (30)
#include <usefull_macros.h>
int start_socket(int server, char *path, TTY_descr **dev);
#endif // SERSOCK_H__