blob: 0bb075216978fd9ecfc1314e4ab9665aae682b0d [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>
Mathias Agopianf001c922010-11-11 17:58:51 -080023#include <utils/KeyedVector.h>
24#include <utils/Singleton.h>
25#include <utils/String8.h>
26
Peng Xu6a2d3a02015-12-21 12:00:23 -080027#include <stdint.h>
28#include <sys/types.h>
Mathias Agopianf001c922010-11-11 17:58:51 -080029
Andreas Huber99fdbb52016-10-10 13:22:58 -070030#ifdef ENABLE_TREBLE
31#include <map>
32
33#include "android/hardware/sensors/1.0/ISensors.h"
34#endif
35
Mathias Agopianf001c922010-11-11 17:58:51 -080036// ---------------------------------------------------------------------------
37
38namespace android {
Andreas Huber99fdbb52016-10-10 13:22:58 -070039
Mathias Agopianf001c922010-11-11 17:58:51 -080040// ---------------------------------------------------------------------------
Peng Xu6a2d3a02015-12-21 12:00:23 -080041using SensorServiceUtil::Dumpable;
Mathias Agopianf001c922010-11-11 17:58:51 -080042
Peng Xu6a2d3a02015-12-21 12:00:23 -080043class SensorDevice : public Singleton<SensorDevice>, public Dumpable {
44public:
45 ssize_t getSensorList(sensor_t const** list);
Andreas Huber99fdbb52016-10-10 13:22:58 -070046
Peng Xu6a2d3a02015-12-21 12:00:23 -080047 void handleDynamicSensorConnection(int handle, bool connected);
48 status_t initCheck() const;
49 int getHalDeviceVersion() const;
Andreas Huber99fdbb52016-10-10 13:22:58 -070050
Peng Xu6a2d3a02015-12-21 12:00:23 -080051 ssize_t poll(sensors_event_t* buffer, size_t count);
Andreas Huber99fdbb52016-10-10 13:22:58 -070052
Peng Xu6a2d3a02015-12-21 12:00:23 -080053 status_t activate(void* ident, int handle, int enabled);
54 status_t batch(void* ident, int handle, int flags, int64_t samplingPeriodNs,
55 int64_t maxBatchReportLatencyNs);
56 // Call batch with timeout zero instead of calling setDelay() for newer devices.
57 status_t setDelay(void* ident, int handle, int64_t ns);
58 status_t flush(void* ident, int handle);
59 status_t setMode(uint32_t mode);
60 void disableAllSensors();
61 void enableAllSensors();
62 void autoDisable(void *ident, int handle);
Andreas Huber99fdbb52016-10-10 13:22:58 -070063
Peng Xu6a2d3a02015-12-21 12:00:23 -080064 status_t injectSensorData(const sensors_event_t *event);
Peng Xu4f707f82016-09-26 11:28:32 -070065 void notifyConnectionDestroyed(void *ident);
Peng Xu6a2d3a02015-12-21 12:00:23 -080066
67 // Dumpable
68 virtual std::string dump() const;
69private:
Mathias Agopianf001c922010-11-11 17:58:51 -080070 friend class Singleton<SensorDevice>;
Andreas Huber99fdbb52016-10-10 13:22:58 -070071#ifdef ENABLE_TREBLE
72 sp<android::hardware::sensors::V1_0::ISensors> mSensors;
73 Vector<sensor_t> mSensorList;
74 std::map<int32_t, sensor_t*> mConnectedDynamicSensors;
75#else
Aravind Akella724d91d2013-06-27 12:04:23 -070076 sensors_poll_device_1_t* mSensorDevice;
Mathias Agopianf001c922010-11-11 17:58:51 -080077 struct sensors_module_t* mSensorModule;
Andreas Huber99fdbb52016-10-10 13:22:58 -070078#endif
79
Aravind Akella724d91d2013-06-27 12:04:23 -070080 static const nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz
81 mutable Mutex mLock; // protect mActivationCount[].batchParams
Mathias Agopianf001c922010-11-11 17:58:51 -080082 // fixed-size array after construction
Aravind Akella724d91d2013-06-27 12:04:23 -070083
84 // Struct to store all the parameters(samplingPeriod, maxBatchReportLatency and flags) from
85 // batch call. For continous mode clients, maxBatchReportLatency is set to zero.
86 struct BatchParams {
Aravind Akella4949c502015-02-11 15:54:35 -080087 // TODO: Get rid of flags parameter everywhere.
Aravind Akella724d91d2013-06-27 12:04:23 -070088 int flags;
89 nsecs_t batchDelay, batchTimeout;
90 BatchParams() : flags(0), batchDelay(0), batchTimeout(0) {}
91 BatchParams(int flag, nsecs_t delay, nsecs_t timeout): flags(flag), batchDelay(delay),
92 batchTimeout(timeout) { }
93 bool operator != (const BatchParams& other) {
94 return other.batchDelay != batchDelay || other.batchTimeout != batchTimeout ||
95 other.flags != flags;
96 }
97 };
98
99 // Store batch parameters in the KeyedVector and the optimal batch_rate and timeout in
100 // bestBatchParams. For every batch() call corresponding params are stored in batchParams
101 // vector. A continuous mode request is batch(... timeout=0 ..) followed by activate(). A batch
102 // mode request is batch(... timeout > 0 ...) followed by activate().
103 // Info is a per-sensor data structure which contains the batch parameters for each client that
104 // has registered for this sensor.
Mathias Agopianf001c922010-11-11 17:58:51 -0800105 struct Info {
Aravind Akella724d91d2013-06-27 12:04:23 -0700106 BatchParams bestBatchParams;
107 // Key is the unique identifier(ident) for each client, value is the batch parameters
108 // requested by the client.
109 KeyedVector<void*, BatchParams> batchParams;
110
Aravind Akella4949c502015-02-11 15:54:35 -0800111 Info() : bestBatchParams(0, -1, -1) {}
Aravind Akella724d91d2013-06-27 12:04:23 -0700112 // Sets batch parameters for this ident. Returns error if this ident is not already present
113 // in the KeyedVector above.
114 status_t setBatchParamsForIdent(void* ident, int flags, int64_t samplingPeriodNs,
115 int64_t maxBatchReportLatencyNs);
116 // Finds the optimal parameters for batching and stores them in bestBatchParams variable.
117 void selectBatchParams();
118 // Removes batchParams for an ident and re-computes bestBatchParams. Returns the index of
119 // the removed ident. If index >=0, ident is present and successfully removed.
120 ssize_t removeBatchParamsForIdent(void* ident);
Aravind Akella4949c502015-02-11 15:54:35 -0800121
122 int numActiveClients();
Mathias Agopianf001c922010-11-11 17:58:51 -0800123 };
124 DefaultKeyedVector<int, Info> mActivationCount;
125
Aravind Akella4949c502015-02-11 15:54:35 -0800126 // Use this vector to determine which client is activated or deactivated.
127 SortedVector<void *> mDisabledClients;
Mathias Agopianf001c922010-11-11 17:58:51 -0800128 SensorDevice();
Aravind Akella4949c502015-02-11 15:54:35 -0800129
130 bool isClientDisabled(void* ident);
131 bool isClientDisabledLocked(void* ident);
Andreas Huber99fdbb52016-10-10 13:22:58 -0700132
133#ifdef ENABLE_TREBLE
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);
143#endif // ENABLE_TREBLE
Mathias Agopianf001c922010-11-11 17:58:51 -0800144};
145
146// ---------------------------------------------------------------------------
147}; // namespace android
148
149#endif // ANDROID_SENSOR_DEVICE_H