blob: e6bcdada5241f4d0e2226fe72c97c596e47af2d0 [file] [log] [blame]
Tyler Trephan17857cc2021-11-16 21:23:12 +00001/*
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
27using ::aidl::android::hardware::common::fmq::MQDescriptor;
28using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite;
29using ::aidl::android::hardware::sensors::ISensors;
30using ::aidl::android::hardware::sensors::ISensorsCallback;
31using ::android::hardware::sensors::V2_1::implementation::convertToOldEvent;
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +000032using ::ndk::ScopedAStatus;
Tyler Trephan17857cc2021-11-16 21:23:12 +000033
34namespace aidl {
35namespace android {
36namespace hardware {
37namespace sensors {
38namespace implementation {
39
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +000040static ScopedAStatus
41resultToAStatus(::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 Trephan17857cc2021-11-16 21:23:12 +000056}
57
58static ::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 Ishiguroeae8adf2022-04-11 21:28:34 +000069 default:
70 assert(false);
Tyler Trephan17857cc2021-11-16 21:23:12 +000071 }
72}
73
74static ::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 Ishiguroeae8adf2022-04-11 21:28:34 +000081 default:
82 assert(false);
Tyler Trephan17857cc2021-11-16 21:23:12 +000083 }
84}
85
86static ::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 Ishiguroeae8adf2022-04-11 21:28:34 +000093 default:
94 assert(false);
Tyler Trephan17857cc2021-11-16 21:23:12 +000095 }
96}
97
98static ::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 Ishiguroeae8adf2022-04-11 21:28:34 +0000103 default:
104 assert(false);
Tyler Trephan17857cc2021-11-16 21:23:12 +0000105 }
106}
107
108static ::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 Ishiguroeae8adf2022-04-11 21:28:34 +0000119ScopedAStatus HalProxyAidl::activate(int32_t in_sensorHandle, bool in_enabled) {
120 return resultToAStatus(HalProxy::activate(in_sensorHandle, in_enabled));
Tyler Trephan17857cc2021-11-16 21:23:12 +0000121}
122
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000123ScopedAStatus 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 Trephan17857cc2021-11-16 21:23:12 +0000128}
129
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000130ScopedAStatus 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 Ishiguroeae8adf2022-04-11 21:28:34 +0000144 return status;
Tyler Trephan17857cc2021-11-16 21:23:12 +0000145}
146
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000147ScopedAStatus HalProxyAidl::flush(int32_t in_sensorHandle) {
148 return resultToAStatus(HalProxy::flush(in_sensorHandle));
Tyler Trephan17857cc2021-11-16 21:23:12 +0000149}
150
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000151ScopedAStatus 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 Trephan17857cc2021-11-16 21:23:12 +0000157}
158
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000159ScopedAStatus 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 Trephan17857cc2021-11-16 21:23:12 +0000167
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000168 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 Trephan17857cc2021-11-16 21:23:12 +0000175
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000176 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 Trephan17857cc2021-11-16 21:23:12 +0000183
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000184 return resultToAStatus(
185 initializeCommon(eventQueue, wakeLockQueue, dynamicCallback));
Tyler Trephan17857cc2021-11-16 21:23:12 +0000186}
187
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000188ScopedAStatus 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 Trephan17857cc2021-11-16 21:23:12 +0000194}
195
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000196ScopedAStatus
197HalProxyAidl::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 Trephan17857cc2021-11-16 21:23:12 +0000203
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000204 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 Trephan17857cc2021-11-16 21:23:12 +0000211
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000212 native_handle_delete(const_cast<native_handle_t *>(
213 sharedMemInfo.memoryHandle.getNativeHandle()));
214
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000215 return status;
Tyler Trephan17857cc2021-11-16 21:23:12 +0000216}
217
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000218ScopedAStatus HalProxyAidl::setOperationMode(
219 ::aidl::android::hardware::sensors::ISensors::OperationMode in_mode) {
220 return resultToAStatus(
221 HalProxy::setOperationMode(convertOperationMode(in_mode)));
Tyler Trephan17857cc2021-11-16 21:23:12 +0000222}
223
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000224ScopedAStatus HalProxyAidl::unregisterDirectChannel(int32_t in_channelHandle) {
225 return resultToAStatus(HalProxy::unregisterDirectChannel(in_channelHandle));
Tyler Trephan17857cc2021-11-16 21:23:12 +0000226}
227
Arthur Ishiguro5fc07602022-04-11 15:35:05 +0000228binder_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 Trephan17857cc2021-11-16 21:23:12 +0000240} // namespace implementation
241} // namespace sensors
242} // namespace hardware
243} // namespace android
244} // namespace aidl