2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_SENSOR_DEVICE_H
18 #define ANDROID_SENSOR_DEVICE_H
20 #include "SensorServiceUtils.h"
22 #include <sensor/Sensor.h>
24 #include <sys/types.h>
25 #include <utils/KeyedVector.h>
26 #include <utils/Singleton.h>
27 #include <utils/String8.h>
30 #include <unordered_map>
31 #include <algorithm> //std::max std::min
33 #include "android/hardware/sensors/1.0/ISensors.h"
35 #include "RingBuffer.h"
37 // ---------------------------------------------------------------------------
41 // ---------------------------------------------------------------------------
42 using SensorServiceUtil::Dumpable;
43 using hardware::Return;
45 class SensorDevice : public Singleton<SensorDevice>, public Dumpable {
48 class HidlTransportErrorLog {
51 HidlTransportErrorLog() {
56 HidlTransportErrorLog(time_t ts, int count) {
61 String8 toString() const {
63 struct tm *timeInfo = localtime(&mTs);
64 result.appendFormat("%02d:%02d:%02d :: %d", timeInfo->tm_hour, timeInfo->tm_min,
65 timeInfo->tm_sec, mCount);
70 time_t mTs; // timestamp of the error
71 int mCount; // number of transport errors observed
74 ssize_t getSensorList(sensor_t const** list);
76 void handleDynamicSensorConnection(int handle, bool connected);
77 status_t initCheck() const;
78 int getHalDeviceVersion() const;
80 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);
90 bool isDirectReportSupported() const;
91 int32_t registerDirectChannel(const sensors_direct_mem_t *memory);
92 void unregisterDirectChannel(int32_t channelHandle);
93 int32_t configureDirectChannel(int32_t sensorHandle,
94 int32_t channelHandle, const struct sensors_direct_cfg_t *config);
96 void disableAllSensors();
97 void enableAllSensors();
98 void autoDisable(void *ident, int handle);
100 status_t injectSensorData(const sensors_event_t *event);
101 void notifyConnectionDestroyed(void *ident);
104 virtual std::string dump() const;
106 friend class Singleton<SensorDevice>;
108 sp<android::hardware::sensors::V1_0::ISensors> mSensors;
109 Vector<sensor_t> mSensorList;
110 std::unordered_map<int32_t, sensor_t*> mConnectedDynamicSensors;
112 static const nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz
113 mutable Mutex mLock; // protect mActivationCount[].batchParams
114 // fixed-size array after construction
116 // Struct to store all the parameters(samplingPeriod, maxBatchReportLatency and flags) from
117 // batch call. For continous mode clients, maxBatchReportLatency is set to zero.
119 nsecs_t mTSample, mTBatch;
120 BatchParams() : mTSample(INT64_MAX), mTBatch(INT64_MAX) {}
121 BatchParams(nsecs_t tSample, nsecs_t tBatch): mTSample(tSample), mTBatch(tBatch) {}
122 bool operator != (const BatchParams& other) {
123 return !(mTSample == other.mTSample && mTBatch == other.mTBatch);
125 // Merge another parameter with this one. The updated mTSample will be the min of the two.
126 // The update mTBatch will be the min of original mTBatch and the apparent batch period
127 // of the other. the apparent batch is the maximum of mTBatch and mTSample,
128 void merge(const BatchParams &other) {
129 mTSample = std::min(mTSample, other.mTSample);
130 mTBatch = std::min(mTBatch, std::max(other.mTBatch, other.mTSample));
134 // Store batch parameters in the KeyedVector and the optimal batch_rate and timeout in
135 // bestBatchParams. For every batch() call corresponding params are stored in batchParams
136 // vector. A continuous mode request is batch(... timeout=0 ..) followed by activate(). A batch
137 // mode request is batch(... timeout > 0 ...) followed by activate().
138 // Info is a per-sensor data structure which contains the batch parameters for each client that
139 // has registered for this sensor.
141 BatchParams bestBatchParams;
142 // Key is the unique identifier(ident) for each client, value is the batch parameters
143 // requested by the client.
144 KeyedVector<void*, BatchParams> batchParams;
146 // Sets batch parameters for this ident. Returns error if this ident is not already present
147 // in the KeyedVector above.
148 status_t setBatchParamsForIdent(void* ident, int flags, int64_t samplingPeriodNs,
149 int64_t maxBatchReportLatencyNs);
150 // Finds the optimal parameters for batching and stores them in bestBatchParams variable.
151 void selectBatchParams();
152 // Removes batchParams for an ident and re-computes bestBatchParams. Returns the index of
153 // the removed ident. If index >=0, ident is present and successfully removed.
154 ssize_t removeBatchParamsForIdent(void* ident);
156 int numActiveClients();
158 DefaultKeyedVector<int, Info> mActivationCount;
160 // Keep track of any hidl transport failures
161 SensorServiceUtil::RingBuffer<HidlTransportErrorLog> mHidlTransportErrors;
162 int mTotalHidlTransportErrors;
164 // Use this vector to determine which client is activated or deactivated.
165 SortedVector<void *> mDisabledClients;
167 bool connectHidlService();
169 static void handleHidlDeath(const std::string &detail);
171 static Return<T> checkReturn(Return<T> &&ret) {
173 handleHidlDeath(ret.description());
175 return std::move(ret);
178 bool isClientDisabled(void* ident);
179 bool isClientDisabledLocked(void* ident);
181 using Event = hardware::sensors::V1_0::Event;
182 using SensorInfo = hardware::sensors::V1_0::SensorInfo;
184 void convertToSensorEvent(const Event &src, sensors_event_t *dst);
186 void convertToSensorEvents(
187 const hardware::hidl_vec<Event> &src,
188 const hardware::hidl_vec<SensorInfo> &dynamicSensorsAdded,
189 sensors_event_t *dst);
191 bool mIsDirectReportSupported;
194 // ---------------------------------------------------------------------------
195 }; // namespace android
197 #endif // ANDROID_SENSOR_DEVICE_H