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
ProximitySensor(const sensor_t & sensor,SensorService & service)90 ProximitySensor::ProximitySensor(const sensor_t& sensor, SensorService& service)
91 : HardwareSensor(sensor), mSensorService(service) {
92 }
93
activate(void * ident,bool enabled)94 status_t ProximitySensor::activate(void* ident, bool enabled) {
95 bool lastState = mSensorDevice.isSensorActive(mSensor.getHandle());
96
97 status_t status = HardwareSensor::activate(ident, enabled);
98 if (status != NO_ERROR) {
99 return status;
100 }
101
102 bool currentState = mSensorDevice.isSensorActive(mSensor.getHandle());
103 if (currentState != lastState) {
104 mSensorService.onProximityActiveLocked(currentState);
105 }
106 return NO_ERROR;
107 }
108
willDisableAllSensors()109 void ProximitySensor::willDisableAllSensors() {
110 if (mSensorDevice.isSensorActive(mSensor.getHandle())) {
111 mSensorService.onProximityActiveLocked(false);
112 }
113 }
114
didEnableAllSensors()115 void ProximitySensor::didEnableAllSensors() {
116 if (mSensorDevice.isSensorActive(mSensor.getHandle())) {
117 mSensorService.onProximityActiveLocked(true);
118 }
119 }
120
121 // ---------------------------------------------------------------------------
122 }; // namespace android
123