• 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 #ifndef ANDROID_SENSORS_HIDL_ENVIRONMENT_BASE_H
18 #define ANDROID_SENSORS_HIDL_ENVIRONMENT_BASE_H
19 
20 #include <VtsHalHidlTargetTestEnvBase.h>
21 
22 #include <android/hardware/sensors/1.0/types.h>
23 
24 #include <atomic>
25 #include <memory>
26 #include <mutex>
27 #include <thread>
28 #include <vector>
29 
30 class IEventCallback {
31    public:
32     virtual ~IEventCallback() = default;
33     virtual void onEvent(const ::android::hardware::sensors::V1_0::Event& event) = 0;
34 };
35 
36 class SensorsHidlEnvironmentBase : public ::testing::VtsHalHidlTargetTestEnvBase {
37    public:
38     using Event = ::android::hardware::sensors::V1_0::Event;
39     virtual void HidlSetUp() override;
40     virtual void HidlTearDown() override;
41 
42     // Get and clear all events collected so far (like "cat" shell command).
43     // If output is nullptr, it clears all collected events.
44     void catEvents(std::vector<Event>* output);
45 
46     // set sensor event collection status
47     void setCollection(bool enable);
48 
49     void registerCallback(IEventCallback* callback);
50     void unregisterCallback();
51 
52    protected:
SensorsHidlEnvironmentBase()53     SensorsHidlEnvironmentBase() : mCollectionEnabled(false), mCallback(nullptr) {}
54 
55     void addEvent(const Event& ev);
56 
57     virtual void startPollingThread() = 0;
58     virtual bool resetHal() = 0;
59 
60     bool mCollectionEnabled;
61     std::atomic_bool mStopThread;
62     std::thread mPollThread;
63     std::vector<Event> mEvents;
64     std::mutex mEventsMutex;
65 
66     IEventCallback* mCallback;
67 
68     GTEST_DISALLOW_COPY_AND_ASSIGN_(SensorsHidlEnvironmentBase);
69 };
70 
71 #endif  // ANDROID_SENSORS_HIDL_ENVIRONMENT_BASE_H