81b77c1827d69cb4ef82d8c1a83ce07cebb4314f
[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 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_flush(peripheral_uart_h uart)
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_flush_sync(
106                         uart_proxy,
107                         uart->handle,
108                         &ret,
109                         NULL,
110                         &error) == FALSE) {
111                 _E("Error in %s() : %s", __func__, error->message);
112                 g_error_free(error);
113                 return PERIPHERAL_ERROR_UNKNOWN;
114         }
115
116         return ret;
117 }
118
119 int peripheral_gdbus_uart_set_baudrate(peripheral_uart_h uart, peripheral_uart_baudrate_e baud)
120 {
121         GError *error = NULL;
122         gint32 ret = PERIPHERAL_ERROR_NONE;
123
124         if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
125
126         if (peripheral_io_gdbus_uart_call_set_baudrate_sync(
127                         uart_proxy,
128                         uart->handle,
129                         baud,
130                         &ret,
131                         NULL,
132                         &error) == FALSE) {
133                 _E("Error in %s() : %s", __func__, error->message);
134                 g_error_free(error);
135                 return PERIPHERAL_ERROR_UNKNOWN;
136         }
137
138         return ret;
139 }
140
141 int peripheral_gdbus_uart_set_mode(peripheral_uart_h uart, peripheral_uart_bytesize_e bytesize, peripheral_uart_parity_e parity, peripheral_uart_stopbits_e stopbits)
142 {
143         GError *error = NULL;
144         gint32 ret = PERIPHERAL_ERROR_NONE;
145
146         if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
147
148         if (peripheral_io_gdbus_uart_call_set_mode_sync(
149                         uart_proxy,
150                         uart->handle,
151                         bytesize,
152                         parity,
153                         stopbits,
154                         &ret,
155                         NULL,
156                         &error) == FALSE) {
157                 _E("Error in %s() : %s", __func__, error->message);
158                 g_error_free(error);
159                 return PERIPHERAL_ERROR_UNKNOWN;
160         }
161
162         return ret;
163 }
164
165 int peripheral_gdbus_uart_set_flowcontrol(peripheral_uart_h uart, bool xonxoff, bool rtscts)
166 {
167         GError *error = NULL;
168         gint32 ret = PERIPHERAL_ERROR_NONE;
169
170         if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
171
172         if (peripheral_io_gdbus_uart_call_set_flowcontrol_sync(
173                         uart_proxy,
174                         uart->handle,
175                         xonxoff,
176                         rtscts,
177                         &ret,
178                         NULL,
179                         &error) == FALSE) {
180                 _E("Error in %s() : %s", __func__, error->message);
181                 g_error_free(error);
182                 return PERIPHERAL_ERROR_UNKNOWN;
183         }
184
185         return ret;
186 }
187
188 int peripheral_gdbus_uart_read(peripheral_uart_h uart, uint8_t *data, int length)
189 {
190         GError *error = NULL;
191         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
192         GVariant *data_array;
193         GVariantIter *iter;
194         guint8 str;
195         int i = 0;
196
197         if (uart_proxy == NULL || data == NULL) return PERIPHERAL_ERROR_UNKNOWN;
198
199         if (peripheral_io_gdbus_uart_call_read_sync(
200                         uart_proxy,
201                         uart->handle,
202                         length,
203                         &data_array,
204                         &ret,
205                         NULL,
206                         &error) == FALSE) {
207                 _E("Error in %s() : %s", __func__, error->message);
208                 g_error_free(error);
209                 return PERIPHERAL_ERROR_UNKNOWN;
210         }
211
212         g_variant_get(data_array, "a(y)", &iter);
213         while (g_variant_iter_loop(iter, "(y)", &str)) {
214                 data[i] = str;
215                 if (i++ == length) break;
216         }
217         g_variant_iter_free(iter);
218         g_variant_unref(data_array);
219
220         return ret;
221 }
222
223 int peripheral_gdbus_uart_write(peripheral_uart_h uart, uint8_t *data, int length)
224 {
225         GError *error = NULL;
226         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
227         GVariantBuilder *builder;
228         GVariant *g_data;
229         int i = 0;
230
231         if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
232
233         builder = g_variant_builder_new(G_VARIANT_TYPE("a(y)"));
234
235         for (i = 0; i < length; i++)
236                 g_variant_builder_add(builder, "(y)", data[i]);
237         g_variant_builder_add(builder, "(y)", 0x00);
238
239         g_data = g_variant_new("a(y)", builder);
240         g_variant_builder_unref(builder);
241
242         if (peripheral_io_gdbus_uart_call_write_sync(
243                         uart_proxy,
244                         uart->handle,
245                         length,
246                         g_data,
247                         &ret,
248                         NULL,
249                         &error) == FALSE) {
250                 _E("Error in %s() : %s", __func__, error->message);
251                 g_error_free(error);
252                 return PERIPHERAL_ERROR_UNKNOWN;
253         }
254
255         return ret;
256 }