blob: 6bf62e4f8264ae4bdee19d869071932e9ed28553 [file] [log] [blame]
Peng Xu1a00e2d2017-09-27 23:08:30 -07001/*
2 * Copyright (C) 2017 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 "SensorDeviceUtils.h"
18
19#include <android/hardware/sensors/1.0/ISensors.h>
Anthony Stange0ff158f2020-03-20 10:46:15 -040020#include <android/hardware/sensors/2.1/ISensors.h>
Peng Xu1a00e2d2017-09-27 23:08:30 -070021#include <utils/Log.h>
22
23#include <chrono>
Anthony Stange0ff158f2020-03-20 10:46:15 -040024#include <cmath>
Peng Xu1a00e2d2017-09-27 23:08:30 -070025#include <thread>
26
27using ::android::hardware::Void;
Anthony Stange0ff158f2020-03-20 10:46:15 -040028using SensorTypeV2_1 = android::hardware::sensors::V2_1::SensorType;
Peng Xu1a00e2d2017-09-27 23:08:30 -070029using namespace android::hardware::sensors::V1_0;
30
31namespace android {
32namespace SensorDeviceUtils {
Anthony Stange0ff158f2020-03-20 10:46:15 -040033namespace {
34
35inline void quantizeValue(float *value, double resolution) {
36 // Increase the value of the sensor's nominal resolution to ensure that
37 // sensor accuracy improvements, like runtime calibration, are not masked
38 // during requantization.
39 double incRes = 0.25 * resolution;
40 *value = round(static_cast<double>(*value) / incRes) * incRes;
41}
42
43} // namespace
44
45void quantizeSensorEventValues(sensors_event_t *event, float resolution) {
46 LOG_FATAL_IF(resolution == 0, "Resolution must be specified for all sensors!");
47 if (resolution == 0) {
48 return;
49 }
50
51 size_t axes = 0;
52 switch ((SensorTypeV2_1)event->type) {
53 case SensorTypeV2_1::ACCELEROMETER:
54 case SensorTypeV2_1::MAGNETIC_FIELD:
55 case SensorTypeV2_1::ORIENTATION:
56 case SensorTypeV2_1::GYROSCOPE:
57 case SensorTypeV2_1::GRAVITY:
58 case SensorTypeV2_1::LINEAR_ACCELERATION:
59 case SensorTypeV2_1::MAGNETIC_FIELD_UNCALIBRATED:
60 case SensorTypeV2_1::GYROSCOPE_UNCALIBRATED:
61 case SensorTypeV2_1::ACCELEROMETER_UNCALIBRATED:
62 axes = 3;
63 break;
64 case SensorTypeV2_1::GAME_ROTATION_VECTOR:
65 axes = 4;
66 break;
67 case SensorTypeV2_1::ROTATION_VECTOR:
68 case SensorTypeV2_1::GEOMAGNETIC_ROTATION_VECTOR:
69 axes = 5;
70 break;
71 case SensorTypeV2_1::DEVICE_ORIENTATION:
72 case SensorTypeV2_1::LIGHT:
73 case SensorTypeV2_1::PRESSURE:
74 case SensorTypeV2_1::TEMPERATURE:
75 case SensorTypeV2_1::PROXIMITY:
76 case SensorTypeV2_1::RELATIVE_HUMIDITY:
77 case SensorTypeV2_1::AMBIENT_TEMPERATURE:
78 case SensorTypeV2_1::SIGNIFICANT_MOTION:
79 case SensorTypeV2_1::STEP_DETECTOR:
80 case SensorTypeV2_1::TILT_DETECTOR:
81 case SensorTypeV2_1::WAKE_GESTURE:
82 case SensorTypeV2_1::GLANCE_GESTURE:
83 case SensorTypeV2_1::PICK_UP_GESTURE:
84 case SensorTypeV2_1::WRIST_TILT_GESTURE:
85 case SensorTypeV2_1::STATIONARY_DETECT:
86 case SensorTypeV2_1::MOTION_DETECT:
87 case SensorTypeV2_1::HEART_BEAT:
88 case SensorTypeV2_1::LOW_LATENCY_OFFBODY_DETECT:
89 case SensorTypeV2_1::HINGE_ANGLE:
90 axes = 1;
91 break;
92 case SensorTypeV2_1::POSE_6DOF:
93 axes = 15;
94 break;
95 default:
96 // No other sensors have data that needs to be rounded.
97 break;
98 }
99
100 // sensor_event_t is a union so we're able to perform the same quanitization action for most
101 // sensors by only knowing the number of axes their output data has.
102 for (size_t i = 0; i < axes; i++) {
103 quantizeValue(&event->data[i], resolution);
104 }
105}
106
107float defaultResolutionForType(int type) {
108 switch ((SensorTypeV2_1)type) {
109 case SensorTypeV2_1::SIGNIFICANT_MOTION:
110 case SensorTypeV2_1::STEP_DETECTOR:
111 case SensorTypeV2_1::STEP_COUNTER:
112 case SensorTypeV2_1::TILT_DETECTOR:
113 case SensorTypeV2_1::WAKE_GESTURE:
114 case SensorTypeV2_1::GLANCE_GESTURE:
115 case SensorTypeV2_1::PICK_UP_GESTURE:
116 case SensorTypeV2_1::WRIST_TILT_GESTURE:
117 case SensorTypeV2_1::STATIONARY_DETECT:
118 case SensorTypeV2_1::MOTION_DETECT:
119 return 1.0f;
120 default:
121 // fall through and return 0 for all other types
122 break;
123 }
124 return 0.0f;
125}
Peng Xu1a00e2d2017-09-27 23:08:30 -0700126
Martijn Coenenb41e87a2017-11-02 14:00:41 +0100127HidlServiceRegistrationWaiter::HidlServiceRegistrationWaiter() {
Yifan Honga53e89d2017-11-02 16:19:19 -0700128}
129
130void HidlServiceRegistrationWaiter::onFirstRef() {
131 // Creating sp<...>(this) in the constructor should be avoided, hence
132 // registerForNotifications is called in onFirstRef callback.
Martijn Coenenb41e87a2017-11-02 14:00:41 +0100133 mRegistered = ISensors::registerForNotifications("default", this);
Peng Xu1a00e2d2017-09-27 23:08:30 -0700134}
135
136Return<void> HidlServiceRegistrationWaiter::onRegistration(
137 const hidl_string &fqName, const hidl_string &name, bool preexisting) {
138 ALOGV("onRegistration fqName %s, name %s, preexisting %d",
139 fqName.c_str(), name.c_str(), preexisting);
140
141 {
142 std::lock_guard<std::mutex> lk(mLock);
143 mRestartObserved = true;
144 }
145 mCondition.notify_all();
146 return Void();
147}
148
149void HidlServiceRegistrationWaiter::reset() {
150 std::lock_guard<std::mutex> lk(mLock);
151 mRestartObserved = false;
152}
153
154bool HidlServiceRegistrationWaiter::wait() {
155 constexpr int DEFAULT_WAIT_MS = 100;
156 constexpr int TIMEOUT_MS = 1000;
157
158 if (!mRegistered) {
159 ALOGW("Cannot register service notification, use default wait(%d ms)", DEFAULT_WAIT_MS);
160 std::this_thread::sleep_for(std::chrono::milliseconds(DEFAULT_WAIT_MS));
161 // not sure if service is actually restarted
162 return false;
163 }
164
165 std::unique_lock<std::mutex> lk(mLock);
166 return mCondition.wait_for(lk, std::chrono::milliseconds(TIMEOUT_MS),
167 [this]{return mRestartObserved;});
168}
169
170} // namespace SensorDeviceUtils
171} // namespace android