1 /*
2  * Copyright (C) 2016 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 "convert.h"
18 
19 #include <android-base/logging.h>
20 
21 namespace android {
22 namespace hardware {
23 namespace sensors {
24 namespace V1_0 {
25 namespace implementation {
26 
convertFromSensor(const sensor_t & src,SensorInfo * dst)27 void convertFromSensor(const sensor_t &src, SensorInfo *dst) {
28     dst->name = src.name;
29     dst->vendor = src.vendor;
30     dst->version = src.version;
31     dst->sensorHandle = src.handle;
32     dst->type = (SensorType)src.type;
33     dst->maxRange = src.maxRange;
34     dst->resolution = src.resolution;
35     dst->power = src.power;
36     dst->minDelay = src.minDelay;
37     dst->fifoReservedEventCount = src.fifoReservedEventCount;
38     dst->fifoMaxEventCount = src.fifoMaxEventCount;
39     dst->typeAsString = src.stringType;
40     dst->requiredPermission = src.requiredPermission;
41     dst->maxDelay = src.maxDelay;
42     dst->flags = src.flags;
43 }
44 
convertToSensor(const::android::hardware::sensors::V1_0::SensorInfo & src,sensor_t * dst)45 void convertToSensor(
46         const ::android::hardware::sensors::V1_0::SensorInfo &src,
47         sensor_t *dst) {
48     dst->name = strdup(src.name.c_str());
49     dst->vendor = strdup(src.vendor.c_str());
50     dst->version = src.version;
51     dst->handle = src.sensorHandle;
52     dst->type = (int)src.type;
53     dst->maxRange = src.maxRange;
54     dst->resolution = src.resolution;
55     dst->power = src.power;
56     dst->minDelay = src.minDelay;
57     dst->fifoReservedEventCount = src.fifoReservedEventCount;
58     dst->fifoMaxEventCount = src.fifoMaxEventCount;
59     dst->stringType = strdup(src.typeAsString.c_str());
60     dst->requiredPermission = strdup(src.requiredPermission.c_str());
61     dst->maxDelay = src.maxDelay;
62     dst->flags = src.flags;
63     dst->reserved[0] = dst->reserved[1] = 0;
64 }
65 
convertFromSensorEvent(const sensors_event_t & src,Event * dst)66 void convertFromSensorEvent(const sensors_event_t &src, Event *dst) {
67     typedef ::android::hardware::sensors::V1_0::SensorType SensorType;
68     typedef ::android::hardware::sensors::V1_0::MetaDataEventType MetaDataEventType;
69 
70     *dst = {
71             .timestamp = src.timestamp,
72             .sensorHandle = src.sensor,
73             .sensorType = (SensorType)src.type,
74     };
75 
76     switch (dst->sensorType) {
77         case SensorType::META_DATA: {
78             dst->u.meta.what = (MetaDataEventType)src.meta_data.what;
79             // Legacy HALs contain the handle reference in the meta data field.
80             // Copy that over to the handle of the event. In legacy HALs this
81             // field was expected to be 0.
82             dst->sensorHandle = src.meta_data.sensor;
83             break;
84         }
85 
86         case SensorType::ACCELEROMETER:
87         case SensorType::MAGNETIC_FIELD:
88         case SensorType::ORIENTATION:
89         case SensorType::GYROSCOPE:
90         case SensorType::GRAVITY:
91         case SensorType::LINEAR_ACCELERATION: {
92             dst->u.vec3.x = src.acceleration.x;
93             dst->u.vec3.y = src.acceleration.y;
94             dst->u.vec3.z = src.acceleration.z;
95             dst->u.vec3.status = (SensorStatus)src.acceleration.status;
96             break;
97         }
98 
99         case SensorType::GAME_ROTATION_VECTOR: {
100             dst->u.vec4.x = src.data[0];
101             dst->u.vec4.y = src.data[1];
102             dst->u.vec4.z = src.data[2];
103             dst->u.vec4.w = src.data[3];
104             break;
105         }
106 
107         case SensorType::ROTATION_VECTOR:
108         case SensorType::GEOMAGNETIC_ROTATION_VECTOR: {
109             dst->u.data[0] = src.data[0];
110             dst->u.data[1] = src.data[1];
111             dst->u.data[2] = src.data[2];
112             dst->u.data[3] = src.data[3];
113             dst->u.data[4] = src.data[4];
114             break;
115         }
116 
117         case SensorType::MAGNETIC_FIELD_UNCALIBRATED:
118         case SensorType::GYROSCOPE_UNCALIBRATED:
119         case SensorType::ACCELEROMETER_UNCALIBRATED: {
120             dst->u.uncal.x = src.uncalibrated_gyro.x_uncalib;
121             dst->u.uncal.y = src.uncalibrated_gyro.y_uncalib;
122             dst->u.uncal.z = src.uncalibrated_gyro.z_uncalib;
123             dst->u.uncal.x_bias = src.uncalibrated_gyro.x_bias;
124             dst->u.uncal.y_bias = src.uncalibrated_gyro.y_bias;
125             dst->u.uncal.z_bias = src.uncalibrated_gyro.z_bias;
126             break;
127         }
128 
129         case SensorType::DEVICE_ORIENTATION:
130         case SensorType::LIGHT:
131         case SensorType::PRESSURE:
132         case SensorType::TEMPERATURE:
133         case SensorType::PROXIMITY:
134         case SensorType::RELATIVE_HUMIDITY:
135         case SensorType::AMBIENT_TEMPERATURE:
136         case SensorType::SIGNIFICANT_MOTION:
137         case SensorType::STEP_DETECTOR:
138         case SensorType::TILT_DETECTOR:
139         case SensorType::WAKE_GESTURE:
140         case SensorType::GLANCE_GESTURE:
141         case SensorType::PICK_UP_GESTURE:
142         case SensorType::WRIST_TILT_GESTURE:
143         case SensorType::STATIONARY_DETECT:
144         case SensorType::MOTION_DETECT:
145         case SensorType::HEART_BEAT:
146         case SensorType::LOW_LATENCY_OFFBODY_DETECT: {
147             dst->u.scalar = src.data[0];
148             break;
149         }
150 
151         case SensorType::STEP_COUNTER: {
152             dst->u.stepCount = src.u64.step_counter;
153             break;
154         }
155 
156         case SensorType::HEART_RATE: {
157             dst->u.heartRate.bpm = src.heart_rate.bpm;
158             dst->u.heartRate.status = (SensorStatus)src.heart_rate.status;
159             break;
160         }
161 
162         case SensorType::POSE_6DOF: {  // 15 floats
163             for (size_t i = 0; i < 15; ++i) {
164                 dst->u.pose6DOF[i] = src.data[i];
165             }
166             break;
167         }
168 
169         case SensorType::DYNAMIC_SENSOR_META: {
170             dst->u.dynamic.connected = src.dynamic_sensor_meta.connected;
171             dst->u.dynamic.sensorHandle = src.dynamic_sensor_meta.handle;
172 
173             memcpy(dst->u.dynamic.uuid.data(), src.dynamic_sensor_meta.uuid, 16);
174 
175             break;
176         }
177 
178         case SensorType::ADDITIONAL_INFO: {
179             ::android::hardware::sensors::V1_0::AdditionalInfo* dstInfo = &dst->u.additional;
180 
181             const additional_info_event_t& srcInfo = src.additional_info;
182 
183             dstInfo->type = (::android::hardware::sensors::V1_0::AdditionalInfoType)srcInfo.type;
184 
185             dstInfo->serial = srcInfo.serial;
186 
187             CHECK_EQ(sizeof(dstInfo->u), sizeof(srcInfo.data_int32));
188             memcpy(&dstInfo->u, srcInfo.data_int32, sizeof(srcInfo.data_int32));
189             break;
190         }
191 
192         default: {
193             memcpy(dst->u.data.data(), src.data, 16 * sizeof(float));
194             break;
195         }
196     }
197 }
198 
convertFromASensorEvent(const ASensorEvent & src,Event * dst)199 void convertFromASensorEvent(const ASensorEvent& src, Event* dst) {
200     convertFromSensorEvent(
201             android::hardware::sensors::implementation::common::convertASensorEvent(src), dst);
202 }
203 
convertToSensorEvent(const Event & src,sensors_event_t * dst)204 void convertToSensorEvent(const Event &src, sensors_event_t *dst) {
205     *dst = {.version = sizeof(sensors_event_t),
206             .sensor = src.sensorHandle,
207             .type = (int32_t)src.sensorType,
208             .reserved0 = 0,
209             .timestamp = src.timestamp};
210 
211     switch (src.sensorType) {
212         case SensorType::META_DATA: {
213             // Legacy HALs expect the handle reference in the meta data field.
214             // Copy it over from the handle of the event.
215             dst->meta_data.what = (int32_t)src.u.meta.what;
216             dst->meta_data.sensor = src.sensorHandle;
217             // Set the sensor handle to 0 to maintain compatibility.
218             dst->sensor = 0;
219             break;
220         }
221 
222         case SensorType::ACCELEROMETER:
223         case SensorType::MAGNETIC_FIELD:
224         case SensorType::ORIENTATION:
225         case SensorType::GYROSCOPE:
226         case SensorType::GRAVITY:
227         case SensorType::LINEAR_ACCELERATION: {
228             dst->acceleration.x = src.u.vec3.x;
229             dst->acceleration.y = src.u.vec3.y;
230             dst->acceleration.z = src.u.vec3.z;
231             dst->acceleration.status = (int8_t)src.u.vec3.status;
232             break;
233         }
234 
235         case SensorType::GAME_ROTATION_VECTOR: {
236             dst->data[0] = src.u.vec4.x;
237             dst->data[1] = src.u.vec4.y;
238             dst->data[2] = src.u.vec4.z;
239             dst->data[3] = src.u.vec4.w;
240             break;
241         }
242 
243         case SensorType::ROTATION_VECTOR:
244         case SensorType::GEOMAGNETIC_ROTATION_VECTOR: {
245             dst->data[0] = src.u.data[0];
246             dst->data[1] = src.u.data[1];
247             dst->data[2] = src.u.data[2];
248             dst->data[3] = src.u.data[3];
249             dst->data[4] = src.u.data[4];
250             break;
251         }
252 
253         case SensorType::MAGNETIC_FIELD_UNCALIBRATED:
254         case SensorType::GYROSCOPE_UNCALIBRATED:
255         case SensorType::ACCELEROMETER_UNCALIBRATED:
256         {
257             dst->uncalibrated_gyro.x_uncalib = src.u.uncal.x;
258             dst->uncalibrated_gyro.y_uncalib = src.u.uncal.y;
259             dst->uncalibrated_gyro.z_uncalib = src.u.uncal.z;
260             dst->uncalibrated_gyro.x_bias = src.u.uncal.x_bias;
261             dst->uncalibrated_gyro.y_bias = src.u.uncal.y_bias;
262             dst->uncalibrated_gyro.z_bias = src.u.uncal.z_bias;
263             break;
264         }
265 
266         case SensorType::DEVICE_ORIENTATION:
267         case SensorType::LIGHT:
268         case SensorType::PRESSURE:
269         case SensorType::TEMPERATURE:
270         case SensorType::PROXIMITY:
271         case SensorType::RELATIVE_HUMIDITY:
272         case SensorType::AMBIENT_TEMPERATURE:
273         case SensorType::SIGNIFICANT_MOTION:
274         case SensorType::STEP_DETECTOR:
275         case SensorType::TILT_DETECTOR:
276         case SensorType::WAKE_GESTURE:
277         case SensorType::GLANCE_GESTURE:
278         case SensorType::PICK_UP_GESTURE:
279         case SensorType::WRIST_TILT_GESTURE:
280         case SensorType::STATIONARY_DETECT:
281         case SensorType::MOTION_DETECT:
282         case SensorType::HEART_BEAT:
283         case SensorType::LOW_LATENCY_OFFBODY_DETECT: {
284             dst->data[0] = src.u.scalar;
285             break;
286         }
287 
288         case SensorType::STEP_COUNTER: {
289             dst->u64.step_counter = src.u.stepCount;
290             break;
291         }
292 
293         case SensorType::HEART_RATE: {
294             dst->heart_rate.bpm = src.u.heartRate.bpm;
295             dst->heart_rate.status = (int8_t)src.u.heartRate.status;
296             break;
297         }
298 
299         case SensorType::POSE_6DOF: {  // 15 floats
300             for (size_t i = 0; i < 15; ++i) {
301                 dst->data[i] = src.u.pose6DOF[i];
302             }
303             break;
304         }
305 
306         case SensorType::DYNAMIC_SENSOR_META: {
307             dst->dynamic_sensor_meta.connected = src.u.dynamic.connected;
308             dst->dynamic_sensor_meta.handle = src.u.dynamic.sensorHandle;
309             dst->dynamic_sensor_meta.sensor = NULL;  // to be filled in later
310 
311             memcpy(dst->dynamic_sensor_meta.uuid,
312                    src.u.dynamic.uuid.data(),
313                    16);
314 
315             break;
316         }
317 
318         case SensorType::ADDITIONAL_INFO: {
319             const ::android::hardware::sensors::V1_0::AdditionalInfo &srcInfo =
320                 src.u.additional;
321 
322             additional_info_event_t *dstInfo = &dst->additional_info;
323             dstInfo->type = (int32_t)srcInfo.type;
324             dstInfo->serial = srcInfo.serial;
325 
326             CHECK_EQ(sizeof(srcInfo.u), sizeof(dstInfo->data_int32));
327 
328             memcpy(dstInfo->data_int32,
329                    &srcInfo.u,
330                    sizeof(dstInfo->data_int32));
331 
332             break;
333         }
334 
335         default: {
336             memcpy(dst->data, src.u.data.data(), 16 * sizeof(float));
337             break;
338         }
339     }
340 }
341 
convertFromSharedMemInfo(const SharedMemInfo & memIn,sensors_direct_mem_t * memOut)342 bool convertFromSharedMemInfo(const SharedMemInfo& memIn, sensors_direct_mem_t *memOut) {
343     if (memOut == nullptr) {
344         return false;
345     }
346 
347     switch(memIn.type) {
348         case SharedMemType::ASHMEM:
349             memOut->type = SENSOR_DIRECT_MEM_TYPE_ASHMEM;
350             break;
351         case SharedMemType::GRALLOC:
352             memOut->type = SENSOR_DIRECT_MEM_TYPE_GRALLOC;
353             break;
354         default:
355             return false;
356     }
357 
358     switch(memIn.format) {
359         case SharedMemFormat::SENSORS_EVENT:
360             memOut->format = SENSOR_DIRECT_FMT_SENSORS_EVENT;
361             break;
362         default:
363             return false;
364     }
365 
366     if (memIn.memoryHandle == nullptr) {
367         return false;
368     }
369 
370     memOut->size = memIn.size;
371     memOut->handle = memIn.memoryHandle;
372     return true;
373 }
374 
convertFromRateLevel(RateLevel rate)375 int convertFromRateLevel(RateLevel rate) {
376     switch(rate) {
377         case RateLevel::STOP:
378             return SENSOR_DIRECT_RATE_STOP;
379         case RateLevel::NORMAL:
380             return SENSOR_DIRECT_RATE_NORMAL;
381         case RateLevel::FAST:
382             return SENSOR_DIRECT_RATE_FAST;
383         case RateLevel::VERY_FAST:
384             return SENSOR_DIRECT_RATE_VERY_FAST;
385         default:
386             return -1;
387     }
388 }
389 
390 }  // namespace implementation
391 }  // namespace V1_0
392 }  // namespace sensors
393 }  // namespace hardware
394 }  // namespace android
395 
396