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 "ConvertUtils.h"
18 #include <android-base/logging.h>
19 #include <log/log.h>
20
21 using AidlSensorInfo = ::aidl::android::hardware::sensors::SensorInfo;
22 using AidlSensorType = ::aidl::android::hardware::sensors::SensorType;
23 using AidlEvent = ::aidl::android::hardware::sensors::Event;
24 using AidlSensorStatus = ::aidl::android::hardware::sensors::SensorStatus;
25 using ::aidl::android::hardware::sensors::AdditionalInfo;
26 using ::aidl::android::hardware::sensors::DynamicSensorInfo;
27 using ::android::hardware::sensors::V1_0::MetaDataEventType;
28 using V1_0SensorStatus = ::android::hardware::sensors::V1_0::SensorStatus;
29 using ::android::hardware::sensors::V1_0::AdditionalInfoType;
30 using V2_1SensorInfo = ::android::hardware::sensors::V2_1::SensorInfo;
31 using V2_1Event = ::android::hardware::sensors::V2_1::Event;
32 using V2_1SensorType = ::android::hardware::sensors::V2_1::SensorType;
33
34 namespace aidl {
35 namespace android {
36 namespace hardware {
37 namespace sensors {
38 namespace implementation {
39
convertSensorInfo(const V2_1SensorInfo & sensorInfo)40 AidlSensorInfo convertSensorInfo(const V2_1SensorInfo& sensorInfo) {
41 AidlSensorInfo aidlSensorInfo;
42 aidlSensorInfo.sensorHandle = sensorInfo.sensorHandle;
43 aidlSensorInfo.name = sensorInfo.name;
44 aidlSensorInfo.vendor = sensorInfo.vendor;
45 aidlSensorInfo.version = sensorInfo.version;
46 aidlSensorInfo.type = (AidlSensorType)sensorInfo.type;
47 aidlSensorInfo.typeAsString = sensorInfo.typeAsString;
48 aidlSensorInfo.maxRange = sensorInfo.maxRange;
49 aidlSensorInfo.resolution = sensorInfo.resolution;
50 aidlSensorInfo.power = sensorInfo.power;
51 aidlSensorInfo.minDelayUs = sensorInfo.minDelay;
52 aidlSensorInfo.fifoReservedEventCount = sensorInfo.fifoReservedEventCount;
53 aidlSensorInfo.fifoMaxEventCount = sensorInfo.fifoMaxEventCount;
54 aidlSensorInfo.requiredPermission = sensorInfo.requiredPermission;
55 aidlSensorInfo.maxDelayUs = sensorInfo.maxDelay;
56 aidlSensorInfo.flags = sensorInfo.flags;
57 return aidlSensorInfo;
58 }
59
convertToHidlEvent(const AidlEvent & aidlEvent,V2_1Event * hidlEvent)60 void convertToHidlEvent(const AidlEvent& aidlEvent, V2_1Event* hidlEvent) {
61 static_assert(decltype(hidlEvent->u.data)::elementCount() == 16);
62 hidlEvent->timestamp = aidlEvent.timestamp;
63 hidlEvent->sensorHandle = aidlEvent.sensorHandle;
64 hidlEvent->sensorType = (V2_1SensorType)aidlEvent.sensorType;
65
66 switch (aidlEvent.sensorType) {
67 case AidlSensorType::META_DATA:
68 hidlEvent->u.meta.what =
69 (MetaDataEventType)aidlEvent.payload.get<Event::EventPayload::meta>().what;
70 break;
71 case AidlSensorType::ACCELEROMETER:
72 case AidlSensorType::MAGNETIC_FIELD:
73 case AidlSensorType::ORIENTATION:
74 case AidlSensorType::GYROSCOPE:
75 case AidlSensorType::GRAVITY:
76 case AidlSensorType::LINEAR_ACCELERATION:
77 hidlEvent->u.vec3.x = aidlEvent.payload.get<Event::EventPayload::vec3>().x;
78 hidlEvent->u.vec3.y = aidlEvent.payload.get<Event::EventPayload::vec3>().y;
79 hidlEvent->u.vec3.z = aidlEvent.payload.get<Event::EventPayload::vec3>().z;
80 hidlEvent->u.vec3.status =
81 (V1_0SensorStatus)aidlEvent.payload.get<Event::EventPayload::vec3>().status;
82 break;
83 case AidlSensorType::GAME_ROTATION_VECTOR:
84 hidlEvent->u.vec4.x = aidlEvent.payload.get<Event::EventPayload::vec4>().x;
85 hidlEvent->u.vec4.y = aidlEvent.payload.get<Event::EventPayload::vec4>().y;
86 hidlEvent->u.vec4.z = aidlEvent.payload.get<Event::EventPayload::vec4>().z;
87 hidlEvent->u.vec4.w = aidlEvent.payload.get<Event::EventPayload::vec4>().w;
88 break;
89 case AidlSensorType::ROTATION_VECTOR:
90 case AidlSensorType::GEOMAGNETIC_ROTATION_VECTOR:
91 std::copy(aidlEvent.payload.get<Event::EventPayload::data>().values.data(),
92 aidlEvent.payload.get<Event::EventPayload::data>().values.data() + 5,
93 hidlEvent->u.data.data());
94 break;
95 case AidlSensorType::ACCELEROMETER_UNCALIBRATED:
96 case AidlSensorType::MAGNETIC_FIELD_UNCALIBRATED:
97 case AidlSensorType::GYROSCOPE_UNCALIBRATED:
98 hidlEvent->u.uncal.x = aidlEvent.payload.get<Event::EventPayload::uncal>().x;
99 hidlEvent->u.uncal.y = aidlEvent.payload.get<Event::EventPayload::uncal>().y;
100 hidlEvent->u.uncal.z = aidlEvent.payload.get<Event::EventPayload::uncal>().z;
101 hidlEvent->u.uncal.x_bias = aidlEvent.payload.get<Event::EventPayload::uncal>().xBias;
102 hidlEvent->u.uncal.y_bias = aidlEvent.payload.get<Event::EventPayload::uncal>().yBias;
103 hidlEvent->u.uncal.z_bias = aidlEvent.payload.get<Event::EventPayload::uncal>().zBias;
104 break;
105 case AidlSensorType::DEVICE_ORIENTATION:
106 case AidlSensorType::LIGHT:
107 case AidlSensorType::PRESSURE:
108 case AidlSensorType::PROXIMITY:
109 case AidlSensorType::RELATIVE_HUMIDITY:
110 case AidlSensorType::AMBIENT_TEMPERATURE:
111 case AidlSensorType::SIGNIFICANT_MOTION:
112 case AidlSensorType::STEP_DETECTOR:
113 case AidlSensorType::TILT_DETECTOR:
114 case AidlSensorType::WAKE_GESTURE:
115 case AidlSensorType::GLANCE_GESTURE:
116 case AidlSensorType::PICK_UP_GESTURE:
117 case AidlSensorType::WRIST_TILT_GESTURE:
118 case AidlSensorType::STATIONARY_DETECT:
119 case AidlSensorType::MOTION_DETECT:
120 case AidlSensorType::HEART_BEAT:
121 case AidlSensorType::LOW_LATENCY_OFFBODY_DETECT:
122 case AidlSensorType::HINGE_ANGLE:
123 hidlEvent->u.scalar = aidlEvent.payload.get<Event::EventPayload::scalar>();
124 break;
125 case AidlSensorType::STEP_COUNTER:
126 hidlEvent->u.stepCount = aidlEvent.payload.get<AidlEvent::EventPayload::stepCount>();
127 break;
128 case AidlSensorType::HEART_RATE:
129 hidlEvent->u.heartRate.bpm =
130 aidlEvent.payload.get<AidlEvent::EventPayload::heartRate>().bpm;
131 hidlEvent->u.heartRate.status =
132 (V1_0SensorStatus)aidlEvent.payload.get<Event::EventPayload::heartRate>()
133 .status;
134 break;
135 case AidlSensorType::POSE_6DOF:
136 std::copy(std::begin(aidlEvent.payload.get<AidlEvent::EventPayload::pose6DOF>().values),
137 std::end(aidlEvent.payload.get<AidlEvent::EventPayload::pose6DOF>().values),
138 hidlEvent->u.pose6DOF.data());
139 break;
140 case AidlSensorType::DYNAMIC_SENSOR_META:
141 hidlEvent->u.dynamic.connected =
142 aidlEvent.payload.get<Event::EventPayload::dynamic>().connected;
143 hidlEvent->u.dynamic.sensorHandle =
144 aidlEvent.payload.get<Event::EventPayload::dynamic>().sensorHandle;
145 std::copy(
146 std::begin(
147 aidlEvent.payload.get<AidlEvent::EventPayload::dynamic>().uuid.values),
148 std::end(aidlEvent.payload.get<AidlEvent::EventPayload::dynamic>().uuid.values),
149 hidlEvent->u.dynamic.uuid.data());
150 break;
151 case AidlSensorType::ADDITIONAL_INFO: {
152 const AdditionalInfo& additionalInfo =
153 aidlEvent.payload.get<AidlEvent::EventPayload::additional>();
154 hidlEvent->u.additional.type = (AdditionalInfoType)additionalInfo.type;
155 hidlEvent->u.additional.serial = additionalInfo.serial;
156
157 switch (additionalInfo.payload.getTag()) {
158 case AdditionalInfo::AdditionalInfoPayload::Tag::dataInt32: {
159 const auto& aidlData =
160 additionalInfo.payload
161 .get<AdditionalInfo::AdditionalInfoPayload::dataInt32>()
162 .values;
163 std::copy(std::begin(aidlData), std::end(aidlData),
164 hidlEvent->u.additional.u.data_int32.data());
165 break;
166 }
167 case AdditionalInfo::AdditionalInfoPayload::Tag::dataFloat: {
168 const auto& aidlData =
169 additionalInfo.payload
170 .get<AdditionalInfo::AdditionalInfoPayload::dataFloat>()
171 .values;
172 std::copy(std::begin(aidlData), std::end(aidlData),
173 hidlEvent->u.additional.u.data_float.data());
174 break;
175 }
176 default:
177 ALOGE("Invalid sensor additioanl info tag: %d",
178 static_cast<int32_t>(additionalInfo.payload.getTag()));
179 break;
180 }
181 break;
182 }
183 case AidlSensorType::HEAD_TRACKER: {
184 const auto& ht = aidlEvent.payload.get<Event::EventPayload::headTracker>();
185 hidlEvent->u.data[0] = ht.rx;
186 hidlEvent->u.data[1] = ht.ry;
187 hidlEvent->u.data[2] = ht.rz;
188 hidlEvent->u.data[3] = ht.vx;
189 hidlEvent->u.data[4] = ht.vy;
190 hidlEvent->u.data[5] = ht.vz;
191
192 // IMPORTANT: Because we want to preserve the data range of discontinuityCount,
193 // we assume the data can be interpreted as an int32_t directly (e.g. the underlying
194 // HIDL HAL must be using memcpy or equivalent to store this value).
195 *(reinterpret_cast<int32_t*>(&hidlEvent->u.data[6])) = ht.discontinuityCount;
196 break;
197 }
198 default: {
199 CHECK_GE((int32_t)aidlEvent.sensorType, (int32_t)SensorType::DEVICE_PRIVATE_BASE);
200 std::copy(std::begin(aidlEvent.payload.get<AidlEvent::EventPayload::data>().values),
201 std::end(aidlEvent.payload.get<AidlEvent::EventPayload::data>().values),
202 hidlEvent->u.data.data());
203 break;
204 }
205 }
206 }
207
convertToAidlEvent(const V2_1Event & hidlEvent,AidlEvent * aidlEvent)208 void convertToAidlEvent(const V2_1Event& hidlEvent, AidlEvent* aidlEvent) {
209 static_assert(decltype(hidlEvent.u.data)::elementCount() == 16);
210 aidlEvent->timestamp = hidlEvent.timestamp;
211 aidlEvent->sensorHandle = hidlEvent.sensorHandle;
212 aidlEvent->sensorType = (AidlSensorType)hidlEvent.sensorType;
213 switch (hidlEvent.sensorType) {
214 case V2_1SensorType::META_DATA: {
215 AidlEvent::EventPayload::MetaData meta;
216 meta.what = (Event::EventPayload::MetaData::MetaDataEventType)hidlEvent.u.meta.what;
217 aidlEvent->payload.set<Event::EventPayload::meta>(meta);
218 break;
219 }
220 case V2_1SensorType::ACCELEROMETER:
221 case V2_1SensorType::MAGNETIC_FIELD:
222 case V2_1SensorType::ORIENTATION:
223 case V2_1SensorType::GYROSCOPE:
224 case V2_1SensorType::GRAVITY:
225 case V2_1SensorType::LINEAR_ACCELERATION: {
226 AidlEvent::EventPayload::Vec3 vec3;
227 vec3.x = hidlEvent.u.vec3.x;
228 vec3.y = hidlEvent.u.vec3.y;
229 vec3.z = hidlEvent.u.vec3.z;
230 vec3.status = (SensorStatus)hidlEvent.u.vec3.status;
231 aidlEvent->payload.set<Event::EventPayload::vec3>(vec3);
232 break;
233 }
234 case V2_1SensorType::GAME_ROTATION_VECTOR: {
235 AidlEvent::EventPayload::Vec4 vec4;
236 vec4.x = hidlEvent.u.vec4.x;
237 vec4.y = hidlEvent.u.vec4.y;
238 vec4.z = hidlEvent.u.vec4.z;
239 vec4.w = hidlEvent.u.vec4.w;
240 aidlEvent->payload.set<Event::EventPayload::vec4>(vec4);
241 break;
242 }
243 case V2_1SensorType::ROTATION_VECTOR:
244 case V2_1SensorType::GEOMAGNETIC_ROTATION_VECTOR: {
245 AidlEvent::EventPayload::Data data;
246 std::copy(hidlEvent.u.data.data(), hidlEvent.u.data.data() + 5,
247 std::begin(data.values));
248 aidlEvent->payload.set<Event::EventPayload::data>(data);
249 break;
250 }
251 case V2_1SensorType::MAGNETIC_FIELD_UNCALIBRATED:
252 case V2_1SensorType::GYROSCOPE_UNCALIBRATED:
253 case V2_1SensorType::ACCELEROMETER_UNCALIBRATED: {
254 AidlEvent::EventPayload::Uncal uncal;
255 uncal.x = hidlEvent.u.uncal.x;
256 uncal.y = hidlEvent.u.uncal.y;
257 uncal.z = hidlEvent.u.uncal.z;
258 uncal.xBias = hidlEvent.u.uncal.x_bias;
259 uncal.yBias = hidlEvent.u.uncal.y_bias;
260 uncal.zBias = hidlEvent.u.uncal.z_bias;
261 aidlEvent->payload.set<Event::EventPayload::uncal>(uncal);
262 break;
263 }
264 case V2_1SensorType::DEVICE_ORIENTATION:
265 case V2_1SensorType::LIGHT:
266 case V2_1SensorType::PRESSURE:
267 case V2_1SensorType::PROXIMITY:
268 case V2_1SensorType::RELATIVE_HUMIDITY:
269 case V2_1SensorType::AMBIENT_TEMPERATURE:
270 case V2_1SensorType::SIGNIFICANT_MOTION:
271 case V2_1SensorType::STEP_DETECTOR:
272 case V2_1SensorType::TILT_DETECTOR:
273 case V2_1SensorType::WAKE_GESTURE:
274 case V2_1SensorType::GLANCE_GESTURE:
275 case V2_1SensorType::PICK_UP_GESTURE:
276 case V2_1SensorType::WRIST_TILT_GESTURE:
277 case V2_1SensorType::STATIONARY_DETECT:
278 case V2_1SensorType::MOTION_DETECT:
279 case V2_1SensorType::HEART_BEAT:
280 case V2_1SensorType::LOW_LATENCY_OFFBODY_DETECT:
281 case V2_1SensorType::HINGE_ANGLE:
282 aidlEvent->payload.set<Event::EventPayload::scalar>(hidlEvent.u.scalar);
283 break;
284 case V2_1SensorType::STEP_COUNTER:
285 aidlEvent->payload.set<Event::EventPayload::stepCount>(hidlEvent.u.stepCount);
286 break;
287 case V2_1SensorType::HEART_RATE: {
288 AidlEvent::EventPayload::HeartRate heartRate;
289 heartRate.bpm = hidlEvent.u.heartRate.bpm;
290 heartRate.status = (SensorStatus)hidlEvent.u.heartRate.status;
291 aidlEvent->payload.set<Event::EventPayload::heartRate>(heartRate);
292 break;
293 }
294 case V2_1SensorType::POSE_6DOF: {
295 AidlEvent::EventPayload::Pose6Dof pose6Dof;
296 std::copy(hidlEvent.u.pose6DOF.data(),
297 hidlEvent.u.pose6DOF.data() + hidlEvent.u.pose6DOF.size(),
298 std::begin(pose6Dof.values));
299 aidlEvent->payload.set<Event::EventPayload::pose6DOF>(pose6Dof);
300 break;
301 }
302 case V2_1SensorType::DYNAMIC_SENSOR_META: {
303 DynamicSensorInfo dynamicSensorInfo;
304 dynamicSensorInfo.connected = hidlEvent.u.dynamic.connected;
305 dynamicSensorInfo.sensorHandle = hidlEvent.u.dynamic.sensorHandle;
306 std::copy(hidlEvent.u.dynamic.uuid.data(),
307 hidlEvent.u.dynamic.uuid.data() + hidlEvent.u.dynamic.uuid.size(),
308 std::begin(dynamicSensorInfo.uuid.values));
309 aidlEvent->payload.set<Event::EventPayload::dynamic>(dynamicSensorInfo);
310 break;
311 }
312 case V2_1SensorType::ADDITIONAL_INFO: {
313 AdditionalInfo additionalInfo;
314 additionalInfo.type = (AdditionalInfo::AdditionalInfoType)hidlEvent.u.additional.type;
315 additionalInfo.serial = hidlEvent.u.additional.serial;
316
317 AdditionalInfo::AdditionalInfoPayload::Int32Values int32Values;
318 std::copy(hidlEvent.u.additional.u.data_int32.data(),
319 hidlEvent.u.additional.u.data_int32.data() +
320 hidlEvent.u.additional.u.data_int32.size(),
321 std::begin(int32Values.values));
322 additionalInfo.payload.set<AdditionalInfo::AdditionalInfoPayload::dataInt32>(
323 int32Values);
324 aidlEvent->payload.set<Event::EventPayload::additional>(additionalInfo);
325 break;
326 }
327 default: {
328 if (static_cast<int32_t>(hidlEvent.sensorType) ==
329 static_cast<int32_t>(AidlSensorType::HEAD_TRACKER)) {
330 Event::EventPayload::HeadTracker headTracker;
331 headTracker.rx = hidlEvent.u.data[0];
332 headTracker.ry = hidlEvent.u.data[1];
333 headTracker.rz = hidlEvent.u.data[2];
334 headTracker.vx = hidlEvent.u.data[3];
335 headTracker.vy = hidlEvent.u.data[4];
336 headTracker.vz = hidlEvent.u.data[5];
337
338 // IMPORTANT: Because we want to preserve the data range of discontinuityCount,
339 // we assume the data can be interpreted as an int32_t directly (e.g. the underlying
340 // HIDL HAL must be using memcpy or equivalent to store this value).
341 headTracker.discontinuityCount =
342 *(reinterpret_cast<const int32_t*>(&hidlEvent.u.data[6]));
343
344 aidlEvent->payload.set<Event::EventPayload::Tag::headTracker>(headTracker);
345 } else {
346 CHECK_GE((int32_t)hidlEvent.sensorType,
347 (int32_t)V2_1SensorType::DEVICE_PRIVATE_BASE);
348 AidlEvent::EventPayload::Data data;
349 std::copy(hidlEvent.u.data.data(),
350 hidlEvent.u.data.data() + hidlEvent.u.data.size(),
351 std::begin(data.values));
352 aidlEvent->payload.set<Event::EventPayload::data>(data);
353 }
354 break;
355 }
356 }
357 }
358
359 } // namespace implementation
360 } // namespace sensors
361 } // namespace hardware
362 } // namespace android
363 } // namespace aidl
364