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.
17 #include "peripheral_gdbus_pwm.h"
19 #define PWM_FD_INDEX_PERIOD 0
20 #define PWM_FD_INDEX_DUTY_CYCLE 1
21 #define PWM_FD_INDEX_POLARITY 2
22 #define PWM_FD_INDEX_ENABLE 3
24 static PeripheralIoGdbusPwm *pwm_proxy = NULL;
26 static int __pwm_proxy_init(void)
30 if (pwm_proxy != NULL) {
31 _E("Pwm proxy is already created");
32 g_object_ref(pwm_proxy);
33 return PERIPHERAL_ERROR_NONE;
36 pwm_proxy = peripheral_io_gdbus_pwm_proxy_new_for_bus_sync(
38 G_DBUS_PROXY_FLAGS_NONE,
39 PERIPHERAL_GDBUS_NAME,
40 PERIPHERAL_GDBUS_PWM_PATH,
44 if (pwm_proxy == NULL) {
45 _E("Failed to create pwm proxy : %s", error->message);
47 return PERIPHERAL_ERROR_UNKNOWN;
50 return PERIPHERAL_ERROR_NONE;
53 static int __pwm_proxy_deinit(void)
55 if (pwm_proxy == NULL) {
56 _E("Pwm proxy is NULL");
57 return PERIPHERAL_ERROR_UNKNOWN;
60 g_object_unref(pwm_proxy);
61 if (!G_IS_OBJECT(pwm_proxy))
64 return PERIPHERAL_ERROR_NONE;
67 int peripheral_gdbus_pwm_open(peripheral_pwm_h pwm, int chip, int pin)
71 GUnixFDList *fd_list = NULL;
73 ret = __pwm_proxy_init();
74 if (ret != PERIPHERAL_ERROR_NONE)
77 if (peripheral_io_gdbus_pwm_call_open_sync(
87 _E("Failed to request daemon to pwm open : %s", error->message);
89 return PERIPHERAL_ERROR_UNKNOWN;
92 // TODO : If ret is not PERIPHERAL_ERROR_NONE, fd list it NULL from daemon.
93 if (ret != PERIPHERAL_ERROR_NONE)
96 pwm->fd_period = g_unix_fd_list_get(fd_list, PWM_FD_INDEX_PERIOD, &error);
97 if (pwm->fd_period < 0) {
98 _E("Failed to get fd for pwm period : %s", error->message);
100 ret = PERIPHERAL_ERROR_UNKNOWN;
103 pwm->fd_duty_cycle = g_unix_fd_list_get(fd_list, PWM_FD_INDEX_DUTY_CYCLE, &error);
104 if (pwm->fd_duty_cycle < 0) {
105 _E("Failed to get fd for pwm duty cycle : %s", error->message);
107 ret = PERIPHERAL_ERROR_UNKNOWN;
110 pwm->fd_polarity = g_unix_fd_list_get(fd_list, PWM_FD_INDEX_POLARITY, &error);
111 if (pwm->fd_polarity < 0) {
112 _E("Failed to get fd for pwm polarity : %s", error->message);
114 ret = PERIPHERAL_ERROR_UNKNOWN;
117 pwm->fd_enable = g_unix_fd_list_get(fd_list, PWM_FD_INDEX_ENABLE, &error);
118 if (pwm->fd_enable < 0) {
119 _E("Failed to get fd for pwm enable : %s", error->message);
121 ret = PERIPHERAL_ERROR_UNKNOWN;
124 g_object_unref(fd_list);
129 int peripheral_gdbus_pwm_close(void)
131 int ret = __pwm_proxy_deinit();