Harpreet "Eli" Sangha | e47ae68 | 2019-06-05 16:52:47 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project * |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
Harpreet \"Eli\" Sangha | bbe7855 | 2020-04-09 17:16:40 +0900 | [diff] [blame] | 16 | #include <thread> |
| 17 | |
Harpreet "Eli" Sangha | e47ae68 | 2019-06-05 16:52:47 +0900 | [diff] [blame] | 18 | #include "utils.h" |
| 19 | #include "vibrator.h" |
| 20 | |
Harpreet \"Eli\" Sangha | bbe7855 | 2020-04-09 17:16:40 +0900 | [diff] [blame] | 21 | using std::chrono::milliseconds; |
| 22 | using std::this_thread::sleep_for; |
| 23 | |
Harpreet "Eli" Sangha | e47ae68 | 2019-06-05 16:52:47 +0900 | [diff] [blame] | 24 | namespace android { |
| 25 | namespace idlcli { |
| 26 | |
| 27 | class CommandVibrator; |
| 28 | |
| 29 | namespace vibrator { |
| 30 | |
| 31 | class CommandOn : public Command { |
| 32 | std::string getDescription() const override { return "Turn on vibrator."; } |
| 33 | |
Harpreet \"Eli\" Sangha | bbe7855 | 2020-04-09 17:16:40 +0900 | [diff] [blame] | 34 | std::string getUsageSummary() const override { return "[options] <duration>"; } |
Harpreet "Eli" Sangha | e47ae68 | 2019-06-05 16:52:47 +0900 | [diff] [blame] | 35 | |
| 36 | UsageDetails getUsageDetails() const override { |
| 37 | UsageDetails details{ |
Harpreet \"Eli\" Sangha | bbe7855 | 2020-04-09 17:16:40 +0900 | [diff] [blame] | 38 | {"-b", {"Block for duration of vibration."}}, |
Harpreet "Eli" Sangha | e47ae68 | 2019-06-05 16:52:47 +0900 | [diff] [blame] | 39 | {"<duration>", {"In milliseconds."}}, |
| 40 | }; |
| 41 | return details; |
| 42 | } |
| 43 | |
| 44 | Status doArgs(Args &args) override { |
Harpreet \"Eli\" Sangha | bbe7855 | 2020-04-09 17:16:40 +0900 | [diff] [blame] | 45 | while (args.get<std::string>().value_or("").find("-") == 0) { |
| 46 | auto opt = *args.pop<std::string>(); |
| 47 | if (opt == "--") { |
| 48 | break; |
| 49 | } else if (opt == "-b") { |
| 50 | mBlocking = true; |
| 51 | } else { |
| 52 | std::cerr << "Invalid Option '" << opt << "'!" << std::endl; |
| 53 | return USAGE; |
| 54 | } |
| 55 | } |
Harpreet "Eli" Sangha | e47ae68 | 2019-06-05 16:52:47 +0900 | [diff] [blame] | 56 | if (auto duration = args.pop<decltype(mDuration)>()) { |
| 57 | mDuration = *duration; |
| 58 | } else { |
| 59 | std::cerr << "Missing or Invalid Duration!" << std::endl; |
| 60 | return USAGE; |
| 61 | } |
| 62 | if (!args.empty()) { |
| 63 | std::cerr << "Unexpected Arguments!" << std::endl; |
| 64 | return USAGE; |
| 65 | } |
| 66 | return OK; |
| 67 | } |
| 68 | |
| 69 | Status doMain(Args && /*args*/) override { |
Harpreet \"Eli\" Sangha | cb350b8 | 2019-11-12 15:15:25 +0900 | [diff] [blame] | 70 | std::string statusStr; |
| 71 | Status ret; |
Harpreet \"Eli\" Sangha | bbe7855 | 2020-04-09 17:16:40 +0900 | [diff] [blame] | 72 | std::shared_ptr<VibratorCallback> callback; |
Harpreet "Eli" Sangha | e47ae68 | 2019-06-05 16:52:47 +0900 | [diff] [blame] | 73 | |
Harpreet \"Eli\" Sangha | cb350b8 | 2019-11-12 15:15:25 +0900 | [diff] [blame] | 74 | if (auto hal = getHal<aidl::IVibrator>()) { |
Harpreet \"Eli\" Sangha | bbe7855 | 2020-04-09 17:16:40 +0900 | [diff] [blame] | 75 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 76 | ABinderProcess_startThreadPool(); |
| 77 | |
| 78 | int32_t cap; |
| 79 | hal->call(&aidl::IVibrator::getCapabilities, &cap); |
| 80 | |
| 81 | if (mBlocking && (cap & aidl::IVibrator::CAP_ON_CALLBACK)) { |
| 82 | callback = ndk::SharedRefBase::make<VibratorCallback>(); |
| 83 | } |
| 84 | |
| 85 | auto status = hal->call(&aidl::IVibrator::on, mDuration, callback); |
| 86 | |
Harpreet \"Eli\" Sangha | 6ce6f73 | 2019-11-28 16:53:41 +0900 | [diff] [blame] | 87 | statusStr = status.getDescription(); |
Harpreet \"Eli\" Sangha | cb350b8 | 2019-11-12 15:15:25 +0900 | [diff] [blame] | 88 | ret = status.isOk() ? OK : ERROR; |
| 89 | } else if (auto hal = getHal<V1_0::IVibrator>()) { |
| 90 | auto status = hal->call(&V1_0::IVibrator::on, mDuration); |
| 91 | statusStr = toString(status); |
| 92 | ret = status.isOk() && status == V1_0::Status::OK ? OK : ERROR; |
| 93 | } else { |
Harpreet "Eli" Sangha | e47ae68 | 2019-06-05 16:52:47 +0900 | [diff] [blame] | 94 | return UNAVAILABLE; |
| 95 | } |
| 96 | |
Harpreet \"Eli\" Sangha | bbe7855 | 2020-04-09 17:16:40 +0900 | [diff] [blame] | 97 | if (ret == OK && mBlocking) { |
| 98 | if (callback) { |
| 99 | callback->waitForComplete(); |
| 100 | } else { |
| 101 | sleep_for(milliseconds(mDuration)); |
| 102 | } |
| 103 | } |
| 104 | |
Harpreet \"Eli\" Sangha | cb350b8 | 2019-11-12 15:15:25 +0900 | [diff] [blame] | 105 | std::cout << "Status: " << statusStr << std::endl; |
Harpreet "Eli" Sangha | e47ae68 | 2019-06-05 16:52:47 +0900 | [diff] [blame] | 106 | |
Harpreet \"Eli\" Sangha | cb350b8 | 2019-11-12 15:15:25 +0900 | [diff] [blame] | 107 | return ret; |
Harpreet "Eli" Sangha | e47ae68 | 2019-06-05 16:52:47 +0900 | [diff] [blame] | 108 | } |
| 109 | |
Harpreet \"Eli\" Sangha | bbe7855 | 2020-04-09 17:16:40 +0900 | [diff] [blame] | 110 | bool mBlocking; |
Harpreet "Eli" Sangha | e47ae68 | 2019-06-05 16:52:47 +0900 | [diff] [blame] | 111 | uint32_t mDuration; |
| 112 | }; |
| 113 | |
| 114 | static const auto Command = CommandRegistry<CommandVibrator>::Register<CommandOn>("on"); |
| 115 | |
| 116 | } // namespace vibrator |
| 117 | } // namespace idlcli |
| 118 | } // namespace android |