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 static 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_set_baud_rate(peripheral_uart_h uart, peripheral_uart_baud_rate_e baud)
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_set_baud_rate_sync(
112 _E("Error in %s() : %s", __func__, error->message);
114 return PERIPHERAL_ERROR_UNKNOWN;
120 int peripheral_gdbus_uart_set_byte_size(peripheral_uart_h uart, peripheral_uart_byte_size_e byte_size)
122 GError *error = NULL;
123 gint32 ret = PERIPHERAL_ERROR_NONE;
125 if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
127 if (peripheral_io_gdbus_uart_call_set_byte_size_sync(
134 _E("Error in %s() : %s", __func__, error->message);
136 return PERIPHERAL_ERROR_UNKNOWN;
142 int peripheral_gdbus_uart_set_parity(peripheral_uart_h uart, peripheral_uart_parity_e parity)
144 GError *error = NULL;
145 gint32 ret = PERIPHERAL_ERROR_NONE;
147 if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
149 if (peripheral_io_gdbus_uart_call_set_parity_sync(
156 _E("Error in %s() : %s", __func__, error->message);
158 return PERIPHERAL_ERROR_UNKNOWN;
164 int peripheral_gdbus_uart_set_stop_bits(peripheral_uart_h uart, peripheral_uart_stop_bits_e stop_bits)
166 GError *error = NULL;
167 gint32 ret = PERIPHERAL_ERROR_NONE;
169 if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
171 if (peripheral_io_gdbus_uart_call_set_stop_bits_sync(
178 _E("Error in %s() : %s", __func__, error->message);
180 return PERIPHERAL_ERROR_UNKNOWN;
186 int peripheral_gdbus_uart_set_flow_control(peripheral_uart_h uart, bool xonxoff, bool rtscts)
188 GError *error = NULL;
189 gint32 ret = PERIPHERAL_ERROR_NONE;
191 if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
193 if (peripheral_io_gdbus_uart_call_set_flow_control_sync(
201 _E("Error in %s() : %s", __func__, error->message);
203 return PERIPHERAL_ERROR_UNKNOWN;
209 int peripheral_gdbus_uart_read(peripheral_uart_h uart, uint8_t *data, int length)
211 GError *error = NULL;
212 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
213 GVariant *data_array;
218 if (uart_proxy == NULL || data == NULL) return PERIPHERAL_ERROR_UNKNOWN;
220 if (peripheral_io_gdbus_uart_call_read_sync(
228 _E("Error in %s() : %s", __func__, error->message);
230 return PERIPHERAL_ERROR_UNKNOWN;
233 g_variant_get(data_array, "a(y)", &iter);
234 while (g_variant_iter_loop(iter, "(y)", &str)) {
236 if (i++ == length) break;
238 g_variant_iter_free(iter);
239 g_variant_unref(data_array);
244 int peripheral_gdbus_uart_write(peripheral_uart_h uart, uint8_t *data, int length)
246 GError *error = NULL;
247 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
248 GVariantBuilder *builder;
252 if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
254 builder = g_variant_builder_new(G_VARIANT_TYPE("a(y)"));
256 for (i = 0; i < length; i++)
257 g_variant_builder_add(builder, "(y)", data[i]);
258 g_variant_builder_add(builder, "(y)", 0x00);
260 g_data = g_variant_new("a(y)", builder);
261 g_variant_builder_unref(builder);
263 if (peripheral_io_gdbus_uart_call_write_sync(
271 _E("Error in %s() : %s", __func__, error->message);
273 return PERIPHERAL_ERROR_UNKNOWN;