blob: 55a745fb2eaff7b064a6d5d64993e5348005d9a4 [file] [log] [blame]
Mathias Agopianf001c922010-11-11 17:58:51 -08001/*
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 Xu6a2d3a02015-12-21 12:00:23 -080020#include "SensorServiceUtils.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080021
Peng Xu6a2d3a02015-12-21 12:00:23 -080022#include <gui/Sensor.h>
Steven Morelandd15c0302016-12-20 11:14:50 -080023#include <stdint.h>
24#include <sys/types.h>
Mathias Agopianf001c922010-11-11 17:58:51 -080025#include <utils/KeyedVector.h>
26#include <utils/Singleton.h>
27#include <utils/String8.h>
28
Peng Xue36e3472016-11-03 11:57:10 -070029#include <string>
Andreas Huber99fdbb52016-10-10 13:22:58 -070030#include <map>
31
32#include "android/hardware/sensors/1.0/ISensors.h"
Andreas Huber99fdbb52016-10-10 13:22:58 -070033
Mathias Agopianf001c922010-11-11 17:58:51 -080034// ---------------------------------------------------------------------------
35
36namespace android {
Andreas Huber99fdbb52016-10-10 13:22:58 -070037
Mathias Agopianf001c922010-11-11 17:58:51 -080038// ---------------------------------------------------------------------------
Peng Xu6a2d3a02015-12-21 12:00:23 -080039using SensorServiceUtil::Dumpable;
Peng Xu3889e6e2017-03-02 19:10:38 -080040using hardware::Return;
Mathias Agopianf001c922010-11-11 17:58:51 -080041
Peng Xu6a2d3a02015-12-21 12:00:23 -080042class SensorDevice : public Singleton<SensorDevice>, public Dumpable {
43public:
44 ssize_t getSensorList(sensor_t const** list);
Andreas Huber99fdbb52016-10-10 13:22:58 -070045
Peng Xu6a2d3a02015-12-21 12:00:23 -080046 void handleDynamicSensorConnection(int handle, bool connected);
47 status_t initCheck() const;
48 int getHalDeviceVersion() const;
Andreas Huber99fdbb52016-10-10 13:22:58 -070049
Peng Xu6a2d3a02015-12-21 12:00:23 -080050 ssize_t poll(sensors_event_t* buffer, size_t count);
Andreas Huber99fdbb52016-10-10 13:22:58 -070051
Peng Xu6a2d3a02015-12-21 12:00:23 -080052 status_t activate(void* ident, int handle, int enabled);
53 status_t batch(void* ident, int handle, int flags, int64_t samplingPeriodNs,
54 int64_t maxBatchReportLatencyNs);
55 // Call batch with timeout zero instead of calling setDelay() for newer devices.
56 status_t setDelay(void* ident, int handle, int64_t ns);
57 status_t flush(void* ident, int handle);
58 status_t setMode(uint32_t mode);
Peng Xue36e3472016-11-03 11:57:10 -070059
Peng Xu53632542017-01-23 20:06:27 -080060 bool isDirectReportSupported() const;
Peng Xue36e3472016-11-03 11:57:10 -070061 int32_t registerDirectChannel(const sensors_direct_mem_t *memory);
62 void unregisterDirectChannel(int32_t channelHandle);
63 int32_t configureDirectChannel(int32_t sensorHandle,
64 int32_t channelHandle, const struct sensors_direct_cfg_t *config);
65
Peng Xu6a2d3a02015-12-21 12:00:23 -080066 void disableAllSensors();
67 void enableAllSensors();
68 void autoDisable(void *ident, int handle);
Andreas Huber99fdbb52016-10-10 13:22:58 -070069
Peng Xu6a2d3a02015-12-21 12:00:23 -080070 status_t injectSensorData(const sensors_event_t *event);
Peng Xu4f707f82016-09-26 11:28:32 -070071 void notifyConnectionDestroyed(void *ident);
Peng Xu6a2d3a02015-12-21 12:00:23 -080072
73 // Dumpable
74 virtual std::string dump() const;
75private:
Mathias Agopianf001c922010-11-11 17:58:51 -080076 friend class Singleton<SensorDevice>;
Steven Morelandd15c0302016-12-20 11:14:50 -080077
Andreas Huber99fdbb52016-10-10 13:22:58 -070078 sp<android::hardware::sensors::V1_0::ISensors> mSensors;
79 Vector<sensor_t> mSensorList;
80 std::map<int32_t, sensor_t*> mConnectedDynamicSensors;
Andreas Huber99fdbb52016-10-10 13:22:58 -070081
Aravind Akella724d91d2013-06-27 12:04:23 -070082 static const nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz
83 mutable Mutex mLock; // protect mActivationCount[].batchParams
Mathias Agopianf001c922010-11-11 17:58:51 -080084 // fixed-size array after construction
Aravind Akella724d91d2013-06-27 12:04:23 -070085
86 // Struct to store all the parameters(samplingPeriod, maxBatchReportLatency and flags) from
87 // batch call. For continous mode clients, maxBatchReportLatency is set to zero.
88 struct BatchParams {
Aravind Akella4949c502015-02-11 15:54:35 -080089 // TODO: Get rid of flags parameter everywhere.
Aravind Akella724d91d2013-06-27 12:04:23 -070090 int flags;
91 nsecs_t batchDelay, batchTimeout;
92 BatchParams() : flags(0), batchDelay(0), batchTimeout(0) {}
93 BatchParams(int flag, nsecs_t delay, nsecs_t timeout): flags(flag), batchDelay(delay),
94 batchTimeout(timeout) { }
95 bool operator != (const BatchParams& other) {
96 return other.batchDelay != batchDelay || other.batchTimeout != batchTimeout ||
97 other.flags != flags;
98 }
99 };
100
101 // Store batch parameters in the KeyedVector and the optimal batch_rate and timeout in
102 // bestBatchParams. For every batch() call corresponding params are stored in batchParams
103 // vector. A continuous mode request is batch(... timeout=0 ..) followed by activate(). A batch
104 // mode request is batch(... timeout > 0 ...) followed by activate().
105 // Info is a per-sensor data structure which contains the batch parameters for each client that
106 // has registered for this sensor.
Mathias Agopianf001c922010-11-11 17:58:51 -0800107 struct Info {
Aravind Akella724d91d2013-06-27 12:04:23 -0700108 BatchParams bestBatchParams;
109 // Key is the unique identifier(ident) for each client, value is the batch parameters
110 // requested by the client.
111 KeyedVector<void*, BatchParams> batchParams;
112
Aravind Akella4949c502015-02-11 15:54:35 -0800113 Info() : bestBatchParams(0, -1, -1) {}
Aravind Akella724d91d2013-06-27 12:04:23 -0700114 // Sets batch parameters for this ident. Returns error if this ident is not already present
115 // in the KeyedVector above.
116 status_t setBatchParamsForIdent(void* ident, int flags, int64_t samplingPeriodNs,
117 int64_t maxBatchReportLatencyNs);
118 // Finds the optimal parameters for batching and stores them in bestBatchParams variable.
119 void selectBatchParams();
120 // Removes batchParams for an ident and re-computes bestBatchParams. Returns the index of
121 // the removed ident. If index >=0, ident is present and successfully removed.
122 ssize_t removeBatchParamsForIdent(void* ident);
Aravind Akella4949c502015-02-11 15:54:35 -0800123
124 int numActiveClients();
Mathias Agopianf001c922010-11-11 17:58:51 -0800125 };
126 DefaultKeyedVector<int, Info> mActivationCount;
127
Aravind Akella4949c502015-02-11 15:54:35 -0800128 // Use this vector to determine which client is activated or deactivated.
129 SortedVector<void *> mDisabledClients;
Mathias Agopianf001c922010-11-11 17:58:51 -0800130 SensorDevice();
Aravind Akella4949c502015-02-11 15:54:35 -0800131
Peng Xu3889e6e2017-03-02 19:10:38 -0800132 static void handleHidlDeath(const std::string &detail);
133 template<typename T>
134 static Return<T> checkReturn(Return<T> &&ret) {
135 if (!ret.isOk()) {
136 handleHidlDeath(ret.description());
137 }
138 return std::move(ret);
139 }
140
Aravind Akella4949c502015-02-11 15:54:35 -0800141 bool isClientDisabled(void* ident);
142 bool isClientDisabledLocked(void* ident);
Andreas Huber99fdbb52016-10-10 13:22:58 -0700143
Andreas Huber99fdbb52016-10-10 13:22:58 -0700144 using Event = hardware::sensors::V1_0::Event;
145 using SensorInfo = hardware::sensors::V1_0::SensorInfo;
146
147 void convertToSensorEvent(const Event &src, sensors_event_t *dst);
148
149 void convertToSensorEvents(
150 const hardware::hidl_vec<Event> &src,
151 const hardware::hidl_vec<SensorInfo> &dynamicSensorsAdded,
152 sensors_event_t *dst);
Peng Xu53632542017-01-23 20:06:27 -0800153
154 bool mIsDirectReportSupported;
Mathias Agopianf001c922010-11-11 17:58:51 -0800155};
156
157// ---------------------------------------------------------------------------
158}; // namespace android
159
160#endif // ANDROID_SENSOR_DEVICE_H