blob: 761b48cf55065a40eb1bc40ec56aeb5fade3061f [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
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <utils/KeyedVector.h>
24#include <utils/Singleton.h>
25#include <utils/String8.h>
26
27#include <gui/Sensor.h>
28
29// ---------------------------------------------------------------------------
30
31namespace android {
32// ---------------------------------------------------------------------------
33
Mathias Agopianf001c922010-11-11 17:58:51 -080034class SensorDevice : public Singleton<SensorDevice> {
35 friend class Singleton<SensorDevice>;
Aravind Akella724d91d2013-06-27 12:04:23 -070036 sensors_poll_device_1_t* mSensorDevice;
Mathias Agopianf001c922010-11-11 17:58:51 -080037 struct sensors_module_t* mSensorModule;
Aravind Akella724d91d2013-06-27 12:04:23 -070038 static const nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz
39 mutable Mutex mLock; // protect mActivationCount[].batchParams
Mathias Agopianf001c922010-11-11 17:58:51 -080040 // fixed-size array after construction
Aravind Akella724d91d2013-06-27 12:04:23 -070041
42 // Struct to store all the parameters(samplingPeriod, maxBatchReportLatency and flags) from
43 // batch call. For continous mode clients, maxBatchReportLatency is set to zero.
44 struct BatchParams {
45 int flags;
46 nsecs_t batchDelay, batchTimeout;
47 BatchParams() : flags(0), batchDelay(0), batchTimeout(0) {}
48 BatchParams(int flag, nsecs_t delay, nsecs_t timeout): flags(flag), batchDelay(delay),
49 batchTimeout(timeout) { }
50 bool operator != (const BatchParams& other) {
51 return other.batchDelay != batchDelay || other.batchTimeout != batchTimeout ||
52 other.flags != flags;
53 }
54 };
55
56 // Store batch parameters in the KeyedVector and the optimal batch_rate and timeout in
57 // bestBatchParams. For every batch() call corresponding params are stored in batchParams
58 // vector. A continuous mode request is batch(... timeout=0 ..) followed by activate(). A batch
59 // mode request is batch(... timeout > 0 ...) followed by activate().
60 // Info is a per-sensor data structure which contains the batch parameters for each client that
61 // has registered for this sensor.
Mathias Agopianf001c922010-11-11 17:58:51 -080062 struct Info {
Aravind Akella724d91d2013-06-27 12:04:23 -070063 BatchParams bestBatchParams;
64 // Key is the unique identifier(ident) for each client, value is the batch parameters
65 // requested by the client.
66 KeyedVector<void*, BatchParams> batchParams;
67
68 Info() : bestBatchParams(-1, -1, -1) {}
69 // Sets batch parameters for this ident. Returns error if this ident is not already present
70 // in the KeyedVector above.
71 status_t setBatchParamsForIdent(void* ident, int flags, int64_t samplingPeriodNs,
72 int64_t maxBatchReportLatencyNs);
73 // Finds the optimal parameters for batching and stores them in bestBatchParams variable.
74 void selectBatchParams();
75 // Removes batchParams for an ident and re-computes bestBatchParams. Returns the index of
76 // the removed ident. If index >=0, ident is present and successfully removed.
77 ssize_t removeBatchParamsForIdent(void* ident);
Mathias Agopianf001c922010-11-11 17:58:51 -080078 };
79 DefaultKeyedVector<int, Info> mActivationCount;
80
81 SensorDevice();
82public:
83 ssize_t getSensorList(sensor_t const** list);
84 status_t initCheck() const;
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -070085 int getHalDeviceVersion() const;
Mathias Agopianf001c922010-11-11 17:58:51 -080086 ssize_t poll(sensors_event_t* buffer, size_t count);
87 status_t activate(void* ident, int handle, int enabled);
Aravind Akella724d91d2013-06-27 12:04:23 -070088 status_t batch(void* ident, int handle, int flags, int64_t samplingPeriodNs,
89 int64_t maxBatchReportLatencyNs);
90 // Call batch with timeout zero instead of calling setDelay() for newer devices.
Mathias Agopianf001c922010-11-11 17:58:51 -080091 status_t setDelay(void* ident, int handle, int64_t ns);
Aravind Akella724d91d2013-06-27 12:04:23 -070092 status_t flush(void* ident, int handle);
Mathias Agopianac9a96d2013-07-12 02:01:16 -070093 void autoDisable(void *ident, int handle);
Mathias Agopianba02cd22013-07-03 16:20:57 -070094 void dump(String8& result);
Mathias Agopianf001c922010-11-11 17:58:51 -080095};
96
97// ---------------------------------------------------------------------------
98}; // namespace android
99
100#endif // ANDROID_SENSOR_DEVICE_H