blob: d340da31c1d10b312d802666254c19db6c70a9fe [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
30// ---------------------------------------------------------------------------
31
32namespace android {
33// ---------------------------------------------------------------------------
Peng Xu6a2d3a02015-12-21 12:00:23 -080034using SensorServiceUtil::Dumpable;
Mathias Agopianf001c922010-11-11 17:58:51 -080035
Peng Xu6a2d3a02015-12-21 12:00:23 -080036class SensorDevice : public Singleton<SensorDevice>, public Dumpable {
37public:
38 ssize_t getSensorList(sensor_t const** list);
39 void handleDynamicSensorConnection(int handle, bool connected);
40 status_t initCheck() const;
41 int getHalDeviceVersion() const;
42 ssize_t poll(sensors_event_t* buffer, size_t count);
43 status_t activate(void* ident, int handle, int enabled);
44 status_t batch(void* ident, int handle, int flags, int64_t samplingPeriodNs,
45 int64_t maxBatchReportLatencyNs);
46 // Call batch with timeout zero instead of calling setDelay() for newer devices.
47 status_t setDelay(void* ident, int handle, int64_t ns);
48 status_t flush(void* ident, int handle);
49 status_t setMode(uint32_t mode);
50 void disableAllSensors();
51 void enableAllSensors();
52 void autoDisable(void *ident, int handle);
53 status_t injectSensorData(const sensors_event_t *event);
Peng Xu4f707f82016-09-26 11:28:32 -070054 void notifyConnectionDestroyed(void *ident);
Peng Xu6a2d3a02015-12-21 12:00:23 -080055
56 // Dumpable
57 virtual std::string dump() const;
58private:
Mathias Agopianf001c922010-11-11 17:58:51 -080059 friend class Singleton<SensorDevice>;
Aravind Akella724d91d2013-06-27 12:04:23 -070060 sensors_poll_device_1_t* mSensorDevice;
Mathias Agopianf001c922010-11-11 17:58:51 -080061 struct sensors_module_t* mSensorModule;
Aravind Akella724d91d2013-06-27 12:04:23 -070062 static const nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz
63 mutable Mutex mLock; // protect mActivationCount[].batchParams
Mathias Agopianf001c922010-11-11 17:58:51 -080064 // fixed-size array after construction
Aravind Akella724d91d2013-06-27 12:04:23 -070065
66 // Struct to store all the parameters(samplingPeriod, maxBatchReportLatency and flags) from
67 // batch call. For continous mode clients, maxBatchReportLatency is set to zero.
68 struct BatchParams {
Aravind Akella4949c502015-02-11 15:54:35 -080069 // TODO: Get rid of flags parameter everywhere.
Aravind Akella724d91d2013-06-27 12:04:23 -070070 int flags;
71 nsecs_t batchDelay, batchTimeout;
72 BatchParams() : flags(0), batchDelay(0), batchTimeout(0) {}
73 BatchParams(int flag, nsecs_t delay, nsecs_t timeout): flags(flag), batchDelay(delay),
74 batchTimeout(timeout) { }
75 bool operator != (const BatchParams& other) {
76 return other.batchDelay != batchDelay || other.batchTimeout != batchTimeout ||
77 other.flags != flags;
78 }
79 };
80
81 // Store batch parameters in the KeyedVector and the optimal batch_rate and timeout in
82 // bestBatchParams. For every batch() call corresponding params are stored in batchParams
83 // vector. A continuous mode request is batch(... timeout=0 ..) followed by activate(). A batch
84 // mode request is batch(... timeout > 0 ...) followed by activate().
85 // Info is a per-sensor data structure which contains the batch parameters for each client that
86 // has registered for this sensor.
Mathias Agopianf001c922010-11-11 17:58:51 -080087 struct Info {
Aravind Akella724d91d2013-06-27 12:04:23 -070088 BatchParams bestBatchParams;
89 // Key is the unique identifier(ident) for each client, value is the batch parameters
90 // requested by the client.
91 KeyedVector<void*, BatchParams> batchParams;
92
Aravind Akella4949c502015-02-11 15:54:35 -080093 Info() : bestBatchParams(0, -1, -1) {}
Aravind Akella724d91d2013-06-27 12:04:23 -070094 // Sets batch parameters for this ident. Returns error if this ident is not already present
95 // in the KeyedVector above.
96 status_t setBatchParamsForIdent(void* ident, int flags, int64_t samplingPeriodNs,
97 int64_t maxBatchReportLatencyNs);
98 // Finds the optimal parameters for batching and stores them in bestBatchParams variable.
99 void selectBatchParams();
100 // Removes batchParams for an ident and re-computes bestBatchParams. Returns the index of
101 // the removed ident. If index >=0, ident is present and successfully removed.
102 ssize_t removeBatchParamsForIdent(void* ident);
Aravind Akella4949c502015-02-11 15:54:35 -0800103
104 int numActiveClients();
Mathias Agopianf001c922010-11-11 17:58:51 -0800105 };
106 DefaultKeyedVector<int, Info> mActivationCount;
107
Aravind Akella4949c502015-02-11 15:54:35 -0800108 // Use this vector to determine which client is activated or deactivated.
109 SortedVector<void *> mDisabledClients;
Mathias Agopianf001c922010-11-11 17:58:51 -0800110 SensorDevice();
Aravind Akella4949c502015-02-11 15:54:35 -0800111
112 bool isClientDisabled(void* ident);
113 bool isClientDisabledLocked(void* ident);
Mathias Agopianf001c922010-11-11 17:58:51 -0800114};
115
116// ---------------------------------------------------------------------------
117}; // namespace android
118
119#endif // ANDROID_SENSOR_DEVICE_H