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 "SensorsHidlEnvironmentBase.h" 18 HidlSetUp()19void SensorsHidlEnvironmentBase::HidlSetUp() { 20 ASSERT_TRUE(resetHal()) << "could not get hidl service"; 21 22 mCollectionEnabled = false; 23 startPollingThread(); 24 25 // In case framework just stopped for test and there is sensor events in the pipe, 26 // wait some time for those events to be cleared to avoid them messing up the test. 27 std::this_thread::sleep_for(std::chrono::seconds(3)); 28 } 29 HidlTearDown()30void SensorsHidlEnvironmentBase::HidlTearDown() { 31 mStopThread = true; 32 if (mPollThread.joinable()) { 33 mPollThread.detach(); 34 } 35 } 36 catEvents(std::vector<Event> * output)37void SensorsHidlEnvironmentBase::catEvents(std::vector<Event>* output) { 38 std::lock_guard<std::mutex> lock(mEventsMutex); 39 if (output) { 40 output->insert(output->end(), mEvents.begin(), mEvents.end()); 41 } 42 mEvents.clear(); 43 } 44 setCollection(bool enable)45void SensorsHidlEnvironmentBase::setCollection(bool enable) { 46 std::lock_guard<std::mutex> lock(mEventsMutex); 47 mCollectionEnabled = enable; 48 } 49 addEvent(const Event & ev)50void SensorsHidlEnvironmentBase::addEvent(const Event& ev) { 51 std::lock_guard<std::mutex> lock(mEventsMutex); 52 if (mCollectionEnabled) { 53 mEvents.push_back(ev); 54 } 55 56 if (mCallback != nullptr) { 57 mCallback->onEvent(ev); 58 } 59 } 60 registerCallback(IEventCallback * callback)61void SensorsHidlEnvironmentBase::registerCallback(IEventCallback* callback) { 62 std::lock_guard<std::mutex> lock(mEventsMutex); 63 mCallback = callback; 64 } 65 unregisterCallback()66void SensorsHidlEnvironmentBase::unregisterCallback() { 67 std::lock_guard<std::mutex> lock(mEventsMutex); 68 mCallback = nullptr; 69 }