e685b71cdf3ceabea51dfea0a5469b9172ff7321
[platform/core/api/peripheral-io.git] / src / peripheral_dbus.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 #include <gio/gio.h>
20
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"
26
27 PeripheralIoGdbusGpio *gpio_proxy = NULL;
28 PeripheralIoGdbusI2c *i2c_proxy = NULL;
29 PeripheralIoGdbusPwm *pwm_proxy = NULL;
30
31 void gpio_proxy_init(void)
32 {
33         GError *error = NULL;
34
35         if (gpio_proxy != NULL)
36                 return;
37
38         gpio_proxy = peripheral_io_gdbus_gpio_proxy_new_for_bus_sync(
39                 G_BUS_TYPE_SYSTEM,
40                 G_DBUS_PROXY_FLAGS_NONE,
41                 PERIPHERAL_DBUS_NAME,
42                 PERIPHERAL_DBUS_GPIO_PATH,
43                 NULL,
44                 &error);
45 }
46
47 void i2c_proxy_init(void)
48 {
49         GError *error = NULL;
50
51         if (i2c_proxy != NULL)
52                 return;
53
54         i2c_proxy = peripheral_io_gdbus_i2c_proxy_new_for_bus_sync(
55                 G_BUS_TYPE_SYSTEM,
56                 G_DBUS_PROXY_FLAGS_NONE,
57                 PERIPHERAL_DBUS_NAME,
58                 PERIPHERAL_DBUS_I2C_PATH,
59                 NULL,
60                 &error);
61 }
62
63 void pwm_proxy_init(void)
64 {
65         GError *error = NULL;
66
67         if (pwm_proxy != NULL)
68                 return;
69
70         pwm_proxy = peripheral_io_gdbus_pwm_proxy_new_for_bus_sync(
71                 G_BUS_TYPE_SYSTEM,
72                 G_DBUS_PROXY_FLAGS_NONE,
73                 PERIPHERAL_DBUS_NAME,
74                 PERIPHERAL_DBUS_PWM_PATH,
75                 NULL,
76                 &error);
77 }
78
79 void gpio_proxy_deinit()
80 {
81         if (gpio_proxy) {
82                 g_object_unref(gpio_proxy);
83                 gpio_proxy = NULL;
84         }
85 }
86
87 void i2c_proxy_deinit()
88 {
89         if (i2c_proxy) {
90                 g_object_unref(i2c_proxy);
91                 i2c_proxy = NULL;
92         }
93 }
94
95 void pwm_proxy_deinit()
96 {
97         if (pwm_proxy) {
98                 g_object_unref(pwm_proxy);
99                 pwm_proxy = NULL;
100         }
101 }
102
103 int peripheral_dbus_gpio_open(peripheral_gpio_h gpio)
104 {
105         GError *error = NULL;
106         gint32 ret = PERIPHERAL_ERROR_NONE;
107
108         if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
109
110         if (peripheral_io_gdbus_gpio_call_open_sync(
111                         gpio_proxy,
112                         gpio->pin,
113                         (gint*)&gpio->edge,
114                         (gint*)&gpio->direction,
115                         &ret,
116                         NULL,
117                         &error) == FALSE) {
118                 _E("Error in %s() : %s\n", __func__, error->message);
119                 g_error_free(error);
120                 return PERIPHERAL_ERROR_UNKNOWN;
121         }
122
123         return ret;
124 }
125
126 int peripheral_dbus_gpio_close(peripheral_gpio_h gpio)
127 {
128         GError *error = NULL;
129         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
130
131         if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
132
133         if (peripheral_io_gdbus_gpio_call_close_sync(
134                         gpio_proxy,
135                         gpio->pin,
136                         &ret,
137                         NULL,
138                         &error) == FALSE) {
139                 _E("Error in %s() : %s\n", __func__, error->message);
140                 g_error_free(error);
141                 return PERIPHERAL_ERROR_UNKNOWN;
142         }
143
144         return ret;
145 }
146
147 int peripheral_dbus_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e *direction)
148 {
149         GError *error = NULL;
150         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
151
152         if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
153
154         if (peripheral_io_gdbus_gpio_call_get_direction_sync(
155                         gpio_proxy,
156                         gpio->pin,
157                         (gint*)direction,
158                         &ret,
159                         NULL,
160                         &error) == FALSE) {
161                 _E("Error in %s() : %s\n", __func__, error->message);
162                 g_error_free(error);
163                 return PERIPHERAL_ERROR_UNKNOWN;
164         }
165         gpio->direction = *direction;
166
167         return ret;
168 }
169
170 int peripheral_dbus_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e direction)
171 {
172         GError *error = NULL;
173         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
174
175         if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
176
177         if (peripheral_io_gdbus_gpio_call_set_direction_sync(
178                         gpio_proxy,
179                         gpio->pin,
180                         direction,
181                         &ret,
182                         NULL,
183                         &error) == FALSE) {
184                 _E("Error in %s() : %s\n", __func__, error->message);
185                 g_error_free(error);
186                 return PERIPHERAL_ERROR_UNKNOWN;
187         }
188         gpio->direction = direction;
189
190         return ret;
191 }
192
193 int peripheral_dbus_gpio_read(peripheral_gpio_h gpio, int *value)
194 {
195         GError *error = NULL;
196         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
197
198         if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
199
200         if (peripheral_io_gdbus_gpio_call_read_sync(
201                         gpio_proxy,
202                         gpio->pin,
203                         value,
204                         &ret,
205                         NULL,
206                         &error) == FALSE) {
207                 _E("Error in %s() : %s\n", __func__, error->message);
208                 g_error_free(error);
209                 return PERIPHERAL_ERROR_UNKNOWN;
210         }
211
212         return ret;
213 }
214
215 int peripheral_dbus_gpio_write(peripheral_gpio_h gpio, int value)
216 {
217         GError *error = NULL;
218         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
219
220         if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
221
222         if (peripheral_io_gdbus_gpio_call_write_sync(
223                         gpio_proxy,
224                         gpio->pin,
225                         value,
226                         &ret,
227                         NULL,
228                         &error) == FALSE) {
229                 _E("Error in %s() : %s\n", __func__, error->message);
230                 g_error_free(error);
231                 return PERIPHERAL_ERROR_UNKNOWN;
232         }
233
234         return ret;
235 }
236
237 int peripheral_dbus_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e *edge)
238 {
239         GError *error = NULL;
240         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
241
242         if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
243
244         if (peripheral_io_gdbus_gpio_call_get_edge_mode_sync(
245                         gpio_proxy,
246                         gpio->pin,
247                         (int*)edge,
248                         &ret,
249                         NULL,
250                         &error) == FALSE) {
251                 _E("Error in %s() : %s\n", __func__, error->message);
252                 g_error_free(error);
253                 return PERIPHERAL_ERROR_UNKNOWN;
254         }
255         gpio->edge = *edge;
256
257         return ret;
258 }
259
260 int peripheral_dbus_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e edge)
261 {
262         GError *error = NULL;
263         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
264
265         if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
266
267         if (peripheral_io_gdbus_gpio_call_set_edge_mode_sync(
268                         gpio_proxy,
269                         gpio->pin,
270                         edge,
271                         &ret,
272                         NULL,
273                         &error) == FALSE) {
274                 _E("Error in %s() : %s\n", __func__, error->message);
275                 g_error_free(error);
276                 return PERIPHERAL_ERROR_UNKNOWN;
277         }
278         gpio->edge = edge;
279
280         return ret;
281 }
282
283 int peripheral_dbus_i2c_open(peripheral_i2c_h i2c, int bus, int address)
284 {
285         GError *error = NULL;
286         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
287
288         if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
289
290         if (peripheral_io_gdbus_i2c_call_open_sync(
291                         i2c_proxy,
292                         bus,
293                         address,
294                         &i2c->fd,
295                         &ret,
296                         NULL,
297                         &error) == FALSE) {
298                 _E("Error in %s() : %s\n", __func__, error->message);
299                 g_error_free(error);
300                 return PERIPHERAL_ERROR_UNKNOWN;
301         }
302
303         return ret;
304 }
305
306 int peripheral_dbus_i2c_close(peripheral_i2c_h i2c)
307 {
308         GError *error = NULL;
309         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
310
311         if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
312
313         if (peripheral_io_gdbus_i2c_call_close_sync(
314                         i2c_proxy,
315                         i2c->fd,
316                         &ret,
317                         NULL,
318                         &error) == FALSE) {
319                 _E("Error in %s() : %s\n", __func__, error->message);
320                 g_error_free(error);
321                 return PERIPHERAL_ERROR_UNKNOWN;
322         }
323
324         return ret;
325 }
326
327 int peripheral_dbus_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length)
328 {
329         GError *error = NULL;
330         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
331         GVariant *data_array;
332         GVariantIter *iter;
333         guint8 str;
334         int i = 0;
335
336         if (i2c_proxy == NULL || data == NULL) return PERIPHERAL_ERROR_UNKNOWN;
337
338         if (peripheral_io_gdbus_i2c_call_read_sync(
339                         i2c_proxy,
340                         i2c->fd,
341                         length,
342                         &data_array,
343                         &ret,
344                         NULL,
345                         &error) == FALSE) {
346                 _E("Error in %s() : %s\n", __func__, error->message);
347                 g_error_free(error);
348                 return PERIPHERAL_ERROR_UNKNOWN;
349         }
350
351         g_variant_get(data_array, "a(y)", &iter);
352         while (g_variant_iter_loop(iter, "(y)", &str)) {
353                 data[i] = str;
354                 if (i++ == length) break;
355         }
356         g_variant_iter_free(iter);
357
358         return ret;
359 }
360
361 int peripheral_dbus_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length)
362 {
363         GError *error = NULL;
364         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
365         GVariantBuilder *builder;
366         GVariant *g_data;
367         int i = 0;
368
369         if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
370
371         builder = g_variant_builder_new(G_VARIANT_TYPE("a(y)"));
372
373         for (i = 0; i < length; i++)
374                 g_variant_builder_add(builder, "(y)", data[i]);
375         g_variant_builder_add(builder, "(y)", 0x00);
376
377         g_data = g_variant_new("a(y)", builder);
378         g_variant_builder_unref(builder);
379
380         if (peripheral_io_gdbus_i2c_call_write_sync(
381                         i2c_proxy,
382                         i2c->fd,
383                         length,
384                         g_data,
385                         &ret,
386                         NULL,
387                         &error) == FALSE) {
388                 _E("Error in %s() : %s\n", __func__, error->message);
389                 g_error_free(error);
390                 return PERIPHERAL_ERROR_UNKNOWN;
391         }
392
393         return ret;
394 }
395
396 int peripheral_dbus_pwm_open(peripheral_pwm_context_h dev, int device, int channel)
397 {
398         GError *error = NULL;
399         gint32 ret = PERIPHERAL_ERROR_NONE;
400
401         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
402
403         /* TODO: Need to reorganize arguments */
404         if (peripheral_io_gdbus_pwm_call_open_sync(
405                         pwm_proxy,
406                         device,
407                         channel,
408                         &ret,
409                         NULL,
410                         &error) == FALSE) {
411                 _E("Error in %s() : %s\n", __func__, error->message);
412                 g_error_free(error);
413                 return PERIPHERAL_ERROR_UNKNOWN;
414         }
415
416         return ret;
417 }
418
419 int peripheral_dbus_pwm_close(peripheral_pwm_context_h dev)
420 {
421         GError *error = NULL;
422         gint32 ret = PERIPHERAL_ERROR_NONE;
423
424         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
425
426         /* TODO: Need to reorganize arguments */
427         if (peripheral_io_gdbus_pwm_call_close_sync(
428                         pwm_proxy,
429                         dev->device,
430                         dev->channel,
431                         &ret,
432                         NULL,
433                         &error) == FALSE) {
434                 _E("Error in %s() : %s\n", __func__, error->message);
435                 g_error_free(error);
436                 return PERIPHERAL_ERROR_UNKNOWN;
437         }
438
439         return ret;
440 }
441
442 int peripheral_dbus_pwm_get_duty_cycle(peripheral_pwm_context_h dev, int *duty_cycle)
443 {
444         GError *error = NULL;
445         gint32 ret = PERIPHERAL_ERROR_NONE;
446
447         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
448
449         /* TODO: Need to reorganize arguments */
450         if (peripheral_io_gdbus_pwm_call_get_duty_cycle_sync(
451                         pwm_proxy,
452                         dev->device,
453                         dev->channel,
454                         duty_cycle,
455                         &ret,
456                         NULL,
457                         &error) == FALSE) {
458                 _E("Error in %s() : %s\n", __func__, error->message);
459                 g_error_free(error);
460                 return PERIPHERAL_ERROR_UNKNOWN;
461         }
462
463         return ret;
464 }
465 int peripheral_dbus_pwm_set_duty_cycle(peripheral_pwm_context_h dev, int duty_cycle)
466 {
467         GError *error = NULL;
468         gint32 ret = PERIPHERAL_ERROR_NONE;
469
470         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
471
472         /* TODO: Need to reorganize arguments */
473         if (peripheral_io_gdbus_pwm_call_set_duty_cycle_sync(
474                         pwm_proxy,
475                         dev->device,
476                         dev->channel,
477                         duty_cycle,
478                         &ret,
479                         NULL,
480                         &error) == FALSE) {
481                 _E("Error in %s() : %s\n", __func__, error->message);
482                 g_error_free(error);
483                 return PERIPHERAL_ERROR_UNKNOWN;
484         }
485
486         return ret;
487 }
488
489 int peripheral_dbus_pwm_get_period(peripheral_pwm_context_h dev, int *period)
490 {
491         GError *error = NULL;
492         gint32 ret = PERIPHERAL_ERROR_NONE;
493
494         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
495
496         /* TODO: Need to reorganize arguments */
497         if (peripheral_io_gdbus_pwm_call_get_period_sync(
498                         pwm_proxy,
499                         dev->device,
500                         dev->channel,
501                         period,
502                         &ret,
503                         NULL,
504                         &error) == FALSE) {
505                 _E("Error in %s() : %s\n", __func__, error->message);
506                 g_error_free(error);
507                 return PERIPHERAL_ERROR_UNKNOWN;
508         }
509
510         return ret;
511 }
512
513 int peripheral_dbus_pwm_set_period(peripheral_pwm_context_h dev, int period)
514 {
515         GError *error = NULL;
516         gint32 ret = PERIPHERAL_ERROR_NONE;
517
518         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
519
520         /* TODO: Need to reorganize arguments */
521         if (peripheral_io_gdbus_pwm_call_set_period_sync(
522                         pwm_proxy,
523                         dev->device,
524                         dev->channel,
525                         period,
526                         &ret,
527                         NULL,
528                         &error) == FALSE) {
529                 _E("Error in %s() : %s\n", __func__, error->message);
530                 g_error_free(error);
531                 return PERIPHERAL_ERROR_UNKNOWN;
532         }
533
534         return ret;
535 }
536
537 int peripheral_dbus_pwm_set_enable(peripheral_pwm_context_h dev, peripheral_pwm_state_e enable)
538 {
539         GError *error = NULL;
540         gint32 ret = PERIPHERAL_ERROR_NONE;
541
542         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
543
544         /* TODO: Need to reorganize arguments */
545         if (peripheral_io_gdbus_pwm_call_set_enable_sync(
546                         pwm_proxy,
547                         dev->device,
548                         dev->channel,
549                         enable,
550                         &ret,
551                         NULL,
552                         &error) == FALSE) {
553                 _E("Error in %s() : %s\n", __func__, error->message);
554                 g_error_free(error);
555                 return PERIPHERAL_ERROR_UNKNOWN;
556         }
557
558         return ret;
559 }