1 /*
2  * Copyright (C) 2017 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_SENSOR_DEVICE_UTIL
18 #define ANDROID_SENSOR_DEVICE_UTIL
19 
20 #include <android/hidl/manager/1.0/IServiceNotification.h>
21 
22 #include <condition_variable>
23 #include <thread>
24 
25 using ::android::hardware::hidl_string;
26 using ::android::hardware::Return;
27 using ::android::hidl::manager::V1_0::IServiceNotification;
28 
29 namespace android {
30 namespace SensorDeviceUtils {
31 
32 class HidlServiceRegistrationWaiter : public IServiceNotification {
33 public:
34 
35     HidlServiceRegistrationWaiter();
36 
37     Return<void> onRegistration(const hidl_string &fqName,
38                                 const hidl_string &name,
39                                 bool preexisting) override;
40 
41     void reset();
42 
43     /**
44      * Wait for service restart
45      *
46      * @return true if service is restart since last reset(); false otherwise.
47      */
48     bool wait();
49 protected:
50     void onFirstRef() override;
51 private:
52     bool mRegistered;
53 
54     std::mutex mLock;
55     std::condition_variable mCondition;
56     bool mRestartObserved;
57 };
58 
59 } // namespace SensorDeviceUtils
60 } // namespace android;
61 
62 #endif // ANDROID_SENSOR_SERVICE_UTIL
63