| 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 | #ifndef ANDROID_SENSOR_DEVICE_UTIL | 
|  | 18 | #define ANDROID_SENSOR_DEVICE_UTIL | 
|  | 19 |  | 
|  | 20 | #include <android/hidl/manager/1.0/IServiceNotification.h> | 
| Anthony Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 21 | #include <hardware/sensors.h> | 
| Anthony Stange | 5945eb3 | 2020-08-06 12:45:16 -0400 | [diff] [blame] | 22 | #include <utils/Log.h> | 
| Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 23 |  | 
| Anthony Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 24 | #include <cmath> | 
| Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 25 | #include <condition_variable> | 
|  | 26 | #include <thread> | 
|  | 27 |  | 
|  | 28 | using ::android::hardware::hidl_string; | 
|  | 29 | using ::android::hardware::Return; | 
|  | 30 | using ::android::hidl::manager::V1_0::IServiceNotification; | 
|  | 31 |  | 
|  | 32 | namespace android { | 
|  | 33 | namespace SensorDeviceUtils { | 
|  | 34 |  | 
| Anthony Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 35 | // Quantizes a single value using a sensor's resolution. | 
|  | 36 | inline void quantizeValue(float *value, double resolution) { | 
| Anthony Stange | 5945eb3 | 2020-08-06 12:45:16 -0400 | [diff] [blame] | 37 | if (resolution == 0) { | 
|  | 38 | return; | 
|  | 39 | } | 
|  | 40 |  | 
| Anthony Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 41 | // Increase the value of the sensor's nominal resolution to ensure that | 
|  | 42 | // sensor accuracy improvements, like runtime calibration, are not masked | 
|  | 43 | // during requantization. | 
| Anthony Stange | 5b201d7 | 2020-06-11 09:04:32 -0400 | [diff] [blame] | 44 | double incRes = 0.125 * resolution; | 
| Anthony Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 45 | *value = round(static_cast<double>(*value) / incRes) * incRes; | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | // Ensures a sensor event doesn't provide values finer grained than its sensor resolution allows. | 
|  | 49 | void quantizeSensorEventValues(sensors_event_t *event, float resolution); | 
|  | 50 |  | 
| Anthony Stange | 5945eb3 | 2020-08-06 12:45:16 -0400 | [diff] [blame] | 51 | // Returns the expected resolution value for the given sensor | 
|  | 52 | float resolutionForSensor(const sensor_t &sensor); | 
| Anthony Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 53 |  | 
| Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 54 | class HidlServiceRegistrationWaiter : public IServiceNotification { | 
|  | 55 | public: | 
|  | 56 |  | 
|  | 57 | HidlServiceRegistrationWaiter(); | 
|  | 58 |  | 
|  | 59 | Return<void> onRegistration(const hidl_string &fqName, | 
|  | 60 | const hidl_string &name, | 
|  | 61 | bool preexisting) override; | 
|  | 62 |  | 
|  | 63 | void reset(); | 
|  | 64 |  | 
|  | 65 | /** | 
|  | 66 | * Wait for service restart | 
|  | 67 | * | 
|  | 68 | * @return true if service is restart since last reset(); false otherwise. | 
|  | 69 | */ | 
|  | 70 | bool wait(); | 
| Yifan Hong | a53e89d | 2017-11-02 16:19:19 -0700 | [diff] [blame] | 71 | protected: | 
|  | 72 | void onFirstRef() override; | 
| Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 73 | private: | 
| Martijn Coenen | b41e87a | 2017-11-02 14:00:41 +0100 | [diff] [blame] | 74 | bool mRegistered; | 
| Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 75 |  | 
|  | 76 | std::mutex mLock; | 
|  | 77 | std::condition_variable mCondition; | 
|  | 78 | bool mRestartObserved; | 
|  | 79 | }; | 
|  | 80 |  | 
|  | 81 | } // namespace SensorDeviceUtils | 
|  | 82 | } // namespace android; | 
|  | 83 |  | 
|  | 84 | #endif // ANDROID_SENSOR_SERVICE_UTIL |