posix: only get/set options once for open

This commit is contained in:
Matthias Ringwald 2017-07-25 21:22:16 +02:00
parent cde6b088be
commit 79d9f1ed77

View File

@ -237,20 +237,34 @@ static int btstack_uart_posix_set_baudrate(uint32_t baudrate){
return 0;
}
static void btstack_uart_posix_set_parity_option(struct termios * toptions, int parity){
if (parity){
// enable even parity
toptions->c_cflag |= PARENB;
} else {
// disable even parity
toptions->c_cflag &= ~PARENB;
}
}
static void btstack_uart_posix_set_flowcontrol_option(struct termios * toptions, int flowcontrol){
if (flowcontrol) {
// with flow control
toptions->c_cflag |= CRTSCTS;
} else {
// no flow control
toptions->c_cflag &= ~CRTSCTS;
}
}
static int btstack_uart_posix_set_parity(int parity){
int fd = transport_data_source.fd;
struct termios toptions;
if (tcgetattr(fd, &toptions) < 0) {
log_error("btstack_uart_posix_set_parity: Couldn't get term attributes");
return -1;
}
if (parity){
toptions.c_cflag |= PARENB; // enable even parity
} else {
toptions.c_cflag &= ~PARENB; // disable even parity
}
btstack_uart_posix_set_parity_option(&toptions, parity);
if(tcsetattr(fd, TCSANOW, &toptions) < 0) {
log_error("posix_set_parity: Couldn't set term attributes");
return -1;
@ -258,22 +272,15 @@ static int btstack_uart_posix_set_parity(int parity){
return 0;
}
static int btstack_uart_posix_set_flowcontrol(int flowcontrol){
int fd = transport_data_source.fd;
struct termios toptions;
if (tcgetattr(fd, &toptions) < 0) {
log_error("btstack_uart_posix_set_parity: Couldn't get term attributes");
return -1;
}
if (flowcontrol) {
// with flow control
toptions.c_cflag |= CRTSCTS;
} else {
// no flow control
toptions.c_cflag &= ~CRTSCTS;
}
btstack_uart_posix_set_flowcontrol_option(&toptions, flowcontrol);
if(tcsetattr(fd, TCSANOW, &toptions) < 0) {
log_error("posix_set_flowcontrol: Couldn't set term attributes");
return -1;
@ -313,6 +320,12 @@ static int btstack_uart_posix_open(void){
toptions.c_cc[VMIN] = 1;
toptions.c_cc[VTIME] = 0;
// no parity
btstack_uart_posix_set_parity_option(&toptions, 0);
// flowcontrol
btstack_uart_posix_set_flowcontrol_option(&toptions, flowcontrol);
if(tcsetattr(fd, TCSANOW, &toptions) < 0) {
log_error("posix_open: Couldn't set term attributes");
return -1;
@ -326,11 +339,6 @@ static int btstack_uart_posix_open(void){
return -1;
}
// also set flow control
if (btstack_uart_posix_set_flowcontrol(flowcontrol) < 0){
return -1;
}
// set up data_source
btstack_run_loop_set_data_source_fd(&transport_data_source, fd);
btstack_run_loop_set_data_source_handler(&transport_data_source, &hci_transport_h5_process);