blob: 97c057fa0c0a79fe59411e3a4f170c40e453ee17 [file] [log] [blame]
Harpreet \"Eli\" Sangha3a8400f2019-10-24 09:20:02 +09001/*
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
19namespace android {
20namespace idlcli {
21
22class CommandVibrator;
23
24namespace vibrator {
25
26using aidl::CompositeEffect;
27
28class CommandCompose : public Command {
29 std::string getDescription() const override { return "Compose vibration."; }
30
Harpreet \"Eli\" Sanghabbe78552020-04-09 17:16:40 +090031 std::string getUsageSummary() const override {
32 return "[options] <delay> <primitive> <scale> ...";
33 }
Harpreet \"Eli\" Sangha3a8400f2019-10-24 09:20:02 +090034
35 UsageDetails getUsageDetails() const override {
36 UsageDetails details{
Harpreet \"Eli\" Sanghabbe78552020-04-09 17:16:40 +090037 {"-b", {"Block for duration of vibration."}},
Harpreet \"Eli\" Sangha3a8400f2019-10-24 09:20:02 +090038 {"<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\" Sanghabbe78552020-04-09 17:16:40 +090047 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\" Sangha3a8400f2019-10-24 09:20:02 +090058 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\" Sanghaeb8505c2020-04-10 12:40:23 +090067 if (auto primitive = args.pop<decltype(effect.primitive)>()) {
68 effect.primitive = *primitive;
Harpreet \"Eli\" Sangha3a8400f2019-10-24 09:20:02 +090069 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\" Sanghabbe78552020-04-09 17:16:40 +090092 auto hal = getHal<aidl::IVibrator>();
93
94 if (!hal) {
Harpreet \"Eli\" Sangha3a8400f2019-10-24 09:20:02 +090095 return UNAVAILABLE;
96 }
97
Harpreet \"Eli\" Sanghabbe78552020-04-09 17:16:40 +090098 ABinderProcess_setThreadPoolMaxThreadCount(1);
99 ABinderProcess_startThreadPool();
Harpreet \"Eli\" Sangha3a8400f2019-10-24 09:20:02 +0900100
Harpreet \"Eli\" Sanghabbe78552020-04-09 17:16:40 +0900101 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\" Sangha3a8400f2019-10-24 09:20:02 +0900116 }
117
Harpreet \"Eli\" Sanghabbe78552020-04-09 17:16:40 +0900118 bool mBlocking;
Harpreet \"Eli\" Sangha3a8400f2019-10-24 09:20:02 +0900119 std::vector<CompositeEffect> mComposite;
120};
121
122static const auto Command = CommandRegistry<CommandVibrator>::Register<CommandCompose>("compose");
123
124} // namespace vibrator
125} // namespace idlcli
126} // namespace android