[3/6] fd passing: use fd directly without opening it
[platform/core/api/peripheral-io.git] / src / interface / pwm.c
1 /*
2  * Copyright (c) 2016-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 <string.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <stdbool.h>
24
25 #include "pwm.h"
26 #include "peripheral_common.h"
27 #include "peripheral_internal.h"
28
29 #define SYSFS_PWM_PATH  "/sys/class/pwm"
30
31 #define PATH_BUF_MAX    64
32 #define PWM_BUF_MAX     16
33 #define MAX_ERR_LEN     255
34
35 int pwm_close(peripheral_pwm_h pwm)
36 {
37         int status;
38
39         status = close(pwm->fd_period);
40         if (status < 0) {
41                 _E("Error: pwm period fd close error \n");
42                 return -EIO;
43         }
44
45         status = close(pwm->fd_duty_cycle);
46         if (status < 0) {
47                 _E("Error: pwm duty cycle fd close error \n");
48                 return -EIO;
49         }
50
51         status = close(pwm->fd_polarity);
52         if (status < 0) {
53                 _E("Error: pwm polarity fd close error \n");
54                 return -EIO;
55         }
56
57         status = close(pwm->fd_enable);
58         if (status < 0) {
59                 _E("Error: pwm enable fd close error \n");
60                 return -EIO;
61         }
62
63         return 0;
64 }
65
66 int pwm_set_period(peripheral_pwm_h pwm, int period)
67 {
68         int len, status;
69         char pwm_buf[PWM_BUF_MAX] = {0};
70
71         len = snprintf(pwm_buf, sizeof(pwm_buf), "%d", period);
72         status = write(pwm->fd_period, pwm_buf, len);
73         if (status < 0) {
74                 _E("Failed to set period");
75                 return -EIO;
76         }
77
78         return 0;
79 }
80
81 int pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle)
82 {
83         int len, status;
84         char pwm_buf[PWM_BUF_MAX] = {0};
85
86         len = snprintf(pwm_buf, sizeof(pwm_buf), "%d", duty_cycle);
87         status = write(pwm->fd_duty_cycle, pwm_buf, len);
88         if (status < 0) {
89                 _E("Failed to set duty cycle");
90                 return -EIO;
91         }
92
93         return 0;
94 }
95
96 int pwm_set_polarity(peripheral_pwm_h pwm, pwm_polarity_e polarity)
97 {
98         int status;
99
100         if (polarity == PWM_POLARITY_NORMAL)
101                 status = write(pwm->fd_polarity, "normal", strlen("normal")+1);
102         else if (polarity == PWM_POLARITY_INVERSED)
103                 status = write(pwm->fd_polarity, "inversed", strlen("inversed")+1);
104         else {
105                 _E("Invalid pwm polarity : %d", polarity);
106                 return -EINVAL;
107         }
108
109         if (status <= 0) {
110                 _E("Failed to set polarity");
111                 return -EIO;
112         }
113
114         return 0;
115 }
116
117 int pwm_set_enable(peripheral_pwm_h pwm, bool enable)
118 {
119         int len, status;
120         char pwm_buf[PWM_BUF_MAX] = {0};
121
122         len = snprintf(pwm_buf, sizeof(pwm_buf), "%d", enable);
123         status = write(pwm->fd_enable, pwm_buf, len);
124         if (status < 0) {
125                 _E("Failed to set enable");
126                 return -EIO;
127         }
128
129         return 0;
130 }