1 /*
2 * Copyright (C) 2018 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 "SensorsHidlEnvironmentV2_X.h"
18
19 #include <android/hardware/sensors/2.0/types.h>
20 #include <android/hardware/sensors/2.1/types.h>
21
22 #include <log/log.h>
23
24 #include <algorithm>
25 #include <vector>
26
27 using ::android::hardware::EventFlag;
28 using ::android::hardware::hidl_vec;
29 using ::android::hardware::Return;
30 using ::android::hardware::sensors::V1_0::Result;
31 using ::android::hardware::sensors::V2_0::EventQueueFlagBits;
32 using ::android::hardware::sensors::V2_1::SensorInfo;
33 #ifdef SENSORS_HAL_2_1
34 using ::android::hardware::sensors::V2_1::ISensors;
35 #else
36 using ::android::hardware::sensors::V2_0::ISensors;
37 #endif
38 using ::android::hardware::sensors::V2_1::ISensorsCallback;
39
40 template <typename EnumType>
asBaseType(EnumType value)41 constexpr typename std::underlying_type<EnumType>::type asBaseType(EnumType value) {
42 return static_cast<typename std::underlying_type<EnumType>::type>(value);
43 }
44
serviceDied(uint64_t,const::android::wp<::android::hidl::base::V1_0::IBase> &)45 void SensorsHalDeathRecipient::serviceDied(
46 uint64_t /* cookie */,
47 const ::android::wp<::android::hidl::base::V1_0::IBase>& /* service */) {
48 ALOGE("Sensors HAL died (likely crashed) during test");
49 FAIL() << "Sensors HAL died during test";
50 }
51
resetHal()52 bool SensorsHidlEnvironmentV2_X::resetHal() {
53 bool succeed = false;
54 do {
55 mSensors = wrapISensors(ISensors::getService(mServiceName));
56 if (mSensors == nullptr) {
57 break;
58 }
59 mSensors->linkToDeath(mDeathRecipient, 0 /* cookie */);
60
61 // Initialize FMQs
62 mWakeLockQueue = std::make_unique<WakeLockQueue>(MAX_RECEIVE_BUFFER_EVENT_COUNT,
63 true /* configureEventFlagWord */);
64
65 if (mWakeLockQueue == nullptr) {
66 break;
67 }
68
69 EventFlag::deleteEventFlag(&mEventQueueFlag);
70 EventFlag::createEventFlag(mSensors->getEventQueue()->getEventFlagWord(), &mEventQueueFlag);
71 if (mEventQueueFlag == nullptr) {
72 break;
73 }
74
75 mSensors->initialize(*mWakeLockQueue->getDesc(), new NoOpSensorsCallback());
76
77 std::vector<SensorInfo> sensorList;
78 if (!mSensors->getSensorsList([&](const hidl_vec<SensorInfo>& list) { sensorList = list; })
79 .isOk()) {
80 break;
81 }
82
83 // stop each sensor individually
84 bool ok = true;
85 for (const auto& i : sensorList) {
86 if (!mSensors->activate(i.sensorHandle, false).isOk()) {
87 ok = false;
88 break;
89 }
90 }
91 if (!ok) {
92 break;
93 }
94
95 // mark it done
96 succeed = true;
97 } while (0);
98
99 if (!succeed) {
100 mSensors = nullptr;
101 }
102
103 return succeed;
104 }
105
HidlTearDown()106 void SensorsHidlEnvironmentV2_X::HidlTearDown() {
107 mStopThread = true;
108
109 if (mEventQueueFlag != nullptr) {
110 // Wake up the event queue so the poll thread can exit
111 mEventQueueFlag->wake(asBaseType(EventQueueFlagBits::READ_AND_PROCESS));
112 if (mPollThread.joinable()) {
113 mPollThread.join();
114 }
115
116 EventFlag::deleteEventFlag(&mEventQueueFlag);
117 }
118 }
119
startPollingThread()120 void SensorsHidlEnvironmentV2_X::startPollingThread() {
121 mStopThread = false;
122 mEvents.reserve(MAX_RECEIVE_BUFFER_EVENT_COUNT);
123 mPollThread = std::thread(pollingThread, this);
124 }
125
readEvents()126 void SensorsHidlEnvironmentV2_X::readEvents() {
127 size_t availableEvents = mSensors->getEventQueue()->availableToRead();
128
129 if (availableEvents == 0) {
130 uint32_t eventFlagState = 0;
131
132 mEventQueueFlag->wait(asBaseType(EventQueueFlagBits::READ_AND_PROCESS), &eventFlagState);
133 availableEvents = mSensors->getEventQueue()->availableToRead();
134 }
135
136 size_t eventsToRead = std::min(availableEvents, mEventBuffer.size());
137 if (eventsToRead > 0) {
138 if (mSensors->getEventQueue()->read(mEventBuffer.data(), eventsToRead)) {
139 mEventQueueFlag->wake(asBaseType(EventQueueFlagBits::EVENTS_READ));
140 for (size_t i = 0; i < eventsToRead; i++) {
141 addEvent(mEventBuffer[i]);
142 }
143 }
144 }
145 }
146
pollingThread(SensorsHidlEnvironmentV2_X * env)147 void SensorsHidlEnvironmentV2_X::pollingThread(SensorsHidlEnvironmentV2_X* env) {
148 ALOGD("polling thread start");
149
150 while (!env->mStopThread.load()) {
151 env->readEvents();
152 }
153
154 ALOGD("polling thread end");
155 }
156