version 0.2.1

This commit is contained in:
2024-11-15 11:42:29 +03:00
parent 511a83e506
commit e77fddc3b8
22 changed files with 1337 additions and 495 deletions

50
term.c
View File

@@ -1,11 +1,10 @@
/*
* client.c - simple terminal client
* This file is part of the Snippets project.
* Copyright 2013 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* Copyright 2013 Edward V. Emelianoff <eddy@sao.ru>
*
* This program is free software; you can redistribute it and/or modify
* 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 2 of the License, or
* 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,
@@ -14,10 +13,9 @@
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <unistd.h> // tcsetattr, close, read, write
#include <sys/ioctl.h> // ioctl
#include <stdio.h> // printf, getchar, fopen, perror
@@ -39,7 +37,7 @@ typedef struct {
tcflag_t bspeed; // baudrate from termios.h
} spdtbl;
static int tty_init(TTY_descr *descr);
static int tty_init(sl_tty_t *descr);
static spdtbl speeds[] = {
{50, B50},
@@ -76,11 +74,11 @@ static spdtbl speeds[] = {
};
/**
* @brief conv_spd - test if `speed` is in .speed of `speeds` array
* @brief sl_tty_convspd - test if `speed` is in .speed of `speeds` array
* @param speed - integer speed (bps)
* @return 0 if error, Bxxx if all OK
*/
tcflag_t conv_spd(int speed){
tcflag_t sl_tty_convspd(int speed){
spdtbl *spd = speeds;
int curspeed = 0;
do{
@@ -98,7 +96,7 @@ tcflag_t conv_spd(int speed){
* @param descr (io) - port descriptor
* @return 0 if all OK or error code
*/
static int tty_init(TTY_descr *descr){
static int tty_init(sl_tty_t *descr){
// |O_NONBLOCK ?
if ((descr->comfd = open(descr->portname, O_RDWR|O_NOCTTY)) < 0){
/// îÅ ÍÏÇÕ ÉÓÐÏÌØÚÏ×ÁÔØ ÐÏÒÔ %s
@@ -134,9 +132,9 @@ static int tty_init(TTY_descr *descr){
/**
* @brief restore_tty - restore opened TTY to previous state and close it
*/
void close_tty(TTY_descr **descr){
void sl_tty_close(sl_tty_t **descr){
if(descr == NULL || *descr == NULL) return;
TTY_descr *d = *descr;
sl_tty_t *d = *descr;
if(d->comfd){
ioctl(d->comfd, TCSANOW, &d->oldtty); // return TTY to previous state
close(d->comfd);
@@ -147,16 +145,16 @@ void close_tty(TTY_descr **descr){
}
/**
* @brief new_tty - create new TTY structure with partially filled fields
* @brief sl_tty_new - create new TTY structure with partially filled fields
* @param comdev - TTY device filename
* @param speed - speed (number)
* @param bufsz - size of buffer for input data (or 0 if opened only to write)
* @return pointer to TTY structure if all OK
*/
TTY_descr *new_tty(char *comdev, int speed, size_t bufsz){
tcflag_t spd = conv_spd(speed);
sl_tty_t *sl_tty_new(char *comdev, int speed, size_t bufsz){
tcflag_t spd = sl_tty_convspd(speed);
if(!spd) return NULL;
TTY_descr *descr = MALLOC(TTY_descr, 1);
sl_tty_t *descr = MALLOC(sl_tty_t, 1);
descr->portname = strdup(comdev);
descr->baudrate = spd;
descr->speed = speed;
@@ -176,12 +174,12 @@ TTY_descr *new_tty(char *comdev, int speed, size_t bufsz){
}
/**
* @brief tty_open - init & open tty device
* @brief sl_tty_open - init & open tty device
* @param d - already filled structure (with new_tty or by hands)
* @param exclusive - == 1 to make exclusive open
* @return pointer to TTY structure if all OK
*/
TTY_descr *tty_open(TTY_descr *d, int exclusive){
sl_tty_t *sl_tty_open(sl_tty_t *d, int exclusive){
if(!d || !d->portname || !d->baudrate) return NULL;
if(exclusive) d->exclusive = TRUE;
else d->exclusive = FALSE;
@@ -191,11 +189,11 @@ TTY_descr *tty_open(TTY_descr *d, int exclusive){
static struct timeval tvdefault = {.tv_sec = 0, .tv_usec = 5000};
/**
* @brief tty_timeout - set timeout for select() on reading
* @brief sl_tty_tmout - set timeout for select() on reading
* @param usec - microseconds of timeout
* @return -1 if usec < 0, 0 if all OK
*/
int tty_timeout(double usec){
int sl_tty_tmout(double usec){
if(usec < 0.) return -1;
tvdefault.tv_sec = 0;
if(usec > 999999){
@@ -207,12 +205,12 @@ int tty_timeout(double usec){
}
/**
* @brief read_tty - read data from TTY with 10ms timeout
* @brief sl_tty_read - read data from TTY with 10ms timeout
* @param buff (o) - buffer for data read
* @param length - buffer len
* @return amount of bytes read or -1 if disconnected
*/
int read_tty(TTY_descr *d){
int sl_tty_read(sl_tty_t *d){
if(!d || d->comfd < 0) return 0;
size_t L = 0;
ssize_t l;
@@ -246,12 +244,12 @@ int read_tty(TTY_descr *d){
}
/**
* @brief write_tty - write data to serial port
* @brief sl_tty_write - write data to serial port
* @param buff (i) - data to write
* @param length - its length
* @return 0 if all OK
*/
int write_tty(int comfd, const char *buff, size_t length){
int sl_tty_write(int comfd, const char *buff, size_t length){
ssize_t L = write(comfd, buff, length);
if((size_t)L != length){
/// "ïÛÉÂËÁ ÚÁÐÉÓÉ!"