1 /*
2 * Copyright (C) 2021 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 <android-base/chrono_utils.h>
18 #include <android-base/logging.h>
19 #include <binder/IServiceManager.h>
20 #include <utils/StrongPointer.h>
21 #include <utils/SystemClock.h>
22
23 #include <thread>
24
25 #include "android/hardware/sensors/2.1/ISensors.h"
26
27 using android::sp;
28 using android::hardware::sensors::V1_0::OperationMode;
29 using android::hardware::sensors::V1_0::Result;
30 using android::hardware::sensors::V1_0::SensorStatus;
31 using android::hardware::sensors::V2_1::Event;
32 using android::hardware::sensors::V2_1::ISensors;
33 using android::hardware::sensors::V2_1::SensorInfo;
34 using android::hardware::sensors::V2_1::SensorType;
35
startSensorInjection()36 sp<ISensors> startSensorInjection() {
37 const sp<ISensors> sensors = ISensors::getService();
38 if (sensors == nullptr) {
39 LOG(FATAL) << "Unable to get ISensors.";
40 }
41
42 // Place the ISensors HAL into DATA_INJECTION mode so that we can
43 // inject events.
44 Result result = sensors->setOperationMode(OperationMode::DATA_INJECTION);
45 if (result != Result::OK) {
46 LOG(FATAL) << "Unable to set ISensors operation mode to DATA_INJECTION: "
47 << toString(result);
48 }
49
50 return sensors;
51 }
52
getSensorHandle(SensorType type,const sp<ISensors> sensors)53 int getSensorHandle(SensorType type, const sp<ISensors> sensors) {
54 // Find the first available sensor of the given type.
55 int handle = -1;
56 const auto& getSensorsList_result =
57 sensors->getSensorsList_2_1([&](const auto& list) {
58 for (const SensorInfo& sensor : list) {
59 if (sensor.type == type) {
60 handle = sensor.sensorHandle;
61 break;
62 }
63 }
64 });
65 if (!getSensorsList_result.isOk()) {
66 LOG(FATAL) << "Unable to get ISensors sensors list: "
67 << getSensorsList_result.description();
68 }
69 if (handle == -1) {
70 LOG(FATAL) << "Unable to find sensor.";
71 }
72 return handle;
73 }
74
endSensorInjection(const sp<ISensors> sensors)75 void endSensorInjection(const sp<ISensors> sensors) {
76 // Return the ISensors HAL back to NORMAL mode.
77 Result result = sensors->setOperationMode(OperationMode::NORMAL);
78 if (result != Result::OK) {
79 LOG(FATAL) << "Unable to set sensors operation mode to NORMAL: "
80 << toString(result);
81 }
82 }
83
84 // Inject ACCELEROMETER events to corresponding to a given physical
85 // device orientation: portrait or landscape.
InjectOrientation(bool portrait)86 void InjectOrientation(bool portrait) {
87 sp<ISensors> sensors = startSensorInjection();
88 int handle = getSensorHandle(SensorType::ACCELEROMETER, sensors);
89
90 // Create a base ISensors accelerometer event.
91 Event event;
92 event.sensorHandle = handle;
93 event.sensorType = SensorType::ACCELEROMETER;
94 if (portrait) {
95 event.u.vec3.x = 0;
96 event.u.vec3.y = 9.2;
97 } else {
98 event.u.vec3.x = 9.2;
99 event.u.vec3.y = 0;
100 }
101 event.u.vec3.z = 3.5;
102 event.u.vec3.status = SensorStatus::ACCURACY_HIGH;
103
104 // Repeatedly inject accelerometer events. The WindowManager orientation
105 // listener responds to sustained accelerometer data, not just a single event.
106 android::base::Timer timer;
107 Result result;
108 while (timer.duration() < 1s) {
109 event.timestamp = android::elapsedRealtimeNano();
110 result = sensors->injectSensorData_2_1(event);
111 if (result != Result::OK) {
112 LOG(FATAL) << "Unable to inject ISensors accelerometer event: "
113 << toString(result);
114 }
115 std::this_thread::sleep_for(10ms);
116 }
117
118 endSensorInjection(sensors);
119 }
120
121 // Inject a single HINGE_ANGLE event at the given angle.
InjectHingeAngle(int angle)122 void InjectHingeAngle(int angle) {
123 sp<ISensors> sensors = startSensorInjection();
124 int handle = getSensorHandle(SensorType::HINGE_ANGLE, sensors);
125
126 // Create a base ISensors hinge_angle event.
127 Event event;
128 event.sensorHandle = handle;
129 event.sensorType = SensorType::HINGE_ANGLE;
130 event.u.scalar = angle;
131 event.u.vec3.status = SensorStatus::ACCURACY_HIGH;
132 event.timestamp = android::elapsedRealtimeNano();
133 Result result = sensors->injectSensorData_2_1(event);
134 if (result != Result::OK) {
135 LOG(FATAL) << "Unable to inject HINGE_ANGLE data: " << toString(result);
136 }
137
138 endSensorInjection(sensors);
139 }
140
main(int argc,char ** argv)141 int main(int argc, char** argv) {
142 if (argc == 2) {
143 LOG(FATAL) << "Expected command line args 'rotate <portrait|landscape>' or "
144 "'hinge_angle <value>'";
145 }
146
147 if (!strcmp(argv[1], "rotate")) {
148 bool portrait = true;
149 if (!strcmp(argv[2], "portrait")) {
150 portrait = true;
151 } else if (!strcmp(argv[2], "landscape")) {
152 portrait = false;
153 } else {
154 LOG(FATAL) << "Expected command line arg 'portrait' or 'landscape'";
155 }
156 InjectOrientation(portrait);
157 } else if (!strcmp(argv[1], "hinge_angle")) {
158 int angle = std::stoi(argv[2]);
159 if (angle < 0 || angle > 360) {
160 LOG(FATAL) << "Bad hinge_angle value: " << argv[2];
161 }
162 InjectHingeAngle(angle);
163 } else {
164 LOG(FATAL) << "Unknown arg: " << argv[1];
165 }
166 }
167