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