ea08352d7ebf9049294b216d2d942e349c167dac
[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
26 #define PWM_ENABLE      1
27 #define PWM_DISABLE     0
28
29 peripheral_pwm_h peripheral_pwm_open(int device, int channel)
30 {
31         peripheral_pwm_h dev = NULL;
32         int ret = PERIPHERAL_ERROR_NONE;
33
34         assert(device >= 0);
35         assert(channel >= 0);
36
37         /* Initialize */
38         dev = (peripheral_pwm_h)malloc(sizeof(struct _peripheral_pwm_s));
39
40         if (dev == NULL) {
41                 _E("Failed to allocate peripheral_pwm_h");
42                 return NULL;
43         }
44
45         pwm_proxy_init();
46
47         dev->device = device;
48         dev->channel = channel;
49
50         ret = peripheral_gdbus_pwm_open(dev, device, channel);
51
52         if (ret != PERIPHERAL_ERROR_NONE) {
53                 free(dev);
54                 dev = NULL;
55         }
56
57         return dev;
58 }
59
60 int peripheral_pwm_close(peripheral_pwm_h pwm)
61 {
62         int ret = PERIPHERAL_ERROR_NONE;
63
64         ret = peripheral_gdbus_pwm_close(pwm);
65         pwm_proxy_deinit();
66
67         if (ret == PERIPHERAL_ERROR_NONE) {
68                 free(pwm);
69                 pwm = NULL;
70         }
71
72         return ret;
73 }
74
75
76 int     peripheral_pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle)
77 {
78         int ret = PERIPHERAL_ERROR_NONE;
79
80         ret = peripheral_gdbus_pwm_set_duty_cycle(pwm, duty_cycle);
81
82         if (ret != PERIPHERAL_ERROR_NONE)
83                 pwm->duty_cycle = duty_cycle;
84
85         return ret;
86 }
87
88 int peripheral_pwm_set_period(peripheral_pwm_h pwm, int period)
89 {
90         int ret = PERIPHERAL_ERROR_NONE;
91
92         ret = peripheral_gdbus_pwm_set_period(pwm, period);
93
94         if (ret != PERIPHERAL_ERROR_NONE)
95                 pwm->period = period;
96
97         return ret;
98 }
99
100 int     peripheral_pwm_set_enabled(peripheral_pwm_h pwm, peripheral_pwm_state_e enable)
101 {
102         int ret = PERIPHERAL_ERROR_NONE;
103
104         ret = peripheral_gdbus_pwm_set_enable(pwm, enable);
105
106         if (ret != PERIPHERAL_ERROR_NONE)
107                 pwm->enabled = enable;
108
109         return PERIPHERAL_ERROR_NONE;
110 }
111
112 int peripheral_pwm_is_enabled(peripheral_pwm_h pwm)
113 {
114         if (pwm->enabled == PWM_ENABLE)
115                 return PWM_ENABLE;
116         else
117                 return PWM_DISABLE;
118 }
119
120 int peripheral_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle)
121 {
122         int ret = PERIPHERAL_ERROR_NONE;
123
124         ret = peripheral_gdbus_pwm_get_duty_cycle(pwm, duty_cycle);
125
126         if (ret != PERIPHERAL_ERROR_NONE)
127                 pwm->duty_cycle = *duty_cycle;
128
129         return ret;
130 }
131
132 int peripheral_pwm_get_period(peripheral_pwm_h pwm, int *period)
133 {
134         int ret = PERIPHERAL_ERROR_NONE;
135
136         ret = peripheral_gdbus_pwm_get_period(pwm, period);
137
138         if (ret != PERIPHERAL_ERROR_NONE)
139                 pwm->period = *period;
140
141         return ret;
142 }