blob: dbef0305f921e2e1e1ada8335f1bb5e87fea458b [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>
Chia-Ching Yu5dcb66f2023-02-01 16:48:37 +080020#include <hidl/HidlSupport.h>
Tyler Trephan17857cc2021-11-16 21:23:12 +000021#include <hidl/Status.h>
22#include "ConvertUtils.h"
23#include "EventMessageQueueWrapperAidl.h"
24#include "ISensorsCallbackWrapperAidl.h"
25#include "WakeLockMessageQueueWrapperAidl.h"
26#include "convertV2_1.h"
27
28using ::aidl::android::hardware::common::fmq::MQDescriptor;
29using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite;
30using ::aidl::android::hardware::sensors::ISensors;
31using ::aidl::android::hardware::sensors::ISensorsCallback;
Chia-Ching Yu5dcb66f2023-02-01 16:48:37 +080032using ::android::hardware::hidl_string;
33using ::android::hardware::hidl_vec;
Tyler Trephan17857cc2021-11-16 21:23:12 +000034using ::android::hardware::sensors::V2_1::implementation::convertToOldEvent;
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +000035using ::ndk::ScopedAStatus;
Tyler Trephan17857cc2021-11-16 21:23:12 +000036
37namespace aidl {
38namespace android {
39namespace hardware {
40namespace sensors {
41namespace implementation {
42
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +000043static ScopedAStatus
44resultToAStatus(::android::hardware::sensors::V1_0::Result result) {
45 switch (result) {
46 case ::android::hardware::sensors::V1_0::Result::OK:
47 return ScopedAStatus::ok();
48 case ::android::hardware::sensors::V1_0::Result::PERMISSION_DENIED:
49 return ScopedAStatus::fromExceptionCode(EX_SECURITY);
50 case ::android::hardware::sensors::V1_0::Result::NO_MEMORY:
51 return ScopedAStatus::fromServiceSpecificError(ISensors::ERROR_NO_MEMORY);
52 case ::android::hardware::sensors::V1_0::Result::BAD_VALUE:
53 return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
54 case ::android::hardware::sensors::V1_0::Result::INVALID_OPERATION:
55 return ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
56 default:
57 return ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED);
58 }
Tyler Trephan17857cc2021-11-16 21:23:12 +000059}
60
61static ::android::hardware::sensors::V1_0::RateLevel convertRateLevel(
62 ISensors::RateLevel rateLevel) {
63 switch (rateLevel) {
64 case ISensors::RateLevel::STOP:
65 return ::android::hardware::sensors::V1_0::RateLevel::STOP;
66 case ISensors::RateLevel::NORMAL:
67 return ::android::hardware::sensors::V1_0::RateLevel::NORMAL;
68 case ISensors::RateLevel::FAST:
69 return ::android::hardware::sensors::V1_0::RateLevel::FAST;
70 case ISensors::RateLevel::VERY_FAST:
71 return ::android::hardware::sensors::V1_0::RateLevel::VERY_FAST;
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +000072 default:
73 assert(false);
Tyler Trephan17857cc2021-11-16 21:23:12 +000074 }
75}
76
77static ::android::hardware::sensors::V1_0::OperationMode convertOperationMode(
78 ISensors::OperationMode operationMode) {
79 switch (operationMode) {
80 case ISensors::OperationMode::NORMAL:
81 return ::android::hardware::sensors::V1_0::OperationMode::NORMAL;
82 case ISensors::OperationMode::DATA_INJECTION:
83 return ::android::hardware::sensors::V1_0::OperationMode::DATA_INJECTION;
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +000084 default:
85 assert(false);
Tyler Trephan17857cc2021-11-16 21:23:12 +000086 }
87}
88
89static ::android::hardware::sensors::V1_0::SharedMemType convertSharedMemType(
90 ISensors::SharedMemInfo::SharedMemType sharedMemType) {
91 switch (sharedMemType) {
92 case ISensors::SharedMemInfo::SharedMemType::ASHMEM:
93 return ::android::hardware::sensors::V1_0::SharedMemType::ASHMEM;
94 case ISensors::SharedMemInfo::SharedMemType::GRALLOC:
95 return ::android::hardware::sensors::V1_0::SharedMemType::GRALLOC;
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +000096 default:
97 assert(false);
Tyler Trephan17857cc2021-11-16 21:23:12 +000098 }
99}
100
101static ::android::hardware::sensors::V1_0::SharedMemFormat convertSharedMemFormat(
102 ISensors::SharedMemInfo::SharedMemFormat sharedMemFormat) {
103 switch (sharedMemFormat) {
104 case ISensors::SharedMemInfo::SharedMemFormat::SENSORS_EVENT:
105 return ::android::hardware::sensors::V1_0::SharedMemFormat::SENSORS_EVENT;
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000106 default:
107 assert(false);
Tyler Trephan17857cc2021-11-16 21:23:12 +0000108 }
109}
110
111static ::android::hardware::sensors::V1_0::SharedMemInfo convertSharedMemInfo(
112 const ISensors::SharedMemInfo& sharedMemInfo) {
113 ::android::hardware::sensors::V1_0::SharedMemInfo v1SharedMemInfo;
114 v1SharedMemInfo.type = convertSharedMemType(sharedMemInfo.type);
115 v1SharedMemInfo.format = convertSharedMemFormat(sharedMemInfo.format);
116 v1SharedMemInfo.size = sharedMemInfo.size;
117 v1SharedMemInfo.memoryHandle =
118 ::android::hardware::hidl_handle(::android::makeFromAidl(sharedMemInfo.memoryHandle));
119 return v1SharedMemInfo;
120}
121
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000122ScopedAStatus HalProxyAidl::activate(int32_t in_sensorHandle, bool in_enabled) {
123 return resultToAStatus(HalProxy::activate(in_sensorHandle, in_enabled));
Tyler Trephan17857cc2021-11-16 21:23:12 +0000124}
125
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000126ScopedAStatus HalProxyAidl::batch(int32_t in_sensorHandle,
127 int64_t in_samplingPeriodNs,
128 int64_t in_maxReportLatencyNs) {
129 return resultToAStatus(HalProxy::batch(in_sensorHandle, in_samplingPeriodNs,
130 in_maxReportLatencyNs));
Tyler Trephan17857cc2021-11-16 21:23:12 +0000131}
132
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000133ScopedAStatus HalProxyAidl::configDirectReport(int32_t in_sensorHandle,
134 int32_t in_channelHandle,
135 ISensors::RateLevel in_rate,
136 int32_t *_aidl_return) {
137 ScopedAStatus status =
138 ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
139 HalProxy::configDirectReport(
140 in_sensorHandle, in_channelHandle, convertRateLevel(in_rate),
141 [&status, _aidl_return](::android::hardware::sensors::V1_0::Result result,
142 int32_t reportToken) {
143 status = resultToAStatus(result);
144 *_aidl_return = reportToken;
145 });
146
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000147 return status;
Tyler Trephan17857cc2021-11-16 21:23:12 +0000148}
149
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000150ScopedAStatus HalProxyAidl::flush(int32_t in_sensorHandle) {
151 return resultToAStatus(HalProxy::flush(in_sensorHandle));
Tyler Trephan17857cc2021-11-16 21:23:12 +0000152}
153
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000154ScopedAStatus HalProxyAidl::getSensorsList(
155 std::vector<::aidl::android::hardware::sensors::SensorInfo> *_aidl_return) {
156 for (const auto &sensor : HalProxy::getSensors()) {
157 _aidl_return->push_back(convertSensorInfo(sensor.second));
158 }
159 return ScopedAStatus::ok();
Tyler Trephan17857cc2021-11-16 21:23:12 +0000160}
161
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000162ScopedAStatus HalProxyAidl::initialize(
163 const MQDescriptor<::aidl::android::hardware::sensors::Event,
164 SynchronizedReadWrite> &in_eventQueueDescriptor,
165 const MQDescriptor<int32_t, SynchronizedReadWrite> &in_wakeLockDescriptor,
166 const std::shared_ptr<ISensorsCallback> &in_sensorsCallback) {
167 ::android::sp<::android::hardware::sensors::V2_1::implementation::
168 ISensorsCallbackWrapperBase>
169 dynamicCallback = new ISensorsCallbackWrapperAidl(in_sensorsCallback);
Tyler Trephan17857cc2021-11-16 21:23:12 +0000170
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000171 auto aidlEventQueue = std::make_unique<::android::AidlMessageQueue<
172 ::aidl::android::hardware::sensors::Event, SynchronizedReadWrite>>(
173 in_eventQueueDescriptor, true /* resetPointers */);
174 std::unique_ptr<::android::hardware::sensors::V2_1::implementation::
175 EventMessageQueueWrapperBase>
176 eventQueue =
177 std::make_unique<EventMessageQueueWrapperAidl>(aidlEventQueue);
Tyler Trephan17857cc2021-11-16 21:23:12 +0000178
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000179 auto aidlWakeLockQueue = std::make_unique<
180 ::android::AidlMessageQueue<int32_t, SynchronizedReadWrite>>(
181 in_wakeLockDescriptor, true /* resetPointers */);
182 std::unique_ptr<::android::hardware::sensors::V2_1::implementation::
183 WakeLockMessageQueueWrapperBase>
184 wakeLockQueue =
185 std::make_unique<WakeLockMessageQueueWrapperAidl>(aidlWakeLockQueue);
Tyler Trephan17857cc2021-11-16 21:23:12 +0000186
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000187 return resultToAStatus(
188 initializeCommon(eventQueue, wakeLockQueue, dynamicCallback));
Tyler Trephan17857cc2021-11-16 21:23:12 +0000189}
190
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000191ScopedAStatus HalProxyAidl::injectSensorData(
192 const ::aidl::android::hardware::sensors::Event &in_event) {
193 ::android::hardware::sensors::V2_1::Event hidlEvent;
194 convertToHidlEvent(in_event, &hidlEvent);
195 return resultToAStatus(
196 HalProxy::injectSensorData(convertToOldEvent(hidlEvent)));
Tyler Trephan17857cc2021-11-16 21:23:12 +0000197}
198
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000199ScopedAStatus
200HalProxyAidl::registerDirectChannel(const ISensors::SharedMemInfo &in_mem,
201 int32_t *_aidl_return) {
202 ScopedAStatus status =
203 ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
204 ::android::hardware::sensors::V1_0::SharedMemInfo sharedMemInfo =
205 convertSharedMemInfo(in_mem);
Tyler Trephan17857cc2021-11-16 21:23:12 +0000206
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000207 HalProxy::registerDirectChannel(
208 sharedMemInfo,
209 [&status, _aidl_return](::android::hardware::sensors::V1_0::Result result,
210 int32_t reportToken) {
211 status = resultToAStatus(result);
212 *_aidl_return = reportToken;
213 });
Tyler Trephan17857cc2021-11-16 21:23:12 +0000214
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000215 native_handle_delete(const_cast<native_handle_t *>(
216 sharedMemInfo.memoryHandle.getNativeHandle()));
217
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000218 return status;
Tyler Trephan17857cc2021-11-16 21:23:12 +0000219}
220
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000221ScopedAStatus HalProxyAidl::setOperationMode(
222 ::aidl::android::hardware::sensors::ISensors::OperationMode in_mode) {
223 return resultToAStatus(
224 HalProxy::setOperationMode(convertOperationMode(in_mode)));
Tyler Trephan17857cc2021-11-16 21:23:12 +0000225}
226
Arthur Ishiguroeae8adf2022-04-11 21:28:34 +0000227ScopedAStatus HalProxyAidl::unregisterDirectChannel(int32_t in_channelHandle) {
228 return resultToAStatus(HalProxy::unregisterDirectChannel(in_channelHandle));
Tyler Trephan17857cc2021-11-16 21:23:12 +0000229}
230
Chia-Ching Yu5dcb66f2023-02-01 16:48:37 +0800231binder_status_t HalProxyAidl::dump(int fd, const char ** args,
232 uint32_t numArgs) {
Arthur Ishiguro5fc07602022-04-11 15:35:05 +0000233 native_handle_t *nativeHandle =
234 native_handle_create(1 /* numFds */, 0 /* numInts */);
235 nativeHandle->data[0] = fd;
236
Chia-Ching Yu5dcb66f2023-02-01 16:48:37 +0800237 hidl_vec<hidl_string> hidl_args;
238 hidl_args.resize(numArgs);
239 for (size_t i = 0; i < numArgs; ++i) {
240 hidl_args[i] = args[i];
241 }
242 HalProxy::debug(nativeHandle, hidl_args);
Arthur Ishiguro5fc07602022-04-11 15:35:05 +0000243
244 native_handle_delete(nativeHandle);
245 return STATUS_OK;
246}
247
Tyler Trephan17857cc2021-11-16 21:23:12 +0000248} // namespace implementation
249} // namespace sensors
250} // namespace hardware
251} // namespace android
Chia-Ching Yu5dcb66f2023-02-01 16:48:37 +0800252} // namespace aidl