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/HidlSupport.h>
21 #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 
28 using ::aidl::android::hardware::common::fmq::MQDescriptor;
29 using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite;
30 using ::aidl::android::hardware::sensors::ISensors;
31 using ::aidl::android::hardware::sensors::ISensorsCallback;
32 using ::android::hardware::hidl_string;
33 using ::android::hardware::hidl_vec;
34 using ::android::hardware::sensors::V2_1::implementation::convertToOldEvent;
35 using ::ndk::ScopedAStatus;
36 
37 namespace aidl {
38 namespace android {
39 namespace hardware {
40 namespace sensors {
41 namespace implementation {
42 
43 static ScopedAStatus
resultToAStatus(::android::hardware::sensors::V1_0::Result result)44 resultToAStatus(::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   }
59 }
60 
convertRateLevel(ISensors::RateLevel rateLevel)61 static ::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;
72         default:
73           assert(false);
74     }
75 }
76 
convertOperationMode(ISensors::OperationMode operationMode)77 static ::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;
84         default:
85           assert(false);
86     }
87 }
88 
convertSharedMemType(ISensors::SharedMemInfo::SharedMemType sharedMemType)89 static ::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;
96         default:
97           assert(false);
98     }
99 }
100 
convertSharedMemFormat(ISensors::SharedMemInfo::SharedMemFormat sharedMemFormat)101 static ::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;
106         default:
107           assert(false);
108     }
109 }
110 
convertSharedMemInfo(const ISensors::SharedMemInfo & sharedMemInfo)111 static ::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 
activate(int32_t in_sensorHandle,bool in_enabled)122 ScopedAStatus HalProxyAidl::activate(int32_t in_sensorHandle, bool in_enabled) {
123   return resultToAStatus(HalProxy::activate(in_sensorHandle, in_enabled));
124 }
125 
batch(int32_t in_sensorHandle,int64_t in_samplingPeriodNs,int64_t in_maxReportLatencyNs)126 ScopedAStatus 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));
131 }
132 
configDirectReport(int32_t in_sensorHandle,int32_t in_channelHandle,ISensors::RateLevel in_rate,int32_t * _aidl_return)133 ScopedAStatus 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 
147   return status;
148 }
149 
flush(int32_t in_sensorHandle)150 ScopedAStatus HalProxyAidl::flush(int32_t in_sensorHandle) {
151   return resultToAStatus(HalProxy::flush(in_sensorHandle));
152 }
153 
getSensorsList(std::vector<::aidl::android::hardware::sensors::SensorInfo> * _aidl_return)154 ScopedAStatus 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();
160 }
161 
initialize(const MQDescriptor<::aidl::android::hardware::sensors::Event,SynchronizedReadWrite> & in_eventQueueDescriptor,const MQDescriptor<int32_t,SynchronizedReadWrite> & in_wakeLockDescriptor,const std::shared_ptr<ISensorsCallback> & in_sensorsCallback)162 ScopedAStatus 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);
170 
171   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);
178 
179   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);
186 
187   return resultToAStatus(
188       initializeCommon(eventQueue, wakeLockQueue, dynamicCallback));
189 }
190 
injectSensorData(const::aidl::android::hardware::sensors::Event & in_event)191 ScopedAStatus 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)));
197 }
198 
199 ScopedAStatus
registerDirectChannel(const ISensors::SharedMemInfo & in_mem,int32_t * _aidl_return)200 HalProxyAidl::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);
206 
207   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       });
214 
215   native_handle_delete(const_cast<native_handle_t *>(
216       sharedMemInfo.memoryHandle.getNativeHandle()));
217 
218   return status;
219 }
220 
setOperationMode(::aidl::android::hardware::sensors::ISensors::OperationMode in_mode)221 ScopedAStatus HalProxyAidl::setOperationMode(
222     ::aidl::android::hardware::sensors::ISensors::OperationMode in_mode) {
223   return resultToAStatus(
224       HalProxy::setOperationMode(convertOperationMode(in_mode)));
225 }
226 
unregisterDirectChannel(int32_t in_channelHandle)227 ScopedAStatus HalProxyAidl::unregisterDirectChannel(int32_t in_channelHandle) {
228   return resultToAStatus(HalProxy::unregisterDirectChannel(in_channelHandle));
229 }
230 
dump(int fd,const char ** args,uint32_t numArgs)231 binder_status_t HalProxyAidl::dump(int fd, const char ** args,
232                                    uint32_t numArgs) {
233   native_handle_t *nativeHandle =
234       native_handle_create(1 /* numFds */, 0 /* numInts */);
235   nativeHandle->data[0] = fd;
236 
237   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);
243 
244   native_handle_delete(nativeHandle);
245   return STATUS_OK;
246 }
247 
248 }  // namespace implementation
249 }  // namespace sensors
250 }  // namespace hardware
251 }  // namespace android
252 }  // namespace aidl
253