#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//--------------------------------------------------------------------------------------------------------
#include "SensorBase.h"
//--------------------------------------------------------------------------------------------------------
#ifdef LOG_TAG
#undef LOG_TAG
#endif
#define LOG_TAG "SensorBase"
#define YLOGD(...) LOGD(__VA_ARGS__)
#define YLOGI(...) LOGI(__VA_ARGS__)
#define YLOGE(...) LOGE(__VA_ARGS__)
#define YLOGW(...) LOGW(__VA_ARGS__)
//--------------------------------------------------------------------------------------------------------
SensorBase::SensorBase(const char* name) : name(name), data_fd(-1), mHasPendingEvent(false), mEnabled(0)
{
data_fd = (name);
// sysfs classpath read
if (getInputClasspath(name) < 0) YLOGE("getInputClasspath failed [%s]\n", name);
YLOGD("SensorBase::SensorBase : [Name : %s, data_fd : %d, classname : %s]\n", name, data_fd, classpath);
}
//--------------------------------------------------------------------------------------------------------
SensorBase::~SensorBase() {
if (data_fd >= 0) close(data_fd);
}
//--------------------------------------------------------------------------------------------------------
int SensorBase::getFd() const {
return data_fd;
}
//--------------------------------------------------------------------------------------------------------
int SensorBase::setDelay(int32_t handle, int64_t ns) {
int err = 0;
int msDelay = (int)(ns / 1000000LL); // convert to ms
YLOGD("SensorBase::setDelay : [Name : %s, Handle : %d, ns : %lld, delay = %d]\n", name, handle, ns, msDelay);
if (setInputAttr("delay", msDelay) < 0) {
YLOGE("setInputAttr failed -> delay : [%s]\n", name); err = -1;
}
return err;
}
//--------------------------------------------------------------------------------------------------------
int SensorBase::enable(int32_t handle, int enabled) {
int err = 0;
YLOGD("SensorBase::enable : [Name : %s, Handle : %d, enabled : %d]\n", name, handle, enabled);
// class path -> set enable, set wake
if (setInputAttr("enable", enabled) < 0) {
YLOGE("setInputAttr failed -> enable : [%s]\n", name); err = -1;
}
if (setInputAttr("wake", enabled) < 0) {
YLOGE("setInputAttr failed -> wake : [%s]\n", name); err = -1;
}
mEnabled = enabled; mHasPendingEvent = true;
return err;
}
//--------------------------------------------------------------------------------------------------------
int SensorBase::getInitialValues(sensors_vec_t* sensor, float divf) {
char buf[sizeof("-2147483647") * 3 + 1]; /* including spaces, LF and '\0' */
char *p, space[] = " ";
int i;
if (getInputAttr("data", buf, sizeof(buf)) < 0) {
for (i = 0; i < 3; i++) sensor->v[i] = 0;
return 0;
}
for (i = 0; i < 3; i++) {
if (i == 0) p = strtok(buf, space);
else p = strtok(NULL, space);
if (p == NULL) sensor->v[i] = 0;
else sensor->v[i] = atoi(p) / divf;
YLOGD("initial value[%s] [%.3f]\n", name, sensor->v[i]);
}
return 0;
}
//--------------------------------------------------------------------------------------------------------
int SensorBase::getInitialStatus(sensors_vec_t* sensor) {
int status;
if (getInputAttr("status", &status) < 0) sensor->status = 0;
else sensor->status = (int8_t)status;
YLOGD("SensorBase::getInitialStatus : [Name : %s, status : %d]\n", name, status);
return 0;
}
//--------------------------------------------------------------------------------------------------------
bool SensorBase::hasPendingEvents() const {
return mHasPendingEvent;
}
//--------------------------------------------------------------------------------------------------------
int64_t SensorBase::getTimestamp() {
struct timespec t;
t.tv_sec = t.tv_nsec = 0;
clock_gettime(CLOCK_MONOTONIC, &t);
return int64_t(t.tv_sec)*1000000000LL + t.tv_nsec;
}
//--------------------------------------------------------------------------------------------------------
int SensorBase::getInputClasspath(const char *inputName) {
DIR *dir;
const char *dirname = "/sys/class/input";
char buf[PATH_MAX];
struct dirent *de;
int fd = -1, nread, found = 0;
if (name == NULL || classpath == NULL) return -EINVAL;
// Open Input class dir
if((dir = opendir(dirname)) == NULL) return -errno;
while((de = readdir(dir))) {
if (strncmp(de->d_name, "input", strlen("input")) != 0) continue;
memset(classpath, 0x00, sizeof(classpath));
snprintf(classpath, PATH_MAX, "%s/%s/", dirname, de->d_name);
snprintf(buf, sizeof(buf), "%s/name", classpath);
if((fd = open(buf, O_RDONLY)) < 0) continue;
if ((nread = read(fd, buf, sizeof(buf))) < 0) {
close(fd);
continue;
}
buf[nread - 1] = '\0';
if (strcmp(buf, inputName) == 0) {
close(fd); closedir(dir); return 0; // Classpath found!!
}
close(fd); fd = -1;
}
closedir(dir); *classpath = '\0';
return -EINVAL; // Classpath not found
}
//--------------------------------------------------------------------------------------------------------
int SensorBase::getInputAttr(const char *attr, char *value, int len) {
char fname[PATH_MAX];
int fd, nread;
if (classpath == NULL || *classpath == '\0' || attr == NULL || value == NULL || len < 1) return -EINVAL;
memset(fname, 0x00, sizeof(fname));
snprintf(fname, sizeof(fname), "%s/%s", classpath, attr); fname[sizeof(fname) - 1] = '\0';
if((fd = open(fname, O_RDONLY)) < 0) return -errno;
if ((nread = read(fd, value, len)) < 0) {
close(fd); return -errno;
}
close(fd);
value[nread - 1] = '\0';
return 0;
}
//--------------------------------------------------------------------------------------------------------
int SensorBase::getInputAttr(const char *attr, int *value) {
char buf[sizeof("-2147483647")];
int rt;
if (value == NULL) return -EINVAL;
if ((rt = getInputAttr(attr, buf, sizeof(buf))) < 0) return rt;
*value = atoi(buf);
return 0;
}
//--------------------------------------------------------------------------------------------------------
int SensorBase::setInputAttr(const char *attr, char *value, int len) {
char fname[PATH_MAX];
int fd;
if (classpath == NULL || *classpath == '\0' || attr == NULL || value == NULL || len < 1) return -EINVAL;
memset(fname, 0x00, sizeof(fname));
snprintf(fname, sizeof(fname), "%s/%s", classpath, attr); fname[sizeof(fname) - 1] = '\0';
if((fd = open(fname, O_WRONLY)) < 0) return -errno;
if (write(fd, value, len) < 0) {
close(fd); return -errno;
}
close(fd);
return 0;
}
int SensorBase::setInputAttr(const char *attr, int value) {
char buf[sizeof("-2147483647")];
memset(buf, 0x00, sizeof(buf));
sprintf(buf, "%d", value);
return setInputAttr(attr, buf, sizeof(buf));
}
int SensorBase::openDataFd(const char *inputName) {
DIR *dir;
struct dirent *de;
const char *dirname = "/dev/input";
char *filename, devname[PATH_MAX];
int fd = -1;
if((dir = opendir(dirname)) == NULL) return -1;
memset(devname, 0x00, sizeof(devname)); strcpy(devname, dirname);
filename = devname + strlen(devname); *filename++ = '/';
while((de = readdir(dir))) {
if(de->d_name[0] == '.' && (de->d_name[1] == '\0' || (de->d_name[1] == '.' && de->d_name[2] == '\0')))
continue;
strcpy(filename, de->d_name);
if((fd = open(devname, O_RDONLY)) >= 0) {
char name[80];
if (ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) name[0] = '\0';
if (!strcmp(name, inputName)) break;
else {
close(fd); fd = -1;
}
}
}
closedir(dir);
LOGE_IF(fd < 0, "couldn't find '%s' input device", inputName);
return fd;
}
|