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 {
getDescription() const29     std::string getDescription() const override { return "Compose vibration."; }
30 
getUsageSummary() const31     std::string getUsageSummary() const override {
32         return "[options] <delay> <primitive> <scale> ...";
33     }
34 
getUsageDetails() const35     UsageDetails getUsageDetails() const override {
36         UsageDetails details{
37                 {"-b", {"Block for duration of vibration."}},
38                 {"<delay>", {"In milliseconds"}},
39                 {"<primitive>", {"Primitive ID."}},
40                 {"<scale>", {"0.0 (inclusive) - 1.0 (inclusive)."}},
41                 {"...", {"May repeat multiple times."}},
42         };
43         return details;
44     }
45 
doArgs(Args & args)46     Status doArgs(Args &args) override {
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         }
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             }
67             if (auto primitive = args.pop<decltype(effect.primitive)>()) {
68                 effect.primitive = *primitive;
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 
doMain(Args &&)91     Status doMain(Args && /*args*/) override {
92         auto hal = getHal<aidl::IVibrator>();
93 
94         if (!hal) {
95             return UNAVAILABLE;
96         }
97 
98         ABinderProcess_setThreadPoolMaxThreadCount(1);
99         ABinderProcess_startThreadPool();
100 
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;
116     }
117 
118     bool mBlocking;
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
127