| 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: | 
| Anthony Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 43 | case SensorTypeV2_1::GYROSCOPE: | 
| Anthony Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 44 | case SensorTypeV2_1::MAGNETIC_FIELD_UNCALIBRATED: | 
|  | 45 | case SensorTypeV2_1::GYROSCOPE_UNCALIBRATED: | 
|  | 46 | case SensorTypeV2_1::ACCELEROMETER_UNCALIBRATED: | 
|  | 47 | axes = 3; | 
|  | 48 | break; | 
| Anthony Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 49 | case SensorTypeV2_1::DEVICE_ORIENTATION: | 
|  | 50 | case SensorTypeV2_1::LIGHT: | 
|  | 51 | case SensorTypeV2_1::PRESSURE: | 
|  | 52 | case SensorTypeV2_1::TEMPERATURE: | 
|  | 53 | case SensorTypeV2_1::PROXIMITY: | 
|  | 54 | case SensorTypeV2_1::RELATIVE_HUMIDITY: | 
|  | 55 | case SensorTypeV2_1::AMBIENT_TEMPERATURE: | 
|  | 56 | case SensorTypeV2_1::SIGNIFICANT_MOTION: | 
|  | 57 | case SensorTypeV2_1::STEP_DETECTOR: | 
|  | 58 | case SensorTypeV2_1::TILT_DETECTOR: | 
|  | 59 | case SensorTypeV2_1::WAKE_GESTURE: | 
|  | 60 | case SensorTypeV2_1::GLANCE_GESTURE: | 
|  | 61 | case SensorTypeV2_1::PICK_UP_GESTURE: | 
|  | 62 | case SensorTypeV2_1::WRIST_TILT_GESTURE: | 
|  | 63 | case SensorTypeV2_1::STATIONARY_DETECT: | 
|  | 64 | case SensorTypeV2_1::MOTION_DETECT: | 
|  | 65 | case SensorTypeV2_1::HEART_BEAT: | 
|  | 66 | case SensorTypeV2_1::LOW_LATENCY_OFFBODY_DETECT: | 
|  | 67 | case SensorTypeV2_1::HINGE_ANGLE: | 
|  | 68 | axes = 1; | 
|  | 69 | break; | 
| Anthony Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 70 | default: | 
| Anthony Stange | 5dc55ca | 2020-06-02 13:56:35 -0400 | [diff] [blame] | 71 | // No other sensors have data that needs to be quantized. | 
| Anthony Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 72 | break; | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | // sensor_event_t is a union so we're able to perform the same quanitization action for most | 
|  | 76 | // sensors by only knowing the number of axes their output data has. | 
|  | 77 | for (size_t i = 0; i < axes; i++) { | 
|  | 78 | quantizeValue(&event->data[i], resolution); | 
|  | 79 | } | 
|  | 80 | } | 
|  | 81 |  | 
|  | 82 | float defaultResolutionForType(int type) { | 
|  | 83 | switch ((SensorTypeV2_1)type) { | 
|  | 84 | case SensorTypeV2_1::SIGNIFICANT_MOTION: | 
|  | 85 | case SensorTypeV2_1::STEP_DETECTOR: | 
|  | 86 | case SensorTypeV2_1::STEP_COUNTER: | 
|  | 87 | case SensorTypeV2_1::TILT_DETECTOR: | 
|  | 88 | case SensorTypeV2_1::WAKE_GESTURE: | 
|  | 89 | case SensorTypeV2_1::GLANCE_GESTURE: | 
|  | 90 | case SensorTypeV2_1::PICK_UP_GESTURE: | 
|  | 91 | case SensorTypeV2_1::WRIST_TILT_GESTURE: | 
|  | 92 | case SensorTypeV2_1::STATIONARY_DETECT: | 
|  | 93 | case SensorTypeV2_1::MOTION_DETECT: | 
|  | 94 | return 1.0f; | 
|  | 95 | default: | 
|  | 96 | // fall through and return 0 for all other types | 
|  | 97 | break; | 
|  | 98 | } | 
|  | 99 | return 0.0f; | 
|  | 100 | } | 
|  | 101 |  | 
| Martijn Coenen | b41e87a | 2017-11-02 14:00:41 +0100 | [diff] [blame] | 102 | HidlServiceRegistrationWaiter::HidlServiceRegistrationWaiter() { | 
| Yifan Hong | a53e89d | 2017-11-02 16:19:19 -0700 | [diff] [blame] | 103 | } | 
|  | 104 |  | 
|  | 105 | void HidlServiceRegistrationWaiter::onFirstRef() { | 
|  | 106 | // Creating sp<...>(this) in the constructor should be avoided, hence | 
|  | 107 | // registerForNotifications is called in onFirstRef callback. | 
| Martijn Coenen | b41e87a | 2017-11-02 14:00:41 +0100 | [diff] [blame] | 108 | mRegistered = ISensors::registerForNotifications("default", this); | 
| Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 109 | } | 
|  | 110 |  | 
|  | 111 | Return<void> HidlServiceRegistrationWaiter::onRegistration( | 
|  | 112 | const hidl_string &fqName, const hidl_string &name, bool preexisting) { | 
|  | 113 | ALOGV("onRegistration fqName %s, name %s, preexisting %d", | 
|  | 114 | fqName.c_str(), name.c_str(), preexisting); | 
|  | 115 |  | 
|  | 116 | { | 
|  | 117 | std::lock_guard<std::mutex> lk(mLock); | 
|  | 118 | mRestartObserved = true; | 
|  | 119 | } | 
|  | 120 | mCondition.notify_all(); | 
|  | 121 | return Void(); | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | void HidlServiceRegistrationWaiter::reset() { | 
|  | 125 | std::lock_guard<std::mutex> lk(mLock); | 
|  | 126 | mRestartObserved = false; | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | bool HidlServiceRegistrationWaiter::wait() { | 
|  | 130 | constexpr int DEFAULT_WAIT_MS = 100; | 
|  | 131 | constexpr int TIMEOUT_MS = 1000; | 
|  | 132 |  | 
|  | 133 | if (!mRegistered) { | 
|  | 134 | ALOGW("Cannot register service notification, use default wait(%d ms)", DEFAULT_WAIT_MS); | 
|  | 135 | std::this_thread::sleep_for(std::chrono::milliseconds(DEFAULT_WAIT_MS)); | 
|  | 136 | // not sure if service is actually restarted | 
|  | 137 | return false; | 
|  | 138 | } | 
|  | 139 |  | 
|  | 140 | std::unique_lock<std::mutex> lk(mLock); | 
|  | 141 | return mCondition.wait_for(lk, std::chrono::milliseconds(TIMEOUT_MS), | 
|  | 142 | [this]{return mRestartObserved;}); | 
|  | 143 | } | 
|  | 144 |  | 
|  | 145 | } // namespace SensorDeviceUtils | 
|  | 146 | } // namespace android |