SensorEventQueue.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (C) 2010 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. #define LOG_TAG "Sensors"
  17. #include <algorithm>
  18. #include <stdint.h>
  19. #include <sys/types.h>
  20. #include <sys/socket.h>
  21. #include <linux/errno.h>
  22. #include <utils/Errors.h>
  23. #include <utils/RefBase.h>
  24. #include <utils/Looper.h>
  25. #include <gui/Sensor.h>
  26. #include <gui/BitTube.h>
  27. #include <gui/SensorEventQueue.h>
  28. #include <gui/ISensorEventConnection.h>
  29. #include <android/sensor.h>
  30. using std::min;
  31. // ----------------------------------------------------------------------------
  32. namespace android {
  33. // ----------------------------------------------------------------------------
  34. SensorEventQueue::SensorEventQueue(const sp<ISensorEventConnection>& connection)
  35. : mSensorEventConnection(connection), mRecBuffer(NULL), mAvailable(0), mConsumed(0),
  36. mNumAcksToSend(0) {
  37. mRecBuffer = new ASensorEvent[MAX_RECEIVE_BUFFER_EVENT_COUNT];
  38. }
  39. SensorEventQueue::~SensorEventQueue() {
  40. delete [] mRecBuffer;
  41. }
  42. void SensorEventQueue::onFirstRef()
  43. {
  44. mSensorChannel = mSensorEventConnection->getSensorChannel();
  45. }
  46. int SensorEventQueue::getFd() const
  47. {
  48. return mSensorChannel->getFd();
  49. }
  50. ssize_t SensorEventQueue::write(const sp<BitTube>& tube,
  51. ASensorEvent const* events, size_t numEvents) {
  52. return BitTube::sendObjects(tube, events, numEvents);
  53. }
  54. ssize_t SensorEventQueue::read(ASensorEvent* events, size_t numEvents) {
  55. if (mAvailable == 0) {
  56. ssize_t err = BitTube::recvObjects(mSensorChannel,
  57. mRecBuffer, MAX_RECEIVE_BUFFER_EVENT_COUNT);
  58. if (err < 0) {
  59. return err;
  60. }
  61. mAvailable = static_cast<size_t>(err);
  62. mConsumed = 0;
  63. }
  64. size_t count = min(numEvents, mAvailable);
  65. memcpy(events, mRecBuffer + mConsumed, count * sizeof(ASensorEvent));
  66. mAvailable -= count;
  67. mConsumed += count;
  68. return static_cast<ssize_t>(count);
  69. }
  70. sp<Looper> SensorEventQueue::getLooper() const
  71. {
  72. Mutex::Autolock _l(mLock);
  73. if (mLooper == 0) {
  74. mLooper = new Looper(true);
  75. mLooper->addFd(getFd(), getFd(), ALOOPER_EVENT_INPUT, NULL, NULL);
  76. }
  77. return mLooper;
  78. }
  79. status_t SensorEventQueue::waitForEvent() const
  80. {
  81. const int fd = getFd();
  82. sp<Looper> looper(getLooper());
  83. int events;
  84. int32_t result;
  85. do {
  86. result = looper->pollOnce(-1, NULL, &events, NULL);
  87. if (result == ALOOPER_POLL_ERROR) {
  88. ALOGE("SensorEventQueue::waitForEvent error (errno=%d)", errno);
  89. result = -EPIPE; // unknown error, so we make up one
  90. break;
  91. }
  92. if (events & ALOOPER_EVENT_HANGUP) {
  93. // the other-side has died
  94. ALOGE("SensorEventQueue::waitForEvent error HANGUP");
  95. result = -EPIPE; // unknown error, so we make up one
  96. break;
  97. }
  98. } while (result != fd);
  99. return (result == fd) ? status_t(NO_ERROR) : result;
  100. }
  101. status_t SensorEventQueue::wake() const
  102. {
  103. sp<Looper> looper(getLooper());
  104. looper->wake();
  105. return NO_ERROR;
  106. }
  107. status_t SensorEventQueue::enableSensor(Sensor const* sensor) const {
  108. return mSensorEventConnection->enableDisable(sensor->getHandle(), true, 0, 0, false);
  109. }
  110. status_t SensorEventQueue::disableSensor(Sensor const* sensor) const {
  111. return mSensorEventConnection->enableDisable(sensor->getHandle(), false, 0, 0, false);
  112. }
  113. status_t SensorEventQueue::enableSensor(int32_t handle, int32_t samplingPeriodUs,
  114. int maxBatchReportLatencyUs, int reservedFlags) const {
  115. return mSensorEventConnection->enableDisable(handle, true, us2ns(samplingPeriodUs),
  116. us2ns(maxBatchReportLatencyUs), reservedFlags);
  117. }
  118. status_t SensorEventQueue::flush() const {
  119. return mSensorEventConnection->flush();
  120. }
  121. status_t SensorEventQueue::disableSensor(int32_t handle) const {
  122. return mSensorEventConnection->enableDisable(handle, false, 0, 0, false);
  123. }
  124. status_t SensorEventQueue::setEventRate(Sensor const* sensor, nsecs_t ns) const {
  125. return mSensorEventConnection->setEventRate(sensor->getHandle(), ns);
  126. }
  127. status_t SensorEventQueue::injectSensorEvent(const ASensorEvent& event) {
  128. do {
  129. // Blocking call.
  130. ssize_t size = ::send(mSensorChannel->getFd(), &event, sizeof(event), MSG_NOSIGNAL);
  131. if (size >= 0) {
  132. return NO_ERROR;
  133. } else if (size < 0 && errno == EAGAIN) {
  134. // If send is returning a "Try again" error, sleep for 100ms and try again. In all
  135. // other cases log a failure and exit.
  136. usleep(100000);
  137. } else {
  138. ALOGE("injectSensorEvent failure %s %zd", strerror(errno), size);
  139. return INVALID_OPERATION;
  140. }
  141. } while (true);
  142. }
  143. void SensorEventQueue::sendAck(const ASensorEvent* events, int count) {
  144. for (int i = 0; i < count; ++i) {
  145. if (events[i].flags & WAKE_UP_SENSOR_EVENT_NEEDS_ACK) {
  146. ++mNumAcksToSend;
  147. }
  148. }
  149. // Send mNumAcksToSend to acknowledge for the wake up sensor events received.
  150. if (mNumAcksToSend > 0) {
  151. ssize_t size = ::send(mSensorChannel->getFd(), &mNumAcksToSend, sizeof(mNumAcksToSend),
  152. MSG_DONTWAIT | MSG_NOSIGNAL);
  153. if (size < 0) {
  154. ALOGE("sendAck failure %zd %d", size, mNumAcksToSend);
  155. } else {
  156. mNumAcksToSend = 0;
  157. }
  158. }
  159. return;
  160. }
  161. // ----------------------------------------------------------------------------
  162. }; // namespace android