Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 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 | |
Steven Moreland | cefd01c | 2016-11-07 19:20:19 -0800 | [diff] [blame] | 17 | #include "convert.h" |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 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 | |
| 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 | |
| 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 | |
| 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->sensorHandle = src.sensor; |
| 71 | dst->sensorType = (SensorType)src.type; |
| 72 | dst->timestamp = src.timestamp; |
| 73 | |
| 74 | switch (dst->sensorType) { |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 75 | case SensorType::META_DATA: |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 76 | { |
| 77 | dst->u.meta.what = (MetaDataEventType)src.meta_data.what; |
| 78 | break; |
| 79 | } |
| 80 | |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 81 | case SensorType::ACCELEROMETER: |
| 82 | case SensorType::MAGNETIC_FIELD: |
| 83 | case SensorType::ORIENTATION: |
| 84 | case SensorType::GYROSCOPE: |
| 85 | case SensorType::GRAVITY: |
| 86 | case SensorType::LINEAR_ACCELERATION: |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 87 | { |
| 88 | dst->u.vec3.x = src.acceleration.x; |
| 89 | dst->u.vec3.y = src.acceleration.y; |
| 90 | dst->u.vec3.z = src.acceleration.z; |
| 91 | dst->u.vec3.status = (SensorStatus)src.acceleration.status; |
| 92 | break; |
| 93 | } |
| 94 | |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 95 | case SensorType::ROTATION_VECTOR: |
| 96 | case SensorType::GAME_ROTATION_VECTOR: |
| 97 | case SensorType::GEOMAGNETIC_ROTATION_VECTOR: |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 98 | { |
| 99 | dst->u.vec4.x = src.data[0]; |
| 100 | dst->u.vec4.y = src.data[1]; |
| 101 | dst->u.vec4.z = src.data[2]; |
| 102 | dst->u.vec4.w = src.data[3]; |
| 103 | break; |
| 104 | } |
| 105 | |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 106 | case SensorType::MAGNETIC_FIELD_UNCALIBRATED: |
| 107 | case SensorType::GYROSCOPE_UNCALIBRATED: |
| 108 | case SensorType::ACCELEROMETER_UNCALIBRATED: |
| 109 | { |
| 110 | dst->u.uncal.x = src.uncalibrated_gyro.x_uncalib; |
| 111 | dst->u.uncal.y = src.uncalibrated_gyro.y_uncalib; |
| 112 | dst->u.uncal.z = src.uncalibrated_gyro.z_uncalib; |
| 113 | dst->u.uncal.x_bias = src.uncalibrated_gyro.x_bias; |
| 114 | dst->u.uncal.y_bias = src.uncalibrated_gyro.y_bias; |
| 115 | dst->u.uncal.z_bias = src.uncalibrated_gyro.z_bias; |
| 116 | break; |
| 117 | } |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 118 | |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 119 | case SensorType::DEVICE_ORIENTATION: |
| 120 | case SensorType::LIGHT: |
| 121 | case SensorType::PRESSURE: |
| 122 | case SensorType::TEMPERATURE: |
| 123 | case SensorType::PROXIMITY: |
| 124 | case SensorType::RELATIVE_HUMIDITY: |
| 125 | case SensorType::AMBIENT_TEMPERATURE: |
| 126 | case SensorType::SIGNIFICANT_MOTION: |
| 127 | case SensorType::STEP_DETECTOR: |
| 128 | case SensorType::TILT_DETECTOR: |
| 129 | case SensorType::WAKE_GESTURE: |
| 130 | case SensorType::GLANCE_GESTURE: |
| 131 | case SensorType::PICK_UP_GESTURE: |
| 132 | case SensorType::WRIST_TILT_GESTURE: |
| 133 | case SensorType::STATIONARY_DETECT: |
| 134 | case SensorType::MOTION_DETECT: |
| 135 | case SensorType::HEART_BEAT: |
| 136 | { |
| 137 | dst->u.scalar = src.data[0]; |
| 138 | break; |
| 139 | } |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 140 | |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 141 | case SensorType::STEP_COUNTER: |
| 142 | { |
| 143 | dst->u.stepCount = src.u64.step_counter; |
| 144 | break; |
| 145 | } |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 146 | |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 147 | case SensorType::HEART_RATE: |
| 148 | { |
| 149 | dst->u.heartRate.bpm = src.heart_rate.bpm; |
| 150 | dst->u.heartRate.status = (SensorStatus)src.heart_rate.status; |
| 151 | break; |
| 152 | } |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 153 | |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 154 | case SensorType::POSE_6DOF: // 15 floats |
| 155 | { |
| 156 | for (size_t i = 0; i < 15; ++i) { |
| 157 | dst->u.pose6DOF[i] = src.data[i]; |
| 158 | } |
| 159 | break; |
| 160 | } |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 161 | |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 162 | case SensorType::DYNAMIC_SENSOR_META: |
| 163 | { |
| 164 | dst->u.dynamic.connected = src.dynamic_sensor_meta.connected; |
| 165 | dst->u.dynamic.sensorHandle = src.dynamic_sensor_meta.handle; |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 166 | |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 167 | memcpy(dst->u.dynamic.uuid.data(), |
| 168 | src.dynamic_sensor_meta.uuid, |
| 169 | 16); |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 170 | |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 171 | break; |
| 172 | } |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 173 | |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 174 | case SensorType::ADDITIONAL_INFO: |
| 175 | { |
| 176 | ::android::hardware::sensors::V1_0::AdditionalInfo *dstInfo = |
| 177 | &dst->u.additional; |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 178 | |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 179 | const additional_info_event_t &srcInfo = src.additional_info; |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 180 | |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 181 | dstInfo->type = |
| 182 | (::android::hardware::sensors::V1_0::AdditionalInfoType) |
| 183 | srcInfo.type; |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 184 | |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 185 | dstInfo->serial = srcInfo.serial; |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 186 | |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 187 | CHECK_EQ(sizeof(dstInfo->u), sizeof(srcInfo.data_int32)); |
| 188 | memcpy(&dstInfo->u, srcInfo.data_int32, sizeof(srcInfo.data_int32)); |
| 189 | break; |
| 190 | } |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 191 | |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 192 | default: |
| 193 | { |
| 194 | CHECK_GE((int32_t)dst->sensorType, |
| 195 | (int32_t)SensorType::DEVICE_PRIVATE_BASE); |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 196 | |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 197 | memcpy(dst->u.data.data(), src.data, 16 * sizeof(float)); |
| 198 | break; |
| 199 | } |
| 200 | } |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | void convertToSensorEvent(const Event &src, sensors_event_t *dst) { |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 204 | dst->version = sizeof(sensors_event_t); |
| 205 | dst->sensor = src.sensorHandle; |
| 206 | dst->type = (int32_t)src.sensorType; |
| 207 | dst->reserved0 = 0; |
| 208 | dst->timestamp = src.timestamp; |
| 209 | dst->flags = 0; |
| 210 | dst->reserved1[0] = dst->reserved1[1] = dst->reserved1[2] = 0; |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 211 | |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 212 | switch (src.sensorType) { |
| 213 | case SensorType::META_DATA: |
| 214 | { |
| 215 | dst->meta_data.what = (int32_t)src.u.meta.what; |
| 216 | dst->meta_data.sensor = dst->sensor; |
| 217 | break; |
| 218 | } |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 219 | |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 220 | case SensorType::ACCELEROMETER: |
| 221 | case SensorType::MAGNETIC_FIELD: |
| 222 | case SensorType::ORIENTATION: |
| 223 | case SensorType::GYROSCOPE: |
| 224 | case SensorType::GRAVITY: |
| 225 | case SensorType::LINEAR_ACCELERATION: |
| 226 | { |
| 227 | dst->acceleration.x = src.u.vec3.x; |
| 228 | dst->acceleration.y = src.u.vec3.y; |
| 229 | dst->acceleration.z = src.u.vec3.z; |
| 230 | dst->acceleration.status = (int8_t)src.u.vec3.status; |
| 231 | break; |
| 232 | } |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 233 | |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 234 | case SensorType::ROTATION_VECTOR: |
| 235 | case SensorType::GAME_ROTATION_VECTOR: |
| 236 | case SensorType::GEOMAGNETIC_ROTATION_VECTOR: |
| 237 | { |
| 238 | dst->data[0] = src.u.vec4.x; |
| 239 | dst->data[1] = src.u.vec4.y; |
| 240 | dst->data[2] = src.u.vec4.z; |
| 241 | dst->data[3] = src.u.vec4.w; |
| 242 | break; |
| 243 | } |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 244 | |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 245 | case SensorType::MAGNETIC_FIELD_UNCALIBRATED: |
| 246 | case SensorType::GYROSCOPE_UNCALIBRATED: |
| 247 | case SensorType::ACCELEROMETER_UNCALIBRATED: |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 248 | { |
| 249 | dst->uncalibrated_gyro.x_uncalib = src.u.uncal.x; |
| 250 | dst->uncalibrated_gyro.y_uncalib = src.u.uncal.y; |
| 251 | dst->uncalibrated_gyro.z_uncalib = src.u.uncal.z; |
| 252 | dst->uncalibrated_gyro.x_bias = src.u.uncal.x_bias; |
| 253 | dst->uncalibrated_gyro.y_bias = src.u.uncal.y_bias; |
| 254 | dst->uncalibrated_gyro.z_bias = src.u.uncal.z_bias; |
| 255 | break; |
| 256 | } |
| 257 | |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 258 | case SensorType::DEVICE_ORIENTATION: |
| 259 | case SensorType::LIGHT: |
| 260 | case SensorType::PRESSURE: |
| 261 | case SensorType::TEMPERATURE: |
| 262 | case SensorType::PROXIMITY: |
| 263 | case SensorType::RELATIVE_HUMIDITY: |
| 264 | case SensorType::AMBIENT_TEMPERATURE: |
| 265 | case SensorType::SIGNIFICANT_MOTION: |
| 266 | case SensorType::STEP_DETECTOR: |
| 267 | case SensorType::TILT_DETECTOR: |
| 268 | case SensorType::WAKE_GESTURE: |
| 269 | case SensorType::GLANCE_GESTURE: |
| 270 | case SensorType::PICK_UP_GESTURE: |
| 271 | case SensorType::WRIST_TILT_GESTURE: |
| 272 | case SensorType::STATIONARY_DETECT: |
| 273 | case SensorType::MOTION_DETECT: |
| 274 | case SensorType::HEART_BEAT: |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 275 | { |
| 276 | dst->data[0] = src.u.scalar; |
| 277 | break; |
| 278 | } |
| 279 | |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 280 | case SensorType::STEP_COUNTER: |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 281 | { |
| 282 | dst->u64.step_counter = src.u.stepCount; |
| 283 | break; |
| 284 | } |
| 285 | |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 286 | case SensorType::HEART_RATE: |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 287 | { |
| 288 | dst->heart_rate.bpm = src.u.heartRate.bpm; |
| 289 | dst->heart_rate.status = (int8_t)src.u.heartRate.status; |
| 290 | break; |
| 291 | } |
| 292 | |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 293 | case SensorType::POSE_6DOF: // 15 floats |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 294 | { |
| 295 | for (size_t i = 0; i < 15; ++i) { |
| 296 | dst->data[i] = src.u.pose6DOF[i]; |
| 297 | } |
| 298 | break; |
| 299 | } |
| 300 | |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 301 | case SensorType::DYNAMIC_SENSOR_META: |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 302 | { |
| 303 | dst->dynamic_sensor_meta.connected = src.u.dynamic.connected; |
| 304 | dst->dynamic_sensor_meta.handle = src.u.dynamic.sensorHandle; |
| 305 | dst->dynamic_sensor_meta.sensor = NULL; // to be filled in later |
| 306 | |
| 307 | memcpy(dst->dynamic_sensor_meta.uuid, |
| 308 | src.u.dynamic.uuid.data(), |
| 309 | 16); |
| 310 | |
| 311 | break; |
| 312 | } |
| 313 | |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 314 | case SensorType::ADDITIONAL_INFO: |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 315 | { |
| 316 | const ::android::hardware::sensors::V1_0::AdditionalInfo &srcInfo = |
| 317 | src.u.additional; |
| 318 | |
| 319 | additional_info_event_t *dstInfo = &dst->additional_info; |
| 320 | dstInfo->type = (int32_t)srcInfo.type; |
| 321 | dstInfo->serial = srcInfo.serial; |
| 322 | |
| 323 | CHECK_EQ(sizeof(srcInfo.u), sizeof(dstInfo->data_int32)); |
| 324 | |
| 325 | memcpy(dstInfo->data_int32, |
| 326 | &srcInfo.u, |
| 327 | sizeof(dstInfo->data_int32)); |
| 328 | |
| 329 | break; |
| 330 | } |
| 331 | |
| 332 | default: |
| 333 | { |
| 334 | CHECK_GE((int32_t)src.sensorType, |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 335 | (int32_t)SensorType::DEVICE_PRIVATE_BASE); |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 336 | |
| 337 | memcpy(dst->data, src.u.data.data(), 16 * sizeof(float)); |
| 338 | break; |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | } // namespace implementation |
| 344 | } // namespace V1_0 |
| 345 | } // namespace sensors |
| 346 | } // namespace hardware |
| 347 | } // namespace android |
| 348 | |