• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 #pragma once
17 
18 #include <future>
19 
20 #include <aidl/android/hardware/vibrator/BnVibratorCallback.h>
21 #include <aidl/android/hardware/vibrator/IVibrator.h>
22 #include <aidl/android/hardware/vibrator/IVibratorManager.h>
23 #include <android/binder_manager.h>
24 #include <android/binder_process.h>
25 
26 #include "IdlCli.h"
27 #include "utils.h"
28 
29 namespace android {
30 
31 using ::aidl::android::hardware::vibrator::IVibrator;
32 using idlcli::IdlCli;
33 
getService(std::string name)34 inline auto getService(std::string name) {
35     const auto instance = std::string() + IVibrator::descriptor + "/" + name;
36     auto vibBinder = ndk::SpAIBinder(AServiceManager_checkService(instance.c_str()));
37     return IVibrator::fromBinder(vibBinder);
38 }
39 
getHal()40 static auto getHal() {
41     // Assume that if getService returns a nullptr, HAL is not available on the device.
42     const auto name = IdlCli::Get().getName();
43     return getService(name.empty() ? "default" : name);
44 }
45 
46 namespace idlcli {
47 namespace vibrator {
48 
49 namespace aidl = ::aidl::android::hardware::vibrator;
50 
51 class VibratorCallback : public aidl::BnVibratorCallback {
52 public:
onComplete()53     ndk::ScopedAStatus onComplete() override {
54         mPromise.set_value();
55         return ndk::ScopedAStatus::ok();
56     }
waitForComplete()57     void waitForComplete() { mPromise.get_future().wait(); }
58 
59 private:
60     std::promise<void> mPromise;
61 };
62 
63 } // namespace vibrator
64 } // namespace idlcli
65 
66 } // namespace android
67