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 #ifndef ANDROID_SENSORS_HIDL_ENVIRONMENT_BASE_H
18 #define ANDROID_SENSORS_HIDL_ENVIRONMENT_BASE_H
19 
20 #include <gtest/gtest.h>
21 
22 #include <atomic>
23 #include <memory>
24 #include <mutex>
25 #include <thread>
26 #include <vector>
27 
28 template <class Event>
29 class IEventCallback {
30   public:
31     virtual ~IEventCallback() = default;
32     virtual void onEvent(const Event& event) = 0;
33 };
34 
35 template <class Event>
36 class SensorsHidlEnvironmentBase {
37   public:
HidlSetUp()38     virtual void HidlSetUp() {
39         ASSERT_TRUE(resetHal()) << "could not get hidl service";
40 
41         mCollectionEnabled = false;
42         startPollingThread();
43 
44         // In case framework just stopped for test and there is sensor events in the pipe,
45         // wait some time for those events to be cleared to avoid them messing up the test.
46         std::this_thread::sleep_for(std::chrono::seconds(3));
47     }
48 
49     virtual void HidlTearDown() = 0;
50 
51     // Get and clear all events collected so far (like "cat" shell command).
52     // If output is nullptr, it clears all collected events.
catEvents(std::vector<Event> * output)53     void catEvents(std::vector<Event>* output) {
54         std::lock_guard<std::mutex> lock(mEventsMutex);
55         if (output) {
56             output->insert(output->end(), mEvents.begin(), mEvents.end());
57         }
58         mEvents.clear();
59     }
60 
61     // set sensor event collection status
setCollection(bool enable)62     void setCollection(bool enable) {
63         std::lock_guard<std::mutex> lock(mEventsMutex);
64         mCollectionEnabled = enable;
65     }
66 
registerCallback(IEventCallback<Event> * callback)67     void registerCallback(IEventCallback<Event>* callback) {
68         std::lock_guard<std::mutex> lock(mEventsMutex);
69         mCallback = callback;
70     }
71 
unregisterCallback()72     void unregisterCallback() {
73         std::lock_guard<std::mutex> lock(mEventsMutex);
74         mCallback = nullptr;
75     }
76 
77    protected:
SensorsHidlEnvironmentBase(const std::string & service_name)78      SensorsHidlEnvironmentBase(const std::string& service_name)
79          : mCollectionEnabled(false), mCallback(nullptr) {
80          mServiceName = service_name;
81      }
~SensorsHidlEnvironmentBase()82      virtual ~SensorsHidlEnvironmentBase(){};
83 
addEvent(const Event & ev)84      void addEvent(const Event& ev) {
85          std::lock_guard<std::mutex> lock(mEventsMutex);
86          if (mCollectionEnabled) {
87              mEvents.push_back(ev);
88          }
89 
90          if (mCallback != nullptr) {
91              mCallback->onEvent(ev);
92          }
93      }
94 
95      virtual void startPollingThread() = 0;
96      virtual bool resetHal() = 0;
97 
98      std::string mServiceName;
99      bool mCollectionEnabled;
100      std::atomic_bool mStopThread;
101      std::thread mPollThread;
102      std::vector<Event> mEvents;
103      std::mutex mEventsMutex;
104 
105      IEventCallback<Event>* mCallback;
106 
107      GTEST_DISALLOW_COPY_AND_ASSIGN_(SensorsHidlEnvironmentBase<Event>);
108 };
109 
110 #endif  // ANDROID_SENSORS_HIDL_ENVIRONMENT_BASE_H