/libsensors/SensorBase.cpp

http://github.com/CyanogenMod/android_device_zte_blade · C++ · 122 lines · 92 code · 14 blank · 16 comment · 18 complexity · 84677eb87053299f895707a04705b648 MD5 · raw file

  1. /*
  2. * Copyright (C) 2008 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <fcntl.h>
  17. #include <errno.h>
  18. #include <math.h>
  19. #include <poll.h>
  20. #include <unistd.h>
  21. #include <dirent.h>
  22. #include <sys/select.h>
  23. #include <cutils/log.h>
  24. #include <linux/input.h>
  25. #include "SensorBase.h"
  26. /*****************************************************************************/
  27. SensorBase::SensorBase(
  28. const char* dev_name,
  29. const char* data_name)
  30. : dev_name(dev_name), data_name(data_name),
  31. dev_fd(-1), data_fd(-1)
  32. {
  33. data_fd = openInput(data_name);
  34. }
  35. SensorBase::~SensorBase() {
  36. if (data_fd >= 0) {
  37. close(data_fd);
  38. }
  39. if (dev_fd >= 0) {
  40. close(dev_fd);
  41. }
  42. }
  43. int SensorBase::open_device() {
  44. if (dev_fd<0 && dev_name) {
  45. dev_fd = open(dev_name, O_RDONLY);
  46. LOGE_IF(dev_fd<0, "Couldn't open %s (%s)", dev_name, strerror(errno));
  47. }
  48. return 0;
  49. }
  50. int SensorBase::close_device() {
  51. if (dev_fd >= 0) {
  52. close(dev_fd);
  53. dev_fd = -1;
  54. }
  55. return 0;
  56. }
  57. int SensorBase::getFd() const {
  58. return data_fd;
  59. }
  60. int SensorBase::setDelay(int32_t handle, int64_t ns) {
  61. return 0;
  62. }
  63. bool SensorBase::hasPendingEvents() const {
  64. return false;
  65. }
  66. int64_t SensorBase::getTimestamp() {
  67. struct timespec t;
  68. t.tv_sec = t.tv_nsec = 0;
  69. clock_gettime(CLOCK_MONOTONIC, &t);
  70. return int64_t(t.tv_sec)*1000000000LL + t.tv_nsec;
  71. }
  72. int SensorBase::openInput(const char* inputName) {
  73. int fd = -1;
  74. const char *dirname = "/dev/input";
  75. char devname[PATH_MAX];
  76. char *filename;
  77. DIR *dir;
  78. struct dirent *de;
  79. dir = opendir(dirname);
  80. if(dir == NULL)
  81. return -1;
  82. strcpy(devname, dirname);
  83. filename = devname + strlen(devname);
  84. *filename++ = '/';
  85. while((de = readdir(dir))) {
  86. if(de->d_name[0] == '.' &&
  87. (de->d_name[1] == '\0' ||
  88. (de->d_name[1] == '.' && de->d_name[2] == '\0')))
  89. continue;
  90. strcpy(filename, de->d_name);
  91. fd = open(devname, O_RDONLY);
  92. if (fd>=0) {
  93. char name[80];
  94. if (ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {
  95. name[0] = '\0';
  96. }
  97. if (!strcmp(name, inputName)) {
  98. break;
  99. } else {
  100. close(fd);
  101. fd = -1;
  102. }
  103. }
  104. }
  105. closedir(dir);
  106. LOGE_IF(fd<0, "couldn't find '%s' input device", inputName);
  107. return fd;
  108. }