1 /*
2  * Copyright (C) 2019 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 "HalProxyCallback.h"
18 
19 #include <cinttypes>
20 
21 namespace android {
22 namespace hardware {
23 namespace sensors {
24 namespace V2_0 {
25 namespace implementation {
26 
27 static constexpr int32_t kBitsAfterSubHalIndex = 24;
28 
29 /**
30  * Set the subhal index as first byte of sensor handle and return this modified version.
31  *
32  * @param sensorHandle The sensor handle to modify.
33  * @param subHalIndex The index in the hal proxy of the sub hal this sensor belongs to.
34  *
35  * @return The modified sensor handle.
36  */
setSubHalIndex(int32_t sensorHandle,size_t subHalIndex)37 int32_t setSubHalIndex(int32_t sensorHandle, size_t subHalIndex) {
38     return sensorHandle | (static_cast<int32_t>(subHalIndex) << kBitsAfterSubHalIndex);
39 }
40 
postEvents(const std::vector<V2_1::Event> & events,ScopedWakelock wakelock)41 void HalProxyCallbackBase::postEvents(const std::vector<V2_1::Event>& events,
42                                       ScopedWakelock wakelock) {
43     if (events.empty() || !mCallback->areThreadsRunning()) return;
44     size_t numWakeupEvents;
45     std::vector<V2_1::Event> processedEvents = processEvents(events, &numWakeupEvents);
46     if (numWakeupEvents > 0) {
47         ALOG_ASSERT(wakelock.isLocked(),
48                     "Wakeup events posted while wakelock unlocked for subhal"
49                     " w/ index %" PRId32 ".",
50                     mSubHalIndex);
51     } else {
52         ALOG_ASSERT(!wakelock.isLocked(),
53                     "No Wakeup events posted but wakelock locked for subhal"
54                     " w/ index %" PRId32 ".",
55                     mSubHalIndex);
56     }
57     mCallback->postEventsToMessageQueue(processedEvents, numWakeupEvents, std::move(wakelock));
58 }
59 
createScopedWakelock(bool lock)60 ScopedWakelock HalProxyCallbackBase::createScopedWakelock(bool lock) {
61     ScopedWakelock wakelock(mRefCounter, lock);
62     return wakelock;
63 }
64 
processEvents(const std::vector<V2_1::Event> & events,size_t * numWakeupEvents) const65 std::vector<V2_1::Event> HalProxyCallbackBase::processEvents(const std::vector<V2_1::Event>& events,
66                                                              size_t* numWakeupEvents) const {
67     *numWakeupEvents = 0;
68     std::vector<V2_1::Event> eventsOut;
69     for (V2_1::Event event : events) {
70         event.sensorHandle = setSubHalIndex(event.sensorHandle, mSubHalIndex);
71         eventsOut.push_back(event);
72         const V2_1::SensorInfo& sensor = mCallback->getSensorInfo(event.sensorHandle);
73         if ((sensor.flags & V1_0::SensorFlagBits::WAKE_UP) != 0) {
74             (*numWakeupEvents)++;
75         }
76     }
77     return eventsOut;
78 }
79 
80 }  // namespace implementation
81 }  // namespace V2_0
82 }  // namespace sensors
83 }  // namespace hardware
84 }  // namespace android
85