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 
17 #include "CustomVibrator.h"
18 
19 #include <android-base/logging.h>
20 #include <thread>
21 
22 namespace aidl::android::hardware::tests::extension::vibrator {
23 
getVendorCapabilities(int32_t * _aidl_return)24 ndk::ScopedAStatus CustomVibrator::getVendorCapabilities(int32_t* _aidl_return) {
25     *_aidl_return = ICustomVibrator::CAP_VENDOR_DIRECTIONALITY;
26     return ndk::ScopedAStatus::ok();
27 }
28 
setDirectionality(Directionality directionality)29 ndk::ScopedAStatus CustomVibrator::setDirectionality(Directionality directionality) {
30     LOG(INFO) << "Custom vibrator set directionality";
31     // do something cool in hardware
32     (void)directionality;
33     return ndk::ScopedAStatus::ok();
34 }
35 
perform(VendorEffect effect,const std::shared_ptr<IVibratorCallback> & callback,int32_t * _aidl_return)36 ndk::ScopedAStatus CustomVibrator::perform(VendorEffect effect,
37                                            const std::shared_ptr<IVibratorCallback>& callback,
38                                            int32_t* _aidl_return) {
39     LOG(INFO) << "Custom vibrator perform";
40 
41     if (effect != VendorEffect::CRACKLE && effect != VendorEffect::WIGGLE) {
42         return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION));
43     }
44 
45     constexpr size_t kEffectMillis = 100;
46 
47     if (callback != nullptr) {
48         std::thread([=] {
49             LOG(INFO) << "Starting vendor perform on another thread";
50             usleep(kEffectMillis * 1000);
51             LOG(INFO) << "Notifying vendor perform complete";
52             callback->onComplete();
53         }).detach();
54     }
55 
56     *_aidl_return = kEffectMillis;
57     return ndk::ScopedAStatus::ok();
58 }
59 
60 }  // namespace aidl::android::hardware::tests::extension::vibrator
61