SensorEventQueue.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #ifndef ANDROID_SENSOR_EVENT_QUEUE_H
  17. #define ANDROID_SENSOR_EVENT_QUEUE_H
  18. #include <stdint.h>
  19. #include <sys/types.h>
  20. #include <utils/Errors.h>
  21. #include <utils/RefBase.h>
  22. #include <utils/Timers.h>
  23. #include <utils/String16.h>
  24. #include <gui/BitTube.h>
  25. // ----------------------------------------------------------------------------
  26. #define WAKE_UP_SENSOR_EVENT_NEEDS_ACK (1U << 31)
  27. struct ALooper;
  28. struct ASensorEvent;
  29. // Concrete types for the NDK
  30. struct ASensorEventQueue {
  31. ALooper* looper;
  32. };
  33. // ----------------------------------------------------------------------------
  34. namespace android {
  35. // ----------------------------------------------------------------------------
  36. class ISensorEventConnection;
  37. class Sensor;
  38. class Looper;
  39. // ----------------------------------------------------------------------------
  40. class SensorEventQueue : public ASensorEventQueue, public RefBase
  41. {
  42. public:
  43. enum { MAX_RECEIVE_BUFFER_EVENT_COUNT = 256 };
  44. SensorEventQueue(const sp<ISensorEventConnection>& connection);
  45. virtual ~SensorEventQueue();
  46. virtual void onFirstRef();
  47. int getFd() const;
  48. static ssize_t write(const sp<BitTube>& tube,
  49. ASensorEvent const* events, size_t numEvents);
  50. ssize_t read(ASensorEvent* events, size_t numEvents);
  51. status_t waitForEvent() const;
  52. status_t wake() const;
  53. status_t enableSensor(Sensor const* sensor) const;
  54. status_t disableSensor(Sensor const* sensor) const;
  55. status_t setEventRate(Sensor const* sensor, nsecs_t ns) const;
  56. // these are here only to support SensorManager.java
  57. status_t enableSensor(int32_t handle, int32_t samplingPeriodUs, int maxBatchReportLatencyUs,
  58. int reservedFlags) const;
  59. status_t disableSensor(int32_t handle) const;
  60. status_t flush() const;
  61. // Send an ack for every wake_up sensor event that is set to WAKE_UP_SENSOR_EVENT_NEEDS_ACK.
  62. void sendAck(const ASensorEvent* events, int count);
  63. status_t injectSensorEvent(const ASensorEvent& event);
  64. private:
  65. sp<Looper> getLooper() const;
  66. sp<ISensorEventConnection> mSensorEventConnection;
  67. sp<BitTube> mSensorChannel;
  68. mutable Mutex mLock;
  69. mutable sp<Looper> mLooper;
  70. ASensorEvent* mRecBuffer;
  71. size_t mAvailable;
  72. size_t mConsumed;
  73. uint32_t mNumAcksToSend;
  74. };
  75. // ----------------------------------------------------------------------------
  76. }; // namespace android
  77. #endif // ANDROID_SENSOR_EVENT_QUEUE_H