[5/6] fd passing: remove unnecessary gdbus functions.
[platform/core/api/peripheral-io.git] / src / peripheral_uart.c
1 /*
2  * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <assert.h>
21 #include <system_info.h>
22
23 #include "peripheral_io.h"
24 #include "peripheral_gdbus_uart.h"
25 #include "peripheral_common.h"
26 #include "peripheral_internal.h"
27
28 #define PERIPHERAL_IO_UART_FEATURE "http://tizen.org/feature/peripheral_io.uart"
29
30 #define UART_FEATURE_UNKNOWN -1
31 #define UART_FEATURE_FALSE    0
32 #define UART_FEATURE_TRUE     1
33
34 static int uart_feature = UART_FEATURE_UNKNOWN;
35
36 static bool __is_feature_supported()
37 {
38         int ret = SYSTEM_INFO_ERROR_NONE;
39         bool feature = false;
40
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");
44
45                 uart_feature = (feature ? UART_FEATURE_TRUE : UART_FEATURE_FALSE);
46         }
47
48         return (uart_feature == UART_FEATURE_TRUE ? true : false);
49 }
50
51 /**
52  * @brief Initializes uart communication and creates uart handle.
53  */
54 int peripheral_uart_open(int port, peripheral_uart_h *uart)
55 {
56         peripheral_uart_h handle;
57         int ret = PERIPHERAL_ERROR_NONE;
58
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");
62
63         handle = (peripheral_uart_h)calloc(1, sizeof(struct _peripheral_uart_s));
64
65         if (handle == NULL) {
66                 _E("Failed to allocate peripheral_uart_h");
67                 return PERIPHERAL_ERROR_OUT_OF_MEMORY;
68         }
69
70         uart_proxy_init();
71
72         ret = peripheral_gdbus_uart_open(handle, port);
73
74         if (ret != PERIPHERAL_ERROR_NONE) {
75                 _E("Failed to open uart port, ret : %d", ret);
76                 free(handle);
77                 handle = NULL;
78         }
79         *uart = handle;
80
81         return ret;
82 }
83
84 /**
85  * @brief Destory the uart handle and release the communication.
86  */
87 int peripheral_uart_close(peripheral_uart_h uart)
88 {
89         int ret = PERIPHERAL_ERROR_NONE;
90
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");
93
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);
97         uart_proxy_deinit();
98
99         free(uart);
100         uart = NULL;
101
102         return ret;
103 }
104
105 /**
106  * @brief Sets baudrate of the uart device.
107  */
108 int peripheral_uart_set_baud_rate(peripheral_uart_h uart, peripheral_uart_baud_rate_e baud)
109 {
110         int ret = PERIPHERAL_ERROR_NONE;
111
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");
115
116         // TODO : replace interface function
117
118         return ret;
119 }
120
121 int peripheral_uart_set_byte_size(peripheral_uart_h uart, peripheral_uart_byte_size_e byte_size)
122 {
123         int ret = PERIPHERAL_ERROR_NONE;
124
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");
128
129         // TODO : replace interface function
130
131         return ret;
132 }
133
134 int peripheral_uart_set_parity(peripheral_uart_h uart, peripheral_uart_parity_e parity)
135 {
136         int ret = PERIPHERAL_ERROR_NONE;
137
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");
141
142         // TODO : replace interface function
143
144         return ret;
145 }
146
147 int peripheral_uart_set_stop_bits(peripheral_uart_h uart, peripheral_uart_stop_bits_e stop_bits)
148 {
149         int ret = PERIPHERAL_ERROR_NONE;
150
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");
154
155         // TODO : replace interface function
156
157         return ret;
158 }
159
160
161 /**
162  * @brief Sets baudrate of the uart device.
163  */
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)
165 {
166         int ret = PERIPHERAL_ERROR_NONE;
167
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");
172
173         // TODO : replace interface function
174
175         return ret;
176 }
177
178 /**
179  * @brief Reads data from the uart device.
180  */
181 int peripheral_uart_read(peripheral_uart_h uart, uint8_t *data, uint32_t length)
182 {
183         int ret = PERIPHERAL_ERROR_NONE;
184
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");
188
189         // TODO : replace interface function
190
191         return ret;
192 }
193
194 /**
195  * @brief Write data to the uart device.
196  */
197 int peripheral_uart_write(peripheral_uart_h uart, uint8_t *data, uint32_t length)
198 {
199         int ret = PERIPHERAL_ERROR_NONE;
200
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");
204
205         // TODO : replace interface function
206
207         return ret;
208 }