• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (resolution == 0) {
35         return;
36     }
37 
38     size_t axes = 0;
39     switch ((SensorTypeV2_1)event->type) {
40         case SensorTypeV2_1::ACCELEROMETER:
41         case SensorTypeV2_1::MAGNETIC_FIELD:
42         case SensorTypeV2_1::GYROSCOPE:
43         case SensorTypeV2_1::MAGNETIC_FIELD_UNCALIBRATED:
44         case SensorTypeV2_1::GYROSCOPE_UNCALIBRATED:
45         case SensorTypeV2_1::ACCELEROMETER_UNCALIBRATED:
46             axes = 3;
47             break;
48         case SensorTypeV2_1::DEVICE_ORIENTATION:
49         case SensorTypeV2_1::LIGHT:
50         case SensorTypeV2_1::PRESSURE:
51         case SensorTypeV2_1::TEMPERATURE:
52         case SensorTypeV2_1::PROXIMITY:
53         case SensorTypeV2_1::RELATIVE_HUMIDITY:
54         case SensorTypeV2_1::AMBIENT_TEMPERATURE:
55         case SensorTypeV2_1::SIGNIFICANT_MOTION:
56         case SensorTypeV2_1::STEP_DETECTOR:
57         case SensorTypeV2_1::TILT_DETECTOR:
58         case SensorTypeV2_1::WAKE_GESTURE:
59         case SensorTypeV2_1::GLANCE_GESTURE:
60         case SensorTypeV2_1::PICK_UP_GESTURE:
61         case SensorTypeV2_1::WRIST_TILT_GESTURE:
62         case SensorTypeV2_1::STATIONARY_DETECT:
63         case SensorTypeV2_1::MOTION_DETECT:
64         case SensorTypeV2_1::HEART_BEAT:
65         case SensorTypeV2_1::LOW_LATENCY_OFFBODY_DETECT:
66         case SensorTypeV2_1::HINGE_ANGLE:
67             axes = 1;
68             break;
69         default:
70             // No other sensors have data that needs to be quantized.
71             break;
72     }
73 
74     // sensor_event_t is a union so we're able to perform the same quanitization action for most
75     // sensors by only knowing the number of axes their output data has.
76     for (size_t i = 0; i < axes; i++) {
77         quantizeValue(&event->data[i], resolution);
78     }
79 }
80 
resolutionForSensor(const sensor_t & sensor)81 float resolutionForSensor(const sensor_t &sensor) {
82     switch ((SensorTypeV2_1)sensor.type) {
83         case SensorTypeV2_1::ACCELEROMETER:
84         case SensorTypeV2_1::MAGNETIC_FIELD:
85         case SensorTypeV2_1::GYROSCOPE:
86         case SensorTypeV2_1::MAGNETIC_FIELD_UNCALIBRATED:
87         case SensorTypeV2_1::GYROSCOPE_UNCALIBRATED:
88         case SensorTypeV2_1::ACCELEROMETER_UNCALIBRATED: {
89             if (sensor.maxRange == 0) {
90                 ALOGE("No max range for sensor type %d, can't determine appropriate resolution",
91                         sensor.type);
92                 return sensor.resolution;
93             }
94             // Accel, gyro, and mag shouldn't have more than 24 bits of resolution on the most
95             // advanced devices.
96             double lowerBound = 2.0 * sensor.maxRange / std::pow(2, 24);
97 
98             // No need to check the upper bound as that's already enforced through CTS.
99             return std::max(sensor.resolution, static_cast<float>(lowerBound));
100         }
101         case SensorTypeV2_1::SIGNIFICANT_MOTION:
102         case SensorTypeV2_1::STEP_DETECTOR:
103         case SensorTypeV2_1::STEP_COUNTER:
104         case SensorTypeV2_1::TILT_DETECTOR:
105         case SensorTypeV2_1::WAKE_GESTURE:
106         case SensorTypeV2_1::GLANCE_GESTURE:
107         case SensorTypeV2_1::PICK_UP_GESTURE:
108         case SensorTypeV2_1::WRIST_TILT_GESTURE:
109         case SensorTypeV2_1::STATIONARY_DETECT:
110         case SensorTypeV2_1::MOTION_DETECT:
111             // Ignore input resolution as all of these sensors are required to have a resolution of
112             // 1.
113             return 1.0f;
114         default:
115             // fall through and return the current resolution for all other types
116             break;
117     }
118     return sensor.resolution;
119 }
120 
HidlServiceRegistrationWaiter()121 HidlServiceRegistrationWaiter::HidlServiceRegistrationWaiter() {
122 }
123 
onFirstRef()124 void HidlServiceRegistrationWaiter::onFirstRef() {
125     // Creating sp<...>(this) in the constructor should be avoided, hence
126     // registerForNotifications is called in onFirstRef callback.
127     mRegistered = ISensors::registerForNotifications("default", this);
128 }
129 
onRegistration(const hidl_string & fqName,const hidl_string & name,bool preexisting)130 Return<void> HidlServiceRegistrationWaiter::onRegistration(
131         const hidl_string &fqName, const hidl_string &name, bool preexisting) {
132     ALOGV("onRegistration fqName %s, name %s, preexisting %d",
133           fqName.c_str(), name.c_str(), preexisting);
134 
135     {
136         std::lock_guard<std::mutex> lk(mLock);
137         mRestartObserved = true;
138     }
139     mCondition.notify_all();
140     return Void();
141 }
142 
reset()143 void HidlServiceRegistrationWaiter::reset() {
144     std::lock_guard<std::mutex> lk(mLock);
145     mRestartObserved = false;
146 }
147 
wait()148 bool HidlServiceRegistrationWaiter::wait() {
149     constexpr int DEFAULT_WAIT_MS = 100;
150     constexpr int TIMEOUT_MS = 1000;
151 
152     if (!mRegistered) {
153         ALOGW("Cannot register service notification, use default wait(%d ms)", DEFAULT_WAIT_MS);
154         std::this_thread::sleep_for(std::chrono::milliseconds(DEFAULT_WAIT_MS));
155         // not sure if service is actually restarted
156         return false;
157     }
158 
159     std::unique_lock<std::mutex> lk(mLock);
160     return mCondition.wait_for(lk, std::chrono::milliseconds(TIMEOUT_MS),
161             [this]{return mRestartObserved;});
162 }
163 
164 } // namespace SensorDeviceUtils
165 } // namespace android
166