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.
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"
26 PeripheralIoGdbusUart *uart_proxy = NULL;
28 void uart_proxy_init(void)
32 if (uart_proxy != NULL) {
33 g_object_ref(uart_proxy);
37 uart_proxy = peripheral_io_gdbus_uart_proxy_new_for_bus_sync(
39 G_DBUS_PROXY_FLAGS_NONE,
40 PERIPHERAL_GDBUS_NAME,
41 PERIPHERAL_GDBUS_UART_PATH,
46 void uart_proxy_deinit()
49 g_object_unref(uart_proxy);
50 if (!G_IS_OBJECT(uart_proxy))
55 int peripheral_gdbus_uart_open(peripheral_uart_h uart, int port)
58 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
60 if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
62 if (peripheral_io_gdbus_uart_call_open_sync(
69 _E("Error in %s() : %s", __func__, error->message);
71 return PERIPHERAL_ERROR_UNKNOWN;
77 int peripheral_gdbus_uart_close(peripheral_uart_h uart)
80 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
82 if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
84 if (peripheral_io_gdbus_uart_call_close_sync(
90 _E("Error in %s() : %s", __func__, error->message);
92 return PERIPHERAL_ERROR_UNKNOWN;
98 int peripheral_gdbus_uart_flush(peripheral_uart_h uart)
100 GError *error = NULL;
101 gint32 ret = PERIPHERAL_ERROR_NONE;
103 if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
105 if (peripheral_io_gdbus_uart_call_flush_sync(
111 _E("Error in %s() : %s", __func__, error->message);
113 return PERIPHERAL_ERROR_UNKNOWN;
119 int peripheral_gdbus_uart_set_baudrate(peripheral_uart_h uart, peripheral_uart_baudrate_e baud)
121 GError *error = NULL;
122 gint32 ret = PERIPHERAL_ERROR_NONE;
124 if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
126 if (peripheral_io_gdbus_uart_call_set_baudrate_sync(
133 _E("Error in %s() : %s", __func__, error->message);
135 return PERIPHERAL_ERROR_UNKNOWN;
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)
143 GError *error = NULL;
144 gint32 ret = PERIPHERAL_ERROR_NONE;
146 if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
148 if (peripheral_io_gdbus_uart_call_set_mode_sync(
157 _E("Error in %s() : %s", __func__, error->message);
159 return PERIPHERAL_ERROR_UNKNOWN;
165 int peripheral_gdbus_uart_set_flowcontrol(peripheral_uart_h uart, bool xonxoff, bool rtscts)
167 GError *error = NULL;
168 gint32 ret = PERIPHERAL_ERROR_NONE;
170 if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
172 if (peripheral_io_gdbus_uart_call_set_flowcontrol_sync(
180 _E("Error in %s() : %s", __func__, error->message);
182 return PERIPHERAL_ERROR_UNKNOWN;
188 int peripheral_gdbus_uart_read(peripheral_uart_h uart, uint8_t *data, int length)
190 GError *error = NULL;
191 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
192 GVariant *data_array;
197 if (uart_proxy == NULL || data == NULL) return PERIPHERAL_ERROR_UNKNOWN;
199 if (peripheral_io_gdbus_uart_call_read_sync(
207 _E("Error in %s() : %s", __func__, error->message);
209 return PERIPHERAL_ERROR_UNKNOWN;
212 g_variant_get(data_array, "a(y)", &iter);
213 while (g_variant_iter_loop(iter, "(y)", &str)) {
215 if (i++ == length) break;
217 g_variant_iter_free(iter);
218 g_variant_unref(data_array);
223 int peripheral_gdbus_uart_write(peripheral_uart_h uart, uint8_t *data, int length)
225 GError *error = NULL;
226 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
227 GVariantBuilder *builder;
231 if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
233 builder = g_variant_builder_new(G_VARIANT_TYPE("a(y)"));
235 for (i = 0; i < length; i++)
236 g_variant_builder_add(builder, "(y)", data[i]);
237 g_variant_builder_add(builder, "(y)", 0x00);
239 g_data = g_variant_new("a(y)", builder);
240 g_variant_builder_unref(builder);
242 if (peripheral_io_gdbus_uart_call_write_sync(
250 _E("Error in %s() : %s", __func__, error->message);
252 return PERIPHERAL_ERROR_UNKNOWN;