hci_transport_hX_posix: extract set baudrate into btstack_uart_posix

This commit is contained in:
Matthias Ringwald 2016-04-13 13:58:15 +02:00
parent 490a6e278f
commit c6a9a3658c
5 changed files with 183 additions and 93 deletions

View File

@ -0,0 +1,111 @@
/*
* Copyright (C) 2016 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
/*
* btstack_uart_posix.c
*
* Common code to access serial port via POSIX interface
* Used by hci_transport_h4_posix.c and hci_transport_h5.posix
*
*/
#include "btstack_uart_posix.h"
#include "btstack_debug.h"
#include <termios.h> /* POSIX terminal control definitions */
#include <fcntl.h> /* File control definitions */
// #include <unistd.h> /* UNIX standard function definitions */
// #include <stdio.h>
// #include <string.h>
int btstack_uart_posix_set_baudrate(int fd, uint32_t baudrate){
log_info("h4_set_baudrate %u", baudrate);
struct termios toptions;
if (tcgetattr(fd, &toptions) < 0) {
perror("init_serialport: Couldn't get term attributes");
return -1;
}
speed_t brate = baudrate; // let you override switch below if needed
switch(baudrate) {
case 57600: brate=B57600; break;
case 115200: brate=B115200; break;
#ifdef B230400
case 230400: brate=B230400; break;
#endif
#ifdef B460800
case 460800: brate=B460800; break;
#endif
#ifdef B921600
case 921600: brate=B921600; break;
#endif
// Hacks to switch to 2/3 mbps on FTDI FT232 chipsets
// requires special config in Info.plist or Registry
case 2000000:
#if defined(HAVE_POSIX_B300_MAPPED_TO_2000000)
log_info("hci_transport_posix: using B300 for 2 mbps");
brate=B300;
#elif defined(HAVE_POSIX_B1200_MAPPED_TO_2000000)
log_info("hci_transport_posix: using B1200 for 2 mbps");
brate=B1200;
#endif
break;
case 3000000:
#if defined(HAVE_POSIX_B600_MAPPED_TO_3000000)
log_info("hci_transport_posix: using B600 for 3 mbps");
brate=B600;
#elif defined(HAVE_POSIX_B2400_MAPPED_TO_3000000)
log_info("hci_transport_posix: using B2400 for 3 mbps");
brate=B2400;
#endif
break;
default:
break;
}
cfsetospeed(&toptions, brate);
cfsetispeed(&toptions, brate);
if( tcsetattr(fd, TCSANOW, &toptions) < 0) {
perror("init_serialport: Couldn't set term attributes");
return -1;
}
return 0;
}

View File

@ -0,0 +1,59 @@
/*
* Copyright (C) 2016 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
/*
* btstack_uart_posix.h
*
* Common code to access serial port via POSIX interface
* Used by hci_transport_h4_posix.c and hci_transport_h5.posix
*
*/
#ifndef __BTSTACK_UART_POSIX_H
#define __BTSTACK_UART_POSIX_H
#include <stdint.h>
/**
* @brief Set baudrate
* @param fd
* @param baudrate
* @returns 0 if successful
*/
int btstack_uart_posix_set_baudrate(int fd, uint32_t baudrate);
#endif

View File

@ -55,6 +55,7 @@
#include "btstack_debug.h"
#include "hci.h"
#include "hci_transport.h"
#include "btstack_uart_posix.h"
#ifdef HAVE_EHCILL
#error "HCI Transport H4 POSIX does not support eHCILL yet. Please remove HAVE_EHCILL from your btstack-config.h"
@ -106,63 +107,9 @@ static uint8_t hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 1 + HCI
static uint8_t * hci_packet = &hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE];
static int h4_set_baudrate(uint32_t baudrate){
log_info("h4_set_baudrate %u", baudrate);
struct termios toptions;
int fd = btstack_run_loop_get_data_source_fd(hci_transport_h4->ds);
if (tcgetattr(fd, &toptions) < 0) {
perror("init_serialport: Couldn't get term attributes");
return -1;
}
speed_t brate = baudrate; // let you override switch below if needed
switch(baudrate) {
case 57600: brate=B57600; break;
case 115200: brate=B115200; break;
#ifdef B230400
case 230400: brate=B230400; break;
#endif
#ifdef B460800
case 460800: brate=B460800; break;
#endif
#ifdef B921600
case 921600: brate=B921600; break;
#endif
// Hacks to switch to 2/3 mbps on FTDI FT232 chipsets
// requires special config in Info.plist or Registry
case 2000000:
#if defined(HAVE_POSIX_B300_MAPPED_TO_2000000)
log_info("hci_transport_posix: using B300 for 2 mbps");
brate=B300;
#elif defined(HAVE_POSIX_B1200_MAPPED_TO_2000000)
log_info("hci_transport_posix: using B1200 for 2 mbps");
brate=B1200;
#endif
break;
case 3000000:
#if defined(HAVE_POSIX_B600_MAPPED_TO_3000000)
log_info("hci_transport_posix: using B600 for 3 mbps");
brate=B600;
#elif defined(HAVE_POSIX_B2400_MAPPED_TO_3000000)
log_info("hci_transport_posix: using B2400 for 3 mbps");
brate=B2400;
#endif
break;
default:
break;
}
cfsetospeed(&toptions, brate);
cfsetispeed(&toptions, brate);
if( tcsetattr(fd, TCSANOW, &toptions) < 0) {
perror("init_serialport: Couldn't set term attributes");
return -1;
}
return 0;
return btstack_uart_posix_set_baudrate(fd, baudrate);
}
static void h4_init(const void * transport_config){

View File

@ -53,6 +53,8 @@
#include "btstack_slip.h"
#include "btstack_debug.h"
#include "hci_transport.h"
#include "btstack_uart_posix.h"
#ifdef HAVE_EHCILL
#error "HCI Transport H5 POSIX does not support eHCILL. Please either use HAVE_EHCILL or H5 Transport"
@ -125,7 +127,7 @@ static void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t si
// Prototypes
static int hci_transport_h5_process(btstack_data_source_t *ds);
static void hci_transport_h5_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type);
static void hci_transport_link_set_timer(uint16_t timeout_ms);
static void hci_transport_link_timeout_handler(btstack_timer_source_t * timer);
static int hci_transport_h5_outgoing_packet(void);
@ -525,42 +527,14 @@ static void hci_transport_h5_init(const void * transport_config){
}
static int hci_transport_h5_set_baudrate(uint32_t baudrate){
log_info("hci_transport_h5_set_baudrate %u", baudrate);
struct termios toptions;
int fd = hci_transport_h5_data_source.fd;
if (tcgetattr(fd, &toptions) < 0) {
perror("init_serialport: Couldn't get term attributes");
return -1;
}
speed_t brate = baudrate; // let you override switch below if needed
switch(baudrate) {
case 57600: brate=B57600; break;
case 115200: brate=B115200; break;
#ifdef B230400
case 230400: brate=B230400; break;
#endif
#ifdef B460800
case 460800: brate=B460800; break;
#endif
#ifdef B921600
case 921600: brate=B921600; break;
#endif
}
cfsetospeed(&toptions, brate);
cfsetispeed(&toptions, brate);
if( tcsetattr(fd, TCSANOW, &toptions) < 0) {
perror("init_serialport: Couldn't set term attributes");
return -1;
}
int res = btstack_uart_posix_set_baudrate(fd, baudrate);
if (res) return res;
// extra for h5: calc resend timeout
link_resend_timeout_ms = hci_transport_h5_calc_resend_timeout(baudrate);
return 0;
}

View File

@ -1,13 +1,6 @@
# Makefile for libusb based examples
BTSTACK_ROOT = ../..
CORE += main.c stdin_support.c
COMMON += \
hci_transport_h4_posix.c \
btstack_run_loop_posix.c \
btstack_link_key_db_fs.c \
CORE += \
bluetooth_init_cc2564B_1.2_BT_Spec_4.1.c \
btstack_chipset_cc256x.c \
@ -15,6 +8,12 @@ CORE += \
btstack_chipset_em9301.c \
btstack_chipset_stlc2500d.c \
btstack_chipset_tc3566x.c \
btstack_link_key_db_fs.c \
btstack_run_loop_posix.c \
btstack_uart_posix.c \
hci_transport_h4_posix.c \
main.c \
stdin_support.c \
# btstack_chipset_bcm.c \
# TI-WL183x requires TIInit_11.8.32.c