blob: 7dd256aaa1f83f66595d03f91edf847c04d549ec [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
Ashutosh Joshi5cafc1e2017-02-09 21:44:04 +000027#include <stdint.h>
28#include <sys/types.h>
Peng Xue36e3472016-11-03 11:57:10 -070029#include <string>
Ashutosh Joshi5cafc1e2017-02-09 21:44:04 +000030
31#ifdef ENABLE_TREBLE
Andreas Huber99fdbb52016-10-10 13:22:58 -070032#include <map>
33
34#include "android/hardware/sensors/1.0/ISensors.h"
Ashutosh Joshi5cafc1e2017-02-09 21:44:04 +000035#endif
Andreas Huber99fdbb52016-10-10 13:22:58 -070036
Mathias Agopianf001c922010-11-11 17:58:51 -080037// ---------------------------------------------------------------------------
38
39namespace android {
Andreas Huber99fdbb52016-10-10 13:22:58 -070040
Mathias Agopianf001c922010-11-11 17:58:51 -080041// ---------------------------------------------------------------------------
Peng Xu6a2d3a02015-12-21 12:00:23 -080042using SensorServiceUtil::Dumpable;
Mathias Agopianf001c922010-11-11 17:58:51 -080043
Peng Xu6a2d3a02015-12-21 12:00:23 -080044class SensorDevice : public Singleton<SensorDevice>, public Dumpable {
45public:
46 ssize_t getSensorList(sensor_t const** list);
Andreas Huber99fdbb52016-10-10 13:22:58 -070047
Peng Xu6a2d3a02015-12-21 12:00:23 -080048 void handleDynamicSensorConnection(int handle, bool connected);
49 status_t initCheck() const;
50 int getHalDeviceVersion() const;
Andreas Huber99fdbb52016-10-10 13:22:58 -070051
Peng Xu6a2d3a02015-12-21 12:00:23 -080052 ssize_t poll(sensors_event_t* buffer, size_t count);
Andreas Huber99fdbb52016-10-10 13:22:58 -070053
Peng Xu6a2d3a02015-12-21 12:00:23 -080054 status_t activate(void* ident, int handle, int enabled);
55 status_t batch(void* ident, int handle, int flags, int64_t samplingPeriodNs,
56 int64_t maxBatchReportLatencyNs);
57 // Call batch with timeout zero instead of calling setDelay() for newer devices.
58 status_t setDelay(void* ident, int handle, int64_t ns);
59 status_t flush(void* ident, int handle);
60 status_t setMode(uint32_t mode);
Peng Xue36e3472016-11-03 11:57:10 -070061
Peng Xu53632542017-01-23 20:06:27 -080062 bool isDirectReportSupported() const;
Peng Xue36e3472016-11-03 11:57:10 -070063 int32_t registerDirectChannel(const sensors_direct_mem_t *memory);
64 void unregisterDirectChannel(int32_t channelHandle);
65 int32_t configureDirectChannel(int32_t sensorHandle,
66 int32_t channelHandle, const struct sensors_direct_cfg_t *config);
67
Peng Xu6a2d3a02015-12-21 12:00:23 -080068 void disableAllSensors();
69 void enableAllSensors();
70 void autoDisable(void *ident, int handle);
Andreas Huber99fdbb52016-10-10 13:22:58 -070071
Peng Xu6a2d3a02015-12-21 12:00:23 -080072 status_t injectSensorData(const sensors_event_t *event);
Peng Xu4f707f82016-09-26 11:28:32 -070073 void notifyConnectionDestroyed(void *ident);
Peng Xu6a2d3a02015-12-21 12:00:23 -080074
75 // Dumpable
76 virtual std::string dump() const;
77private:
Mathias Agopianf001c922010-11-11 17:58:51 -080078 friend class Singleton<SensorDevice>;
Ashutosh Joshi5cafc1e2017-02-09 21:44:04 +000079#ifdef ENABLE_TREBLE
Andreas Huber99fdbb52016-10-10 13:22:58 -070080 sp<android::hardware::sensors::V1_0::ISensors> mSensors;
81 Vector<sensor_t> mSensorList;
82 std::map<int32_t, sensor_t*> mConnectedDynamicSensors;
Ashutosh Joshi5cafc1e2017-02-09 21:44:04 +000083#else
84 sensors_poll_device_1_t* mSensorDevice;
85 struct sensors_module_t* mSensorModule;
86#endif
Andreas Huber99fdbb52016-10-10 13:22:58 -070087
Aravind Akella724d91d2013-06-27 12:04:23 -070088 static const nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz
89 mutable Mutex mLock; // protect mActivationCount[].batchParams
Mathias Agopianf001c922010-11-11 17:58:51 -080090 // fixed-size array after construction
Aravind Akella724d91d2013-06-27 12:04:23 -070091
92 // Struct to store all the parameters(samplingPeriod, maxBatchReportLatency and flags) from
93 // batch call. For continous mode clients, maxBatchReportLatency is set to zero.
94 struct BatchParams {
Aravind Akella4949c502015-02-11 15:54:35 -080095 // TODO: Get rid of flags parameter everywhere.
Aravind Akella724d91d2013-06-27 12:04:23 -070096 int flags;
97 nsecs_t batchDelay, batchTimeout;
98 BatchParams() : flags(0), batchDelay(0), batchTimeout(0) {}
99 BatchParams(int flag, nsecs_t delay, nsecs_t timeout): flags(flag), batchDelay(delay),
100 batchTimeout(timeout) { }
101 bool operator != (const BatchParams& other) {
102 return other.batchDelay != batchDelay || other.batchTimeout != batchTimeout ||
103 other.flags != flags;
104 }
105 };
106
107 // Store batch parameters in the KeyedVector and the optimal batch_rate and timeout in
108 // bestBatchParams. For every batch() call corresponding params are stored in batchParams
109 // vector. A continuous mode request is batch(... timeout=0 ..) followed by activate(). A batch
110 // mode request is batch(... timeout > 0 ...) followed by activate().
111 // Info is a per-sensor data structure which contains the batch parameters for each client that
112 // has registered for this sensor.
Mathias Agopianf001c922010-11-11 17:58:51 -0800113 struct Info {
Aravind Akella724d91d2013-06-27 12:04:23 -0700114 BatchParams bestBatchParams;
115 // Key is the unique identifier(ident) for each client, value is the batch parameters
116 // requested by the client.
117 KeyedVector<void*, BatchParams> batchParams;
118
Aravind Akella4949c502015-02-11 15:54:35 -0800119 Info() : bestBatchParams(0, -1, -1) {}
Aravind Akella724d91d2013-06-27 12:04:23 -0700120 // Sets batch parameters for this ident. Returns error if this ident is not already present
121 // in the KeyedVector above.
122 status_t setBatchParamsForIdent(void* ident, int flags, int64_t samplingPeriodNs,
123 int64_t maxBatchReportLatencyNs);
124 // Finds the optimal parameters for batching and stores them in bestBatchParams variable.
125 void selectBatchParams();
126 // Removes batchParams for an ident and re-computes bestBatchParams. Returns the index of
127 // the removed ident. If index >=0, ident is present and successfully removed.
128 ssize_t removeBatchParamsForIdent(void* ident);
Aravind Akella4949c502015-02-11 15:54:35 -0800129
130 int numActiveClients();
Mathias Agopianf001c922010-11-11 17:58:51 -0800131 };
132 DefaultKeyedVector<int, Info> mActivationCount;
133
Aravind Akella4949c502015-02-11 15:54:35 -0800134 // Use this vector to determine which client is activated or deactivated.
135 SortedVector<void *> mDisabledClients;
Mathias Agopianf001c922010-11-11 17:58:51 -0800136 SensorDevice();
Aravind Akella4949c502015-02-11 15:54:35 -0800137
138 bool isClientDisabled(void* ident);
139 bool isClientDisabledLocked(void* ident);
Andreas Huber99fdbb52016-10-10 13:22:58 -0700140
Ashutosh Joshi5cafc1e2017-02-09 21:44:04 +0000141#ifdef ENABLE_TREBLE
Andreas Huber99fdbb52016-10-10 13:22:58 -0700142 using Event = hardware::sensors::V1_0::Event;
143 using SensorInfo = hardware::sensors::V1_0::SensorInfo;
144
145 void convertToSensorEvent(const Event &src, sensors_event_t *dst);
146
147 void convertToSensorEvents(
148 const hardware::hidl_vec<Event> &src,
149 const hardware::hidl_vec<SensorInfo> &dynamicSensorsAdded,
150 sensors_event_t *dst);
Peng Xu53632542017-01-23 20:06:27 -0800151
152 bool mIsDirectReportSupported;
Ashutosh Joshi5cafc1e2017-02-09 21:44:04 +0000153#endif // ENABLE_TREBLE
Mathias Agopianf001c922010-11-11 17:58:51 -0800154};
155
156// ---------------------------------------------------------------------------
157}; // namespace android
158
159#endif // ANDROID_SENSOR_DEVICE_H