#include "mbtk_type.h"
#include "lynq-gpio.h"
#include "unistd.h"
#include "fcntl.h"
#include "mbtk_log.h"

#include <errno.h>
static int gpio_export(int gpio)
{
//    int index=0;
    int file=-1;
    int result =-1;
    char pin_index_buffer[5]= {0};

    char buffer[50];
    memset(buffer,0,50);
    sprintf(buffer,"/sys/class/gpio/gpio%d/direction", gpio);
    if(access(buffer , F_OK) == 0)
    {
        LOGD("%d has export.", gpio);
        return 0;
    }

    file = open("/sys/class/gpio/export",O_WRONLY);
    if(file == -1)
    {
        LOGE("Open gpio export file fail.");
        return -1;
    }

    memset(pin_index_buffer,0,5);
    sprintf(pin_index_buffer,"%d", gpio);
    result = write(file,pin_index_buffer,strlen(pin_index_buffer));
    if(result < 0)
    {
        LOGE("Gpio[%d] export fail. err = %d", gpio, errno);
        close(file);
        return -1;
    }
    close(file);

    return 0;
}

static int gpio_unexport(int gpio)
{
//    int index=0;
    int file=-1;
    int result =-1;
    char pin_index_buffer[5]= {0};
    char buffer[50];
    memset(buffer,0,50);
    sprintf(buffer,"/sys/class/gpio/gpio%d/direction", gpio);
    if(access(buffer , F_OK) == -1)
    {
        LOGD("%d not export.", gpio);
        return 0;
    }

    file = open("/sys/class/gpio/unexport",O_WRONLY);
    if(file == -1)
    {
        LOGE("Open gpio unexport file fail.");
        return -1;
    }

    memset(pin_index_buffer,0,5);
    sprintf(pin_index_buffer,"%d", gpio);
    result=write(file,pin_index_buffer,strlen(pin_index_buffer));
    if(result < 0)
    {
        close(file);
        LOGE("Gpio[%d] unexport fail.", gpio);
        return -1;
    }
    close(file);

    return 0;
}

#if 0
static int gpio_direct_get(int gpio, char *value, int value_size)
{
    char buffer[50]= {0};
    int file =-1;
    int result =-1;

    memset(buffer,0,50);
    sprintf(buffer,"/sys/class/gpio/gpio%d/direction", gpio);
    file = open(buffer, O_RDONLY);
    if(file == -1)
    {
        LOGE("Open gpio[%d] direct fail.", gpio);
        return -1;
    }

    memset(value, 0x0, value_size);
    result = read(file,value,value_size);
    if(result <= 0)
    {
        LOGE("Get gpio[%d] direct fail.", gpio);
        close(file);
        return -1;
    }
    close(file);

    return 0;
}
#endif

static int gpio_direct_set(int gpio, char *value)
{
    char buffer[50]= {0};
    int file =-1;
    int result =-1;

    memset(buffer,0,50);
    sprintf(buffer,"/sys/class/gpio/gpio%d/direction", gpio);
    file = open(buffer, O_WRONLY);
    if(file == -1)
    {
        LOGE("Open gpio[%d] direct fail.", gpio);
        return -1;
    }

    result = write(file,value,strlen(value));
    if(result != strlen(value))
    {
        LOGE("Set gpio[%d] direct fail.", gpio);
        close(file);
        return -1;
    }
    close(file);

    return 0;
}

static int gpio_value_get(int gpio)
{
    char buffer[50];
    int fd =-1;

    memset(buffer, 0, sizeof(buffer));
    sprintf(buffer, "/sys/class/gpio/gpio%d/value", gpio);
    fd = open(buffer, O_RDONLY);
    if(fd == -1)
    {
        LOGE("Open gpio[%d] fail.", gpio);
        return -1;
    }

    memset(buffer, 0, sizeof(buffer));
    if(read(fd, buffer, sizeof(buffer)) <= 0)
    {
        LOGE("Get gpio[%d] value fail", gpio);
        close(fd);
        return -1;
    }

    close(fd);
    return atoi(buffer);
}

static int gpio_value_set(int gpio, int value)
{
    char buffer[50]= {0};
    int file =-1;
    int result =-1;

    memset(buffer,0,50);
    sprintf(buffer,"/sys/class/gpio/gpio%d/value", gpio);
    file = open(buffer,O_WRONLY);
    if(file == -1)
    {
        LOGE("Open gpio[%d] value fail.", gpio);
        return -1;
    }
    if(value == 0) {
        result = write(file,"0",1);
    } else {
        result = write(file,"1",1);
    }
    if(result != 1)
    {
        LOGE("Set gpio[%d] value fail err =%d.", gpio, errno);
        close(file);
        return -1;
    }
    close(file);

    return 0;
}


int lynq_gpio_init(int gpio, int direction, int value, int pullsel)
{
    //UNUSED(gpio);
    //UNUSED(direction);
    //UNUSED(value);
    UNUSED(pullsel);

    if (direction != 1 && direction != 0)
    {
        LOGE("[lynq_gpio_init] direction fail.");
        return -1;
    }

    if (value != 1 && value != 0)
    {
        LOGE("[lynq_gpio_init] value fail.");
        return -1;
    }

    if(gpio_export(gpio))
    {
        LOGE("[lynq_gpio_init]gpio_export fail.");
        return -1;
    }

    if(gpio_direct_set(gpio, direction == 0 ? "in" : "out"))
    {
        LOGE("[lynq_gpio_init]gpio_direct_set fail.");
        return -1;
    }

    if(direction == 1 && (gpio_value_set(gpio, value) != 0))
    {
        LOGE("[lynq_gpio_init]gpio_value_set fail.");
        return -1;
    }


    return 0;
}

int lynq_gpio_deinit(int gpio)
{
    UNUSED(gpio);

    if(gpio_unexport(gpio))
    {
        LOGE("[lynq_gpio_deinit]gpio_unexport fail.");
        return -1;
    }

    return 0;
}

int lynq_gpio_direction_set(int gpio, int direction)
{
    //UNUSED(gpio);
    //UNUSED(direction);

    if(gpio_direct_set(gpio, direction == 0 ? "in" : "out"))
    {
        LOGE("[lynq_gpio_direction_set]gpio_direct_set fail.");
        return -1;
    }

    return 0;
}


int lynq_gpio_value_set(int gpio, int value)
{
    //UNUSED(gpio);
    //UNUSED(value);

    if(gpio_value_set(gpio, value))
    {
        LOGE("[lynq_gpio_value_set]gpio_value_set fail.");
        return -1;
    }

    return 0;
}

int lynq_gpio_value_get(int gpio)
{
    //UNUSED(gpio);
    int ret = -1;

    ret = gpio_value_get(gpio);
    if (ret == -1)
    {
        LOGE("[lynq_gpio_value_get]gpio_value_get fail.");
        return -1;
    }

    return ret;
}

int lynq_gpio_pullsel_set(int gpio, int pullsel)
{
    //UNUSED(gpio);
    //UNUSED(pullsel);
    int ret = -1;
    int value_t;

    if (pullsel == 1)
        value_t = 0;
    else if (pullsel == 2)
        value_t = 1;
    else
    {
        LOGE("[lynq_gpio_pullsel_set] value_t fail.");
        return -1;
    }

    ret = gpio_value_set(gpio, value_t);
    if(ret == -1)
    {
        LOGE("[lynq_gpio_pullsel_set]gpio_value_set() fail.");
        return -1;
    }

    return ret;
}


int lynq_gpio_pullsel_get(int gpio)
{
    //UNUSED(gpio);
    int ret = -1;

    ret = gpio_value_get(gpio);
    if (ret == -1)
    {
        LOGE("[lynq_gpio_pullsel_get]gpio_value_get() fail.");
        return -1;
    }

    return ret + 1;
}



