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