• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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()19 void 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()30 void SensorsHidlEnvironmentBase::HidlTearDown() {
31     mStopThread = true;
32     mPollThread.detach();
33 }
34 
catEvents(std::vector<Event> * output)35 void SensorsHidlEnvironmentBase::catEvents(std::vector<Event>* output) {
36     std::lock_guard<std::mutex> lock(mEventsMutex);
37     if (output) {
38         output->insert(output->end(), mEvents.begin(), mEvents.end());
39     }
40     mEvents.clear();
41 }
42 
setCollection(bool enable)43 void SensorsHidlEnvironmentBase::setCollection(bool enable) {
44     std::lock_guard<std::mutex> lock(mEventsMutex);
45     mCollectionEnabled = enable;
46 }
47 
addEvent(const Event & ev)48 void SensorsHidlEnvironmentBase::addEvent(const Event& ev) {
49     std::lock_guard<std::mutex> lock(mEventsMutex);
50     if (mCollectionEnabled) {
51         mEvents.push_back(ev);
52     }
53 
54     if (mCallback != nullptr) {
55         mCallback->onEvent(ev);
56     }
57 }
58 
registerCallback(IEventCallback * callback)59 void SensorsHidlEnvironmentBase::registerCallback(IEventCallback* callback) {
60     std::lock_guard<std::mutex> lock(mEventsMutex);
61     mCallback = callback;
62 }
63 
unregisterCallback()64 void SensorsHidlEnvironmentBase::unregisterCallback() {
65     std::lock_guard<std::mutex> lock(mEventsMutex);
66     mCallback = nullptr;
67 }