86962a606965439a47b4ae984a19b4a4dad9cb60
[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                 return;
34
35         pwm_proxy = peripheral_io_gdbus_pwm_proxy_new_for_bus_sync(
36                 G_BUS_TYPE_SYSTEM,
37                 G_DBUS_PROXY_FLAGS_NONE,
38                 PERIPHERAL_GDBUS_NAME,
39                 PERIPHERAL_GDBUS_PWM_PATH,
40                 NULL,
41                 &error);
42 }
43
44 void pwm_proxy_deinit()
45 {
46         if (pwm_proxy) {
47                 g_object_unref(pwm_proxy);
48                 pwm_proxy = NULL;
49         }
50 }
51
52 int peripheral_gdbus_pwm_open(peripheral_pwm_h dev, int device, int channel)
53 {
54         GError *error = NULL;
55         gint32 ret = PERIPHERAL_ERROR_NONE;
56
57         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
58
59         /* TODO: Need to reorganize arguments */
60         if (peripheral_io_gdbus_pwm_call_open_sync(
61                         pwm_proxy,
62                         device,
63                         channel,
64                         &ret,
65                         NULL,
66                         &error) == FALSE) {
67                 _E("Error in %s() : %s\n", __func__, error->message);
68                 g_error_free(error);
69                 return PERIPHERAL_ERROR_UNKNOWN;
70         }
71
72         return ret;
73 }
74
75 int peripheral_gdbus_pwm_close(peripheral_pwm_h dev)
76 {
77         GError *error = NULL;
78         gint32 ret = PERIPHERAL_ERROR_NONE;
79
80         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
81
82         /* TODO: Need to reorganize arguments */
83         if (peripheral_io_gdbus_pwm_call_close_sync(
84                         pwm_proxy,
85                         dev->device,
86                         dev->channel,
87                         &ret,
88                         NULL,
89                         &error) == FALSE) {
90                 _E("Error in %s() : %s\n", __func__, error->message);
91                 g_error_free(error);
92                 return PERIPHERAL_ERROR_UNKNOWN;
93         }
94
95         return ret;
96 }
97
98 int peripheral_gdbus_pwm_set_period(peripheral_pwm_h dev, int period)
99 {
100         GError *error = NULL;
101         gint32 ret = PERIPHERAL_ERROR_NONE;
102
103         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
104
105         /* TODO: Need to reorganize arguments */
106         if (peripheral_io_gdbus_pwm_call_set_period_sync(
107                         pwm_proxy,
108                         dev->device,
109                         dev->channel,
110                         period,
111                         &ret,
112                         NULL,
113                         &error) == FALSE) {
114                 _E("Error in %s() : %s\n", __func__, error->message);
115                 g_error_free(error);
116                 return PERIPHERAL_ERROR_UNKNOWN;
117         }
118
119         return ret;
120 }
121
122 int peripheral_gdbus_pwm_get_period(peripheral_pwm_h dev, int *period)
123 {
124         GError *error = NULL;
125         gint32 ret = PERIPHERAL_ERROR_NONE;
126
127         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
128
129         /* TODO: Need to reorganize arguments */
130         if (peripheral_io_gdbus_pwm_call_get_period_sync(
131                         pwm_proxy,
132                         dev->device,
133                         dev->channel,
134                         period,
135                         &ret,
136                         NULL,
137                         &error) == FALSE) {
138                 _E("Error in %s() : %s\n", __func__, error->message);
139                 g_error_free(error);
140                 return PERIPHERAL_ERROR_UNKNOWN;
141         }
142
143         return ret;
144 }
145
146 int peripheral_gdbus_pwm_set_duty_cycle(peripheral_pwm_h dev, int duty_cycle)
147 {
148         GError *error = NULL;
149         gint32 ret = PERIPHERAL_ERROR_NONE;
150
151         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
152
153         /* TODO: Need to reorganize arguments */
154         if (peripheral_io_gdbus_pwm_call_set_duty_cycle_sync(
155                         pwm_proxy,
156                         dev->device,
157                         dev->channel,
158                         duty_cycle,
159                         &ret,
160                         NULL,
161                         &error) == FALSE) {
162                 _E("Error in %s() : %s\n", __func__, error->message);
163                 g_error_free(error);
164                 return PERIPHERAL_ERROR_UNKNOWN;
165         }
166
167         return ret;
168 }
169
170 int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_h dev, int *duty_cycle)
171 {
172         GError *error = NULL;
173         gint32 ret = PERIPHERAL_ERROR_NONE;
174
175         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
176
177         /* TODO: Need to reorganize arguments */
178         if (peripheral_io_gdbus_pwm_call_get_duty_cycle_sync(
179                         pwm_proxy,
180                         dev->device,
181                         dev->channel,
182                         duty_cycle,
183                         &ret,
184                         NULL,
185                         &error) == FALSE) {
186                 _E("Error in %s() : %s\n", __func__, error->message);
187                 g_error_free(error);
188                 return PERIPHERAL_ERROR_UNKNOWN;
189         }
190
191         return ret;
192 }
193
194 int peripheral_gdbus_pwm_set_enable(peripheral_pwm_h dev, peripheral_pwm_state_e enable)
195 {
196         GError *error = NULL;
197         gint32 ret = PERIPHERAL_ERROR_NONE;
198
199         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
200
201         /* TODO: Need to reorganize arguments */
202         if (peripheral_io_gdbus_pwm_call_set_enable_sync(
203                         pwm_proxy,
204                         dev->device,
205                         dev->channel,
206                         enable,
207                         &ret,
208                         NULL,
209                         &error) == FALSE) {
210                 _E("Error in %s() : %s\n", __func__, error->message);
211                 g_error_free(error);
212                 return PERIPHERAL_ERROR_UNKNOWN;
213         }
214
215         return ret;
216 }