[5/6] fd passing: remove unnecessary gdbus functions.
[platform/core/api/peripheral-io.git] / src / peripheral_gpio.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 <unistd.h>
20 #include <assert.h>
21 #include <system_info.h>
22 #include <glib.h>
23
24 #include "peripheral_io.h"
25 #include "peripheral_gdbus_gpio.h"
26 #include "peripheral_common.h"
27 #include "peripheral_internal.h"
28
29 #define PERIPHERAL_IO_GPIO_FEATURE "http://tizen.org/feature/peripheral_io.gpio"
30
31 #define GPIO_FEATURE_UNKNOWN -1
32 #define GPIO_FEATURE_FALSE    0
33 #define GPIO_FEATURE_TRUE     1
34
35 static int gpio_feature = GPIO_FEATURE_UNKNOWN;
36
37 static bool __is_feature_supported()
38 {
39         int ret = SYSTEM_INFO_ERROR_NONE;
40         bool feature = false;
41
42         if (gpio_feature == GPIO_FEATURE_UNKNOWN) {
43                 ret = system_info_get_platform_bool(PERIPHERAL_IO_GPIO_FEATURE, &feature);
44                 RETVM_IF(ret != SYSTEM_INFO_ERROR_NONE, false, "Failed to get system info");
45
46                 gpio_feature = (feature ? GPIO_FEATURE_TRUE : GPIO_FEATURE_FALSE);
47         }
48         return (gpio_feature == GPIO_FEATURE_TRUE ? true : false);
49 }
50
51 /**
52  * @brief Initializes(export) gpio pin and creates gpio handle.
53  */
54 int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio)
55 {
56         int ret = PERIPHERAL_ERROR_NONE;
57         peripheral_gpio_h handle;
58
59         RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "GPIO feature is not supported");
60         RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid gpio handle");
61         RETVM_IF(gpio_pin < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid gpio pin number");
62
63         /* Initialize */
64         handle = (peripheral_gpio_h)calloc(1, sizeof(struct _peripheral_gpio_s));
65
66         if (handle == NULL) {
67                 _E("Failed to allocate peripheral_gpio_h");
68                 return PERIPHERAL_ERROR_OUT_OF_MEMORY;
69         }
70         handle->pin = gpio_pin;
71
72         gpio_proxy_init();
73
74         ret = peripheral_gdbus_gpio_open(handle);
75
76         if (ret != PERIPHERAL_ERROR_NONE) {
77                 _E("Failed to open the gpio pin, ret : %d", ret);
78                 free(handle);
79                 handle = NULL;
80         }
81
82         *gpio = handle;
83
84         return ret;
85 }
86
87 /**
88  * @brief Closes the gpio_context.
89  * @brief Releases the gpio handle and finalize(unexport) the gpio pin.
90  */
91 int peripheral_gpio_close(peripheral_gpio_h gpio)
92 {
93         int ret = PERIPHERAL_ERROR_NONE;
94
95         RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "GPIO feature is not supported");
96         RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL");
97
98         /* call gpio_close */
99         ret = peripheral_gdbus_gpio_close(gpio);
100         if (ret != PERIPHERAL_ERROR_NONE)
101                 _E("Failed to close the gpio pin, ret : %d", ret);
102         gpio_proxy_deinit();
103
104         free(gpio);
105         gpio = NULL;
106
107         return ret;
108 }
109
110 /**
111  * @brief Sets direction of the gpio pin.
112  */
113 int peripheral_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e direction)
114 {
115         int ret = PERIPHERAL_ERROR_NONE;
116
117         RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "GPIO feature is not supported");
118         RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL");
119         RETVM_IF((direction < PERIPHERAL_GPIO_DIRECTION_IN) || (direction > PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW), PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid direction input");
120
121         /* call gpio_set_direction */
122         // TODO : replace interface function
123
124         return ret;
125 }
126
127
128 /**
129  * @brief Sets the edge mode of the gpio pin.
130  */
131 int peripheral_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e edge)
132 {
133         int ret = PERIPHERAL_ERROR_NONE;
134
135         RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "GPIO feature is not supported");
136         RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL");
137         RETVM_IF((edge < PERIPHERAL_GPIO_EDGE_NONE) || (edge > PERIPHERAL_GPIO_EDGE_BOTH), PERIPHERAL_ERROR_INVALID_PARAMETER, "edge input is invalid");
138
139         /* call gpio_set_edge_mode */
140         // TODO : replace interface function
141
142         return ret;
143 }
144
145 /**
146  * @brief Registers a callback function to be invoked when the gpio interrupt is triggered.
147  */
148 int peripheral_gpio_set_interrupted_cb(peripheral_gpio_h gpio, peripheral_gpio_interrupted_cb callback, void *user_data)
149 {
150         int ret = PERIPHERAL_ERROR_NONE;
151
152         RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "GPIO feature is not supported");
153         RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL");
154         RETVM_IF(callback == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio interrupted callback is NULL");
155
156         // TODO : replace interface function
157
158         return ret;
159 }
160
161 /**
162  * @brief Unregisters the callback function for the gpio handler.
163  */
164 int peripheral_gpio_unset_interrupted_cb(peripheral_gpio_h gpio)
165 {
166         int ret = PERIPHERAL_ERROR_NONE;
167
168         RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "GPIO feature is not supported");
169         RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL");
170
171         // TODO : replace interface function
172
173         return ret;
174 }
175
176 /**
177  * @brief Reads value of the gpio.
178  */
179 int peripheral_gpio_read(peripheral_gpio_h gpio, uint32_t *value)
180 {
181         int ret = PERIPHERAL_ERROR_NONE;
182
183         RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "GPIO feature is not supported");
184         RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL");
185         RETVM_IF(value == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio read value is invalid");
186
187         /* call gpio_read */
188         // TODO : replace interface function
189
190         return ret;
191 }
192
193 /**
194  * @brief Writes value to the gpio.
195  */
196 int peripheral_gpio_write(peripheral_gpio_h gpio, uint32_t value)
197 {
198         int ret = PERIPHERAL_ERROR_NONE;
199
200         RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "GPIO feature is not supported");
201         RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL");
202         RETVM_IF((value != 0) && (value != 1), PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio value is invalid");
203
204         /* call gpio_write */
205         // TODO : replace interface function
206
207         return ret;
208 }