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