• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "SensorServiceUtils.h"
21 
22 #include <gui/Sensor.h>
23 #include <utils/KeyedVector.h>
24 #include <utils/Singleton.h>
25 #include <utils/String8.h>
26 
27 #include <stdint.h>
28 #include <sys/types.h>
29 
30 // ---------------------------------------------------------------------------
31 
32 namespace android {
33 // ---------------------------------------------------------------------------
34 using SensorServiceUtil::Dumpable;
35 
36 class SensorDevice : public Singleton<SensorDevice>, public Dumpable {
37 public:
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);
54     void notifyConnectionDestroyed(void *ident);
55 
56     // Dumpable
57     virtual std::string dump() const;
58 private:
59     friend class Singleton<SensorDevice>;
60     sensors_poll_device_1_t* mSensorDevice;
61     struct sensors_module_t* mSensorModule;
62     static const nsecs_t MINIMUM_EVENTS_PERIOD =   1000000; // 1000 Hz
63     mutable Mutex mLock; // protect mActivationCount[].batchParams
64     // fixed-size array after construction
65 
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 {
69       // TODO: Get rid of flags parameter everywhere.
70       int flags;
71       nsecs_t batchDelay, batchTimeout;
BatchParamsBatchParams72       BatchParams() : flags(0), batchDelay(0), batchTimeout(0) {}
BatchParamsBatchParams73       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.
87     struct Info {
88         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 
InfoInfo93         Info() : bestBatchParams(0, -1, -1) {}
94         // 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);
103 
104         int numActiveClients();
105     };
106     DefaultKeyedVector<int, Info> mActivationCount;
107 
108     // Use this vector to determine which client is activated or deactivated.
109     SortedVector<void *> mDisabledClients;
110     SensorDevice();
111 
112     bool isClientDisabled(void* ident);
113     bool isClientDisabledLocked(void* ident);
114 };
115 
116 // ---------------------------------------------------------------------------
117 }; // namespace android
118 
119 #endif // ANDROID_SENSOR_DEVICE_H
120