e1fdae98b0d32032d2523c2df5c791250aa54d90
[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 static 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 chip, int pin)
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                         chip,
65                         pin,
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_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle_ns)
122 {
123         GError *error = NULL;
124         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
125
126         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
127
128         if (peripheral_io_gdbus_pwm_call_set_duty_cycle_sync(
129                         pwm_proxy,
130                         pwm->handle,
131                         duty_cycle_ns,
132                         &ret,
133                         NULL,
134                         &error) == FALSE) {
135                 _E("%s", error->message);
136                 g_error_free(error);
137                 return PERIPHERAL_ERROR_UNKNOWN;
138         }
139
140         return ret;
141 }
142
143 int peripheral_gdbus_pwm_set_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e polarity)
144 {
145         GError *error = NULL;
146         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
147
148         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
149
150         if (peripheral_io_gdbus_pwm_call_set_polarity_sync(
151                         pwm_proxy,
152                         pwm->handle,
153                         polarity,
154                         &ret,
155                         NULL,
156                         &error) == FALSE) {
157                 _E("%s", error->message);
158                 g_error_free(error);
159                 return PERIPHERAL_ERROR_UNKNOWN;
160         }
161
162         return ret;
163 }
164
165 int peripheral_gdbus_pwm_set_enable(peripheral_pwm_h pwm, bool enable)
166 {
167         GError *error = NULL;
168         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
169
170         if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
171
172         if (peripheral_io_gdbus_pwm_call_set_enable_sync(
173                         pwm_proxy,
174                         pwm->handle,
175                         enable,
176                         &ret,
177                         NULL,
178                         &error) == FALSE) {
179                 _E("%s", error->message);
180                 g_error_free(error);
181                 return PERIPHERAL_ERROR_UNKNOWN;
182         }
183
184         return ret;
185 }