e3718eebc997ea558210a47066f1b11351d6296a
[platform/core/api/peripheral-io.git] / src / peripheral_pwm.c
1 /*
2  * Copyright (c) 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 #include <unistd.h>
20 #include <assert.h>
21
22 #include "peripheral_io.h"
23 #include "peripheral_gdbus_pwm.h"
24 #include "peripheral_common.h"
25 #include "peripheral_internal.h"
26
27 int peripheral_pwm_open(int device, int channel, peripheral_pwm_h *pwm)
28 {
29         peripheral_pwm_h handle;
30         int ret = PERIPHERAL_ERROR_NONE;
31
32         RETVM_IF(device < 0 || channel < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
33
34         /* Initialize */
35         handle = (peripheral_pwm_h)calloc(1, sizeof(struct _peripheral_pwm_s));
36
37         if (handle == NULL) {
38                 _E("Failed to allocate peripheral_pwm_h");
39                 return PERIPHERAL_ERROR_OUT_OF_MEMORY;
40         }
41
42         pwm_proxy_init();
43
44         ret = peripheral_gdbus_pwm_open(handle, device, channel);
45
46         if (ret != PERIPHERAL_ERROR_NONE) {
47                 _E("Failed to open PWM device : %d, channel : %d", device, channel);
48                 free(handle);
49                 handle = NULL;
50         }
51         *pwm = handle;
52
53         return ret;
54 }
55
56 int peripheral_pwm_close(peripheral_pwm_h pwm)
57 {
58         int ret = PERIPHERAL_ERROR_NONE;
59
60         RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL");
61
62         if ((ret = peripheral_gdbus_pwm_close(pwm)) < 0)
63                 _E("Failed to close PWM device, continuing anyway, ret : %d", ret);
64
65         pwm_proxy_deinit();
66         free(pwm);
67
68         return ret;
69 }
70
71 int peripheral_pwm_set_period(peripheral_pwm_h pwm, int period)
72 {
73         int ret;
74
75         RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL");
76
77         ret = peripheral_gdbus_pwm_set_period(pwm, period);
78         if (ret != PERIPHERAL_ERROR_NONE)
79                 _E("Failed to set period, ret : %d", ret);
80
81         return ret;
82 }
83
84 int peripheral_pwm_get_period(peripheral_pwm_h pwm, int *period)
85 {
86         int ret;
87
88         RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL");
89
90         ret = peripheral_gdbus_pwm_get_period(pwm, period);
91         if (ret != PERIPHERAL_ERROR_NONE)
92                 _E("Failed to get period, ret : %d", ret);
93
94         return ret;
95 }
96
97 int peripheral_pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle)
98 {
99         int ret;
100
101         RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL");
102
103         ret = peripheral_gdbus_pwm_set_duty_cycle(pwm, duty_cycle);
104         if (ret != PERIPHERAL_ERROR_NONE)
105                 _E("Failed to set duty cycle, ret : %d", ret);
106
107         return ret;
108 }
109
110 int peripheral_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle)
111 {
112         int ret;
113
114         RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL");
115
116         ret = peripheral_gdbus_pwm_get_duty_cycle(pwm, duty_cycle);
117         if (ret != PERIPHERAL_ERROR_NONE)
118                 _E("Failed to get duty cycle, ret : %d", ret);
119
120         return ret;
121 }
122
123 int peripheral_pwm_set_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e polarity)
124 {
125         int ret;
126
127         RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL");
128         RETVM_IF(polarity > PERIPHERAL_PWM_POLARITY_INVERSED, PERIPHERAL_ERROR_INVALID_PARAMETER,
129                 "Invalid polarity parameter");
130
131         ret = peripheral_gdbus_pwm_set_polarity(pwm, polarity);
132         if (ret != PERIPHERAL_ERROR_NONE)
133                 _E("Failed to set polarity, ret : %d", ret);
134
135         return ret;
136 }
137
138 int peripheral_pwm_get_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e *polarity)
139 {
140         int ret;
141
142         RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL");
143
144         ret = peripheral_gdbus_pwm_get_polarity(pwm, polarity);
145         if (ret != PERIPHERAL_ERROR_NONE)
146                 _E("Failed to get polarity, ret : %d", ret);
147
148         return ret;
149 }
150
151 int peripheral_pwm_set_enable(peripheral_pwm_h pwm, bool enable)
152 {
153         int ret;
154
155         RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL");
156
157         ret = peripheral_gdbus_pwm_set_enable(pwm, enable);
158         if (ret != PERIPHERAL_ERROR_NONE)
159                 _E("Failed to set enable, ret : %d", ret);
160
161         return ret;
162 }
163
164 int peripheral_pwm_get_enable(peripheral_pwm_h pwm, bool *enable)
165 {
166         int ret;
167
168         RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL");
169
170         ret = peripheral_gdbus_pwm_get_enable(pwm, enable);
171         if (ret != PERIPHERAL_ERROR_NONE)
172                 _E("Failed to get enable, ret : %d", ret);
173
174         return ret;
175 }