42025de30c087fcda7e56637b5fd1517b0a6053d
[platform/core/api/peripheral-io.git] / src / 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 <stdio.h>
18 #include <stdlib.h>
19
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"
25
26 extern int peripheral_gpio_interrupted_cb_handler(int pin, int value, unsigned long long timestamp, int err);
27
28 static PeripheralIoGdbusGpio *gpio_proxy = NULL;
29
30 static void __peripheral_gpio_interrupted_cb(PeripheralIoGdbusGpio *gpio, gint pin, gint value, guint64 timestamp, gpointer user_data)
31 {
32         int err = PERIPHERAL_ERROR_NONE;
33         if (!gpio)
34                 err = PERIPHERAL_ERROR_IO_ERROR;
35
36         peripheral_gpio_interrupted_cb_handler(pin, value, timestamp, err);
37 }
38
39 void gpio_proxy_init(void)
40 {
41         GError *error = NULL;
42
43         if (gpio_proxy != NULL) {
44                 g_object_ref(gpio_proxy);
45                 return;
46         }
47
48         gpio_proxy = peripheral_io_gdbus_gpio_proxy_new_for_bus_sync(
49                 G_BUS_TYPE_SYSTEM,
50                 G_DBUS_PROXY_FLAGS_NONE,
51                 PERIPHERAL_GDBUS_NAME,
52                 PERIPHERAL_GDBUS_GPIO_PATH,
53                 NULL,
54                 &error);
55         if (gpio_proxy == NULL) {
56                 _E("Can not create gpio proxy : %s", error->message);
57                 g_error_free(error);
58                 return;
59         }
60
61         g_signal_connect(gpio_proxy,
62                         "interrupted-cb",
63                         G_CALLBACK(__peripheral_gpio_interrupted_cb),
64                         NULL);
65 }
66
67 void gpio_proxy_deinit()
68 {
69         if (gpio_proxy) {
70                 g_object_unref(gpio_proxy);
71                 if (!G_IS_OBJECT(gpio_proxy))
72                         gpio_proxy = NULL;
73         }
74 }
75
76 int peripheral_gdbus_gpio_open(peripheral_gpio_h gpio)
77 {
78         GError *error = NULL;
79         gint32 ret = PERIPHERAL_ERROR_NONE;
80
81         if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
82
83         if (peripheral_io_gdbus_gpio_call_open_sync(
84                         gpio_proxy,
85                         gpio->pin,
86                         &gpio->handle,
87                         &ret,
88                         NULL,
89                         &error) == FALSE) {
90                 _E("Error in %s() : %s", __func__, error->message);
91                 g_error_free(error);
92                 return PERIPHERAL_ERROR_UNKNOWN;
93         }
94
95         return ret;
96 }
97
98 int peripheral_gdbus_gpio_close(peripheral_gpio_h gpio)
99 {
100         GError *error = NULL;
101         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
102
103         if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
104
105         if (peripheral_io_gdbus_gpio_call_close_sync(
106                         gpio_proxy,
107                         gpio->handle,
108                         &ret,
109                         NULL,
110                         &error) == FALSE) {
111                 _E("Error in %s() : %s", __func__, error->message);
112                 g_error_free(error);
113                 return PERIPHERAL_ERROR_UNKNOWN;
114         }
115
116         return ret;
117 }
118
119 int peripheral_gdbus_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e direction)
120 {
121         GError *error = NULL;
122         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
123
124         if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
125
126         if (peripheral_io_gdbus_gpio_call_set_direction_sync(
127                         gpio_proxy,
128                         gpio->handle,
129                         direction,
130                         &ret,
131                         NULL,
132                         &error) == FALSE) {
133                 _E("Error in %s() : %s", __func__, error->message);
134                 g_error_free(error);
135                 return PERIPHERAL_ERROR_UNKNOWN;
136         }
137
138         return ret;
139 }
140
141 int peripheral_gdbus_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e edge)
142 {
143         GError *error = NULL;
144         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
145
146         if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
147
148         if (peripheral_io_gdbus_gpio_call_set_edge_mode_sync(
149                         gpio_proxy,
150                         gpio->handle,
151                         edge,
152                         &ret,
153                         NULL,
154                         &error) == FALSE) {
155                 _E("Error in %s() : %s", __func__, error->message);
156                 g_error_free(error);
157                 return PERIPHERAL_ERROR_UNKNOWN;
158         }
159
160         return ret;
161 }
162
163 int peripheral_gdbus_gpio_set_interrupted_cb(peripheral_gpio_h gpio, peripheral_gpio_interrupted_cb callback, void *user_data)
164 {
165         GError *error = NULL;
166         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
167
168         if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
169
170         if (peripheral_io_gdbus_gpio_call_set_interrupted_cb_sync(
171                         gpio_proxy,
172                         gpio->handle,
173                         &ret,
174                         NULL,
175                         &error) == FALSE) {
176                 _E("Error in %s() : %s", __func__, error->message);
177                 g_error_free(error);
178                 return PERIPHERAL_ERROR_UNKNOWN;
179         }
180
181         return ret;
182 }
183
184 int peripheral_gdbus_gpio_unset_interrupted_cb(peripheral_gpio_h gpio)
185 {
186         GError *error = NULL;
187         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
188
189         if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
190
191         if (peripheral_io_gdbus_gpio_call_unset_interrupted_cb_sync(
192                         gpio_proxy,
193                         gpio->handle,
194                         &ret,
195                         NULL,
196                         &error) == FALSE) {
197                 _E("Error in %s() : %s", __func__, error->message);
198                 g_error_free(error);
199                 return PERIPHERAL_ERROR_UNKNOWN;
200         }
201
202         return ret;
203 }
204
205 int peripheral_gdbus_gpio_read(peripheral_gpio_h gpio, int *value)
206 {
207         GError *error = NULL;
208         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
209
210         if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
211
212         if (peripheral_io_gdbus_gpio_call_read_sync(
213                         gpio_proxy,
214                         gpio->handle,
215                         value,
216                         &ret,
217                         NULL,
218                         &error) == FALSE) {
219                 _E("Error in %s() : %s", __func__, error->message);
220                 g_error_free(error);
221                 return PERIPHERAL_ERROR_UNKNOWN;
222         }
223
224         return ret;
225 }
226
227 int peripheral_gdbus_gpio_write(peripheral_gpio_h gpio, int value)
228 {
229         GError *error = NULL;
230         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
231
232         if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
233
234         if (peripheral_io_gdbus_gpio_call_write_sync(
235                         gpio_proxy,
236                         gpio->handle,
237                         value,
238                         &ret,
239                         NULL,
240                         &error) == FALSE) {
241                 _E("Error in %s() : %s", __func__, error->message);
242                 g_error_free(error);
243                 return PERIPHERAL_ERROR_UNKNOWN;
244         }
245
246         return ret;
247 }