diff --git a/sensors/1.0/default/Android.bp b/sensors/1.0/default/Android.bp index 2e4e1b015c..bb31050d34 100644 --- a/sensors/1.0/default/Android.bp +++ b/sensors/1.0/default/Android.bp @@ -44,6 +44,12 @@ cc_library_static { "libhidlbase", "android.hardware.sensors@1.0", ], + whole_static_libs: [ + "sensors_common_convert", + ], + export_static_lib_headers: [ + "sensors_common_convert", + ], local_include_dirs: ["include/sensors"], export_shared_lib_headers: [ "libhardware", diff --git a/sensors/1.0/default/convert.cpp b/sensors/1.0/default/convert.cpp index 43ee3276b9..ae71a9712e 100644 --- a/sensors/1.0/default/convert.cpp +++ b/sensors/1.0/default/convert.cpp @@ -196,6 +196,11 @@ void convertFromSensorEvent(const sensors_event_t &src, Event *dst) { } } +void convertFromASensorEvent(const ASensorEvent& src, Event* dst) { + convertFromSensorEvent( + android::hardware::sensors::implementation::common::convertASensorEvent(src), dst); +} + void convertToSensorEvent(const Event &src, sensors_event_t *dst) { *dst = {.version = sizeof(sensors_event_t), .sensor = src.sensorHandle, diff --git a/sensors/1.0/default/include/sensors/convert.h b/sensors/1.0/default/include/sensors/convert.h index c3a0125d51..ae773df475 100644 --- a/sensors/1.0/default/include/sensors/convert.h +++ b/sensors/1.0/default/include/sensors/convert.h @@ -20,6 +20,7 @@ #include #include +#include namespace android { namespace hardware { @@ -31,6 +32,7 @@ void convertFromSensor(const sensor_t &src, SensorInfo *dst); void convertToSensor(const SensorInfo &src, sensor_t *dst); void convertFromSensorEvent(const sensors_event_t &src, Event *dst); +void convertFromASensorEvent(const ASensorEvent& src, Event* dst); void convertToSensorEvent(const Event &src, sensors_event_t *dst); bool convertFromSharedMemInfo(const SharedMemInfo& memIn, sensors_direct_mem_t *memOut); diff --git a/sensors/aidl/convert/Android.bp b/sensors/aidl/convert/Android.bp index d47de8ec6d..0b31597478 100644 --- a/sensors/aidl/convert/Android.bp +++ b/sensors/aidl/convert/Android.bp @@ -37,6 +37,12 @@ cc_library_static { "libutils", "android.hardware.sensors-V1-ndk", ], + whole_static_libs: [ + "sensors_common_convert", + ], + export_static_lib_headers: [ + "sensors_common_convert", + ], local_include_dirs: ["include/aidl/sensors"], export_shared_lib_headers: [ "libhardware", diff --git a/sensors/aidl/convert/convert.cpp b/sensors/aidl/convert/convert.cpp index 415f4352f5..abd4d55039 100644 --- a/sensors/aidl/convert/convert.cpp +++ b/sensors/aidl/convert/convert.cpp @@ -490,6 +490,10 @@ void convertFromSensorEvent(const sensors_event_t& src, Event* dst) { } } +void convertFromASensorEvent(const ASensorEvent& src, Event* dst) { + convertFromSensorEvent(common::convertASensorEvent(src), dst); +} + } // namespace implementation } // namespace sensors } // namespace hardware diff --git a/sensors/aidl/convert/include/aidl/sensors/convert.h b/sensors/aidl/convert/include/aidl/sensors/convert.h index 702b2266ba..44504fef9c 100644 --- a/sensors/aidl/convert/include/aidl/sensors/convert.h +++ b/sensors/aidl/convert/include/aidl/sensors/convert.h @@ -18,6 +18,7 @@ #include #include +#include namespace android { namespace hardware { @@ -29,6 +30,7 @@ void convertToSensor(const aidl::android::hardware::sensors::SensorInfo& src, se void convertToSensorEvent(const aidl::android::hardware::sensors::Event& src, sensors_event_t* dst); void convertFromSensorEvent(const sensors_event_t& src, aidl::android::hardware::sensors::Event* dst); +void convertFromASensorEvent(const ASensorEvent& src, aidl::android::hardware::sensors::Event* dst); } // namespace implementation } // namespace sensors diff --git a/sensors/common/convert/Android.bp b/sensors/common/convert/Android.bp new file mode 100644 index 0000000000..230665e71f --- /dev/null +++ b/sensors/common/convert/Android.bp @@ -0,0 +1,42 @@ +// Copyright (C) 2022 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package { + default_applicable_licenses: ["Android-Apache-2.0"], +} + +cc_library { + name: "sensors_common_convert", + srcs: [ + "convert.cpp", + ], + vendor_available: true, + host_supported: true, + local_include_dirs: ["include"], + cflags: [ + "-Wall", + "-Werror", + ], + shared_libs: [ + "libhardware", + ], + header_libs: [ + "libandroid_sensor_headers", + ], + + export_include_dirs: ["include"], + export_header_lib_headers: [ + "libandroid_sensor_headers", + ], +} diff --git a/sensors/common/convert/convert.cpp b/sensors/common/convert/convert.cpp new file mode 100644 index 0000000000..27de32c26f --- /dev/null +++ b/sensors/common/convert/convert.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +namespace android { +namespace hardware { +namespace sensors { +namespace implementation { +namespace common { + +sensors_event_t convertASensorEvent(const ASensorEvent& src) { + // Attempt to ensure these types are compatible. + static_assert(sizeof(sensors_event_t) == sizeof(ASensorEvent)); + static_assert(offsetof(sensors_event_t, timestamp) == offsetof(ASensorEvent, timestamp)); + static_assert(offsetof(sensors_event_t, flags) == offsetof(ASensorEvent, flags)); + + // TODO(b/259711109) Follow up work to handle this in a safer way. + return *reinterpret_cast(&src); +} + +} // namespace common +} // namespace implementation +} // namespace sensors +} // namespace hardware +} // namespace android diff --git a/sensors/common/convert/include/sensors/common_convert.h b/sensors/common/convert/include/sensors/common_convert.h new file mode 100644 index 0000000000..a281369021 --- /dev/null +++ b/sensors/common/convert/include/sensors/common_convert.h @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include + +namespace android { +namespace hardware { +namespace sensors { +namespace implementation { +namespace common { + +sensors_event_t convertASensorEvent(const ASensorEvent& aEvent); + +} // namespace common +} // namespace implementation +} // namespace sensors +} // namespace hardware +} // namespace android