1
0
mirror of https://github.com/UzixLS/picocom.git synced 2025-07-19 07:21:18 +03:00

term_get_xxx functions and CMSPAR support.

Added functions to read and decode settings (baudrate, databits, parity,
flow-control) from the "currtermios" structure of a managed port.

Added support for "mark" and "space" parity (CMSPAR bit).
This commit is contained in:
Nick Patavalis
2015-08-14 20:14:54 +03:00
parent 8135bbdd5f
commit e936f5bfe2
2 changed files with 210 additions and 9 deletions

152
term.c
View File

@ -68,6 +68,7 @@ static const char * const term_err_str[] = {
[TERM_EBAUD] = "Invalid baud rate",
[TERM_ESETOSPEED] = "Cannot set the output speed",
[TERM_ESETISPEED] = "Cannot set the input speed",
[TERM_EGETSPEED] = "Cannot decode speed",
[TERM_EPARITY] = "Invalid parity mode",
[TERM_EDATABITS] = "Invalid number of databits",
[TERM_EFLOW] = "Invalid flowcontrol mode",
@ -248,6 +249,20 @@ Bcode(int speed)
return code;
}
static int
Bspeed(speed_t code)
{
int speed = -1, i;
for (i = 0; i < BAUD_TABLE_SZ; i++) {
if ( baud_table[i].code == code ) {
speed = baud_table[i].speed;
break;
}
}
return speed;
}
/**************************************************************************/
static int
@ -735,6 +750,34 @@ term_set_baudrate (int fd, int baudrate)
return rval;
}
int
term_get_baudrate (int fd, int *ispeed)
{
speed_t code;
int i, ospeed;
do { /* dummy */
i = term_find(fd);
if ( i < 0 ) {
ospeed = -1;
break;
}
if ( ispeed ) {
code = cfgetispeed(&term.currtermios[i]);
*ispeed = Bspeed(code);
}
code = cfgetospeed(&term.currtermios[i]);
ospeed = Bspeed(code);
if ( ospeed < 0 )
term_errno = TERM_EGETSPEED;
} while (0);
return ospeed;
}
/***************************************************************************/
int
@ -757,14 +800,22 @@ term_set_parity (int fd, enum parity_e parity)
switch (parity) {
case P_EVEN:
tiop->c_cflag &= ~PARODD;
tiop->c_cflag &= ~(PARODD | CMSPAR);
tiop->c_cflag |= PARENB;
break;
case P_ODD:
tiop->c_cflag &= ~CMSPAR;
tiop->c_cflag |= PARENB | PARODD;
break;
case P_MARK:
tiop->c_cflag |= PARENB | PARODD | CMSPAR;
break;
case P_SPACE:
tiop->c_cflag &= ~PARODD;
tiop->c_cflag |= PARENB | CMSPAR;
break;
case P_NONE:
tiop->c_cflag &= ~(PARENB | PARODD);
tiop->c_cflag &= ~(PARENB | PARODD | CMSPAR);
break;
default:
term_errno = TERM_EPARITY;
@ -778,6 +829,34 @@ term_set_parity (int fd, enum parity_e parity)
return rval;
}
enum parity_e
term_get_parity (int fd)
{
tcflag_t flg;
int i, parity;
do { /* dummy */
i = term_find(fd);
if ( i < 0 ) {
parity = -1;
break;
}
flg = term.currtermios[i].c_cflag;
if ( ! (flg & PARENB) ) {
parity = P_NONE;
} else if ( flg & CMSPAR ) {
parity = (flg & PARODD) ? P_MARK : P_SPACE;
} else {
parity = (flg & PARODD) ? P_ODD : P_EVEN;
}
} while (0);
return parity;
}
/***************************************************************************/
int
@ -823,6 +902,42 @@ term_set_databits (int fd, int databits)
return rval;
}
int
term_get_databits (int fd)
{
tcflag_t flg;
int i, bits;
do { /* dummy */
i = term_find(fd);
if ( i < 0 ) {
bits = -1;
break;
}
flg = term.currtermios[i].c_cflag & CSIZE;
switch (flg) {
case CS5:
bits = 5;
break;
case CS6:
bits = 6;
break;
case CS7:
bits = 7;
break;
case CS8:
default:
bits = 8;
break;
}
} while (0);
return bits;
}
/***************************************************************************/
int
@ -868,6 +983,39 @@ term_set_flowcntrl (int fd, enum flowcntrl_e flowcntl)
return rval;
}
enum flowcntrl_e
term_get_flowcntrl (int fd)
{
int i, flow;
int rtscts, xoff, xon;
do { /* dummy */
i = term_find(fd);
if ( i < 0 ) {
flow = -1;
break;
}
rtscts = (term.currtermios[i].c_cflag & CRTSCTS) ? 1 : 0;
xoff = (term.currtermios[i].c_iflag & IXOFF) ? 1 : 0;
xon = (term.currtermios[i].c_iflag & (IXON | IXANY)) ? 1 : 0;
if ( rtscts && ! xoff && ! xon ) {
flow = FC_RTSCTS;
} else if ( ! rtscts && xoff && xon ) {
flow = FC_XONXOFF;
} else if ( ! rtscts && ! xoff && ! xon ) {
flow = FC_NONE;
} else {
flow = FC_OTHER;
}
} while (0);
return flow;
}
/***************************************************************************/
int

67
term.h
View File

@ -52,6 +52,10 @@
* F term_set_hupcl - enable or disable hupcl in "nexttermios"
* F term_set_local - set "nexttermios" to local or non-local mode
* F term_set - set all params of "nexttermios" in a single stroke
* F term_get_baudrate - return the baudrate set in "currtermios"
* F term_get_parity - return the parity setting in "currtermios"
* F term_get_databits - return the data-bits setting in "currtermios"
* F term_get_flowcntrl - return the flow-control setting in "currtermios"
* F term_pulse_dtr - pulse the DTR line a device
* F term_lower_dtr - lower the DTR line of a device
* F term_raise_dtr - raise the DTR line of a device
@ -126,6 +130,7 @@ enum term_errno_e {
TERM_EBAUD,
TERM_ESETOSPEED,
TERM_ESETISPEED,
TERM_EGETSPEED,
TERM_EPARITY,
TERM_EDATABITS,
TERM_EFLOW,
@ -139,14 +144,18 @@ enum term_errno_e {
*
* Parity modes supported by the library:
*
* P_NONE - no patiry
* P_EVEN - even parity
* P_ODD - odd parity
* P_NONE - no patiry
* P_EVEN - even parity
* P_ODD - odd parity
* P_MARK - mark parity (parity bit always 1)
* P_SPACE - space parity (parity bit always 0)
*/
enum parity_e {
P_NONE,
P_NONE = 0,
P_EVEN,
P_ODD
P_ODD,
P_MARK,
P_SPACE
};
/*
@ -160,9 +169,10 @@ enum parity_e {
* FC_XONXOFF - xon/xoff flow control.
*/
enum flowcntrl_e {
FC_NONE,
FC_NONE = 0,
FC_RTSCTS,
FC_XONXOFF
FC_XONXOFF,
FC_OTHER
};
/***************************************************************************/
@ -475,6 +485,49 @@ int term_set (int fd,
enum parity_e parity, int bits, enum flowcntrl_e fc,
int local, int hupcl);
/* F term_get_baudrate
*
* Reads and decodes the current baudrate settings in the
* "currtermios" structure of the managed filedes "fd".
*
* Returns the decoded output baudrate (as bits-per-second), or -1 if
* the output baudrate cannot be decoded, or if "fd" does not
* correspond to a managed filedes. If "ispeed" is not NULL, it writes
* the decoded input baudrate to the integer pointed-to by "ispeed";
* if the input baudrate cannot be decoded in writes -1 instead.
*/
int term_get_baudrate (int fd, int *ispeed);
/* F term_get_parity
*
* Reads and decodes the current parity settings in the
* "currtermios" structure of the managed filedes "fd".
*
* Returns one of the "enum parity_e" members, or -1 if "fd" does not
* correspond to a managed filedes.
*/
enum parity_e term_get_parity (int fd);
/* F term_get_databits
*
* Reads and decodes the current databits settings in the
* "currtermios" structure of the managed filedes "fd".
*
* Returns the number of databits (5..8), or -1 if "fd" does not
* correspond to a managed filedes.
*/
int term_get_databits (int fd);
/* F term_get_flowcntrl
*
* Reads and decodes the current flow-control settings in the
* "currtermios" structure of the managed filedes "fd".
*
* Returns one of the "enum flowcntrl_e" members, or -1 if "fd" does
* not correspond to a managed filedes.
*/
enum flowcntrl_e term_get_flowcntrl (int fd);
/* F term_pulse_dtr
*
* Pulses the DTR line of the device associated with the managed