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