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.
19 #include <gio/gunixfdlist.h>
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"
27 #define UART_FD_INDEX 0
29 static PeripheralIoGdbusUart *uart_proxy = NULL;
31 void uart_proxy_init(void)
35 if (uart_proxy != NULL) {
36 g_object_ref(uart_proxy);
40 uart_proxy = peripheral_io_gdbus_uart_proxy_new_for_bus_sync(
42 G_DBUS_PROXY_FLAGS_NONE,
43 PERIPHERAL_GDBUS_NAME,
44 PERIPHERAL_GDBUS_UART_PATH,
49 void uart_proxy_deinit()
52 g_object_unref(uart_proxy);
53 if (!G_IS_OBJECT(uart_proxy))
58 int peripheral_gdbus_uart_open(peripheral_uart_h uart, int port)
61 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
62 GUnixFDList *fd_list = NULL;
64 if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
66 if (peripheral_io_gdbus_uart_call_open_sync(
75 _E("Error in %s() : %s", __func__, error->message);
77 return PERIPHERAL_ERROR_UNKNOWN;
80 uart->fd = g_unix_fd_list_get(fd_list, UART_FD_INDEX, &error);
82 _E("Failed to get fd for uart : %s", error->message);
84 ret = PERIPHERAL_ERROR_UNKNOWN;
87 g_object_unref(fd_list);
92 int peripheral_gdbus_uart_close(peripheral_uart_h uart)
95 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
97 if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
99 if (peripheral_io_gdbus_uart_call_close_sync(
105 _E("Error in %s() : %s", __func__, error->message);
107 return PERIPHERAL_ERROR_UNKNOWN;
113 int peripheral_gdbus_uart_set_baud_rate(peripheral_uart_h uart, peripheral_uart_baud_rate_e baud)
115 GError *error = NULL;
116 gint32 ret = PERIPHERAL_ERROR_NONE;
118 if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
120 if (peripheral_io_gdbus_uart_call_set_baud_rate_sync(
127 _E("Error in %s() : %s", __func__, error->message);
129 return PERIPHERAL_ERROR_UNKNOWN;
135 int peripheral_gdbus_uart_set_byte_size(peripheral_uart_h uart, peripheral_uart_byte_size_e byte_size)
137 GError *error = NULL;
138 gint32 ret = PERIPHERAL_ERROR_NONE;
140 if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
142 if (peripheral_io_gdbus_uart_call_set_byte_size_sync(
149 _E("Error in %s() : %s", __func__, error->message);
151 return PERIPHERAL_ERROR_UNKNOWN;
157 int peripheral_gdbus_uart_set_parity(peripheral_uart_h uart, peripheral_uart_parity_e parity)
159 GError *error = NULL;
160 gint32 ret = PERIPHERAL_ERROR_NONE;
162 if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
164 if (peripheral_io_gdbus_uart_call_set_parity_sync(
171 _E("Error in %s() : %s", __func__, error->message);
173 return PERIPHERAL_ERROR_UNKNOWN;
179 int peripheral_gdbus_uart_set_stop_bits(peripheral_uart_h uart, peripheral_uart_stop_bits_e stop_bits)
181 GError *error = NULL;
182 gint32 ret = PERIPHERAL_ERROR_NONE;
184 if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
186 if (peripheral_io_gdbus_uart_call_set_stop_bits_sync(
193 _E("Error in %s() : %s", __func__, error->message);
195 return PERIPHERAL_ERROR_UNKNOWN;
201 int peripheral_gdbus_uart_set_flow_control(peripheral_uart_h uart, bool xonxoff, bool rtscts)
203 GError *error = NULL;
204 gint32 ret = PERIPHERAL_ERROR_NONE;
206 if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
208 if (peripheral_io_gdbus_uart_call_set_flow_control_sync(
216 _E("Error in %s() : %s", __func__, error->message);
218 return PERIPHERAL_ERROR_UNKNOWN;
224 int peripheral_gdbus_uart_read(peripheral_uart_h uart, uint8_t *data, int length)
226 GError *error = NULL;
227 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
228 GVariant *data_array;
233 if (uart_proxy == NULL || data == NULL) return PERIPHERAL_ERROR_UNKNOWN;
235 if (peripheral_io_gdbus_uart_call_read_sync(
243 _E("Error in %s() : %s", __func__, error->message);
245 return PERIPHERAL_ERROR_UNKNOWN;
248 g_variant_get(data_array, "a(y)", &iter);
249 while (g_variant_iter_loop(iter, "(y)", &str)) {
251 if (i++ == length) break;
253 g_variant_iter_free(iter);
254 g_variant_unref(data_array);
259 int peripheral_gdbus_uart_write(peripheral_uart_h uart, uint8_t *data, int length)
261 GError *error = NULL;
262 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
263 GVariantBuilder *builder;
267 if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
269 builder = g_variant_builder_new(G_VARIANT_TYPE("a(y)"));
271 for (i = 0; i < length; i++)
272 g_variant_builder_add(builder, "(y)", data[i]);
273 g_variant_builder_add(builder, "(y)", 0x00);
275 g_data = g_variant_new("a(y)", builder);
276 g_variant_builder_unref(builder);
278 if (peripheral_io_gdbus_uart_call_write_sync(
286 _E("Error in %s() : %s", __func__, error->message);
288 return PERIPHERAL_ERROR_UNKNOWN;