|
|
@ -15,142 +15,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
#include "GPIOControl.h"
|
|
|
|
#include "GPIOControl.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define GPIO_DIRECTION_IN 0
|
|
|
|
|
|
|
|
#define GPIO_DIRECTION_OUT 1
|
|
|
|
|
|
|
|
#define GPIO_VALUE_LOW 0
|
|
|
|
|
|
|
|
#define GPIO_VALUE_HIGH 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define BUFFER_MAX 3
|
|
|
|
#define IOT_PARAM_WRITE 0xAE
|
|
|
|
#define DIRECTION_MAX 48
|
|
|
|
#define IOT_PARAM_READ 0xAF
|
|
|
|
|
|
|
|
#define MAX_STRING_LEN 32
|
|
|
|
|
|
|
|
|
|
|
|
int GpioControl::exp(int gpio)
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
{
|
|
|
|
char buffer[BUFFER_MAX];
|
|
|
|
int cmd;
|
|
|
|
int len;
|
|
|
|
int value;
|
|
|
|
int fd;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
long value2;
|
|
|
|
fd = open("/sys/class/gpio/export", O_WRONLY);
|
|
|
|
char str[MAX_STRING_LEN];
|
|
|
|
if (fd < 0)
|
|
|
|
}IOT_PARAM;
|
|
|
|
{
|
|
|
|
|
|
|
|
// LOGE("Failed to open export for writing!\n");
|
|
|
|
void GpioControl::setInt(int cmd, int value)
|
|
|
|
return(0);
|
|
|
|
{
|
|
|
|
}
|
|
|
|
int fd = open("/dev/mtkgpioctrl", O_RDONLY);
|
|
|
|
|
|
|
|
IOT_PARAM param;
|
|
|
|
len = snprintf(buffer, BUFFER_MAX, "%d", gpio);
|
|
|
|
param.cmd = cmd;
|
|
|
|
if (write(fd, buffer, len) < 0)
|
|
|
|
param.value = value;
|
|
|
|
|
|
|
|
// LOGE("set_int fd=%d,cmd=%d,value=%d\r\n",fd, cmd, value);
|
|
|
|
|
|
|
|
if( fd > 0 )
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// LOGE("Fail to export gpio!\n");
|
|
|
|
int res = ioctl(fd, IOT_PARAM_WRITE, ¶m);
|
|
|
|
return 0;
|
|
|
|
// LOGE("set_int22 cmd=%d,value=%d,result=%d\r\n",param.cmd, param.value, param.result);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int GpioControl::setDirection(int gpio, int direction)
|
|
|
|
int GpioControl::getInt(int cmd)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
static const char dir_str[] = "in\0out";
|
|
|
|
int fd = open("/dev/mtkgpioctrl", O_RDONLY);
|
|
|
|
char path[DIRECTION_MAX];
|
|
|
|
// LOGE("get_int fd=%d,cmd=%d\r\n",fd, cmd);
|
|
|
|
int fd;
|
|
|
|
if( fd > 0 )
|
|
|
|
|
|
|
|
|
|
|
|
snprintf(path, DIRECTION_MAX, "/sys/class/gpio/gpio%d/direction", gpio);
|
|
|
|
|
|
|
|
fd = open(path, O_WRONLY);
|
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
// LOGE("failed to open gpio direction for writing!\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (write(fd, &dir_str[direction == GPIO_DIRECTION_IN ? 0 : 3], direction == GPIO_DIRECTION_IN ? 2 : 3) < 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// LOGE("failed to set direction!\n");
|
|
|
|
IOT_PARAM param;
|
|
|
|
return 0;
|
|
|
|
param.cmd = cmd;
|
|
|
|
|
|
|
|
ioctl(fd, IOT_PARAM_READ, ¶m);
|
|
|
|
|
|
|
|
// LOGE("get_int22 cmd=%d,value=%d,result=%d\r\n",param.cmd, param.value, param.result);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return param.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int GpioControl::readStatus(int gpio)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
char path[DIRECTION_MAX];
|
|
|
|
|
|
|
|
char value_str[3];
|
|
|
|
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
snprintf(path, DIRECTION_MAX, "/sys/class/gpio/gpio%d/value", gpio);
|
|
|
|
void GpioControl::setLong(int cmd, long value)
|
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
{
|
|
|
|
if (fd < 0)
|
|
|
|
int fd = open("/dev/mtkgpioctrl", O_RDONLY);
|
|
|
|
{
|
|
|
|
IOT_PARAM param;
|
|
|
|
// LOGE("failed to open gpio value for reading!\n");
|
|
|
|
param.cmd = cmd;
|
|
|
|
return -1;
|
|
|
|
param.value2 = value;
|
|
|
|
}
|
|
|
|
// LOGE("set_long fd=%d,cmd=%d,value2=%ld\r\n",fd, param.cmd, param.value2);
|
|
|
|
|
|
|
|
|
|
|
|
if (read(fd, value_str, 3) < 0)
|
|
|
|
if( fd > 0 )
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// LOGE("failed to read value!\n");
|
|
|
|
ioctl(fd, IOT_PARAM_WRITE, ¶m);
|
|
|
|
return -1;
|
|
|
|
// LOGE("set_long22 cmd=%d,value2=%ld,result=%d\r\n",param.cmd, param.value2, param.result);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return (atoi(value_str));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int GpioControl::writeStatus(int gpio, int value)
|
|
|
|
long GpioControl::getLong(int cmd)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
static const char values_str[] = "01";
|
|
|
|
int fd = open("/dev/mtkgpioctrl", O_RDONLY);
|
|
|
|
char path[DIRECTION_MAX];
|
|
|
|
// LOGE("get_long fd=%d,cmd=%d\r\n",fd, cmd);
|
|
|
|
int fd;
|
|
|
|
if( fd > 0 )
|
|
|
|
|
|
|
|
|
|
|
|
snprintf(path, DIRECTION_MAX, "/sys/class/gpio/gpio%d/value", gpio);
|
|
|
|
|
|
|
|
fd = open(path, O_WRONLY);
|
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
// LOGE("failed to open gpio value for writing!\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (write(fd, &values_str[value == GPIO_VALUE_LOW ? 0 : 1], 1) < 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// LOGE("failed to write value!\n");
|
|
|
|
IOT_PARAM param;
|
|
|
|
return 0;
|
|
|
|
param.cmd = cmd;
|
|
|
|
|
|
|
|
ioctl(fd, IOT_PARAM_READ, ¶m);
|
|
|
|
|
|
|
|
// LOGE("get_long22 cmd=%d,value2=%ld,result=%d\r\n",param.cmd, param.value2, param.result);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return param.value2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int GpioControl::unexp(int gpio)
|
|
|
|
void GpioControl::setString(int cmd, const std::string& value)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
char buffer[BUFFER_MAX];
|
|
|
|
IOT_PARAM param;
|
|
|
|
int len;
|
|
|
|
// char *pval = jstringToChars(env, value);
|
|
|
|
int fd;
|
|
|
|
int fd = open("/dev/mtkgpioctrl", O_RDONLY);
|
|
|
|
|
|
|
|
int len = MAX_STRING_LEN < value.size() ? MAX_STRING_LEN : value.size();
|
|
|
|
fd = open("/sys/class/gpio/unexport", O_WRONLY);
|
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
param.cmd = cmd;
|
|
|
|
|
|
|
|
memset(param.str, 0, MAX_STRING_LEN);
|
|
|
|
|
|
|
|
memcpy(param.str, value.c_str(), len);
|
|
|
|
|
|
|
|
// LOGE("set_string fd=%d,cmd=%d,str=%s\r\n",fd, param.cmd, param.str);
|
|
|
|
|
|
|
|
if( fd > 0 )
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// LOGE("Failed to open unexport for writing!\n");
|
|
|
|
ioctl(fd, IOT_PARAM_WRITE, ¶m);
|
|
|
|
return 0;
|
|
|
|
// LOGE("set_string22 cmd=%d,str=%s,result=%d\r\n",param.cmd, param.str, param.result);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
len = snprintf(buffer, BUFFER_MAX, "%d", gpio);
|
|
|
|
|
|
|
|
if (write(fd, buffer, len) < 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
// LOGE("Fail to unexport gpio!");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool GpioControl::EnableGpio(int gpio, bool en)
|
|
|
|
std::string GpioControl::getString(int cmd)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
GpioControl gpioControl;
|
|
|
|
int fd = open("/dev/mtkgpioctrl", O_RDONLY);
|
|
|
|
gpioControl.exp(gpio);
|
|
|
|
// LOGE("get_string fd=%d,cmd=%d\r\n",fd, cmd);
|
|
|
|
gpioControl.setDirection(gpio, GPIO_DIRECTION_OUT);
|
|
|
|
if( fd > 0 )
|
|
|
|
|
|
|
|
{
|
|
|
|
gpioControl.writeStatus(gpio, en ? GPIO_VALUE_HIGH : GPIO_VALUE_LOW);
|
|
|
|
IOT_PARAM param;
|
|
|
|
|
|
|
|
param.cmd = cmd;
|
|
|
|
gpioControl.unexp(gpio);
|
|
|
|
ioctl(fd, IOT_PARAM_READ, ¶m);
|
|
|
|
|
|
|
|
// LOGE("get_string22 cmd=%d,str=%s,result=%d\r\n",param.cmd, param.str, param.result);
|
|
|
|
return true;
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return std::string(param.str);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|