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