Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | #ifndef ANDROID_HARDWARE_SENSORS_V2_1_CONVERT_H |
| 18 | #define ANDROID_HARDWARE_SENSORS_V2_1_CONVERT_H |
| 19 | |
| 20 | #include <android/hardware/sensors/2.1/types.h> |
| 21 | #include <hardware/sensors.h> |
| 22 | #include <sensors/convert.h> |
| 23 | |
| 24 | namespace android { |
| 25 | namespace hardware { |
| 26 | namespace sensors { |
| 27 | namespace V2_1 { |
| 28 | namespace implementation { |
| 29 | |
| 30 | static_assert(sizeof(V1_0::Event) == sizeof(V2_1::Event), |
| 31 | "New and old Event types must have the same size"); |
| 32 | static_assert(sizeof(V1_0::SensorInfo) == sizeof(V2_1::SensorInfo), |
| 33 | "New and old SensorInfo types must have the same size"); |
| 34 | |
| 35 | // The following conversion methods are safe as the only difference between |
| 36 | // V1_0 and V2_1 for these types is an added enum value to SensorType which doesn't |
| 37 | // change the memory layout of the types. |
| 38 | inline const V1_0::Event& convertToOldEvent(const V2_1::Event& event) { |
| 39 | return reinterpret_cast<const V1_0::Event&>(event); |
| 40 | } |
| 41 | |
| 42 | inline const std::vector<V1_0::Event>& convertToOldEvents(const std::vector<V2_1::Event>& events) { |
| 43 | return reinterpret_cast<const std::vector<V1_0::Event>&>(events); |
| 44 | } |
| 45 | |
| 46 | inline V1_0::Event* convertToOldEvent(V2_1::Event* event) { |
| 47 | return reinterpret_cast<V1_0::Event*>(event); |
| 48 | } |
| 49 | |
| 50 | inline const V2_1::SensorInfo& convertToNewSensorInfo(const V1_0::SensorInfo& info) { |
| 51 | return reinterpret_cast<const V2_1::SensorInfo&>(info); |
| 52 | } |
| 53 | |
| 54 | inline const V1_0::SensorInfo& convertToOldSensorInfo(const V2_1::SensorInfo& info) { |
| 55 | return reinterpret_cast<const V1_0::SensorInfo&>(info); |
| 56 | } |
| 57 | |
| 58 | inline const V2_1::Event& convertToNewEvent(const V1_0::Event& event) { |
| 59 | return reinterpret_cast<const V2_1::Event&>(event); |
| 60 | } |
| 61 | |
| 62 | inline const std::vector<V2_1::Event>& convertToNewEvents(const std::vector<V1_0::Event>& events) { |
| 63 | return reinterpret_cast<const std::vector<V2_1::Event>&>(events); |
| 64 | } |
| 65 | |
| 66 | inline const hidl_vec<V2_1::Event>& convertToNewEvents(const hidl_vec<V1_0::Event>& events) { |
| 67 | return reinterpret_cast<const hidl_vec<V2_1::Event>&>(events); |
| 68 | } |
| 69 | |
| 70 | inline const hidl_vec<V2_1::SensorInfo>& convertToNewSensorInfos( |
| 71 | const hidl_vec<V1_0::SensorInfo>& infos) { |
| 72 | return reinterpret_cast<const hidl_vec<V2_1::SensorInfo>&>(infos); |
| 73 | } |
| 74 | |
| 75 | inline const hidl_vec<V1_0::SensorInfo>& convertToOldSensorInfos( |
| 76 | const hidl_vec<V2_1::SensorInfo>& infos) { |
| 77 | return reinterpret_cast<const hidl_vec<V1_0::SensorInfo>&>(infos); |
| 78 | } |
| 79 | |
Arthur Ishiguro | b0b47e3 | 2021-11-16 00:12:47 +0000 | [diff] [blame] | 80 | inline void convertToSensor(const V2_1::SensorInfo& src, sensor_t* dst) { |
| 81 | dst->name = strdup(src.name.c_str()); |
| 82 | dst->vendor = strdup(src.vendor.c_str()); |
| 83 | dst->version = src.version; |
| 84 | dst->handle = src.sensorHandle; |
| 85 | dst->type = (int)src.type; |
| 86 | dst->maxRange = src.maxRange; |
| 87 | dst->resolution = src.resolution; |
| 88 | dst->power = src.power; |
| 89 | dst->minDelay = src.minDelay; |
| 90 | dst->fifoReservedEventCount = src.fifoReservedEventCount; |
| 91 | dst->fifoMaxEventCount = src.fifoMaxEventCount; |
| 92 | dst->stringType = strdup(src.typeAsString.c_str()); |
| 93 | dst->requiredPermission = strdup(src.requiredPermission.c_str()); |
| 94 | dst->maxDelay = src.maxDelay; |
| 95 | dst->flags = src.flags; |
| 96 | dst->reserved[0] = dst->reserved[1] = 0; |
| 97 | } |
| 98 | |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 99 | inline void convertFromSensorEvent(const sensors_event_t& src, V2_1::Event* dst) { |
| 100 | switch ((SensorType)src.type) { |
| 101 | case SensorType::HINGE_ANGLE: |
| 102 | // Only fill in values for hinge angle as other sensors |
| 103 | // will have it filled in by legacy code. |
| 104 | *dst = { |
| 105 | .timestamp = src.timestamp, |
| 106 | .sensorHandle = src.sensor, |
| 107 | .sensorType = (SensorType)src.type, |
| 108 | }; |
| 109 | dst->u.scalar = src.data[0]; |
| 110 | break; |
| 111 | default: |
| 112 | V1_0::implementation::convertFromSensorEvent(src, convertToOldEvent(dst)); |
| 113 | break; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | inline void convertToSensorEvent(const V2_1::Event& src, sensors_event_t* dst) { |
| 118 | switch (src.sensorType) { |
| 119 | case SensorType::HINGE_ANGLE: |
| 120 | // Only fill in values for hinge angle as other sensors |
| 121 | // will have it filled in by legacy code. |
| 122 | *dst = {.version = sizeof(sensors_event_t), |
| 123 | .sensor = src.sensorHandle, |
| 124 | .type = (int32_t)src.sensorType, |
| 125 | .reserved0 = 0, |
| 126 | .timestamp = src.timestamp}; |
| 127 | dst->data[0] = src.u.scalar; |
| 128 | break; |
| 129 | default: |
| 130 | V1_0::implementation::convertToSensorEvent(convertToOldEvent(src), dst); |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | } // namespace implementation |
| 136 | } // namespace V2_1 |
| 137 | } // namespace sensors |
| 138 | } // namespace hardware |
| 139 | } // namespace android |
| 140 | |
| 141 | #endif // ANDROID_HARDWARE_SENSORS_V2_1_CONVERT_H |