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.
22 #include "peripheral_io.h"
23 #include "peripheral_gdbus_gpio.h"
24 #include "peripheral_common.h"
25 #include "peripheral_internal.h"
26 #include "peripheral_io_gdbus.h"
34 static GList *gpio_isr_list = NULL;
36 int peripheral_gpio_isr_callback(int pin)
39 gpio_isr_data_s *isr_data;
43 isr_data = (gpio_isr_data_s*)link->data;
45 if (isr_data->pin == pin) {
46 if (isr_data->callback)
47 isr_data->callback(isr_data->user_data);
48 return PERIPHERAL_ERROR_NONE;
50 link = g_list_next(link);
53 return PERIPHERAL_ERROR_NONE;
56 int peripheral_gpio_isr_set(int pin, gpio_isr_cb callback, void *user_data)
59 gpio_isr_data_s *isr_data = NULL;
64 tmp = (gpio_isr_data_s*)link->data;
65 if (tmp->pin == pin) {
69 link = g_list_next(link);
72 if (isr_data == NULL) {
73 isr_data = (gpio_isr_data_s*)calloc(1, sizeof(gpio_isr_data_s));
74 if (isr_data == NULL) {
75 _E("failed to allocate gpio_isr_data_s");
76 return PERIPHERAL_ERROR_OUT_OF_MEMORY;
79 gpio_isr_list = g_list_append(gpio_isr_list, isr_data);
83 isr_data->callback = callback;
84 isr_data->user_data = user_data;
86 return PERIPHERAL_ERROR_NONE;
89 int peripheral_gpio_isr_unset(int pin)
92 gpio_isr_data_s *isr_data;
96 isr_data = (gpio_isr_data_s*)link->data;
98 if (isr_data->pin == pin) {
99 gpio_isr_list = g_list_remove_link(gpio_isr_list, link);
104 link = g_list_next(link);
107 return PERIPHERAL_ERROR_NONE;
111 * @brief Initializes(export) gpio pin and creates gpio handle.
113 int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio)
115 int ret = PERIPHERAL_ERROR_NONE;
116 peripheral_gpio_h handle;
118 RETVM_IF(gpio_pin < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid gpio pin number");
121 handle = (peripheral_gpio_h)calloc(1, sizeof(struct _peripheral_gpio_s));
123 if (handle == NULL) {
124 _E("Failed to allocate peripheral_gpio_h");
125 return PERIPHERAL_ERROR_OUT_OF_MEMORY;
127 handle->pin = gpio_pin;
131 ret = peripheral_gdbus_gpio_open(handle);
133 if (ret != PERIPHERAL_ERROR_NONE) {
134 _E("Failed to open the gpio pin, ret : %d", ret);
145 * @brief Closes the gpio_context.
146 * @brief Releases the gpio handle and finalize(unexport) the gpio pin.
148 int peripheral_gpio_close(peripheral_gpio_h gpio)
150 int ret = PERIPHERAL_ERROR_NONE;
152 RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL");
154 /* call gpio_close */
155 ret = peripheral_gdbus_gpio_close(gpio);
156 if (ret != PERIPHERAL_ERROR_NONE)
157 _E("Failed to close the gpio pin, ret : %d", ret);
167 * @brief Gets direction of the gpio.
169 int peripheral_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e *direction)
171 int ret = PERIPHERAL_ERROR_NONE;
173 RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL");
175 ret = peripheral_gdbus_gpio_get_direction(gpio, direction);
176 if (ret != PERIPHERAL_ERROR_NONE)
177 _E("Failed to get direction of the gpio pin, ret : %d", ret);
184 * @brief Sets direction of the gpio pin.
186 int peripheral_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e direction)
188 int ret = PERIPHERAL_ERROR_NONE;
190 RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL");
191 RETVM_IF(direction > PERIPHERAL_GPIO_DIRECTION_OUT_HIGH, PERIPHERAL_ERROR_INVALID_PARAMETER,
192 "Invalid direction input");
194 /* call gpio_set_direction */
195 ret = peripheral_gdbus_gpio_set_direction(gpio, direction);
196 if (ret != PERIPHERAL_ERROR_NONE)
197 _E("Failed to set gpio direction, ret : %d", ret);
203 * @brief Reads value of the gpio.
205 int peripheral_gpio_read(peripheral_gpio_h gpio, int *value)
207 int ret = PERIPHERAL_ERROR_NONE;
209 RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL");
212 ret = peripheral_gdbus_gpio_read(gpio, value);
213 if (ret != PERIPHERAL_ERROR_NONE)
214 _E("Failed to read value of the gpio pin, ret : %d", ret);
220 * @brief Writes value to the gpio.
222 int peripheral_gpio_write(peripheral_gpio_h gpio, int value)
224 int ret = PERIPHERAL_ERROR_NONE;
226 RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL");
228 /* call gpio_write */
229 ret = peripheral_gdbus_gpio_write(gpio, value);
230 if (ret != PERIPHERAL_ERROR_NONE)
231 _E("Failed to write to the gpio pin, ret : %d", ret);
237 * @brief Gets the edge mode of the gpio.
239 int peripheral_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e *edge)
241 int ret = PERIPHERAL_ERROR_NONE;
243 RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL");
245 ret = peripheral_gdbus_gpio_get_edge_mode(gpio, edge);
246 if (ret != PERIPHERAL_ERROR_NONE)
247 _E("Failed to get edge mode of the gpio pin, ret : %d", ret);
253 * @brief Sets the edge mode of the gpio pin.
255 int peripheral_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e edge)
257 int ret = PERIPHERAL_ERROR_NONE;
259 RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL");
260 RETVM_IF(edge > PERIPHERAL_GPIO_EDGE_BOTH, PERIPHERAL_ERROR_INVALID_PARAMETER,
261 "Invalid edge input");
263 /* call gpio_set_edge_mode */
264 ret = peripheral_gdbus_gpio_set_edge_mode(gpio, edge);
265 if (ret != PERIPHERAL_ERROR_NONE)
266 _E("Failed to set edge mode of the gpio pin, ret : %d", ret);
272 * @brief Registers a callback function to be invoked when the gpio interrupt is triggered.
274 int peripheral_gpio_register_cb(peripheral_gpio_h gpio, gpio_isr_cb callback, void *user_data)
276 int ret = PERIPHERAL_ERROR_NONE;
278 RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL");
280 ret = peripheral_gdbus_gpio_register_cb(gpio, callback, user_data);
281 if (ret != PERIPHERAL_ERROR_NONE) {
282 _E("Failed to register cb, ret : %d", ret);
287 ret = peripheral_gpio_isr_set(gpio->pin, callback, user_data);
288 if (ret != PERIPHERAL_ERROR_NONE)
289 _E("Failed to register gpio isr, ret : %d", ret);
295 * @brief Unregisters the callback function for the gpio handler.
297 int peripheral_gpio_unregister_cb(peripheral_gpio_h gpio)
299 int ret = PERIPHERAL_ERROR_NONE;
301 RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL");
303 ret = peripheral_gdbus_gpio_unregister_cb(gpio);
304 if (ret != PERIPHERAL_ERROR_NONE) {
305 _E("Failed to unregister gpio isr, ret : %d", ret);
310 ret = peripheral_gpio_isr_unset(gpio->pin);
316 * @brief Gets pin number of the gpio handle.
318 int peripheral_gpio_get_pin(peripheral_gpio_h gpio, int *gpio_pin)
320 RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL");
322 *gpio_pin = gpio->pin;
324 return PERIPHERAL_ERROR_NONE;