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.
21 #include "peripheral_io.h"
22 #include "peripheral_dbus.h"
23 #include "peripheral_common.h"
24 #include "peripheral_internal.h"
25 #include "peripheral_io_gdbus.h"
27 PeripheralIoGdbusGpio *gpio_proxy = NULL;
28 PeripheralIoGdbusI2c *i2c_proxy = NULL;
29 PeripheralIoGdbusPwm *pwm_proxy = NULL;
31 void gpio_proxy_init(void)
35 if (gpio_proxy != NULL)
38 gpio_proxy = peripheral_io_gdbus_gpio_proxy_new_for_bus_sync(
40 G_DBUS_PROXY_FLAGS_NONE,
42 PERIPHERAL_DBUS_GPIO_PATH,
47 void i2c_proxy_init(void)
51 if (i2c_proxy != NULL)
54 i2c_proxy = peripheral_io_gdbus_i2c_proxy_new_for_bus_sync(
56 G_DBUS_PROXY_FLAGS_NONE,
58 PERIPHERAL_DBUS_I2C_PATH,
63 void pwm_proxy_init(void)
67 if (pwm_proxy != NULL)
70 pwm_proxy = peripheral_io_gdbus_pwm_proxy_new_for_bus_sync(
72 G_DBUS_PROXY_FLAGS_NONE,
74 PERIPHERAL_DBUS_PWM_PATH,
79 void gpio_proxy_deinit()
82 g_object_unref(gpio_proxy);
87 void i2c_proxy_deinit()
90 g_object_unref(i2c_proxy);
95 void pwm_proxy_deinit()
98 g_object_unref(pwm_proxy);
103 int peripheral_dbus_gpio_open(peripheral_gpio_h gpio)
105 GError *error = NULL;
106 gint32 ret = PERIPHERAL_ERROR_NONE;
108 if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
110 if (peripheral_io_gdbus_gpio_call_open_sync(
114 (gint*)&gpio->direction,
118 _E("Error in %s() : %s\n", __func__, error->message);
120 return PERIPHERAL_ERROR_UNKNOWN;
126 int peripheral_dbus_gpio_close(peripheral_gpio_h gpio)
128 GError *error = NULL;
129 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
131 if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
133 if (peripheral_io_gdbus_gpio_call_close_sync(
139 _E("Error in %s() : %s\n", __func__, error->message);
141 return PERIPHERAL_ERROR_UNKNOWN;
147 int peripheral_dbus_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e *direction)
149 GError *error = NULL;
150 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
152 if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
154 if (peripheral_io_gdbus_gpio_call_get_direction_sync(
161 _E("Error in %s() : %s\n", __func__, error->message);
163 return PERIPHERAL_ERROR_UNKNOWN;
165 gpio->direction = *direction;
170 int peripheral_dbus_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e direction)
172 GError *error = NULL;
173 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
175 if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
177 if (peripheral_io_gdbus_gpio_call_set_direction_sync(
184 _E("Error in %s() : %s\n", __func__, error->message);
186 return PERIPHERAL_ERROR_UNKNOWN;
188 gpio->direction = direction;
193 int peripheral_dbus_gpio_read(peripheral_gpio_h gpio, int *value)
195 GError *error = NULL;
196 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
198 if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
200 if (peripheral_io_gdbus_gpio_call_read_sync(
207 _E("Error in %s() : %s\n", __func__, error->message);
209 return PERIPHERAL_ERROR_UNKNOWN;
215 int peripheral_dbus_gpio_write(peripheral_gpio_h gpio, int value)
217 GError *error = NULL;
218 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
220 if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
222 if (peripheral_io_gdbus_gpio_call_write_sync(
229 _E("Error in %s() : %s\n", __func__, error->message);
231 return PERIPHERAL_ERROR_UNKNOWN;
237 int peripheral_dbus_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e *edge)
239 GError *error = NULL;
240 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
242 if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
244 if (peripheral_io_gdbus_gpio_call_get_edge_mode_sync(
251 _E("Error in %s() : %s\n", __func__, error->message);
253 return PERIPHERAL_ERROR_UNKNOWN;
260 int peripheral_dbus_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e edge)
262 GError *error = NULL;
263 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
265 if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
267 if (peripheral_io_gdbus_gpio_call_set_edge_mode_sync(
274 _E("Error in %s() : %s\n", __func__, error->message);
276 return PERIPHERAL_ERROR_UNKNOWN;
283 int peripheral_dbus_i2c_open(peripheral_i2c_h i2c, int bus, int address)
285 GError *error = NULL;
286 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
288 if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
290 if (peripheral_io_gdbus_i2c_call_open_sync(
298 _E("Error in %s() : %s\n", __func__, error->message);
300 return PERIPHERAL_ERROR_UNKNOWN;
306 int peripheral_dbus_i2c_close(peripheral_i2c_h i2c)
308 GError *error = NULL;
309 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
311 if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
313 if (peripheral_io_gdbus_i2c_call_close_sync(
319 _E("Error in %s() : %s\n", __func__, error->message);
321 return PERIPHERAL_ERROR_UNKNOWN;
327 int peripheral_dbus_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length)
329 GError *error = NULL;
330 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
331 GVariant *data_array;
336 if (i2c_proxy == NULL || data == NULL) return PERIPHERAL_ERROR_UNKNOWN;
338 if (peripheral_io_gdbus_i2c_call_read_sync(
346 _E("Error in %s() : %s\n", __func__, error->message);
348 return PERIPHERAL_ERROR_UNKNOWN;
351 g_variant_get(data_array, "a(y)", &iter);
352 while (g_variant_iter_loop(iter, "(y)", &str)) {
354 if (i++ == length) break;
356 g_variant_iter_free(iter);
361 int peripheral_dbus_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length)
363 GError *error = NULL;
364 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
365 GVariantBuilder *builder;
369 if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
371 builder = g_variant_builder_new(G_VARIANT_TYPE("a(y)"));
373 for (i = 0; i < length; i++)
374 g_variant_builder_add(builder, "(y)", data[i]);
375 g_variant_builder_add(builder, "(y)", 0x00);
377 g_data = g_variant_new("a(y)", builder);
378 g_variant_builder_unref(builder);
380 if (peripheral_io_gdbus_i2c_call_write_sync(
388 _E("Error in %s() : %s\n", __func__, error->message);
390 return PERIPHERAL_ERROR_UNKNOWN;
396 int peripheral_dbus_pwm_open(peripheral_pwm_context_h dev, int device, int channel)
398 GError *error = NULL;
399 gint32 ret = PERIPHERAL_ERROR_NONE;
401 if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
403 /* TODO: Need to reorganize arguments */
404 if (peripheral_io_gdbus_pwm_call_open_sync(
411 _E("Error in %s() : %s\n", __func__, error->message);
413 return PERIPHERAL_ERROR_UNKNOWN;
419 int peripheral_dbus_pwm_close(peripheral_pwm_context_h dev)
421 GError *error = NULL;
422 gint32 ret = PERIPHERAL_ERROR_NONE;
424 if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
426 /* TODO: Need to reorganize arguments */
427 if (peripheral_io_gdbus_pwm_call_close_sync(
434 _E("Error in %s() : %s\n", __func__, error->message);
436 return PERIPHERAL_ERROR_UNKNOWN;
442 int peripheral_dbus_pwm_get_duty_cycle(peripheral_pwm_context_h dev, int *duty_cycle)
444 GError *error = NULL;
445 gint32 ret = PERIPHERAL_ERROR_NONE;
447 if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
449 /* TODO: Need to reorganize arguments */
450 if (peripheral_io_gdbus_pwm_call_get_duty_cycle_sync(
458 _E("Error in %s() : %s\n", __func__, error->message);
460 return PERIPHERAL_ERROR_UNKNOWN;
465 int peripheral_dbus_pwm_set_duty_cycle(peripheral_pwm_context_h dev, int duty_cycle)
467 GError *error = NULL;
468 gint32 ret = PERIPHERAL_ERROR_NONE;
470 if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
472 /* TODO: Need to reorganize arguments */
473 if (peripheral_io_gdbus_pwm_call_set_duty_cycle_sync(
481 _E("Error in %s() : %s\n", __func__, error->message);
483 return PERIPHERAL_ERROR_UNKNOWN;
489 int peripheral_dbus_pwm_get_period(peripheral_pwm_context_h dev, int *period)
491 GError *error = NULL;
492 gint32 ret = PERIPHERAL_ERROR_NONE;
494 if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
496 /* TODO: Need to reorganize arguments */
497 if (peripheral_io_gdbus_pwm_call_get_period_sync(
505 _E("Error in %s() : %s\n", __func__, error->message);
507 return PERIPHERAL_ERROR_UNKNOWN;
513 int peripheral_dbus_pwm_set_period(peripheral_pwm_context_h dev, int period)
515 GError *error = NULL;
516 gint32 ret = PERIPHERAL_ERROR_NONE;
518 if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
520 /* TODO: Need to reorganize arguments */
521 if (peripheral_io_gdbus_pwm_call_set_period_sync(
529 _E("Error in %s() : %s\n", __func__, error->message);
531 return PERIPHERAL_ERROR_UNKNOWN;
537 int peripheral_dbus_pwm_set_enable(peripheral_pwm_context_h dev, peripheral_pwm_state_e enable)
539 GError *error = NULL;
540 gint32 ret = PERIPHERAL_ERROR_NONE;
542 if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
544 /* TODO: Need to reorganize arguments */
545 if (peripheral_io_gdbus_pwm_call_set_enable_sync(
553 _E("Error in %s() : %s\n", __func__, error->message);
555 return PERIPHERAL_ERROR_UNKNOWN;