2f2ead6af16fb46cdeaf356f868c662f0fa8b3c1
[platform/core/api/peripheral-io.git] / src / gdbus / peripheral_gdbus_gpio.c
1 /*
2  * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "peripheral_gdbus_gpio.h"
18
19 #define GPIO_FD_INDEX_DIRECTION 0
20 #define GPIO_FD_INDEX_EDGE      1
21 #define GPIO_FD_INDEX_VALUE     2
22
23 static PeripheralIoGdbusGpio *gpio_proxy = NULL;
24
25 static int __gpio_proxy_init(void)
26 {
27         GError *error = NULL;
28
29         if (gpio_proxy != NULL) {
30                 _E("Gpio proxy is already created");
31                 g_object_ref(gpio_proxy);
32                 return PERIPHERAL_ERROR_NONE;
33         }
34
35         gpio_proxy = peripheral_io_gdbus_gpio_proxy_new_for_bus_sync(
36                 G_BUS_TYPE_SYSTEM,
37                 G_DBUS_PROXY_FLAGS_NONE,
38                 PERIPHERAL_GDBUS_NAME,
39                 PERIPHERAL_GDBUS_GPIO_PATH,
40                 NULL,
41                 &error);
42
43         if (gpio_proxy == NULL) {
44                 _E("Failed to create gpio proxy : %s", error->message);
45                 g_error_free(error);
46                 return PERIPHERAL_ERROR_UNKNOWN;
47         }
48
49         return PERIPHERAL_ERROR_NONE;
50 }
51
52 static int __gpio_proxy_deinit(void)
53 {
54         if (gpio_proxy == NULL) {
55                 _E("Gpio proxy is NULL");
56                 return PERIPHERAL_ERROR_UNKNOWN;
57         }
58
59         g_object_unref(gpio_proxy);
60         if (!G_IS_OBJECT(gpio_proxy))
61                 gpio_proxy = NULL;
62
63         return PERIPHERAL_ERROR_NONE;
64 }
65
66 int peripheral_gdbus_gpio_open(peripheral_gpio_h gpio)
67 {
68         int ret;
69         GError *error = NULL;
70         GUnixFDList *fd_list = NULL;
71
72         ret = __gpio_proxy_init();
73         if (ret != PERIPHERAL_ERROR_NONE)
74                 return ret;
75
76         if (peripheral_io_gdbus_gpio_call_open_sync(
77                         gpio_proxy,
78                         gpio->pin,
79                         NULL,
80                         &gpio->handle,
81                         &ret,
82                         &fd_list,
83                         NULL,
84                         &error) == FALSE) {
85                 _E("Failed to request daemon to gpio open : %s", error->message);
86                 g_error_free(error);
87                 return PERIPHERAL_ERROR_UNKNOWN;
88         }
89
90         // TODO : If ret is not PERIPHERAL_ERROR_NONE, fd list it NULL from daemon.
91         if (ret != PERIPHERAL_ERROR_NONE)
92                 return ret;
93
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);
97                 g_error_free(error);
98                 ret = PERIPHERAL_ERROR_UNKNOWN;
99         }
100
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);
104                 g_error_free(error);
105                 ret = PERIPHERAL_ERROR_UNKNOWN;
106         }
107
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);
111                 g_error_free(error);
112                 ret = PERIPHERAL_ERROR_UNKNOWN;
113         }
114
115         g_object_unref(fd_list);
116
117         return ret;
118 }
119
120 int peripheral_gdbus_gpio_close(void)
121 {
122         int ret = __gpio_proxy_deinit();
123         return ret;
124 }