Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 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 | |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 17 | #include "SensorInterface.h" |
| 18 | #include "SensorDevice.h" |
| 19 | #include "SensorFusion.h" |
Andrew Lehmer | 3a60257 | 2021-03-25 15:19:56 -0700 | [diff] [blame] | 20 | #include "SensorService.h" |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 21 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 22 | #include <stdint.h> |
| 23 | #include <sys/types.h> |
| 24 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 25 | namespace android { |
| 26 | // --------------------------------------------------------------------------- |
| 27 | |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 28 | namespace { |
| 29 | const sensor_t DUMMY_SENSOR = { |
| 30 | .name = "", .vendor = "", .stringType = "", .requiredPermission = ""}; |
| 31 | } //unnamed namespace |
| 32 | |
| 33 | BaseSensor::BaseSensor(const sensor_t& sensor) : |
| 34 | mSensorDevice(SensorDevice::getInstance()), |
| 35 | mSensor(&sensor, mSensorDevice.getHalDeviceVersion()) { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 36 | } |
| 37 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 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 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 43 | // --------------------------------------------------------------------------- |
| 44 | |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 45 | HardwareSensor::HardwareSensor(const sensor_t& sensor): |
| 46 | BaseSensor(sensor) { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 49 | HardwareSensor::HardwareSensor(const sensor_t& sensor, const uint8_t (&uuid)[16]): |
| 50 | BaseSensor(sensor, uuid) { |
| 51 | } |
| 52 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 53 | HardwareSensor::~HardwareSensor() { |
| 54 | } |
| 55 | |
| 56 | bool HardwareSensor::process(sensors_event_t* outEvent, |
| 57 | const sensors_event_t& event) { |
| 58 | *outEvent = event; |
| 59 | return true; |
| 60 | } |
| 61 | |
Mathias Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 62 | status_t HardwareSensor::activate(void* ident, bool enabled) { |
| 63 | return mSensorDevice.activate(ident, mSensor.getHandle(), enabled); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 66 | status_t HardwareSensor::batch(void* ident, int /*handle*/, int flags, |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 67 | int64_t samplingPeriodNs, int64_t maxBatchReportLatencyNs) { |
| 68 | return mSensorDevice.batch(ident, mSensor.getHandle(), flags, samplingPeriodNs, |
| 69 | maxBatchReportLatencyNs); |
| 70 | } |
| 71 | |
| 72 | status_t HardwareSensor::flush(void* ident, int handle) { |
| 73 | return mSensorDevice.flush(ident, handle); |
| 74 | } |
| 75 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 76 | status_t HardwareSensor::setDelay(void* ident, int handle, int64_t ns) { |
| 77 | return mSensorDevice.setDelay(ident, handle, ns); |
| 78 | } |
| 79 | |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 80 | void HardwareSensor::autoDisable(void *ident, int handle) { |
| 81 | mSensorDevice.autoDisable(ident, handle); |
Jaikumar Ganesh | 4c01b1a | 2013-04-16 15:52:23 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 84 | VirtualSensor::VirtualSensor() : |
| 85 | BaseSensor(DUMMY_SENSOR), mSensorFusion(SensorFusion::getInstance()) { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 88 | // --------------------------------------------------------------------------- |
Andrew Lehmer | 3a60257 | 2021-03-25 15:19:56 -0700 | [diff] [blame] | 89 | |
Vladimir Komsiyski | f76bba5 | 2022-10-23 10:56:06 +0200 | [diff] [blame^] | 90 | RuntimeSensor::RuntimeSensor(const sensor_t& sensor, sp<StateChangeCallback> callback) |
| 91 | : BaseSensor(sensor), mCallback(std::move(callback)) { |
| 92 | } |
| 93 | |
| 94 | status_t RuntimeSensor::activate(void*, bool enabled) { |
| 95 | if (enabled != mEnabled) { |
| 96 | mEnabled = enabled; |
| 97 | mCallback->onStateChanged(mEnabled, mSamplingPeriodNs, mBatchReportLatencyNs); |
| 98 | } |
| 99 | return OK; |
| 100 | } |
| 101 | |
| 102 | status_t RuntimeSensor::batch(void*, int, int, int64_t samplingPeriodNs, |
| 103 | int64_t maxBatchReportLatencyNs) { |
| 104 | if (mSamplingPeriodNs != samplingPeriodNs || mBatchReportLatencyNs != maxBatchReportLatencyNs) { |
| 105 | mSamplingPeriodNs = samplingPeriodNs; |
| 106 | mBatchReportLatencyNs = maxBatchReportLatencyNs; |
| 107 | if (mEnabled) { |
| 108 | mCallback->onStateChanged(mEnabled, mSamplingPeriodNs, mBatchReportLatencyNs); |
| 109 | } |
| 110 | } |
| 111 | return OK; |
| 112 | } |
| 113 | |
| 114 | status_t RuntimeSensor::setDelay(void*, int, int64_t ns) { |
| 115 | if (mSamplingPeriodNs != ns) { |
| 116 | mSamplingPeriodNs = ns; |
| 117 | if (mEnabled) { |
| 118 | mCallback->onStateChanged(mEnabled, mSamplingPeriodNs, mBatchReportLatencyNs); |
| 119 | } |
| 120 | } |
| 121 | return OK; |
| 122 | } |
| 123 | |
| 124 | // --------------------------------------------------------------------------- |
| 125 | |
Andrew Lehmer | 3a60257 | 2021-03-25 15:19:56 -0700 | [diff] [blame] | 126 | ProximitySensor::ProximitySensor(const sensor_t& sensor, SensorService& service) |
| 127 | : HardwareSensor(sensor), mSensorService(service) { |
| 128 | } |
| 129 | |
| 130 | status_t ProximitySensor::activate(void* ident, bool enabled) { |
Andrew Lehmer | 3a60257 | 2021-03-25 15:19:56 -0700 | [diff] [blame] | 131 | status_t status = HardwareSensor::activate(ident, enabled); |
| 132 | if (status != NO_ERROR) { |
| 133 | return status; |
| 134 | } |
Chris Kuiper | df11ff2 | 2021-10-12 16:30:01 -0700 | [diff] [blame] | 135 | mSensorService.checkAndReportProxStateChangeLocked(); |
Andrew Lehmer | 3a60257 | 2021-03-25 15:19:56 -0700 | [diff] [blame] | 136 | return NO_ERROR; |
| 137 | } |
| 138 | |
Andrew Lehmer | 3a60257 | 2021-03-25 15:19:56 -0700 | [diff] [blame] | 139 | // --------------------------------------------------------------------------- |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 140 | }; // namespace android |