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 | |
| 80 | inline void convertFromSensorEvent(const sensors_event_t& src, V2_1::Event* dst) { |
| 81 | switch ((SensorType)src.type) { |
| 82 | case SensorType::HINGE_ANGLE: |
| 83 | // Only fill in values for hinge angle as other sensors |
| 84 | // will have it filled in by legacy code. |
| 85 | *dst = { |
| 86 | .timestamp = src.timestamp, |
| 87 | .sensorHandle = src.sensor, |
| 88 | .sensorType = (SensorType)src.type, |
| 89 | }; |
| 90 | dst->u.scalar = src.data[0]; |
| 91 | break; |
| 92 | default: |
| 93 | V1_0::implementation::convertFromSensorEvent(src, convertToOldEvent(dst)); |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | inline void convertToSensorEvent(const V2_1::Event& src, sensors_event_t* dst) { |
| 99 | switch (src.sensorType) { |
| 100 | case SensorType::HINGE_ANGLE: |
| 101 | // Only fill in values for hinge angle as other sensors |
| 102 | // will have it filled in by legacy code. |
| 103 | *dst = {.version = sizeof(sensors_event_t), |
| 104 | .sensor = src.sensorHandle, |
| 105 | .type = (int32_t)src.sensorType, |
| 106 | .reserved0 = 0, |
| 107 | .timestamp = src.timestamp}; |
| 108 | dst->data[0] = src.u.scalar; |
| 109 | break; |
| 110 | default: |
| 111 | V1_0::implementation::convertToSensorEvent(convertToOldEvent(src), dst); |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | } // namespace implementation |
| 117 | } // namespace V2_1 |
| 118 | } // namespace sensors |
| 119 | } // namespace hardware |
| 120 | } // namespace android |
| 121 | |
| 122 | #endif // ANDROID_HARDWARE_SENSORS_V2_1_CONVERT_H |