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