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 "SensorsAidlEnvironment.h"
18
19 #include <android/binder_manager.h>
20 #include <log/log.h>
21
22 #include <aidl/android/hardware/sensors/BnSensorsCallback.h>
23
24 using aidl::android::hardware::sensors::BnSensorsCallback;
25 using aidl::android::hardware::sensors::SensorInfo;
26 using android::hardware::EventFlag;
27 using ndk::ScopedAStatus;
28 using ndk::SpAIBinder;
29
30 namespace {
31
serviceDied(void *)32 void serviceDied(void* /* cookie */) {
33 ALOGE("Sensors HAL died (likely crashed) during test");
34 FAIL() << "Sensors HAL died during test";
35 }
36
37 class NoOpSensorsCallback : public BnSensorsCallback {
38 public:
onDynamicSensorsConnected(const std::vector<SensorInfo> &)39 ScopedAStatus onDynamicSensorsConnected(
40 const std::vector<SensorInfo>& /* sensorInfos */) override {
41 return ScopedAStatus::ok();
42 }
43
onDynamicSensorsDisconnected(const std::vector<int32_t> &)44 ScopedAStatus onDynamicSensorsDisconnected(
45 const std::vector<int32_t>& /* sensorHandles */) override {
46 return ScopedAStatus::ok();
47 }
48 };
49
50 } // anonymous namespace
51
SensorsAidlEnvironment(const std::string & service_name)52 SensorsAidlEnvironment::SensorsAidlEnvironment(const std::string& service_name)
53 : SensorsVtsEnvironmentBase(service_name),
54 mCallback(ndk::SharedRefBase::make<NoOpSensorsCallback>()),
55 mDeathRecipient(AIBinder_DeathRecipient_new(serviceDied)) {}
56
resetHal()57 bool SensorsAidlEnvironment::resetHal() {
58 bool succeed = false;
59 do {
60 mSensors = ISensors::fromBinder(
61 SpAIBinder(AServiceManager_waitForService(mServiceName.c_str())));
62 if (mSensors == nullptr) {
63 break;
64 }
65
66 AIBinder_linkToDeath(mSensors->asBinder().get(), mDeathRecipient.get(), this);
67
68 // Initialize FMQs
69 mWakeLockQueue = std::make_unique<WakeLockQueue>(MAX_RECEIVE_BUFFER_EVENT_COUNT,
70 true /* configureEventFlagWord */);
71 mEventQueue = std::make_unique<EventQueue>(MAX_RECEIVE_BUFFER_EVENT_COUNT,
72 true /* configureEventFlagWord */);
73
74 if (mWakeLockQueue == nullptr || mEventQueue == nullptr) {
75 break;
76 }
77
78 EventFlag::deleteEventFlag(&mEventQueueFlag);
79 EventFlag::createEventFlag(mEventQueue->getEventFlagWord(), &mEventQueueFlag);
80 if (mEventQueueFlag == nullptr) {
81 break;
82 }
83
84 mSensors->initialize(mEventQueue->dupeDesc(), mWakeLockQueue->dupeDesc(), mCallback);
85
86 std::vector<SensorInfo> sensorList;
87 if (!mSensors->getSensorsList(&sensorList).isOk()) {
88 break;
89 }
90
91 // stop each sensor individually
92 bool ok = true;
93 for (const auto& i : sensorList) {
94 if (!mSensors->activate(i.sensorHandle, false).isOk()) {
95 ok = false;
96 break;
97 }
98 }
99
100 if (!ok) {
101 break;
102 }
103
104 // mark it done
105 succeed = true;
106 } while (0);
107
108 if (!succeed) {
109 mSensors = nullptr;
110 }
111
112 return succeed;
113 }
114
TearDown()115 void SensorsAidlEnvironment::TearDown() {
116 mStopThread = true;
117
118 if (mEventQueueFlag != nullptr) {
119 // Wake up the event queue so the poll thread can exit
120 mEventQueueFlag->wake(ISensors::EVENT_QUEUE_FLAG_BITS_READ_AND_PROCESS);
121 if (mPollThread.joinable()) {
122 mPollThread.join();
123 }
124
125 EventFlag::deleteEventFlag(&mEventQueueFlag);
126 }
127 }
128
startPollingThread()129 void SensorsAidlEnvironment::startPollingThread() {
130 mStopThread = false;
131 mEvents.reserve(MAX_RECEIVE_BUFFER_EVENT_COUNT);
132 mPollThread = std::thread(pollingThread, this);
133 }
134
readEvents()135 void SensorsAidlEnvironment::readEvents() {
136 size_t availableEvents = mEventQueue->availableToRead();
137
138 if (availableEvents == 0) {
139 uint32_t eventFlagState = 0;
140
141 mEventQueueFlag->wait(ISensors::EVENT_QUEUE_FLAG_BITS_READ_AND_PROCESS, &eventFlagState);
142 availableEvents = mEventQueue->availableToRead();
143 }
144
145 size_t eventsToRead = std::min(availableEvents, mEventBuffer.size());
146 if (eventsToRead > 0) {
147 if (mEventQueue->read(mEventBuffer.data(), eventsToRead)) {
148 mEventQueueFlag->wake(ISensors::EVENT_QUEUE_FLAG_BITS_EVENTS_READ);
149 for (size_t i = 0; i < eventsToRead; i++) {
150 addEvent(mEventBuffer[i]);
151 }
152 }
153 }
154 }
155
pollingThread(SensorsAidlEnvironment * env)156 void SensorsAidlEnvironment::pollingThread(SensorsAidlEnvironment* env) {
157 ALOGD("polling thread start");
158
159 while (!env->mStopThread.load()) {
160 env->readEvents();
161 }
162
163 ALOGD("polling thread end");
164 }
165