From 7aaf71bd93de8eac8e43db5da97e2aee739d7b46 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Wed, 13 Apr 2016 14:33:02 +0200 Subject: [PATCH] hci_transport_hX_posix: extract open into btstack_uart_posix --- platform/posix/btstack_uart_posix.c | 50 ++++++++++++++++++ platform/posix/btstack_uart_posix.h | 9 ++++ platform/posix/hci_transport_h4_posix.c | 48 ++--------------- platform/posix/hci_transport_h5_posix.c | 70 ++++++++----------------- 4 files changed, 84 insertions(+), 93 deletions(-) diff --git a/platform/posix/btstack_uart_posix.c b/platform/posix/btstack_uart_posix.c index f927223f2..1cfd78d6b 100644 --- a/platform/posix/btstack_uart_posix.c +++ b/platform/posix/btstack_uart_posix.c @@ -52,6 +52,56 @@ // #include // #include +int btstack_uart_posix_open(const char * device_name, int flowcontrol, uint32_t baudrate){ + + struct termios toptions; + int flags = O_RDWR | O_NOCTTY | O_NONBLOCK; + int fd = open(device_name, flags); + if (fd == -1) { + perror("init_serialport: Unable to open port "); + perror(device_name); + return -1; + } + + if (tcgetattr(fd, &toptions) < 0) { + perror("init_serialport: Couldn't get term attributes"); + return -1; + } + + cfmakeraw(&toptions); // make raw + + // 8N1 + toptions.c_cflag &= ~CSTOPB; + toptions.c_cflag |= CS8; + + if (flowcontrol) { + // with flow control + toptions.c_cflag |= CRTSCTS; + } else { + // no flow control + toptions.c_cflag &= ~CRTSCTS; + } + + toptions.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines + toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl + + // see: http://unixwiz.net/techtips/termios-vmin-vtime.html + toptions.c_cc[VMIN] = 1; + toptions.c_cc[VTIME] = 0; + + if(tcsetattr(fd, TCSANOW, &toptions) < 0) { + perror("init_serialport: Couldn't set term attributes"); + return -1; + } + + // also set baudrate + if (btstack_uart_posix_set_baudrate(fd, baudrate) < 0){ + return -1; + } + + return fd; +} + int btstack_uart_posix_set_baudrate(int fd, uint32_t baudrate){ log_info("h4_set_baudrate %u", baudrate); diff --git a/platform/posix/btstack_uart_posix.h b/platform/posix/btstack_uart_posix.h index 7cfd854fa..3ad1441ca 100644 --- a/platform/posix/btstack_uart_posix.h +++ b/platform/posix/btstack_uart_posix.h @@ -48,6 +48,15 @@ #include +/** + * @brief Open serial port + * @param device_name + * @param flowcontrol enabled + * @param baudrate + * @returns fd if successful + */ +int btstack_uart_posix_open(const char * device_name, int flowcontrol, uint32_t baudrate); + /** * @brief Set baudrate * @param fd diff --git a/platform/posix/hci_transport_h4_posix.c b/platform/posix/hci_transport_h4_posix.c index 05efd6108..7dbece8e3 100644 --- a/platform/posix/hci_transport_h4_posix.c +++ b/platform/posix/hci_transport_h4_posix.c @@ -127,47 +127,12 @@ static void h4_init(const void * transport_config){ static int h4_open(void){ - struct termios toptions; - int flags = O_RDWR | O_NOCTTY | O_NONBLOCK; - int fd = open(hci_transport_config_uart->device_name, flags); - if (fd == -1) { - perror("init_serialport: Unable to open port "); - perror(hci_transport_config_uart->device_name); - return -1; + int fd = btstack_uart_posix_open(hci_transport_config_uart->device_name, hci_transport_config_uart->flowcontrol, hci_transport_config_uart->baudrate_init); + if (fd < 0){ + return fd; } - - if (tcgetattr(fd, &toptions) < 0) { - perror("init_serialport: Couldn't get term attributes"); - return -1; - } - - cfmakeraw(&toptions); // make raw - // 8N1 - toptions.c_cflag &= ~CSTOPB; - toptions.c_cflag |= CS8; - - if (hci_transport_config_uart->flowcontrol) { - // with flow control - toptions.c_cflag |= CRTSCTS; - } else { - // no flow control - toptions.c_cflag &= ~CRTSCTS; - } - - toptions.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines - toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl - - // see: http://unixwiz.net/techtips/termios-vmin-vtime.html - toptions.c_cc[VMIN] = 1; - toptions.c_cc[VTIME] = 0; - - if( tcsetattr(fd, TCSANOW, &toptions) < 0) { - perror("init_serialport: Couldn't set term attributes"); - return -1; - } - - // set up data_source + // set up data_source hci_transport_h4->ds = (btstack_data_source_t*) malloc(sizeof(btstack_data_source_t)); if (!hci_transport_h4->ds) return -1; hci_transport_h4->uart_fd = fd; @@ -175,11 +140,6 @@ static int h4_open(void){ btstack_run_loop_set_data_source_handler(hci_transport_h4->ds, &h4_process); btstack_run_loop_enable_data_source_callbacks(hci_transport_h4->ds, DATA_SOURCE_CALLBACK_READ); btstack_run_loop_add_data_source(hci_transport_h4->ds); - - // also set baudrate - if (h4_set_baudrate(hci_transport_config_uart->baudrate_init) < 0){ - return -1; - } // init state machine bytes_to_read = 1; diff --git a/platform/posix/hci_transport_h5_posix.c b/platform/posix/hci_transport_h5_posix.c index 2af59ebad..80ea4691a 100644 --- a/platform/posix/hci_transport_h5_posix.c +++ b/platform/posix/hci_transport_h5_posix.c @@ -540,60 +540,17 @@ static int hci_transport_h5_set_baudrate(uint32_t baudrate){ static int hci_transport_h5_open(void){ - struct termios toptions; - int flags = O_RDWR | O_NOCTTY | O_NONBLOCK; - int fd = open(hci_transport_config_uart->device_name, flags); - if (fd == -1) { - perror("h5_open: Unable to open port "); - perror(hci_transport_config_uart->device_name); - return -1; + int fd = btstack_uart_posix_open(hci_transport_config_uart->device_name, hci_transport_config_uart->flowcontrol, hci_transport_config_uart->baudrate_init); + if (fd < 0){ + return fd; } - - if (tcgetattr(fd, &toptions) < 0) { - perror("h5_open: Couldn't get term attributes"); - return -1; - } - - cfmakeraw(&toptions); // make raw - // 8N1 - toptions.c_cflag &= ~CSTOPB; - toptions.c_cflag |= CS8; - - if (hci_transport_config_uart->flowcontrol) { - // with flow control - toptions.c_cflag |= CRTSCTS; - } else { - // no flow control - toptions.c_cflag &= ~CRTSCTS; - } - - toptions.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines - // - // toptions.c_cflag |= PARENB; // enable even parity - // - toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl - - // see: http://unixwiz.net/techtips/termios-vmin-vtime.html - toptions.c_cc[VMIN] = 1; - toptions.c_cc[VTIME] = 0; - - if( tcsetattr(fd, TCSANOW, &toptions) < 0) { - perror("init_serialport: Couldn't set term attributes"); - return -1; - } - // set up data_source btstack_run_loop_set_data_source_fd(&hci_transport_h5_data_source, fd); btstack_run_loop_set_data_source_handler(&hci_transport_h5_data_source, &hci_transport_h5_process); btstack_run_loop_enable_data_source_callbacks(&hci_transport_h5_data_source, DATA_SOURCE_CALLBACK_READ); btstack_run_loop_add_data_source(&hci_transport_h5_data_source); - // also set baudrate - if (hci_transport_h5_set_baudrate(hci_transport_config_uart->baudrate_init) < 0){ - return -1; - } - // init slip parser state machine hci_transport_slip_init(); @@ -627,9 +584,7 @@ static int hci_transport_h5_close(void){ return 0; } -static void hci_transport_h5_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type) { - if (hci_transport_h5_data_source.fd < 0) return; - +static void hci_transport_h5_process_read(btstack_data_source_t * ds){ // process data byte by byte uint8_t data; while (1) { @@ -645,6 +600,23 @@ static void hci_transport_h5_process(btstack_data_source_t *ds, btstack_data_sou }; } +static void hci_transport_h5_process_write(btstack_data_source_t *ds){ + // not implemented yet +} + +static void hci_transport_h5_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type) { + if (hci_transport_h5_data_source.fd < 0) return; + switch (callback_type){ + case DATA_SOURCE_CALLBACK_READ: + hci_transport_h5_process_read(ds); + break; + case DATA_SOURCE_CALLBACK_WRITE: + hci_transport_h5_process_write(ds); + default: + break; + } +} + // get h5 singleton const hci_transport_t * hci_transport_h5_instance(void) { if (hci_transport_h5 == NULL) {