blob: d7e621c035fa63a1c6c25f000f81dfcb68e79c05 [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>
Peng Xu1a00e2d2017-09-27 23:08:30 -070022
Anthony Stange12924862020-03-20 10:46:15 -040023#include <cmath>
Peng Xu1a00e2d2017-09-27 23:08:30 -070024#include <condition_variable>
25#include <thread>
26
27using ::android::hardware::hidl_string;
28using ::android::hardware::Return;
29using ::android::hidl::manager::V1_0::IServiceNotification;
30
31namespace android {
32namespace SensorDeviceUtils {
33
Anthony Stange12924862020-03-20 10:46:15 -040034// Quantizes a single value using a sensor's resolution.
35inline void quantizeValue(float *value, double resolution) {
36 // Increase the value of the sensor's nominal resolution to ensure that
37 // sensor accuracy improvements, like runtime calibration, are not masked
38 // during requantization.
39 double incRes = 0.25 * resolution;
40 *value = round(static_cast<double>(*value) / incRes) * incRes;
41}
42
43// Ensures a sensor event doesn't provide values finer grained than its sensor resolution allows.
44void quantizeSensorEventValues(sensors_event_t *event, float resolution);
45
46// Provides a default resolution for simple sensor types if one wasn't provided by the HAL.
47float defaultResolutionForType(int type);
48
Peng Xu1a00e2d2017-09-27 23:08:30 -070049class HidlServiceRegistrationWaiter : public IServiceNotification {
50public:
51
52 HidlServiceRegistrationWaiter();
53
54 Return<void> onRegistration(const hidl_string &fqName,
55 const hidl_string &name,
56 bool preexisting) override;
57
58 void reset();
59
60 /**
61 * Wait for service restart
62 *
63 * @return true if service is restart since last reset(); false otherwise.
64 */
65 bool wait();
Yifan Honga53e89d2017-11-02 16:19:19 -070066protected:
67 void onFirstRef() override;
Peng Xu1a00e2d2017-09-27 23:08:30 -070068private:
Martijn Coenenb41e87a2017-11-02 14:00:41 +010069 bool mRegistered;
Peng Xu1a00e2d2017-09-27 23:08:30 -070070
71 std::mutex mLock;
72 std::condition_variable mCondition;
73 bool mRestartObserved;
74};
75
76} // namespace SensorDeviceUtils
77} // namespace android;
78
79#endif // ANDROID_SENSOR_SERVICE_UTIL