2 * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <system_info.h>
24 #include "peripheral_io.h"
25 #include "peripheral_gdbus_gpio.h"
26 #include "peripheral_common.h"
27 #include "peripheral_internal.h"
29 #define PERIPHERAL_IO_GPIO_FEATURE "http://tizen.org/feature/peripheral_io.gpio"
31 #define GPIO_FEATURE_UNKNOWN -1
32 #define GPIO_FEATURE_FALSE 0
33 #define GPIO_FEATURE_TRUE 1
35 static int gpio_feature = GPIO_FEATURE_UNKNOWN;
37 static bool __is_feature_supported()
39 int ret = SYSTEM_INFO_ERROR_NONE;
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");
46 gpio_feature = (feature ? GPIO_FEATURE_TRUE : GPIO_FEATURE_FALSE);
48 return (gpio_feature == GPIO_FEATURE_TRUE ? true : false);
52 * @brief Initializes(export) gpio pin and creates gpio handle.
54 int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio)
56 int ret = PERIPHERAL_ERROR_NONE;
57 peripheral_gpio_h handle;
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");
64 handle = (peripheral_gpio_h)calloc(1, sizeof(struct _peripheral_gpio_s));
67 _E("Failed to allocate peripheral_gpio_h");
68 return PERIPHERAL_ERROR_OUT_OF_MEMORY;
70 handle->pin = gpio_pin;
74 ret = peripheral_gdbus_gpio_open(handle);
76 if (ret != PERIPHERAL_ERROR_NONE) {
77 _E("Failed to open the gpio pin, ret : %d", ret);
88 * @brief Closes the gpio_context.
89 * @brief Releases the gpio handle and finalize(unexport) the gpio pin.
91 int peripheral_gpio_close(peripheral_gpio_h gpio)
93 int ret = PERIPHERAL_ERROR_NONE;
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");
99 ret = peripheral_gdbus_gpio_close(gpio);
100 if (ret != PERIPHERAL_ERROR_NONE)
101 _E("Failed to close the gpio pin, ret : %d", ret);
111 * @brief Sets direction of the gpio pin.
113 int peripheral_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e direction)
115 int ret = PERIPHERAL_ERROR_NONE;
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");
121 /* call gpio_set_direction */
122 // TODO : replace interface function
129 * @brief Sets the edge mode of the gpio pin.
131 int peripheral_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e edge)
133 int ret = PERIPHERAL_ERROR_NONE;
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");
139 /* call gpio_set_edge_mode */
140 // TODO : replace interface function
146 * @brief Registers a callback function to be invoked when the gpio interrupt is triggered.
148 int peripheral_gpio_set_interrupted_cb(peripheral_gpio_h gpio, peripheral_gpio_interrupted_cb callback, void *user_data)
150 int ret = PERIPHERAL_ERROR_NONE;
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");
156 // TODO : replace interface function
162 * @brief Unregisters the callback function for the gpio handler.
164 int peripheral_gpio_unset_interrupted_cb(peripheral_gpio_h gpio)
166 int ret = PERIPHERAL_ERROR_NONE;
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");
171 // TODO : replace interface function
177 * @brief Reads value of the gpio.
179 int peripheral_gpio_read(peripheral_gpio_h gpio, uint32_t *value)
181 int ret = PERIPHERAL_ERROR_NONE;
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");
188 // TODO : replace interface function
194 * @brief Writes value to the gpio.
196 int peripheral_gpio_write(peripheral_gpio_h gpio, uint32_t value)
198 int ret = PERIPHERAL_ERROR_NONE;
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");
204 /* call gpio_write */
205 // TODO : replace interface function