<commit_before>/****************************************************************************
 *
 * Copyright 2016 Samsung Electronics All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 *
 ****************************************************************************/

/**
 * @file iotbus_gpio.c
 * @brief Iotbus APIs for Gpio
 */

#include <tinyara/config.h>
#include <tinyara/gpio.h>
#include <sys/ioctl.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <debug.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>

#include <include/iotbus/iotbus_gpio.h>
#include <include/iotbus/iotbus_error.h>
#include "iotapi_evt_handler.h"

#define zdbg printf

/**
 * @brief Struct for iotbus_gpio_s
 */
struct _iotbus_gpio_s {
	int pin;
	iotbus_gpio_drive_e drive;
	iotbus_gpio_direction_e dir;
	iotbus_gpio_edge_e edge;
	int fd;
	gpio_isr_cb isr_cb;
	void *ud;
};

#ifdef __cplusplus
extern "C" {
#else
#endif

void gpio_async_handler(void *data)
{
	struct _iotbus_gpio_s *item = (struct _iotbus_gpio_s *)data;
	item->isr_cb(item->ud);

	return;
}

/**
 * @brief Initializes gpio_context, based on Gpio pin.
 */
iotbus_gpio_context_h iotbus_gpio_open(int gpiopin)
{
	struct _iotbus_gpio_s *dev = NULL;
	dev = (struct _iotbus_gpio_s *)malloc(sizeof(struct _iotbus_gpio_s));

	if (dev == NULL) {
		zdbg("malloc failed: %d\n", errno);
		return NULL;
	}
	char gpio_dev[16] = { 0, };
	snprintf(gpio_dev, 16, "/dev/gpio%d", gpiopin);

	dev->fd = open(gpio_dev, O_RDWR);
	if (dev->fd < 0) {
		zdbg("open %s failed: %d\n", gpio_dev, errno);
		free(dev);
		return NULL;
	}
	dev->pin = gpiopin;
	dev->drive = IOTBUS_GPIO_DRIVE_PULLUP;
	dev->dir = IOTBUS_GPIO_DIRECTION_OUT;
	dev->edge = IOTBUS_GPIO_EDGE_NONE;
	dev->isr_cb = NULL;

	return dev;
}

/**
 * @brief Closes the gpio_context.
 */
int iotbus_gpio_close(iotbus_gpio_context_h dev)
{
	if (!dev)
		return IOTBUS_ERROR_INVALID_PARAMETER;

	if (dev->isr_cb != NULL) {
		int ret = iotbus_gpio_unregister_cb(dev);
		if (ret != IOTBUS_ERROR_NONE)
			return ret;
	}

	close(dev->fd);
	free(dev);
	return IOTBUS_ERROR_NONE;
}

/**
 * @brief Sets Gpio direction.
 */
int iotbus_gpio_set_direction(iotbus_gpio_context_h dev, iotbus_gpio_direction_e dir)
{
	int ret = -1;

	if (!dev)
		return IOTBUS_ERROR_INVALID_PARAMETER;

	dev->dir = dir;
	switch (dev->dir) {
	case IOTBUS_GPIO_DIRECTION_IN:
		ret = ioctl(dev->fd, GPIOIOC_SET_DIRECTION, GPIO_DIRECTION_IN);
		break;
	case IOTBUS_GPIO_DIRECTION_OUT:<commit_msg>framework/iotbus/gpio: set a dev to NULL on close<commit_after>/****************************************************************************
 *
 * Copyright 2016 Samsung Electronics All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 *
 ****************************************************************************/

/**
 * @file iotbus_gpio.c
 * @brief Iotbus APIs for Gpio
 */

#include <tinyara/config.h>
#include <tinyara/gpio.h>
#include <sys/ioctl.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <debug.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>

#include <include/iotbus/iotbus_gpio.h>
#include <include/iotbus/iotbus_error.h>
#include "iotapi_evt_handler.h"

#define zdbg printf

/**
 * @brief Struct for iotbus_gpio_s
 */
struct _iotbus_gpio_s {
	int pin;
	iotbus_gpio_drive_e drive;
	iotbus_gpio_direction_e dir;
	iotbus_gpio_edge_e edge;
	int fd;
	gpio_isr_cb isr_cb;
	void *ud;
};

#ifdef __cplusplus
extern "C" {
#else
#endif

void gpio_async_handler(void *data)
{
	struct _iotbus_gpio_s *item = (struct _iotbus_gpio_s *)data;
	item->isr_cb(item->ud);

	return;
}

/**
 * @brief Initializes gpio_context, based on Gpio pin.
 */
iotbus_gpio_context_h iotbus_gpio_open(int gpiopin)
{
	struct _iotbus_gpio_s *dev = NULL;
	dev = (struct _iotbus_gpio_s *)malloc(sizeof(struct _iotbus_gpio_s));

	if (dev == NULL) {
		zdbg("malloc failed: %d\n", errno);
		return NULL;
	}
	char gpio_dev[16] = { 0, };
	snprintf(gpio_dev, 16, "/dev/gpio%d", gpiopin);

	dev->fd = open(gpio_dev, O_RDWR);
	if (dev->fd < 0) {
		zdbg("open %s failed: %d\n", gpio_dev, errno);
		free(dev);
		return NULL;
	}
	dev->pin = gpiopin;
	dev->drive = IOTBUS_GPIO_DRIVE_PULLUP;
	dev->dir = IOTBUS_GPIO_DIRECTION_OUT;
	dev->edge = IOTBUS_GPIO_EDGE_NONE;
	dev->isr_cb = NULL;

	return dev;
}

/**
 * @brief Closes the gpio_context.
 */
int iotbus_gpio_close(iotbus_gpio_context_h dev)
{
	if (!dev)
		return IOTBUS_ERROR_INVALID_PARAMETER;

	if (dev->isr_cb != NULL) {
		int ret = iotbus_gpio_unregister_cb(dev);
		if (ret != IOTBUS_ERROR_NONE)
			return ret;
	}

	close(dev->fd);
	free(dev);
	dev = NULL;
	return IOTBUS_ERROR_NONE;
}

/**
 * @brief Sets Gpio direction.
 */
int iotbus_gpio_set_direction(iotbus_gpio_context_h dev, iotbus_gpio_direction_e dir)
{
	int ret = -1;

	if (!dev)
		return IOTBUS_ERROR_INVALID_PARAMETER;

	dev->dir = dir;
	switch (dev->dir) {
	case IOTBUS_GPIO_DIRECTION_IN:
		ret = ioctl(dev->fd, GPIOIOC_SET_DIRECTION, GPIO_DIRECTION_IN);
		break;
	case IOTBUS_GPIO_DIRECTION_OUT:
{'fraction_non_alphanumeric': 0.1661981147676534, 'fraction_numerical': 0.00496113775425831, 'mean_word_length': 4.738140417457306, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6419010', 'n_tokens_mistral': 2330, 'n_tokens_neox': 2249, 'n_words': 560}