• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         if (event.sensorType == V2_1::SensorType::DYNAMIC_SENSOR_META) {
72             event.u.dynamic.sensorHandle =
73                     setSubHalIndex(event.u.dynamic.sensorHandle, mSubHalIndex);
74         }
75         eventsOut.push_back(event);
76         const V2_1::SensorInfo& sensor = mCallback->getSensorInfo(event.sensorHandle);
77         if ((sensor.flags & V1_0::SensorFlagBits::WAKE_UP) != 0) {
78             (*numWakeupEvents)++;
79         }
80     }
81     return eventsOut;
82 }
83 
84 }  // namespace implementation
85 }  // namespace V2_0
86 }  // namespace sensors
87 }  // namespace hardware
88 }  // namespace android
89