1 /*
2 * Copyright (C) 2018 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 "Sensor.h"
18
19 #include <utils/SystemClock.h>
20
21 #include <cmath>
22
23 namespace android {
24 namespace hardware {
25 namespace sensors {
26 namespace V2_X {
27 namespace implementation {
28
29 using ::android::hardware::sensors::V1_0::MetaDataEventType;
30 using ::android::hardware::sensors::V1_0::OperationMode;
31 using ::android::hardware::sensors::V1_0::Result;
32 using ::android::hardware::sensors::V1_0::SensorFlagBits;
33 using ::android::hardware::sensors::V1_0::SensorStatus;
34 using ::android::hardware::sensors::V2_1::Event;
35 using ::android::hardware::sensors::V2_1::SensorInfo;
36 using ::android::hardware::sensors::V2_1::SensorType;
37
Sensor(ISensorsEventCallback * callback)38 Sensor::Sensor(ISensorsEventCallback* callback)
39 : mIsEnabled(false),
40 mSamplingPeriodNs(0),
41 mLastSampleTimeNs(0),
42 mCallback(callback),
43 mMode(OperationMode::NORMAL) {
44 mRunThread = std::thread(startThread, this);
45 }
46
~Sensor()47 Sensor::~Sensor() {
48 std::unique_lock<std::mutex> lock(mRunMutex);
49 mStopThread = true;
50 mIsEnabled = false;
51 mWaitCV.notify_all();
52 lock.release();
53 mRunThread.join();
54 }
55
getSensorInfo() const56 const SensorInfo& Sensor::getSensorInfo() const {
57 return mSensorInfo;
58 }
59
batch(int32_t samplingPeriodNs)60 void Sensor::batch(int32_t samplingPeriodNs) {
61 if (samplingPeriodNs < mSensorInfo.minDelay * 1000) {
62 samplingPeriodNs = mSensorInfo.minDelay * 1000;
63 } else if (samplingPeriodNs > mSensorInfo.maxDelay * 1000) {
64 samplingPeriodNs = mSensorInfo.maxDelay * 1000;
65 }
66
67 if (mSamplingPeriodNs != samplingPeriodNs) {
68 mSamplingPeriodNs = samplingPeriodNs;
69 // Wake up the 'run' thread to check if a new event should be generated now
70 mWaitCV.notify_all();
71 }
72 }
73
activate(bool enable)74 void Sensor::activate(bool enable) {
75 if (mIsEnabled != enable) {
76 std::unique_lock<std::mutex> lock(mRunMutex);
77 mIsEnabled = enable;
78 mWaitCV.notify_all();
79 }
80 }
81
flush()82 Result Sensor::flush() {
83 // Only generate a flush complete event if the sensor is enabled and if the sensor is not a
84 // one-shot sensor.
85 if (!mIsEnabled || (mSensorInfo.flags & static_cast<uint32_t>(SensorFlagBits::ONE_SHOT_MODE))) {
86 return Result::BAD_VALUE;
87 }
88
89 // Note: If a sensor supports batching, write all of the currently batched events for the sensor
90 // to the Event FMQ prior to writing the flush complete event.
91 Event ev;
92 ev.sensorHandle = mSensorInfo.sensorHandle;
93 ev.sensorType = SensorType::META_DATA;
94 ev.u.meta.what = MetaDataEventType::META_DATA_FLUSH_COMPLETE;
95 std::vector<Event> evs{ev};
96 mCallback->postEvents(evs, isWakeUpSensor());
97
98 return Result::OK;
99 }
100
startThread(Sensor * sensor)101 void Sensor::startThread(Sensor* sensor) {
102 sensor->run();
103 }
104
run()105 void Sensor::run() {
106 std::unique_lock<std::mutex> runLock(mRunMutex);
107 constexpr int64_t kNanosecondsInSeconds = 1000 * 1000 * 1000;
108
109 while (!mStopThread) {
110 if (!mIsEnabled || mMode == OperationMode::DATA_INJECTION) {
111 mWaitCV.wait(runLock, [&] {
112 return ((mIsEnabled && mMode == OperationMode::NORMAL) || mStopThread);
113 });
114 } else {
115 timespec curTime;
116 clock_gettime(CLOCK_REALTIME, &curTime);
117 int64_t now = (curTime.tv_sec * kNanosecondsInSeconds) + curTime.tv_nsec;
118 int64_t nextSampleTime = mLastSampleTimeNs + mSamplingPeriodNs;
119
120 if (now >= nextSampleTime) {
121 mLastSampleTimeNs = now;
122 nextSampleTime = mLastSampleTimeNs + mSamplingPeriodNs;
123 mCallback->postEvents(readEvents(), isWakeUpSensor());
124 }
125
126 mWaitCV.wait_for(runLock, std::chrono::nanoseconds(nextSampleTime - now));
127 }
128 }
129 }
130
isWakeUpSensor()131 bool Sensor::isWakeUpSensor() {
132 return mSensorInfo.flags & static_cast<uint32_t>(SensorFlagBits::WAKE_UP);
133 }
134
readEvents()135 std::vector<Event> Sensor::readEvents() {
136 std::vector<Event> events;
137 Event event;
138 event.sensorHandle = mSensorInfo.sensorHandle;
139 event.sensorType = mSensorInfo.type;
140 event.timestamp = ::android::elapsedRealtimeNano();
141 event.u.vec3.x = 0;
142 event.u.vec3.y = 0;
143 event.u.vec3.z = 0;
144 event.u.vec3.status = SensorStatus::ACCURACY_HIGH;
145 events.push_back(event);
146 return events;
147 }
148
setOperationMode(OperationMode mode)149 void Sensor::setOperationMode(OperationMode mode) {
150 if (mMode != mode) {
151 std::unique_lock<std::mutex> lock(mRunMutex);
152 mMode = mode;
153 mWaitCV.notify_all();
154 }
155 }
156
supportsDataInjection() const157 bool Sensor::supportsDataInjection() const {
158 return mSensorInfo.flags & static_cast<uint32_t>(SensorFlagBits::DATA_INJECTION);
159 }
160
injectEvent(const Event & event)161 Result Sensor::injectEvent(const Event& event) {
162 Result result = Result::OK;
163 if (event.sensorType == SensorType::ADDITIONAL_INFO) {
164 // When in OperationMode::NORMAL, SensorType::ADDITIONAL_INFO is used to push operation
165 // environment data into the device.
166 } else if (!supportsDataInjection()) {
167 result = Result::INVALID_OPERATION;
168 } else if (mMode == OperationMode::DATA_INJECTION) {
169 mCallback->postEvents(std::vector<Event>{event}, isWakeUpSensor());
170 } else {
171 result = Result::BAD_VALUE;
172 }
173 return result;
174 }
175
OnChangeSensor(ISensorsEventCallback * callback)176 OnChangeSensor::OnChangeSensor(ISensorsEventCallback* callback)
177 : Sensor(callback), mPreviousEventSet(false) {}
178
activate(bool enable)179 void OnChangeSensor::activate(bool enable) {
180 Sensor::activate(enable);
181 if (!enable) {
182 mPreviousEventSet = false;
183 }
184 }
185
readEvents()186 std::vector<Event> OnChangeSensor::readEvents() {
187 std::vector<Event> events = Sensor::readEvents();
188 std::vector<Event> outputEvents;
189
190 for (auto iter = events.begin(); iter != events.end(); ++iter) {
191 Event ev = *iter;
192 if (ev.u.vec3 != mPreviousEvent.u.vec3 || !mPreviousEventSet) {
193 outputEvents.push_back(ev);
194 mPreviousEvent = ev;
195 mPreviousEventSet = true;
196 }
197 }
198 return outputEvents;
199 }
200
AccelSensor(int32_t sensorHandle,ISensorsEventCallback * callback)201 AccelSensor::AccelSensor(int32_t sensorHandle, ISensorsEventCallback* callback) : Sensor(callback) {
202 mSensorInfo.sensorHandle = sensorHandle;
203 mSensorInfo.name = "Accel Sensor";
204 mSensorInfo.vendor = "Vendor String";
205 mSensorInfo.version = 1;
206 mSensorInfo.type = SensorType::ACCELEROMETER;
207 mSensorInfo.typeAsString = "";
208 mSensorInfo.maxRange = 78.4f; // +/- 8g
209 mSensorInfo.resolution = 1.52e-5;
210 mSensorInfo.power = 0.001f; // mA
211 mSensorInfo.minDelay = 20 * 1000; // microseconds
212 mSensorInfo.maxDelay = kDefaultMaxDelayUs;
213 mSensorInfo.fifoReservedEventCount = 0;
214 mSensorInfo.fifoMaxEventCount = 0;
215 mSensorInfo.requiredPermission = "";
216 mSensorInfo.flags = static_cast<uint32_t>(SensorFlagBits::DATA_INJECTION);
217 };
218
PressureSensor(int32_t sensorHandle,ISensorsEventCallback * callback)219 PressureSensor::PressureSensor(int32_t sensorHandle, ISensorsEventCallback* callback)
220 : Sensor(callback) {
221 mSensorInfo.sensorHandle = sensorHandle;
222 mSensorInfo.name = "Pressure Sensor";
223 mSensorInfo.vendor = "Vendor String";
224 mSensorInfo.version = 1;
225 mSensorInfo.type = SensorType::PRESSURE;
226 mSensorInfo.typeAsString = "";
227 mSensorInfo.maxRange = 1100.0f; // hPa
228 mSensorInfo.resolution = 0.005f; // hPa
229 mSensorInfo.power = 0.001f; // mA
230 mSensorInfo.minDelay = 100 * 1000; // microseconds
231 mSensorInfo.maxDelay = kDefaultMaxDelayUs;
232 mSensorInfo.fifoReservedEventCount = 0;
233 mSensorInfo.fifoMaxEventCount = 0;
234 mSensorInfo.requiredPermission = "";
235 mSensorInfo.flags = 0;
236 };
237
MagnetometerSensor(int32_t sensorHandle,ISensorsEventCallback * callback)238 MagnetometerSensor::MagnetometerSensor(int32_t sensorHandle, ISensorsEventCallback* callback)
239 : Sensor(callback) {
240 mSensorInfo.sensorHandle = sensorHandle;
241 mSensorInfo.name = "Magnetic Field Sensor";
242 mSensorInfo.vendor = "Vendor String";
243 mSensorInfo.version = 1;
244 mSensorInfo.type = SensorType::MAGNETIC_FIELD;
245 mSensorInfo.typeAsString = "";
246 mSensorInfo.maxRange = 1300.0f;
247 mSensorInfo.resolution = 0.01f;
248 mSensorInfo.power = 0.001f; // mA
249 mSensorInfo.minDelay = 20 * 1000; // microseconds
250 mSensorInfo.maxDelay = kDefaultMaxDelayUs;
251 mSensorInfo.fifoReservedEventCount = 0;
252 mSensorInfo.fifoMaxEventCount = 0;
253 mSensorInfo.requiredPermission = "";
254 mSensorInfo.flags = 0;
255 };
256
LightSensor(int32_t sensorHandle,ISensorsEventCallback * callback)257 LightSensor::LightSensor(int32_t sensorHandle, ISensorsEventCallback* callback)
258 : OnChangeSensor(callback) {
259 mSensorInfo.sensorHandle = sensorHandle;
260 mSensorInfo.name = "Light Sensor";
261 mSensorInfo.vendor = "Vendor String";
262 mSensorInfo.version = 1;
263 mSensorInfo.type = SensorType::LIGHT;
264 mSensorInfo.typeAsString = "";
265 mSensorInfo.maxRange = 43000.0f;
266 mSensorInfo.resolution = 10.0f;
267 mSensorInfo.power = 0.001f; // mA
268 mSensorInfo.minDelay = 200 * 1000; // microseconds
269 mSensorInfo.maxDelay = kDefaultMaxDelayUs;
270 mSensorInfo.fifoReservedEventCount = 0;
271 mSensorInfo.fifoMaxEventCount = 0;
272 mSensorInfo.requiredPermission = "";
273 mSensorInfo.flags = static_cast<uint32_t>(SensorFlagBits::ON_CHANGE_MODE);
274 };
275
ProximitySensor(int32_t sensorHandle,ISensorsEventCallback * callback)276 ProximitySensor::ProximitySensor(int32_t sensorHandle, ISensorsEventCallback* callback)
277 : OnChangeSensor(callback) {
278 mSensorInfo.sensorHandle = sensorHandle;
279 mSensorInfo.name = "Proximity Sensor";
280 mSensorInfo.vendor = "Vendor String";
281 mSensorInfo.version = 1;
282 mSensorInfo.type = SensorType::PROXIMITY;
283 mSensorInfo.typeAsString = "";
284 mSensorInfo.maxRange = 5.0f;
285 mSensorInfo.resolution = 1.0f;
286 mSensorInfo.power = 0.012f; // mA
287 mSensorInfo.minDelay = 200 * 1000; // microseconds
288 mSensorInfo.maxDelay = kDefaultMaxDelayUs;
289 mSensorInfo.fifoReservedEventCount = 0;
290 mSensorInfo.fifoMaxEventCount = 0;
291 mSensorInfo.requiredPermission = "";
292 mSensorInfo.flags =
293 static_cast<uint32_t>(SensorFlagBits::ON_CHANGE_MODE | SensorFlagBits::WAKE_UP);
294 };
295
GyroSensor(int32_t sensorHandle,ISensorsEventCallback * callback)296 GyroSensor::GyroSensor(int32_t sensorHandle, ISensorsEventCallback* callback) : Sensor(callback) {
297 mSensorInfo.sensorHandle = sensorHandle;
298 mSensorInfo.name = "Gyro Sensor";
299 mSensorInfo.vendor = "Vendor String";
300 mSensorInfo.version = 1;
301 mSensorInfo.type = SensorType::GYROSCOPE;
302 mSensorInfo.typeAsString = "";
303 mSensorInfo.maxRange = 1000.0f * M_PI / 180.0f;
304 mSensorInfo.resolution = 1000.0f * M_PI / (180.0f * 32768.0f);
305 mSensorInfo.power = 0.001f;
306 mSensorInfo.minDelay = 2.5f * 1000; // microseconds
307 mSensorInfo.maxDelay = kDefaultMaxDelayUs;
308 mSensorInfo.fifoReservedEventCount = 0;
309 mSensorInfo.fifoMaxEventCount = 0;
310 mSensorInfo.requiredPermission = "";
311 mSensorInfo.flags = 0;
312 };
313
AmbientTempSensor(int32_t sensorHandle,ISensorsEventCallback * callback)314 AmbientTempSensor::AmbientTempSensor(int32_t sensorHandle, ISensorsEventCallback* callback)
315 : OnChangeSensor(callback) {
316 mSensorInfo.sensorHandle = sensorHandle;
317 mSensorInfo.name = "Ambient Temp Sensor";
318 mSensorInfo.vendor = "Vendor String";
319 mSensorInfo.version = 1;
320 mSensorInfo.type = SensorType::AMBIENT_TEMPERATURE;
321 mSensorInfo.typeAsString = "";
322 mSensorInfo.maxRange = 80.0f;
323 mSensorInfo.resolution = 0.01f;
324 mSensorInfo.power = 0.001f;
325 mSensorInfo.minDelay = 40 * 1000; // microseconds
326 mSensorInfo.maxDelay = kDefaultMaxDelayUs;
327 mSensorInfo.fifoReservedEventCount = 0;
328 mSensorInfo.fifoMaxEventCount = 0;
329 mSensorInfo.requiredPermission = "";
330 mSensorInfo.flags = static_cast<uint32_t>(SensorFlagBits::ON_CHANGE_MODE);
331 };
332
DeviceTempSensor(int32_t sensorHandle,ISensorsEventCallback * callback)333 DeviceTempSensor::DeviceTempSensor(int32_t sensorHandle, ISensorsEventCallback* callback)
334 : OnChangeSensor(callback) {
335 mSensorInfo.sensorHandle = sensorHandle;
336 mSensorInfo.name = "Device Temp Sensor";
337 mSensorInfo.vendor = "Vendor String";
338 mSensorInfo.version = 1;
339 mSensorInfo.type = SensorType::TEMPERATURE;
340 mSensorInfo.typeAsString = "";
341 mSensorInfo.maxRange = 80.0f;
342 mSensorInfo.resolution = 0.01f;
343 mSensorInfo.power = 0.001f;
344 mSensorInfo.minDelay = 40 * 1000; // microseconds
345 mSensorInfo.maxDelay = kDefaultMaxDelayUs;
346 mSensorInfo.fifoReservedEventCount = 0;
347 mSensorInfo.fifoMaxEventCount = 0;
348 mSensorInfo.requiredPermission = "";
349 mSensorInfo.flags = static_cast<uint32_t>(SensorFlagBits::ON_CHANGE_MODE);
350 }
351
RelativeHumiditySensor(int32_t sensorHandle,ISensorsEventCallback * callback)352 RelativeHumiditySensor::RelativeHumiditySensor(int32_t sensorHandle,
353 ISensorsEventCallback* callback)
354 : OnChangeSensor(callback) {
355 mSensorInfo.sensorHandle = sensorHandle;
356 mSensorInfo.name = "Relative Humidity Sensor";
357 mSensorInfo.vendor = "Vendor String";
358 mSensorInfo.version = 1;
359 mSensorInfo.type = SensorType::RELATIVE_HUMIDITY;
360 mSensorInfo.typeAsString = "";
361 mSensorInfo.maxRange = 100.0f;
362 mSensorInfo.resolution = 0.1f;
363 mSensorInfo.power = 0.001f;
364 mSensorInfo.minDelay = 40 * 1000; // microseconds
365 mSensorInfo.maxDelay = kDefaultMaxDelayUs;
366 mSensorInfo.fifoReservedEventCount = 0;
367 mSensorInfo.fifoMaxEventCount = 0;
368 mSensorInfo.requiredPermission = "";
369 mSensorInfo.flags = static_cast<uint32_t>(SensorFlagBits::ON_CHANGE_MODE);
370 }
371
372 } // namespace implementation
373 } // namespace V2_X
374 } // namespace sensors
375 } // namespace hardware
376 } // namespace android
377