1 /*
2  * Copyright (C) 2022 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 "aidl/sensors/convert.h"
18 #include "android-base/logging.h"
19 
20 namespace android {
21 namespace hardware {
22 namespace sensors {
23 namespace implementation {
24 
25 using aidl::android::hardware::sensors::AdditionalInfo;
26 using aidl::android::hardware::sensors::DynamicSensorInfo;
27 using aidl::android::hardware::sensors::Event;
28 using aidl::android::hardware::sensors::ISensors;
29 using aidl::android::hardware::sensors::SensorInfo;
30 using aidl::android::hardware::sensors::SensorStatus;
31 using aidl::android::hardware::sensors::SensorType;
32 
convertToStatus(ndk::ScopedAStatus status)33 status_t convertToStatus(ndk::ScopedAStatus status) {
34     if (status.isOk()) {
35         return OK;
36     } else {
37         switch (status.getExceptionCode()) {
38             case EX_ILLEGAL_ARGUMENT: {
39                 return BAD_VALUE;
40             }
41             case EX_SECURITY: {
42                 return PERMISSION_DENIED;
43             }
44             case EX_UNSUPPORTED_OPERATION: {
45                 return INVALID_OPERATION;
46             }
47             case EX_SERVICE_SPECIFIC: {
48                 switch (status.getServiceSpecificError()) {
49                     case ISensors::ERROR_BAD_VALUE: {
50                         return BAD_VALUE;
51                     }
52                     case ISensors::ERROR_NO_MEMORY: {
53                         return NO_MEMORY;
54                     }
55                     default: {
56                         return UNKNOWN_ERROR;
57                     }
58                 }
59             }
60             default: {
61                 return UNKNOWN_ERROR;
62             }
63         }
64     }
65 }
66 
convertToSensor(const SensorInfo & src,sensor_t * dst)67 void convertToSensor(const SensorInfo& src, sensor_t* dst) {
68     dst->name = strdup(src.name.c_str());
69     dst->vendor = strdup(src.vendor.c_str());
70     dst->version = src.version;
71     dst->handle = src.sensorHandle;
72     dst->type = (int)src.type;
73     dst->maxRange = src.maxRange;
74     dst->resolution = src.resolution;
75     dst->power = src.power;
76     dst->minDelay = src.minDelayUs;
77     dst->fifoReservedEventCount = src.fifoReservedEventCount;
78     dst->fifoMaxEventCount = src.fifoMaxEventCount;
79     dst->stringType = strdup(src.typeAsString.c_str());
80     dst->requiredPermission = strdup(src.requiredPermission.c_str());
81     dst->maxDelay = src.maxDelayUs;
82     dst->flags = src.flags;
83     dst->reserved[0] = dst->reserved[1] = 0;
84 }
85 
convertToSensorEvent(const Event & src,sensors_event_t * dst)86 void convertToSensorEvent(const Event& src, sensors_event_t* dst) {
87     *dst = {.version = sizeof(sensors_event_t),
88             .sensor = src.sensorHandle,
89             .type = (int32_t)src.sensorType,
90             .reserved0 = 0,
91             .timestamp = src.timestamp};
92 
93     switch (src.sensorType) {
94         case SensorType::META_DATA: {
95             // Legacy HALs expect the handle reference in the meta data field.
96             // Copy it over from the handle of the event.
97             dst->meta_data.what = (int32_t)src.payload.get<Event::EventPayload::meta>().what;
98             dst->meta_data.sensor = src.sensorHandle;
99             // Set the sensor handle to 0 to maintain compatibility.
100             dst->sensor = 0;
101             break;
102         }
103 
104         case SensorType::ACCELEROMETER:
105         case SensorType::MAGNETIC_FIELD:
106         case SensorType::ORIENTATION:
107         case SensorType::GYROSCOPE:
108         case SensorType::GRAVITY:
109         case SensorType::LINEAR_ACCELERATION: {
110             dst->acceleration.x = src.payload.get<Event::EventPayload::vec3>().x;
111             dst->acceleration.y = src.payload.get<Event::EventPayload::vec3>().y;
112             dst->acceleration.z = src.payload.get<Event::EventPayload::vec3>().z;
113             dst->acceleration.status = (int32_t)src.payload.get<Event::EventPayload::vec3>().status;
114             break;
115         }
116 
117         case SensorType::GAME_ROTATION_VECTOR: {
118             dst->data[0] = src.payload.get<Event::EventPayload::vec4>().x;
119             dst->data[1] = src.payload.get<Event::EventPayload::vec4>().y;
120             dst->data[2] = src.payload.get<Event::EventPayload::vec4>().z;
121             dst->data[3] = src.payload.get<Event::EventPayload::vec4>().w;
122             break;
123         }
124 
125         case SensorType::ROTATION_VECTOR:
126         case SensorType::GEOMAGNETIC_ROTATION_VECTOR: {
127             dst->data[0] = src.payload.get<Event::EventPayload::data>().values[0];
128             dst->data[1] = src.payload.get<Event::EventPayload::data>().values[1];
129             dst->data[2] = src.payload.get<Event::EventPayload::data>().values[2];
130             dst->data[3] = src.payload.get<Event::EventPayload::data>().values[3];
131             dst->data[4] = src.payload.get<Event::EventPayload::data>().values[4];
132             break;
133         }
134 
135         case SensorType::MAGNETIC_FIELD_UNCALIBRATED:
136         case SensorType::GYROSCOPE_UNCALIBRATED:
137         case SensorType::ACCELEROMETER_UNCALIBRATED: {
138             dst->uncalibrated_gyro.x_uncalib = src.payload.get<Event::EventPayload::uncal>().x;
139             dst->uncalibrated_gyro.y_uncalib = src.payload.get<Event::EventPayload::uncal>().y;
140             dst->uncalibrated_gyro.z_uncalib = src.payload.get<Event::EventPayload::uncal>().z;
141             dst->uncalibrated_gyro.x_bias = src.payload.get<Event::EventPayload::uncal>().xBias;
142             dst->uncalibrated_gyro.y_bias = src.payload.get<Event::EventPayload::uncal>().yBias;
143             dst->uncalibrated_gyro.z_bias = src.payload.get<Event::EventPayload::uncal>().zBias;
144             break;
145         }
146 
147         case SensorType::HINGE_ANGLE:
148         case SensorType::DEVICE_ORIENTATION:
149         case SensorType::LIGHT:
150         case SensorType::PRESSURE:
151         case SensorType::PROXIMITY:
152         case SensorType::RELATIVE_HUMIDITY:
153         case SensorType::AMBIENT_TEMPERATURE:
154         case SensorType::SIGNIFICANT_MOTION:
155         case SensorType::STEP_DETECTOR:
156         case SensorType::TILT_DETECTOR:
157         case SensorType::WAKE_GESTURE:
158         case SensorType::GLANCE_GESTURE:
159         case SensorType::PICK_UP_GESTURE:
160         case SensorType::WRIST_TILT_GESTURE:
161         case SensorType::STATIONARY_DETECT:
162         case SensorType::MOTION_DETECT:
163         case SensorType::HEART_BEAT:
164         case SensorType::LOW_LATENCY_OFFBODY_DETECT: {
165             dst->data[0] = src.payload.get<Event::EventPayload::scalar>();
166             break;
167         }
168 
169         case SensorType::STEP_COUNTER: {
170             dst->u64.step_counter = src.payload.get<Event::EventPayload::stepCount>();
171             break;
172         }
173 
174         case SensorType::HEART_RATE: {
175             dst->heart_rate.bpm = src.payload.get<Event::EventPayload::heartRate>().bpm;
176             dst->heart_rate.status =
177                     (int8_t)src.payload.get<Event::EventPayload::heartRate>().status;
178             break;
179         }
180 
181         case SensorType::POSE_6DOF: {  // 15 floats
182             for (size_t i = 0; i < 15; ++i) {
183                 dst->data[i] = src.payload.get<Event::EventPayload::pose6DOF>().values[i];
184             }
185             break;
186         }
187 
188         case SensorType::DYNAMIC_SENSOR_META: {
189             dst->dynamic_sensor_meta.connected =
190                     src.payload.get<Event::EventPayload::dynamic>().connected;
191             dst->dynamic_sensor_meta.handle =
192                     src.payload.get<Event::EventPayload::dynamic>().sensorHandle;
193             dst->dynamic_sensor_meta.sensor = NULL;  // to be filled in later
194 
195             memcpy(dst->dynamic_sensor_meta.uuid,
196                    src.payload.get<Event::EventPayload::dynamic>().uuid.values.data(), 16);
197 
198             break;
199         }
200 
201         case SensorType::ADDITIONAL_INFO: {
202             const AdditionalInfo& srcInfo = src.payload.get<Event::EventPayload::additional>();
203 
204             additional_info_event_t* dstInfo = &dst->additional_info;
205             dstInfo->type = (int32_t)srcInfo.type;
206             dstInfo->serial = srcInfo.serial;
207 
208             switch (srcInfo.payload.getTag()) {
209                 case AdditionalInfo::AdditionalInfoPayload::Tag::dataInt32: {
210                     const auto& values =
211                             srcInfo.payload.get<AdditionalInfo::AdditionalInfoPayload::dataInt32>()
212                                     .values;
213                     CHECK_EQ(values.size() * sizeof(int32_t), sizeof(dstInfo->data_int32));
214                     memcpy(dstInfo->data_int32, values.data(), sizeof(dstInfo->data_int32));
215                     break;
216                 }
217                 case AdditionalInfo::AdditionalInfoPayload::Tag::dataFloat: {
218                     const auto& values =
219                             srcInfo.payload.get<AdditionalInfo::AdditionalInfoPayload::dataFloat>()
220                                     .values;
221                     CHECK_EQ(values.size() * sizeof(float), sizeof(dstInfo->data_float));
222                     memcpy(dstInfo->data_float, values.data(), sizeof(dstInfo->data_float));
223                     break;
224                 }
225                 default: {
226                     LOG(ERROR) << "Invalid sensor additional info tag: ",
227                             (int)srcInfo.payload.getTag();
228                 }
229             }
230             break;
231         }
232 
233         case SensorType::HEAD_TRACKER: {
234             const auto& ht = src.payload.get<Event::EventPayload::headTracker>();
235             dst->head_tracker.rx = ht.rx;
236             dst->head_tracker.ry = ht.ry;
237             dst->head_tracker.rz = ht.rz;
238             dst->head_tracker.vx = ht.vx;
239             dst->head_tracker.vy = ht.vy;
240             dst->head_tracker.vz = ht.vz;
241             dst->head_tracker.discontinuity_count = ht.discontinuityCount;
242             break;
243         }
244 
245         case SensorType::ACCELEROMETER_LIMITED_AXES:
246         case SensorType::GYROSCOPE_LIMITED_AXES:
247             dst->limited_axes_imu.x = src.payload.get<Event::EventPayload::limitedAxesImu>().x;
248             dst->limited_axes_imu.y = src.payload.get<Event::EventPayload::limitedAxesImu>().y;
249             dst->limited_axes_imu.z = src.payload.get<Event::EventPayload::limitedAxesImu>().z;
250             dst->limited_axes_imu.x_supported =
251                     src.payload.get<Event::EventPayload::limitedAxesImu>().xSupported;
252             dst->limited_axes_imu.y_supported =
253                     src.payload.get<Event::EventPayload::limitedAxesImu>().ySupported;
254             dst->limited_axes_imu.z_supported =
255                     src.payload.get<Event::EventPayload::limitedAxesImu>().zSupported;
256             break;
257 
258         case SensorType::ACCELEROMETER_LIMITED_AXES_UNCALIBRATED:
259         case SensorType::GYROSCOPE_LIMITED_AXES_UNCALIBRATED:
260             dst->limited_axes_imu_uncalibrated.x_uncalib =
261                     src.payload.get<Event::EventPayload::limitedAxesImuUncal>().x;
262             dst->limited_axes_imu_uncalibrated.y_uncalib =
263                     src.payload.get<Event::EventPayload::limitedAxesImuUncal>().y;
264             dst->limited_axes_imu_uncalibrated.z_uncalib =
265                     src.payload.get<Event::EventPayload::limitedAxesImuUncal>().z;
266             dst->limited_axes_imu_uncalibrated.x_bias =
267                     src.payload.get<Event::EventPayload::limitedAxesImuUncal>().xBias;
268             dst->limited_axes_imu_uncalibrated.y_bias =
269                     src.payload.get<Event::EventPayload::limitedAxesImuUncal>().yBias;
270             dst->limited_axes_imu_uncalibrated.z_bias =
271                     src.payload.get<Event::EventPayload::limitedAxesImuUncal>().zBias;
272             dst->limited_axes_imu_uncalibrated.x_supported =
273                     src.payload.get<Event::EventPayload::limitedAxesImuUncal>().xSupported;
274             dst->limited_axes_imu_uncalibrated.y_supported =
275                     src.payload.get<Event::EventPayload::limitedAxesImuUncal>().ySupported;
276             dst->limited_axes_imu_uncalibrated.z_supported =
277                     src.payload.get<Event::EventPayload::limitedAxesImuUncal>().zSupported;
278             break;
279 
280         case SensorType::HEADING:
281             dst->heading.heading = src.payload.get<Event::EventPayload::heading>().heading;
282             dst->heading.accuracy = src.payload.get<Event::EventPayload::heading>().accuracy;
283             break;
284 
285         default: {
286             CHECK_GE((int32_t)src.sensorType, (int32_t)SensorType::DEVICE_PRIVATE_BASE);
287 
288             memcpy(dst->data, src.payload.get<Event::EventPayload::data>().values.data(),
289                    16 * sizeof(float));
290             break;
291         }
292     }
293 }
294 
convertFromSensorEvent(const sensors_event_t & src,Event * dst)295 void convertFromSensorEvent(const sensors_event_t& src, Event* dst) {
296     *dst = {
297             .timestamp = src.timestamp,
298             .sensorHandle = src.sensor,
299             .sensorType = (SensorType)src.type,
300     };
301 
302     switch (dst->sensorType) {
303         case SensorType::META_DATA: {
304             Event::EventPayload::MetaData meta;
305             meta.what = (Event::EventPayload::MetaData::MetaDataEventType)src.meta_data.what;
306             // Legacy HALs contain the handle reference in the meta data field.
307             // Copy that over to the handle of the event. In legacy HALs this
308             // field was expected to be 0.
309             dst->sensorHandle = src.meta_data.sensor;
310             dst->payload.set<Event::EventPayload::Tag::meta>(meta);
311             break;
312         }
313 
314         case SensorType::ACCELEROMETER:
315         case SensorType::MAGNETIC_FIELD:
316         case SensorType::ORIENTATION:
317         case SensorType::GYROSCOPE:
318         case SensorType::GRAVITY:
319         case SensorType::LINEAR_ACCELERATION: {
320             Event::EventPayload::Vec3 vec3;
321             vec3.x = src.acceleration.x;
322             vec3.y = src.acceleration.y;
323             vec3.z = src.acceleration.z;
324             vec3.status = (SensorStatus)src.acceleration.status;
325             dst->payload.set<Event::EventPayload::Tag::vec3>(vec3);
326             break;
327         }
328 
329         case SensorType::GAME_ROTATION_VECTOR: {
330             Event::EventPayload::Vec4 vec4;
331             vec4.x = src.data[0];
332             vec4.y = src.data[1];
333             vec4.z = src.data[2];
334             vec4.w = src.data[3];
335             dst->payload.set<Event::EventPayload::Tag::vec4>(vec4);
336             break;
337         }
338 
339         case SensorType::ROTATION_VECTOR:
340         case SensorType::GEOMAGNETIC_ROTATION_VECTOR: {
341             Event::EventPayload::Data data;
342             memcpy(data.values.data(), src.data, 5 * sizeof(float));
343             dst->payload.set<Event::EventPayload::Tag::data>(data);
344             break;
345         }
346 
347         case SensorType::MAGNETIC_FIELD_UNCALIBRATED:
348         case SensorType::GYROSCOPE_UNCALIBRATED:
349         case SensorType::ACCELEROMETER_UNCALIBRATED: {
350             Event::EventPayload::Uncal uncal;
351             uncal.x = src.uncalibrated_gyro.x_uncalib;
352             uncal.y = src.uncalibrated_gyro.y_uncalib;
353             uncal.z = src.uncalibrated_gyro.z_uncalib;
354             uncal.xBias = src.uncalibrated_gyro.x_bias;
355             uncal.yBias = src.uncalibrated_gyro.y_bias;
356             uncal.zBias = src.uncalibrated_gyro.z_bias;
357             dst->payload.set<Event::EventPayload::Tag::uncal>(uncal);
358             break;
359         }
360 
361         case SensorType::DEVICE_ORIENTATION:
362         case SensorType::LIGHT:
363         case SensorType::PRESSURE:
364         case SensorType::PROXIMITY:
365         case SensorType::RELATIVE_HUMIDITY:
366         case SensorType::AMBIENT_TEMPERATURE:
367         case SensorType::SIGNIFICANT_MOTION:
368         case SensorType::STEP_DETECTOR:
369         case SensorType::TILT_DETECTOR:
370         case SensorType::WAKE_GESTURE:
371         case SensorType::GLANCE_GESTURE:
372         case SensorType::PICK_UP_GESTURE:
373         case SensorType::WRIST_TILT_GESTURE:
374         case SensorType::STATIONARY_DETECT:
375         case SensorType::MOTION_DETECT:
376         case SensorType::HEART_BEAT:
377         case SensorType::LOW_LATENCY_OFFBODY_DETECT:
378         case SensorType::HINGE_ANGLE: {
379             dst->payload.set<Event::EventPayload::Tag::scalar>((float)src.data[0]);
380             break;
381         }
382 
383         case SensorType::STEP_COUNTER: {
384             dst->payload.set<Event::EventPayload::Tag::stepCount>(src.u64.step_counter);
385             break;
386         }
387 
388         case SensorType::HEART_RATE: {
389             Event::EventPayload::HeartRate heartRate;
390             heartRate.bpm = src.heart_rate.bpm;
391             heartRate.status = (SensorStatus)src.heart_rate.status;
392             dst->payload.set<Event::EventPayload::Tag::heartRate>(heartRate);
393             break;
394         }
395 
396         case SensorType::POSE_6DOF: {  // 15 floats
397             Event::EventPayload::Pose6Dof pose6DOF;
398             for (size_t i = 0; i < 15; ++i) {
399                 pose6DOF.values[i] = src.data[i];
400             }
401             dst->payload.set<Event::EventPayload::Tag::pose6DOF>(pose6DOF);
402             break;
403         }
404 
405         case SensorType::DYNAMIC_SENSOR_META: {
406             DynamicSensorInfo dynamic;
407             dynamic.connected = src.dynamic_sensor_meta.connected;
408             dynamic.sensorHandle = src.dynamic_sensor_meta.handle;
409 
410             memcpy(dynamic.uuid.values.data(), src.dynamic_sensor_meta.uuid, 16);
411             dst->payload.set<Event::EventPayload::Tag::dynamic>(dynamic);
412             break;
413         }
414 
415         case SensorType::ADDITIONAL_INFO: {
416             AdditionalInfo info;
417             const additional_info_event_t& srcInfo = src.additional_info;
418             info.type = (AdditionalInfo::AdditionalInfoType)srcInfo.type;
419             info.serial = srcInfo.serial;
420 
421             AdditionalInfo::AdditionalInfoPayload::Int32Values data;
422             CHECK_EQ(data.values.size() * sizeof(int32_t), sizeof(srcInfo.data_int32));
423             memcpy(data.values.data(), srcInfo.data_int32, sizeof(srcInfo.data_int32));
424             info.payload.set<AdditionalInfo::AdditionalInfoPayload::Tag::dataInt32>(data);
425 
426             dst->payload.set<Event::EventPayload::Tag::additional>(info);
427             break;
428         }
429 
430         case SensorType::HEAD_TRACKER: {
431             Event::EventPayload::HeadTracker headTracker;
432             headTracker.rx = src.head_tracker.rx;
433             headTracker.ry = src.head_tracker.ry;
434             headTracker.rz = src.head_tracker.rz;
435             headTracker.vx = src.head_tracker.vx;
436             headTracker.vy = src.head_tracker.vy;
437             headTracker.vz = src.head_tracker.vz;
438             headTracker.discontinuityCount = src.head_tracker.discontinuity_count;
439 
440             dst->payload.set<Event::EventPayload::Tag::headTracker>(headTracker);
441             break;
442         }
443 
444         case SensorType::ACCELEROMETER_LIMITED_AXES:
445         case SensorType::GYROSCOPE_LIMITED_AXES: {
446             Event::EventPayload::LimitedAxesImu limitedAxesImu;
447             limitedAxesImu.x = src.limited_axes_imu.x;
448             limitedAxesImu.y = src.limited_axes_imu.y;
449             limitedAxesImu.z = src.limited_axes_imu.z;
450             limitedAxesImu.xSupported = src.limited_axes_imu.x_supported;
451             limitedAxesImu.ySupported = src.limited_axes_imu.y_supported;
452             limitedAxesImu.zSupported = src.limited_axes_imu.z_supported;
453             dst->payload.set<Event::EventPayload::Tag::limitedAxesImu>(limitedAxesImu);
454             break;
455         }
456 
457         case SensorType::ACCELEROMETER_LIMITED_AXES_UNCALIBRATED:
458         case SensorType::GYROSCOPE_LIMITED_AXES_UNCALIBRATED: {
459             Event::EventPayload::LimitedAxesImuUncal limitedAxesImuUncal;
460             limitedAxesImuUncal.x = src.limited_axes_imu_uncalibrated.x_uncalib;
461             limitedAxesImuUncal.y = src.limited_axes_imu_uncalibrated.y_uncalib;
462             limitedAxesImuUncal.z = src.limited_axes_imu_uncalibrated.z_uncalib;
463             limitedAxesImuUncal.xBias = src.limited_axes_imu_uncalibrated.x_bias;
464             limitedAxesImuUncal.yBias = src.limited_axes_imu_uncalibrated.y_bias;
465             limitedAxesImuUncal.zBias = src.limited_axes_imu_uncalibrated.z_bias;
466             limitedAxesImuUncal.xSupported = src.limited_axes_imu_uncalibrated.x_supported;
467             limitedAxesImuUncal.ySupported = src.limited_axes_imu_uncalibrated.y_supported;
468             limitedAxesImuUncal.zSupported = src.limited_axes_imu_uncalibrated.z_supported;
469             dst->payload.set<Event::EventPayload::Tag::limitedAxesImuUncal>(limitedAxesImuUncal);
470             break;
471         }
472 
473         case SensorType::HEADING: {
474             Event::EventPayload::Heading heading;
475             heading.heading = src.heading.heading;
476             heading.accuracy = src.heading.accuracy;
477             dst->payload.set<Event::EventPayload::heading>(heading);
478             break;
479         }
480 
481         default: {
482             CHECK_GE((int32_t)dst->sensorType, (int32_t)SensorType::DEVICE_PRIVATE_BASE);
483 
484             Event::EventPayload::Data data;
485             memcpy(data.values.data(), src.data, 16 * sizeof(float));
486             dst->payload.set<Event::EventPayload::Tag::data>(data);
487             break;
488         }
489     }
490 }
491 
convertFromASensorEvent(const ASensorEvent & src,Event * dst)492 void convertFromASensorEvent(const ASensorEvent& src, Event* dst) {
493     convertFromSensorEvent(common::convertASensorEvent(src), dst);
494 }
495 
496 }  // namespace implementation
497 }  // namespace sensors
498 }  // namespace hardware
499 }  // namespace android
500