e7e57b692599041020601080567c50f7d8feb1ea
[platform/core/api/peripheral-io.git] / src / gdbus / peripheral_gdbus_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 <gio/gunixfdlist.h>
20
21 #include "peripheral_io.h"
22 #include "peripheral_gdbus.h"
23 #include "peripheral_common.h"
24 #include "peripheral_internal.h"
25 #include "peripheral_io_gdbus.h"
26
27 #define UART_FD_INDEX 0
28
29 static PeripheralIoGdbusUart *uart_proxy = NULL;
30
31 void uart_proxy_init(void)
32 {
33         GError *error = NULL;
34
35         if (uart_proxy != NULL) {
36                 g_object_ref(uart_proxy);
37                 return;
38         }
39
40         uart_proxy = peripheral_io_gdbus_uart_proxy_new_for_bus_sync(
41                 G_BUS_TYPE_SYSTEM,
42                 G_DBUS_PROXY_FLAGS_NONE,
43                 PERIPHERAL_GDBUS_NAME,
44                 PERIPHERAL_GDBUS_UART_PATH,
45                 NULL,
46                 &error);
47 }
48
49 void uart_proxy_deinit()
50 {
51         if (uart_proxy) {
52                 g_object_unref(uart_proxy);
53                 if (!G_IS_OBJECT(uart_proxy))
54                         uart_proxy = NULL;
55         }
56 }
57
58 int peripheral_gdbus_uart_open(peripheral_uart_h uart, int port)
59 {
60         GError *error = NULL;
61         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
62         GUnixFDList *fd_list = NULL;
63
64         if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
65
66         if (peripheral_io_gdbus_uart_call_open_sync(
67                         uart_proxy,
68                         port,
69                         NULL,
70                         &uart->handle,
71                         &ret,
72                         &fd_list,
73                         NULL,
74                         &error) == FALSE) {
75                 _E("Error in %s() : %s", __func__, error->message);
76                 g_error_free(error);
77                 return PERIPHERAL_ERROR_UNKNOWN;
78         }
79
80         uart->fd = g_unix_fd_list_get(fd_list, UART_FD_INDEX, &error);
81         if (uart->fd < 0) {
82                 _E("Failed to get fd for uart : %s", error->message);
83                 g_error_free(error);
84                 ret = PERIPHERAL_ERROR_UNKNOWN;
85         }
86
87         g_object_unref(fd_list);
88
89         return ret;
90 }
91
92 int peripheral_gdbus_uart_close(peripheral_uart_h uart)
93 {
94         GError *error = NULL;
95         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
96
97         if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
98
99         if (peripheral_io_gdbus_uart_call_close_sync(
100                         uart_proxy,
101                         uart->handle,
102                         &ret,
103                         NULL,
104                         &error) == FALSE) {
105                 _E("Error in %s() : %s", __func__, error->message);
106                 g_error_free(error);
107                 return PERIPHERAL_ERROR_UNKNOWN;
108         }
109
110         return ret;
111 }
112
113 int peripheral_gdbus_uart_set_baud_rate(peripheral_uart_h uart, peripheral_uart_baud_rate_e baud)
114 {
115         GError *error = NULL;
116         gint32 ret = PERIPHERAL_ERROR_NONE;
117
118         if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
119
120         if (peripheral_io_gdbus_uart_call_set_baud_rate_sync(
121                         uart_proxy,
122                         uart->handle,
123                         baud,
124                         &ret,
125                         NULL,
126                         &error) == FALSE) {
127                 _E("Error in %s() : %s", __func__, error->message);
128                 g_error_free(error);
129                 return PERIPHERAL_ERROR_UNKNOWN;
130         }
131
132         return ret;
133 }
134
135 int peripheral_gdbus_uart_set_byte_size(peripheral_uart_h uart, peripheral_uart_byte_size_e byte_size)
136 {
137         GError *error = NULL;
138         gint32 ret = PERIPHERAL_ERROR_NONE;
139
140         if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
141
142         if (peripheral_io_gdbus_uart_call_set_byte_size_sync(
143                         uart_proxy,
144                         uart->handle,
145                         byte_size,
146                         &ret,
147                         NULL,
148                         &error) == FALSE) {
149                 _E("Error in %s() : %s", __func__, error->message);
150                 g_error_free(error);
151                 return PERIPHERAL_ERROR_UNKNOWN;
152         }
153
154         return ret;
155 }
156
157 int peripheral_gdbus_uart_set_parity(peripheral_uart_h uart, peripheral_uart_parity_e parity)
158 {
159         GError *error = NULL;
160         gint32 ret = PERIPHERAL_ERROR_NONE;
161
162         if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
163
164         if (peripheral_io_gdbus_uart_call_set_parity_sync(
165                         uart_proxy,
166                         uart->handle,
167                         parity,
168                         &ret,
169                         NULL,
170                         &error) == FALSE) {
171                 _E("Error in %s() : %s", __func__, error->message);
172                 g_error_free(error);
173                 return PERIPHERAL_ERROR_UNKNOWN;
174         }
175
176         return ret;
177 }
178
179 int peripheral_gdbus_uart_set_stop_bits(peripheral_uart_h uart, peripheral_uart_stop_bits_e stop_bits)
180 {
181         GError *error = NULL;
182         gint32 ret = PERIPHERAL_ERROR_NONE;
183
184         if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
185
186         if (peripheral_io_gdbus_uart_call_set_stop_bits_sync(
187                         uart_proxy,
188                         uart->handle,
189                         stop_bits,
190                         &ret,
191                         NULL,
192                         &error) == FALSE) {
193                 _E("Error in %s() : %s", __func__, error->message);
194                 g_error_free(error);
195                 return PERIPHERAL_ERROR_UNKNOWN;
196         }
197
198         return ret;
199 }
200
201 int peripheral_gdbus_uart_set_flow_control(peripheral_uart_h uart, bool xonxoff, bool rtscts)
202 {
203         GError *error = NULL;
204         gint32 ret = PERIPHERAL_ERROR_NONE;
205
206         if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
207
208         if (peripheral_io_gdbus_uart_call_set_flow_control_sync(
209                         uart_proxy,
210                         uart->handle,
211                         xonxoff,
212                         rtscts,
213                         &ret,
214                         NULL,
215                         &error) == FALSE) {
216                 _E("Error in %s() : %s", __func__, error->message);
217                 g_error_free(error);
218                 return PERIPHERAL_ERROR_UNKNOWN;
219         }
220
221         return ret;
222 }
223
224 int peripheral_gdbus_uart_read(peripheral_uart_h uart, uint8_t *data, int length)
225 {
226         GError *error = NULL;
227         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
228         GVariant *data_array;
229         GVariantIter *iter;
230         guint8 str;
231         int i = 0;
232
233         if (uart_proxy == NULL || data == NULL) return PERIPHERAL_ERROR_UNKNOWN;
234
235         if (peripheral_io_gdbus_uart_call_read_sync(
236                         uart_proxy,
237                         uart->handle,
238                         length,
239                         &data_array,
240                         &ret,
241                         NULL,
242                         &error) == FALSE) {
243                 _E("Error in %s() : %s", __func__, error->message);
244                 g_error_free(error);
245                 return PERIPHERAL_ERROR_UNKNOWN;
246         }
247
248         g_variant_get(data_array, "a(y)", &iter);
249         while (g_variant_iter_loop(iter, "(y)", &str)) {
250                 data[i] = str;
251                 if (i++ == length) break;
252         }
253         g_variant_iter_free(iter);
254         g_variant_unref(data_array);
255
256         return ret;
257 }
258
259 int peripheral_gdbus_uart_write(peripheral_uart_h uart, uint8_t *data, int length)
260 {
261         GError *error = NULL;
262         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
263         GVariantBuilder *builder;
264         GVariant *g_data;
265         int i = 0;
266
267         if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
268
269         builder = g_variant_builder_new(G_VARIANT_TYPE("a(y)"));
270
271         for (i = 0; i < length; i++)
272                 g_variant_builder_add(builder, "(y)", data[i]);
273         g_variant_builder_add(builder, "(y)", 0x00);
274
275         g_data = g_variant_new("a(y)", builder);
276         g_variant_builder_unref(builder);
277
278         if (peripheral_io_gdbus_uart_call_write_sync(
279                         uart_proxy,
280                         uart->handle,
281                         length,
282                         g_data,
283                         &ret,
284                         NULL,
285                         &error) == FALSE) {
286                 _E("Error in %s() : %s", __func__, error->message);
287                 g_error_free(error);
288                 return PERIPHERAL_ERROR_UNKNOWN;
289         }
290
291         return ret;
292 }