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 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 = { 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 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 CHECK_GE((int32_t)dst->sensorType, (int32_t)SensorType::DEVICE_PRIVATE_BASE); 194 195 memcpy(dst->u.data.data(), src.data, 16 * sizeof(float)); 196 break; 197 } 198 } 199 } 200 201 void convertToSensorEvent(const Event &src, sensors_event_t *dst) { 202 *dst = {.version = sizeof(sensors_event_t), 203 .sensor = src.sensorHandle, 204 .type = (int32_t)src.sensorType, 205 .reserved0 = 0, 206 .timestamp = src.timestamp}; 207 208 switch (src.sensorType) { 209 case SensorType::META_DATA: { 210 // Legacy HALs expect the handle reference in the meta data field. 211 // Copy it over from the handle of the event. 212 dst->meta_data.what = (int32_t)src.u.meta.what; 213 dst->meta_data.sensor = src.sensorHandle; 214 // Set the sensor handle to 0 to maintain compatibility. 215 dst->sensor = 0; 216 break; 217 } 218 219 case SensorType::ACCELEROMETER: 220 case SensorType::MAGNETIC_FIELD: 221 case SensorType::ORIENTATION: 222 case SensorType::GYROSCOPE: 223 case SensorType::GRAVITY: 224 case SensorType::LINEAR_ACCELERATION: { 225 dst->acceleration.x = src.u.vec3.x; 226 dst->acceleration.y = src.u.vec3.y; 227 dst->acceleration.z = src.u.vec3.z; 228 dst->acceleration.status = (int8_t)src.u.vec3.status; 229 break; 230 } 231 232 case SensorType::GAME_ROTATION_VECTOR: { 233 dst->data[0] = src.u.vec4.x; 234 dst->data[1] = src.u.vec4.y; 235 dst->data[2] = src.u.vec4.z; 236 dst->data[3] = src.u.vec4.w; 237 break; 238 } 239 240 case SensorType::ROTATION_VECTOR: 241 case SensorType::GEOMAGNETIC_ROTATION_VECTOR: { 242 dst->data[0] = src.u.data[0]; 243 dst->data[1] = src.u.data[1]; 244 dst->data[2] = src.u.data[2]; 245 dst->data[3] = src.u.data[3]; 246 dst->data[4] = src.u.data[4]; 247 break; 248 } 249 250 case SensorType::MAGNETIC_FIELD_UNCALIBRATED: 251 case SensorType::GYROSCOPE_UNCALIBRATED: 252 case SensorType::ACCELEROMETER_UNCALIBRATED: 253 { 254 dst->uncalibrated_gyro.x_uncalib = src.u.uncal.x; 255 dst->uncalibrated_gyro.y_uncalib = src.u.uncal.y; 256 dst->uncalibrated_gyro.z_uncalib = src.u.uncal.z; 257 dst->uncalibrated_gyro.x_bias = src.u.uncal.x_bias; 258 dst->uncalibrated_gyro.y_bias = src.u.uncal.y_bias; 259 dst->uncalibrated_gyro.z_bias = src.u.uncal.z_bias; 260 break; 261 } 262 263 case SensorType::DEVICE_ORIENTATION: 264 case SensorType::LIGHT: 265 case SensorType::PRESSURE: 266 case SensorType::TEMPERATURE: 267 case SensorType::PROXIMITY: 268 case SensorType::RELATIVE_HUMIDITY: 269 case SensorType::AMBIENT_TEMPERATURE: 270 case SensorType::SIGNIFICANT_MOTION: 271 case SensorType::STEP_DETECTOR: 272 case SensorType::TILT_DETECTOR: 273 case SensorType::WAKE_GESTURE: 274 case SensorType::GLANCE_GESTURE: 275 case SensorType::PICK_UP_GESTURE: 276 case SensorType::WRIST_TILT_GESTURE: 277 case SensorType::STATIONARY_DETECT: 278 case SensorType::MOTION_DETECT: 279 case SensorType::HEART_BEAT: 280 case SensorType::LOW_LATENCY_OFFBODY_DETECT: { 281 dst->data[0] = src.u.scalar; 282 break; 283 } 284 285 case SensorType::STEP_COUNTER: { 286 dst->u64.step_counter = src.u.stepCount; 287 break; 288 } 289 290 case SensorType::HEART_RATE: { 291 dst->heart_rate.bpm = src.u.heartRate.bpm; 292 dst->heart_rate.status = (int8_t)src.u.heartRate.status; 293 break; 294 } 295 296 case SensorType::POSE_6DOF: { // 15 floats 297 for (size_t i = 0; i < 15; ++i) { 298 dst->data[i] = src.u.pose6DOF[i]; 299 } 300 break; 301 } 302 303 case SensorType::DYNAMIC_SENSOR_META: { 304 dst->dynamic_sensor_meta.connected = src.u.dynamic.connected; 305 dst->dynamic_sensor_meta.handle = src.u.dynamic.sensorHandle; 306 dst->dynamic_sensor_meta.sensor = NULL; // to be filled in later 307 308 memcpy(dst->dynamic_sensor_meta.uuid, 309 src.u.dynamic.uuid.data(), 310 16); 311 312 break; 313 } 314 315 case SensorType::ADDITIONAL_INFO: { 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 CHECK_GE((int32_t)src.sensorType, 334 (int32_t)SensorType::DEVICE_PRIVATE_BASE); 335 336 memcpy(dst->data, src.u.data.data(), 16 * sizeof(float)); 337 break; 338 } 339 } 340 } 341 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 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