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