1f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian/*
2f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * Copyright (C) 2010 The Android Open Source Project
3f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian *
4f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
5f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * you may not use this file except in compliance with the License.
6f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * You may obtain a copy of the License at
7f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian *
8f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
9f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian *
10f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * Unless required by applicable law or agreed to in writing, software
11f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
12f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * See the License for the specific language governing permissions and
14f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * limitations under the License.
15f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian */
16f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
17f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#ifndef ANDROID_SENSOR_DEVICE_H
18f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#define ANDROID_SENSOR_DEVICE_H
19f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
20f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include <stdint.h>
21f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include <sys/types.h>
22f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
23f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include <utils/KeyedVector.h>
24f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include <utils/Singleton.h>
25f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include <utils/String8.h>
26f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
27f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include <gui/Sensor.h>
28f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
29f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian// ---------------------------------------------------------------------------
30f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
31f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopiannamespace android {
32f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian// ---------------------------------------------------------------------------
33f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
34f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianclass SensorDevice : public Singleton<SensorDevice> {
35f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    friend class Singleton<SensorDevice>;
36724d91d778e71c8186399f4955de14b54812b3edAravind Akella    sensors_poll_device_1_t* mSensorDevice;
37f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    struct sensors_module_t* mSensorModule;
38724d91d778e71c8186399f4955de14b54812b3edAravind Akella    static const nsecs_t MINIMUM_EVENTS_PERIOD =   1000000; // 1000 Hz
39724d91d778e71c8186399f4955de14b54812b3edAravind Akella    mutable Mutex mLock; // protect mActivationCount[].batchParams
40f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    // fixed-size array after construction
41724d91d778e71c8186399f4955de14b54812b3edAravind Akella
42724d91d778e71c8186399f4955de14b54812b3edAravind Akella    // Struct to store all the parameters(samplingPeriod, maxBatchReportLatency and flags) from
43724d91d778e71c8186399f4955de14b54812b3edAravind Akella    // batch call. For continous mode clients, maxBatchReportLatency is set to zero.
44724d91d778e71c8186399f4955de14b54812b3edAravind Akella    struct BatchParams {
45724d91d778e71c8186399f4955de14b54812b3edAravind Akella      int flags;
46724d91d778e71c8186399f4955de14b54812b3edAravind Akella      nsecs_t batchDelay, batchTimeout;
47724d91d778e71c8186399f4955de14b54812b3edAravind Akella      BatchParams() : flags(0), batchDelay(0), batchTimeout(0) {}
48724d91d778e71c8186399f4955de14b54812b3edAravind Akella      BatchParams(int flag, nsecs_t delay, nsecs_t timeout): flags(flag), batchDelay(delay),
49724d91d778e71c8186399f4955de14b54812b3edAravind Akella          batchTimeout(timeout) { }
50724d91d778e71c8186399f4955de14b54812b3edAravind Akella      bool operator != (const BatchParams& other) {
51724d91d778e71c8186399f4955de14b54812b3edAravind Akella          return other.batchDelay != batchDelay || other.batchTimeout != batchTimeout ||
52724d91d778e71c8186399f4955de14b54812b3edAravind Akella                 other.flags != flags;
53724d91d778e71c8186399f4955de14b54812b3edAravind Akella      }
54724d91d778e71c8186399f4955de14b54812b3edAravind Akella    };
55724d91d778e71c8186399f4955de14b54812b3edAravind Akella
56724d91d778e71c8186399f4955de14b54812b3edAravind Akella    // Store batch parameters in the KeyedVector and the optimal batch_rate and timeout in
57724d91d778e71c8186399f4955de14b54812b3edAravind Akella    // bestBatchParams. For every batch() call corresponding params are stored in batchParams
58724d91d778e71c8186399f4955de14b54812b3edAravind Akella    // vector. A continuous mode request is batch(... timeout=0 ..) followed by activate(). A batch
59724d91d778e71c8186399f4955de14b54812b3edAravind Akella    // mode request is batch(... timeout > 0 ...) followed by activate().
60724d91d778e71c8186399f4955de14b54812b3edAravind Akella    // Info is a per-sensor data structure which contains the batch parameters for each client that
61724d91d778e71c8186399f4955de14b54812b3edAravind Akella    // has registered for this sensor.
62f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    struct Info {
63724d91d778e71c8186399f4955de14b54812b3edAravind Akella        BatchParams bestBatchParams;
64724d91d778e71c8186399f4955de14b54812b3edAravind Akella        // Key is the unique identifier(ident) for each client, value is the batch parameters
65724d91d778e71c8186399f4955de14b54812b3edAravind Akella        // requested by the client.
66724d91d778e71c8186399f4955de14b54812b3edAravind Akella        KeyedVector<void*, BatchParams> batchParams;
67724d91d778e71c8186399f4955de14b54812b3edAravind Akella
68724d91d778e71c8186399f4955de14b54812b3edAravind Akella        Info() : bestBatchParams(-1, -1, -1) {}
69724d91d778e71c8186399f4955de14b54812b3edAravind Akella        // Sets batch parameters for this ident. Returns error if this ident is not already present
70724d91d778e71c8186399f4955de14b54812b3edAravind Akella        // in the KeyedVector above.
71724d91d778e71c8186399f4955de14b54812b3edAravind Akella        status_t setBatchParamsForIdent(void* ident, int flags, int64_t samplingPeriodNs,
72724d91d778e71c8186399f4955de14b54812b3edAravind Akella                                        int64_t maxBatchReportLatencyNs);
73724d91d778e71c8186399f4955de14b54812b3edAravind Akella        // Finds the optimal parameters for batching and stores them in bestBatchParams variable.
74724d91d778e71c8186399f4955de14b54812b3edAravind Akella        void selectBatchParams();
75724d91d778e71c8186399f4955de14b54812b3edAravind Akella        // Removes batchParams for an ident and re-computes bestBatchParams. Returns the index of
76724d91d778e71c8186399f4955de14b54812b3edAravind Akella        // the removed ident. If index >=0, ident is present and successfully removed.
77724d91d778e71c8186399f4955de14b54812b3edAravind Akella        ssize_t removeBatchParamsForIdent(void* ident);
78f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    };
79f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    DefaultKeyedVector<int, Info> mActivationCount;
80f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
81f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    SensorDevice();
82f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianpublic:
83f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    ssize_t getSensorList(sensor_t const** list);
84f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    status_t initCheck() const;
854342fdf14ffb792a36c1de25ad14b745df628da2Jaikumar Ganesh    int getHalDeviceVersion() const;
86f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    ssize_t poll(sensors_event_t* buffer, size_t count);
87f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    status_t activate(void* ident, int handle, int enabled);
88724d91d778e71c8186399f4955de14b54812b3edAravind Akella    status_t batch(void* ident, int handle, int flags, int64_t samplingPeriodNs,
89724d91d778e71c8186399f4955de14b54812b3edAravind Akella                   int64_t maxBatchReportLatencyNs);
90724d91d778e71c8186399f4955de14b54812b3edAravind Akella    // Call batch with timeout zero instead of calling setDelay() for newer devices.
91f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    status_t setDelay(void* ident, int handle, int64_t ns);
92724d91d778e71c8186399f4955de14b54812b3edAravind Akella    status_t flush(void* ident, int handle);
93ac9a96da65f6eae4513654adaad8a457d1c1575cMathias Agopian    void autoDisable(void *ident, int handle);
94ba02cd2f6cc3f59adf66cb2b9176bfe6c9e382d1Mathias Agopian    void dump(String8& result);
95f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian};
96f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
97f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian// ---------------------------------------------------------------------------
98f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian}; // namespace android
99f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
100f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#endif // ANDROID_SENSOR_DEVICE_H
101