2 * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <system_info.h>
23 #include "peripheral_io.h"
24 #include "peripheral_gdbus_uart.h"
25 #include "peripheral_common.h"
26 #include "peripheral_internal.h"
28 #define PERIPHERAL_IO_UART_FEATURE "http://tizen.org/feature/peripheral_io.uart"
30 #define UART_FEATURE_UNKNOWN -1
31 #define UART_FEATURE_FALSE 0
32 #define UART_FEATURE_TRUE 1
34 static int uart_feature = UART_FEATURE_UNKNOWN;
36 static bool __is_feature_supported()
38 int ret = SYSTEM_INFO_ERROR_NONE;
41 if (uart_feature == UART_FEATURE_UNKNOWN) {
42 ret = system_info_get_platform_bool(PERIPHERAL_IO_UART_FEATURE, &feature);
43 RETVM_IF(ret != SYSTEM_INFO_ERROR_NONE, false, "Failed to get system info");
45 uart_feature = (feature ? UART_FEATURE_TRUE : UART_FEATURE_FALSE);
48 return (uart_feature == UART_FEATURE_TRUE ? true : false);
52 * @brief Initializes uart communication and creates uart handle.
54 int peripheral_uart_open(int port, peripheral_uart_h *uart)
56 peripheral_uart_h handle;
57 int ret = PERIPHERAL_ERROR_NONE;
59 RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "UART feature is not supported");
60 RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid uart handle");
61 RETVM_IF(port < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid port number");
63 handle = (peripheral_uart_h)calloc(1, sizeof(struct _peripheral_uart_s));
66 _E("Failed to allocate peripheral_uart_h");
67 return PERIPHERAL_ERROR_OUT_OF_MEMORY;
72 ret = peripheral_gdbus_uart_open(handle, port);
74 if (ret != PERIPHERAL_ERROR_NONE) {
75 _E("Failed to open uart port, ret : %d", ret);
85 * @brief Destory the uart handle and release the communication.
87 int peripheral_uart_close(peripheral_uart_h uart)
89 int ret = PERIPHERAL_ERROR_NONE;
91 RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "UART feature is not supported");
92 RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL");
94 ret = peripheral_gdbus_uart_close(uart);
95 if (ret < PERIPHERAL_ERROR_NONE)
96 _E("Failed to close uart communication, continuing anyway, ret : %d", ret);
106 * @brief Sets baudrate of the uart device.
108 int peripheral_uart_set_baud_rate(peripheral_uart_h uart, peripheral_uart_baud_rate_e baud)
110 int ret = PERIPHERAL_ERROR_NONE;
112 RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "UART feature is not supported");
113 RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL");
114 RETVM_IF((baud < PERIPHERAL_UART_BAUD_RATE_0) || (baud > PERIPHERAL_UART_BAUD_RATE_230400), PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid baud input");
116 // TODO : replace interface function
121 int peripheral_uart_set_byte_size(peripheral_uart_h uart, peripheral_uart_byte_size_e byte_size)
123 int ret = PERIPHERAL_ERROR_NONE;
125 RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "UART feature is not supported");
126 RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL");
127 RETVM_IF((byte_size < PERIPHERAL_UART_BYTE_SIZE_5BIT) || (byte_size > PERIPHERAL_UART_BYTE_SIZE_8BIT), PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
129 // TODO : replace interface function
134 int peripheral_uart_set_parity(peripheral_uart_h uart, peripheral_uart_parity_e parity)
136 int ret = PERIPHERAL_ERROR_NONE;
138 RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "UART feature is not supported");
139 RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL");
140 RETVM_IF((parity < PERIPHERAL_UART_PARITY_NONE) || (parity > PERIPHERAL_UART_PARITY_ODD), PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
142 // TODO : replace interface function
147 int peripheral_uart_set_stop_bits(peripheral_uart_h uart, peripheral_uart_stop_bits_e stop_bits)
149 int ret = PERIPHERAL_ERROR_NONE;
151 RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "UART feature is not supported");
152 RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL");
153 RETVM_IF((stop_bits < PERIPHERAL_UART_STOP_BITS_1BIT) || (stop_bits > PERIPHERAL_UART_STOP_BITS_2BIT), PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
155 // TODO : replace interface function
162 * @brief Sets baudrate of the uart device.
164 int peripheral_uart_set_flow_control(peripheral_uart_h uart, peripheral_uart_software_flow_control_e sw_flow_control, peripheral_uart_hardware_flow_control_e hw_flow_control)
166 int ret = PERIPHERAL_ERROR_NONE;
168 RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "UART feature is not supported");
169 RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL");
170 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");
171 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");
173 // TODO : replace interface function
179 * @brief Reads data from the uart device.
181 int peripheral_uart_read(peripheral_uart_h uart, uint8_t *data, uint32_t length)
183 int ret = PERIPHERAL_ERROR_NONE;
185 RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "UART feature is not supported");
186 RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL");
187 RETVM_IF(data == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
189 // TODO : replace interface function
195 * @brief Write data to the uart device.
197 int peripheral_uart_write(peripheral_uart_h uart, uint8_t *data, uint32_t length)
199 int ret = PERIPHERAL_ERROR_NONE;
201 RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "UART feature is not supported");
202 RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL");
203 RETVM_IF(data == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
205 // TODO : replace interface function