Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 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 | #include "SensorDeviceUtils.h" |
| 18 | |
| 19 | #include <android/hardware/sensors/1.0/ISensors.h> |
| 20 | #include <utils/Log.h> |
| 21 | |
| 22 | #include <chrono> |
| 23 | #include <thread> |
| 24 | |
| 25 | using ::android::hardware::Void; |
| 26 | using namespace android::hardware::sensors::V1_0; |
| 27 | |
| 28 | namespace android { |
| 29 | namespace SensorDeviceUtils { |
| 30 | |
Martijn Coenen | b41e87a | 2017-11-02 14:00:41 +0100 | [diff] [blame^] | 31 | HidlServiceRegistrationWaiter::HidlServiceRegistrationWaiter() { |
| 32 | mRegistered = ISensors::registerForNotifications("default", this); |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | Return<void> HidlServiceRegistrationWaiter::onRegistration( |
| 36 | const hidl_string &fqName, const hidl_string &name, bool preexisting) { |
| 37 | ALOGV("onRegistration fqName %s, name %s, preexisting %d", |
| 38 | fqName.c_str(), name.c_str(), preexisting); |
| 39 | |
| 40 | { |
| 41 | std::lock_guard<std::mutex> lk(mLock); |
| 42 | mRestartObserved = true; |
| 43 | } |
| 44 | mCondition.notify_all(); |
| 45 | return Void(); |
| 46 | } |
| 47 | |
| 48 | void HidlServiceRegistrationWaiter::reset() { |
| 49 | std::lock_guard<std::mutex> lk(mLock); |
| 50 | mRestartObserved = false; |
| 51 | } |
| 52 | |
| 53 | bool HidlServiceRegistrationWaiter::wait() { |
| 54 | constexpr int DEFAULT_WAIT_MS = 100; |
| 55 | constexpr int TIMEOUT_MS = 1000; |
| 56 | |
| 57 | if (!mRegistered) { |
| 58 | ALOGW("Cannot register service notification, use default wait(%d ms)", DEFAULT_WAIT_MS); |
| 59 | std::this_thread::sleep_for(std::chrono::milliseconds(DEFAULT_WAIT_MS)); |
| 60 | // not sure if service is actually restarted |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | std::unique_lock<std::mutex> lk(mLock); |
| 65 | return mCondition.wait_for(lk, std::chrono::milliseconds(TIMEOUT_MS), |
| 66 | [this]{return mRestartObserved;}); |
| 67 | } |
| 68 | |
| 69 | } // namespace SensorDeviceUtils |
| 70 | } // namespace android |