Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 1 | /* |
| 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 Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 20 | #include <android/hardware/sensors/2.1/ISensors.h> |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 21 | #include <utils/Log.h> |
| 22 | |
| 23 | #include <chrono> |
| 24 | #include <thread> |
| 25 | |
| 26 | using ::android::hardware::Void; |
Anthony Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 27 | using SensorTypeV2_1 = android::hardware::sensors::V2_1::SensorType; |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 28 | using namespace android::hardware::sensors::V1_0; |
| 29 | |
| 30 | namespace android { |
| 31 | namespace SensorDeviceUtils { |
| 32 | |
Anthony Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 33 | void 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 | |
| 95 | float 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 Coenen | b41e87a | 2017-11-02 14:00:41 +0100 | [diff] [blame] | 115 | HidlServiceRegistrationWaiter::HidlServiceRegistrationWaiter() { |
Yifan Hong | a53e89d | 2017-11-02 16:19:19 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void HidlServiceRegistrationWaiter::onFirstRef() { |
| 119 | // Creating sp<...>(this) in the constructor should be avoided, hence |
| 120 | // registerForNotifications is called in onFirstRef callback. |
Martijn Coenen | b41e87a | 2017-11-02 14:00:41 +0100 | [diff] [blame] | 121 | mRegistered = ISensors::registerForNotifications("default", this); |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | Return<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 | |
| 137 | void HidlServiceRegistrationWaiter::reset() { |
| 138 | std::lock_guard<std::mutex> lk(mLock); |
| 139 | mRestartObserved = false; |
| 140 | } |
| 141 | |
| 142 | bool 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 |