1eb34359cc1292ae569f1711d13311bf04977878
[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 <stdio.h>
18 #include <stdlib.h>
19 #include <gio/gunixfdlist.h>
20
21 #include "peripheral_io.h"
22 #include "peripheral_gdbus.h"
23 #include "peripheral_common.h"
24 #include "peripheral_internal.h"
25 #include "peripheral_io_gdbus.h"
26
27 #define PWM_FD_INDEX_PERIOD      0
28 #define PWM_FD_INDEX_DUTY_CYCLE  1
29 #define PWM_FD_INDEX_POLARITY    2
30 #define PWM_FD_INDEX_ENABLE      3
31
32 static PeripheralIoGdbusPwm *pwm_proxy = NULL;
33
34 void pwm_proxy_init(void)
35 {
36         GError *error = NULL;
37
38         if (pwm_proxy != NULL) {
39                 g_object_ref(pwm_proxy);
40                 return;
41         }
42
43         pwm_proxy = peripheral_io_gdbus_pwm_proxy_new_for_bus_sync(
44                 G_BUS_TYPE_SYSTEM,
45                 G_DBUS_PROXY_FLAGS_NONE,
46                 PERIPHERAL_GDBUS_NAME,
47                 PERIPHERAL_GDBUS_PWM_PATH,
48                 NULL,
49                 &error);
50 }
51
52 void pwm_proxy_deinit()
53 {
54         if (pwm_proxy) {
55                 g_object_unref(pwm_proxy);
56                 if (!G_IS_OBJECT(pwm_proxy))
57                         pwm_proxy = NULL;
58         }
59 }
60
61 int peripheral_gdbus_pwm_open(peripheral_pwm_h pwm, int chip, int pin)
62 {
63         GError *error = NULL;
64         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
65         GUnixFDList *fd_list = NULL;
66
67         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
68
69         if (peripheral_io_gdbus_pwm_call_open_sync(
70                         pwm_proxy,
71                         chip,
72                         pin,
73                         NULL,
74                         &pwm->handle,
75                         &ret,
76                         &fd_list,
77                         NULL,
78                         &error) == FALSE) {
79                 _E("%s", error->message);
80                 g_error_free(error);
81                 return PERIPHERAL_ERROR_UNKNOWN;
82         }
83
84         pwm->fd_period = g_unix_fd_list_get(fd_list, PWM_FD_INDEX_PERIOD, &error);
85         if (pwm->fd_period < 0) {
86                 _E("Failed to get fd for pwm period : %s", error->message);
87                 g_error_free(error);
88                 ret = PERIPHERAL_ERROR_UNKNOWN;
89         }
90
91         pwm->fd_duty_cycle = g_unix_fd_list_get(fd_list, PWM_FD_INDEX_DUTY_CYCLE, &error);
92         if (pwm->fd_duty_cycle < 0) {
93                 _E("Failed to get fd for pwm duty cycle : %s", error->message);
94                 g_error_free(error);
95                 ret = PERIPHERAL_ERROR_UNKNOWN;
96         }
97
98         pwm->fd_polarity = g_unix_fd_list_get(fd_list, PWM_FD_INDEX_POLARITY, &error);
99         if (pwm->fd_polarity < 0) {
100                 _E("Failed to get fd for pwm polarity : %s", error->message);
101                 g_error_free(error);
102                 ret = PERIPHERAL_ERROR_UNKNOWN;
103         }
104
105         pwm->fd_enable = g_unix_fd_list_get(fd_list, PWM_FD_INDEX_ENABLE, &error);
106         if (pwm->fd_enable < 0) {
107                 _E("Failed to get fd for pwm enable : %s", error->message);
108                 g_error_free(error);
109                 ret = PERIPHERAL_ERROR_UNKNOWN;
110         }
111
112         g_object_unref(fd_list);
113
114         return ret;
115 }
116
117 int peripheral_gdbus_pwm_close(peripheral_pwm_h pwm)
118 {
119         GError *error = NULL;
120         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
121
122         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
123
124         if (peripheral_io_gdbus_pwm_call_close_sync(
125                         pwm_proxy,
126                         pwm->handle,
127                         &ret,
128                         NULL,
129                         &error) == FALSE) {
130                 _E("%s", error->message);
131                 g_error_free(error);
132                 return PERIPHERAL_ERROR_UNKNOWN;
133         }
134
135         return ret;
136 }
137
138 int peripheral_gdbus_pwm_set_period(peripheral_pwm_h pwm, int period)
139 {
140         GError *error = NULL;
141         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
142
143         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
144
145         if (peripheral_io_gdbus_pwm_call_set_period_sync(
146                         pwm_proxy,
147                         pwm->handle,
148                         period,
149                         &ret,
150                         NULL,
151                         &error) == FALSE) {
152                 _E("%s", error->message);
153                 g_error_free(error);
154                 return PERIPHERAL_ERROR_UNKNOWN;
155         }
156
157         return ret;
158 }
159
160 int peripheral_gdbus_pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle_ns)
161 {
162         GError *error = NULL;
163         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
164
165         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
166
167         if (peripheral_io_gdbus_pwm_call_set_duty_cycle_sync(
168                         pwm_proxy,
169                         pwm->handle,
170                         duty_cycle_ns,
171                         &ret,
172                         NULL,
173                         &error) == FALSE) {
174                 _E("%s", error->message);
175                 g_error_free(error);
176                 return PERIPHERAL_ERROR_UNKNOWN;
177         }
178
179         return ret;
180 }
181
182 int peripheral_gdbus_pwm_set_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e polarity)
183 {
184         GError *error = NULL;
185         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
186
187         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
188
189         if (peripheral_io_gdbus_pwm_call_set_polarity_sync(
190                         pwm_proxy,
191                         pwm->handle,
192                         polarity,
193                         &ret,
194                         NULL,
195                         &error) == FALSE) {
196                 _E("%s", error->message);
197                 g_error_free(error);
198                 return PERIPHERAL_ERROR_UNKNOWN;
199         }
200
201         return ret;
202 }
203
204 int peripheral_gdbus_pwm_set_enable(peripheral_pwm_h pwm, bool enable)
205 {
206         GError *error = NULL;
207         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
208
209         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
210
211         if (peripheral_io_gdbus_pwm_call_set_enable_sync(
212                         pwm_proxy,
213                         pwm->handle,
214                         enable,
215                         &ret,
216                         NULL,
217                         &error) == FALSE) {
218                 _E("%s", error->message);
219                 g_error_free(error);
220                 return PERIPHERAL_ERROR_UNKNOWN;
221         }
222
223         return ret;
224 }