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 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 20 | #include "SensorServiceUtils.h" |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 21 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 22 | #include <gui/Sensor.h> |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame^] | 23 | #include <stdint.h> |
| 24 | #include <sys/types.h> |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 25 | #include <utils/KeyedVector.h> |
| 26 | #include <utils/Singleton.h> |
| 27 | #include <utils/String8.h> |
| 28 | |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 29 | #include <string> |
Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 30 | #include <map> |
| 31 | |
| 32 | #include "android/hardware/sensors/1.0/ISensors.h" |
Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 33 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 34 | // --------------------------------------------------------------------------- |
| 35 | |
| 36 | namespace android { |
Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 37 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 38 | // --------------------------------------------------------------------------- |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 39 | using SensorServiceUtil::Dumpable; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 40 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 41 | class SensorDevice : public Singleton<SensorDevice>, public Dumpable { |
| 42 | public: |
| 43 | ssize_t getSensorList(sensor_t const** list); |
Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 44 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 45 | void handleDynamicSensorConnection(int handle, bool connected); |
| 46 | status_t initCheck() const; |
| 47 | int getHalDeviceVersion() const; |
Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 48 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 49 | ssize_t poll(sensors_event_t* buffer, size_t count); |
Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 50 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 51 | status_t activate(void* ident, int handle, int enabled); |
| 52 | status_t batch(void* ident, int handle, int flags, int64_t samplingPeriodNs, |
| 53 | int64_t maxBatchReportLatencyNs); |
| 54 | // Call batch with timeout zero instead of calling setDelay() for newer devices. |
| 55 | status_t setDelay(void* ident, int handle, int64_t ns); |
| 56 | status_t flush(void* ident, int handle); |
| 57 | status_t setMode(uint32_t mode); |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 58 | |
Peng Xu | 5363254 | 2017-01-23 20:06:27 -0800 | [diff] [blame] | 59 | bool isDirectReportSupported() const; |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 60 | int32_t registerDirectChannel(const sensors_direct_mem_t *memory); |
| 61 | void unregisterDirectChannel(int32_t channelHandle); |
| 62 | int32_t configureDirectChannel(int32_t sensorHandle, |
| 63 | int32_t channelHandle, const struct sensors_direct_cfg_t *config); |
| 64 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 65 | void disableAllSensors(); |
| 66 | void enableAllSensors(); |
| 67 | void autoDisable(void *ident, int handle); |
Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 68 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 69 | status_t injectSensorData(const sensors_event_t *event); |
Peng Xu | 4f707f8 | 2016-09-26 11:28:32 -0700 | [diff] [blame] | 70 | void notifyConnectionDestroyed(void *ident); |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 71 | |
| 72 | // Dumpable |
| 73 | virtual std::string dump() const; |
| 74 | private: |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 75 | friend class Singleton<SensorDevice>; |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame^] | 76 | |
Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 77 | sp<android::hardware::sensors::V1_0::ISensors> mSensors; |
| 78 | Vector<sensor_t> mSensorList; |
| 79 | std::map<int32_t, sensor_t*> mConnectedDynamicSensors; |
Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 80 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 81 | static const nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz |
| 82 | mutable Mutex mLock; // protect mActivationCount[].batchParams |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 83 | // fixed-size array after construction |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 84 | |
| 85 | // Struct to store all the parameters(samplingPeriod, maxBatchReportLatency and flags) from |
| 86 | // batch call. For continous mode clients, maxBatchReportLatency is set to zero. |
| 87 | struct BatchParams { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 88 | // TODO: Get rid of flags parameter everywhere. |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 89 | int flags; |
| 90 | nsecs_t batchDelay, batchTimeout; |
| 91 | BatchParams() : flags(0), batchDelay(0), batchTimeout(0) {} |
| 92 | BatchParams(int flag, nsecs_t delay, nsecs_t timeout): flags(flag), batchDelay(delay), |
| 93 | batchTimeout(timeout) { } |
| 94 | bool operator != (const BatchParams& other) { |
| 95 | return other.batchDelay != batchDelay || other.batchTimeout != batchTimeout || |
| 96 | other.flags != flags; |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | // Store batch parameters in the KeyedVector and the optimal batch_rate and timeout in |
| 101 | // bestBatchParams. For every batch() call corresponding params are stored in batchParams |
| 102 | // vector. A continuous mode request is batch(... timeout=0 ..) followed by activate(). A batch |
| 103 | // mode request is batch(... timeout > 0 ...) followed by activate(). |
| 104 | // Info is a per-sensor data structure which contains the batch parameters for each client that |
| 105 | // has registered for this sensor. |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 106 | struct Info { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 107 | BatchParams bestBatchParams; |
| 108 | // Key is the unique identifier(ident) for each client, value is the batch parameters |
| 109 | // requested by the client. |
| 110 | KeyedVector<void*, BatchParams> batchParams; |
| 111 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 112 | Info() : bestBatchParams(0, -1, -1) {} |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 113 | // Sets batch parameters for this ident. Returns error if this ident is not already present |
| 114 | // in the KeyedVector above. |
| 115 | status_t setBatchParamsForIdent(void* ident, int flags, int64_t samplingPeriodNs, |
| 116 | int64_t maxBatchReportLatencyNs); |
| 117 | // Finds the optimal parameters for batching and stores them in bestBatchParams variable. |
| 118 | void selectBatchParams(); |
| 119 | // Removes batchParams for an ident and re-computes bestBatchParams. Returns the index of |
| 120 | // the removed ident. If index >=0, ident is present and successfully removed. |
| 121 | ssize_t removeBatchParamsForIdent(void* ident); |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 122 | |
| 123 | int numActiveClients(); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 124 | }; |
| 125 | DefaultKeyedVector<int, Info> mActivationCount; |
| 126 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 127 | // Use this vector to determine which client is activated or deactivated. |
| 128 | SortedVector<void *> mDisabledClients; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 129 | SensorDevice(); |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 130 | |
| 131 | bool isClientDisabled(void* ident); |
| 132 | bool isClientDisabledLocked(void* ident); |
Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 133 | |
Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 134 | using Event = hardware::sensors::V1_0::Event; |
| 135 | using SensorInfo = hardware::sensors::V1_0::SensorInfo; |
| 136 | |
| 137 | void convertToSensorEvent(const Event &src, sensors_event_t *dst); |
| 138 | |
| 139 | void convertToSensorEvents( |
| 140 | const hardware::hidl_vec<Event> &src, |
| 141 | const hardware::hidl_vec<SensorInfo> &dynamicSensorsAdded, |
| 142 | sensors_event_t *dst); |
Peng Xu | 5363254 | 2017-01-23 20:06:27 -0800 | [diff] [blame] | 143 | |
| 144 | bool mIsDirectReportSupported; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | // --------------------------------------------------------------------------- |
| 148 | }; // namespace android |
| 149 | |
| 150 | #endif // ANDROID_SENSOR_DEVICE_H |