• 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_INTERFACE_H
18 #define ANDROID_SENSOR_INTERFACE_H
19 
20 #include <sensor/Sensor.h>
21 #include <utils/RefBase.h>
22 
23 // ---------------------------------------------------------------------------
24 
25 namespace android {
26 // ---------------------------------------------------------------------------
27 class SensorDevice;
28 class SensorFusion;
29 class SensorService;
30 
31 class SensorInterface : public VirtualLightRefBase {
32 public:
~SensorInterface()33     virtual ~SensorInterface() {}
34 
35     virtual bool process(sensors_event_t* outEvent, const sensors_event_t& event) = 0;
36 
37     virtual status_t activate(void* ident, bool enabled) = 0;
38     virtual status_t setDelay(void* ident, int handle, int64_t ns) = 0;
39     virtual status_t batch(void* ident, int handle, int /*flags*/, int64_t samplingPeriodNs,
40                            int64_t maxBatchReportLatencyNs) = 0;
41 
42     virtual status_t flush(void* /*ident*/, int /*handle*/) = 0;
43 
44     virtual const Sensor& getSensor() const = 0;
45     virtual bool isVirtual() const = 0;
46     virtual void autoDisable(void* /*ident*/, int /*handle*/) = 0;
47 
48     virtual void willDisableAllSensors() = 0;
49     virtual void didEnableAllSensors() = 0;
50 };
51 
52 class BaseSensor : public SensorInterface {
53 public:
54     explicit BaseSensor(const sensor_t& sensor);
55     BaseSensor(const sensor_t& sensor, const uint8_t (&uuid)[16]);
56 
57     // Not all sensors need to support batching.
batch(void * ident,int handle,int,int64_t samplingPeriodNs,int64_t maxBatchReportLatencyNs)58     virtual status_t batch(void* ident, int handle, int, int64_t samplingPeriodNs,
59                            int64_t maxBatchReportLatencyNs) override {
60         if (maxBatchReportLatencyNs == 0) {
61             return setDelay(ident, handle, samplingPeriodNs);
62         }
63         return -EINVAL;
64     }
65 
flush(void *,int)66     virtual status_t flush(void* /*ident*/, int /*handle*/) override {
67         return -EINVAL;
68     }
69 
getSensor()70     virtual const Sensor& getSensor() const override { return mSensor; }
autoDisable(void *,int)71     virtual void autoDisable(void* /*ident*/, int /*handle*/) override { }
72 
willDisableAllSensors()73     virtual void willDisableAllSensors() override { }
didEnableAllSensors()74     virtual void didEnableAllSensors() override { }
75 protected:
76     SensorDevice& mSensorDevice;
77     Sensor mSensor;
78 };
79 
80 // ---------------------------------------------------------------------------
81 
82 class HardwareSensor : public BaseSensor {
83 public:
84     explicit HardwareSensor(const sensor_t& sensor);
85     HardwareSensor(const sensor_t& sensor, const uint8_t (&uuid)[16]);
86 
87     virtual ~HardwareSensor();
88 
89     virtual bool process(sensors_event_t* outEvent,
90             const sensors_event_t& event);
91 
92     virtual status_t activate(void* ident, bool enabled) override;
93     virtual status_t batch(void* ident, int handle, int flags, int64_t samplingPeriodNs,
94                            int64_t maxBatchReportLatencyNs) override;
95     virtual status_t setDelay(void* ident, int handle, int64_t ns) override;
96     virtual status_t flush(void* ident, int handle) override;
isVirtual()97     virtual bool isVirtual() const override { return false; }
98     virtual void autoDisable(void *ident, int handle) override;
99 };
100 
101 class VirtualSensor : public BaseSensor
102 {
103 public:
104     VirtualSensor();
isVirtual()105     virtual bool isVirtual() const override { return true; }
106 protected:
107     SensorFusion& mSensorFusion;
108 };
109 
110 // ---------------------------------------------------------------------------
111 
112 class ProximitySensor : public HardwareSensor {
113 public:
114     explicit ProximitySensor(const sensor_t& sensor, SensorService& service);
115 
116     status_t activate(void* ident, bool enabled) override;
117 
118     void willDisableAllSensors() override;
119     void didEnableAllSensors() override;
120 private:
121     SensorService& mSensorService;
122 };
123 
124 // ---------------------------------------------------------------------------
125 }; // namespace android
126 
127 #endif // ANDROID_SENSOR_INTERFACE_H
128