blob: 255f7e107c903c94058e05fe5f9246d8ba7f668d [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#ifndef ANDROID_SENSOR_DEVICE_UTIL
18#define ANDROID_SENSOR_DEVICE_UTIL
19
20#include <android/hidl/manager/1.0/IServiceNotification.h>
Anthony Stange12924862020-03-20 10:46:15 -040021#include <hardware/sensors.h>
Anthony Stange5945eb32020-08-06 12:45:16 -040022#include <utils/Log.h>
Peng Xu1a00e2d2017-09-27 23:08:30 -070023
Anthony Stange12924862020-03-20 10:46:15 -040024#include <cmath>
Peng Xu1a00e2d2017-09-27 23:08:30 -070025#include <condition_variable>
26#include <thread>
27
28using ::android::hardware::hidl_string;
29using ::android::hardware::Return;
30using ::android::hidl::manager::V1_0::IServiceNotification;
31
32namespace android {
33namespace SensorDeviceUtils {
34
Brian Duddie3dd85022021-04-27 17:18:00 -070035// Quantizes a single value to (a fractional factor of) a sensor's resolution. Typically we
36// increase the value of the sensor's nominal resolution to ensure that sensor accuracy
37// improvements, like runtime calibration, are not masked during requantization.
38inline void quantizeValue(float *value, double resolution, double factor = 0.125) {
Anthony Stange5945eb32020-08-06 12:45:16 -040039 if (resolution == 0) {
40 return;
41 }
42
Brian Duddie3dd85022021-04-27 17:18:00 -070043 double incRes = factor * resolution;
Anthony Stange12924862020-03-20 10:46:15 -040044 *value = round(static_cast<double>(*value) / incRes) * incRes;
45}
46
47// Ensures a sensor event doesn't provide values finer grained than its sensor resolution allows.
48void quantizeSensorEventValues(sensors_event_t *event, float resolution);
49
Anthony Stange5945eb32020-08-06 12:45:16 -040050// Returns the expected resolution value for the given sensor
51float resolutionForSensor(const sensor_t &sensor);
Anthony Stange12924862020-03-20 10:46:15 -040052
Peng Xu1a00e2d2017-09-27 23:08:30 -070053class HidlServiceRegistrationWaiter : public IServiceNotification {
54public:
55
56 HidlServiceRegistrationWaiter();
57
58 Return<void> onRegistration(const hidl_string &fqName,
59 const hidl_string &name,
60 bool preexisting) override;
61
62 void reset();
63
64 /**
65 * Wait for service restart
66 *
67 * @return true if service is restart since last reset(); false otherwise.
68 */
69 bool wait();
Yifan Honga53e89d2017-11-02 16:19:19 -070070protected:
71 void onFirstRef() override;
Peng Xu1a00e2d2017-09-27 23:08:30 -070072private:
Martijn Coenenb41e87a2017-11-02 14:00:41 +010073 bool mRegistered;
Peng Xu1a00e2d2017-09-27 23:08:30 -070074
75 std::mutex mLock;
76 std::condition_variable mCondition;
77 bool mRestartObserved;
78};
79
80} // namespace SensorDeviceUtils
81} // namespace android;
82
83#endif // ANDROID_SENSOR_SERVICE_UTIL