fixed some bugs, switching to actual usefull_macros library

This commit is contained in:
2026-05-19 17:37:45 +03:00
parent 9c2b6aeebf
commit a6978dd84a
12 changed files with 465 additions and 418 deletions

View File

@@ -16,6 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#if 0
#include <arpa/inet.h>
#include <fcntl.h>
#include <netdb.h>
@@ -27,297 +28,128 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/un.h> // unix socket
#endif
#include <string.h>
#include <usefull_macros.h>
#include "dbg.h"
#include "string_functions.h"
#include "ttysocket.h"
static int sec = 0, usec = 100; // timeout
static FILE *dupfile = NULL; // file for output
static chardevice *device = NULL; // current opened device
// TODO: if unix socket name starts with \0 translate it as \\0 to d->name!
// set Read_tty timeout in milliseconds
void settimeout(int tmout){
sec = 0;
if(tmout > 999){
sec = tmout / 1000;
tmout -= sec * 1000;
}
usec = tmout * 1000L;
}
/**
* wait for answer from socket
* @param sock - socket fd
* @return 0 in case of timeout, 1 in case of socket ready, -1 if error
*/
static int waittoread(int fd){
fd_set fds;
struct timeval timeout;
timeout.tv_sec = sec;
timeout.tv_usec = usec;
FD_ZERO(&fds);
FD_SET(fd, &fds);
do{
int rc = select(fd+1, &fds, NULL, NULL, &timeout);
if(rc < 0){
if(errno != EINTR){
WARN("select()");
return -1;
}
continue;
}
break;
}while(1);
if(FD_ISSET(fd, &fds)){
//DBG("FD_ISSET");
return 1;
}
return 0;
}
// get data drom TTY
static uint8_t *getttydata(int *len){
if(!device || !device->dev) return NULL;
sl_tty_t *D = device->dev;
if(D->comfd < 0) return NULL;
int L = 0;
int length = D->bufsz - 1; // -1 for terminating zero
uint8_t *ptr = (uint8_t*)D->buf;
int s = 0;
do{
if(!(s = waittoread(D->comfd))) break;
if(s < 0){
if(len) *len = 0;
return NULL;
}
int l = read(D->comfd, ptr, length);
if(l < 1){ // disconnected
if(len) *len = -1;
return NULL;
}
ptr += l; L += l;
length -= l;
}while(length);
D->buflen = L;
D->buf[L] = 0; // for text buffers
if(len) *len = L;
if(!L) return NULL;
DBG("buffer len: %zd, content: =%s=", D->buflen, D->buf);
return (uint8_t*)D->buf;
}
static uint8_t *getsockdata(int *len){
if(!device || !device->dev) return NULL;
sl_tty_t *D = device->dev;
if(D->comfd < 0) return NULL;
uint8_t *ptr = NULL;
int n = waittoread(D->comfd);
if(n == 1){
n = read(D->comfd, D->buf, D->bufsz-1);
if(n > 0){
ptr = (uint8_t*)D->buf;
ptr[n] = 0;
D->buflen = n;
DBG("got %d: ..%s..", n, ptr);
}else{
DBG("Got nothing");
n = -1;
}
}
if(len) *len = n;
return ptr;
}
/**
* @brief ReadData - get data from serial device or socket
* @param d - device
* @param len (o) - length of data read (-1 if device disconnected)
* @return NULL or string
* @param buf - buffer
* @param len - buffer length
* @return amount of read bytes or -1 in case of error / disconnection
*/
uint8_t *ReadData(int *len){
if(!device || !device->dev) return NULL;
if(len) *len = -1;
uint8_t *r = NULL;
switch(device->type){
case DEV_TTY:
r = getttydata(len);
break;
case DEV_NETSOCKET:
case DEV_UNIXSOCKET:
r = getsockdata(len);
break;
default:
break;
ssize_t ReadData(uint8_t *buf, size_t len){
if(!device || device->fd < 0){
WARNX("ReadData(): No connection");
return -1;
}
if(r && dupfile){
fwrite("< ", 1, 2, dupfile);
fwrite(r, 1, *len, dupfile);
if(!buf || len == 0){
WARNX("ReadData(): Bad receive buffer");
return 0;
}
return r;
ssize_t l = -1;
static int errctr = 0;
if(0 == pthread_mutex_lock(&device->mutex)){
int s = sl_canread(device->fd);
if(s < 1){
pthread_mutex_unlock(&device->mutex);
return (ssize_t)s;
}
l = read(device->fd, buf, len);
if(l > 0){
DBG("receive: %zd", l);
if(dupfile){
fwrite("< ", 1, 2, dupfile);
fwrite(buf, 1, l, dupfile);
}
errctr = 0;
}else{
DBG("Disconnected? Got %zd bytes", l);
if(++errctr > 3){
l = -1;
errctr = 0;
}
}
pthread_mutex_unlock(&device->mutex);
}else WARN("ReadData(): pthread_mutex_lock()");
return l;
}
/**
* @brief SendData - send data to tty or socket
* @param d - device
* @param data - buffer with data
* @return 0 if error or empty string, -1 if disconnected
* @return 0 if error or empty string, -1 if disconnected or error
*/
int SendData(const uint8_t *data, size_t len){
if(!device) return -1;
if(!data || len == 0) return 0;
int ret = 0;
ssize_t SendData(const uint8_t *data, size_t len){
if(!device || device->fd < 0){
WARNX("SendData(): No connection");
return -1;
}
if(!data || len == 0){
WARNX("SendData(): Bad data to send");
return 0;
}
ssize_t ret = -1;
DBG("Send %d bytes", len);
DBG("lock");
if(0 == pthread_mutex_lock(&device->mutex)){
switch(device->type){
case DEV_TTY:
if(sl_tty_write(device->dev->comfd, (const char*)data, len)) ret = 0;
else ret = len;
break;
case DEV_NETSOCKET:
case DEV_UNIXSOCKET:
if(len != (size_t)send(device->dev->comfd, data, len, MSG_NOSIGNAL)) ret = 0;
else ret = len;
break;
default:
data = NULL;
break;
}
DBG("write");
ret = write(device->fd, data, len);
if(data && dupfile){
fwrite("> ", 1, 2, dupfile);
fwrite(data, 1, len, dupfile);
}
DBG("unlock");
pthread_mutex_unlock(&device->mutex);
}else ret = -1;
DBG("ret=%d", ret);
}else WARN("SendData(): pthread_mutex_lock()");
DBG("send: %zd", ret);
return ret;
}
static const int socktypes[] = {SOCK_STREAM, SOCK_RAW, SOCK_RDM, SOCK_SEQPACKET, SOCK_DCCP, SOCK_PACKET, SOCK_DGRAM, 0};
static sl_tty_t* opensocket(){
if(!device) return FALSE;
sl_tty_t *descr = MALLOC(sl_tty_t, 1); // only for `buf` and bufsz/buflen
descr->buf = MALLOC(char, BUFSIZ);
descr->bufsz = BUFSIZ;
// now try to open a socket
descr->comfd = -1;
struct hostent *host;
struct sockaddr_in addr = {0};
struct sockaddr_un saddr = {0};
struct sockaddr *sa = NULL;
socklen_t addrlen = 0;
int domain = -1;
if(device->type == DEV_NETSOCKET){
DBG("NETSOCK to %s", device->name);
sa = (struct sockaddr*) &addr;
addrlen = sizeof(addr);
if((host = gethostbyname(device->name)) == NULL ){
WARN("gethostbyname()");
FREE(descr->buf);
FREE(descr);
return NULL;
}
struct in_addr *ia = (struct in_addr*)host->h_addr_list[0];
DBG("addr: %s", inet_ntoa(*ia));
addr.sin_family = AF_INET;
int p = atoi(device->port); DBG("PORT: %s - %d", device->port, p);
addr.sin_port = htons(p);
//addr.sin_addr.s_addr = *(long*)(host->h_addr);
addr.sin_addr.s_addr = ia->s_addr;
domain = AF_INET;
}else{
DBG("UNSOCK");
sa = (struct sockaddr*) &saddr;
addrlen = sizeof(saddr);
saddr.sun_family = AF_UNIX;
if(*(device->name) == 0){ // if sun_path[0] == 0 then don't create a file
DBG("convert name");
saddr.sun_path[0] = 0;
strncpy(saddr.sun_path+1, device->name+1, 105);
}
else if(strncmp("\\0", device->name, 2) == 0){
DBG("convert name");
saddr.sun_path[0] = 0;
strncpy(saddr.sun_path+1, device->name+2, 105);
}else strncpy(saddr.sun_path, device->name, 106);
domain = AF_UNIX;
}
const int *type = socktypes;
while(*type){
DBG("type = %d", *type);
if((descr->comfd = socket(domain, *type, 0)) > -1){
if(connect(descr->comfd, sa, addrlen) < 0){
DBG("CANT connect");
close(descr->comfd);
}else break;
}
++type;
}
if(descr->comfd < 0){
DBG("NO types");
WARNX("No types can be choosen");
FREE(descr->buf);
FREE(descr);
return NULL;
}
return descr;
}
static sl_tty_t* opentty(){
if(!device->name){
/// ïÔÓÕÔÓÔ×ÕÅÔ ÉÍÑ ÐÏÒÔÁ
WARNX(_("Port name is missing"));
return NULL;
}
sl_tty_t *descr = sl_tty_new(device->name, device->speed, 4096);
if(!descr){
WARN("Can't init %s", device->name);
return NULL;
}
descr->format = device->port;
descr = sl_tty_open(descr, device->exclusive);
return descr;
}
/**
* @brief opendev - open TTY or socket output device
* @param d - device type
* @param path - path to dump file or NULL
* @return FALSE if failed
*/
int opendev(chardevice *d, char *path){
if(!d) return FALSE;
DBG("Try to open device");
device = MALLOC(chardevice, 1);
memcpy(device, d, sizeof(chardevice));
device->name = strdup(d->name);
if(d->port) device->port = strdup(d->port);
device = d;//MALLOC(chardevice, 1);
//memcpy(device, d, sizeof(chardevice));
//device->node = strdup(d->node);
//if(d->serformat) device->serformat = strdup(d->serformat);
DBG("devtype=%d", device->type);
switch(device->type){
case DEV_TTY:
DBG("Serial");
device->dev = opentty();
if(!device->dev){
WARN("Can't open device %s", device->name);
DBG("CANT OPEN");
return FALSE;
}
device->fd = sl_tty_fdescr(device->node, device->serformat, device->speed, device->exclusive);
break;
case DEV_NETSOCKET:
DBG("INET socket");
device->fd = sl_sock_open(SOCKT_NET, device->node, 0, 0);
break;
case DEV_UNIXSOCKET:
DBG("Socket");
device->dev = opensocket();
if(!device->dev){
WARNX("Can't open socket");
DBG("CANT OPEN");
return FALSE;
}
DBG("UNIX-socket");
device->fd = sl_sock_open(SOCKT_UNIX, device->node, 0, 0);
break;
default:
return FALSE;
}
if(device->fd < 0){
WARN("Can't open %s", device->node);
DBG("CANT OPEN");
return FALSE;
}
if(path){ // open logging file
dupfile = fopen(path, "a");
if(!dupfile){
@@ -327,42 +159,31 @@ int opendev(chardevice *d, char *path){
}
}
changeeol(device->eol); // allow string functions to know EOL
memcpy(d, device, sizeof(chardevice));
//d->fd = device->fd;
return TRUE;
}
void closedev(){
FNAME();
if(!device) return;
pthread_mutex_unlock(&device->mutex);
// avoid dead-locks
pthread_mutex_trylock(&device->mutex);
int fd = device->fd;
device->fd = -1;
usleep(1000); // wait a little for threads
close(fd);
DBG("Device closed");
pthread_mutex_unlock(&device->mutex);
if(dupfile){
DBG("Dupfile closed");
fclose(dupfile);
dupfile = NULL;
}
switch(device->type){
case DEV_TTY:
if(device->dev){
sl_tty_t *t = device->dev;
ioctl(t->comfd, TCSETS2, &t->oldtty); // return TTY to previous state
close(t->comfd);
}
break;
case DEV_NETSOCKET:
if(device->dev){
close(device->dev->comfd);
FREE(device->dev);
}
break;
default:
return;
}
if(device->dev){
FREE(device->dev->format);
FREE(device->dev->portname);
FREE(device->dev->buf);
FREE(device->dev);
}
FREE(device->name);
DBG("free node");
FREE(device->node);
DBG("free serformat");
FREE(device->serformat);
DBG("free device");
FREE(device);
DBG("Device closed");
}