blob: 705e40bbf2d5c1a4946411dd03d9560f0ef0e9b8 [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
31 std::string getUsageSummary() const override { return "<delay> <primitive> <scale> ..."; }
32
33 UsageDetails getUsageDetails() const override {
34 UsageDetails details{
35 {"<delay>", {"In milliseconds"}},
36 {"<primitive>", {"Primitive ID."}},
37 {"<scale>", {"0.0 (exclusive) - 1.0 (inclusive)."}},
38 {"...", {"May repeat multiple times."}},
39 };
40 return details;
41 }
42
43 Status doArgs(Args &args) override {
44 while (!args.empty()) {
45 CompositeEffect effect;
46 if (auto delay = args.pop<decltype(effect.delayMs)>()) {
47 effect.delayMs = *delay;
48 std::cout << "Delay: " << effect.delayMs << std::endl;
49 } else {
50 std::cerr << "Missing or Invalid Delay!" << std::endl;
51 return USAGE;
52 }
53 // TODO: Use range validation when supported by AIDL
54 if (auto primitive = args.pop<std::underlying_type_t<decltype(effect.primitive)>>()) {
55 effect.primitive = static_cast<decltype(effect.primitive)>(*primitive);
56 std::cout << "Primitive: " << toString(effect.primitive) << std::endl;
57 } else {
58 std::cerr << "Missing or Invalid Primitive!" << std::endl;
59 return USAGE;
60 }
61 if (auto scale = args.pop<decltype(effect.scale)>();
62 scale && *scale > 0.0 && scale <= 1.0) {
63 effect.scale = *scale;
64 std::cout << "Scale: " << effect.scale << std::endl;
65 } else {
66 std::cerr << "Missing or Invalid Scale!" << std::endl;
67 return USAGE;
68 }
69 mComposite.emplace_back(std::move(effect));
70 }
71 if (!args.empty()) {
72 std::cerr << "Unexpected Arguments!" << std::endl;
73 return USAGE;
74 }
75 return OK;
76 }
77
78 Status doMain(Args && /*args*/) override {
79 std::string statusStr;
80 Status ret;
81 if (auto hal = getHal<aidl::IVibrator>()) {
82 auto status = hal->call(&aidl::IVibrator::compose, mComposite, nullptr);
83 statusStr = status.toString8();
84 ret = status.isOk() ? OK : ERROR;
85 } else {
86 return UNAVAILABLE;
87 }
88
89 std::cout << "Status: " << statusStr << std::endl;
90
91 return ret;
92 }
93
94 std::vector<CompositeEffect> mComposite;
95};
96
97static const auto Command = CommandRegistry<CommandVibrator>::Register<CommandCompose>("compose");
98
99} // namespace vibrator
100} // namespace idlcli
101} // namespace android