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.
17 #include "peripheral_gdbus_gpio.h"
19 #define GPIO_FD_INDEX_DIRECTION 0
20 #define GPIO_FD_INDEX_EDGE 1
21 #define GPIO_FD_INDEX_VALUE 2
23 static PeripheralIoGdbusGpio *gpio_proxy = NULL;
25 static int __gpio_proxy_init(void)
29 if (gpio_proxy != NULL) {
30 _E("Gpio proxy is already created");
31 g_object_ref(gpio_proxy);
32 return PERIPHERAL_ERROR_NONE;
35 gpio_proxy = peripheral_io_gdbus_gpio_proxy_new_for_bus_sync(
37 G_DBUS_PROXY_FLAGS_NONE,
38 PERIPHERAL_GDBUS_NAME,
39 PERIPHERAL_GDBUS_GPIO_PATH,
43 if (gpio_proxy == NULL) {
44 _E("Failed to create gpio proxy : %s", error->message);
46 return PERIPHERAL_ERROR_UNKNOWN;
49 return PERIPHERAL_ERROR_NONE;
52 static int __gpio_proxy_deinit(void)
54 if (gpio_proxy == NULL) {
55 _E("Gpio proxy is NULL");
56 return PERIPHERAL_ERROR_UNKNOWN;
59 g_object_unref(gpio_proxy);
60 if (!G_IS_OBJECT(gpio_proxy))
63 return PERIPHERAL_ERROR_NONE;
66 int peripheral_gdbus_gpio_open(peripheral_gpio_h gpio)
70 GUnixFDList *fd_list = NULL;
72 ret = __gpio_proxy_init();
73 if (ret != PERIPHERAL_ERROR_NONE)
76 if (peripheral_io_gdbus_gpio_call_open_sync(
85 _E("Failed to request daemon to gpio open : %s", error->message);
87 return PERIPHERAL_ERROR_UNKNOWN;
90 // TODO : If ret is not PERIPHERAL_ERROR_NONE, fd list it NULL from daemon.
91 if (ret != PERIPHERAL_ERROR_NONE)
94 gpio->fd_direction = g_unix_fd_list_get(fd_list, GPIO_FD_INDEX_DIRECTION, &error);
95 if (gpio->fd_direction < 0) {
96 _E("Failed to get fd for gpio direction : %s", error->message);
98 ret = PERIPHERAL_ERROR_UNKNOWN;
101 gpio->fd_edge = g_unix_fd_list_get(fd_list, GPIO_FD_INDEX_EDGE, &error);
102 if (gpio->fd_edge < 0) {
103 _E("Failed to get fd for gpio edge : %s", error->message);
105 ret = PERIPHERAL_ERROR_UNKNOWN;
108 gpio->fd_value = g_unix_fd_list_get(fd_list, GPIO_FD_INDEX_VALUE, &error);
109 if (gpio->fd_value < 0) {
110 _E("Failed to get fd for gpio value : %s", error->message);
112 ret = PERIPHERAL_ERROR_UNKNOWN;
115 g_object_unref(fd_list);
120 int peripheral_gdbus_gpio_close(void)
122 int ret = __gpio_proxy_deinit();