Merge src/peripheral_uart.c with src/interface/peripheral_interface_uart.c.
Change-Id: I1fdb5a0fb7c23a18bf591911a47eba1314246e91
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
+++ /dev/null
-/*
- * 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__ */
-
* @brief Internal struct for uart context
*/
struct _peripheral_uart_s {
- uint handle;
int fd;
};
+++ /dev/null
-/*
- * 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 <termios.h>
-#include <limits.h>
-
-#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;
-}
#include <termios.h>
#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"
#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)
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)
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)
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)
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;
}
/**
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;
}
/**
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;
}
/**
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;
}