b32074e4ddf9361bea1ec175092f25bd43060bcf
[platform/core/api/peripheral-io.git] / src / 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
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 PeripheralIoGdbusPwm *pwm_proxy = NULL;
27
28 void pwm_proxy_init(void)
29 {
30         GError *error = NULL;
31
32         if (pwm_proxy != NULL) {
33                 g_object_ref(pwm_proxy);
34                 return;
35         }
36
37         pwm_proxy = peripheral_io_gdbus_pwm_proxy_new_for_bus_sync(
38                 G_BUS_TYPE_SYSTEM,
39                 G_DBUS_PROXY_FLAGS_NONE,
40                 PERIPHERAL_GDBUS_NAME,
41                 PERIPHERAL_GDBUS_PWM_PATH,
42                 NULL,
43                 &error);
44 }
45
46 void pwm_proxy_deinit()
47 {
48         if (pwm_proxy) {
49                 g_object_unref(pwm_proxy);
50                 if (!G_IS_OBJECT(pwm_proxy))
51                         pwm_proxy = NULL;
52         }
53 }
54
55 int peripheral_gdbus_pwm_open(peripheral_pwm_h pwm, int device, int channel)
56 {
57         GError *error = NULL;
58         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
59
60         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
61
62         if (peripheral_io_gdbus_pwm_call_open_sync(
63                         pwm_proxy,
64                         device,
65                         channel,
66                         &pwm->handle,
67                         &ret,
68                         NULL,
69                         &error) == FALSE) {
70                 _E("%s", error->message);
71                 g_error_free(error);
72                 return PERIPHERAL_ERROR_UNKNOWN;
73         }
74
75         return ret;
76 }
77
78 int peripheral_gdbus_pwm_close(peripheral_pwm_h pwm)
79 {
80         GError *error = NULL;
81         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
82
83         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
84
85         if (peripheral_io_gdbus_pwm_call_close_sync(
86                         pwm_proxy,
87                         pwm->handle,
88                         &ret,
89                         NULL,
90                         &error) == FALSE) {
91                 _E("%s", error->message);
92                 g_error_free(error);
93                 return PERIPHERAL_ERROR_UNKNOWN;
94         }
95
96         return ret;
97 }
98
99 int peripheral_gdbus_pwm_set_period(peripheral_pwm_h pwm, int period)
100 {
101         GError *error = NULL;
102         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
103
104         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
105
106         if (peripheral_io_gdbus_pwm_call_set_period_sync(
107                         pwm_proxy,
108                         pwm->handle,
109                         period,
110                         &ret,
111                         NULL,
112                         &error) == FALSE) {
113                 _E("%s", error->message);
114                 g_error_free(error);
115                 return PERIPHERAL_ERROR_UNKNOWN;
116         }
117
118         return ret;
119 }
120
121 int peripheral_gdbus_pwm_get_period(peripheral_pwm_h pwm, int *period)
122 {
123         GError *error = NULL;
124         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
125         gint value = 0;
126
127         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
128
129         if (peripheral_io_gdbus_pwm_call_get_period_sync(
130                         pwm_proxy,
131                         pwm->handle,
132                         &value,
133                         &ret,
134                         NULL,
135                         &error) == FALSE) {
136                 _E("%s", error->message);
137                 g_error_free(error);
138                 return PERIPHERAL_ERROR_UNKNOWN;
139         }
140
141         *period = value;
142
143         return ret;
144 }
145
146 int peripheral_gdbus_pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle)
147 {
148         GError *error = NULL;
149         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
150
151         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
152
153         if (peripheral_io_gdbus_pwm_call_set_duty_cycle_sync(
154                         pwm_proxy,
155                         pwm->handle,
156                         duty_cycle,
157                         &ret,
158                         NULL,
159                         &error) == FALSE) {
160                 _E("%s", error->message);
161                 g_error_free(error);
162                 return PERIPHERAL_ERROR_UNKNOWN;
163         }
164
165         return ret;
166 }
167
168 int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle)
169 {
170         GError *error = NULL;
171         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
172         gint value = 0;
173
174         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
175
176         if (peripheral_io_gdbus_pwm_call_get_duty_cycle_sync(
177                         pwm_proxy,
178                         pwm->handle,
179                         &value,
180                         &ret,
181                         NULL,
182                         &error) == FALSE) {
183                 _E("%s", error->message);
184                 g_error_free(error);
185                 return PERIPHERAL_ERROR_UNKNOWN;
186         }
187
188         *duty_cycle = value;
189
190         return ret;
191 }
192
193 int peripheral_gdbus_pwm_set_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e polarity)
194 {
195         GError *error = NULL;
196         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
197
198         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
199
200         if (peripheral_io_gdbus_pwm_call_set_polarity_sync(
201                         pwm_proxy,
202                         pwm->handle,
203                         polarity,
204                         &ret,
205                         NULL,
206                         &error) == FALSE) {
207                 _E("%s", error->message);
208                 g_error_free(error);
209                 return PERIPHERAL_ERROR_UNKNOWN;
210         }
211
212         return ret;
213 }
214
215 int peripheral_gdbus_pwm_get_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e *polarity)
216 {
217         GError *error = NULL;
218         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
219         gint value = 0;
220
221         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
222
223         if (peripheral_io_gdbus_pwm_call_get_polarity_sync(
224                         pwm_proxy,
225                         pwm->handle,
226                         &value,
227                         &ret,
228                         NULL,
229                         &error) == FALSE) {
230                 _E("%s", error->message);
231                 g_error_free(error);
232                 return PERIPHERAL_ERROR_UNKNOWN;
233         }
234
235         if (!value)
236                 *polarity = PERIPHERAL_PWM_POLARITY_NORMAL;
237         else
238                 *polarity = PERIPHERAL_PWM_POLARITY_INVERSED;
239
240         return ret;
241 }
242
243 int peripheral_gdbus_pwm_set_enable(peripheral_pwm_h pwm, bool enable)
244 {
245         GError *error = NULL;
246         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
247
248         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
249
250         if (peripheral_io_gdbus_pwm_call_set_enable_sync(
251                         pwm_proxy,
252                         pwm->handle,
253                         enable,
254                         &ret,
255                         NULL,
256                         &error) == FALSE) {
257                 _E("%s", error->message);
258                 g_error_free(error);
259                 return PERIPHERAL_ERROR_UNKNOWN;
260         }
261
262         return ret;
263 }
264
265 int peripheral_gdbus_pwm_get_enable(peripheral_pwm_h pwm, bool *enable)
266 {
267         GError *error = NULL;
268         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
269         gboolean value = 0;
270
271         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
272
273         if (peripheral_io_gdbus_pwm_call_get_enable_sync(
274                         pwm_proxy,
275                         pwm->handle,
276                         &value,
277                         &ret,
278                         NULL,
279                         &error) == FALSE) {
280                 _E("%s", error->message);
281                 g_error_free(error);
282                 return PERIPHERAL_ERROR_UNKNOWN;
283         }
284
285         if (!value)
286                 *enable = false;
287         else
288                 *enable = true;
289
290         return ret;
291 }
292