SensorDevice.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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_DEVICE_H
  17. #define ANDROID_SENSOR_DEVICE_H
  18. #include <stdint.h>
  19. #include <sys/types.h>
  20. #include <utils/KeyedVector.h>
  21. #include <utils/Singleton.h>
  22. #include <utils/String8.h>
  23. #include <gui/Sensor.h>
  24. // ---------------------------------------------------------------------------
  25. namespace android {
  26. // ---------------------------------------------------------------------------
  27. class SensorDevice : public Singleton<SensorDevice> {
  28. friend class Singleton<SensorDevice>;
  29. sensors_poll_device_1_t* mSensorDevice;
  30. struct sensors_module_t* mSensorModule;
  31. static const nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz
  32. mutable Mutex mLock; // protect mActivationCount[].batchParams
  33. // fixed-size array after construction
  34. // Struct to store all the parameters(samplingPeriod, maxBatchReportLatency and flags) from
  35. // batch call. For continous mode clients, maxBatchReportLatency is set to zero.
  36. struct BatchParams {
  37. // TODO: Get rid of flags parameter everywhere.
  38. int flags;
  39. nsecs_t batchDelay, batchTimeout;
  40. BatchParams() : flags(0), batchDelay(0), batchTimeout(0) {}
  41. BatchParams(int flag, nsecs_t delay, nsecs_t timeout): flags(flag), batchDelay(delay),
  42. batchTimeout(timeout) { }
  43. bool operator != (const BatchParams& other) {
  44. return other.batchDelay != batchDelay || other.batchTimeout != batchTimeout ||
  45. other.flags != flags;
  46. }
  47. };
  48. // Store batch parameters in the KeyedVector and the optimal batch_rate and timeout in
  49. // bestBatchParams. For every batch() call corresponding params are stored in batchParams
  50. // vector. A continuous mode request is batch(... timeout=0 ..) followed by activate(). A batch
  51. // mode request is batch(... timeout > 0 ...) followed by activate().
  52. // Info is a per-sensor data structure which contains the batch parameters for each client that
  53. // has registered for this sensor.
  54. struct Info {
  55. BatchParams bestBatchParams;
  56. // Key is the unique identifier(ident) for each client, value is the batch parameters
  57. // requested by the client.
  58. KeyedVector<void*, BatchParams> batchParams;
  59. Info() : bestBatchParams(0, -1, -1) {}
  60. // Sets batch parameters for this ident. Returns error if this ident is not already present
  61. // in the KeyedVector above.
  62. status_t setBatchParamsForIdent(void* ident, int flags, int64_t samplingPeriodNs,
  63. int64_t maxBatchReportLatencyNs);
  64. // Finds the optimal parameters for batching and stores them in bestBatchParams variable.
  65. void selectBatchParams();
  66. // Removes batchParams for an ident and re-computes bestBatchParams. Returns the index of
  67. // the removed ident. If index >=0, ident is present and successfully removed.
  68. ssize_t removeBatchParamsForIdent(void* ident);
  69. int numActiveClients();
  70. };
  71. DefaultKeyedVector<int, Info> mActivationCount;
  72. // Use this vector to determine which client is activated or deactivated.
  73. SortedVector<void *> mDisabledClients;
  74. SensorDevice();
  75. bool isClientDisabled(void* ident);
  76. bool isClientDisabledLocked(void* ident);
  77. public:
  78. ssize_t getSensorList(sensor_t const** list);
  79. status_t initCheck() const;
  80. int getHalDeviceVersion() const;
  81. ssize_t poll(sensors_event_t* buffer, size_t count);
  82. status_t activate(void* ident, int handle, int enabled);
  83. status_t batch(void* ident, int handle, int flags, int64_t samplingPeriodNs,
  84. int64_t maxBatchReportLatencyNs);
  85. // Call batch with timeout zero instead of calling setDelay() for newer devices.
  86. status_t setDelay(void* ident, int handle, int64_t ns);
  87. status_t flush(void* ident, int handle);
  88. status_t setMode(uint32_t mode);
  89. void disableAllSensors();
  90. void enableAllSensors();
  91. void autoDisable(void *ident, int handle);
  92. status_t injectSensorData(const sensors_event_t *event);
  93. void dump(String8& result);
  94. };
  95. // ---------------------------------------------------------------------------
  96. }; // namespace android
  97. #endif // ANDROID_SENSOR_DEVICE_H