| Harpreet \"Eli\" Sangha | 3a8400f | 2019-10-24 09:20:02 +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 |  | 
|  | 16 | #include "utils.h" | 
|  | 17 | #include "vibrator.h" | 
|  | 18 |  | 
|  | 19 | namespace android { | 
|  | 20 | namespace idlcli { | 
|  | 21 |  | 
|  | 22 | class CommandVibrator; | 
|  | 23 |  | 
|  | 24 | namespace vibrator { | 
|  | 25 |  | 
|  | 26 | using aidl::CompositeEffect; | 
|  | 27 |  | 
|  | 28 | class CommandCompose : public Command { | 
|  | 29 | std::string getDescription() const override { return "Compose vibration."; } | 
|  | 30 |  | 
| Harpreet \"Eli\" Sangha | bbe7855 | 2020-04-09 17:16:40 +0900 | [diff] [blame] | 31 | std::string getUsageSummary() const override { | 
|  | 32 | return "[options] <delay> <primitive> <scale> ..."; | 
|  | 33 | } | 
| Harpreet \"Eli\" Sangha | 3a8400f | 2019-10-24 09:20:02 +0900 | [diff] [blame] | 34 |  | 
|  | 35 | UsageDetails getUsageDetails() const override { | 
|  | 36 | UsageDetails details{ | 
| Harpreet \"Eli\" Sangha | bbe7855 | 2020-04-09 17:16:40 +0900 | [diff] [blame] | 37 | {"-b", {"Block for duration of vibration."}}, | 
| Harpreet \"Eli\" Sangha | 3a8400f | 2019-10-24 09:20:02 +0900 | [diff] [blame] | 38 | {"<delay>", {"In milliseconds"}}, | 
|  | 39 | {"<primitive>", {"Primitive ID."}}, | 
|  | 40 | {"<scale>", {"0.0 (exclusive) - 1.0 (inclusive)."}}, | 
|  | 41 | {"...", {"May repeat multiple times."}}, | 
|  | 42 | }; | 
|  | 43 | return details; | 
|  | 44 | } | 
|  | 45 |  | 
|  | 46 | Status doArgs(Args &args) override { | 
| Harpreet \"Eli\" Sangha | bbe7855 | 2020-04-09 17:16:40 +0900 | [diff] [blame] | 47 | while (args.get<std::string>().value_or("").find("-") == 0) { | 
|  | 48 | auto opt = *args.pop<std::string>(); | 
|  | 49 | if (opt == "--") { | 
|  | 50 | break; | 
|  | 51 | } else if (opt == "-b") { | 
|  | 52 | mBlocking = true; | 
|  | 53 | } else { | 
|  | 54 | std::cerr << "Invalid Option '" << opt << "'!" << std::endl; | 
|  | 55 | return USAGE; | 
|  | 56 | } | 
|  | 57 | } | 
| Harpreet \"Eli\" Sangha | 3a8400f | 2019-10-24 09:20:02 +0900 | [diff] [blame] | 58 | while (!args.empty()) { | 
|  | 59 | CompositeEffect effect; | 
|  | 60 | if (auto delay = args.pop<decltype(effect.delayMs)>()) { | 
|  | 61 | effect.delayMs = *delay; | 
|  | 62 | std::cout << "Delay: " << effect.delayMs << std::endl; | 
|  | 63 | } else { | 
|  | 64 | std::cerr << "Missing or Invalid Delay!" << std::endl; | 
|  | 65 | return USAGE; | 
|  | 66 | } | 
| Harpreet \"Eli\" Sangha | eb8505c | 2020-04-10 12:40:23 +0900 | [diff] [blame] | 67 | if (auto primitive = args.pop<decltype(effect.primitive)>()) { | 
|  | 68 | effect.primitive = *primitive; | 
| Harpreet \"Eli\" Sangha | 3a8400f | 2019-10-24 09:20:02 +0900 | [diff] [blame] | 69 | std::cout << "Primitive: " << toString(effect.primitive) << std::endl; | 
|  | 70 | } else { | 
|  | 71 | std::cerr << "Missing or Invalid Primitive!" << std::endl; | 
|  | 72 | return USAGE; | 
|  | 73 | } | 
|  | 74 | if (auto scale = args.pop<decltype(effect.scale)>(); | 
|  | 75 | scale && *scale > 0.0 && scale <= 1.0) { | 
|  | 76 | effect.scale = *scale; | 
|  | 77 | std::cout << "Scale: " << effect.scale << std::endl; | 
|  | 78 | } else { | 
|  | 79 | std::cerr << "Missing or Invalid Scale!" << std::endl; | 
|  | 80 | return USAGE; | 
|  | 81 | } | 
|  | 82 | mComposite.emplace_back(std::move(effect)); | 
|  | 83 | } | 
|  | 84 | if (!args.empty()) { | 
|  | 85 | std::cerr << "Unexpected Arguments!" << std::endl; | 
|  | 86 | return USAGE; | 
|  | 87 | } | 
|  | 88 | return OK; | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | Status doMain(Args && /*args*/) override { | 
| Harpreet \"Eli\" Sangha | bbe7855 | 2020-04-09 17:16:40 +0900 | [diff] [blame] | 92 | auto hal = getHal<aidl::IVibrator>(); | 
|  | 93 |  | 
|  | 94 | if (!hal) { | 
| Harpreet \"Eli\" Sangha | 3a8400f | 2019-10-24 09:20:02 +0900 | [diff] [blame] | 95 | return UNAVAILABLE; | 
|  | 96 | } | 
|  | 97 |  | 
| Harpreet \"Eli\" Sangha | bbe7855 | 2020-04-09 17:16:40 +0900 | [diff] [blame] | 98 | ABinderProcess_setThreadPoolMaxThreadCount(1); | 
|  | 99 | ABinderProcess_startThreadPool(); | 
| Harpreet \"Eli\" Sangha | 3a8400f | 2019-10-24 09:20:02 +0900 | [diff] [blame] | 100 |  | 
| Harpreet \"Eli\" Sangha | bbe7855 | 2020-04-09 17:16:40 +0900 | [diff] [blame] | 101 | std::shared_ptr<VibratorCallback> callback; | 
|  | 102 |  | 
|  | 103 | if (mBlocking) { | 
|  | 104 | callback = ndk::SharedRefBase::make<VibratorCallback>(); | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | auto status = hal->call(&aidl::IVibrator::compose, mComposite, callback); | 
|  | 108 |  | 
|  | 109 | if (status.isOk() && callback) { | 
|  | 110 | callback->waitForComplete(); | 
|  | 111 | } | 
|  | 112 |  | 
|  | 113 | std::cout << "Status: " << status.getDescription() << std::endl; | 
|  | 114 |  | 
|  | 115 | return status.isOk() ? OK : ERROR; | 
| Harpreet \"Eli\" Sangha | 3a8400f | 2019-10-24 09:20:02 +0900 | [diff] [blame] | 116 | } | 
|  | 117 |  | 
| Harpreet \"Eli\" Sangha | bbe7855 | 2020-04-09 17:16:40 +0900 | [diff] [blame] | 118 | bool mBlocking; | 
| Harpreet \"Eli\" Sangha | 3a8400f | 2019-10-24 09:20:02 +0900 | [diff] [blame] | 119 | std::vector<CompositeEffect> mComposite; | 
|  | 120 | }; | 
|  | 121 |  | 
|  | 122 | static const auto Command = CommandRegistry<CommandVibrator>::Register<CommandCompose>("compose"); | 
|  | 123 |  | 
|  | 124 | } // namespace vibrator | 
|  | 125 | } // namespace idlcli | 
|  | 126 | } // namespace android |