blob: 9bd8a791ecd67a286d3bb498afa5ed932f780e3f [file] [log] [blame]
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdint.h>
#include <string.h>
#include <dlfcn.h>
#include "gsw_gpio.h"
#ifndef LOG_ERR_LEVEL
#define LOG_ERR_LEVEL 3 /* error conditions */
#endif
#ifndef LOG_WARN_LEVEL
#define LOG_WARN_LEVEL 4 /* warning conditions */
#endif
#ifndef LOG_INFO_LEVEL
#define LOG_INFO_LEVEL 6 /* informational */
#endif
#ifndef LOG_DEBUG_LEVEL
#define LOG_DEBUG_LEVEL 7 /* debug-level messages */
#endif
#ifndef LOG_VERBOSE_LEVEL
#define LOG_VERBOSE_LEVEL 8
#endif
#define LOGV(fmt, args ...) \
do{ \
char *file_ptr_1001 = __FILE__; \
char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
char line_1001[10] = {0}; \
sprintf(line_1001, "%d", __LINE__); \
while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
if(*ptr_1001 == '/') \
break; \
ptr_1001--; \
} \
fun_ptr_log(LOG_VERBOSE_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
} while(0)
#define LOGI(fmt, args...) \
do{ \
char *file_ptr_1001 = __FILE__; \
char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
char line_1001[10] = {0}; \
sprintf(line_1001, "%d", __LINE__); \
while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
if(*ptr_1001 == '/') \
break; \
ptr_1001--; \
} \
fun_ptr_log(LOG_INFO_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
} while(0)
#define LOGD(fmt, args...) \
do{ \
char *file_ptr_1001 = __FILE__; \
char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
char line_1001[10] = {0}; \
sprintf(line_1001, "%d", __LINE__); \
while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
if(*ptr_1001 == '/') \
break; \
ptr_1001--; \
} \
fun_ptr_log(LOG_DEBUG_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
} while(0)
#define LOGW(fmt, args...) \
do{ \
char *file_ptr_1001 = __FILE__; \
char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
char line_1001[10] = {0}; \
sprintf(line_1001, "%d", __LINE__); \
while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
if(*ptr_1001 == '/') \
break; \
ptr_1001--; \
} \
fun_ptr_log(LOG_WARN_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
} while(0)
#define LOGE(fmt, args...) \
do{ \
char *file_ptr_1001 = __FILE__; \
char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
char line_1001[10] = {0}; \
sprintf(line_1001, "%d", __LINE__); \
while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
if(*ptr_1001 == '/') \
break; \
ptr_1001--; \
} \
fun_ptr_log(LOG_ERR_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
} while(0)
#define GSW_HAL_SUCCESS 0
#define GSW_HAL_FAIL -1 //表示失败(通用性)
#define GSW_HAL_MEM_INVAILD -2 //表示入参地址为NULL
#define wakeout 117
typedef void (*mbtk_log)(int level, const char *format,...);
static mbtk_log fun_ptr_log = NULL;
void *dlHandle_gpio = NULL;
char *lynqLib_gpio = "/lib/libmbtk_lib.so";
int gpio_check(int gpio)
{
char buffer[128];
memset(buffer,0,128);
sprintf(buffer,"/sys/class/gpio/gpio%d/direction", gpio);
return access(buffer , F_OK);
}
static int handle()
{
if(dlHandle_gpio == NULL || fun_ptr_log == NULL)
{
dlHandle_gpio = dlopen(lynqLib_gpio, RTLD_NOW);
fun_ptr_log = (mbtk_log)dlsym(dlHandle_gpio, "mbtk_log");
if(fun_ptr_log == NULL || dlHandle_gpio == NULL)
{
return GSW_HAL_FAIL;
}
}
return GSW_HAL_SUCCESS;
}
static int gpio_export(int gpio)
{
int file=-1;
int result =-1;
char pin_index_buffer[5]= {0};
if (gpio_check(gpio) == 0)
{
LOGE("export has been add\n");
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.", gpio);
close(file);
return -1;
}
close(file);
return 0;
}
static int gpio_unexport(int gpio)
{
int file=-1;
int result =-1;
char pin_index_buffer[5]= {0};
if (gpio_check(gpio) == -1)
{
LOGE("export has been not add\n");
return -1;
}
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;
}
int32_t gsw_gpio_SetDirection(uint32_t gpioNum, gsw_hal_pinDirection dir)
{
char buffer[128]= {0};
int file =-1;
int result =-1;
char direction[10] = {0};
memset(buffer,0,128);
if (handle())
return GSW_HAL_FAIL;
if (gpio_check(gpioNum) == -1)
{
LOGD("in dir export has been not add\n");
gpio_export(gpioNum);
}
sprintf(buffer,"/sys/class/gpio/gpio%u/direction", gpioNum);
file = open(buffer, O_WRONLY);
if(file == -1)
{
LOGE("Open gpio[%d] direct fail.", gpioNum);
return -1;
}
if (dir == GSW_HAL_PINDIR_IN)
sprintf(direction,"in");
else if (dir == GSW_HAL_PINDIR_OUT)
sprintf(direction,"out");
result = write(file,direction,strlen(direction));
if(result != strlen(direction))
{
LOGE("Set gpio[%d] direct fail.", gpioNum);
close(file);
gpio_unexport(gpioNum);
return GSW_HAL_FAIL;
}
close(file);
gpio_unexport(gpioNum);
return GSW_HAL_SUCCESS;
}
gsw_hal_pinLevel gsw_gpio_GetLevel(uint32_t gpioNum)
{
char buffer[50];
char path[128];
int file =-1;
int result =-1;
memset(path,0,128);
memset(buffer,0,50);
if (handle())
return GSW_HAL_FAIL;
if (gpio_check(gpioNum) == -1)
{
LOGD("in get value export has been not add\n");
gpio_export(gpioNum);
}
sprintf(path,"/sys/class/gpio/gpio%u/value", gpioNum);
file = open(path,O_RDONLY);
if(file == -1)
{
LOGE("Open gpio[%d] fail.", gpioNum);
return -1;
}
result = read(file,buffer,50);
if(result <= 0)
{
LOGE("Get gpio[%d] value fail", gpioNum);
close(file);
gpio_unexport(gpioNum);
return GSW_HAL_FAIL;
}
close(file);
gpio_unexport(gpioNum);
return atoi(buffer);
}
int32_t gsw_gpio_SetLevel(uint32_t gpioNum, gsw_hal_pinLevel level)
{
char buffer[128]= {0};
int file =-1;
int result =-1;
memset(buffer,0,50);
if (handle())
return GSW_HAL_FAIL;
if (gpio_check(gpioNum) == -1)
{
LOGD("in set value export has been not add\n");
gpio_export(gpioNum);
}
sprintf(buffer,"/sys/class/gpio/gpio%u/value", gpioNum);
file = open(buffer,O_WRONLY);
if(file == -1)
{
LOGE("Open gpio[%d] value fail.", gpioNum);
return -1;
}
if(level == GSW_HAL_LEVEL_LOW) {
result = write(file,"0",1);
} else {
result = write(file,"1",1);
}
if(result != 1)
{
LOGE("Set gpio[%d] value fail err =%d.", gpioNum, errno);
close(file);
gpio_unexport(gpioNum);
return GSW_HAL_FAIL;
}
close(file);
gpio_unexport(gpioNum);
return GSW_HAL_SUCCESS;
}
int32_t gsw_gpio_Init(uint32_t gpioNum, gsw_hal_pinDirection dir, gsw_hal_pinLevel level, gsw_hal_pinPullSel pullsel)
{
if (handle())
return GSW_HAL_FAIL;
if (dir != 1 && dir != 0)
{
LOGE("[lynq_gpio_init] direction fail.");
return -1;
}
if (level != 1 && level != 0)
{
LOGE("[lynq_gpio_init] value fail.");
return GSW_HAL_FAIL;
}
if(gpio_export(gpioNum))
{
LOGE("[lynq_gpio_init]gpio_export fail.");
return GSW_HAL_FAIL;
}
if(gsw_gpio_SetDirection(gpioNum, dir))
{
LOGE("[lynq_gpio_init]gpio_direct_set fail.");
return GSW_HAL_FAIL;
}
if(dir == 1 && (gsw_gpio_SetLevel(gpioNum, level) != 0))
{
LOGE("[lynq_gpio_init]gpio_value_set fail.");
return GSW_HAL_FAIL;
}
return GSW_HAL_SUCCESS;
}
gsw_hal_usbStatus gsw_hwpin_is_usb_connected(void)
{
FILE *fp;
char cmd[128] = {0};
char tmp_rsp[20] = {0};
//char *CONNECTED = "configured";
char *DISCONNECTED = "not attached";
if (handle())
return GSW_HAL_FAIL;
sprintf(cmd,"cat /sys/devices/platform/soc/d4200000.axi/c0000000.usb/udc/c0000000.usb/state");
fp=popen(cmd, "r");
if (fgets(tmp_rsp,sizeof(tmp_rsp),fp) == NULL)
LOGE("gsw_hwpin_is_usb_connected fail.");
pclose(fp);
if (strncmp(tmp_rsp,DISCONNECTED,strlen(DISCONNECTED)) == 0)
return GSW_HAL_USB_DISCONNECTED;
else
return GSW_HAL_USB_CONNECTED;
}
int32_t gsw_setWakeupLevel(gsw_hal_pinLevel level)
{
if (gsw_gpio_SetLevel(wakeout, level) == GSW_HAL_FAIL)
{
return GSW_HAL_FAIL;
}
return GSW_HAL_SUCCESS;
}