1274744f1591fc2475dffa02aa0a7e33e2502fe2
[platform/core/api/peripheral-io.git] / src / gdbus / peripheral_gdbus_pwm.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_pwm.h"
18
19 #define PWM_FD_INDEX_PERIOD      0
20 #define PWM_FD_INDEX_DUTY_CYCLE  1
21 #define PWM_FD_INDEX_POLARITY    2
22 #define PWM_FD_INDEX_ENABLE      3
23
24 static PeripheralIoGdbusPwm *pwm_proxy = NULL;
25
26 static int __pwm_proxy_init(void)
27 {
28         GError *error = NULL;
29
30         if (pwm_proxy != NULL) {
31                 _E("Pwm proxy is already created");
32                 g_object_ref(pwm_proxy);
33                 return PERIPHERAL_ERROR_NONE;
34         }
35
36         pwm_proxy = peripheral_io_gdbus_pwm_proxy_new_for_bus_sync(
37                 G_BUS_TYPE_SYSTEM,
38                 G_DBUS_PROXY_FLAGS_NONE,
39                 PERIPHERAL_GDBUS_NAME,
40                 PERIPHERAL_GDBUS_PWM_PATH,
41                 NULL,
42                 &error);
43
44         if (pwm_proxy == NULL) {
45                 _E("Failed to create pwm proxy : %s", error->message);
46                 g_error_free(error);
47                 return PERIPHERAL_ERROR_UNKNOWN;
48         }
49
50         return PERIPHERAL_ERROR_NONE;
51 }
52
53 static int __pwm_proxy_deinit(void)
54 {
55         if (pwm_proxy == NULL) {
56                 _E("Pwm proxy is NULL");
57                 return PERIPHERAL_ERROR_UNKNOWN;
58         }
59
60         g_object_unref(pwm_proxy);
61         if (!G_IS_OBJECT(pwm_proxy))
62                 pwm_proxy = NULL;
63
64         return PERIPHERAL_ERROR_NONE;
65 }
66
67 int peripheral_gdbus_pwm_open(peripheral_pwm_h pwm, int chip, int pin)
68 {
69         int ret;
70         GError *error = NULL;
71         GUnixFDList *fd_list = NULL;
72
73         ret = __pwm_proxy_init();
74         if (ret != PERIPHERAL_ERROR_NONE)
75                 return ret;
76
77         if (peripheral_io_gdbus_pwm_call_open_sync(
78                         pwm_proxy,
79                         chip,
80                         pin,
81                         NULL,
82                         &pwm->handle,
83                         &ret,
84                         &fd_list,
85                         NULL,
86                         &error) == FALSE) {
87                 _E("Failed to request daemon to pwm open : %s", error->message);
88                 g_error_free(error);
89                 return PERIPHERAL_ERROR_UNKNOWN;
90         }
91
92         // TODO : If ret is not PERIPHERAL_ERROR_NONE, fd list it NULL from daemon.
93         if (ret != PERIPHERAL_ERROR_NONE)
94                 return ret;
95
96         pwm->fd_period = g_unix_fd_list_get(fd_list, PWM_FD_INDEX_PERIOD, &error);
97         if (pwm->fd_period < 0) {
98                 _E("Failed to get fd for pwm period : %s", error->message);
99                 g_error_free(error);
100                 ret = PERIPHERAL_ERROR_UNKNOWN;
101         }
102
103         pwm->fd_duty_cycle = g_unix_fd_list_get(fd_list, PWM_FD_INDEX_DUTY_CYCLE, &error);
104         if (pwm->fd_duty_cycle < 0) {
105                 _E("Failed to get fd for pwm duty cycle : %s", error->message);
106                 g_error_free(error);
107                 ret = PERIPHERAL_ERROR_UNKNOWN;
108         }
109
110         pwm->fd_polarity = g_unix_fd_list_get(fd_list, PWM_FD_INDEX_POLARITY, &error);
111         if (pwm->fd_polarity < 0) {
112                 _E("Failed to get fd for pwm polarity : %s", error->message);
113                 g_error_free(error);
114                 ret = PERIPHERAL_ERROR_UNKNOWN;
115         }
116
117         pwm->fd_enable = g_unix_fd_list_get(fd_list, PWM_FD_INDEX_ENABLE, &error);
118         if (pwm->fd_enable < 0) {
119                 _E("Failed to get fd for pwm enable : %s", error->message);
120                 g_error_free(error);
121                 ret = PERIPHERAL_ERROR_UNKNOWN;
122         }
123
124         g_object_unref(fd_list);
125
126         return ret;
127 }
128
129 int peripheral_gdbus_pwm_close(void)
130 {
131         int ret = __pwm_proxy_deinit();
132         return ret;
133 }