api: add feature invalid check.
[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 #include <system_info.h>
22
23 #include "peripheral_io.h"
24 #include "peripheral_gdbus_pwm.h"
25 #include "peripheral_common.h"
26 #include "peripheral_internal.h"
27
28 #define PERIPHERAL_IO_PWM_FEATURE "http://tizen.org/feature/peripheral_io.pwm"
29
30 #define PWM_FEATURE_UNKNOWN -1
31 #define PWM_FEATURE_FALSE    0
32 #define PWM_FEATURE_TRUE     1
33
34 static int pwm_feature = PWM_FEATURE_UNKNOWN;
35
36 static bool __is_feature_supported()
37 {
38         bool feature = false;
39
40         if (pwm_feature == PWM_FEATURE_UNKNOWN) {
41                 system_info_get_platform_bool(PERIPHERAL_IO_PWM_FEATURE, &feature);
42                 pwm_feature = (feature ? PWM_FEATURE_TRUE : PWM_FEATURE_FALSE);
43         }
44
45         return (pwm_feature == PWM_FEATURE_TRUE ? true : false);
46 }
47
48
49 int peripheral_pwm_open(int chip, int pin, peripheral_pwm_h *pwm)
50 {
51         peripheral_pwm_h handle;
52         int ret = PERIPHERAL_ERROR_NONE;
53
54         RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "PWM feature is not supported");
55         RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid pwm handle");
56         RETVM_IF(chip < 0 || pin < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
57
58         /* Initialize */
59         handle = (peripheral_pwm_h)calloc(1, sizeof(struct _peripheral_pwm_s));
60
61         if (handle == NULL) {
62                 _E("Failed to allocate peripheral_pwm_h");
63                 return PERIPHERAL_ERROR_OUT_OF_MEMORY;
64         }
65
66         pwm_proxy_init();
67
68         ret = peripheral_gdbus_pwm_open(handle, chip, pin);
69
70         if (ret != PERIPHERAL_ERROR_NONE) {
71                 _E("Failed to open PWM chip : %d, pin : %d", chip, pin);
72                 free(handle);
73                 handle = NULL;
74         }
75         *pwm = handle;
76
77         return ret;
78 }
79
80 int peripheral_pwm_close(peripheral_pwm_h pwm)
81 {
82         int ret = PERIPHERAL_ERROR_NONE;
83
84         RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "PWM feature is not supported");
85         RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL");
86
87         if ((ret = peripheral_gdbus_pwm_close(pwm)) < 0)
88                 _E("Failed to close PWM chip, continuing anyway, ret : %d", ret);
89
90         pwm_proxy_deinit();
91         free(pwm);
92
93         return ret;
94 }
95
96 int peripheral_pwm_set_period(peripheral_pwm_h pwm, uint32_t period_ns)
97 {
98         int ret;
99
100         RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "PWM feature is not supported");
101         RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL");
102
103         ret = peripheral_gdbus_pwm_set_period(pwm, (int)period_ns);
104         if (ret != PERIPHERAL_ERROR_NONE)
105                 _E("Failed to set period, ret : %d", ret);
106
107         return ret;
108 }
109
110 int peripheral_pwm_set_duty_cycle(peripheral_pwm_h pwm, uint32_t duty_cycle_ns)
111 {
112         int ret;
113
114         RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "PWM feature is not supported");
115         RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL");
116
117         ret = peripheral_gdbus_pwm_set_duty_cycle(pwm, (int)duty_cycle_ns);
118         if (ret != PERIPHERAL_ERROR_NONE)
119                 _E("Failed to set duty cycle, ret : %d", ret);
120
121         return ret;
122 }
123
124 int peripheral_pwm_set_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e polarity)
125 {
126         int ret;
127
128         RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "PWM feature is not supported");
129         RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL");
130         RETVM_IF((polarity < PERIPHERAL_PWM_POLARITY_ACTIVE_HIGH) || (polarity > PERIPHERAL_PWM_POLARITY_ACTIVE_LOW), PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid polarity parameter");
131
132         ret = peripheral_gdbus_pwm_set_polarity(pwm, polarity);
133         if (ret != PERIPHERAL_ERROR_NONE)
134                 _E("Failed to set polarity, ret : %d", ret);
135
136         return ret;
137 }
138
139 int peripheral_pwm_set_enabled(peripheral_pwm_h pwm, bool enable)
140 {
141         int ret;
142
143         RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "PWM feature is not supported");
144         RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL");
145
146         ret = peripheral_gdbus_pwm_set_enable(pwm, enable);
147         if (ret != PERIPHERAL_ERROR_NONE)
148                 _E("Failed to set enable, ret : %d", ret);
149
150         return ret;
151 }