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