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 #include "SensorInterface.h"
18 #include "SensorDevice.h"
19 #include "SensorFusion.h"
20 #include "SensorService.h"
21 
22 #include <stdint.h>
23 #include <sys/types.h>
24 
25 namespace android {
26 // ---------------------------------------------------------------------------
27 
28 namespace {
29 const sensor_t DUMMY_SENSOR = {
30         .name = "", .vendor = "", .stringType = "", .requiredPermission = ""};
31 } //unnamed namespace
32 
BaseSensor(const sensor_t & sensor)33 BaseSensor::BaseSensor(const sensor_t& sensor) :
34         mSensorDevice(SensorDevice::getInstance()),
35         mSensor(&sensor, mSensorDevice.getHalDeviceVersion()) {
36 }
37 
BaseSensor(const sensor_t & sensor,const uint8_t (& uuid)[16])38 BaseSensor::BaseSensor(const sensor_t& sensor, const uint8_t (&uuid)[16]) :
39         mSensorDevice(SensorDevice::getInstance()),
40         mSensor(sensor, Sensor::uuid_t(uuid), mSensorDevice.getHalDeviceVersion()) {
41 }
42 
43 // ---------------------------------------------------------------------------
44 
HardwareSensor(const sensor_t & sensor)45 HardwareSensor::HardwareSensor(const sensor_t& sensor):
46         BaseSensor(sensor) {
47 }
48 
HardwareSensor(const sensor_t & sensor,const uint8_t (& uuid)[16])49 HardwareSensor::HardwareSensor(const sensor_t& sensor, const uint8_t (&uuid)[16]):
50         BaseSensor(sensor, uuid) {
51 }
52 
~HardwareSensor()53 HardwareSensor::~HardwareSensor() {
54 }
55 
process(sensors_event_t * outEvent,const sensors_event_t & event)56 bool HardwareSensor::process(sensors_event_t* outEvent,
57         const sensors_event_t& event) {
58     *outEvent = event;
59     return true;
60 }
61 
activate(void * ident,bool enabled)62 status_t HardwareSensor::activate(void* ident, bool enabled) {
63     return mSensorDevice.activate(ident, mSensor.getHandle(), enabled);
64 }
65 
batch(void * ident,int,int flags,int64_t samplingPeriodNs,int64_t maxBatchReportLatencyNs)66 status_t HardwareSensor::batch(void* ident, int /*handle*/, int flags,
67                                int64_t samplingPeriodNs, int64_t maxBatchReportLatencyNs) {
68     return mSensorDevice.batch(ident, mSensor.getHandle(), flags, samplingPeriodNs,
69                                maxBatchReportLatencyNs);
70 }
71 
flush(void * ident,int handle)72 status_t HardwareSensor::flush(void* ident, int handle) {
73     return mSensorDevice.flush(ident, handle);
74 }
75 
setDelay(void * ident,int handle,int64_t ns)76 status_t HardwareSensor::setDelay(void* ident, int handle, int64_t ns) {
77     return mSensorDevice.setDelay(ident, handle, ns);
78 }
79 
autoDisable(void * ident,int handle)80 void HardwareSensor::autoDisable(void *ident, int handle) {
81     mSensorDevice.autoDisable(ident, handle);
82 }
83 
VirtualSensor()84 VirtualSensor::VirtualSensor() :
85         BaseSensor(DUMMY_SENSOR), mSensorFusion(SensorFusion::getInstance()) {
86 }
87 
88 // ---------------------------------------------------------------------------
89 
RuntimeSensor(const sensor_t & sensor,sp<SensorCallback> callback)90 RuntimeSensor::RuntimeSensor(const sensor_t& sensor, sp<SensorCallback> callback)
91   : BaseSensor(sensor), mCallback(std::move(callback)) {
92 }
93 
activate(void *,bool enabled)94 status_t RuntimeSensor::activate(void*, bool enabled) {
95     if (enabled != mEnabled) {
96         mEnabled = enabled;
97         return mCallback->onConfigurationChanged(mSensor.getHandle(), mEnabled, mSamplingPeriodNs,
98                 mBatchReportLatencyNs);
99     }
100     return OK;
101 }
102 
batch(void *,int,int,int64_t samplingPeriodNs,int64_t maxBatchReportLatencyNs)103 status_t RuntimeSensor::batch(void*, int, int, int64_t samplingPeriodNs,
104                               int64_t maxBatchReportLatencyNs) {
105     if (mSamplingPeriodNs != samplingPeriodNs || mBatchReportLatencyNs != maxBatchReportLatencyNs) {
106         mSamplingPeriodNs = samplingPeriodNs;
107         mBatchReportLatencyNs = maxBatchReportLatencyNs;
108         if (mEnabled) {
109             return mCallback->onConfigurationChanged(mSensor.getHandle(), mEnabled,
110                     mSamplingPeriodNs, mBatchReportLatencyNs);
111         }
112     }
113     return OK;
114 }
115 
setDelay(void *,int,int64_t ns)116 status_t RuntimeSensor::setDelay(void*, int, int64_t ns) {
117     if (mSamplingPeriodNs != ns) {
118         mSamplingPeriodNs = ns;
119         if (mEnabled) {
120             return mCallback->onConfigurationChanged(mSensor.getHandle(), mEnabled,
121                     mSamplingPeriodNs, mBatchReportLatencyNs);
122         }
123     }
124     return OK;
125 }
126 
127 // ---------------------------------------------------------------------------
128 
ProximitySensor(const sensor_t & sensor,SensorService & service)129 ProximitySensor::ProximitySensor(const sensor_t& sensor, SensorService& service)
130         : HardwareSensor(sensor), mSensorService(service) {
131 }
132 
activate(void * ident,bool enabled)133 status_t ProximitySensor::activate(void* ident, bool enabled) {
134     status_t status = HardwareSensor::activate(ident, enabled);
135     if (status != NO_ERROR) {
136         return status;
137     }
138     mSensorService.checkAndReportProxStateChangeLocked();
139     return NO_ERROR;
140 }
141 
142 // ---------------------------------------------------------------------------
143 }; // namespace android
144