blob: 30de63fd798adb6249cb7a3abadc487c6d7fac64 [file] [log] [blame]
Peng Xu1a00e2d2017-09-27 23:08:30 -07001/*
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
25using ::android::hardware::Void;
26using namespace android::hardware::sensors::V1_0;
27
28namespace android {
29namespace SensorDeviceUtils {
30
Martijn Coenenb41e87a2017-11-02 14:00:41 +010031HidlServiceRegistrationWaiter::HidlServiceRegistrationWaiter() {
32 mRegistered = ISensors::registerForNotifications("default", this);
Peng Xu1a00e2d2017-09-27 23:08:30 -070033}
34
35Return<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
48void HidlServiceRegistrationWaiter::reset() {
49 std::lock_guard<std::mutex> lk(mLock);
50 mRestartObserved = false;
51}
52
53bool 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