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>
20 #include <android/hardware/sensors/2.1/ISensors.h>
21 #include <utils/Log.h>
22 
23 #include <chrono>
24 #include <thread>
25 
26 using ::android::hardware::Void;
27 using SensorTypeV2_1 = android::hardware::sensors::V2_1::SensorType;
28 using namespace android::hardware::sensors::V1_0;
29 
30 namespace android {
31 namespace SensorDeviceUtils {
32 
quantizeSensorEventValues(sensors_event_t * event,float resolution)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::GYROSCOPE:
44         case SensorTypeV2_1::MAGNETIC_FIELD_UNCALIBRATED:
45         case SensorTypeV2_1::GYROSCOPE_UNCALIBRATED:
46         case SensorTypeV2_1::ACCELEROMETER_UNCALIBRATED:
47             axes = 3;
48             break;
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;
70         default:
71             // No other sensors have data that needs to be quantized.
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 
defaultResolutionForType(int type)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 
HidlServiceRegistrationWaiter()102 HidlServiceRegistrationWaiter::HidlServiceRegistrationWaiter() {
103 }
104 
onFirstRef()105 void HidlServiceRegistrationWaiter::onFirstRef() {
106     // Creating sp<...>(this) in the constructor should be avoided, hence
107     // registerForNotifications is called in onFirstRef callback.
108     mRegistered = ISensors::registerForNotifications("default", this);
109 }
110 
onRegistration(const hidl_string & fqName,const hidl_string & name,bool preexisting)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 
reset()124 void HidlServiceRegistrationWaiter::reset() {
125     std::lock_guard<std::mutex> lk(mLock);
126     mRestartObserved = false;
127 }
128 
wait()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
147