2 * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <gio/gunixfdlist.h>
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"
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
32 static PeripheralIoGdbusPwm *pwm_proxy = NULL;
34 void pwm_proxy_init(void)
38 if (pwm_proxy != NULL) {
39 g_object_ref(pwm_proxy);
43 pwm_proxy = peripheral_io_gdbus_pwm_proxy_new_for_bus_sync(
45 G_DBUS_PROXY_FLAGS_NONE,
46 PERIPHERAL_GDBUS_NAME,
47 PERIPHERAL_GDBUS_PWM_PATH,
52 void pwm_proxy_deinit()
55 g_object_unref(pwm_proxy);
56 if (!G_IS_OBJECT(pwm_proxy))
61 int peripheral_gdbus_pwm_open(peripheral_pwm_h pwm, int chip, int pin)
64 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
65 GUnixFDList *fd_list = NULL;
67 if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
69 if (peripheral_io_gdbus_pwm_call_open_sync(
79 _E("%s", error->message);
81 return PERIPHERAL_ERROR_UNKNOWN;
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);
88 ret = PERIPHERAL_ERROR_UNKNOWN;
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);
95 ret = PERIPHERAL_ERROR_UNKNOWN;
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);
102 ret = PERIPHERAL_ERROR_UNKNOWN;
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);
109 ret = PERIPHERAL_ERROR_UNKNOWN;
112 g_object_unref(fd_list);
117 int peripheral_gdbus_pwm_close(peripheral_pwm_h pwm)
119 GError *error = NULL;
120 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
122 if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
124 if (peripheral_io_gdbus_pwm_call_close_sync(
130 _E("%s", error->message);
132 return PERIPHERAL_ERROR_UNKNOWN;
138 int peripheral_gdbus_pwm_set_period(peripheral_pwm_h pwm, int period)
140 GError *error = NULL;
141 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
143 if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
145 if (peripheral_io_gdbus_pwm_call_set_period_sync(
152 _E("%s", error->message);
154 return PERIPHERAL_ERROR_UNKNOWN;
160 int peripheral_gdbus_pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle_ns)
162 GError *error = NULL;
163 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
165 if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
167 if (peripheral_io_gdbus_pwm_call_set_duty_cycle_sync(
174 _E("%s", error->message);
176 return PERIPHERAL_ERROR_UNKNOWN;
182 int peripheral_gdbus_pwm_set_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e polarity)
184 GError *error = NULL;
185 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
187 if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
189 if (peripheral_io_gdbus_pwm_call_set_polarity_sync(
196 _E("%s", error->message);
198 return PERIPHERAL_ERROR_UNKNOWN;
204 int peripheral_gdbus_pwm_set_enable(peripheral_pwm_h pwm, bool enable)
206 GError *error = NULL;
207 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
209 if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
211 if (peripheral_io_gdbus_pwm_call_set_enable_sync(
218 _E("%s", error->message);
220 return PERIPHERAL_ERROR_UNKNOWN;