blob: 0b4c93c46996b7fa241d871479b3f6f23c6557db [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 }
67 // TODO: Use range validation when supported by AIDL
68 if (auto primitive = args.pop<std::underlying_type_t<decltype(effect.primitive)>>()) {
69 effect.primitive = static_cast<decltype(effect.primitive)>(*primitive);
70 std::cout << "Primitive: " << toString(effect.primitive) << std::endl;
71 } else {
72 std::cerr << "Missing or Invalid Primitive!" << std::endl;
73 return USAGE;
74 }
75 if (auto scale = args.pop<decltype(effect.scale)>();
76 scale && *scale > 0.0 && scale <= 1.0) {
77 effect.scale = *scale;
78 std::cout << "Scale: " << effect.scale << std::endl;
79 } else {
80 std::cerr << "Missing or Invalid Scale!" << std::endl;
81 return USAGE;
82 }
83 mComposite.emplace_back(std::move(effect));
84 }
85 if (!args.empty()) {
86 std::cerr << "Unexpected Arguments!" << std::endl;
87 return USAGE;
88 }
89 return OK;
90 }
91
92 Status doMain(Args && /*args*/) override {
Harpreet \"Eli\" Sanghabbe78552020-04-09 17:16:40 +090093 auto hal = getHal<aidl::IVibrator>();
94
95 if (!hal) {
Harpreet \"Eli\" Sangha3a8400f2019-10-24 09:20:02 +090096 return UNAVAILABLE;
97 }
98
Harpreet \"Eli\" Sanghabbe78552020-04-09 17:16:40 +090099 ABinderProcess_setThreadPoolMaxThreadCount(1);
100 ABinderProcess_startThreadPool();
Harpreet \"Eli\" Sangha3a8400f2019-10-24 09:20:02 +0900101
Harpreet \"Eli\" Sanghabbe78552020-04-09 17:16:40 +0900102 std::shared_ptr<VibratorCallback> callback;
103
104 if (mBlocking) {
105 callback = ndk::SharedRefBase::make<VibratorCallback>();
106 }
107
108 auto status = hal->call(&aidl::IVibrator::compose, mComposite, callback);
109
110 if (status.isOk() && callback) {
111 callback->waitForComplete();
112 }
113
114 std::cout << "Status: " << status.getDescription() << std::endl;
115
116 return status.isOk() ? OK : ERROR;
Harpreet \"Eli\" Sangha3a8400f2019-10-24 09:20:02 +0900117 }
118
Harpreet \"Eli\" Sanghabbe78552020-04-09 17:16:40 +0900119 bool mBlocking;
Harpreet \"Eli\" Sangha3a8400f2019-10-24 09:20:02 +0900120 std::vector<CompositeEffect> mComposite;
121};
122
123static const auto Command = CommandRegistry<CommandVibrator>::Register<CommandCompose>("compose");
124
125} // namespace vibrator
126} // namespace idlcli
127} // namespace android