From 5d983f4e4c79431e91b983dc7f8ee95a60f2c18a Mon Sep 17 00:00:00 2001 From: Adrian Szyndela Date: Fri, 11 Jun 2021 15:29:28 +0200 Subject: [PATCH] uart: flatten structure - remove 'interface' Merge src/peripheral_uart.c with src/interface/peripheral_interface_uart.c. Change-Id: I1fdb5a0fb7c23a18bf591911a47eba1314246e91 --- CMakeLists.txt | 1 - include/interface/peripheral_interface_uart.h | 104 ---------- include/peripheral_handle.h | 1 - src/interface/peripheral_interface_uart.c | 196 ------------------ src/peripheral_uart.c | 145 ++++++++++++- 5 files changed, 136 insertions(+), 311 deletions(-) delete mode 100644 include/interface/peripheral_interface_uart.h delete mode 100644 src/interface/peripheral_interface_uart.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c10c3a..e1b8631 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,7 +63,6 @@ SET(SOURCES src/peripheral_gpio.c src/interface/peripheral_interface_gpio.c src/interface/peripheral_interface_pwm.c src/interface/peripheral_interface_adc.c - src/interface/peripheral_interface_uart.c src/gdbus/peripheral_gdbus_gpio.c src/gdbus/peripheral_gdbus_pwm.c src/gdbus/peripheral_gdbus_adc.c diff --git a/include/interface/peripheral_interface_uart.h b/include/interface/peripheral_interface_uart.h deleted file mode 100644 index 75748ce..0000000 --- a/include/interface/peripheral_interface_uart.h +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __PERIPHERAL_INTERFACE_UART_H__ -#define __PERIPHERAL_INTERFACE_UART_H__ - -#include "peripheral_interface_common.h" - -/** -* @brief uart_close() closes uart port. -* -* @param[in] file_hndl handle of uart_context -* @return On success, 0 is returned. On failure, a negative value is returned. -*/ -void peripheral_interface_uart_close(peripheral_uart_h uart); - -/** -* @brief uart_flush() flushes uart buffer. -* -* @param[in] file_hndl handle of uart_context -*/ -void peripheral_interface_uart_flush(peripheral_uart_h uart); - -/** -* @brief uart_set_baudrate() sets uart baud rate. -* -* @param[in] file_hndl handle of uart_context -* @param[in] baud uart baud rate -* @return On success, 0 is returned. On failure, a negative value is returned. -*/ -int peripheral_interface_uart_set_baud_rate(peripheral_uart_h uart, peripheral_uart_baud_rate_e baud); - -/** -* @brief peripheral_bus_uart_set_byte_size() set byte size. -* -* @param[in] file_hndl handle of uart_context -* @param[in] byte_size uart byte size -* @return On success, 0 is returned. On failure, a negative value is returned. -*/ -int peripheral_interface_uart_set_byte_size(peripheral_uart_h uart, peripheral_uart_byte_size_e byte_size); - -/** -* @brief peripheral_bus_uart_set_parity() set parity bit. -* -* @param[in] file_hndl handle of uart_context -* @param[in] parity uart parity type (even/odd/none) -* @return On success, 0 is returned. On failure, a negative value is returned. -*/ -int peripheral_interface_uart_set_parity(peripheral_uart_h uart, peripheral_uart_parity_e parity); - -/** -* @brief peripheral_bus_uart_set_stop_bits() set stop bits. -* -* @param[in] file_hndl handle of uart_context -* @param[in] stop_bits uart stop bits -* @return On success, 0 is returned. On failure, a negative value is returned. -*/ -int peripheral_interface_uart_set_stop_bits(peripheral_uart_h uart, peripheral_uart_stop_bits_e stop_bits); - -/** -* @brief uart_set_flow_control() set flow control settings. -* -* @param[in] file_hndl handle of uart_context -* @param[in] xonxoff ixon/ixoff -* @param[in] rtscts rts/cts -* @return On success, 0 is returned. On failure, a negative value is returned. -*/ -int peripheral_interface_uart_set_flow_control(peripheral_uart_h uart, peripheral_uart_software_flow_control_e xonxoff, peripheral_uart_hardware_flow_control_e rtscts); - -/** -* @brief uart_read() reads data over uart bus. -* -* @param[in] file_hndl handle of uart_context -* @param[in] buf the pointer of data buffer -* @param[in] length size to read -* @return On success, 0 is returned. On failure, a negative value is returned. -*/ -int peripheral_interface_uart_read(peripheral_uart_h uart, uint8_t *buf, uint32_t length); - -/** -* @brief uart_write() writes data over uart bus. -* -* @param[in] file_hndl handle of uart_context -* @param[in] buf the pointer of data buffer -* @param[in] length size to write -* @return On success, 0 is returned. On failure, a negative value is returned. -*/ -int peripheral_interface_uart_write(peripheral_uart_h uart, uint8_t *buf, uint32_t length); - -#endif /* __PERIPHERAL_INTERFACE_UART_H__ */ - diff --git a/include/peripheral_handle.h b/include/peripheral_handle.h index bd6567e..2bd5fea 100644 --- a/include/peripheral_handle.h +++ b/include/peripheral_handle.h @@ -80,7 +80,6 @@ struct _peripheral_adc_s { * @brief Internal struct for uart context */ struct _peripheral_uart_s { - uint handle; int fd; }; diff --git a/src/interface/peripheral_interface_uart.c b/src/interface/peripheral_interface_uart.c deleted file mode 100644 index 09c4978..0000000 --- a/src/interface/peripheral_interface_uart.c +++ /dev/null @@ -1,196 +0,0 @@ -/* - * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include - -#include "peripheral_interface_uart.h" - -#define UART_BAUDRATE_SIZE 19 - -static const int peripheral_uart_br[UART_BAUDRATE_SIZE] = { - B0, B50, B75, B110, B134, - B150, B200, B300, B600, B1200, - B1800, B2400, B4800, B9600, B19200, - B38400, B57600, B115200, B230400 -}; - -static const int byteinfo[4] = {CS5, CS6, CS7, CS8}; - -void peripheral_interface_uart_close(peripheral_uart_h uart) -{ - peripheral_interface_uart_flush(uart); - close(uart->fd); -} - -void peripheral_interface_uart_flush(peripheral_uart_h uart) -{ - tcflush(uart->fd, TCIOFLUSH); -} - -int peripheral_interface_uart_set_baud_rate(peripheral_uart_h uart, peripheral_uart_baud_rate_e baud) -{ - int ret; - struct termios tio; - - ret = tcgetattr(uart->fd, &tio); - CHECK_ERROR(ret != 0); - - tio.c_cflag = peripheral_uart_br[baud]; - tio.c_iflag = IGNPAR; - tio.c_oflag = 0; - tio.c_lflag = 0; - tio.c_cc[VMIN] = 1; - tio.c_cc[VTIME] = 0; - - peripheral_interface_uart_flush(uart); - - ret = tcsetattr(uart->fd, TCSANOW, &tio); - CHECK_ERROR(ret != 0); - - return PERIPHERAL_ERROR_NONE; -} - -int peripheral_interface_uart_set_byte_size(peripheral_uart_h uart, peripheral_uart_byte_size_e byte_size) -{ - int ret; - struct termios tio; - - ret = tcgetattr(uart->fd, &tio); - CHECK_ERROR(ret != 0); - - /* set byte size */ - tio.c_cflag &= ~CSIZE; - tio.c_cflag |= byteinfo[byte_size]; - tio.c_cflag |= (CLOCAL | CREAD); - - peripheral_interface_uart_flush(uart); - - ret = tcsetattr(uart->fd, TCSANOW, &tio); - CHECK_ERROR(ret != 0); - - return PERIPHERAL_ERROR_NONE; -} - -int peripheral_interface_uart_set_parity(peripheral_uart_h uart, peripheral_uart_parity_e parity) -{ - int ret; - struct termios tio; - - ret = tcgetattr(uart->fd, &tio); - CHECK_ERROR(ret != 0); - - /* set parity info */ - switch (parity) { - case PERIPHERAL_UART_PARITY_EVEN: - tio.c_cflag |= PARENB; - tio.c_cflag &= ~PARODD; - break; - case PERIPHERAL_UART_PARITY_ODD: - tio.c_cflag |= PARENB; - tio.c_cflag |= PARODD; - break; - case PERIPHERAL_UART_PARITY_NONE: - default: - tio.c_cflag &= ~PARENB; - tio.c_cflag &= ~PARODD; - break; - } - - peripheral_interface_uart_flush(uart); - - ret = tcsetattr(uart->fd, TCSANOW, &tio); - CHECK_ERROR(ret != 0); - - return PERIPHERAL_ERROR_NONE; -} - -int peripheral_interface_uart_set_stop_bits(peripheral_uart_h uart, peripheral_uart_stop_bits_e stop_bits) -{ - int ret; - struct termios tio; - - ret = tcgetattr(uart->fd, &tio); - CHECK_ERROR(ret != 0); - - /* set stop bit */ - switch (stop_bits) { - case PERIPHERAL_UART_STOP_BITS_1BIT: - tio.c_cflag &= ~CSTOPB; - break; - case PERIPHERAL_UART_STOP_BITS_2BIT: - tio.c_cflag |= CSTOPB; - break; - default: - _E("Invalid parameter stop_bits"); - return PERIPHERAL_ERROR_INVALID_PARAMETER; - } - - peripheral_interface_uart_flush(uart); - - ret = tcsetattr(uart->fd, TCSANOW, &tio); - CHECK_ERROR(ret != 0); - - return PERIPHERAL_ERROR_NONE; -} - -int peripheral_interface_uart_set_flow_control(peripheral_uart_h uart, peripheral_uart_software_flow_control_e xonxoff, peripheral_uart_hardware_flow_control_e rtscts) -{ - int ret; - struct termios tio; - - ret = tcgetattr(uart->fd, &tio); - CHECK_ERROR(ret != 0); - - if (rtscts == PERIPHERAL_UART_HARDWARE_FLOW_CONTROL_AUTO_RTSCTS) - tio.c_cflag |= CRTSCTS; - else if (rtscts == PERIPHERAL_UART_HARDWARE_FLOW_CONTROL_NONE) - tio.c_cflag &= ~CRTSCTS; - else - return PERIPHERAL_ERROR_INVALID_PARAMETER; - - if (xonxoff == PERIPHERAL_UART_SOFTWARE_FLOW_CONTROL_XONXOFF) - tio.c_iflag |= (IXON | IXOFF | IXANY); - else if (xonxoff == PERIPHERAL_UART_SOFTWARE_FLOW_CONTROL_NONE) - tio.c_iflag &= ~(IXON | IXOFF | IXANY); - else - return PERIPHERAL_ERROR_INVALID_PARAMETER; - - peripheral_interface_uart_flush(uart); - - ret = tcsetattr(uart->fd, TCSANOW, &tio); - CHECK_ERROR(ret != 0); - - return PERIPHERAL_ERROR_NONE; -} - -int peripheral_interface_uart_read(peripheral_uart_h uart, uint8_t *buf, uint32_t length) -{ - /* the return type has to allow negative values (for error codes), so we can't use the upper half of unsigned values */ - int ret = read(uart->fd, (void *)buf, length > INT_MAX ? INT_MAX : length); - CHECK_ERROR(ret < 0); - - return ret; -} - -int peripheral_interface_uart_write(peripheral_uart_h uart, uint8_t *buf, uint32_t length) -{ - /* the return type has to allow negative values (for error codes), so we can't use the upper half of unsigned values */ - int ret = write(uart->fd, buf, length > INT_MAX ? INT_MAX : length); - CHECK_ERROR(ret < 0); - - return ret; -} diff --git a/src/peripheral_uart.c b/src/peripheral_uart.c index 3330bd2..daeb2e0 100644 --- a/src/peripheral_uart.c +++ b/src/peripheral_uart.c @@ -23,8 +23,7 @@ #include #include "peripheral_io.h" -#include "peripheral_handle.h" -#include "peripheral_interface_uart.h" +#include "peripheral_interface_common.h" #include "peripheral_log.h" #define PERIPHERAL_IO_UART_FEATURE "http://tizen.org/feature/peripheral_io.uart" @@ -37,6 +36,15 @@ #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0])) #endif +static const int peripheral_uart_br[] = { + B0, B50, B75, B110, B134, + B150, B200, B300, B600, B1200, + B1800, B2400, B4800, B9600, B19200, + B38400, B57600, B115200, B230400 +}; + +static const int byteinfo[4] = {CS5, CS6, CS7, CS8}; + static int uart_feature = UART_FEATURE_UNKNOWN; static bool __is_feature_supported(void) @@ -145,7 +153,25 @@ int peripheral_uart_set_baud_rate(peripheral_uart_h uart, peripheral_uart_baud_r RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL"); RETVM_IF((baud < PERIPHERAL_UART_BAUD_RATE_0) || (baud > PERIPHERAL_UART_BAUD_RATE_230400), PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid baud input"); - return peripheral_interface_uart_set_baud_rate(uart, baud); + int ret; + struct termios tio; + + ret = tcgetattr(uart->fd, &tio); + CHECK_ERROR(ret != 0); + + tio.c_cflag = peripheral_uart_br[baud]; + tio.c_iflag = IGNPAR; + tio.c_oflag = 0; + tio.c_lflag = 0; + tio.c_cc[VMIN] = 1; + tio.c_cc[VTIME] = 0; + + peripheral_uart_flush(uart); + + ret = tcsetattr(uart->fd, TCSANOW, &tio); + CHECK_ERROR(ret != 0); + + return PERIPHERAL_ERROR_NONE; } int peripheral_uart_set_byte_size(peripheral_uart_h uart, peripheral_uart_byte_size_e byte_size) @@ -154,7 +180,23 @@ int peripheral_uart_set_byte_size(peripheral_uart_h uart, peripheral_uart_byte_s RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL"); RETVM_IF((byte_size < PERIPHERAL_UART_BYTE_SIZE_5BIT) || (byte_size > PERIPHERAL_UART_BYTE_SIZE_8BIT), PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); - return peripheral_interface_uart_set_byte_size(uart, byte_size); + int ret; + struct termios tio; + + ret = tcgetattr(uart->fd, &tio); + CHECK_ERROR(ret != 0); + + /* set byte size */ + tio.c_cflag &= ~CSIZE; + tio.c_cflag |= byteinfo[byte_size]; + tio.c_cflag |= (CLOCAL | CREAD); + + peripheral_uart_flush(uart); + + ret = tcsetattr(uart->fd, TCSANOW, &tio); + CHECK_ERROR(ret != 0); + + return PERIPHERAL_ERROR_NONE; } int peripheral_uart_set_parity(peripheral_uart_h uart, peripheral_uart_parity_e parity) @@ -163,7 +205,35 @@ int peripheral_uart_set_parity(peripheral_uart_h uart, peripheral_uart_parity_e RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL"); RETVM_IF((parity < PERIPHERAL_UART_PARITY_NONE) || (parity > PERIPHERAL_UART_PARITY_ODD), PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); - return peripheral_interface_uart_set_parity(uart, parity); + int ret; + struct termios tio; + + ret = tcgetattr(uart->fd, &tio); + CHECK_ERROR(ret != 0); + + /* set parity info */ + switch (parity) { + case PERIPHERAL_UART_PARITY_EVEN: + tio.c_cflag |= PARENB; + tio.c_cflag &= ~PARODD; + break; + case PERIPHERAL_UART_PARITY_ODD: + tio.c_cflag |= PARENB; + tio.c_cflag |= PARODD; + break; + case PERIPHERAL_UART_PARITY_NONE: + default: + tio.c_cflag &= ~PARENB; + tio.c_cflag &= ~PARODD; + break; + } + + peripheral_uart_flush(uart); + + ret = tcsetattr(uart->fd, TCSANOW, &tio); + CHECK_ERROR(ret != 0); + + return PERIPHERAL_ERROR_NONE; } int peripheral_uart_set_stop_bits(peripheral_uart_h uart, peripheral_uart_stop_bits_e stop_bits) @@ -172,7 +242,31 @@ int peripheral_uart_set_stop_bits(peripheral_uart_h uart, peripheral_uart_stop_b RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL"); RETVM_IF((stop_bits < PERIPHERAL_UART_STOP_BITS_1BIT) || (stop_bits > PERIPHERAL_UART_STOP_BITS_2BIT), PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); - return peripheral_interface_uart_set_stop_bits(uart, stop_bits); + int ret; + struct termios tio; + + ret = tcgetattr(uart->fd, &tio); + CHECK_ERROR(ret != 0); + + /* set stop bit */ + switch (stop_bits) { + case PERIPHERAL_UART_STOP_BITS_1BIT: + tio.c_cflag &= ~CSTOPB; + break; + case PERIPHERAL_UART_STOP_BITS_2BIT: + tio.c_cflag |= CSTOPB; + break; + default: + _E("Invalid parameter stop_bits"); + return PERIPHERAL_ERROR_INVALID_PARAMETER; + } + + peripheral_uart_flush(uart); + + ret = tcsetattr(uart->fd, TCSANOW, &tio); + CHECK_ERROR(ret != 0); + + return PERIPHERAL_ERROR_NONE; } /** @@ -185,7 +279,32 @@ int peripheral_uart_set_flow_control(peripheral_uart_h uart, peripheral_uart_sof RETVM_IF((sw_flow_control < PERIPHERAL_UART_SOFTWARE_FLOW_CONTROL_NONE) || (sw_flow_control > PERIPHERAL_UART_SOFTWARE_FLOW_CONTROL_XONXOFF), PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid sw_flow_control parameter"); RETVM_IF((hw_flow_control < PERIPHERAL_UART_HARDWARE_FLOW_CONTROL_NONE) || (hw_flow_control > PERIPHERAL_UART_HARDWARE_FLOW_CONTROL_AUTO_RTSCTS), PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid hw_flow_control parameter"); - return peripheral_interface_uart_set_flow_control(uart, sw_flow_control, hw_flow_control); + int ret; + struct termios tio; + + ret = tcgetattr(uart->fd, &tio); + CHECK_ERROR(ret != 0); + + if (hw_flow_control == PERIPHERAL_UART_HARDWARE_FLOW_CONTROL_AUTO_RTSCTS) + tio.c_cflag |= CRTSCTS; + else if (hw_flow_control == PERIPHERAL_UART_HARDWARE_FLOW_CONTROL_NONE) + tio.c_cflag &= ~CRTSCTS; + else + return PERIPHERAL_ERROR_INVALID_PARAMETER; + + if (sw_flow_control == PERIPHERAL_UART_SOFTWARE_FLOW_CONTROL_XONXOFF) + tio.c_iflag |= (IXON | IXOFF | IXANY); + else if (sw_flow_control == PERIPHERAL_UART_SOFTWARE_FLOW_CONTROL_NONE) + tio.c_iflag &= ~(IXON | IXOFF | IXANY); + else + return PERIPHERAL_ERROR_INVALID_PARAMETER; + + peripheral_uart_flush(uart); + + ret = tcsetattr(uart->fd, TCSANOW, &tio); + CHECK_ERROR(ret != 0); + + return PERIPHERAL_ERROR_NONE; } /** @@ -197,7 +316,11 @@ int peripheral_uart_read(peripheral_uart_h uart, uint8_t *data, uint32_t length) RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL"); RETVM_IF(data == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); - return peripheral_interface_uart_read(uart, data, length); + /* the return type has to allow negative values (for error codes), so we can't use the upper half of unsigned values */ + int ret = read(uart->fd, (void *)data, length > INT_MAX ? INT_MAX : length); + CHECK_ERROR(ret < 0); + + return ret; } /** @@ -209,5 +332,9 @@ int peripheral_uart_write(peripheral_uart_h uart, uint8_t *data, uint32_t length RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL"); RETVM_IF(data == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); - return peripheral_interface_uart_write(uart, data, length); + /* the return type has to allow negative values (for error codes), so we can't use the upper half of unsigned values */ + int ret = write(uart->fd, data, length > INT_MAX ? INT_MAX : length); + CHECK_ERROR(ret < 0); + + return ret; } -- 2.34.1