Tyler Trephan | 17857cc | 2021-11-16 21:23:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 "HalProxyAidl.h" |
| 18 | #include <aidlcommonsupport/NativeHandle.h> |
| 19 | #include <fmq/AidlMessageQueue.h> |
| 20 | #include <hidl/Status.h> |
| 21 | #include "ConvertUtils.h" |
| 22 | #include "EventMessageQueueWrapperAidl.h" |
| 23 | #include "ISensorsCallbackWrapperAidl.h" |
| 24 | #include "WakeLockMessageQueueWrapperAidl.h" |
| 25 | #include "convertV2_1.h" |
| 26 | |
| 27 | using ::aidl::android::hardware::common::fmq::MQDescriptor; |
| 28 | using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite; |
| 29 | using ::aidl::android::hardware::sensors::ISensors; |
| 30 | using ::aidl::android::hardware::sensors::ISensorsCallback; |
| 31 | using ::android::hardware::sensors::V2_1::implementation::convertToOldEvent; |
Arthur Ishiguro | eae8adf | 2022-04-11 21:28:34 +0000 | [diff] [blame] | 32 | using ::ndk::ScopedAStatus; |
Tyler Trephan | 17857cc | 2021-11-16 21:23:12 +0000 | [diff] [blame] | 33 | |
| 34 | namespace aidl { |
| 35 | namespace android { |
| 36 | namespace hardware { |
| 37 | namespace sensors { |
| 38 | namespace implementation { |
| 39 | |
Arthur Ishiguro | eae8adf | 2022-04-11 21:28:34 +0000 | [diff] [blame] | 40 | static ScopedAStatus |
| 41 | resultToAStatus(::android::hardware::sensors::V1_0::Result result) { |
| 42 | switch (result) { |
| 43 | case ::android::hardware::sensors::V1_0::Result::OK: |
| 44 | return ScopedAStatus::ok(); |
| 45 | case ::android::hardware::sensors::V1_0::Result::PERMISSION_DENIED: |
| 46 | return ScopedAStatus::fromExceptionCode(EX_SECURITY); |
| 47 | case ::android::hardware::sensors::V1_0::Result::NO_MEMORY: |
| 48 | return ScopedAStatus::fromServiceSpecificError(ISensors::ERROR_NO_MEMORY); |
| 49 | case ::android::hardware::sensors::V1_0::Result::BAD_VALUE: |
| 50 | return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 51 | case ::android::hardware::sensors::V1_0::Result::INVALID_OPERATION: |
| 52 | return ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 53 | default: |
| 54 | return ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED); |
| 55 | } |
Tyler Trephan | 17857cc | 2021-11-16 21:23:12 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static ::android::hardware::sensors::V1_0::RateLevel convertRateLevel( |
| 59 | ISensors::RateLevel rateLevel) { |
| 60 | switch (rateLevel) { |
| 61 | case ISensors::RateLevel::STOP: |
| 62 | return ::android::hardware::sensors::V1_0::RateLevel::STOP; |
| 63 | case ISensors::RateLevel::NORMAL: |
| 64 | return ::android::hardware::sensors::V1_0::RateLevel::NORMAL; |
| 65 | case ISensors::RateLevel::FAST: |
| 66 | return ::android::hardware::sensors::V1_0::RateLevel::FAST; |
| 67 | case ISensors::RateLevel::VERY_FAST: |
| 68 | return ::android::hardware::sensors::V1_0::RateLevel::VERY_FAST; |
Arthur Ishiguro | eae8adf | 2022-04-11 21:28:34 +0000 | [diff] [blame] | 69 | default: |
| 70 | assert(false); |
Tyler Trephan | 17857cc | 2021-11-16 21:23:12 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | static ::android::hardware::sensors::V1_0::OperationMode convertOperationMode( |
| 75 | ISensors::OperationMode operationMode) { |
| 76 | switch (operationMode) { |
| 77 | case ISensors::OperationMode::NORMAL: |
| 78 | return ::android::hardware::sensors::V1_0::OperationMode::NORMAL; |
| 79 | case ISensors::OperationMode::DATA_INJECTION: |
| 80 | return ::android::hardware::sensors::V1_0::OperationMode::DATA_INJECTION; |
Arthur Ishiguro | eae8adf | 2022-04-11 21:28:34 +0000 | [diff] [blame] | 81 | default: |
| 82 | assert(false); |
Tyler Trephan | 17857cc | 2021-11-16 21:23:12 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | static ::android::hardware::sensors::V1_0::SharedMemType convertSharedMemType( |
| 87 | ISensors::SharedMemInfo::SharedMemType sharedMemType) { |
| 88 | switch (sharedMemType) { |
| 89 | case ISensors::SharedMemInfo::SharedMemType::ASHMEM: |
| 90 | return ::android::hardware::sensors::V1_0::SharedMemType::ASHMEM; |
| 91 | case ISensors::SharedMemInfo::SharedMemType::GRALLOC: |
| 92 | return ::android::hardware::sensors::V1_0::SharedMemType::GRALLOC; |
Arthur Ishiguro | eae8adf | 2022-04-11 21:28:34 +0000 | [diff] [blame] | 93 | default: |
| 94 | assert(false); |
Tyler Trephan | 17857cc | 2021-11-16 21:23:12 +0000 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | static ::android::hardware::sensors::V1_0::SharedMemFormat convertSharedMemFormat( |
| 99 | ISensors::SharedMemInfo::SharedMemFormat sharedMemFormat) { |
| 100 | switch (sharedMemFormat) { |
| 101 | case ISensors::SharedMemInfo::SharedMemFormat::SENSORS_EVENT: |
| 102 | return ::android::hardware::sensors::V1_0::SharedMemFormat::SENSORS_EVENT; |
Arthur Ishiguro | eae8adf | 2022-04-11 21:28:34 +0000 | [diff] [blame] | 103 | default: |
| 104 | assert(false); |
Tyler Trephan | 17857cc | 2021-11-16 21:23:12 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | static ::android::hardware::sensors::V1_0::SharedMemInfo convertSharedMemInfo( |
| 109 | const ISensors::SharedMemInfo& sharedMemInfo) { |
| 110 | ::android::hardware::sensors::V1_0::SharedMemInfo v1SharedMemInfo; |
| 111 | v1SharedMemInfo.type = convertSharedMemType(sharedMemInfo.type); |
| 112 | v1SharedMemInfo.format = convertSharedMemFormat(sharedMemInfo.format); |
| 113 | v1SharedMemInfo.size = sharedMemInfo.size; |
| 114 | v1SharedMemInfo.memoryHandle = |
| 115 | ::android::hardware::hidl_handle(::android::makeFromAidl(sharedMemInfo.memoryHandle)); |
| 116 | return v1SharedMemInfo; |
| 117 | } |
| 118 | |
Arthur Ishiguro | eae8adf | 2022-04-11 21:28:34 +0000 | [diff] [blame] | 119 | ScopedAStatus HalProxyAidl::activate(int32_t in_sensorHandle, bool in_enabled) { |
| 120 | return resultToAStatus(HalProxy::activate(in_sensorHandle, in_enabled)); |
Tyler Trephan | 17857cc | 2021-11-16 21:23:12 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Arthur Ishiguro | eae8adf | 2022-04-11 21:28:34 +0000 | [diff] [blame] | 123 | ScopedAStatus HalProxyAidl::batch(int32_t in_sensorHandle, |
| 124 | int64_t in_samplingPeriodNs, |
| 125 | int64_t in_maxReportLatencyNs) { |
| 126 | return resultToAStatus(HalProxy::batch(in_sensorHandle, in_samplingPeriodNs, |
| 127 | in_maxReportLatencyNs)); |
Tyler Trephan | 17857cc | 2021-11-16 21:23:12 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Arthur Ishiguro | eae8adf | 2022-04-11 21:28:34 +0000 | [diff] [blame] | 130 | ScopedAStatus HalProxyAidl::configDirectReport(int32_t in_sensorHandle, |
| 131 | int32_t in_channelHandle, |
| 132 | ISensors::RateLevel in_rate, |
| 133 | int32_t *_aidl_return) { |
| 134 | ScopedAStatus status = |
| 135 | ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 136 | HalProxy::configDirectReport( |
| 137 | in_sensorHandle, in_channelHandle, convertRateLevel(in_rate), |
| 138 | [&status, _aidl_return](::android::hardware::sensors::V1_0::Result result, |
| 139 | int32_t reportToken) { |
| 140 | status = resultToAStatus(result); |
| 141 | *_aidl_return = reportToken; |
| 142 | }); |
| 143 | |
Arthur Ishiguro | eae8adf | 2022-04-11 21:28:34 +0000 | [diff] [blame] | 144 | return status; |
Tyler Trephan | 17857cc | 2021-11-16 21:23:12 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Arthur Ishiguro | eae8adf | 2022-04-11 21:28:34 +0000 | [diff] [blame] | 147 | ScopedAStatus HalProxyAidl::flush(int32_t in_sensorHandle) { |
| 148 | return resultToAStatus(HalProxy::flush(in_sensorHandle)); |
Tyler Trephan | 17857cc | 2021-11-16 21:23:12 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Arthur Ishiguro | eae8adf | 2022-04-11 21:28:34 +0000 | [diff] [blame] | 151 | ScopedAStatus HalProxyAidl::getSensorsList( |
| 152 | std::vector<::aidl::android::hardware::sensors::SensorInfo> *_aidl_return) { |
| 153 | for (const auto &sensor : HalProxy::getSensors()) { |
| 154 | _aidl_return->push_back(convertSensorInfo(sensor.second)); |
| 155 | } |
| 156 | return ScopedAStatus::ok(); |
Tyler Trephan | 17857cc | 2021-11-16 21:23:12 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Arthur Ishiguro | eae8adf | 2022-04-11 21:28:34 +0000 | [diff] [blame] | 159 | ScopedAStatus HalProxyAidl::initialize( |
| 160 | const MQDescriptor<::aidl::android::hardware::sensors::Event, |
| 161 | SynchronizedReadWrite> &in_eventQueueDescriptor, |
| 162 | const MQDescriptor<int32_t, SynchronizedReadWrite> &in_wakeLockDescriptor, |
| 163 | const std::shared_ptr<ISensorsCallback> &in_sensorsCallback) { |
| 164 | ::android::sp<::android::hardware::sensors::V2_1::implementation:: |
| 165 | ISensorsCallbackWrapperBase> |
| 166 | dynamicCallback = new ISensorsCallbackWrapperAidl(in_sensorsCallback); |
Tyler Trephan | 17857cc | 2021-11-16 21:23:12 +0000 | [diff] [blame] | 167 | |
Arthur Ishiguro | eae8adf | 2022-04-11 21:28:34 +0000 | [diff] [blame] | 168 | auto aidlEventQueue = std::make_unique<::android::AidlMessageQueue< |
| 169 | ::aidl::android::hardware::sensors::Event, SynchronizedReadWrite>>( |
| 170 | in_eventQueueDescriptor, true /* resetPointers */); |
| 171 | std::unique_ptr<::android::hardware::sensors::V2_1::implementation:: |
| 172 | EventMessageQueueWrapperBase> |
| 173 | eventQueue = |
| 174 | std::make_unique<EventMessageQueueWrapperAidl>(aidlEventQueue); |
Tyler Trephan | 17857cc | 2021-11-16 21:23:12 +0000 | [diff] [blame] | 175 | |
Arthur Ishiguro | eae8adf | 2022-04-11 21:28:34 +0000 | [diff] [blame] | 176 | auto aidlWakeLockQueue = std::make_unique< |
| 177 | ::android::AidlMessageQueue<int32_t, SynchronizedReadWrite>>( |
| 178 | in_wakeLockDescriptor, true /* resetPointers */); |
| 179 | std::unique_ptr<::android::hardware::sensors::V2_1::implementation:: |
| 180 | WakeLockMessageQueueWrapperBase> |
| 181 | wakeLockQueue = |
| 182 | std::make_unique<WakeLockMessageQueueWrapperAidl>(aidlWakeLockQueue); |
Tyler Trephan | 17857cc | 2021-11-16 21:23:12 +0000 | [diff] [blame] | 183 | |
Arthur Ishiguro | eae8adf | 2022-04-11 21:28:34 +0000 | [diff] [blame] | 184 | return resultToAStatus( |
| 185 | initializeCommon(eventQueue, wakeLockQueue, dynamicCallback)); |
Tyler Trephan | 17857cc | 2021-11-16 21:23:12 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Arthur Ishiguro | eae8adf | 2022-04-11 21:28:34 +0000 | [diff] [blame] | 188 | ScopedAStatus HalProxyAidl::injectSensorData( |
| 189 | const ::aidl::android::hardware::sensors::Event &in_event) { |
| 190 | ::android::hardware::sensors::V2_1::Event hidlEvent; |
| 191 | convertToHidlEvent(in_event, &hidlEvent); |
| 192 | return resultToAStatus( |
| 193 | HalProxy::injectSensorData(convertToOldEvent(hidlEvent))); |
Tyler Trephan | 17857cc | 2021-11-16 21:23:12 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Arthur Ishiguro | eae8adf | 2022-04-11 21:28:34 +0000 | [diff] [blame] | 196 | ScopedAStatus |
| 197 | HalProxyAidl::registerDirectChannel(const ISensors::SharedMemInfo &in_mem, |
| 198 | int32_t *_aidl_return) { |
| 199 | ScopedAStatus status = |
| 200 | ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 201 | ::android::hardware::sensors::V1_0::SharedMemInfo sharedMemInfo = |
| 202 | convertSharedMemInfo(in_mem); |
Tyler Trephan | 17857cc | 2021-11-16 21:23:12 +0000 | [diff] [blame] | 203 | |
Arthur Ishiguro | eae8adf | 2022-04-11 21:28:34 +0000 | [diff] [blame] | 204 | HalProxy::registerDirectChannel( |
| 205 | sharedMemInfo, |
| 206 | [&status, _aidl_return](::android::hardware::sensors::V1_0::Result result, |
| 207 | int32_t reportToken) { |
| 208 | status = resultToAStatus(result); |
| 209 | *_aidl_return = reportToken; |
| 210 | }); |
Tyler Trephan | 17857cc | 2021-11-16 21:23:12 +0000 | [diff] [blame] | 211 | |
Arthur Ishiguro | eae8adf | 2022-04-11 21:28:34 +0000 | [diff] [blame] | 212 | native_handle_delete(const_cast<native_handle_t *>( |
| 213 | sharedMemInfo.memoryHandle.getNativeHandle())); |
| 214 | |
Arthur Ishiguro | eae8adf | 2022-04-11 21:28:34 +0000 | [diff] [blame] | 215 | return status; |
Tyler Trephan | 17857cc | 2021-11-16 21:23:12 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Arthur Ishiguro | eae8adf | 2022-04-11 21:28:34 +0000 | [diff] [blame] | 218 | ScopedAStatus HalProxyAidl::setOperationMode( |
| 219 | ::aidl::android::hardware::sensors::ISensors::OperationMode in_mode) { |
| 220 | return resultToAStatus( |
| 221 | HalProxy::setOperationMode(convertOperationMode(in_mode))); |
Tyler Trephan | 17857cc | 2021-11-16 21:23:12 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Arthur Ishiguro | eae8adf | 2022-04-11 21:28:34 +0000 | [diff] [blame] | 224 | ScopedAStatus HalProxyAidl::unregisterDirectChannel(int32_t in_channelHandle) { |
| 225 | return resultToAStatus(HalProxy::unregisterDirectChannel(in_channelHandle)); |
Tyler Trephan | 17857cc | 2021-11-16 21:23:12 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Arthur Ishiguro | 5fc0760 | 2022-04-11 15:35:05 +0000 | [diff] [blame] | 228 | binder_status_t HalProxyAidl::dump(int fd, const char ** /* args */, |
| 229 | uint32_t /* numArgs */) { |
| 230 | native_handle_t *nativeHandle = |
| 231 | native_handle_create(1 /* numFds */, 0 /* numInts */); |
| 232 | nativeHandle->data[0] = fd; |
| 233 | |
| 234 | HalProxy::debug(nativeHandle, {} /* args */); |
| 235 | |
| 236 | native_handle_delete(nativeHandle); |
| 237 | return STATUS_OK; |
| 238 | } |
| 239 | |
Tyler Trephan | 17857cc | 2021-11-16 21:23:12 +0000 | [diff] [blame] | 240 | } // namespace implementation |
| 241 | } // namespace sensors |
| 242 | } // namespace hardware |
| 243 | } // namespace android |
| 244 | } // namespace aidl |